Change how DWARF index writer finds address map
[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"
c2a62a3d 43#include "dwarf2/sect-names.h"
2b2558bf 44#include "dwarf2/stringify.h"
70182375 45#include "dwarf2/public.h"
4de283e4
TT
46#include "bfd.h"
47#include "elf-bfd.h"
48#include "symtab.h"
49#include "gdbtypes.h"
50#include "objfiles.h"
d55e5aa6 51#include "dwarf2.h"
4de283e4
TT
52#include "buildsym.h"
53#include "demangle.h"
54#include "gdb-demangle.h"
4de283e4 55#include "filenames.h" /* for DOSish file names */
4de283e4
TT
56#include "language.h"
57#include "complaints.h"
82ca8957
TT
58#include "dwarf2/expr.h"
59#include "dwarf2/loc.h"
4de283e4
TT
60#include "cp-support.h"
61#include "hashtab.h"
62#include "command.h"
d55e5aa6 63#include "gdbcmd.h"
4de283e4
TT
64#include "block.h"
65#include "addrmap.h"
66#include "typeprint.h"
67#include "psympriv.h"
4de283e4 68#include "c-lang.h"
d55e5aa6 69#include "go-lang.h"
4de283e4
TT
70#include "valprint.h"
71#include "gdbcore.h" /* for gnutarget */
72#include "gdb/gdb-index.h"
4de283e4
TT
73#include "gdb_bfd.h"
74#include "f-lang.h"
75#include "source.h"
4de283e4 76#include "build-id.h"
d55e5aa6 77#include "namespace.h"
268a13a5
TT
78#include "gdbsupport/function-view.h"
79#include "gdbsupport/gdb_optional.h"
80#include "gdbsupport/underlying.h"
268a13a5 81#include "gdbsupport/hash_enum.h"
4de283e4 82#include "filename-seen-cache.h"
b32b108a 83#include "producer.h"
4de283e4 84#include <fcntl.h>
4de283e4 85#include <algorithm>
4de283e4 86#include <unordered_map>
268a13a5 87#include "gdbsupport/selftest.h"
c9317f21 88#include "rust-lang.h"
268a13a5 89#include "gdbsupport/pathstuff.h"
edd45eb0 90#include "count-one-bits.h"
437afbb8 91
73be47f5
DE
92/* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
b4f54984
DE
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95static unsigned int dwarf_read_debug = 0;
45cfd468 96
6f738b01
SM
97/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
98
99#define dwarf_read_debug_printf(fmt, ...) \
74b773fc
SM
100 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
101 ##__VA_ARGS__)
6f738b01
SM
102
103/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
104
105#define dwarf_read_debug_printf_v(fmt, ...) \
74b773fc
SM
106 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
6f738b01 108
d97bc12b 109/* When non-zero, dump DIEs after they are read in. */
b4f54984 110static unsigned int dwarf_die_debug = 0;
d97bc12b 111
27e0867f 112/* When non-zero, dump line number entries as they are read in. */
8fdd972c 113unsigned int dwarf_line_debug = 0;
27e0867f 114
491144b5
CB
115/* When true, cross-check physname against demangler. */
116static bool check_physname = false;
900e11f9 117
491144b5
CB
118/* When true, do not reject deprecated .gdb_index sections. */
119static bool use_deprecated_index_sections = false;
481860b3 120
17ee85fc
TT
121/* This is used to store the data that is always per objfile. */
122static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
123
124/* These are used to store the dwarf2_per_bfd objects.
125
126 objfiles having the same BFD, which doesn't require relocations, are going to
127 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
128
129 Other objfiles are not going to share a dwarf2_per_bfd with any other
130 objfiles, so they'll have their own version kept in the _objfile_data_key
131 version. */
132static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
133static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 134
f1e6e072
TT
135/* The "aclass" indices for various kinds of computed DWARF symbols. */
136
137static int dwarf2_locexpr_index;
138static int dwarf2_loclist_index;
139static int dwarf2_locexpr_block_index;
140static int dwarf2_loclist_block_index;
141
41144253 142/* Size of .debug_loclists section header for 32-bit DWARF format. */
143#define LOCLIST_HEADER_SIZE32 12
144
145/* Size of .debug_loclists section header for 64-bit DWARF format. */
146#define LOCLIST_HEADER_SIZE64 20
147
d0ce17d8
CT
148/* Size of .debug_rnglists section header for 32-bit DWARF format. */
149#define RNGLIST_HEADER_SIZE32 12
150
151/* Size of .debug_rnglists section header for 64-bit DWARF format. */
152#define RNGLIST_HEADER_SIZE64 20
153
3f563c84
PA
154/* An index into a (C++) symbol name component in a symbol name as
155 recorded in the mapped_index's symbol table. For each C++ symbol
156 in the symbol table, we record one entry for the start of each
157 component in the symbol in a table of name components, and then
158 sort the table, in order to be able to binary search symbol names,
159 ignoring leading namespaces, both completion and regular look up.
160 For example, for symbol "A::B::C", we'll have an entry that points
161 to "A::B::C", another that points to "B::C", and another for "C".
162 Note that function symbols in GDB index have no parameter
163 information, just the function/method names. You can convert a
164 name_component to a "const char *" using the
165 'mapped_index::symbol_name_at(offset_type)' method. */
166
167struct name_component
168{
169 /* Offset in the symbol name where the component starts. Stored as
170 a (32-bit) offset instead of a pointer to save memory and improve
171 locality on 64-bit architectures. */
172 offset_type name_offset;
173
174 /* The symbol's index in the symbol and constant pool tables of a
175 mapped_index. */
176 offset_type idx;
177};
178
44ed8f3e
PA
179/* Base class containing bits shared by both .gdb_index and
180 .debug_name indexes. */
181
182struct mapped_index_base
183{
22ca247e
TT
184 mapped_index_base () = default;
185 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
186
44ed8f3e
PA
187 /* The name_component table (a sorted vector). See name_component's
188 description above. */
189 std::vector<name_component> name_components;
190
191 /* How NAME_COMPONENTS is sorted. */
192 enum case_sensitivity name_components_casing;
193
194 /* Return the number of names in the symbol table. */
195 virtual size_t symbol_name_count () const = 0;
196
197 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
198 virtual const char *symbol_name_at
199 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
200
201 /* Return whether the name at IDX in the symbol table should be
202 ignored. */
203 virtual bool symbol_name_slot_invalid (offset_type idx) const
204 {
205 return false;
206 }
207
208 /* Build the symbol name component sorted vector, if we haven't
209 yet. */
fcf23d5b 210 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
211
212 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
213 possible matches for LN_NO_PARAMS in the name component
214 vector. */
215 std::pair<std::vector<name_component>::const_iterator,
216 std::vector<name_component>::const_iterator>
3b00ef10 217 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
218 enum language lang,
219 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
220
221 /* Prevent deleting/destroying via a base class pointer. */
222protected:
223 ~mapped_index_base() = default;
224};
225
9291a0cd
TT
226/* A description of the mapped index. The file format is described in
227 a comment by the code that writes the index. */
fc898b42 228struct mapped_index final : public mapped_index_base
9291a0cd 229{
f00a2de2
PA
230 /* A slot/bucket in the symbol table hash. */
231 struct symbol_table_slot
232 {
233 const offset_type name;
234 const offset_type vec;
235 };
236
559a7a62 237 /* Index data format version. */
3063847f 238 int version = 0;
559a7a62 239
f00a2de2
PA
240 /* The address table data. */
241 gdb::array_view<const gdb_byte> address_table;
b11b1f88 242
3876f04e 243 /* The symbol table, implemented as a hash table. */
f00a2de2 244 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 245
9291a0cd 246 /* A pointer to the constant pool. */
3063847f 247 const char *constant_pool = nullptr;
3f563c84 248
44ed8f3e
PA
249 bool symbol_name_slot_invalid (offset_type idx) const override
250 {
251 const auto &bucket = this->symbol_table[idx];
9ab08412 252 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 253 }
5c58de74 254
3f563c84
PA
255 /* Convenience method to get at the name of the symbol at IDX in the
256 symbol table. */
fcf23d5b
SM
257 const char *symbol_name_at
258 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
f00a2de2 259 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 260
44ed8f3e
PA
261 size_t symbol_name_count () const override
262 { return this->symbol_table.size (); }
9291a0cd
TT
263};
264
927aa2e7
JK
265/* A description of the mapped .debug_names.
266 Uninitialized map has CU_COUNT 0. */
fc898b42 267struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
268{
269 bfd_endian dwarf5_byte_order;
270 bool dwarf5_is_dwarf64;
271 bool augmentation_is_gdb;
272 uint8_t offset_size;
273 uint32_t cu_count = 0;
274 uint32_t tu_count, bucket_count, name_count;
275 const gdb_byte *cu_table_reordered, *tu_table_reordered;
276 const uint32_t *bucket_table_reordered, *hash_table_reordered;
277 const gdb_byte *name_table_string_offs_reordered;
278 const gdb_byte *name_table_entry_offs_reordered;
279 const gdb_byte *entry_pool;
280
281 struct index_val
282 {
283 ULONGEST dwarf_tag;
284 struct attr
285 {
286 /* Attribute name DW_IDX_*. */
287 ULONGEST dw_idx;
288
289 /* Attribute form DW_FORM_*. */
290 ULONGEST form;
291
292 /* Value if FORM is DW_FORM_implicit_const. */
293 LONGEST implicit_const;
294 };
295 std::vector<attr> attr_vec;
296 };
297
298 std::unordered_map<ULONGEST, index_val> abbrev_map;
299
fcf23d5b
SM
300 const char *namei_to_name
301 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
302
303 /* Implementation of the mapped_index_base virtual interface, for
304 the name_components cache. */
305
fcf23d5b
SM
306 const char *symbol_name_at
307 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
308 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
309
310 size_t symbol_name_count () const override
311 { return this->name_count; }
927aa2e7
JK
312};
313
cd4fb1b2 314/* See dwarf2read.h. */
ed2dc618 315
cd4fb1b2 316dwarf2_per_objfile *
ed2dc618
SM
317get_dwarf2_per_objfile (struct objfile *objfile)
318{
5bfd760d 319 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 320}
c906108c 321
251d32d9 322/* Default names of the debugging sections. */
c906108c 323
233a11ab
CS
324/* Note that if the debugging section has been compressed, it might
325 have a name like .zdebug_info. */
326
9938d15a 327const struct dwarf2_debug_sections dwarf2_elf_names =
9cdd5dbd 328{
251d32d9
TG
329 { ".debug_info", ".zdebug_info" },
330 { ".debug_abbrev", ".zdebug_abbrev" },
331 { ".debug_line", ".zdebug_line" },
332 { ".debug_loc", ".zdebug_loc" },
43988095 333 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 334 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 335 { ".debug_macro", ".zdebug_macro" },
251d32d9 336 { ".debug_str", ".zdebug_str" },
18a8505e 337 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 338 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 339 { ".debug_ranges", ".zdebug_ranges" },
43988095 340 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 341 { ".debug_types", ".zdebug_types" },
3019eac3 342 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
343 { ".debug_frame", ".zdebug_frame" },
344 { ".eh_frame", NULL },
24d3216f 345 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
346 { ".debug_names", ".zdebug_names" },
347 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 348 23
251d32d9 349};
c906108c 350
80626a55 351/* List of DWO/DWP sections. */
3019eac3 352
80626a55 353static const struct dwop_section_names
3019eac3
DE
354{
355 struct dwarf2_section_names abbrev_dwo;
356 struct dwarf2_section_names info_dwo;
357 struct dwarf2_section_names line_dwo;
358 struct dwarf2_section_names loc_dwo;
43988095 359 struct dwarf2_section_names loclists_dwo;
09262596
DE
360 struct dwarf2_section_names macinfo_dwo;
361 struct dwarf2_section_names macro_dwo;
d0ce17d8 362 struct dwarf2_section_names rnglists_dwo;
3019eac3
DE
363 struct dwarf2_section_names str_dwo;
364 struct dwarf2_section_names str_offsets_dwo;
365 struct dwarf2_section_names types_dwo;
80626a55
DE
366 struct dwarf2_section_names cu_index;
367 struct dwarf2_section_names tu_index;
3019eac3 368}
80626a55 369dwop_section_names =
3019eac3
DE
370{
371 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
372 { ".debug_info.dwo", ".zdebug_info.dwo" },
373 { ".debug_line.dwo", ".zdebug_line.dwo" },
374 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 375 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
376 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
377 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
d0ce17d8 378 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
3019eac3
DE
379 { ".debug_str.dwo", ".zdebug_str.dwo" },
380 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
381 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
382 { ".debug_cu_index", ".zdebug_cu_index" },
383 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
384};
385
c906108c
SS
386/* local data types */
387
d0ce17d8
CT
388/* The location list and range list sections (.debug_loclists & .debug_rnglists)
389 begin with a header, which contains the following information. */
390struct loclists_rnglists_header
41144253 391{
392 /* A 4-byte or 12-byte length containing the length of the
393 set of entries for this compilation unit, not including the
394 length field itself. */
395 unsigned int length;
396
397 /* A 2-byte version identifier. */
398 short version;
399
400 /* A 1-byte unsigned integer containing the size in bytes of an address on
401 the target system. */
402 unsigned char addr_size;
403
404 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
405 on the target system. */
406 unsigned char segment_collector_size;
407
408 /* A 4-byte count of the number of offsets that follow the header. */
409 unsigned int offset_entry_count;
410};
411
3da10d80
KS
412/* Type used for delaying computation of method physnames.
413 See comments for compute_delayed_physnames. */
414struct delayed_method_info
415{
416 /* The type to which the method is attached, i.e., its parent class. */
417 struct type *type;
418
419 /* The index of the method in the type's function fieldlists. */
420 int fnfield_index;
421
422 /* The index of the method in the fieldlist. */
423 int index;
424
425 /* The name of the DIE. */
426 const char *name;
427
428 /* The DIE associated with this method. */
429 struct die_info *die;
430};
431
e7c27a73
DJ
432/* Internal state when decoding a particular compilation unit. */
433struct dwarf2_cu
434{
9e021579
SM
435 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
436 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
437
438 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
439
c24bdb02
KS
440 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
441 Create the set of symtabs used by this TU, or if this TU is sharing
442 symtabs with another TU and the symtabs have already been created
443 then restore those symtabs in the line header.
444 We don't need the pc/line-number mapping for type units. */
445 void setup_type_unit_groups (struct die_info *die);
446
447 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
448 buildsym_compunit constructor. */
449 struct compunit_symtab *start_symtab (const char *name,
450 const char *comp_dir,
451 CORE_ADDR low_pc);
452
453 /* Reset the builder. */
454 void reset_builder () { m_builder.reset (); }
455
293e7e51
SM
456 /* Return a type that is a generic pointer type, the size of which
457 matches the address size given in the compilation unit header for
458 this CU. */
459 struct type *addr_type () const;
460
461 /* Find an integer type the same size as the address size given in
462 the compilation unit header for this CU. UNSIGNED_P controls if
463 the integer is unsigned or not. */
464 struct type *addr_sized_int_type (bool unsigned_p) const;
465
d00adf39 466 /* The header of the compilation unit. */
fcd3b13d 467 struct comp_unit_head header {};
e142c38c 468
d00adf39 469 /* Base address of this compilation unit. */
2b24b6e4 470 gdb::optional<CORE_ADDR> base_address;
d00adf39 471
e142c38c 472 /* The language we are debugging. */
fcd3b13d
SM
473 enum language language = language_unknown;
474 const struct language_defn *language_defn = nullptr;
e142c38c 475
fcd3b13d 476 const char *producer = nullptr;
b0f35d58 477
c24bdb02 478private:
804d2729
TT
479 /* The symtab builder for this CU. This is only non-NULL when full
480 symbols are being read. */
c24bdb02 481 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 482
c24bdb02 483public:
e142c38c
DJ
484 /* The generic symbol table building routines have separate lists for
485 file scope symbols and all all other scopes (local scopes). So
486 we need to select the right one to pass to add_symbol_to_list().
487 We do it by keeping a pointer to the correct list in list_in_scope.
488
489 FIXME: The original dwarf code just treated the file scope as the
490 first local scope, and all other local scopes as nested local
491 scopes, and worked fine. Check to see if we really need to
492 distinguish these in buildsym.c. */
fcd3b13d 493 struct pending **list_in_scope = nullptr;
e142c38c 494
b64f50a1
JK
495 /* Hash table holding all the loaded partial DIEs
496 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 497 htab_t partial_dies = nullptr;
72bf9492
DJ
498
499 /* Storage for things with the same lifetime as this read-in compilation
500 unit, including partial DIEs. */
fcd3b13d 501 auto_obstack comp_unit_obstack;
72bf9492 502
69d751e3 503 /* Backlink to our per_cu entry. */
ae038cb0
DJ
504 struct dwarf2_per_cu_data *per_cu;
505
9e021579 506 /* The dwarf2_per_objfile that owns this. */
976ca316 507 dwarf2_per_objfile *per_objfile;
9e021579 508
ae038cb0 509 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 510 int last_used = 0;
ae038cb0 511
b64f50a1
JK
512 /* A hash table of DIE cu_offset for following references with
513 die_info->offset.sect_off as hash. */
fcd3b13d 514 htab_t die_hash = nullptr;
10b3939b
DJ
515
516 /* Full DIEs if read in. */
fcd3b13d 517 struct die_info *dies = nullptr;
10b3939b
DJ
518
519 /* A set of pointers to dwarf2_per_cu_data objects for compilation
520 units referenced by this one. Only set during full symbol processing;
521 partial symbol tables do not have dependencies. */
fcd3b13d 522 htab_t dependencies = nullptr;
10b3939b 523
cb1df416 524 /* Header data from the line table, during full symbol processing. */
fcd3b13d 525 struct line_header *line_header = nullptr;
4c8aa72d 526 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 527 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
528 this is the DW_TAG_compile_unit die for this CU. We'll hold on
529 to the line header as long as this DIE is being processed. See
530 process_die_scope. */
fcd3b13d 531 die_info *line_header_die_owner = nullptr;
cb1df416 532
3da10d80
KS
533 /* A list of methods which need to have physnames computed
534 after all type information has been read. */
c89b44cd 535 std::vector<delayed_method_info> method_list;
3da10d80 536
96408a79 537 /* To be copied to symtab->call_site_htab. */
fcd3b13d 538 htab_t call_site_htab = nullptr;
96408a79 539
034e5797
DE
540 /* Non-NULL if this CU came from a DWO file.
541 There is an invariant here that is important to remember:
542 Except for attributes copied from the top level DIE in the "main"
543 (or "stub") file in preparation for reading the DWO file
18a8505e 544 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
545 Either there isn't a DWO file (in which case this is NULL and the point
546 is moot), or there is and either we're not going to read it (in which
547 case this is NULL) or there is and we are reading it (in which case this
548 is non-NULL). */
fcd3b13d 549 struct dwo_unit *dwo_unit = nullptr;
3019eac3 550
18a8505e 551 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 552 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 553 gdb::optional<ULONGEST> addr_base;
3019eac3 554
2b0c7f41
SM
555 /* The DW_AT_GNU_ranges_base attribute, if present.
556
557 This is only relevant in the context of pre-DWARF 5 split units. In this
558 context, there is a .debug_ranges section in the linked executable,
559 containing all the ranges data for all the compilation units. Each
560 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
561 indicates the base of its contribution to that section. The DW_AT_ranges
562 attributes in the split-unit are of the form DW_FORM_sec_offset and point
563 into the .debug_ranges section of the linked file. However, they are not
564 "true" DW_FORM_sec_offset, because they are relative to the base of their
565 compilation unit's contribution, rather than relative to the beginning of
566 the section. The DW_AT_GNU_ranges_base value must be added to it to make
567 it relative to the beginning of the section.
568
569 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
570 case, so this value can be added without needing to know whether we are in
571 this case or not.
572
573 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
574 skeleton/stub, it must not have the base added, as it already points to the
575 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
576 have a DW_AT_ranges attribute, we can use the
577
578 die->tag != DW_AT_compile_unit
579
580 to determine whether the base should be added or not. */
581 ULONGEST gnu_ranges_base = 0;
582
583 /* The DW_AT_rnglists_base attribute, if present.
584
585 This is used when processing attributes of form DW_FORM_rnglistx in
586 non-split units. Attributes of this form found in a split unit don't
587 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
588 section. */
589 ULONGEST rnglists_base = 0;
2e3cf129 590
41144253 591 /* The DW_AT_loclists_base attribute if present. */
592 ULONGEST loclist_base = 0;
593
c9317f21
TT
594 /* When reading debug info generated by older versions of rustc, we
595 have to rewrite some union types to be struct types with a
596 variant part. This rewriting must be done after the CU is fully
597 read in, because otherwise at the point of rewriting some struct
598 type might not have been fully processed. So, we keep a list of
599 all such types here and process them after expansion. */
600 std::vector<struct type *> rust_unions;
601
18a8505e
AT
602 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
603 files, the value is implicitly zero. For DWARF 5 version DWO files, the
604 value is often implicit and is the size of the header of
605 .debug_str_offsets section (8 or 4, depending on the address size). */
606 gdb::optional<ULONGEST> str_offsets_base;
607
ae038cb0 608 /* Mark used when releasing cached dies. */
9068261f 609 bool mark : 1;
ae038cb0 610
8be455d7
JK
611 /* This CU references .debug_loc. See the symtab->locations_valid field.
612 This test is imperfect as there may exist optimized debug code not using
613 any location list and still facing inlining issues if handled as
614 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 615 bool has_loclist : 1;
ba919b58 616
9068261f 617 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
618 if all the producer_is_* fields are valid. This information is cached
619 because profiling CU expansion showed excessive time spent in
620 producer_is_gxx_lt_4_6. */
9068261f
AB
621 bool checked_producer : 1;
622 bool producer_is_gxx_lt_4_6 : 1;
623 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 624 bool producer_is_icc : 1;
9068261f 625 bool producer_is_icc_lt_14 : 1;
c258c396 626 bool producer_is_codewarrior : 1;
4d4ec4e5 627
9068261f 628 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
629 debugging info for C++ namespaces. GCC 3.3.x did not produce
630 this information, but later versions do. */
631
9068261f 632 bool processing_has_namespace_info : 1;
d590ff25
YQ
633
634 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
635
636 /* If this CU was inherited by another CU (via specification,
637 abstract_origin, etc), this is the ancestor CU. */
638 dwarf2_cu *ancestor;
639
640 /* Get the buildsym_compunit for this CU. */
641 buildsym_compunit *get_builder ()
642 {
643 /* If this CU has a builder associated with it, use that. */
644 if (m_builder != nullptr)
645 return m_builder.get ();
646
647 /* Otherwise, search ancestors for a valid builder. */
648 if (ancestor != nullptr)
649 return ancestor->get_builder ();
650
651 return nullptr;
652 }
e7c27a73
DJ
653};
654
094b34ac
DE
655/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
656 This includes type_unit_group and quick_file_names. */
657
658struct stmt_list_hash
659{
660 /* The DWO unit this table is from or NULL if there is none. */
661 struct dwo_unit *dwo_unit;
662
663 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 664 sect_offset line_sect_off;
094b34ac
DE
665};
666
5989a64e 667/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
668 an object of this type. This contains elements of type unit groups
669 that can be shared across objfiles. The non-shareable parts are in
670 type_unit_group_unshareable. */
f4dc4d17
DE
671
672struct type_unit_group
673{
0186c6a7 674 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
675 To simplify things we create an artificial CU that "includes" all the
676 type units using this stmt_list so that the rest of the code still has
197400e8 677 a "per_cu" handle on the symtab. */
094b34ac
DE
678 struct dwarf2_per_cu_data per_cu;
679
0186c6a7
DE
680 /* The TUs that share this DW_AT_stmt_list entry.
681 This is added to while parsing type units to build partial symtabs,
682 and is deleted afterwards and not used again. */
a8b3b8e9 683 std::vector<signatured_type *> *tus;
f4dc4d17 684
094b34ac
DE
685 /* The data used to construct the hash key. */
686 struct stmt_list_hash hash;
f4dc4d17
DE
687};
688
73869dc2 689/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
690
691struct dwo_sections
692{
693 struct dwarf2_section_info abbrev;
3019eac3
DE
694 struct dwarf2_section_info line;
695 struct dwarf2_section_info loc;
43988095 696 struct dwarf2_section_info loclists;
09262596
DE
697 struct dwarf2_section_info macinfo;
698 struct dwarf2_section_info macro;
d0ce17d8 699 struct dwarf2_section_info rnglists;
3019eac3
DE
700 struct dwarf2_section_info str;
701 struct dwarf2_section_info str_offsets;
80626a55
DE
702 /* In the case of a virtual DWO file, these two are unused. */
703 struct dwarf2_section_info info;
fd5866f6 704 std::vector<dwarf2_section_info> types;
3019eac3
DE
705};
706
c88ee1f0 707/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
708
709struct dwo_unit
710{
711 /* Backlink to the containing struct dwo_file. */
712 struct dwo_file *dwo_file;
713
714 /* The "id" that distinguishes this CU/TU.
715 .debug_info calls this "dwo_id", .debug_types calls this "signature".
716 Since signatures came first, we stick with it for consistency. */
717 ULONGEST signature;
718
719 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 720 struct dwarf2_section_info *section;
3019eac3 721
9c541725
PA
722 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
723 sect_offset sect_off;
3019eac3
DE
724 unsigned int length;
725
726 /* For types, offset in the type's DIE of the type defined by this TU. */
727 cu_offset type_offset_in_tu;
728};
729
73869dc2
DE
730/* include/dwarf2.h defines the DWP section codes.
731 It defines a max value but it doesn't define a min value, which we
732 use for error checking, so provide one. */
733
734enum dwp_v2_section_ids
735{
736 DW_SECT_MIN = 1
737};
738
80626a55 739/* Data for one DWO file.
57d63ce2
DE
740
741 This includes virtual DWO files (a virtual DWO file is a DWO file as it
742 appears in a DWP file). DWP files don't really have DWO files per se -
743 comdat folding of types "loses" the DWO file they came from, and from
744 a high level view DWP files appear to contain a mass of random types.
745 However, to maintain consistency with the non-DWP case we pretend DWP
746 files contain virtual DWO files, and we assign each TU with one virtual
747 DWO file (generally based on the line and abbrev section offsets -
748 a heuristic that seems to work in practice). */
3019eac3
DE
749
750struct dwo_file
751{
51ac9db5
SM
752 dwo_file () = default;
753 DISABLE_COPY_AND_ASSIGN (dwo_file);
754
18a8505e 755 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
756 For virtual DWO files the name is constructed from the section offsets
757 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
758 from related CU+TUs. */
51ac9db5 759 const char *dwo_name = nullptr;
0ac5b59e
DE
760
761 /* The DW_AT_comp_dir attribute. */
51ac9db5 762 const char *comp_dir = nullptr;
3019eac3 763
80626a55
DE
764 /* The bfd, when the file is open. Otherwise this is NULL.
765 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 766 gdb_bfd_ref_ptr dbfd;
3019eac3 767
73869dc2 768 /* The sections that make up this DWO file.
d2854d8d 769 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 770 sections (for lack of a better name). */
51ac9db5 771 struct dwo_sections sections {};
3019eac3 772
33c5cd75
DB
773 /* The CUs in the file.
774 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
775 an extension to handle LLVM's Link Time Optimization output (where
776 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 777 htab_up cus;
3019eac3
DE
778
779 /* Table of TUs in the file.
780 Each element is a struct dwo_unit. */
b0b6a987 781 htab_up tus;
3019eac3
DE
782};
783
80626a55
DE
784/* These sections are what may appear in a DWP file. */
785
786struct dwp_sections
787{
d2854d8d 788 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
73869dc2 792
d2854d8d 793 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
794 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
795 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
796 In DWP version 2 or 5 there is at most one copy of all these sections,
797 each section being (effectively) comprised of the concatenation of all of
798 the individual sections that exist in the version 1 format.
73869dc2
DE
799 To keep the code simple we treat each of these concatenated pieces as a
800 section itself (a virtual section?). */
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info info;
803 struct dwarf2_section_info line;
804 struct dwarf2_section_info loc;
d2854d8d 805 struct dwarf2_section_info loclists;
73869dc2
DE
806 struct dwarf2_section_info macinfo;
807 struct dwarf2_section_info macro;
d2854d8d 808 struct dwarf2_section_info rnglists;
73869dc2
DE
809 struct dwarf2_section_info str_offsets;
810 struct dwarf2_section_info types;
80626a55
DE
811};
812
73869dc2
DE
813/* These sections are what may appear in a virtual DWO file in DWP version 1.
814 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 815
73869dc2 816struct virtual_v1_dwo_sections
80626a55
DE
817{
818 struct dwarf2_section_info abbrev;
819 struct dwarf2_section_info line;
820 struct dwarf2_section_info loc;
821 struct dwarf2_section_info macinfo;
822 struct dwarf2_section_info macro;
823 struct dwarf2_section_info str_offsets;
824 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 825 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
826 struct dwarf2_section_info info_or_types;
827};
828
d2854d8d 829/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
830 In version 2, the sections of the DWO files are concatenated together
831 and stored in one section of that name. Thus each ELF section contains
832 several "virtual" sections. */
833
d2854d8d 834struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
835{
836 bfd_size_type abbrev_offset;
837 bfd_size_type abbrev_size;
838
839 bfd_size_type line_offset;
840 bfd_size_type line_size;
841
842 bfd_size_type loc_offset;
843 bfd_size_type loc_size;
844
d2854d8d
CT
845 bfd_size_type loclists_offset;
846 bfd_size_type loclists_size;
847
73869dc2
DE
848 bfd_size_type macinfo_offset;
849 bfd_size_type macinfo_size;
850
851 bfd_size_type macro_offset;
852 bfd_size_type macro_size;
853
d2854d8d
CT
854 bfd_size_type rnglists_offset;
855 bfd_size_type rnglists_size;
856
73869dc2
DE
857 bfd_size_type str_offsets_offset;
858 bfd_size_type str_offsets_size;
859
860 /* Each DWP hash table entry records one CU or one TU.
861 That is recorded here, and copied to dwo_unit.section. */
862 bfd_size_type info_or_types_offset;
863 bfd_size_type info_or_types_size;
864};
865
80626a55
DE
866/* Contents of DWP hash tables. */
867
868struct dwp_hash_table
869{
73869dc2 870 uint32_t version, nr_columns;
80626a55 871 uint32_t nr_units, nr_slots;
73869dc2
DE
872 const gdb_byte *hash_table, *unit_table;
873 union
874 {
875 struct
876 {
877 const gdb_byte *indices;
878 } v1;
879 struct
880 {
881 /* This is indexed by column number and gives the id of the section
882 in that column. */
883#define MAX_NR_V2_DWO_SECTIONS \
884 (1 /* .debug_info or .debug_types */ \
885 + 1 /* .debug_abbrev */ \
886 + 1 /* .debug_line */ \
887 + 1 /* .debug_loc */ \
888 + 1 /* .debug_str_offsets */ \
889 + 1 /* .debug_macro or .debug_macinfo */)
890 int section_ids[MAX_NR_V2_DWO_SECTIONS];
891 const gdb_byte *offsets;
892 const gdb_byte *sizes;
893 } v2;
d2854d8d
CT
894 struct
895 {
896 /* This is indexed by column number and gives the id of the section
897 in that column. */
898#define MAX_NR_V5_DWO_SECTIONS \
899 (1 /* .debug_info */ \
900 + 1 /* .debug_abbrev */ \
901 + 1 /* .debug_line */ \
902 + 1 /* .debug_loclists */ \
903 + 1 /* .debug_str_offsets */ \
904 + 1 /* .debug_macro */ \
905 + 1 /* .debug_rnglists */)
906 int section_ids[MAX_NR_V5_DWO_SECTIONS];
907 const gdb_byte *offsets;
908 const gdb_byte *sizes;
909 } v5;
73869dc2 910 } section_pool;
80626a55
DE
911};
912
913/* Data for one DWP file. */
914
915struct dwp_file
916{
400174b1
TT
917 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
918 : name (name_),
919 dbfd (std::move (abfd))
920 {
921 }
922
80626a55
DE
923 /* Name of the file. */
924 const char *name;
925
73869dc2 926 /* File format version. */
400174b1 927 int version = 0;
73869dc2 928
93417882 929 /* The bfd. */
400174b1 930 gdb_bfd_ref_ptr dbfd;
80626a55
DE
931
932 /* Section info for this file. */
400174b1 933 struct dwp_sections sections {};
80626a55 934
57d63ce2 935 /* Table of CUs in the file. */
400174b1 936 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
937
938 /* Table of TUs in the file. */
400174b1 939 const struct dwp_hash_table *tus = nullptr;
80626a55 940
19ac8c2e 941 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
942 htab_up loaded_cus;
943 htab_up loaded_tus;
80626a55 944
73869dc2
DE
945 /* Table to map ELF section numbers to their sections.
946 This is only needed for the DWP V1 file format. */
400174b1
TT
947 unsigned int num_sections = 0;
948 asection **elf_sections = nullptr;
80626a55
DE
949};
950
0963b4bd
MS
951/* Struct used to pass misc. parameters to read_die_and_children, et
952 al. which are used for both .debug_info and .debug_types dies.
953 All parameters here are unchanging for the life of the call. This
dee91e82 954 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
955
956struct die_reader_specs
957{
a32a8923 958 /* The bfd of die_section. */
0280fdcc 959 bfd *abfd;
93311388
DE
960
961 /* The CU of the DIE we are parsing. */
962 struct dwarf2_cu *cu;
963
80626a55 964 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
965 struct dwo_file *dwo_file;
966
dee91e82 967 /* The section the die comes from.
3019eac3 968 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
969 struct dwarf2_section_info *die_section;
970
971 /* die_section->buffer. */
d521ce57 972 const gdb_byte *buffer;
f664829e
DE
973
974 /* The end of the buffer. */
975 const gdb_byte *buffer_end;
a2ce51a0 976
685af9cd
TT
977 /* The abbreviation table to use when reading the DIEs. */
978 struct abbrev_table *abbrev_table;
93311388
DE
979};
980
c0ab21c2
TT
981/* A subclass of die_reader_specs that holds storage and has complex
982 constructor and destructor behavior. */
983
984class cutu_reader : public die_reader_specs
985{
986public:
987
ab432490
SM
988 cutu_reader (dwarf2_per_cu_data *this_cu,
989 dwarf2_per_objfile *per_objfile,
c0ab21c2 990 struct abbrev_table *abbrev_table,
2e671100 991 dwarf2_cu *existing_cu,
c0ab21c2
TT
992 bool skip_partial);
993
994 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 995 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
996 struct dwarf2_cu *parent_cu = nullptr,
997 struct dwo_file *dwo_file = nullptr);
998
c0ab21c2
TT
999 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1000
1001 const gdb_byte *info_ptr = nullptr;
1002 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
1003 bool dummy_p = false;
1004
6751ebae
TT
1005 /* Release the new CU, putting it on the chain. This cannot be done
1006 for dummy CUs. */
1007 void keep ();
1008
c0ab21c2 1009private:
9e021579
SM
1010 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1011 dwarf2_per_objfile *per_objfile,
2e671100 1012 dwarf2_cu *existing_cu);
c0ab21c2
TT
1013
1014 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
1015 std::unique_ptr<dwarf2_cu> m_new_cu;
1016
1017 /* The ordinary abbreviation table. */
1018 abbrev_table_up m_abbrev_table_holder;
1019
1020 /* The DWO abbreviation table. */
1021 abbrev_table_up m_dwo_abbrev_table;
1022};
dee91e82 1023
c906108c 1024/* When we construct a partial symbol table entry we only
0963b4bd 1025 need this much information. */
6f06d47b 1026struct partial_die_info : public allocate_on_obstack
c906108c 1027 {
7c32eebb 1028 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
6f06d47b
YQ
1029
1030 /* Disable assign but still keep copy ctor, which is needed
1031 load_partial_dies. */
1032 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1033
52356b79
YQ
1034 /* Adjust the partial die before generating a symbol for it. This
1035 function may set the is_external flag or change the DIE's
1036 name. */
1037 void fixup (struct dwarf2_cu *cu);
1038
48fbe735
YQ
1039 /* Read a minimal amount of information into the minimal die
1040 structure. */
1041 const gdb_byte *read (const struct die_reader_specs *reader,
1042 const struct abbrev_info &abbrev,
1043 const gdb_byte *info_ptr);
1044
7d00ffec
TT
1045 /* Compute the name of this partial DIE. This memoizes the
1046 result, so it is safe to call multiple times. */
1047 const char *name (dwarf2_cu *cu);
1048
72bf9492 1049 /* Offset of this DIE. */
6f06d47b 1050 const sect_offset sect_off;
72bf9492
DJ
1051
1052 /* DWARF-2 tag for this DIE. */
6f06d47b 1053 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1054
72bf9492 1055 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1056 const unsigned int has_children : 1;
1057
72bf9492
DJ
1058 unsigned int is_external : 1;
1059 unsigned int is_declaration : 1;
1060 unsigned int has_type : 1;
1061 unsigned int has_specification : 1;
1062 unsigned int has_pc_info : 1;
481860b3 1063 unsigned int may_be_inlined : 1;
72bf9492 1064
0c1b455e
TT
1065 /* This DIE has been marked DW_AT_main_subprogram. */
1066 unsigned int main_subprogram : 1;
1067
72bf9492
DJ
1068 /* Flag set if the SCOPE field of this structure has been
1069 computed. */
1070 unsigned int scope_set : 1;
1071
fa4028e9
JB
1072 /* Flag set if the DIE has a byte_size attribute. */
1073 unsigned int has_byte_size : 1;
1074
ff908ebf
AW
1075 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1076 unsigned int has_const_value : 1;
1077
98bfdba5
PA
1078 /* Flag set if any of the DIE's children are template arguments. */
1079 unsigned int has_template_arguments : 1;
1080
52356b79 1081 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1082 unsigned int fixup_called : 1;
1083
36586728
TT
1084 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1085 unsigned int is_dwz : 1;
1086
1087 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1088 unsigned int spec_is_dwz : 1;
1089
7d00ffec
TT
1090 unsigned int canonical_name : 1;
1091
72bf9492 1092 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1093 sometimes a default name for unnamed DIEs. */
7d00ffec 1094 const char *raw_name = nullptr;
72bf9492 1095
abc72ce4 1096 /* The linkage name, if present. */
6f06d47b 1097 const char *linkage_name = nullptr;
abc72ce4 1098
72bf9492
DJ
1099 /* The scope to prepend to our children. This is generally
1100 allocated on the comp_unit_obstack, so will disappear
1101 when this compilation unit leaves the cache. */
6f06d47b 1102 const char *scope = nullptr;
72bf9492 1103
95554aad
TT
1104 /* Some data associated with the partial DIE. The tag determines
1105 which field is live. */
1106 union
1107 {
1108 /* The location description associated with this DIE, if any. */
1109 struct dwarf_block *locdesc;
1110 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1111 sect_offset sect_off;
6f06d47b 1112 } d {};
72bf9492
DJ
1113
1114 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1115 CORE_ADDR lowpc = 0;
1116 CORE_ADDR highpc = 0;
72bf9492 1117
93311388 1118 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1119 DW_AT_sibling, if any. */
48fbe735
YQ
1120 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1121 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1122 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1123
1124 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1125 DW_AT_specification (or DW_AT_abstract_origin or
1126 DW_AT_extension). */
6f06d47b 1127 sect_offset spec_offset {};
72bf9492
DJ
1128
1129 /* Pointers to this DIE's parent, first child, and next sibling,
1130 if any. */
6f06d47b
YQ
1131 struct partial_die_info *die_parent = nullptr;
1132 struct partial_die_info *die_child = nullptr;
1133 struct partial_die_info *die_sibling = nullptr;
1134
1135 friend struct partial_die_info *
1136 dwarf2_cu::find_partial_die (sect_offset sect_off);
1137
1138 private:
1139 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1140 partial_die_info (sect_offset sect_off)
1141 : partial_die_info (sect_off, DW_TAG_padding, 0)
1142 {
1143 }
1144
1145 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1146 int has_children_)
1147 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1148 {
1149 is_external = 0;
1150 is_declaration = 0;
1151 has_type = 0;
1152 has_specification = 0;
1153 has_pc_info = 0;
1154 may_be_inlined = 0;
1155 main_subprogram = 0;
1156 scope_set = 0;
1157 has_byte_size = 0;
1158 has_const_value = 0;
1159 has_template_arguments = 0;
1160 fixup_called = 0;
1161 is_dwz = 0;
1162 spec_is_dwz = 0;
7d00ffec 1163 canonical_name = 0;
6f06d47b 1164 }
c906108c
SS
1165 };
1166
c906108c
SS
1167/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1168 but this would require a corresponding change in unpack_field_as_long
1169 and friends. */
1170static int bits_per_byte = 8;
1171
9c6a1327
TT
1172struct variant_part_builder;
1173
1174/* When reading a variant, we track a bit more information about the
1175 field, and store it in an object of this type. */
2ddeaf8a
TT
1176
1177struct variant_field
1178{
9c6a1327
TT
1179 int first_field = -1;
1180 int last_field = -1;
1181
1182 /* A variant can contain other variant parts. */
1183 std::vector<variant_part_builder> variant_parts;
1184
2ddeaf8a
TT
1185 /* If we see a DW_TAG_variant, then this will be set if this is the
1186 default branch. */
9c6a1327
TT
1187 bool default_branch = false;
1188 /* If we see a DW_AT_discr_value, then this will be the discriminant
1189 value. */
1190 ULONGEST discriminant_value = 0;
1191 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1192 data. */
1193 struct dwarf_block *discr_list_data = nullptr;
1194};
1195
1196/* This represents a DW_TAG_variant_part. */
1197
1198struct variant_part_builder
1199{
1200 /* The offset of the discriminant field. */
1201 sect_offset discriminant_offset {};
1202
1203 /* Variants that are direct children of this variant part. */
1204 std::vector<variant_field> variants;
1205
1206 /* True if we're currently reading a variant. */
1207 bool processing_variant = false;
2ddeaf8a
TT
1208};
1209
52059ffd
TT
1210struct nextfield
1211{
be2daae6
TT
1212 int accessibility = 0;
1213 int virtuality = 0;
9c6a1327
TT
1214 /* Variant parts need to find the discriminant, which is a DIE
1215 reference. We track the section offset of each field to make
1216 this link. */
1217 sect_offset offset;
be2daae6 1218 struct field field {};
52059ffd
TT
1219};
1220
1221struct fnfieldlist
1222{
be2daae6
TT
1223 const char *name = nullptr;
1224 std::vector<struct fn_field> fnfields;
52059ffd
TT
1225};
1226
c906108c
SS
1227/* The routines that read and process dies for a C struct or C++ class
1228 pass lists of data member fields and lists of member function fields
1229 in an instance of a field_info structure, as defined below. */
1230struct field_info
2de01bdb
SM
1231{
1232 /* List of data member and baseclasses fields. */
1233 std::vector<struct nextfield> fields;
1234 std::vector<struct nextfield> baseclasses;
1235
1236 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1237 bool non_public_fields = false;
2de01bdb
SM
1238
1239 /* Member function fieldlist array, contains name of possibly overloaded
1240 member function, number of overloaded member functions and a pointer
1241 to the head of the member function field chain. */
1242 std::vector<struct fnfieldlist> fnfieldlists;
1243
1244 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1245 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1246 std::vector<struct decl_field> typedef_field_list;
1247
1248 /* Nested types defined by this class and the number of elements in this
1249 list. */
1250 std::vector<struct decl_field> nested_types_list;
1251
1252 /* If non-null, this is the variant part we are currently
1253 reading. */
1254 variant_part_builder *current_variant_part = nullptr;
1255 /* This holds all the top-level variant parts attached to the type
1256 we're reading. */
1257 std::vector<variant_part_builder> variant_parts;
1258
1259 /* Return the total number of fields (including baseclasses). */
1260 int nfields () const
c5aa993b 1261 {
2de01bdb
SM
1262 return fields.size () + baseclasses.size ();
1263 }
1264};
c906108c 1265
ae038cb0
DJ
1266/* Loaded secondary compilation units are kept in memory until they
1267 have not been referenced for the processing of this many
1268 compilation units. Set this to zero to disable caching. Cache
1269 sizes of up to at least twenty will improve startup time for
1270 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1271static int dwarf_max_cache_age = 5;
920d2a44 1272static void
b4f54984
DE
1273show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1274 struct cmd_list_element *c, const char *value)
920d2a44 1275{
3e43a32a 1276 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1277 "DWARF compilation units is %s.\n"),
920d2a44
AC
1278 value);
1279}
4390d890 1280\f
c906108c
SS
1281/* local function prototypes */
1282
918dd910
JK
1283static void dwarf2_find_base_address (struct die_info *die,
1284 struct dwarf2_cu *cu);
1285
891813be 1286static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1287 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1288 const char *name);
0018ea6f 1289
f1902523
JK
1290static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1291 const gdb_byte *info_ptr,
3e225074 1292 struct die_info *type_unit_die);
f1902523 1293
976ca316 1294static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1295
72bf9492
DJ
1296static void scan_partial_symbols (struct partial_die_info *,
1297 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1298 int, struct dwarf2_cu *);
c906108c 1299
72bf9492
DJ
1300static void add_partial_symbol (struct partial_die_info *,
1301 struct dwarf2_cu *);
63d06c5c 1302
72bf9492
DJ
1303static void add_partial_namespace (struct partial_die_info *pdi,
1304 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1305 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1306
5d7cb8df 1307static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1308 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1309 struct dwarf2_cu *cu);
1310
72bf9492
DJ
1311static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1312 struct dwarf2_cu *cu);
91c24f0a 1313
bc30ff58
JB
1314static void add_partial_subprogram (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1316 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1317
d521ce57 1318static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1319
dee91e82 1320static struct partial_die_info *load_partial_dies
d521ce57 1321 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1322
fb816e8b
TV
1323/* A pair of partial_die_info and compilation unit. */
1324struct cu_partial_die_info
1325{
1326 /* The compilation unit of the partial_die_info. */
1327 struct dwarf2_cu *cu;
1328 /* A partial_die_info. */
1329 struct partial_die_info *pdi;
122cf0f2
AB
1330
1331 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1332 : cu (cu),
1333 pdi (pdi)
405feb71 1334 { /* Nothing. */ }
122cf0f2
AB
1335
1336private:
1337 cu_partial_die_info () = delete;
fb816e8b
TV
1338};
1339
122cf0f2
AB
1340static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1341 struct dwarf2_cu *);
72bf9492 1342
d521ce57 1343static const gdb_byte *read_attribute (const struct die_reader_specs *,
4444f407
TT
1344 struct attribute *,
1345 const struct attr_abbrev *,
7a5f294d 1346 const gdb_byte *);
18a8505e
AT
1347
1348static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1349 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1350
1351static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1352
976ca316
SM
1353static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1354 dwarf2_section_info *, sect_offset);
f4dc4d17 1355
ed2dc618 1356static const char *read_indirect_string
976ca316 1357 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1358 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1359
ed2dc618 1360static const char *read_indirect_string_at_offset
976ca316 1361 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1362
d521ce57
TT
1363static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1364 const gdb_byte *,
3019eac3
DE
1365 unsigned int *);
1366
18a8505e
AT
1367static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1368 ULONGEST str_index);
1369
1370static const char *read_stub_str_index (struct dwarf2_cu *cu,
1371 ULONGEST str_index);
3019eac3 1372
e142c38c 1373static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1374
e142c38c
DJ
1375static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1376 struct dwarf2_cu *);
c906108c 1377
7d45c7c3 1378static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1379 struct dwarf2_cu *cu);
7d45c7c3 1380
a084a2a6
AT
1381static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1382
05cf31d1 1383static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1384 struct dwarf2_cu *cu);
05cf31d1 1385
e142c38c 1386static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1387
e142c38c 1388static struct die_info *die_specification (struct die_info *die,
f2f0e013 1389 struct dwarf2_cu **);
63d06c5c 1390
9c541725 1391static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1392 struct dwarf2_cu *cu);
debd256d 1393
f3f5162e 1394static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1395 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1396 CORE_ADDR, int decode_mapping);
c906108c 1397
804d2729
TT
1398static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1399 const char *);
c906108c 1400
a14ed312 1401static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1402 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1403
ff39bb5e 1404static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1405 struct dwarf2_cu *);
c906108c 1406
ff39bb5e 1407static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
12df843f 1411 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1412 const gdb_byte **bytes,
98bfdba5 1413 struct dwarf2_locexpr_baton **baton);
2df3850c 1414
57567375
TT
1415static struct type *read_subrange_index_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
e7c27a73 1418static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1419
b4ba55a1
JB
1420static int need_gnat_info (struct dwarf2_cu *);
1421
3e43a32a
MS
1422static struct type *die_descriptive_type (struct die_info *,
1423 struct dwarf2_cu *);
b4ba55a1
JB
1424
1425static void set_descriptive_type (struct type *, struct die_info *,
1426 struct dwarf2_cu *);
1427
e7c27a73
DJ
1428static struct type *die_containing_type (struct die_info *,
1429 struct dwarf2_cu *);
c906108c 1430
ff39bb5e 1431static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1432 struct dwarf2_cu *);
c906108c 1433
f792889a 1434static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1435
673bfd45
DE
1436static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1437
0d5cff50 1438static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1439
6e70227d 1440static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1441 const char *suffix, int physname,
1442 struct dwarf2_cu *cu);
63d06c5c 1443
e7c27a73 1444static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1445
348e048f
DE
1446static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1447
e7c27a73 1448static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1449
e7c27a73 1450static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1451
96408a79
SA
1452static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1453
71a3c369
TT
1454static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1455
41144253 1456/* Return the .debug_loclists section to use for cu. */
1457static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1458
d0ce17d8
CT
1459/* Return the .debug_rnglists section to use for cu. */
1460static struct dwarf2_section_info *cu_debug_rnglists_section
1461 (struct dwarf2_cu *cu, dwarf_tag tag);
1462
3a2b436a 1463/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1464 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1465enum pc_bounds_kind
1466{
e385593e 1467 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1468 PC_BOUNDS_NOT_PRESENT,
1469
e385593e
JK
1470 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1471 were present but they do not form a valid range of PC addresses. */
1472 PC_BOUNDS_INVALID,
1473
3a2b436a
JK
1474 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1475 PC_BOUNDS_RANGES,
1476
1477 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1478 PC_BOUNDS_HIGH_LOW,
1479};
1480
1481static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1482 CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *,
891813be 1484 dwarf2_psymtab *);
c906108c 1485
fae299cd
DC
1486static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
801e3a5b 1490static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1491 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1492
a14ed312 1493static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1494 struct dwarf2_cu *);
c906108c 1495
a14ed312 1496static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1497 struct type *, struct dwarf2_cu *);
c906108c 1498
a14ed312 1499static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1500 struct die_info *, struct type *,
e7c27a73 1501 struct dwarf2_cu *);
c906108c 1502
a14ed312 1503static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1504 struct type *,
1505 struct dwarf2_cu *);
c906108c 1506
134d01f1 1507static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1508
e7c27a73 1509static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1510
e7c27a73 1511static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1512
5d7cb8df
JK
1513static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
804d2729 1515static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1516
27aa8d6a
SW
1517static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1518
74921315
KS
1519static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1520
f55ee35c
JK
1521static struct type *read_module_type (struct die_info *die,
1522 struct dwarf2_cu *cu);
1523
38d518c9 1524static const char *namespace_name (struct die_info *die,
e142c38c 1525 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1526
134d01f1 1527static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1528
7d79de9a
TT
1529static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1530 bool * = nullptr);
c906108c 1531
6e70227d 1532static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1533 struct dwarf2_cu *);
1534
bf6af496 1535static struct die_info *read_die_and_siblings_1
d521ce57 1536 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1537 struct die_info *);
639d11d3 1538
dee91e82 1539static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1540 const gdb_byte *info_ptr,
1541 const gdb_byte **new_info_ptr,
639d11d3
DC
1542 struct die_info *parent);
1543
d521ce57
TT
1544static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1545 struct die_info **, const gdb_byte *,
3e225074 1546 int);
3019eac3 1547
d521ce57 1548static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1549 struct die_info **, const gdb_byte *);
93311388 1550
e7c27a73 1551static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1552
15d034d0 1553static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1554 struct objfile *);
71c25dea 1555
15d034d0 1556static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1557
15d034d0 1558static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1559 struct die_info *die,
1560 struct dwarf2_cu *cu);
1561
ca69b9e6
DE
1562static const char *dwarf2_physname (const char *name, struct die_info *die,
1563 struct dwarf2_cu *cu);
1564
e142c38c 1565static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1566 struct dwarf2_cu **);
9219021c 1567
d97bc12b
DE
1568static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1569
1570static void dump_die_for_error (struct die_info *);
1571
1572static void dump_die_1 (struct ui_file *, int level, int max_level,
1573 struct die_info *);
c906108c 1574
d97bc12b 1575/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1576
51545339 1577static void store_in_ref_table (struct die_info *,
10b3939b 1578 struct dwarf2_cu *);
c906108c 1579
348e048f 1580static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1581 const struct attribute *,
348e048f
DE
1582 struct dwarf2_cu **);
1583
10b3939b 1584static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1585 const struct attribute *,
f2f0e013 1586 struct dwarf2_cu **);
c906108c 1587
348e048f 1588static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1589 const struct attribute *,
348e048f
DE
1590 struct dwarf2_cu **);
1591
ac9ec31b
DE
1592static struct type *get_signatured_type (struct die_info *, ULONGEST,
1593 struct dwarf2_cu *);
1594
1595static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1596 const struct attribute *,
ac9ec31b
DE
1597 struct dwarf2_cu *);
1598
ab432490
SM
1599static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1600 dwarf2_per_objfile *per_objfile);
348e048f 1601
ab432490
SM
1602static void read_signatured_type (signatured_type *sig_type,
1603 dwarf2_per_objfile *per_objfile);
348e048f 1604
63e43d3a
PMR
1605static int attr_to_dynamic_prop (const struct attribute *attr,
1606 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1607 struct dynamic_prop *prop, struct type *type);
63e43d3a 1608
c906108c
SS
1609/* memory allocation interface */
1610
7b5a2f43 1611static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1612
b60c80d6 1613static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1614
43f3e411 1615static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1616
8cf6f0b1
TT
1617static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1618 struct dwarf2_loclist_baton *baton,
ff39bb5e 1619 const struct attribute *attr);
8cf6f0b1 1620
ff39bb5e 1621static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1622 struct symbol *sym,
f1e6e072
TT
1623 struct dwarf2_cu *cu,
1624 int is_block);
4c2df51b 1625
d521ce57
TT
1626static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1627 const gdb_byte *info_ptr,
7c32eebb 1628 const struct abbrev_info *abbrev);
4bb7a0a7 1629
72bf9492
DJ
1630static hashval_t partial_die_hash (const void *item);
1631
1632static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1633
ae038cb0 1634static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1635 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1636 dwarf2_per_objfile *per_objfile);
ae038cb0 1637
9816fde3 1638static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1639 struct die_info *comp_unit_die,
1640 enum language pretend_language);
93311388 1641
f792889a 1642static struct type *set_die_type (struct die_info *, struct type *,
57567375 1643 struct dwarf2_cu *, bool = false);
1c379e20 1644
976ca316 1645static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1646
976ca316 1647static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1fd400ff 1648
ab432490
SM
1649static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1650 dwarf2_per_objfile *per_objfile,
4a636814 1651 dwarf2_cu *existing_cu,
ab432490
SM
1652 bool skip_partial,
1653 enum language pretend_language);
10b3939b 1654
8fc0b21d 1655static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1656 enum language pretend_language);
10b3939b 1657
8fc0b21d 1658static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1659 enum language pretend_language);
f4dc4d17 1660
10b3939b
DJ
1661static void dwarf2_add_dependence (struct dwarf2_cu *,
1662 struct dwarf2_per_cu_data *);
1663
ae038cb0
DJ
1664static void dwarf2_mark (struct dwarf2_cu *);
1665
b64f50a1 1666static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1667 dwarf2_per_cu_data *per_cu,
1668 dwarf2_per_objfile *per_objfile);
673bfd45 1669
f792889a 1670static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1671
120ce1b5
SM
1672static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1673 dwarf2_per_objfile *per_objfile,
95554aad
TT
1674 enum language pretend_language);
1675
976ca316 1676static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1677
b303c6f6
AB
1678/* Class, the destructor of which frees all allocated queue entries. This
1679 will only have work to do if an error was thrown while processing the
1680 dwarf. If no error was thrown then the queue entries should have all
1681 been processed, and freed, as we went along. */
1682
1683class dwarf2_queue_guard
1684{
1685public:
39856def
TT
1686 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1687 : m_per_objfile (per_objfile)
1688 {
08ac5771
SM
1689 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1690
1691 m_per_objfile->per_bfd->queue.emplace ();
39856def 1692 }
b303c6f6
AB
1693
1694 /* Free any entries remaining on the queue. There should only be
1695 entries left if we hit an error while processing the dwarf. */
1696 ~dwarf2_queue_guard ()
1697 {
08ac5771
SM
1698 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1699
1700 m_per_objfile->per_bfd->queue.reset ();
39856def 1701 }
b303c6f6 1702
39856def 1703 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1704
39856def
TT
1705private:
1706 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1707};
1708
39856def
TT
1709dwarf2_queue_item::~dwarf2_queue_item ()
1710{
1711 /* Anything still marked queued is likely to be in an
1712 inconsistent state, so discard it. */
1713 if (per_cu->queued)
1714 {
7188ed02 1715 per_objfile->remove_cu (per_cu);
39856def
TT
1716 per_cu->queued = 0;
1717 }
1718}
1719
d721ba37
PA
1720/* The return type of find_file_and_directory. Note, the enclosed
1721 string pointers are only valid while this object is valid. */
1722
1723struct file_and_directory
1724{
1725 /* The filename. This is never NULL. */
1726 const char *name;
1727
1728 /* The compilation directory. NULL if not known. If we needed to
1729 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1730 points directly to the DW_AT_comp_dir string attribute owned by
1731 the obstack that owns the DIE. */
1732 const char *comp_dir;
1733
1734 /* If we needed to build a new string for comp_dir, this is what
1735 owns the storage. */
1736 std::string comp_dir_storage;
1737};
1738
1739static file_and_directory find_file_and_directory (struct die_info *die,
1740 struct dwarf2_cu *cu);
9291a0cd 1741
298e9637 1742static htab_up allocate_signatured_type_table ();
1fd400ff 1743
298e9637 1744static htab_up allocate_dwo_unit_table ();
3019eac3 1745
57d63ce2 1746static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1747 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1748 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1749
976ca316 1750static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1751
3019eac3 1752static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1753 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1754 ULONGEST signature);
3019eac3
DE
1755
1756static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1757 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1758
1b555f17 1759static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1760
263db9a1
TT
1761/* A unique pointer to a dwo_file. */
1762
51ac9db5 1763typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1764
976ca316 1765static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1766
1b80a9fa 1767static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1768
1769static void free_line_header_voidp (void *arg);
4390d890
DE
1770\f
1771/* Various complaints about symbol reading that don't abort the process. */
1772
4390d890
DE
1773static void
1774dwarf2_debug_line_missing_file_complaint (void)
1775{
b98664d3 1776 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1777}
1778
1779static void
1780dwarf2_debug_line_missing_end_sequence_complaint (void)
1781{
b98664d3 1782 complaint (_(".debug_line section has line "
4390d890
DE
1783 "program sequence without an end"));
1784}
1785
1786static void
1787dwarf2_complex_location_expr_complaint (void)
1788{
b98664d3 1789 complaint (_("location expression too complex"));
4390d890
DE
1790}
1791
1792static void
1793dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1794 int arg3)
1795{
b98664d3 1796 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1797 arg1, arg2, arg3);
1798}
1799
4390d890
DE
1800static void
1801dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1802{
b98664d3 1803 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1804 arg1, arg2);
1805}
527f3840
JK
1806
1807/* Hash function for line_header_hash. */
1808
1809static hashval_t
1810line_header_hash (const struct line_header *ofs)
1811{
9c541725 1812 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1813}
1814
1815/* Hash function for htab_create_alloc_ex for line_header_hash. */
1816
1817static hashval_t
1818line_header_hash_voidp (const void *item)
1819{
9a3c8263 1820 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1821
1822 return line_header_hash (ofs);
1823}
1824
1825/* Equality function for line_header_hash. */
1826
1827static int
1828line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1829{
9a3c8263
SM
1830 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1831 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1832
9c541725 1833 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1834 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1835}
1836
4390d890 1837\f
9291a0cd 1838
330cdd98
PA
1839/* See declaration. */
1840
5989a64e
SM
1841dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1842 bool can_copy_)
c3699833
SM
1843 : obfd (obfd),
1844 can_copy (can_copy_)
330cdd98
PA
1845{
1846 if (names == NULL)
1847 names = &dwarf2_elf_names;
1848
330cdd98
PA
1849 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1850 locate_sections (obfd, sec, *names);
1851}
1852
5989a64e 1853dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1854{
b76e467d 1855 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1856 per_cu->imported_symtabs_free ();
fc8e7e75 1857
b2bdb8cf 1858 for (signatured_type *sig_type : all_type_units)
ae640021 1859 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1860
5989a64e 1861 /* Everything else should be on this->obstack. */
330cdd98
PA
1862}
1863
7188ed02 1864/* See read.h. */
330cdd98
PA
1865
1866void
7188ed02 1867dwarf2_per_objfile::remove_all_cus ()
330cdd98 1868{
08ac5771
SM
1869 gdb_assert (!this->per_bfd->queue.has_value ());
1870
7188ed02
SM
1871 for (auto pair : m_dwarf2_cus)
1872 delete pair.second;
330cdd98 1873
7188ed02 1874 m_dwarf2_cus.clear ();
330cdd98
PA
1875}
1876
11ed8cad
TT
1877/* A helper class that calls free_cached_comp_units on
1878 destruction. */
1879
1880class free_cached_comp_units
1881{
1882public:
1883
1884 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1885 : m_per_objfile (per_objfile)
1886 {
1887 }
1888
1889 ~free_cached_comp_units ()
1890 {
7188ed02 1891 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1892 }
1893
1894 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1895
1896private:
1897
1898 dwarf2_per_objfile *m_per_objfile;
1899};
1900
af758d11
SM
1901/* See read.h. */
1902
1903bool
1904dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1905{
1906 gdb_assert (per_cu->index < this->m_symtabs.size ());
1907
1908 return this->m_symtabs[per_cu->index] != nullptr;
1909}
1910
1911/* See read.h. */
1912
1913compunit_symtab *
1914dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1915{
1916 gdb_assert (per_cu->index < this->m_symtabs.size ());
1917
1918 return this->m_symtabs[per_cu->index];
1919}
1920
1921/* See read.h. */
1922
1923void
1924dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1925 compunit_symtab *symtab)
1926{
1927 gdb_assert (per_cu->index < this->m_symtabs.size ());
1928 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1929
1930 this->m_symtabs[per_cu->index] = symtab;
1931}
1932
c906108c 1933/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1934 information and return true if we have enough to do something.
1935 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1936 ELF names are used. CAN_COPY is true for formats where symbol
1937 interposition is possible and so symbol values must follow copy
1938 relocation rules. */
c906108c
SS
1939
1940int
251d32d9 1941dwarf2_has_info (struct objfile *objfile,
dda83cd7 1942 const struct dwarf2_debug_sections *names,
4b610737 1943 bool can_copy)
c906108c 1944{
97cbe998
SDJ
1945 if (objfile->flags & OBJF_READNEVER)
1946 return 0;
1947
976ca316 1948 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1949
976ca316 1950 if (per_objfile == NULL)
5989a64e 1951 {
17ee85fc
TT
1952 dwarf2_per_bfd *per_bfd;
1953
1954 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
dda83cd7 1955 doesn't require relocations and if there aren't partial symbols
17ee85fc 1956 from some other reader. */
a8ad4f3c 1957 if (!objfile->has_partial_symbols ()
17ee85fc
TT
1958 && !gdb_bfd_requires_relocations (objfile->obfd))
1959 {
1960 /* See if one has been created for this BFD yet. */
1961 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1962
1963 if (per_bfd == nullptr)
1964 {
1965 /* No, create it now. */
1966 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1967 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1968 }
1969 }
1970 else
1971 {
1972 /* No sharing possible, create one specifically for this objfile. */
1973 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1974 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1975 }
5989a64e 1976
976ca316 1977 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1978 }
5bfd760d 1979
976ca316
SM
1980 return (!per_objfile->per_bfd->info.is_virtual
1981 && per_objfile->per_bfd->info.s.section != NULL
1982 && !per_objfile->per_bfd->abbrev.is_virtual
1983 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1984}
1985
330cdd98 1986/* See declaration. */
c906108c 1987
330cdd98 1988void
5989a64e
SM
1989dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1990 const dwarf2_debug_sections &names)
c906108c 1991{
fd361982 1992 flagword aflag = bfd_section_flags (sectp);
251d32d9 1993
dc7650b8
JK
1994 if ((aflag & SEC_HAS_CONTENTS) == 0)
1995 {
1996 }
950b7495
KS
1997 else if (elf_section_data (sectp)->this_hdr.sh_size
1998 > bfd_get_file_size (abfd))
1999 {
2000 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2001 warning (_("Discarding section %s which has a section size (%s"
2002 ") larger than the file size [in module %s]"),
2003 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2004 bfd_get_filename (abfd));
2005 }
fbedd546 2006 else if (names.info.matches (sectp->name))
c906108c 2007 {
330cdd98 2008 this->info.s.section = sectp;
fd361982 2009 this->info.size = bfd_section_size (sectp);
c906108c 2010 }
fbedd546 2011 else if (names.abbrev.matches (sectp->name))
c906108c 2012 {
330cdd98 2013 this->abbrev.s.section = sectp;
fd361982 2014 this->abbrev.size = bfd_section_size (sectp);
c906108c 2015 }
fbedd546 2016 else if (names.line.matches (sectp->name))
c906108c 2017 {
330cdd98 2018 this->line.s.section = sectp;
fd361982 2019 this->line.size = bfd_section_size (sectp);
c906108c 2020 }
fbedd546 2021 else if (names.loc.matches (sectp->name))
c906108c 2022 {
330cdd98 2023 this->loc.s.section = sectp;
fd361982 2024 this->loc.size = bfd_section_size (sectp);
c906108c 2025 }
fbedd546 2026 else if (names.loclists.matches (sectp->name))
43988095 2027 {
330cdd98 2028 this->loclists.s.section = sectp;
fd361982 2029 this->loclists.size = bfd_section_size (sectp);
43988095 2030 }
fbedd546 2031 else if (names.macinfo.matches (sectp->name))
c906108c 2032 {
330cdd98 2033 this->macinfo.s.section = sectp;
fd361982 2034 this->macinfo.size = bfd_section_size (sectp);
c906108c 2035 }
fbedd546 2036 else if (names.macro.matches (sectp->name))
cf2c3c16 2037 {
330cdd98 2038 this->macro.s.section = sectp;
fd361982 2039 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2040 }
fbedd546 2041 else if (names.str.matches (sectp->name))
c906108c 2042 {
330cdd98 2043 this->str.s.section = sectp;
fd361982 2044 this->str.size = bfd_section_size (sectp);
c906108c 2045 }
fbedd546 2046 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
2047 {
2048 this->str_offsets.s.section = sectp;
2049 this->str_offsets.size = bfd_section_size (sectp);
2050 }
fbedd546 2051 else if (names.line_str.matches (sectp->name))
43988095 2052 {
330cdd98 2053 this->line_str.s.section = sectp;
fd361982 2054 this->line_str.size = bfd_section_size (sectp);
43988095 2055 }
fbedd546 2056 else if (names.addr.matches (sectp->name))
3019eac3 2057 {
330cdd98 2058 this->addr.s.section = sectp;
fd361982 2059 this->addr.size = bfd_section_size (sectp);
3019eac3 2060 }
fbedd546 2061 else if (names.frame.matches (sectp->name))
b6af0555 2062 {
330cdd98 2063 this->frame.s.section = sectp;
fd361982 2064 this->frame.size = bfd_section_size (sectp);
b6af0555 2065 }
fbedd546 2066 else if (names.eh_frame.matches (sectp->name))
b6af0555 2067 {
330cdd98 2068 this->eh_frame.s.section = sectp;
fd361982 2069 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2070 }
fbedd546 2071 else if (names.ranges.matches (sectp->name))
af34e669 2072 {
330cdd98 2073 this->ranges.s.section = sectp;
fd361982 2074 this->ranges.size = bfd_section_size (sectp);
af34e669 2075 }
fbedd546 2076 else if (names.rnglists.matches (sectp->name))
43988095 2077 {
330cdd98 2078 this->rnglists.s.section = sectp;
fd361982 2079 this->rnglists.size = bfd_section_size (sectp);
43988095 2080 }
fbedd546 2081 else if (names.types.matches (sectp->name))
348e048f 2082 {
8b70b953
TT
2083 struct dwarf2_section_info type_section;
2084
2085 memset (&type_section, 0, sizeof (type_section));
049412e3 2086 type_section.s.section = sectp;
fd361982 2087 type_section.size = bfd_section_size (sectp);
8b70b953 2088
fd5866f6 2089 this->types.push_back (type_section);
348e048f 2090 }
fbedd546 2091 else if (names.gdb_index.matches (sectp->name))
9291a0cd 2092 {
330cdd98 2093 this->gdb_index.s.section = sectp;
fd361982 2094 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2095 }
fbedd546 2096 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
2097 {
2098 this->debug_names.s.section = sectp;
fd361982 2099 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 2100 }
fbedd546 2101 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
2102 {
2103 this->debug_aranges.s.section = sectp;
fd361982 2104 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2105 }
dce234bc 2106
fd361982
AM
2107 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2108 && bfd_section_vma (sectp) == 0)
330cdd98 2109 this->has_section_at_zero = true;
c906108c
SS
2110}
2111
dce234bc 2112/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2113 SECTION_NAME. */
af34e669 2114
dce234bc 2115void
3017a003 2116dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2117 enum dwarf2_section_enum sect,
2118 asection **sectp, const gdb_byte **bufp,
2119 bfd_size_type *sizep)
dce234bc 2120{
976ca316 2121 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2122 struct dwarf2_section_info *info;
a3b2a86b
TT
2123
2124 /* We may see an objfile without any DWARF, in which case we just
2125 return nothing. */
976ca316 2126 if (per_objfile == NULL)
a3b2a86b
TT
2127 {
2128 *sectp = NULL;
2129 *bufp = NULL;
2130 *sizep = 0;
2131 return;
2132 }
3017a003
TG
2133 switch (sect)
2134 {
2135 case DWARF2_DEBUG_FRAME:
976ca316 2136 info = &per_objfile->per_bfd->frame;
3017a003
TG
2137 break;
2138 case DWARF2_EH_FRAME:
976ca316 2139 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2140 break;
2141 default:
2142 gdb_assert_not_reached ("unexpected section");
2143 }
dce234bc 2144
96b79293 2145 info->read (objfile);
dce234bc 2146
96b79293 2147 *sectp = info->get_bfd_section ();
dce234bc
PP
2148 *bufp = info->buffer;
2149 *sizep = info->size;
2150}
2151
9291a0cd 2152\f
39298a5d 2153/* DWARF quick_symbol_functions support. */
7b9f3c50
DE
2154
2155/* TUs can share .debug_line entries, and there can be a lot more TUs than
2156 unique line tables, so we maintain a separate table of all .debug_line
2157 derived entries to support the sharing.
2158 All the quick functions need is the list of file names. We discard the
2159 line_header when we're done and don't need to record it here. */
2160struct quick_file_names
2161{
094b34ac
DE
2162 /* The data used to construct the hash key. */
2163 struct stmt_list_hash hash;
7b9f3c50
DE
2164
2165 /* The number of entries in file_names, real_names. */
2166 unsigned int num_file_names;
2167
2168 /* The file names from the line table, after being run through
2169 file_full_name. */
2170 const char **file_names;
2171
2172 /* The file names from the line table after being run through
2173 gdb_realpath. These are computed lazily. */
2174 const char **real_names;
2175};
2176
2177/* When using the index (and thus not using psymtabs), each CU has an
2178 object of this type. This is used to hold information needed by
2179 the various "quick" methods. */
2180struct dwarf2_per_cu_quick_data
2181{
2182 /* The file table. This can be NULL if there was no file table
2183 or it's currently not read in.
5989a64e 2184 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2185 struct quick_file_names *file_names;
2186
7b9f3c50
DE
2187 /* A temporary mark bit used when iterating over all CUs in
2188 expand_symtabs_matching. */
2189 unsigned int mark : 1;
2190
2191 /* True if we've tried to read the file table and found there isn't one.
2192 There will be no point in trying to read it again next time. */
2193 unsigned int no_file_data : 1;
2194};
2195
39298a5d
TT
2196struct dwarf2_base_index_functions : public quick_symbol_functions
2197{
2198 bool has_symbols (struct objfile *objfile) override;
2199
2200 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2201
2202 void forget_cached_source_info (struct objfile *objfile) override;
2203
2204 bool map_symtabs_matching_filename
2205 (struct objfile *objfile, const char *name, const char *real_path,
2206 gdb::function_view<bool (symtab *)> callback) override;
2207
2208 enum language lookup_global_symbol_language (struct objfile *objfile,
2209 const char *name,
2210 domain_enum domain,
2211 bool *symbol_found_p) override
2212 {
2213 *symbol_found_p = false;
2214 return language_unknown;
2215 }
2216
4829711b 2217 void print_stats (struct objfile *objfile, bool print_bcache) override;
39298a5d
TT
2218
2219 void expand_all_symtabs (struct objfile *objfile) override;
2220
2221 void expand_symtabs_with_fullname (struct objfile *objfile,
2222 const char *fullname) override;
2223
2224 struct compunit_symtab *find_pc_sect_compunit_symtab
2225 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2226 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2227
2228 struct compunit_symtab *find_compunit_symtab_by_address
2229 (struct objfile *objfile, CORE_ADDR address) override
2230 {
2231 return nullptr;
2232 }
2233
2234 void map_symbol_filenames (struct objfile *objfile,
2235 symbol_filename_ftype *fun, void *data,
2236 int need_fullname) override;
2237};
2238
2239struct dwarf2_gdb_index : public dwarf2_base_index_functions
2240{
2241 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2242 block_enum block_index,
2243 const char *name,
2244 domain_enum domain) override;
2245
2246 void dump (struct objfile *objfile) override;
2247
2248 void expand_symtabs_for_function (struct objfile *objfile,
2249 const char *func_name) override;
2250
2251 void map_matching_symbols
2252 (struct objfile *,
2253 const lookup_name_info &lookup_name,
2254 domain_enum domain,
2255 int global,
2256 gdb::function_view<symbol_found_callback_ftype> callback,
2257 symbol_compare_ftype *ordered_compare) override;
2258
2259 void expand_symtabs_matching
2260 (struct objfile *objfile,
2261 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2262 const lookup_name_info *lookup_name,
2263 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2264 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2265 enum search_domain kind) override;
2266};
2267
2268struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2269{
2270 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2271 block_enum block_index,
2272 const char *name,
2273 domain_enum domain) override;
2274
2275 void dump (struct objfile *objfile) override;
2276
2277 void expand_symtabs_for_function (struct objfile *objfile,
2278 const char *func_name) override;
2279
2280 void map_matching_symbols
2281 (struct objfile *,
2282 const lookup_name_info &lookup_name,
2283 domain_enum domain,
2284 int global,
2285 gdb::function_view<symbol_found_callback_ftype> callback,
2286 symbol_compare_ftype *ordered_compare) override;
2287
2288 void expand_symtabs_matching
2289 (struct objfile *objfile,
2290 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2291 const lookup_name_info *lookup_name,
2292 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2293 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2294 enum search_domain kind) override;
2295};
2296
2297quick_symbol_functions_up
2298make_dwarf_gdb_index ()
2299{
2300 return quick_symbol_functions_up (new dwarf2_gdb_index);
2301}
2302
2303quick_symbol_functions_up
2304make_dwarf_debug_names ()
2305{
2306 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2307}
2308
094b34ac
DE
2309/* Utility hash function for a stmt_list_hash. */
2310
2311static hashval_t
2312hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2313{
2314 hashval_t v = 0;
2315
2316 if (stmt_list_hash->dwo_unit != NULL)
2317 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2318 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2319 return v;
2320}
2321
2322/* Utility equality function for a stmt_list_hash. */
2323
2324static int
2325eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2326 const struct stmt_list_hash *rhs)
2327{
2328 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2329 return 0;
2330 if (lhs->dwo_unit != NULL
2331 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2332 return 0;
2333
9c541725 2334 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2335}
2336
7b9f3c50
DE
2337/* Hash function for a quick_file_names. */
2338
2339static hashval_t
2340hash_file_name_entry (const void *e)
2341{
9a3c8263
SM
2342 const struct quick_file_names *file_data
2343 = (const struct quick_file_names *) e;
7b9f3c50 2344
094b34ac 2345 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2346}
2347
2348/* Equality function for a quick_file_names. */
2349
2350static int
2351eq_file_name_entry (const void *a, const void *b)
2352{
9a3c8263
SM
2353 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2354 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2355
094b34ac 2356 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2357}
2358
2359/* Delete function for a quick_file_names. */
2360
2361static void
2362delete_file_name_entry (void *e)
2363{
9a3c8263 2364 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2365 int i;
2366
2367 for (i = 0; i < file_data->num_file_names; ++i)
2368 {
2369 xfree ((void*) file_data->file_names[i]);
2370 if (file_data->real_names)
2371 xfree ((void*) file_data->real_names[i]);
2372 }
2373
45940949
TT
2374 /* The space for the struct itself lives on the obstack, so we don't
2375 free it here. */
7b9f3c50
DE
2376}
2377
2378/* Create a quick_file_names hash table. */
2379
5895093f 2380static htab_up
7b9f3c50
DE
2381create_quick_file_names_table (unsigned int nr_initial_entries)
2382{
5895093f
TT
2383 return htab_up (htab_create_alloc (nr_initial_entries,
2384 hash_file_name_entry, eq_file_name_entry,
2385 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2386}
9291a0cd 2387
ab432490
SM
2388/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2389 function is unrelated to symtabs, symtab would have to be created afterwards.
2390 You should call age_cached_comp_units after processing the CU. */
918dd910 2391
1b555f17 2392static dwarf2_cu *
ab432490
SM
2393load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2394 bool skip_partial)
918dd910 2395{
3019eac3 2396 if (per_cu->is_debug_types)
ab432490 2397 load_full_type_unit (per_cu, per_objfile);
918dd910 2398 else
4a636814
SM
2399 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2400 skip_partial, language_minimal);
918dd910 2401
7188ed02
SM
2402 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2403 if (cu == nullptr)
1b555f17 2404 return nullptr; /* Dummy CU. */
2dc860c0 2405
7188ed02 2406 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2407
7188ed02 2408 return cu;
918dd910
JK
2409}
2410
1350c3b4 2411/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2412
9291a0cd 2413static void
97a1449a 2414dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2415 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2416{
f4dc4d17
DE
2417 /* Skip type_unit_groups, reading the type units they contain
2418 is handled elsewhere. */
197400e8 2419 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2420 return;
2421
08ac5771
SM
2422 {
2423 /* The destructor of dwarf2_queue_guard frees any entries left on
2424 the queue. After this point we're guaranteed to leave this function
2425 with the dwarf queue empty. */
2426 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2427
08ac5771
SM
2428 if (!per_objfile->symtab_set_p (per_cu))
2429 {
2430 queue_comp_unit (per_cu, per_objfile, language_minimal);
2431 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2432
2433 /* If we just loaded a CU from a DWO, and we're working with an index
2434 that may badly handle TUs, load all the TUs in that DWO as well.
2435 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2436 if (!per_cu->is_debug_types
2437 && cu != NULL
2438 && cu->dwo_unit != NULL
2439 && per_objfile->per_bfd->index_table != NULL
2440 && per_objfile->per_bfd->index_table->version <= 7
2441 /* DWP files aren't supported yet. */
2442 && get_dwp_file (per_objfile) == NULL)
2443 queue_and_load_all_dwo_tus (cu);
2444 }
9291a0cd 2445
08ac5771
SM
2446 process_queue (per_objfile);
2447 }
9291a0cd
TT
2448
2449 /* Age the cache, releasing compilation units that have not
2450 been used recently. */
976ca316 2451 per_objfile->age_comp_units ();
9291a0cd
TT
2452}
2453
97a1449a
SM
2454/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2455 the per-objfile for which this symtab is instantiated.
2456
2457 Returns the resulting symbol table. */
2fdf6df6 2458
43f3e411 2459static struct compunit_symtab *
97a1449a 2460dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2461 dwarf2_per_objfile *per_objfile,
97a1449a 2462 bool skip_partial)
9291a0cd 2463{
976ca316 2464 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2465
976ca316 2466 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2467 {
976ca316 2468 free_cached_comp_units freer (per_objfile);
c83dd867 2469 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2470 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2471 process_cu_includes (per_objfile);
9291a0cd 2472 }
f194fefb 2473
976ca316 2474 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2475}
2476
ff4c9fec 2477/* See declaration. */
f4dc4d17 2478
ff4c9fec 2479dwarf2_per_cu_data *
5989a64e 2480dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2481{
b76e467d 2482 if (index >= this->all_comp_units.size ())
ff4c9fec 2483 {
b76e467d 2484 index -= this->all_comp_units.size ();
b2bdb8cf 2485 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2486 return &this->all_type_units[index]->per_cu;
2487 }
f4dc4d17 2488
ff4c9fec
SM
2489 return this->all_comp_units[index];
2490}
f4dc4d17 2491
ff4c9fec 2492/* See declaration. */
2fdf6df6 2493
ff4c9fec 2494dwarf2_per_cu_data *
5989a64e 2495dwarf2_per_bfd::get_cu (int index)
1fd400ff 2496{
b76e467d 2497 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2498
ff4c9fec 2499 return this->all_comp_units[index];
f4dc4d17
DE
2500}
2501
ff4c9fec 2502/* See declaration. */
f4dc4d17 2503
ff4c9fec 2504signatured_type *
5989a64e 2505dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2506{
b2bdb8cf 2507 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2508
ff4c9fec 2509 return this->all_type_units[index];
1fd400ff
TT
2510}
2511
d3473f0c
TT
2512/* See read.h. */
2513
2514dwarf2_per_cu_data *
5989a64e 2515dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2516{
2517 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2518 result->per_bfd = this;
d3473f0c
TT
2519 result->index = m_num_psymtabs++;
2520 return result;
2521}
2522
2523/* See read.h. */
2524
2525signatured_type *
5989a64e 2526dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2527{
2528 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2529 result->per_cu.per_bfd = this;
d3473f0c
TT
2530 result->per_cu.index = m_num_psymtabs++;
2531 return result;
2532}
2533
168c9250 2534/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2535 obstack, and constructed with the specified field values. */
4b514bc8
JK
2536
2537static dwarf2_per_cu_data *
168c9250
SM
2538create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2539 struct dwarf2_section_info *section,
2540 int is_dwz,
2541 sect_offset sect_off, ULONGEST length)
4b514bc8 2542{
168c9250 2543 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2544 the_cu->sect_off = sect_off;
2545 the_cu->length = length;
4b514bc8 2546 the_cu->section = section;
168c9250 2547 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2548 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2549 the_cu->is_dwz = is_dwz;
2550 return the_cu;
2551}
2552
2ec9a5e0
TT
2553/* A helper for create_cus_from_index that handles a given list of
2554 CUs. */
2fdf6df6 2555
74a0d9f6 2556static void
168c9250 2557create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2558 const gdb_byte *cu_list, offset_type n_elements,
2559 struct dwarf2_section_info *section,
b76e467d 2560 int is_dwz)
9291a0cd 2561{
12359b5e 2562 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2563 {
74a0d9f6 2564 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2565
2566 sect_offset sect_off
2567 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2568 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2569 cu_list += 2 * 8;
2570
b76e467d 2571 dwarf2_per_cu_data *per_cu
168c9250
SM
2572 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2573 length);
2574 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2575 }
9291a0cd
TT
2576}
2577
2ec9a5e0 2578/* Read the CU list from the mapped index, and use it to create all
168c9250 2579 the CU objects for PER_BFD. */
2ec9a5e0 2580
74a0d9f6 2581static void
168c9250 2582create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2583 const gdb_byte *cu_list, offset_type cu_list_elements,
2584 const gdb_byte *dwz_list, offset_type dwz_elements)
2585{
168c9250
SM
2586 gdb_assert (per_bfd->all_comp_units.empty ());
2587 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2588
168c9250
SM
2589 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2590 &per_bfd->info, 0);
2ec9a5e0
TT
2591
2592 if (dwz_elements == 0)
74a0d9f6 2593 return;
2ec9a5e0 2594
168c9250
SM
2595 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2596 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2597 &dwz->info, 1);
2ec9a5e0
TT
2598}
2599
1fd400ff 2600/* Create the signatured type hash table from the index. */
673bfd45 2601
74a0d9f6 2602static void
12359b5e 2603create_signatured_type_table_from_index
168c9250
SM
2604 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2605 const gdb_byte *bytes, offset_type elements)
1fd400ff 2606{
168c9250
SM
2607 gdb_assert (per_bfd->all_type_units.empty ());
2608 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2609
298e9637 2610 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2611
12359b5e 2612 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2613 {
52dc124a 2614 struct signatured_type *sig_type;
9c541725 2615 ULONGEST signature;
1fd400ff 2616 void **slot;
9c541725 2617 cu_offset type_offset_in_tu;
1fd400ff 2618
74a0d9f6 2619 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2620 sect_offset sect_off
2621 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2622 type_offset_in_tu
2623 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2624 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2625 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2626 bytes += 3 * 8;
2627
168c9250 2628 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2629 sig_type->signature = signature;
9c541725 2630 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2631 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2632 sig_type->per_cu.section = section;
9c541725 2633 sig_type->per_cu.sect_off = sect_off;
52dc124a 2634 sig_type->per_cu.v.quick
168c9250 2635 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2636 struct dwarf2_per_cu_quick_data);
2637
b0b6a987 2638 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2639 *slot = sig_type;
1fd400ff 2640
168c9250 2641 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2642 }
2643
168c9250 2644 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2645}
2646
927aa2e7
JK
2647/* Create the signatured type hash table from .debug_names. */
2648
2649static void
2650create_signatured_type_table_from_debug_names
976ca316 2651 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2652 const mapped_debug_names &map,
2653 struct dwarf2_section_info *section,
2654 struct dwarf2_section_info *abbrev_section)
2655{
976ca316 2656 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2657
96b79293
TT
2658 section->read (objfile);
2659 abbrev_section->read (objfile);
927aa2e7 2660
976ca316
SM
2661 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2662 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2663
298e9637 2664 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2665
2666 for (uint32_t i = 0; i < map.tu_count; ++i)
2667 {
2668 struct signatured_type *sig_type;
927aa2e7 2669 void **slot;
927aa2e7
JK
2670
2671 sect_offset sect_off
2672 = (sect_offset) (extract_unsigned_integer
2673 (map.tu_table_reordered + i * map.offset_size,
2674 map.offset_size,
2675 map.dwarf5_byte_order));
2676
2677 comp_unit_head cu_header;
976ca316 2678 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2679 abbrev_section,
927aa2e7
JK
2680 section->buffer + to_underlying (sect_off),
2681 rcuh_kind::TYPE);
2682
976ca316 2683 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2684 sig_type->signature = cu_header.signature;
2685 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2686 sig_type->per_cu.is_debug_types = 1;
2687 sig_type->per_cu.section = section;
2688 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2689 sig_type->per_cu.v.quick
976ca316 2690 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2691 struct dwarf2_per_cu_quick_data);
2692
b0b6a987 2693 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2694 *slot = sig_type;
2695
976ca316 2696 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2697 }
2698
976ca316 2699 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2700}
2701
9291a0cd 2702/* Read the address map data from the mapped index, and use it to
efd7398e 2703 populate the psymtabs_addrmap. */
2fdf6df6 2704
9291a0cd 2705static void
976ca316 2706create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2707 struct mapped_index *index)
9291a0cd 2708{
976ca316 2709 struct objfile *objfile = per_objfile->objfile;
efd7398e 2710 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2711 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2712 const gdb_byte *iter, *end;
9291a0cd 2713 struct addrmap *mutable_map;
9291a0cd
TT
2714 CORE_ADDR baseaddr;
2715
8268c778
PA
2716 auto_obstack temp_obstack;
2717
9291a0cd
TT
2718 mutable_map = addrmap_create_mutable (&temp_obstack);
2719
f00a2de2
PA
2720 iter = index->address_table.data ();
2721 end = iter + index->address_table.size ();
9291a0cd 2722
b3b3bada 2723 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2724
2725 while (iter < end)
2726 {
2727 ULONGEST hi, lo, cu_index;
2728 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2729 iter += 8;
2730 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2731 iter += 8;
2732 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2733 iter += 4;
f652bce2 2734
24a55014 2735 if (lo > hi)
f652bce2 2736 {
b98664d3 2737 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2738 hex_string (lo), hex_string (hi));
24a55014 2739 continue;
f652bce2 2740 }
24a55014 2741
efd7398e 2742 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2743 {
b98664d3 2744 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2745 (unsigned) cu_index);
24a55014 2746 continue;
f652bce2 2747 }
24a55014 2748
79748972
TT
2749 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2750 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2751 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2752 per_bfd->get_cu (cu_index));
9291a0cd
TT
2753 }
2754
efd7398e
TT
2755 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2756 &per_bfd->obstack);
9291a0cd
TT
2757}
2758
927aa2e7 2759/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2760 populate the psymtabs_addrmap. */
927aa2e7
JK
2761
2762static void
976ca316 2763create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2764 struct dwarf2_section_info *section)
2765{
976ca316 2766 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2767 bfd *abfd = objfile->obfd;
08feed99 2768 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2769 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2770 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2771
2772 auto_obstack temp_obstack;
2773 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2774
2775 std::unordered_map<sect_offset,
2776 dwarf2_per_cu_data *,
2777 gdb::hash_enum<sect_offset>>
2778 debug_info_offset_to_per_cu;
976ca316 2779 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2780 {
927aa2e7
JK
2781 const auto insertpair
2782 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2783 if (!insertpair.second)
2784 {
2785 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2786 "debug_info_offset %s, ignoring .debug_aranges."),
2787 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2788 return;
2789 }
2790 }
2791
96b79293 2792 section->read (objfile);
927aa2e7
JK
2793
2794 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2795
2796 const gdb_byte *addr = section->buffer;
2797
2798 while (addr < section->buffer + section->size)
2799 {
2800 const gdb_byte *const entry_addr = addr;
2801 unsigned int bytes_read;
2802
2803 const LONGEST entry_length = read_initial_length (abfd, addr,
2804 &bytes_read);
2805 addr += bytes_read;
2806
2807 const gdb_byte *const entry_end = addr + entry_length;
2808 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2809 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2810 if (addr + entry_length > section->buffer + section->size)
2811 {
47e3f474 2812 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2813 "length %s exceeds section length %s, "
927aa2e7 2814 "ignoring .debug_aranges."),
47e3f474
TV
2815 objfile_name (objfile),
2816 plongest (entry_addr - section->buffer),
927aa2e7
JK
2817 plongest (bytes_read + entry_length),
2818 pulongest (section->size));
2819 return;
2820 }
2821
2822 /* The version number. */
2823 const uint16_t version = read_2_bytes (abfd, addr);
2824 addr += 2;
2825 if (version != 2)
2826 {
47e3f474 2827 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2828 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2829 objfile_name (objfile),
2830 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2831 return;
2832 }
2833
2834 const uint64_t debug_info_offset
2835 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2836 addr += offset_size;
2837 const auto per_cu_it
2838 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2839 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2840 {
47e3f474 2841 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2842 "debug_info_offset %s does not exists, "
2843 "ignoring .debug_aranges."),
47e3f474
TV
2844 objfile_name (objfile),
2845 plongest (entry_addr - section->buffer),
927aa2e7
JK
2846 pulongest (debug_info_offset));
2847 return;
2848 }
2849 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2850
2851 const uint8_t address_size = *addr++;
2852 if (address_size < 1 || address_size > 8)
2853 {
47e3f474 2854 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2855 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2856 objfile_name (objfile),
2857 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2858 return;
2859 }
2860
2861 const uint8_t segment_selector_size = *addr++;
2862 if (segment_selector_size != 0)
2863 {
47e3f474 2864 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2865 "segment_selector_size %u is not supported, "
2866 "ignoring .debug_aranges."),
47e3f474
TV
2867 objfile_name (objfile),
2868 plongest (entry_addr - section->buffer),
927aa2e7
JK
2869 segment_selector_size);
2870 return;
2871 }
2872
2873 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2874 size. It is undocumented by the DWARF standard but GCC does
2875 use it. */
927aa2e7
JK
2876 for (size_t padding = ((-(addr - section->buffer))
2877 & (2 * address_size - 1));
dda83cd7 2878 padding > 0; padding--)
927aa2e7
JK
2879 if (*addr++ != 0)
2880 {
47e3f474 2881 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2882 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2883 objfile_name (objfile),
2884 plongest (entry_addr - section->buffer));
927aa2e7
JK
2885 return;
2886 }
2887
2888 for (;;)
2889 {
2890 if (addr + 2 * address_size > entry_end)
2891 {
47e3f474 2892 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2893 "address list is not properly terminated, "
2894 "ignoring .debug_aranges."),
47e3f474
TV
2895 objfile_name (objfile),
2896 plongest (entry_addr - section->buffer));
927aa2e7
JK
2897 return;
2898 }
2899 ULONGEST start = extract_unsigned_integer (addr, address_size,
2900 dwarf5_byte_order);
2901 addr += address_size;
2902 ULONGEST length = extract_unsigned_integer (addr, address_size,
2903 dwarf5_byte_order);
2904 addr += address_size;
2905 if (start == 0 && length == 0)
2906 break;
efd7398e 2907 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2908 {
2909 /* Symbol was eliminated due to a COMDAT group. */
2910 continue;
2911 }
2912 ULONGEST end = start + length;
79748972
TT
2913 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2914 - baseaddr);
2915 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2916 - baseaddr);
927aa2e7
JK
2917 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2918 }
2919 }
2920
efd7398e
TT
2921 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2922 &per_bfd->obstack);
927aa2e7
JK
2923}
2924
9291a0cd
TT
2925/* Find a slot in the mapped index INDEX for the object named NAME.
2926 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2927 constant pool and return true. If NAME cannot be found, return
2928 false. */
2fdf6df6 2929
109483d9 2930static bool
9291a0cd
TT
2931find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2932 offset_type **vec_out)
2933{
0cf03b49 2934 offset_type hash;
9291a0cd 2935 offset_type slot, step;
559a7a62 2936 int (*cmp) (const char *, const char *);
9291a0cd 2937
791afaa2 2938 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2939 if (current_language->la_language == language_cplus
45280282
IB
2940 || current_language->la_language == language_fortran
2941 || current_language->la_language == language_d)
0cf03b49
JK
2942 {
2943 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2944 not contain any. */
a8719064 2945
72998fb3 2946 if (strchr (name, '(') != NULL)
0cf03b49 2947 {
109483d9 2948 without_params = cp_remove_params (name);
0cf03b49 2949
72998fb3 2950 if (without_params != NULL)
791afaa2 2951 name = without_params.get ();
0cf03b49
JK
2952 }
2953 }
2954
559a7a62 2955 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2956 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2957 simulate our NAME being searched is also lowercased. */
2958 hash = mapped_index_string_hash ((index->version == 4
dda83cd7 2959 && case_sensitivity == case_sensitive_off
559a7a62
JK
2960 ? 5 : index->version),
2961 name);
2962
f00a2de2
PA
2963 slot = hash & (index->symbol_table.size () - 1);
2964 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2965 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2966
2967 for (;;)
2968 {
9291a0cd 2969 const char *str;
f00a2de2
PA
2970
2971 const auto &bucket = index->symbol_table[slot];
2972 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2973 return false;
9291a0cd 2974
f00a2de2 2975 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2976 if (!cmp (name, str))
9291a0cd
TT
2977 {
2978 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2979 + MAYBE_SWAP (bucket.vec));
109483d9 2980 return true;
9291a0cd
TT
2981 }
2982
f00a2de2 2983 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2984 }
2985}
2986
4485a1c1
SM
2987/* A helper function that reads the .gdb_index from BUFFER and fills
2988 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2989 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2990 ok to use deprecated sections.
2991
2992 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2993 out parameters that are filled in with information about the CU and
2994 TU lists in the section.
2995
4485a1c1 2996 Returns true if all went well, false otherwise. */
2fdf6df6 2997
d33bc52e 2998static bool
3810f182 2999read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
3000 bool deprecated_ok,
3001 gdb::array_view<const gdb_byte> buffer,
3002 struct mapped_index *map,
3003 const gdb_byte **cu_list,
3004 offset_type *cu_list_elements,
3005 const gdb_byte **types_list,
3006 offset_type *types_list_elements)
3007{
3008 const gdb_byte *addr = &buffer[0];
82430852 3009
9291a0cd 3010 /* Version check. */
4485a1c1 3011 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3012 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3013 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3014 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3015 indices. */
831adc1f 3016 if (version < 4)
481860b3
GB
3017 {
3018 static int warning_printed = 0;
3019 if (!warning_printed)
3020 {
3021 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3022 filename);
481860b3
GB
3023 warning_printed = 1;
3024 }
3025 return 0;
3026 }
3027 /* Index version 4 uses a different hash function than index version
3028 5 and later.
3029
3030 Versions earlier than 6 did not emit psymbols for inlined
3031 functions. Using these files will cause GDB not to be able to
3032 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3033 indices unless the user has done
3034 "set use-deprecated-index-sections on". */
2ec9a5e0 3035 if (version < 6 && !deprecated_ok)
481860b3
GB
3036 {
3037 static int warning_printed = 0;
3038 if (!warning_printed)
3039 {
e615022a
DE
3040 warning (_("\
3041Skipping deprecated .gdb_index section in %s.\n\
3042Do \"set use-deprecated-index-sections on\" before the file is read\n\
3043to use the section anyway."),
2ec9a5e0 3044 filename);
481860b3
GB
3045 warning_printed = 1;
3046 }
3047 return 0;
3048 }
796a7ff8 3049 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3050 of the TU (for symbols coming from TUs),
3051 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3052 Plus gold-generated indices can have duplicate entries for global symbols,
3053 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3054 These are just performance bugs, and we can't distinguish gdb-generated
3055 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3056
481860b3 3057 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3058 longer backward compatible. */
796a7ff8 3059 if (version > 8)
594e8718 3060 return 0;
9291a0cd 3061
559a7a62 3062 map->version = version;
9291a0cd 3063
4485a1c1 3064 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3065
4485a1c1 3066 int i = 0;
2ec9a5e0
TT
3067 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3068 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3069 / 8);
1fd400ff
TT
3070 ++i;
3071
2ec9a5e0
TT
3072 *types_list = addr + MAYBE_SWAP (metadata[i]);
3073 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3074 - MAYBE_SWAP (metadata[i]))
3075 / 8);
987d643c 3076 ++i;
1fd400ff 3077
f00a2de2
PA
3078 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3079 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3080 map->address_table
3081 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3082 ++i;
3083
f00a2de2
PA
3084 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3085 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3086 map->symbol_table
3087 = gdb::array_view<mapped_index::symbol_table_slot>
3088 ((mapped_index::symbol_table_slot *) symbol_table,
3089 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3090
f00a2de2 3091 ++i;
f9d83a0b 3092 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3093
2ec9a5e0
TT
3094 return 1;
3095}
3096
4485a1c1
SM
3097/* Callback types for dwarf2_read_gdb_index. */
3098
3099typedef gdb::function_view
5989a64e 3100 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3101 get_gdb_index_contents_ftype;
3102typedef gdb::function_view
3103 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3104 get_gdb_index_contents_dwz_ftype;
3105
927aa2e7 3106/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3107 elements of all the CUs and return 1. Otherwise, return 0. */
3108
3109static int
4485a1c1 3110dwarf2_read_gdb_index
976ca316 3111 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3112 get_gdb_index_contents_ftype get_gdb_index_contents,
3113 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3114{
2ec9a5e0
TT
3115 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3116 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3117 struct dwz_file *dwz;
976ca316 3118 struct objfile *objfile = per_objfile->objfile;
f8c41851 3119 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3120
4485a1c1 3121 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3122 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3123
3124 if (main_index_contents.empty ())
3125 return 0;
3126
3063847f 3127 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3128 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3129 use_deprecated_index_sections,
3130 main_index_contents, map.get (), &cu_list,
3131 &cu_list_elements, &types_list,
3132 &types_list_elements))
2ec9a5e0
TT
3133 return 0;
3134
0fefef59 3135 /* Don't use the index if it's empty. */
3063847f 3136 if (map->symbol_table.empty ())
0fefef59
DE
3137 return 0;
3138
2ec9a5e0
TT
3139 /* If there is a .dwz file, read it so we can get its CU list as
3140 well. */
f8c41851 3141 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3142 if (dwz != NULL)
2ec9a5e0 3143 {
2ec9a5e0
TT
3144 struct mapped_index dwz_map;
3145 const gdb_byte *dwz_types_ignore;
3146 offset_type dwz_types_elements_ignore;
3147
4485a1c1
SM
3148 gdb::array_view<const gdb_byte> dwz_index_content
3149 = get_gdb_index_contents_dwz (objfile, dwz);
3150
3151 if (dwz_index_content.empty ())
3152 return 0;
3153
3810f182 3154 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3155 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3156 &dwz_list, &dwz_list_elements,
3157 &dwz_types_ignore,
3158 &dwz_types_elements_ignore))
2ec9a5e0
TT
3159 {
3160 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3161 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3162 return 0;
3163 }
3164 }
3165
f8c41851
SM
3166 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3167 dwz_list_elements);
1fd400ff 3168
8b70b953
TT
3169 if (types_list_elements)
3170 {
8b70b953
TT
3171 /* We can only handle a single .debug_types when we have an
3172 index. */
f8c41851 3173 if (per_bfd->types.size () != 1)
8b70b953
TT
3174 return 0;
3175
f8c41851 3176 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3177
f8c41851 3178 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3179 types_list_elements);
8b70b953 3180 }
9291a0cd 3181
976ca316 3182 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3183
f8c41851
SM
3184 per_bfd->index_table = std::move (map);
3185 per_bfd->using_index = 1;
3186 per_bfd->quick_file_names_table =
3187 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3188
9291a0cd
TT
3189 return 1;
3190}
3191
dee91e82 3192/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3193
dee91e82
DE
3194static void
3195dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 3196 struct die_info *comp_unit_die)
9291a0cd 3197{
dee91e82 3198 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3199 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3200 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3201 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3202 struct attribute *attr;
7b9f3c50
DE
3203 void **slot;
3204 struct quick_file_names *qfn;
9291a0cd 3205
0186c6a7
DE
3206 gdb_assert (! this_cu->is_debug_types);
3207
07261596
TT
3208 /* Our callers never want to match partial units -- instead they
3209 will match the enclosing full CU. */
3210 if (comp_unit_die->tag == DW_TAG_partial_unit)
3211 {
3212 this_cu->v.quick->no_file_data = 1;
3213 return;
3214 }
3215
0186c6a7 3216 lh_cu = this_cu;
7b9f3c50 3217 slot = NULL;
dee91e82 3218
fff8551c 3219 line_header_up lh;
9c541725 3220 sect_offset line_offset {};
fff8551c 3221
dee91e82 3222 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3223 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3224 {
7b9f3c50
DE
3225 struct quick_file_names find_entry;
3226
d4df075e 3227 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3228
3229 /* We may have already read in this line header (TU line header sharing).
3230 If we have we're done. */
094b34ac 3231 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3232 find_entry.hash.line_sect_off = line_offset;
976ca316 3233 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3234 &find_entry, INSERT);
3235 if (*slot != NULL)
3236 {
9a3c8263 3237 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3238 return;
7b9f3c50
DE
3239 }
3240
3019eac3 3241 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3242 }
3243 if (lh == NULL)
3244 {
094b34ac 3245 lh_cu->v.quick->no_file_data = 1;
dee91e82 3246 return;
9291a0cd
TT
3247 }
3248
976ca316 3249 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3250 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3251 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3252 gdb_assert (slot != NULL);
3253 *slot = qfn;
9291a0cd 3254
d721ba37 3255 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3256
aa391654
TT
3257 int offset = 0;
3258 if (strcmp (fnd.name, "<unknown>") != 0)
3259 ++offset;
3260
7ba99d21 3261 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3262 qfn->file_names =
976ca316 3263 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3264 qfn->num_file_names);
aa391654
TT
3265 if (offset != 0)
3266 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3267 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3268 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3269 fnd.comp_dir).release ();
7b9f3c50 3270 qfn->real_names = NULL;
9291a0cd 3271
094b34ac 3272 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3273}
3274
3275/* A helper for the "quick" functions which attempts to read the line
3276 table for THIS_CU. */
3277
3278static struct quick_file_names *
ab432490
SM
3279dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3280 dwarf2_per_objfile *per_objfile)
dee91e82 3281{
0186c6a7
DE
3282 /* This should never be called for TUs. */
3283 gdb_assert (! this_cu->is_debug_types);
3284 /* Nor type unit groups. */
197400e8 3285 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3286
dee91e82
DE
3287 if (this_cu->v.quick->file_names != NULL)
3288 return this_cu->v.quick->file_names;
3289 /* If we know there is no line data, no point in looking again. */
3290 if (this_cu->v.quick->no_file_data)
3291 return NULL;
3292
ab432490 3293 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3294 if (!reader.dummy_p)
eb5dd737 3295 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
3296
3297 if (this_cu->v.quick->no_file_data)
3298 return NULL;
3299 return this_cu->v.quick->file_names;
9291a0cd
TT
3300}
3301
3302/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3303 real path for a given file name from the line table. */
2fdf6df6 3304
9291a0cd 3305static const char *
976ca316 3306dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3307 struct quick_file_names *qfn, int index)
9291a0cd 3308{
7b9f3c50 3309 if (qfn->real_names == NULL)
976ca316 3310 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3311 qfn->num_file_names, const char *);
9291a0cd 3312
7b9f3c50 3313 if (qfn->real_names[index] == NULL)
14278e1f 3314 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3315
7b9f3c50 3316 return qfn->real_names[index];
9291a0cd
TT
3317}
3318
39298a5d
TT
3319struct symtab *
3320dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3321{
976ca316
SM
3322 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3323 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3324 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3325
43f3e411
DE
3326 if (cust == NULL)
3327 return NULL;
ed2dc618 3328
43f3e411 3329 return compunit_primary_filetab (cust);
9291a0cd
TT
3330}
3331
7b9f3c50
DE
3332/* Traversal function for dw2_forget_cached_source_info. */
3333
3334static int
3335dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3336{
7b9f3c50 3337 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3338
7b9f3c50 3339 if (file_data->real_names)
9291a0cd 3340 {
7b9f3c50 3341 int i;
9291a0cd 3342
7b9f3c50 3343 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3344 {
7b9f3c50
DE
3345 xfree ((void*) file_data->real_names[i]);
3346 file_data->real_names[i] = NULL;
9291a0cd
TT
3347 }
3348 }
7b9f3c50
DE
3349
3350 return 1;
3351}
3352
39298a5d
TT
3353void
3354dwarf2_base_index_functions::forget_cached_source_info
3355 (struct objfile *objfile)
7b9f3c50 3356{
976ca316 3357 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3358
976ca316 3359 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3360 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3361}
3362
f8eba3c6
TT
3363/* Helper function for dw2_map_symtabs_matching_filename that expands
3364 the symtabs and calls the iterator. */
3365
3366static int
3367dw2_map_expand_apply (struct objfile *objfile,
3368 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3369 const char *name, const char *real_path,
14bc53a8 3370 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3371{
43f3e411 3372 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3373
3374 /* Don't visit already-expanded CUs. */
af758d11
SM
3375 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3376 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3377 return 0;
3378
3379 /* This may expand more than one symtab, and we want to iterate over
3380 all of them. */
97a1449a 3381 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3382
14bc53a8
PA
3383 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3384 last_made, callback);
f8eba3c6
TT
3385}
3386
3387/* Implementation of the map_symtabs_matching_filename method. */
3388
39298a5d
TT
3389bool
3390dwarf2_base_index_functions::map_symtabs_matching_filename
14bc53a8
PA
3391 (struct objfile *objfile, const char *name, const char *real_path,
3392 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3393{
c011a4f4 3394 const char *name_basename = lbasename (name);
976ca316 3395 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3396
848e3e78
DE
3397 /* The rule is CUs specify all the files, including those used by
3398 any TU, so there's no need to scan TUs here. */
f4dc4d17 3399
976ca316 3400 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3401 {
3d7bb9d9 3402 /* We only need to look at symtabs not already expanded. */
976ca316 3403 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3404 continue;
3405
976ca316 3406 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3407 if (file_data == NULL)
9291a0cd
TT
3408 continue;
3409
b76e467d 3410 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3411 {
7b9f3c50 3412 const char *this_name = file_data->file_names[j];
da235a7c 3413 const char *this_real_name;
9291a0cd 3414
af529f8f 3415 if (compare_filenames_for_search (this_name, name))
9291a0cd 3416 {
f5b95b50 3417 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3418 callback))
3419 return true;
288e77a7 3420 continue;
4aac40c8 3421 }
9291a0cd 3422
c011a4f4
DE
3423 /* Before we invoke realpath, which can get expensive when many
3424 files are involved, do a quick comparison of the basenames. */
3425 if (! basenames_may_differ
3426 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3427 continue;
3428
976ca316 3429 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3430 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3431 {
da235a7c 3432 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3433 callback))
3434 return true;
288e77a7 3435 continue;
da235a7c 3436 }
9291a0cd 3437
da235a7c
JK
3438 if (real_path != NULL)
3439 {
af529f8f
JK
3440 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3441 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3442 if (this_real_name != NULL
af529f8f 3443 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3444 {
f5b95b50 3445 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3446 callback))
3447 return true;
288e77a7 3448 continue;
9291a0cd
TT
3449 }
3450 }
3451 }
3452 }
3453
14bc53a8 3454 return false;
9291a0cd
TT
3455}
3456
da51c347
DE
3457/* Struct used to manage iterating over all CUs looking for a symbol. */
3458
3459struct dw2_symtab_iterator
9291a0cd 3460{
ed2dc618 3461 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3462 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3463 /* If set, only look for symbols that match that block. Valid values are
3464 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3465 gdb::optional<block_enum> block_index;
da51c347
DE
3466 /* The kind of symbol we're looking for. */
3467 domain_enum domain;
3468 /* The list of CUs from the index entry of the symbol,
3469 or NULL if not found. */
3470 offset_type *vec;
3471 /* The next element in VEC to look at. */
3472 int next;
3473 /* The number of elements in VEC, or zero if there is no match. */
3474 int length;
8943b874
DE
3475 /* Have we seen a global version of the symbol?
3476 If so we can ignore all further global instances.
3477 This is to work around gold/15646, inefficient gold-generated
3478 indices. */
3479 int global_seen;
da51c347 3480};
9291a0cd 3481
e5f3ece2 3482/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3483
9291a0cd 3484static void
e5f3ece2
TV
3485dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3486 dwarf2_per_objfile *per_objfile,
3487 gdb::optional<block_enum> block_index,
3488 domain_enum domain)
da51c347 3489{
976ca316 3490 iter->per_objfile = per_objfile;
da51c347
DE
3491 iter->block_index = block_index;
3492 iter->domain = domain;
3493 iter->next = 0;
8943b874 3494 iter->global_seen = 0;
e5f3ece2
TV
3495 iter->vec = NULL;
3496 iter->length = 0;
3497}
da51c347 3498
e5f3ece2
TV
3499/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3500
3501static void
3502dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3503 dwarf2_per_objfile *per_objfile,
3504 gdb::optional<block_enum> block_index,
3505 domain_enum domain,
3506 const char *name)
3507{
3508 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3509
e5f3ece2 3510 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3511 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3512 if (index == NULL)
3513 return;
3514
3515 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3516 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3517}
3518
3519/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3520
3521static void
3522dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3523 dwarf2_per_objfile *per_objfile,
3524 gdb::optional<block_enum> block_index,
3525 domain_enum domain, offset_type namei)
3526{
3527 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3528
3529 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3530 /* index is NULL if OBJF_READNOW. */
3531 if (index == NULL)
3532 return;
3533
3534 gdb_assert (!index->symbol_name_slot_invalid (namei));
3535 const auto &bucket = index->symbol_table[namei];
3536
3537 iter->vec = (offset_type *) (index->constant_pool
3538 + MAYBE_SWAP (bucket.vec));
3539 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3540}
3541
3542/* Return the next matching CU or NULL if there are no more. */
3543
3544static struct dwarf2_per_cu_data *
3545dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3546{
976ca316 3547 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3548
da51c347
DE
3549 for ( ; iter->next < iter->length; ++iter->next)
3550 {
3551 offset_type cu_index_and_attrs =
3552 MAYBE_SWAP (iter->vec[iter->next + 1]);
3553 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3554 gdb_index_symbol_kind symbol_kind =
3555 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3556 /* Only check the symbol attributes if they're present.
3557 Indices prior to version 7 don't record them,
3558 and indices >= 7 may elide them for certain symbols
3559 (gold does this). */
3560 int attrs_valid =
976ca316 3561 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3562 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3563
3190f0c6 3564 /* Don't crash on bad data. */
976ca316
SM
3565 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3566 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3567 {
b98664d3 3568 complaint (_(".gdb_index entry has bad CU index"
976ca316 3569 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3570 continue;
3571 }
3572
976ca316 3573 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3574
da51c347 3575 /* Skip if already read in. */
976ca316 3576 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3577 continue;
3578
8943b874
DE
3579 /* Check static vs global. */
3580 if (attrs_valid)
3581 {
2b79f376
SM
3582 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3583
3584 if (iter->block_index.has_value ())
3585 {
3586 bool want_static = *iter->block_index == STATIC_BLOCK;
3587
3588 if (is_static != want_static)
3589 continue;
3590 }
3591
8943b874 3592 /* Work around gold/15646. */
f030440d
TV
3593 if (!is_static
3594 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3595 {
3596 if (iter->global_seen)
3597 continue;
3598
3599 iter->global_seen = 1;
3600 }
8943b874 3601 }
da51c347
DE
3602
3603 /* Only check the symbol's kind if it has one. */
3604 if (attrs_valid)
3605 {
3606 switch (iter->domain)
3607 {
3608 case VAR_DOMAIN:
3609 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3610 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3611 /* Some types are also in VAR_DOMAIN. */
3612 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3613 continue;
3614 break;
3615 case STRUCT_DOMAIN:
3616 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3617 continue;
3618 break;
3619 case LABEL_DOMAIN:
3620 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3621 continue;
3622 break;
59c35742
AB
3623 case MODULE_DOMAIN:
3624 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3625 continue;
3626 break;
da51c347
DE
3627 default:
3628 break;
3629 }
3630 }
3631
3632 ++iter->next;
3633 return per_cu;
3634 }
3635
3636 return NULL;
3637}
3638
39298a5d
TT
3639struct compunit_symtab *
3640dwarf2_gdb_index::lookup_symbol (struct objfile *objfile,
3641 block_enum block_index,
3642 const char *name, domain_enum domain)
9291a0cd 3643{
43f3e411 3644 struct compunit_symtab *stab_best = NULL;
976ca316 3645 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3646
b5ec771e
PA
3647 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3648
ed2dc618
SM
3649 struct dw2_symtab_iterator iter;
3650 struct dwarf2_per_cu_data *per_cu;
da51c347 3651
976ca316 3652 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3653
ed2dc618
SM
3654 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3655 {
3656 struct symbol *sym, *with_opaque = NULL;
97a1449a 3657 struct compunit_symtab *stab
976ca316 3658 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3659 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3660 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3661
ed2dc618
SM
3662 sym = block_find_symbol (block, name, domain,
3663 block_find_non_opaque_type_preferred,
3664 &with_opaque);
b2e2f908 3665
ed2dc618
SM
3666 /* Some caution must be observed with overloaded functions
3667 and methods, since the index will not contain any overload
3668 information (but NAME might contain it). */
da51c347 3669
ed2dc618
SM
3670 if (sym != NULL
3671 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3672 return stab;
3673 if (with_opaque != NULL
3674 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3675 stab_best = stab;
da51c347 3676
ed2dc618 3677 /* Keep looking through other CUs. */
9291a0cd 3678 }
9291a0cd 3679
da51c347 3680 return stab_best;
9291a0cd
TT
3681}
3682
39298a5d 3683void
4829711b
TT
3684dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3685 bool print_bcache)
9291a0cd 3686{
4829711b
TT
3687 if (print_bcache)
3688 return;
3689
976ca316
SM
3690 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3691 int total = (per_objfile->per_bfd->all_comp_units.size ()
3692 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3693 int count = 0;
9291a0cd 3694
ed2dc618 3695 for (int i = 0; i < total; ++i)
9291a0cd 3696 {
976ca316 3697 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3698
976ca316 3699 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3700 ++count;
3701 }
e4a48d9d 3702 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3703 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3704}
3705
779bd270
DE
3706/* This dumps minimal information about the index.
3707 It is called via "mt print objfiles".
3708 One use is to verify .gdb_index has been loaded by the
3709 gdb.dwarf2/gdb-index.exp testcase. */
3710
39298a5d
TT
3711void
3712dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3713{
976ca316 3714 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3715
976ca316 3716 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3717 printf_filtered (".gdb_index:");
976ca316 3718 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3719 {
3720 printf_filtered (" version %d\n",
976ca316 3721 per_objfile->per_bfd->index_table->version);
779bd270
DE
3722 }
3723 else
3724 printf_filtered (" faked for \"readnow\"\n");
3725 printf_filtered ("\n");
9291a0cd
TT
3726}
3727
39298a5d
TT
3728void
3729dwarf2_gdb_index::expand_symtabs_for_function (struct objfile *objfile,
3730 const char *func_name)
9291a0cd 3731{
976ca316 3732 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3733
ed2dc618
SM
3734 struct dw2_symtab_iterator iter;
3735 struct dwarf2_per_cu_data *per_cu;
da51c347 3736
976ca316 3737 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3738
ed2dc618 3739 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3740 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3741
9291a0cd
TT
3742}
3743
39298a5d
TT
3744void
3745dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3746{
976ca316
SM
3747 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3748 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3749 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3750
ed2dc618 3751 for (int i = 0; i < total_units; ++i)
9291a0cd 3752 {
976ca316 3753 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3754
58f0c718
TT
3755 /* We don't want to directly expand a partial CU, because if we
3756 read it with the wrong language, then assertion failures can
3757 be triggered later on. See PR symtab/23010. So, tell
3758 dw2_instantiate_symtab to skip partial CUs -- any important
3759 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3760 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3761 }
3762}
3763
39298a5d
TT
3764void
3765dwarf2_base_index_functions::expand_symtabs_with_fullname
3766 (struct objfile *objfile, const char *fullname)
9291a0cd 3767{
976ca316 3768 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3769
3770 /* We don't need to consider type units here.
3771 This is only called for examining code, e.g. expand_line_sal.
3772 There can be an order of magnitude (or more) more type units
3773 than comp units, and we avoid them if we can. */
3774
976ca316 3775 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3776 {
3d7bb9d9 3777 /* We only need to look at symtabs not already expanded. */
976ca316 3778 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3779 continue;
3780
976ca316 3781 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3782 if (file_data == NULL)
9291a0cd
TT
3783 continue;
3784
b76e467d 3785 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3786 {
652a8996
JK
3787 const char *this_fullname = file_data->file_names[j];
3788
3789 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3790 {
976ca316 3791 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3792 break;
3793 }
3794 }
3795 }
3796}
3797
9a0bacfb
TV
3798static void
3799dw2_expand_symtabs_matching_symbol
3800 (mapped_index_base &index,
3801 const lookup_name_info &lookup_name_in,
3802 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3803 enum search_domain kind,
fcf23d5b
SM
3804 gdb::function_view<bool (offset_type)> match_callback,
3805 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3806
3807static void
3808dw2_expand_symtabs_matching_one
97a1449a
SM
3809 (dwarf2_per_cu_data *per_cu,
3810 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3811 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3812 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3813
9291a0cd 3814static void
199b4314
TT
3815dw2_map_matching_symbols
3816 (struct objfile *objfile,
b054970d 3817 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3818 int global,
3819 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3820 symbol_compare_ftype *ordered_compare)
9291a0cd 3821{
1aa98955 3822 /* Used for Ada. */
976ca316 3823 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3824
9a0bacfb
TV
3825 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3826
976ca316 3827 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3828 {
976ca316 3829 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3830
9a0bacfb
TV
3831 const char *match_name = name.ada ().lookup_name ().c_str ();
3832 auto matcher = [&] (const char *symname)
3833 {
3834 if (ordered_compare == nullptr)
3835 return true;
3836 return ordered_compare (symname, match_name) == 0;
3837 };
3838
3839 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3840 [&] (offset_type namei)
3841 {
3842 struct dw2_symtab_iterator iter;
3843 struct dwarf2_per_cu_data *per_cu;
3844
976ca316 3845 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3846 namei);
9a0bacfb 3847 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3848 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3849 nullptr);
9a0bacfb 3850 return true;
976ca316 3851 }, per_objfile);
9a0bacfb
TV
3852 }
3853 else
3854 {
3855 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3856 proceed assuming all symtabs have been read in. */
3857 }
1aa98955
TV
3858
3859 for (compunit_symtab *cust : objfile->compunits ())
3860 {
3861 const struct block *block;
3862
3863 if (cust == NULL)
3864 continue;
3865 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3866 if (!iterate_over_symbols_terminated (block, name,
3867 domain, callback))
3868 return;
3869 }
9291a0cd
TT
3870}
3871
39298a5d
TT
3872void
3873dwarf2_gdb_index::map_matching_symbols
3874 (struct objfile *objfile,
3875 const lookup_name_info &name, domain_enum domain,
3876 int global,
3877 gdb::function_view<symbol_found_callback_ftype> callback,
3878 symbol_compare_ftype *ordered_compare)
3879{
3880 dw2_map_matching_symbols (objfile, name, domain, global, callback,
3881 ordered_compare);
3882}
3883
e1ef7d7a
PA
3884/* Starting from a search name, return the string that finds the upper
3885 bound of all strings that start with SEARCH_NAME in a sorted name
3886 list. Returns the empty string to indicate that the upper bound is
3887 the end of the list. */
3888
3889static std::string
3890make_sort_after_prefix_name (const char *search_name)
3891{
3892 /* When looking to complete "func", we find the upper bound of all
3893 symbols that start with "func" by looking for where we'd insert
3894 the closest string that would follow "func" in lexicographical
3895 order. Usually, that's "func"-with-last-character-incremented,
3896 i.e. "fund". Mind non-ASCII characters, though. Usually those
3897 will be UTF-8 multi-byte sequences, but we can't be certain.
3898 Especially mind the 0xff character, which is a valid character in
3899 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3900 rule out compilers allowing it in identifiers. Note that
3901 conveniently, strcmp/strcasecmp are specified to compare
3902 characters interpreted as unsigned char. So what we do is treat
3903 the whole string as a base 256 number composed of a sequence of
3904 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3905 to 0, and carries 1 to the following more-significant position.
3906 If the very first character in SEARCH_NAME ends up incremented
3907 and carries/overflows, then the upper bound is the end of the
3908 list. The string after the empty string is also the empty
3909 string.
3910
3911 Some examples of this operation:
3912
3913 SEARCH_NAME => "+1" RESULT
3914
3915 "abc" => "abd"
3916 "ab\xff" => "ac"
3917 "\xff" "a" "\xff" => "\xff" "b"
3918 "\xff" => ""
3919 "\xff\xff" => ""
3920 "" => ""
3921
3922 Then, with these symbols for example:
3923
3924 func
3925 func1
3926 fund
3927
3928 completing "func" looks for symbols between "func" and
3929 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3930 which finds "func" and "func1", but not "fund".
3931
3932 And with:
3933
3934 funcÿ (Latin1 'ÿ' [0xff])
3935 funcÿ1
3936 fund
3937
3938 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3939 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3940
3941 And with:
3942
3943 ÿÿ (Latin1 'ÿ' [0xff])
3944 ÿÿ1
3945
3946 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3947 the end of the list.
3948 */
3949 std::string after = search_name;
3950 while (!after.empty () && (unsigned char) after.back () == 0xff)
3951 after.pop_back ();
3952 if (!after.empty ())
3953 after.back () = (unsigned char) after.back () + 1;
3954 return after;
3955}
3956
5c58de74 3957/* See declaration. */
61d96d7e 3958
5c58de74
PA
3959std::pair<std::vector<name_component>::const_iterator,
3960 std::vector<name_component>::const_iterator>
44ed8f3e 3961mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3962 (const lookup_name_info &lookup_name_without_params, language lang,
3963 dwarf2_per_objfile *per_objfile) const
3f563c84 3964{
5c58de74
PA
3965 auto *name_cmp
3966 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3967
3b00ef10 3968 const char *lang_name
e0802d59 3969 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3970
3f563c84
PA
3971 /* Comparison function object for lower_bound that matches against a
3972 given symbol name. */
3973 auto lookup_compare_lower = [&] (const name_component &elem,
3974 const char *name)
3975 {
fcf23d5b 3976 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3977 const char *elem_name = elem_qualified + elem.name_offset;
3978 return name_cmp (elem_name, name) < 0;
3979 };
3980
3981 /* Comparison function object for upper_bound that matches against a
3982 given symbol name. */
3983 auto lookup_compare_upper = [&] (const char *name,
3984 const name_component &elem)
3985 {
fcf23d5b 3986 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3987 const char *elem_name = elem_qualified + elem.name_offset;
3988 return name_cmp (name, elem_name) < 0;
3989 };
3990
5c58de74
PA
3991 auto begin = this->name_components.begin ();
3992 auto end = this->name_components.end ();
3f563c84
PA
3993
3994 /* Find the lower bound. */
3995 auto lower = [&] ()
3996 {
3b00ef10 3997 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3998 return begin;
3999 else
3b00ef10 4000 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4001 } ();
4002
4003 /* Find the upper bound. */
4004 auto upper = [&] ()
4005 {
5c58de74 4006 if (lookup_name_without_params.completion_mode ())
3f563c84 4007 {
e1ef7d7a
PA
4008 /* In completion mode, we want UPPER to point past all
4009 symbols names that have the same prefix. I.e., with
4010 these symbols, and completing "func":
4011
4012 function << lower bound
4013 function1
4014 other_function << upper bound
4015
4016 We find the upper bound by looking for the insertion
4017 point of "func"-with-last-character-incremented,
4018 i.e. "fund". */
3b00ef10 4019 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4020 if (after.empty ())
3f563c84 4021 return end;
e6b2f5ef
PA
4022 return std::lower_bound (lower, end, after.c_str (),
4023 lookup_compare_lower);
3f563c84
PA
4024 }
4025 else
3b00ef10 4026 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4027 } ();
4028
5c58de74
PA
4029 return {lower, upper};
4030}
4031
4032/* See declaration. */
4033
4034void
fcf23d5b 4035mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
4036{
4037 if (!this->name_components.empty ())
4038 return;
4039
4040 this->name_components_casing = case_sensitivity;
4041 auto *name_cmp
4042 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4043
4044 /* The code below only knows how to break apart components of C++
4045 symbol names (and other languages that use '::' as
3b00ef10 4046 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4047 auto count = this->symbol_name_count ();
4048 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4049 {
44ed8f3e 4050 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4051 continue;
4052
fcf23d5b 4053 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
4054
4055 /* Add each name component to the name component table. */
4056 unsigned int previous_len = 0;
3b00ef10
TT
4057
4058 if (strstr (name, "::") != nullptr)
4059 {
4060 for (unsigned int current_len = cp_find_first_component (name);
4061 name[current_len] != '\0';
4062 current_len += cp_find_first_component (name + current_len))
4063 {
4064 gdb_assert (name[current_len] == ':');
4065 this->name_components.push_back ({previous_len, idx});
4066 /* Skip the '::'. */
4067 current_len += 2;
4068 previous_len = current_len;
4069 }
4070 }
4071 else
5c58de74 4072 {
3b00ef10
TT
4073 /* Handle the Ada encoded (aka mangled) form here. */
4074 for (const char *iter = strstr (name, "__");
4075 iter != nullptr;
4076 iter = strstr (iter, "__"))
4077 {
4078 this->name_components.push_back ({previous_len, idx});
4079 iter += 2;
4080 previous_len = iter - name;
4081 }
5c58de74 4082 }
3b00ef10 4083
5c58de74
PA
4084 this->name_components.push_back ({previous_len, idx});
4085 }
4086
4087 /* Sort name_components elements by name. */
4088 auto name_comp_compare = [&] (const name_component &left,
4089 const name_component &right)
4090 {
fcf23d5b
SM
4091 const char *left_qualified
4092 = this->symbol_name_at (left.idx, per_objfile);
4093 const char *right_qualified
4094 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4095
4096 const char *left_name = left_qualified + left.name_offset;
4097 const char *right_name = right_qualified + right.name_offset;
4098
4099 return name_cmp (left_name, right_name) < 0;
4100 };
4101
4102 std::sort (this->name_components.begin (),
4103 this->name_components.end (),
4104 name_comp_compare);
4105}
4106
4107/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4108 mapped_index_base instead of the containing objfile. This is split
4109 to a separate function in order to be able to unit test the
4110 name_components matching using a mock mapped_index_base. For each
5c58de74 4111 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4112 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4113
4114static void
4115dw2_expand_symtabs_matching_symbol
44ed8f3e 4116 (mapped_index_base &index,
5c58de74
PA
4117 const lookup_name_info &lookup_name_in,
4118 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4119 enum search_domain kind,
fcf23d5b
SM
4120 gdb::function_view<bool (offset_type)> match_callback,
4121 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4122{
4123 lookup_name_info lookup_name_without_params
4124 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4125
4126 /* Build the symbol name component sorted vector, if we haven't
4127 yet. */
fcf23d5b 4128 index.build_name_components (per_objfile);
5c58de74 4129
3f563c84
PA
4130 /* The same symbol may appear more than once in the range though.
4131 E.g., if we're looking for symbols that complete "w", and we have
4132 a symbol named "w1::w2", we'll find the two name components for
4133 that same symbol in the range. To be sure we only call the
4134 callback once per symbol, we first collect the symbol name
4135 indexes that matched in a temporary vector and ignore
4136 duplicates. */
4137 std::vector<offset_type> matches;
3f563c84 4138
3b00ef10
TT
4139 struct name_and_matcher
4140 {
4141 symbol_name_matcher_ftype *matcher;
ecc6c606 4142 const char *name;
3b00ef10
TT
4143
4144 bool operator== (const name_and_matcher &other) const
3f563c84 4145 {
ecc6c606 4146 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4147 }
4148 };
4149
4150 /* A vector holding all the different symbol name matchers, for all
4151 languages. */
4152 std::vector<name_and_matcher> matchers;
4153
4154 for (int i = 0; i < nr_languages; i++)
4155 {
4156 enum language lang_e = (enum language) i;
4157
4158 const language_defn *lang = language_def (lang_e);
4159 symbol_name_matcher_ftype *name_matcher
c9debfb9 4160 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4161
3b00ef10 4162 name_and_matcher key {
dda83cd7 4163 name_matcher,
3b00ef10
TT
4164 lookup_name_without_params.language_lookup_name (lang_e)
4165 };
4166
4167 /* Don't insert the same comparison routine more than once.
4168 Note that we do this linear walk. This is not a problem in
4169 practice because the number of supported languages is
4170 low. */
4171 if (std::find (matchers.begin (), matchers.end (), key)
4172 != matchers.end ())
9291a0cd 4173 continue;
3b00ef10
TT
4174 matchers.push_back (std::move (key));
4175
4176 auto bounds
4177 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4178 lang_e, per_objfile);
3b00ef10
TT
4179
4180 /* Now for each symbol name in range, check to see if we have a name
4181 match, and if so, call the MATCH_CALLBACK callback. */
4182
4183 for (; bounds.first != bounds.second; ++bounds.first)
4184 {
fcf23d5b
SM
4185 const char *qualified
4186 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4187
4188 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4189 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4190 continue;
9291a0cd 4191
3b00ef10
TT
4192 matches.push_back (bounds.first->idx);
4193 }
3f563c84
PA
4194 }
4195
4196 std::sort (matches.begin (), matches.end ());
4197
4198 /* Finally call the callback, once per match. */
4199 ULONGEST prev = -1;
4200 for (offset_type idx : matches)
4201 {
4202 if (prev != idx)
4203 {
3b00ef10
TT
4204 if (!match_callback (idx))
4205 break;
3f563c84
PA
4206 prev = idx;
4207 }
4208 }
4209
4210 /* Above we use a type wider than idx's for 'prev', since 0 and
4211 (offset_type)-1 are both possible values. */
4212 static_assert (sizeof (prev) > sizeof (offset_type), "");
4213}
4214
c62446b1
PA
4215#if GDB_SELF_TEST
4216
4217namespace selftests { namespace dw2_expand_symtabs_matching {
4218
a3c5fafd
PA
4219/* A mock .gdb_index/.debug_names-like name index table, enough to
4220 exercise dw2_expand_symtabs_matching_symbol, which works with the
4221 mapped_index_base interface. Builds an index from the symbol list
4222 passed as parameter to the constructor. */
4223class mock_mapped_index : public mapped_index_base
c62446b1
PA
4224{
4225public:
a3c5fafd
PA
4226 mock_mapped_index (gdb::array_view<const char *> symbols)
4227 : m_symbol_table (symbols)
c62446b1
PA
4228 {}
4229
a3c5fafd 4230 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4231
a3c5fafd 4232 /* Return the number of names in the symbol table. */
632e107b 4233 size_t symbol_name_count () const override
c62446b1 4234 {
a3c5fafd 4235 return m_symbol_table.size ();
c62446b1
PA
4236 }
4237
a3c5fafd 4238 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4239 const char *symbol_name_at
4240 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4241 {
4242 return m_symbol_table[idx];
4243 }
c62446b1 4244
a3c5fafd
PA
4245private:
4246 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4247};
4248
4249/* Convenience function that converts a NULL pointer to a "<null>"
4250 string, to pass to print routines. */
4251
4252static const char *
4253string_or_null (const char *str)
4254{
4255 return str != NULL ? str : "<null>";
4256}
4257
4258/* Check if a lookup_name_info built from
4259 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4260 index. EXPECTED_LIST is the list of expected matches, in expected
4261 matching order. If no match expected, then an empty list is
4262 specified. Returns true on success. On failure prints a warning
4263 indicating the file:line that failed, and returns false. */
4264
4265static bool
4266check_match (const char *file, int line,
4267 mock_mapped_index &mock_index,
4268 const char *name, symbol_name_match_type match_type,
4269 bool completion_mode,
fcf23d5b
SM
4270 std::initializer_list<const char *> expected_list,
4271 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4272{
4273 lookup_name_info lookup_name (name, match_type, completion_mode);
4274
4275 bool matched = true;
4276
4277 auto mismatch = [&] (const char *expected_str,
4278 const char *got)
4279 {
4280 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4281 "expected=\"%s\", got=\"%s\"\n"),
4282 file, line,
4283 (match_type == symbol_name_match_type::FULL
4284 ? "FULL" : "WILD"),
4285 name, string_or_null (expected_str), string_or_null (got));
4286 matched = false;
4287 };
4288
4289 auto expected_it = expected_list.begin ();
4290 auto expected_end = expected_list.end ();
4291
a3c5fafd 4292 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4293 NULL, ALL_DOMAIN,
4294 [&] (offset_type idx)
4295 {
fcf23d5b 4296 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4297 const char *expected_str
4298 = expected_it == expected_end ? NULL : *expected_it++;
4299
4300 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4301 mismatch (expected_str, matched_name);
3b00ef10 4302 return true;
fcf23d5b 4303 }, per_objfile);
c62446b1
PA
4304
4305 const char *expected_str
4306 = expected_it == expected_end ? NULL : *expected_it++;
4307 if (expected_str != NULL)
4308 mismatch (expected_str, NULL);
4309
4310 return matched;
4311}
4312
4313/* The symbols added to the mock mapped_index for testing (in
4314 canonical form). */
4315static const char *test_symbols[] = {
4316 "function",
4317 "std::bar",
4318 "std::zfunction",
4319 "std::zfunction2",
4320 "w1::w2",
4321 "ns::foo<char*>",
4322 "ns::foo<int>",
4323 "ns::foo<long>",
a20714ff
PA
4324 "ns2::tmpl<int>::foo2",
4325 "(anonymous namespace)::A::B::C",
c62446b1 4326
e1ef7d7a
PA
4327 /* These are used to check that the increment-last-char in the
4328 matching algorithm for completion doesn't match "t1_fund" when
4329 completing "t1_func". */
4330 "t1_func",
4331 "t1_func1",
4332 "t1_fund",
4333 "t1_fund1",
4334
4335 /* A UTF-8 name with multi-byte sequences to make sure that
4336 cp-name-parser understands this as a single identifier ("função"
4337 is "function" in PT). */
4338 u8"u8função",
4339
4340 /* \377 (0xff) is Latin1 'ÿ'. */
4341 "yfunc\377",
4342
4343 /* \377 (0xff) is Latin1 'ÿ'. */
4344 "\377",
4345 "\377\377123",
4346
c62446b1
PA
4347 /* A name with all sorts of complications. Starts with "z" to make
4348 it easier for the completion tests below. */
4349#define Z_SYM_NAME \
4350 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4351 "::tuple<(anonymous namespace)::ui*, " \
4352 "std::default_delete<(anonymous namespace)::ui>, void>"
4353
4354 Z_SYM_NAME
4355};
4356
a3c5fafd
PA
4357/* Returns true if the mapped_index_base::find_name_component_bounds
4358 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4359 in completion mode. */
5c58de74
PA
4360
4361static bool
a3c5fafd 4362check_find_bounds_finds (mapped_index_base &index,
5c58de74 4363 const char *search_name,
fcf23d5b
SM
4364 gdb::array_view<const char *> expected_syms,
4365 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4366{
4367 lookup_name_info lookup_name (search_name,
4368 symbol_name_match_type::FULL, true);
4369
3b00ef10 4370 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4371 language_cplus,
4372 per_objfile);
5c58de74
PA
4373
4374 size_t distance = std::distance (bounds.first, bounds.second);
4375 if (distance != expected_syms.size ())
4376 return false;
4377
4378 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4379 {
4380 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4381 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4382 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4383 return false;
4384 }
4385
4386 return true;
4387}
4388
4389/* Test the lower-level mapped_index::find_name_component_bounds
4390 method. */
4391
c62446b1 4392static void
5c58de74
PA
4393test_mapped_index_find_name_component_bounds ()
4394{
4395 mock_mapped_index mock_index (test_symbols);
4396
fcf23d5b 4397 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4398
4399 /* Test the lower-level mapped_index::find_name_component_bounds
4400 method in completion mode. */
4401 {
4402 static const char *expected_syms[] = {
4403 "t1_func",
4404 "t1_func1",
5c58de74
PA
4405 };
4406
fcf23d5b
SM
4407 SELF_CHECK (check_find_bounds_finds
4408 (mock_index, "t1_func", expected_syms,
4409 NULL /* per_objfile */));
5c58de74
PA
4410 }
4411
4412 /* Check that the increment-last-char in the name matching algorithm
4413 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4414 {
4415 static const char *expected_syms1[] = {
4416 "\377",
4417 "\377\377123",
4418 };
fcf23d5b
SM
4419 SELF_CHECK (check_find_bounds_finds
4420 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4421
4422 static const char *expected_syms2[] = {
4423 "\377\377123",
4424 };
fcf23d5b
SM
4425 SELF_CHECK (check_find_bounds_finds
4426 (mock_index, "\377\377", expected_syms2,
4427 NULL /* per_objfile */));
5c58de74
PA
4428 }
4429}
4430
4431/* Test dw2_expand_symtabs_matching_symbol. */
4432
4433static void
4434test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4435{
4436 mock_mapped_index mock_index (test_symbols);
4437
4438 /* We let all tests run until the end even if some fails, for debug
4439 convenience. */
4440 bool any_mismatch = false;
4441
4442 /* Create the expected symbols list (an initializer_list). Needed
4443 because lists have commas, and we need to pass them to CHECK,
4444 which is a macro. */
4445#define EXPECT(...) { __VA_ARGS__ }
4446
4447 /* Wrapper for check_match that passes down the current
4448 __FILE__/__LINE__. */
4449#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4450 any_mismatch |= !check_match (__FILE__, __LINE__, \
4451 mock_index, \
4452 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4453 EXPECTED_LIST, NULL)
c62446b1
PA
4454
4455 /* Identity checks. */
4456 for (const char *sym : test_symbols)
4457 {
4458 /* Should be able to match all existing symbols. */
4459 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4460 EXPECT (sym));
4461
4462 /* Should be able to match all existing symbols with
4463 parameters. */
4464 std::string with_params = std::string (sym) + "(int)";
4465 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4466 EXPECT (sym));
4467
4468 /* Should be able to match all existing symbols with
4469 parameters and qualifiers. */
4470 with_params = std::string (sym) + " ( int ) const";
4471 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4472 EXPECT (sym));
4473
4474 /* This should really find sym, but cp-name-parser.y doesn't
4475 know about lvalue/rvalue qualifiers yet. */
4476 with_params = std::string (sym) + " ( int ) &&";
4477 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4478 {});
4479 }
4480
e1ef7d7a
PA
4481 /* Check that the name matching algorithm for completion doesn't get
4482 confused with Latin1 'ÿ' / 0xff. */
4483 {
4484 static const char str[] = "\377";
4485 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4486 EXPECT ("\377", "\377\377123"));
4487 }
4488
4489 /* Check that the increment-last-char in the matching algorithm for
4490 completion doesn't match "t1_fund" when completing "t1_func". */
4491 {
4492 static const char str[] = "t1_func";
4493 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4494 EXPECT ("t1_func", "t1_func1"));
4495 }
4496
c62446b1
PA
4497 /* Check that completion mode works at each prefix of the expected
4498 symbol name. */
4499 {
4500 static const char str[] = "function(int)";
4501 size_t len = strlen (str);
4502 std::string lookup;
4503
4504 for (size_t i = 1; i < len; i++)
4505 {
4506 lookup.assign (str, i);
4507 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4508 EXPECT ("function"));
4509 }
4510 }
4511
4512 /* While "w" is a prefix of both components, the match function
4513 should still only be called once. */
4514 {
4515 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4516 EXPECT ("w1::w2"));
a20714ff
PA
4517 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4518 EXPECT ("w1::w2"));
c62446b1
PA
4519 }
4520
4521 /* Same, with a "complicated" symbol. */
4522 {
4523 static const char str[] = Z_SYM_NAME;
4524 size_t len = strlen (str);
4525 std::string lookup;
4526
4527 for (size_t i = 1; i < len; i++)
4528 {
4529 lookup.assign (str, i);
4530 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4531 EXPECT (Z_SYM_NAME));
4532 }
4533 }
4534
4535 /* In FULL mode, an incomplete symbol doesn't match. */
4536 {
4537 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4538 {});
4539 }
4540
4541 /* A complete symbol with parameters matches any overload, since the
4542 index has no overload info. */
4543 {
4544 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4545 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4546 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4547 EXPECT ("std::zfunction", "std::zfunction2"));
4548 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4549 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4550 }
4551
4552 /* Check that whitespace is ignored appropriately. A symbol with a
4553 template argument list. */
4554 {
4555 static const char expected[] = "ns::foo<int>";
4556 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4557 EXPECT (expected));
a20714ff
PA
4558 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4559 EXPECT (expected));
c62446b1
PA
4560 }
4561
4562 /* Check that whitespace is ignored appropriately. A symbol with a
4563 template argument list that includes a pointer. */
4564 {
4565 static const char expected[] = "ns::foo<char*>";
4566 /* Try both completion and non-completion modes. */
4567 static const bool completion_mode[2] = {false, true};
4568 for (size_t i = 0; i < 2; i++)
4569 {
4570 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4571 completion_mode[i], EXPECT (expected));
a20714ff
PA
4572 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4573 completion_mode[i], EXPECT (expected));
c62446b1
PA
4574
4575 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4576 completion_mode[i], EXPECT (expected));
a20714ff
PA
4577 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4578 completion_mode[i], EXPECT (expected));
c62446b1
PA
4579 }
4580 }
4581
4582 {
4583 /* Check method qualifiers are ignored. */
4584 static const char expected[] = "ns::foo<char*>";
4585 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4586 symbol_name_match_type::FULL, true, EXPECT (expected));
4587 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4588 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4589 CHECK_MATCH ("foo < char * > ( int ) const",
4590 symbol_name_match_type::WILD, true, EXPECT (expected));
4591 CHECK_MATCH ("foo < char * > ( int ) &&",
4592 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4593 }
4594
4595 /* Test lookup names that don't match anything. */
4596 {
a20714ff
PA
4597 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4598 {});
4599
c62446b1
PA
4600 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4601 {});
4602 }
4603
a20714ff
PA
4604 /* Some wild matching tests, exercising "(anonymous namespace)",
4605 which should not be confused with a parameter list. */
4606 {
4607 static const char *syms[] = {
4608 "A::B::C",
4609 "B::C",
4610 "C",
4611 "A :: B :: C ( int )",
4612 "B :: C ( int )",
4613 "C ( int )",
4614 };
4615
4616 for (const char *s : syms)
4617 {
4618 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4619 EXPECT ("(anonymous namespace)::A::B::C"));
4620 }
4621 }
4622
4623 {
4624 static const char expected[] = "ns2::tmpl<int>::foo2";
4625 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4626 EXPECT (expected));
4627 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4628 EXPECT (expected));
4629 }
4630
c62446b1
PA
4631 SELF_CHECK (!any_mismatch);
4632
4633#undef EXPECT
4634#undef CHECK_MATCH
4635}
4636
5c58de74
PA
4637static void
4638run_test ()
4639{
4640 test_mapped_index_find_name_component_bounds ();
4641 test_dw2_expand_symtabs_matching_symbol ();
4642}
4643
c62446b1
PA
4644}} // namespace selftests::dw2_expand_symtabs_matching
4645
4646#endif /* GDB_SELF_TEST */
4647
4b514bc8
JK
4648/* If FILE_MATCHER is NULL or if PER_CU has
4649 dwarf2_per_cu_quick_data::MARK set (see
4650 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4651 EXPANSION_NOTIFY on it. */
4652
4653static void
4654dw2_expand_symtabs_matching_one
97a1449a
SM
4655 (dwarf2_per_cu_data *per_cu,
4656 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4657 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4658 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4659{
4660 if (file_matcher == NULL || per_cu->v.quick->mark)
4661 {
af758d11 4662 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4663
97a1449a
SM
4664 compunit_symtab *symtab
4665 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4666 gdb_assert (symtab != nullptr);
4b514bc8 4667
af758d11
SM
4668 if (expansion_notify != NULL && symtab_was_null)
4669 expansion_notify (symtab);
4b514bc8
JK
4670 }
4671}
4672
3f563c84
PA
4673/* Helper for dw2_expand_matching symtabs. Called on each symbol
4674 matched, to expand corresponding CUs that were marked. IDX is the
4675 index of the symbol name that matched. */
4676
4677static void
4678dw2_expand_marked_cus
976ca316 4679 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4680 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4681 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4682 search_domain kind)
4683{
3f563c84
PA
4684 offset_type *vec, vec_len, vec_idx;
4685 bool global_seen = false;
976ca316 4686 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4687
61920122 4688 vec = (offset_type *) (index.constant_pool
f00a2de2 4689 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4690 vec_len = MAYBE_SWAP (vec[0]);
4691 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4692 {
61920122
PA
4693 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4694 /* This value is only valid for index versions >= 7. */
4695 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4696 gdb_index_symbol_kind symbol_kind =
4697 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4698 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4699 /* Only check the symbol attributes if they're present.
4700 Indices prior to version 7 don't record them,
4701 and indices >= 7 may elide them for certain symbols
4702 (gold does this). */
4703 int attrs_valid =
4704 (index.version >= 7
4705 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4706
4707 /* Work around gold/15646. */
f030440d
TV
4708 if (attrs_valid
4709 && !is_static
4710 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4711 {
f030440d 4712 if (global_seen)
61920122 4713 continue;
f030440d
TV
4714
4715 global_seen = true;
61920122 4716 }
3190f0c6 4717
61920122
PA
4718 /* Only check the symbol's kind if it has one. */
4719 if (attrs_valid)
4720 {
4721 switch (kind)
8943b874 4722 {
61920122
PA
4723 case VARIABLES_DOMAIN:
4724 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4725 continue;
4726 break;
4727 case FUNCTIONS_DOMAIN:
4728 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4729 continue;
61920122
PA
4730 break;
4731 case TYPES_DOMAIN:
4732 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4733 continue;
4734 break;
59c35742
AB
4735 case MODULES_DOMAIN:
4736 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4737 continue;
4738 break;
61920122
PA
4739 default:
4740 break;
8943b874 4741 }
61920122 4742 }
8943b874 4743
61920122 4744 /* Don't crash on bad data. */
976ca316
SM
4745 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4746 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4747 {
b98664d3 4748 complaint (_(".gdb_index entry has bad CU index"
976ca316 4749 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4750 continue;
4751 }
4752
976ca316
SM
4753 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4754 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4755 expansion_notify);
61920122
PA
4756 }
4757}
4758
4b514bc8
JK
4759/* If FILE_MATCHER is non-NULL, set all the
4760 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4761 that match FILE_MATCHER. */
4762
61920122 4763static void
4b514bc8 4764dw_expand_symtabs_matching_file_matcher
976ca316 4765 (dwarf2_per_objfile *per_objfile,
ed2dc618 4766 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4767{
4b514bc8 4768 if (file_matcher == NULL)
61920122
PA
4769 return;
4770
4b514bc8
JK
4771 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4772 htab_eq_pointer,
4773 NULL, xcalloc, xfree));
4774 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4775 htab_eq_pointer,
4776 NULL, xcalloc, xfree));
61920122 4777
4b514bc8
JK
4778 /* The rule is CUs specify all the files, including those used by
4779 any TU, so there's no need to scan TUs here. */
61920122 4780
976ca316 4781 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4782 {
927aa2e7
JK
4783 QUIT;
4784
4785 per_cu->v.quick->mark = 0;
4786
4787 /* We only need to look at symtabs not already expanded. */
976ca316 4788 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4789 continue;
4790
976ca316 4791 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4792 if (file_data == NULL)
4793 continue;
4794
4795 if (htab_find (visited_not_found.get (), file_data) != NULL)
4796 continue;
4797 else if (htab_find (visited_found.get (), file_data) != NULL)
4798 {
4799 per_cu->v.quick->mark = 1;
4800 continue;
4801 }
4802
b76e467d 4803 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4804 {
4805 const char *this_real_name;
4806
4807 if (file_matcher (file_data->file_names[j], false))
4808 {
4809 per_cu->v.quick->mark = 1;
4810 break;
4811 }
4812
4813 /* Before we invoke realpath, which can get expensive when many
4814 files are involved, do a quick comparison of the basenames. */
4815 if (!basenames_may_differ
4816 && !file_matcher (lbasename (file_data->file_names[j]),
4817 true))
4818 continue;
4819
976ca316 4820 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4821 if (file_matcher (this_real_name, false))
4822 {
4823 per_cu->v.quick->mark = 1;
4824 break;
4825 }
4826 }
4827
b76e467d
SM
4828 void **slot = htab_find_slot (per_cu->v.quick->mark
4829 ? visited_found.get ()
4830 : visited_not_found.get (),
4831 file_data, INSERT);
927aa2e7
JK
4832 *slot = file_data;
4833 }
4834}
4835
4836static void
4837dw2_expand_symtabs_matching
4838 (struct objfile *objfile,
4839 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4840 const lookup_name_info *lookup_name,
927aa2e7
JK
4841 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4842 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4843 enum search_domain kind)
4844{
976ca316 4845 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4846
4847 /* index_table is NULL if OBJF_READNOW. */
976ca316 4848 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4849 return;
4850
976ca316 4851 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4852
c1a66c06
TV
4853 if (symbol_matcher == NULL && lookup_name == NULL)
4854 {
976ca316 4855 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4856 {
4857 QUIT;
4858
976ca316 4859 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4860 file_matcher, expansion_notify);
c1a66c06
TV
4861 }
4862 return;
4863 }
4864
976ca316 4865 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4866
c1a66c06 4867 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4868 symbol_matcher,
4869 kind, [&] (offset_type idx)
4870 {
976ca316
SM
4871 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4872 kind);
3b00ef10 4873 return true;
976ca316 4874 }, per_objfile);
927aa2e7
JK
4875}
4876
39298a5d
TT
4877void
4878dwarf2_gdb_index::expand_symtabs_matching
4879 (struct objfile *objfile,
4880 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4881 const lookup_name_info *lookup_name,
4882 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4883 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4884 enum search_domain kind)
4885{
4886 dw2_expand_symtabs_matching (objfile, file_matcher, lookup_name,
4887 symbol_matcher, expansion_notify, kind);
4888}
4889
927aa2e7
JK
4890/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4891 symtab. */
4892
4893static struct compunit_symtab *
4894recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4895 CORE_ADDR pc)
4896{
4897 int i;
4898
4899 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4900 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4901 return cust;
4902
4903 if (cust->includes == NULL)
4904 return NULL;
4905
4906 for (i = 0; cust->includes[i]; ++i)
4907 {
4908 struct compunit_symtab *s = cust->includes[i];
4909
4910 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4911 if (s != NULL)
4912 return s;
4913 }
4914
4915 return NULL;
4916}
4917
39298a5d
TT
4918struct compunit_symtab *
4919dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4920 (struct objfile *objfile,
4921 struct bound_minimal_symbol msymbol,
4922 CORE_ADDR pc,
4923 struct obj_section *section,
4924 int warn_if_readin)
927aa2e7
JK
4925{
4926 struct dwarf2_per_cu_data *data;
4927 struct compunit_symtab *result;
4928
efd7398e
TT
4929 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4930 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4931 return NULL;
4932
b3b3bada 4933 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4934 data = ((struct dwarf2_per_cu_data *)
4935 addrmap_find (per_objfile->per_bfd->index_addrmap,
4936 pc - baseaddr));
927aa2e7
JK
4937 if (!data)
4938 return NULL;
4939
af758d11 4940 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4941 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4942 paddress (objfile->arch (), pc));
927aa2e7 4943
97a1449a
SM
4944 result = recursively_find_pc_sect_compunit_symtab
4945 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4946
927aa2e7
JK
4947 gdb_assert (result != NULL);
4948 return result;
4949}
4950
39298a5d
TT
4951void
4952dwarf2_base_index_functions::map_symbol_filenames (struct objfile *objfile,
4953 symbol_filename_ftype *fun,
4954 void *data,
4955 int need_fullname)
927aa2e7 4956{
976ca316 4957 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4958
976ca316 4959 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4960 {
976ca316 4961 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4962
4963 htab_up visited (htab_create_alloc (10,
4964 htab_hash_pointer, htab_eq_pointer,
4965 NULL, xcalloc, xfree));
4966
4967 /* The rule is CUs specify all the files, including those used
4968 by any TU, so there's no need to scan TUs here. We can
4969 ignore file names coming from already-expanded CUs. */
4970
976ca316 4971 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4972 {
976ca316 4973 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4974 {
4975 void **slot = htab_find_slot (visited.get (),
4976 per_cu->v.quick->file_names,
4977 INSERT);
4978
4979 *slot = per_cu->v.quick->file_names;
4980 }
4981 }
4982
976ca316 4983 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4984 {
927aa2e7 4985 /* We only need to look at symtabs not already expanded. */
976ca316 4986 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4987 continue;
4988
ab432490 4989 quick_file_names *file_data
976ca316 4990 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4991 if (file_data == NULL)
4992 continue;
4993
b76e467d 4994 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4995 if (*slot)
4996 {
4997 /* Already visited. */
4998 continue;
4999 }
5000 *slot = file_data;
5001
5002 for (int j = 0; j < file_data->num_file_names; ++j)
5003 {
5004 const char *filename = file_data->file_names[j];
976ca316 5005 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
5006 }
5007 }
5008 }
5009
976ca316 5010 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
5011 {
5012 gdb::unique_xmalloc_ptr<char> this_real_name;
5013
5014 if (need_fullname)
5015 this_real_name = gdb_realpath (filename);
5016 (*fun) (filename, this_real_name.get (), data);
5017 });
5018}
5019
39298a5d
TT
5020bool
5021dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 5022{
fae2120b 5023 return true;
927aa2e7
JK
5024}
5025
927aa2e7
JK
5026/* DWARF-5 debug_names reader. */
5027
5028/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5029static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5030
5031/* A helper function that reads the .debug_names section in SECTION
5032 and fills in MAP. FILENAME is the name of the file containing the
5033 section; it is used for error reporting.
5034
5035 Returns true if all went well, false otherwise. */
5036
5037static bool
5038read_debug_names_from_section (struct objfile *objfile,
5039 const char *filename,
5040 struct dwarf2_section_info *section,
5041 mapped_debug_names &map)
5042{
96b79293 5043 if (section->empty ())
927aa2e7
JK
5044 return false;
5045
5046 /* Older elfutils strip versions could keep the section in the main
5047 executable while splitting it for the separate debug info file. */
96b79293 5048 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5049 return false;
5050
96b79293 5051 section->read (objfile);
927aa2e7 5052
08feed99 5053 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
5054
5055 const gdb_byte *addr = section->buffer;
5056
96b79293 5057 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5058
5059 unsigned int bytes_read;
5060 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5061 addr += bytes_read;
5062
5063 map.dwarf5_is_dwarf64 = bytes_read != 4;
5064 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5065 if (bytes_read + length != section->size)
5066 {
5067 /* There may be multiple per-CU indices. */
5068 warning (_("Section .debug_names in %s length %s does not match "
5069 "section length %s, ignoring .debug_names."),
5070 filename, plongest (bytes_read + length),
5071 pulongest (section->size));
5072 return false;
5073 }
5074
5075 /* The version number. */
5076 uint16_t version = read_2_bytes (abfd, addr);
5077 addr += 2;
5078 if (version != 5)
5079 {
5080 warning (_("Section .debug_names in %s has unsupported version %d, "
5081 "ignoring .debug_names."),
5082 filename, version);
5083 return false;
5084 }
5085
5086 /* Padding. */
5087 uint16_t padding = read_2_bytes (abfd, addr);
5088 addr += 2;
5089 if (padding != 0)
5090 {
5091 warning (_("Section .debug_names in %s has unsupported padding %d, "
5092 "ignoring .debug_names."),
5093 filename, padding);
5094 return false;
5095 }
5096
5097 /* comp_unit_count - The number of CUs in the CU list. */
5098 map.cu_count = read_4_bytes (abfd, addr);
5099 addr += 4;
5100
5101 /* local_type_unit_count - The number of TUs in the local TU
5102 list. */
5103 map.tu_count = read_4_bytes (abfd, addr);
5104 addr += 4;
5105
5106 /* foreign_type_unit_count - The number of TUs in the foreign TU
5107 list. */
5108 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5109 addr += 4;
5110 if (foreign_tu_count != 0)
5111 {
5112 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5113 "ignoring .debug_names."),
5114 filename, static_cast<unsigned long> (foreign_tu_count));
5115 return false;
5116 }
5117
5118 /* bucket_count - The number of hash buckets in the hash lookup
5119 table. */
5120 map.bucket_count = read_4_bytes (abfd, addr);
5121 addr += 4;
5122
5123 /* name_count - The number of unique names in the index. */
5124 map.name_count = read_4_bytes (abfd, addr);
5125 addr += 4;
5126
5127 /* abbrev_table_size - The size in bytes of the abbreviations
5128 table. */
5129 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5130 addr += 4;
5131
5132 /* augmentation_string_size - The size in bytes of the augmentation
5133 string. This value is rounded up to a multiple of 4. */
5134 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5135 addr += 4;
5136 map.augmentation_is_gdb = ((augmentation_string_size
5137 == sizeof (dwarf5_augmentation))
5138 && memcmp (addr, dwarf5_augmentation,
5139 sizeof (dwarf5_augmentation)) == 0);
5140 augmentation_string_size += (-augmentation_string_size) & 3;
5141 addr += augmentation_string_size;
5142
5143 /* List of CUs */
5144 map.cu_table_reordered = addr;
5145 addr += map.cu_count * map.offset_size;
5146
5147 /* List of Local TUs */
5148 map.tu_table_reordered = addr;
5149 addr += map.tu_count * map.offset_size;
5150
5151 /* Hash Lookup Table */
5152 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5153 addr += map.bucket_count * 4;
5154 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5155 addr += map.name_count * 4;
5156
5157 /* Name Table */
5158 map.name_table_string_offs_reordered = addr;
5159 addr += map.name_count * map.offset_size;
5160 map.name_table_entry_offs_reordered = addr;
5161 addr += map.name_count * map.offset_size;
5162
5163 const gdb_byte *abbrev_table_start = addr;
5164 for (;;)
5165 {
927aa2e7
JK
5166 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5167 addr += bytes_read;
5168 if (index_num == 0)
5169 break;
5170
5171 const auto insertpair
5172 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5173 if (!insertpair.second)
5174 {
5175 warning (_("Section .debug_names in %s has duplicate index %s, "
5176 "ignoring .debug_names."),
5177 filename, pulongest (index_num));
5178 return false;
5179 }
5180 mapped_debug_names::index_val &indexval = insertpair.first->second;
5181 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5182 addr += bytes_read;
5183
5184 for (;;)
5185 {
5186 mapped_debug_names::index_val::attr attr;
5187 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5188 addr += bytes_read;
5189 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5190 addr += bytes_read;
5191 if (attr.form == DW_FORM_implicit_const)
5192 {
5193 attr.implicit_const = read_signed_leb128 (abfd, addr,
5194 &bytes_read);
5195 addr += bytes_read;
5196 }
5197 if (attr.dw_idx == 0 && attr.form == 0)
5198 break;
5199 indexval.attr_vec.push_back (std::move (attr));
5200 }
5201 }
5202 if (addr != abbrev_table_start + abbrev_table_size)
5203 {
5204 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5205 "of size %s vs. written as %u, ignoring .debug_names."),
5206 filename, plongest (addr - abbrev_table_start),
5207 abbrev_table_size);
927aa2e7
JK
5208 return false;
5209 }
5210 map.entry_pool = addr;
5211
5212 return true;
5213}
5214
5215/* A helper for create_cus_from_debug_names that handles the MAP's CU
5216 list. */
5217
5218static void
168c9250 5219create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5220 const mapped_debug_names &map,
5221 dwarf2_section_info &section,
b76e467d 5222 bool is_dwz)
927aa2e7 5223{
3ee6bb11
TV
5224 if (!map.augmentation_is_gdb)
5225 {
fc9a13fb
TV
5226 for (uint32_t i = 0; i < map.cu_count; ++i)
5227 {
5228 sect_offset sect_off
5229 = (sect_offset) (extract_unsigned_integer
5230 (map.cu_table_reordered + i * map.offset_size,
5231 map.offset_size,
5232 map.dwarf5_byte_order));
5233 /* We don't know the length of the CU, because the CU list in a
5234 .debug_names index can be incomplete, so we can't use the start
5235 of the next CU as end of this CU. We create the CUs here with
5236 length 0, and in cutu_reader::cutu_reader we'll fill in the
5237 actual length. */
5238 dwarf2_per_cu_data *per_cu
5239 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5240 sect_off, 0);
5241 per_bfd->all_comp_units.push_back (per_cu);
5242 }
d3b54e63 5243 return;
3ee6bb11
TV
5244 }
5245
927aa2e7
JK
5246 sect_offset sect_off_prev;
5247 for (uint32_t i = 0; i <= map.cu_count; ++i)
5248 {
5249 sect_offset sect_off_next;
5250 if (i < map.cu_count)
5251 {
5252 sect_off_next
5253 = (sect_offset) (extract_unsigned_integer
5254 (map.cu_table_reordered + i * map.offset_size,
5255 map.offset_size,
5256 map.dwarf5_byte_order));
5257 }
5258 else
5259 sect_off_next = (sect_offset) section.size;
5260 if (i >= 1)
5261 {
5262 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5263 dwarf2_per_cu_data *per_cu
168c9250 5264 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5265 sect_off_prev, length);
168c9250 5266 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5267 }
5268 sect_off_prev = sect_off_next;
5269 }
5270}
5271
5272/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5273 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5274
5275static void
168c9250 5276create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5277 const mapped_debug_names &map,
5278 const mapped_debug_names &dwz_map)
5279{
168c9250
SM
5280 gdb_assert (per_bfd->all_comp_units.empty ());
5281 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5282
168c9250 5283 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5284 false /* is_dwz */);
927aa2e7
JK
5285
5286 if (dwz_map.cu_count == 0)
5287 return;
5288
168c9250
SM
5289 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5290 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5291 true /* is_dwz */);
927aa2e7
JK
5292}
5293
5294/* Read .debug_names. If everything went ok, initialize the "quick"
5295 elements of all the CUs and return true. Otherwise, return false. */
5296
5297static bool
976ca316 5298dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5299{
fcf23d5b
SM
5300 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5301 mapped_debug_names dwz_map;
976ca316 5302 struct objfile *objfile = per_objfile->objfile;
f8c41851 5303 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5304
5305 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5306 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5307 return false;
5308
5309 /* Don't use the index if it's empty. */
22ca247e 5310 if (map->name_count == 0)
927aa2e7
JK
5311 return false;
5312
5313 /* If there is a .dwz file, read it so we can get its CU list as
5314 well. */
f8c41851 5315 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5316 if (dwz != NULL)
5317 {
5318 if (!read_debug_names_from_section (objfile,
00f93c44 5319 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5320 &dwz->debug_names, dwz_map))
5321 {
5322 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5323 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5324 return false;
5325 }
5326 }
5327
f8c41851 5328 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5329
22ca247e 5330 if (map->tu_count != 0)
927aa2e7
JK
5331 {
5332 /* We can only handle a single .debug_types when we have an
5333 index. */
f8c41851 5334 if (per_bfd->types.size () != 1)
927aa2e7
JK
5335 return false;
5336
f8c41851 5337 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5338
5339 create_signatured_type_table_from_debug_names
f8c41851 5340 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5341 }
5342
f8c41851 5343 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5344
f8c41851
SM
5345 per_bfd->debug_names_table = std::move (map);
5346 per_bfd->using_index = 1;
5347 per_bfd->quick_file_names_table =
976ca316 5348 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5349
5350 return true;
5351}
5352
927aa2e7
JK
5353/* Type used to manage iterating over all CUs looking for a symbol for
5354 .debug_names. */
5355
5356class dw2_debug_names_iterator
5357{
5358public:
927aa2e7 5359 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5360 gdb::optional<block_enum> block_index,
5361 domain_enum domain,
fcf23d5b 5362 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5363 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5364 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5365 m_per_objfile (per_objfile)
927aa2e7
JK
5366 {}
5367
5368 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5369 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5370 : m_map (map),
5371 m_search (search),
fcf23d5b
SM
5372 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5373 m_per_objfile (per_objfile)
927aa2e7
JK
5374 {}
5375
3b00ef10
TT
5376 dw2_debug_names_iterator (const mapped_debug_names &map,
5377 block_enum block_index, domain_enum domain,
fcf23d5b 5378 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5379 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5380 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5381 m_per_objfile (per_objfile)
3b00ef10
TT
5382 {}
5383
927aa2e7
JK
5384 /* Return the next matching CU or NULL if there are no more. */
5385 dwarf2_per_cu_data *next ();
5386
5387private:
5388 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5389 const char *name,
5390 dwarf2_per_objfile *per_objfile);
927aa2e7 5391 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5392 uint32_t namei,
5393 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5394
5395 /* The internalized form of .debug_names. */
5396 const mapped_debug_names &m_map;
5397
2b79f376
SM
5398 /* If set, only look for symbols that match that block. Valid values are
5399 GLOBAL_BLOCK and STATIC_BLOCK. */
5400 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5401
5402 /* The kind of symbol we're looking for. */
5403 const domain_enum m_domain = UNDEF_DOMAIN;
5404 const search_domain m_search = ALL_DOMAIN;
5405
5406 /* The list of CUs from the index entry of the symbol, or NULL if
5407 not found. */
5408 const gdb_byte *m_addr;
fcf23d5b
SM
5409
5410 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5411};
5412
5413const char *
fcf23d5b 5414mapped_debug_names::namei_to_name
976ca316 5415 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5416{
5417 const ULONGEST namei_string_offs
5418 = extract_unsigned_integer ((name_table_string_offs_reordered
5419 + namei * offset_size),
5420 offset_size,
5421 dwarf5_byte_order);
976ca316 5422 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5423}
5424
5425/* Find a slot in .debug_names for the object named NAME. If NAME is
5426 found, return pointer to its pool data. If NAME cannot be found,
5427 return NULL. */
5428
5429const gdb_byte *
5430dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5431 (const mapped_debug_names &map, const char *name,
5432 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5433{
5434 int (*cmp) (const char *, const char *);
5435
54ee4252 5436 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5437 if (current_language->la_language == language_cplus
5438 || current_language->la_language == language_fortran
5439 || current_language->la_language == language_d)
5440 {
5441 /* NAME is already canonical. Drop any qualifiers as
5442 .debug_names does not contain any. */
5443
5444 if (strchr (name, '(') != NULL)
5445 {
54ee4252 5446 without_params = cp_remove_params (name);
927aa2e7 5447 if (without_params != NULL)
54ee4252 5448 name = without_params.get ();
927aa2e7
JK
5449 }
5450 }
5451
5452 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5453
5454 const uint32_t full_hash = dwarf5_djb_hash (name);
5455 uint32_t namei
5456 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5457 (map.bucket_table_reordered
5458 + (full_hash % map.bucket_count)), 4,
5459 map.dwarf5_byte_order);
5460 if (namei == 0)
5461 return NULL;
5462 --namei;
5463 if (namei >= map.name_count)
5464 {
b98664d3 5465 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5466 "[in module %s]"),
5467 namei, map.name_count,
fcf23d5b 5468 objfile_name (per_objfile->objfile));
927aa2e7
JK
5469 return NULL;
5470 }
5471
5472 for (;;)
5473 {
5474 const uint32_t namei_full_hash
5475 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5476 (map.hash_table_reordered + namei), 4,
5477 map.dwarf5_byte_order);
5478 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5479 return NULL;
5480
5481 if (full_hash == namei_full_hash)
5482 {
fcf23d5b 5483 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5484
5485#if 0 /* An expensive sanity check. */
5486 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5487 {
b98664d3 5488 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5489 "[in module %s]"),
5490 namei, objfile_name (dwarf2_per_objfile->objfile));
5491 return NULL;
5492 }
5493#endif
5494
5495 if (cmp (namei_string, name) == 0)
5496 {
5497 const ULONGEST namei_entry_offs
5498 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5499 + namei * map.offset_size),
5500 map.offset_size, map.dwarf5_byte_order);
5501 return map.entry_pool + namei_entry_offs;
5502 }
5503 }
5504
5505 ++namei;
5506 if (namei >= map.name_count)
5507 return NULL;
5508 }
5509}
5510
5511const gdb_byte *
5512dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5513 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5514{
5515 if (namei >= map.name_count)
5516 {
b98664d3 5517 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5518 "[in module %s]"),
5519 namei, map.name_count,
fcf23d5b 5520 objfile_name (per_objfile->objfile));
927aa2e7
JK
5521 return NULL;
5522 }
5523
5524 const ULONGEST namei_entry_offs
5525 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5526 + namei * map.offset_size),
5527 map.offset_size, map.dwarf5_byte_order);
5528 return map.entry_pool + namei_entry_offs;
5529}
5530
5531/* See dw2_debug_names_iterator. */
5532
5533dwarf2_per_cu_data *
5534dw2_debug_names_iterator::next ()
5535{
5536 if (m_addr == NULL)
5537 return NULL;
5538
fcf23d5b
SM
5539 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5540 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5541 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5542
5543 again:
5544
5545 unsigned int bytes_read;
5546 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5547 m_addr += bytes_read;
5548 if (abbrev == 0)
5549 return NULL;
5550
5551 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5552 if (indexval_it == m_map.abbrev_map.cend ())
5553 {
b98664d3 5554 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5555 "[in module %s]"),
ed2dc618 5556 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5557 return NULL;
5558 }
5559 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5560 enum class symbol_linkage {
5561 unknown,
5562 static_,
5563 extern_,
23c13d42 5564 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5565 dwarf2_per_cu_data *per_cu = NULL;
5566 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5567 {
5568 ULONGEST ull;
5569 switch (attr.form)
5570 {
5571 case DW_FORM_implicit_const:
5572 ull = attr.implicit_const;
5573 break;
5574 case DW_FORM_flag_present:
5575 ull = 1;
5576 break;
5577 case DW_FORM_udata:
5578 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5579 m_addr += bytes_read;
5580 break;
6dc55ce9 5581 case DW_FORM_ref4:
5582 ull = read_4_bytes (abfd, m_addr);
5583 m_addr += 4;
5584 break;
5585 case DW_FORM_ref8:
5586 ull = read_8_bytes (abfd, m_addr);
5587 m_addr += 8;
5588 break;
5589 case DW_FORM_ref_sig8:
5590 ull = read_8_bytes (abfd, m_addr);
5591 m_addr += 8;
5592 break;
927aa2e7 5593 default:
b98664d3 5594 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5595 dwarf_form_name (attr.form),
ed2dc618 5596 objfile_name (objfile));
927aa2e7
JK
5597 return NULL;
5598 }
5599 switch (attr.dw_idx)
5600 {
5601 case DW_IDX_compile_unit:
5602 /* Don't crash on bad data. */
fcf23d5b 5603 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5604 {
b98664d3 5605 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5606 " [in module %s]"),
5607 pulongest (ull),
fcf23d5b 5608 objfile_name (objfile));
927aa2e7
JK
5609 continue;
5610 }
fcf23d5b 5611 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5612 break;
8af5c486
JK
5613 case DW_IDX_type_unit:
5614 /* Don't crash on bad data. */
fcf23d5b 5615 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5616 {
b98664d3 5617 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5618 " [in module %s]"),
5619 pulongest (ull),
fcf23d5b 5620 objfile_name (objfile));
8af5c486
JK
5621 continue;
5622 }
fcf23d5b 5623 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5624 break;
6dc55ce9 5625 case DW_IDX_die_offset:
5626 /* In a per-CU index (as opposed to a per-module index), index
5627 entries without CU attribute implicitly refer to the single CU. */
5628 if (per_cu == NULL)
fcf23d5b 5629 per_cu = per_bfd->get_cu (0);
6dc55ce9 5630 break;
927aa2e7
JK
5631 case DW_IDX_GNU_internal:
5632 if (!m_map.augmentation_is_gdb)
5633 break;
23c13d42 5634 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5635 break;
5636 case DW_IDX_GNU_external:
5637 if (!m_map.augmentation_is_gdb)
5638 break;
23c13d42 5639 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5640 break;
5641 }
5642 }
5643
5644 /* Skip if already read in. */
fcf23d5b 5645 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5646 goto again;
5647
5648 /* Check static vs global. */
23c13d42 5649 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5650 {
2b79f376 5651 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5652 const bool symbol_is_static =
5653 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5654 if (want_static != symbol_is_static)
2b79f376 5655 goto again;
927aa2e7
JK
5656 }
5657
5658 /* Match dw2_symtab_iter_next, symbol_kind
5659 and debug_names::psymbol_tag. */
5660 switch (m_domain)
5661 {
5662 case VAR_DOMAIN:
5663 switch (indexval.dwarf_tag)
5664 {
5665 case DW_TAG_variable:
5666 case DW_TAG_subprogram:
5667 /* Some types are also in VAR_DOMAIN. */
5668 case DW_TAG_typedef:
5669 case DW_TAG_structure_type:
5670 break;
5671 default:
5672 goto again;
5673 }
5674 break;
5675 case STRUCT_DOMAIN:
5676 switch (indexval.dwarf_tag)
5677 {
5678 case DW_TAG_typedef:
5679 case DW_TAG_structure_type:
5680 break;
5681 default:
5682 goto again;
5683 }
5684 break;
5685 case LABEL_DOMAIN:
5686 switch (indexval.dwarf_tag)
5687 {
5688 case 0:
5689 case DW_TAG_variable:
5690 break;
5691 default:
5692 goto again;
5693 }
5694 break;
59c35742
AB
5695 case MODULE_DOMAIN:
5696 switch (indexval.dwarf_tag)
5697 {
5698 case DW_TAG_module:
5699 break;
5700 default:
5701 goto again;
5702 }
5703 break;
927aa2e7
JK
5704 default:
5705 break;
5706 }
5707
5708 /* Match dw2_expand_symtabs_matching, symbol_kind and
5709 debug_names::psymbol_tag. */
5710 switch (m_search)
4b514bc8 5711 {
927aa2e7
JK
5712 case VARIABLES_DOMAIN:
5713 switch (indexval.dwarf_tag)
4b514bc8 5714 {
927aa2e7
JK
5715 case DW_TAG_variable:
5716 break;
5717 default:
5718 goto again;
4b514bc8 5719 }
927aa2e7
JK
5720 break;
5721 case FUNCTIONS_DOMAIN:
5722 switch (indexval.dwarf_tag)
4b514bc8 5723 {
927aa2e7
JK
5724 case DW_TAG_subprogram:
5725 break;
5726 default:
5727 goto again;
4b514bc8 5728 }
927aa2e7
JK
5729 break;
5730 case TYPES_DOMAIN:
5731 switch (indexval.dwarf_tag)
5732 {
5733 case DW_TAG_typedef:
5734 case DW_TAG_structure_type:
5735 break;
5736 default:
5737 goto again;
5738 }
5739 break;
59c35742
AB
5740 case MODULES_DOMAIN:
5741 switch (indexval.dwarf_tag)
5742 {
5743 case DW_TAG_module:
5744 break;
5745 default:
5746 goto again;
5747 }
927aa2e7
JK
5748 default:
5749 break;
4b514bc8 5750 }
927aa2e7
JK
5751
5752 return per_cu;
4b514bc8 5753}
61920122 5754
39298a5d
TT
5755struct compunit_symtab *
5756dwarf2_debug_names_index::lookup_symbol
5757 (struct objfile *objfile, block_enum block_index,
5758 const char *name, domain_enum domain)
4b514bc8 5759{
976ca316 5760 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5761
976ca316 5762 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5763 if (!mapp)
61920122 5764 {
927aa2e7
JK
5765 /* index is NULL if OBJF_READNOW. */
5766 return NULL;
5767 }
5768 const auto &map = *mapp;
9291a0cd 5769
976ca316 5770 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5771
927aa2e7
JK
5772 struct compunit_symtab *stab_best = NULL;
5773 struct dwarf2_per_cu_data *per_cu;
5774 while ((per_cu = iter.next ()) != NULL)
5775 {
5776 struct symbol *sym, *with_opaque = NULL;
97a1449a 5777 compunit_symtab *stab
976ca316 5778 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5779 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5780 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5781
927aa2e7
JK
5782 sym = block_find_symbol (block, name, domain,
5783 block_find_non_opaque_type_preferred,
5784 &with_opaque);
9703b513 5785
927aa2e7
JK
5786 /* Some caution must be observed with overloaded functions and
5787 methods, since the index will not contain any overload
5788 information (but NAME might contain it). */
a3ec0bb1 5789
927aa2e7 5790 if (sym != NULL
987012b8 5791 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5792 return stab;
5793 if (with_opaque != NULL
987012b8 5794 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5795 stab_best = stab;
9703b513 5796
927aa2e7 5797 /* Keep looking through other CUs. */
9703b513
TT
5798 }
5799
927aa2e7 5800 return stab_best;
9703b513
TT
5801}
5802
927aa2e7
JK
5803/* This dumps minimal information about .debug_names. It is called
5804 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5805 uses this to verify that .debug_names has been loaded. */
9291a0cd 5806
39298a5d
TT
5807void
5808dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5809{
976ca316 5810 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5811
976ca316 5812 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5813 printf_filtered (".debug_names:");
976ca316 5814 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5815 printf_filtered (" exists\n");
5816 else
5817 printf_filtered (" faked for \"readnow\"\n");
5818 printf_filtered ("\n");
9291a0cd
TT
5819}
5820
39298a5d
TT
5821void
5822dwarf2_debug_names_index::expand_symtabs_for_function
5823 (struct objfile *objfile, const char *func_name)
9291a0cd 5824{
976ca316 5825 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5826
976ca316
SM
5827 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5828 if (per_objfile->per_bfd->debug_names_table)
24c79950 5829 {
976ca316 5830 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5831
fcf23d5b 5832 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5833 per_objfile);
24c79950 5834
927aa2e7
JK
5835 struct dwarf2_per_cu_data *per_cu;
5836 while ((per_cu = iter.next ()) != NULL)
976ca316 5837 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5838 }
5839}
24c79950 5840
39298a5d
TT
5841void
5842dwarf2_debug_names_index::map_matching_symbols
3b00ef10
TT
5843 (struct objfile *objfile,
5844 const lookup_name_info &name, domain_enum domain,
5845 int global,
5846 gdb::function_view<symbol_found_callback_ftype> callback,
5847 symbol_compare_ftype *ordered_compare)
5848{
976ca316 5849 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5850
5851 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5852 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5853 return;
5854
976ca316 5855 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5856 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5857
5858 const char *match_name = name.ada ().lookup_name ().c_str ();
5859 auto matcher = [&] (const char *symname)
5860 {
5861 if (ordered_compare == nullptr)
5862 return true;
5863 return ordered_compare (symname, match_name) == 0;
5864 };
5865
5866 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5867 [&] (offset_type namei)
5868 {
5869 /* The name was matched, now expand corresponding CUs that were
5870 marked. */
fcf23d5b 5871 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5872 per_objfile);
3b00ef10
TT
5873
5874 struct dwarf2_per_cu_data *per_cu;
5875 while ((per_cu = iter.next ()) != NULL)
976ca316 5876 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5877 nullptr);
3b00ef10 5878 return true;
976ca316 5879 }, per_objfile);
3b00ef10
TT
5880
5881 /* It's a shame we couldn't do this inside the
5882 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5883 that have already been expanded. Instead, this loop matches what
5884 the psymtab code does. */
976ca316 5885 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5886 {
976ca316 5887 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5888 if (symtab != nullptr)
3b00ef10
TT
5889 {
5890 const struct block *block
af758d11 5891 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5892 if (!iterate_over_symbols_terminated (block, name,
5893 domain, callback))
5894 break;
5895 }
5896 }
5897}
5898
39298a5d
TT
5899void
5900dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5901 (struct objfile *objfile,
5902 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5903 const lookup_name_info *lookup_name,
927aa2e7
JK
5904 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5905 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5906 enum search_domain kind)
5907{
976ca316 5908 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5909
927aa2e7 5910 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5911 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5912 return;
9291a0cd 5913
976ca316 5914 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5915
c1a66c06
TV
5916 if (symbol_matcher == NULL && lookup_name == NULL)
5917 {
976ca316 5918 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5919 {
5920 QUIT;
5921
976ca316
SM
5922 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5923 expansion_notify);
c1a66c06
TV
5924 }
5925 return;
5926 }
5927
976ca316 5928 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5929
c1a66c06 5930 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5931 symbol_matcher,
5932 kind, [&] (offset_type namei)
927aa2e7 5933 {
927aa2e7
JK
5934 /* The name was matched, now expand corresponding CUs that were
5935 marked. */
976ca316 5936 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5937
927aa2e7
JK
5938 struct dwarf2_per_cu_data *per_cu;
5939 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5940 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5941 expansion_notify);
3b00ef10 5942 return true;
976ca316 5943 }, per_objfile);
9291a0cd
TT
5944}
5945
4485a1c1 5946/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5947 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5948
5949template <typename T>
5950static gdb::array_view<const gdb_byte>
5951get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5952{
5953 dwarf2_section_info *section = &section_owner->gdb_index;
5954
96b79293 5955 if (section->empty ())
4485a1c1
SM
5956 return {};
5957
5958 /* Older elfutils strip versions could keep the section in the main
5959 executable while splitting it for the separate debug info file. */
96b79293 5960 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5961 return {};
5962
96b79293 5963 section->read (obj);
4485a1c1 5964
8bebfcda
PA
5965 /* dwarf2_section_info::size is a bfd_size_type, while
5966 gdb::array_view works with size_t. On 32-bit hosts, with
5967 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5968 is 32-bit. So we need an explicit narrowing conversion here.
5969 This is fine, because it's impossible to allocate or mmap an
5970 array/buffer larger than what size_t can represent. */
5971 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5972}
5973
87d6a7aa
SM
5974/* Lookup the index cache for the contents of the index associated to
5975 DWARF2_OBJ. */
5976
5977static gdb::array_view<const gdb_byte>
5989a64e 5978get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5979{
5980 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5981 if (build_id == nullptr)
5982 return {};
5983
5984 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5985 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5986}
5987
5988/* Same as the above, but for DWZ. */
5989
5990static gdb::array_view<const gdb_byte>
5991get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5992{
5993 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5994 if (build_id == nullptr)
5995 return {};
5996
5997 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5998}
5999
3c0aa29a 6000/* See symfile.h. */
9291a0cd 6001
3c0aa29a
PA
6002bool
6003dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6004{
976ca316
SM
6005 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6006 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 6007
850ed749
SM
6008 dwarf_read_debug_printf ("called");
6009
9291a0cd
TT
6010 /* If we're about to read full symbols, don't bother with the
6011 indices. In this case we also don't care if some other debug
6012 format is making psymtabs, because they are all about to be
6013 expanded anyway. */
6014 if ((objfile->flags & OBJF_READNOW))
6015 {
850ed749
SM
6016 dwarf_read_debug_printf ("readnow requested");
6017
17ee85fc
TT
6018 /* When using READNOW, the using_index flag (set below) indicates that
6019 PER_BFD was already initialized, when we loaded some other objfile. */
6020 if (per_bfd->using_index)
6021 {
850ed749 6022 dwarf_read_debug_printf ("using_index already set");
17ee85fc 6023 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6024 per_objfile->resize_symtabs ();
17ee85fc
TT
6025 return true;
6026 }
6027
6028 per_bfd->using_index = 1;
976ca316
SM
6029 create_all_comp_units (per_objfile);
6030 create_all_type_units (per_objfile);
17ee85fc
TT
6031 per_bfd->quick_file_names_table
6032 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 6033 per_objfile->resize_symtabs ();
9291a0cd 6034
17ee85fc
TT
6035 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6036 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 6037 {
17ee85fc 6038 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 6039
17ee85fc 6040 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6041 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6042 }
6043
6044 /* Return 1 so that gdb sees the "quick" functions. However,
6045 these functions will be no-ops because we will have expanded
6046 all symtabs. */
3c0aa29a
PA
6047 *index_kind = dw_index_kind::GDB_INDEX;
6048 return true;
9291a0cd
TT
6049 }
6050
17ee85fc
TT
6051 /* Was a debug names index already read when we processed an objfile sharing
6052 PER_BFD? */
6053 if (per_bfd->debug_names_table != nullptr)
6054 {
850ed749 6055 dwarf_read_debug_printf ("re-using shared debug names table");
17ee85fc 6056 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6057 per_objfile->resize_symtabs ();
17ee85fc
TT
6058 return true;
6059 }
6060
6061 /* Was a GDB index already read when we processed an objfile sharing
6062 PER_BFD? */
6063 if (per_bfd->index_table != nullptr)
6064 {
850ed749 6065 dwarf_read_debug_printf ("re-using shared index table");
17ee85fc 6066 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6067 per_objfile->resize_symtabs ();
17ee85fc
TT
6068 return true;
6069 }
6070
efb763a5
SM
6071 /* There might already be partial symtabs built for this BFD. This happens
6072 when loading the same binary twice with the index-cache enabled. If so,
6073 don't try to read an index. The objfile / per_objfile initialization will
6074 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6075 code path. */
6076 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
6077 {
6078 dwarf_read_debug_printf ("re-using shared partial symtabs");
6079 return false;
6080 }
efb763a5 6081
976ca316 6082 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 6083 {
850ed749 6084 dwarf_read_debug_printf ("found debug names");
3c0aa29a 6085 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6086 per_objfile->resize_symtabs ();
3c0aa29a
PA
6087 return true;
6088 }
927aa2e7 6089
976ca316 6090 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6091 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6092 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 6093 {
850ed749 6094 dwarf_read_debug_printf ("found gdb index from file");
3c0aa29a 6095 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6096 per_objfile->resize_symtabs ();
3c0aa29a
PA
6097 return true;
6098 }
9291a0cd 6099
87d6a7aa 6100 /* ... otherwise, try to find the index in the index cache. */
976ca316 6101 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6102 get_gdb_index_contents_from_cache,
6103 get_gdb_index_contents_from_cache_dwz))
6104 {
850ed749 6105 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa
SM
6106 global_index_cache.hit ();
6107 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6108 per_objfile->resize_symtabs ();
87d6a7aa
SM
6109 return true;
6110 }
6111
6112 global_index_cache.miss ();
3c0aa29a 6113 return false;
9291a0cd
TT
6114}
6115
6116\f
6117
dce234bc
PP
6118/* Build a partial symbol table. */
6119
6120void
f29dff0a 6121dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6122{
976ca316
SM
6123 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6124 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6125
6126 if (per_bfd->partial_symtabs != nullptr)
6127 {
6128 /* Partial symbols were already read, so now we can simply
6129 attach them. */
6130 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6131 per_objfile->resize_symtabs ();
17ee85fc
TT
6132 return;
6133 }
c9bf0622 6134
51962708
TT
6135 /* Set the local reference to partial symtabs, so that we don't try
6136 to read them again if reading another objfile with the same BFD.
6137 If we can't in fact share, this won't make a difference anyway as
6138 the dwarf2_per_bfd object won't be shared. */
6139 per_bfd->partial_symtabs = objfile->partial_symtabs;
6140
a70b8144 6141 try
c9bf0622
TT
6142 {
6143 /* This isn't really ideal: all the data we allocate on the
6144 objfile's obstack is still uselessly kept around. However,
6145 freeing it seems unsafe. */
484b1090 6146 psymtab_discarder psymtabs (objfile->partial_symtabs.get ());
976ca316 6147 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6148 psymtabs.keep ();
87d6a7aa 6149
976ca316 6150 per_objfile->resize_symtabs ();
af758d11 6151
87d6a7aa 6152 /* (maybe) store an index in the cache. */
976ca316 6153 global_index_cache.store (per_objfile);
c9bf0622 6154 }
230d2906 6155 catch (const gdb_exception_error &except)
492d29ea
PA
6156 {
6157 exception_print (gdb_stderr, except);
6158 }
c906108c 6159}
c906108c 6160
3b80fe9b
DE
6161/* Find the base address of the compilation unit for range lists and
6162 location lists. It will normally be specified by DW_AT_low_pc.
6163 In DWARF-3 draft 4, the base address could be overridden by
6164 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6165 compilation units with discontinuous ranges. */
6166
6167static void
6168dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6169{
6170 struct attribute *attr;
6171
2b24b6e4 6172 cu->base_address.reset ();
3b80fe9b
DE
6173
6174 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6175 if (attr != nullptr)
95f982e5 6176 cu->base_address = attr->as_address ();
3b80fe9b
DE
6177 else
6178 {
6179 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6180 if (attr != nullptr)
95f982e5 6181 cu->base_address = attr->as_address ();
3b80fe9b
DE
6182 }
6183}
6184
36586728
TT
6185/* Helper function that returns the proper abbrev section for
6186 THIS_CU. */
6187
6188static struct dwarf2_section_info *
6189get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6190{
6191 struct dwarf2_section_info *abbrev;
c3699833 6192 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6193
6194 if (this_cu->is_dwz)
a7308ce0 6195 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 6196 else
c3699833 6197 abbrev = &per_bfd->abbrev;
36586728
TT
6198
6199 return abbrev;
6200}
6201
f4dc4d17
DE
6202/* Fetch the abbreviation table offset from a comp or type unit header. */
6203
6204static sect_offset
976ca316 6205read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6206 struct dwarf2_section_info *section,
9c541725 6207 sect_offset sect_off)
f4dc4d17 6208{
96b79293 6209 bfd *abfd = section->get_bfd_owner ();
d521ce57 6210 const gdb_byte *info_ptr;
ac298888 6211 unsigned int initial_length_size, offset_size;
43988095 6212 uint16_t version;
f4dc4d17 6213
976ca316 6214 section->read (per_objfile->objfile);
9c541725 6215 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6216 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6217 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6218 info_ptr += initial_length_size;
6219
6220 version = read_2_bytes (abfd, info_ptr);
6221 info_ptr += 2;
6222 if (version >= 5)
6223 {
6224 /* Skip unit type and address size. */
6225 info_ptr += 2;
6226 }
6227
24aa364d 6228 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6229}
6230
b83470bf
TT
6231/* A partial symtab that is used only for include files. */
6232struct dwarf2_include_psymtab : public partial_symtab
6233{
6234 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6235 : partial_symtab (filename, objfile)
6236 {
6237 }
6238
6239 void read_symtab (struct objfile *objfile) override
6240 {
194d088f
TV
6241 /* It's an include file, no symbols to read for it.
6242 Everything is in the includer symtab. */
6243
6244 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6245 expansion of the includer psymtab. We use the dependencies[0] field to
6246 model the includer. But if we go the regular route of calling
6247 expand_psymtab here, and having expand_psymtab call expand_dependencies
6248 to expand the includer, we'll only use expand_psymtab on the includer
6249 (making it a non-toplevel psymtab), while if we expand the includer via
6250 another path, we'll use read_symtab (making it a toplevel psymtab).
6251 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6252 psymtab, and trigger read_symtab on the includer here directly. */
6253 includer ()->read_symtab (objfile);
b83470bf
TT
6254 }
6255
6256 void expand_psymtab (struct objfile *objfile) override
6257 {
194d088f
TV
6258 /* This is not called by read_symtab, and should not be called by any
6259 expand_dependencies. */
6260 gdb_assert (false);
b83470bf
TT
6261 }
6262
5717c425 6263 bool readin_p (struct objfile *objfile) const override
b83470bf 6264 {
5717c425 6265 return includer ()->readin_p (objfile);
b83470bf
TT
6266 }
6267
5717c425 6268 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6269 {
6270 return nullptr;
6271 }
6272
6273private:
194d088f
TV
6274 partial_symtab *includer () const
6275 {
6276 /* An include psymtab has exactly one dependency: the psymtab that
6277 includes it. */
6278 gdb_assert (this->number_of_dependencies == 1);
6279 return this->dependencies[0];
6280 }
b83470bf
TT
6281};
6282
aaa75496
JB
6283/* Allocate a new partial symtab for file named NAME and mark this new
6284 partial symtab as being an include of PST. */
6285
6286static void
891813be 6287dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
dda83cd7 6288 struct objfile *objfile)
aaa75496 6289{
b83470bf 6290 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6291
fbd9ab74 6292 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6293 subpst->dirname = pst->dirname;
fbd9ab74 6294
a9342b62 6295 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6296 subpst->dependencies[0] = pst;
6297 subpst->number_of_dependencies = 1;
aaa75496
JB
6298}
6299
6300/* Read the Line Number Program data and extract the list of files
6301 included by the source file represented by PST. Build an include
d85a05f0 6302 partial symtab for each of these included files. */
aaa75496
JB
6303
6304static void
6305dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6306 struct die_info *die,
891813be 6307 dwarf2_psymtab *pst)
aaa75496 6308{
fff8551c 6309 line_header_up lh;
d85a05f0 6310 struct attribute *attr;
aaa75496 6311
d85a05f0 6312 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6313 if (attr != nullptr && attr->form_is_unsigned ())
6314 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6315 if (lh == NULL)
6316 return; /* No linetable, so no includes. */
6317
79748972
TT
6318 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6319 that we pass in the raw text_low here; that is ok because we're
6320 only decoding the line table to make include partial symtabs, and
6321 so the addresses aren't really used. */
4ae976d1 6322 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6323 pst->raw_text_low (), 1);
aaa75496
JB
6324}
6325
348e048f 6326static hashval_t
52dc124a 6327hash_signatured_type (const void *item)
348e048f 6328{
9a3c8263
SM
6329 const struct signatured_type *sig_type
6330 = (const struct signatured_type *) item;
9a619af0 6331
348e048f 6332 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6333 return sig_type->signature;
348e048f
DE
6334}
6335
6336static int
52dc124a 6337eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6338{
9a3c8263
SM
6339 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6340 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6341
348e048f
DE
6342 return lhs->signature == rhs->signature;
6343}
6344
1fd400ff
TT
6345/* Allocate a hash table for signatured types. */
6346
b0b6a987 6347static htab_up
298e9637 6348allocate_signatured_type_table ()
1fd400ff 6349{
b0b6a987
TT
6350 return htab_up (htab_create_alloc (41,
6351 hash_signatured_type,
6352 eq_signatured_type,
6353 NULL, xcalloc, xfree));
1fd400ff
TT
6354}
6355
d467dd73 6356/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6357
6358static int
d467dd73 6359add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6360{
9a3c8263 6361 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6362 std::vector<signatured_type *> *all_type_units
6363 = (std::vector<signatured_type *> *) datum;
1fd400ff 6364
b2bdb8cf 6365 all_type_units->push_back (sigt);
1fd400ff
TT
6366
6367 return 1;
6368}
6369
78d4d2c5 6370/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6371 and fill them into TYPES_HTAB. It will process only type units,
6372 therefore DW_UT_type. */
c88ee1f0 6373
78d4d2c5 6374static void
976ca316 6375create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6376 struct dwo_file *dwo_file,
b0b6a987 6377 dwarf2_section_info *section, htab_up &types_htab,
43988095 6378 rcuh_kind section_kind)
348e048f 6379{
976ca316 6380 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6381 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6382 bfd *abfd;
6383 const gdb_byte *info_ptr, *end_ptr;
348e048f 6384
4bdcc0c1
DE
6385 abbrev_section = (dwo_file != NULL
6386 ? &dwo_file->sections.abbrev
976ca316 6387 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6388
4800761a 6389 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
6390 section->get_name (),
6391 abbrev_section->get_file_name ());
09406207 6392
96b79293 6393 section->read (objfile);
78d4d2c5 6394 info_ptr = section->buffer;
348e048f 6395
78d4d2c5
JK
6396 if (info_ptr == NULL)
6397 return;
348e048f 6398
78d4d2c5
JK
6399 /* We can't set abfd until now because the section may be empty or
6400 not present, in which case the bfd is unknown. */
96b79293 6401 abfd = section->get_bfd_owner ();
348e048f 6402
c0ab21c2
TT
6403 /* We don't use cutu_reader here because we don't need to read
6404 any dies: the signature is in the header. */
3019eac3 6405
78d4d2c5
JK
6406 end_ptr = info_ptr + section->size;
6407 while (info_ptr < end_ptr)
6408 {
78d4d2c5
JK
6409 struct signatured_type *sig_type;
6410 struct dwo_unit *dwo_tu;
6411 void **slot;
6412 const gdb_byte *ptr = info_ptr;
6413 struct comp_unit_head header;
6414 unsigned int length;
8b70b953 6415
9c541725 6416 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6417
a49dd8dd
JK
6418 /* Initialize it due to a false compiler warning. */
6419 header.signature = -1;
9c541725 6420 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6421
78d4d2c5
JK
6422 /* We need to read the type's signature in order to build the hash
6423 table, but we don't need anything else just yet. */
348e048f 6424
976ca316 6425 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6426 abbrev_section, ptr, section_kind);
348e048f 6427
4057dfde 6428 length = header.get_length ();
6caca83c 6429
78d4d2c5
JK
6430 /* Skip dummy type units. */
6431 if (ptr >= info_ptr + length
43988095 6432 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6433 || (header.unit_type != DW_UT_type
6434 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6435 {
6436 info_ptr += length;
6437 continue;
6438 }
dee91e82 6439
78d4d2c5
JK
6440 if (types_htab == NULL)
6441 {
6442 if (dwo_file)
298e9637 6443 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6444 else
298e9637 6445 types_htab = allocate_signatured_type_table ();
78d4d2c5 6446 }
8b70b953 6447
78d4d2c5
JK
6448 if (dwo_file)
6449 {
6450 sig_type = NULL;
976ca316 6451 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6452 dwo_tu->dwo_file = dwo_file;
43988095 6453 dwo_tu->signature = header.signature;
9c541725 6454 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6455 dwo_tu->section = section;
9c541725 6456 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6457 dwo_tu->length = length;
6458 }
6459 else
6460 {
6461 /* N.B.: type_offset is not usable if this type uses a DWO file.
6462 The real type_offset is in the DWO file. */
6463 dwo_tu = NULL;
976ca316 6464 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6465 sig_type->signature = header.signature;
9c541725 6466 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6467 sig_type->per_cu.is_debug_types = 1;
6468 sig_type->per_cu.section = section;
9c541725 6469 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6470 sig_type->per_cu.length = length;
6471 }
6472
b0b6a987 6473 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6474 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6475 INSERT);
6476 gdb_assert (slot != NULL);
6477 if (*slot != NULL)
6478 {
9c541725 6479 sect_offset dup_sect_off;
0349ea22 6480
3019eac3
DE
6481 if (dwo_file)
6482 {
78d4d2c5
JK
6483 const struct dwo_unit *dup_tu
6484 = (const struct dwo_unit *) *slot;
6485
9c541725 6486 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6487 }
6488 else
6489 {
78d4d2c5
JK
6490 const struct signatured_type *dup_tu
6491 = (const struct signatured_type *) *slot;
6492
9c541725 6493 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6494 }
8b70b953 6495
b98664d3 6496 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6497 " the entry at offset %s, signature %s"),
6498 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6499 hex_string (header.signature));
78d4d2c5
JK
6500 }
6501 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6502
6f738b01
SM
6503 dwarf_read_debug_printf_v (" offset %s, signature %s",
6504 sect_offset_str (sect_off),
6505 hex_string (header.signature));
3019eac3 6506
78d4d2c5
JK
6507 info_ptr += length;
6508 }
6509}
3019eac3 6510
78d4d2c5
JK
6511/* Create the hash table of all entries in the .debug_types
6512 (or .debug_types.dwo) section(s).
6513 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6514 otherwise it is NULL.
b3c8eb43 6515
78d4d2c5 6516 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6517
78d4d2c5 6518 Note: This function processes DWO files only, not DWP files. */
348e048f 6519
78d4d2c5 6520static void
976ca316 6521create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6522 struct dwo_file *dwo_file,
fd5866f6 6523 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6524 htab_up &types_htab)
78d4d2c5 6525{
fd5866f6 6526 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6527 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6528 rcuh_kind::TYPE);
3019eac3
DE
6529}
6530
6531/* Create the hash table of all entries in the .debug_types section,
6532 and initialize all_type_units.
6533 The result is zero if there is an error (e.g. missing .debug_types section),
6534 otherwise non-zero. */
6535
6536static int
976ca316 6537create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6538{
b0b6a987 6539 htab_up types_htab;
3019eac3 6540
976ca316
SM
6541 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6542 types_htab, rcuh_kind::COMPILE);
6543 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6544 types_htab);
3019eac3
DE
6545 if (types_htab == NULL)
6546 {
976ca316 6547 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6548 return 0;
6549 }
6550
976ca316 6551 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6552
976ca316
SM
6553 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6554 per_objfile->per_bfd->all_type_units.reserve
6555 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6556
976ca316 6557 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6558 add_signatured_type_cu_to_table,
976ca316 6559 &per_objfile->per_bfd->all_type_units);
1fd400ff 6560
348e048f
DE
6561 return 1;
6562}
6563
5989a64e 6564/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6565 If SLOT is non-NULL, it is the entry to use in the hash table.
6566 Otherwise we find one. */
6567
6568static struct signatured_type *
976ca316 6569add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6570{
976ca316
SM
6571 if (per_objfile->per_bfd->all_type_units.size ()
6572 == per_objfile->per_bfd->all_type_units.capacity ())
6573 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6574
976ca316 6575 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6576
976ca316 6577 per_objfile->resize_symtabs ();
af758d11 6578
976ca316 6579 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6580 sig_type->signature = sig;
6581 sig_type->per_cu.is_debug_types = 1;
976ca316 6582 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6583 {
6584 sig_type->per_cu.v.quick =
976ca316 6585 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6586 struct dwarf2_per_cu_quick_data);
6587 }
6588
6589 if (slot == NULL)
6590 {
976ca316 6591 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6592 sig_type, INSERT);
6593 }
6594 gdb_assert (*slot == NULL);
6595 *slot = sig_type;
6596 /* The rest of sig_type must be filled in by the caller. */
6597 return sig_type;
6598}
6599
a2ce51a0
DE
6600/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6601 Fill in SIG_ENTRY with DWO_ENTRY. */
6602
6603static void
976ca316 6604fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6605 struct signatured_type *sig_entry,
6606 struct dwo_unit *dwo_entry)
6607{
976ca316 6608 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6609
7ee85ab1 6610 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6611 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6612 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6613 if (per_bfd->using_index)
6aa5f3a6
DE
6614 {
6615 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6616 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6617 }
6618 else
6619 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6620 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6621 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6622 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6623 gdb_assert (sig_entry->dwo_unit == NULL);
6624
6625 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6626 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6627 sig_entry->per_cu.length = dwo_entry->length;
6628 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6629 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6630 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6631 sig_entry->dwo_unit = dwo_entry;
6632}
6633
6634/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6635 If we haven't read the TU yet, create the signatured_type data structure
6636 for a TU to be read in directly from a DWO file, bypassing the stub.
6637 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6638 using .gdb_index, then when reading a CU we want to stay in the DWO file
6639 containing that CU. Otherwise we could end up reading several other DWO
6640 files (due to comdat folding) to process the transitive closure of all the
6641 mentioned TUs, and that can be slow. The current DWO file will have every
6642 type signature that it needs.
a2ce51a0
DE
6643 We only do this for .gdb_index because in the psymtab case we already have
6644 to read all the DWOs to build the type unit groups. */
6645
6646static struct signatured_type *
6647lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6648{
976ca316 6649 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6650 struct dwo_file *dwo_file;
6651 struct dwo_unit find_dwo_entry, *dwo_entry;
6652 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6653 void **slot;
a2ce51a0 6654
976ca316 6655 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6656
6aa5f3a6
DE
6657 /* If TU skeletons have been removed then we may not have read in any
6658 TUs yet. */
976ca316
SM
6659 if (per_objfile->per_bfd->signatured_types == NULL)
6660 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6661
6662 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6663 Use the global signatured_types array to do our own comdat-folding
6664 of types. If this is the first time we're reading this TU, and
6665 the TU has an entry in .gdb_index, replace the recorded data from
6666 .gdb_index with this TU. */
a2ce51a0 6667
a2ce51a0 6668 find_sig_entry.signature = sig;
976ca316 6669 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6670 &find_sig_entry, INSERT);
9a3c8263 6671 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6672
6673 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6674 read. Don't reassign the global entry to point to this DWO if that's
6675 the case. Also note that if the TU is already being read, it may not
6676 have come from a DWO, the program may be a mix of Fission-compiled
6677 code and non-Fission-compiled code. */
6678
6679 /* Have we already tried to read this TU?
6680 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6681 needn't exist in the global table yet). */
6682 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6683 return sig_entry;
6684
6aa5f3a6
DE
6685 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6686 dwo_unit of the TU itself. */
6687 dwo_file = cu->dwo_unit->dwo_file;
6688
a2ce51a0
DE
6689 /* Ok, this is the first time we're reading this TU. */
6690 if (dwo_file->tus == NULL)
6691 return NULL;
6692 find_dwo_entry.signature = sig;
b0b6a987
TT
6693 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6694 &find_dwo_entry);
a2ce51a0
DE
6695 if (dwo_entry == NULL)
6696 return NULL;
6697
6aa5f3a6
DE
6698 /* If the global table doesn't have an entry for this TU, add one. */
6699 if (sig_entry == NULL)
976ca316 6700 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6701
976ca316 6702 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6703 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6704 return sig_entry;
6705}
6706
a2ce51a0
DE
6707/* Subroutine of lookup_signatured_type.
6708 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6709 then try the DWP file. If the TU stub (skeleton) has been removed then
6710 it won't be in .gdb_index. */
a2ce51a0
DE
6711
6712static struct signatured_type *
6713lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6714{
976ca316
SM
6715 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6716 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6717 struct dwo_unit *dwo_entry;
6718 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6719 void **slot;
a2ce51a0 6720
976ca316 6721 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6722 gdb_assert (dwp_file != NULL);
6723
6aa5f3a6
DE
6724 /* If TU skeletons have been removed then we may not have read in any
6725 TUs yet. */
976ca316
SM
6726 if (per_objfile->per_bfd->signatured_types == NULL)
6727 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6728
6aa5f3a6 6729 find_sig_entry.signature = sig;
976ca316 6730 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6731 &find_sig_entry, INSERT);
9a3c8263 6732 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6733
6734 /* Have we already tried to read this TU?
6735 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6736 needn't exist in the global table yet). */
6737 if (sig_entry != NULL)
6738 return sig_entry;
6739
a2ce51a0
DE
6740 if (dwp_file->tus == NULL)
6741 return NULL;
976ca316
SM
6742 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6743 1 /* is_debug_types */);
a2ce51a0
DE
6744 if (dwo_entry == NULL)
6745 return NULL;
6746
976ca316
SM
6747 sig_entry = add_type_unit (per_objfile, sig, slot);
6748 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6749
a2ce51a0
DE
6750 return sig_entry;
6751}
6752
380bca97 6753/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6754 Returns NULL if signature SIG is not present in the table.
6755 It is up to the caller to complain about this. */
348e048f
DE
6756
6757static struct signatured_type *
a2ce51a0 6758lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6759{
976ca316 6760 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6761
976ca316 6762 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6763 {
6764 /* We're in a DWO/DWP file, and we're using .gdb_index.
6765 These cases require special processing. */
976ca316 6766 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6767 return lookup_dwo_signatured_type (cu, sig);
6768 else
6769 return lookup_dwp_signatured_type (cu, sig);
6770 }
6771 else
6772 {
6773 struct signatured_type find_entry, *entry;
348e048f 6774
976ca316 6775 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6776 return NULL;
6777 find_entry.signature = sig;
9a3c8263 6778 entry = ((struct signatured_type *)
976ca316 6779 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6780 &find_entry));
a2ce51a0
DE
6781 return entry;
6782 }
348e048f 6783}
18a8505e 6784
42e7ad6c 6785/* Low level DIE reading support. */
348e048f 6786
d85a05f0
DJ
6787/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6788
6789static void
6790init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6791 struct dwarf2_cu *cu,
3019eac3 6792 struct dwarf2_section_info *section,
685af9cd
TT
6793 struct dwo_file *dwo_file,
6794 struct abbrev_table *abbrev_table)
d85a05f0 6795{
fceca515 6796 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6797 reader->abfd = section->get_bfd_owner ();
d85a05f0 6798 reader->cu = cu;
3019eac3 6799 reader->dwo_file = dwo_file;
dee91e82
DE
6800 reader->die_section = section;
6801 reader->buffer = section->buffer;
f664829e 6802 reader->buffer_end = section->buffer + section->size;
685af9cd 6803 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6804}
6805
c0ab21c2 6806/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6807 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6808 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6809 already.
6810
6811 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6812 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6813 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6814 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6815 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6816 STUB_COMP_DIR may be non-NULL.
3e225074 6817 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6818 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6819 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6820 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6821 kept around for at least as long as *RESULT_READER.
6822
b0c7bfa9
DE
6823 The result is non-zero if a valid (non-dummy) DIE was found. */
6824
6825static int
4ab09049 6826read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6827 struct dwo_unit *dwo_unit,
b0c7bfa9 6828 struct die_info *stub_comp_unit_die,
a2ce51a0 6829 const char *stub_comp_dir,
b0c7bfa9 6830 struct die_reader_specs *result_reader,
d521ce57 6831 const gdb_byte **result_info_ptr,
b0c7bfa9 6832 struct die_info **result_comp_unit_die,
685af9cd 6833 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6834{
976ca316 6835 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6836 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6837 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6838 bfd *abfd;
d521ce57 6839 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6840 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6841 int i,num_extra_attrs;
6842 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6843 struct die_info *comp_unit_die;
6844
b0aeadb3
DE
6845 /* At most one of these may be provided. */
6846 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6847
b0c7bfa9
DE
6848 /* These attributes aren't processed until later:
6849 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6850 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6851 referenced later. However, these attributes are found in the stub
6852 which we won't have later. In order to not impose this complication
6853 on the rest of the code, we read them here and copy them to the
6854 DWO CU/TU die. */
b0c7bfa9
DE
6855
6856 stmt_list = NULL;
6857 low_pc = NULL;
6858 high_pc = NULL;
6859 ranges = NULL;
6860 comp_dir = NULL;
6861
6862 if (stub_comp_unit_die != NULL)
6863 {
6864 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6865 DWO file. */
4ab09049 6866 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6867 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6868 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6869 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6870 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6871 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6872
a39fdb41 6873 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6874
2b0c7f41
SM
6875 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6876 We need the value before we can process DW_AT_ranges values from the
6877 DWO. */
6878 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6879
6880 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6881 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6882 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6883 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6884 section. */
6885 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6886 }
a2ce51a0
DE
6887 else if (stub_comp_dir != NULL)
6888 {
6889 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6890 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6891 comp_dir->name = DW_AT_comp_dir;
6892 comp_dir->form = DW_FORM_string;
c6481205 6893 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6894 }
b0c7bfa9
DE
6895
6896 /* Set up for reading the DWO CU/TU. */
6897 cu->dwo_unit = dwo_unit;
685af9cd 6898 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6899 section->read (objfile);
6900 abfd = section->get_bfd_owner ();
9c541725
PA
6901 begin_info_ptr = info_ptr = (section->buffer
6902 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6903 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6904
4ab09049 6905 if (per_cu->is_debug_types)
b0c7bfa9 6906 {
4ab09049 6907 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6908
976ca316
SM
6909 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6910 section, dwo_abbrev_section,
43988095 6911 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6912 /* This is not an assert because it can be caused by bad debug info. */
43988095 6913 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6914 {
6915 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6916 " TU at offset %s [in module %s]"),
a2ce51a0 6917 hex_string (sig_type->signature),
43988095 6918 hex_string (cu->header.signature),
9d8780f0 6919 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6920 bfd_get_filename (abfd));
6921 }
9c541725 6922 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6923 /* For DWOs coming from DWP files, we don't know the CU length
6924 nor the type's offset in the TU until now. */
4057dfde 6925 dwo_unit->length = cu->header.get_length ();
9c541725 6926 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6927
6928 /* Establish the type offset that can be used to lookup the type.
6929 For DWO files, we don't know it until now. */
9c541725
PA
6930 sig_type->type_offset_in_section
6931 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6932 }
6933 else
6934 {
976ca316
SM
6935 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6936 section, dwo_abbrev_section,
43988095 6937 info_ptr, rcuh_kind::COMPILE);
9c541725 6938 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6939 /* For DWOs coming from DWP files, we don't know the CU length
6940 until now. */
4057dfde 6941 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6942 }
6943
606decb2 6944 dwo_abbrev_section->read (objfile);
685af9cd 6945 *result_dwo_abbrev_table
606decb2 6946 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6947 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6948 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6949
6950 /* Read in the die, but leave space to copy over the attributes
6951 from the stub. This has the benefit of simplifying the rest of
6952 the code - all the work to maintain the illusion of a single
6953 DW_TAG_{compile,type}_unit DIE is done here. */
6954 num_extra_attrs = ((stmt_list != NULL)
6955 + (low_pc != NULL)
6956 + (high_pc != NULL)
6957 + (ranges != NULL)
6958 + (comp_dir != NULL));
6959 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6960 num_extra_attrs);
b0c7bfa9
DE
6961
6962 /* Copy over the attributes from the stub to the DIE we just read in. */
6963 comp_unit_die = *result_comp_unit_die;
6964 i = comp_unit_die->num_attrs;
6965 if (stmt_list != NULL)
6966 comp_unit_die->attrs[i++] = *stmt_list;
6967 if (low_pc != NULL)
6968 comp_unit_die->attrs[i++] = *low_pc;
6969 if (high_pc != NULL)
6970 comp_unit_die->attrs[i++] = *high_pc;
6971 if (ranges != NULL)
6972 comp_unit_die->attrs[i++] = *ranges;
6973 if (comp_dir != NULL)
6974 comp_unit_die->attrs[i++] = *comp_dir;
6975 comp_unit_die->num_attrs += num_extra_attrs;
6976
b4f54984 6977 if (dwarf_die_debug)
bf6af496
DE
6978 {
6979 fprintf_unfiltered (gdb_stdlog,
6980 "Read die from %s@0x%x of %s:\n",
96b79293 6981 section->get_name (),
bf6af496
DE
6982 (unsigned) (begin_info_ptr - section->buffer),
6983 bfd_get_filename (abfd));
b4f54984 6984 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6985 }
6986
b0c7bfa9
DE
6987 /* Skip dummy compilation units. */
6988 if (info_ptr >= begin_info_ptr + dwo_unit->length
6989 || peek_abbrev_code (abfd, info_ptr) == 0)
6990 return 0;
6991
6992 *result_info_ptr = info_ptr;
6993 return 1;
6994}
6995
a084a2a6
AT
6996/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6997 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6998 signature is part of the header. */
6999static gdb::optional<ULONGEST>
7000lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7001{
7002 if (cu->header.version >= 5)
7003 return cu->header.signature;
7004 struct attribute *attr;
7005 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 7006 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 7007 return gdb::optional<ULONGEST> ();
529908cb 7008 return attr->as_unsigned ();
a084a2a6
AT
7009}
7010
c0ab21c2 7011/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7012 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7013 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7014
7015static struct dwo_unit *
4ab09049 7016lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 7017{
4ab09049 7018 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 7019 struct dwo_unit *dwo_unit;
c0ab21c2 7020 const char *comp_dir;
b0c7bfa9 7021
a2ce51a0
DE
7022 gdb_assert (cu != NULL);
7023
b0c7bfa9 7024 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7025 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7026 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 7027
4ab09049
SM
7028 if (per_cu->is_debug_types)
7029 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
7030 else
7031 {
a084a2a6 7032 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 7033
a084a2a6 7034 if (!signature.has_value ())
b0c7bfa9
DE
7035 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7036 " [in module %s]"),
4ab09049
SM
7037 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7038
7039 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
7040 }
7041
b0c7bfa9
DE
7042 return dwo_unit;
7043}
7044
c0ab21c2 7045/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7046 See it for a description of the parameters.
fcd3b13d 7047 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7048
c0ab21c2 7049void
9e021579
SM
7050cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7051 dwarf2_per_objfile *per_objfile,
2e671100 7052 dwarf2_cu *existing_cu)
a2ce51a0 7053{
a2ce51a0 7054 struct signatured_type *sig_type;
a2ce51a0
DE
7055
7056 /* Verify we can do the following downcast, and that we have the
7057 data we need. */
7058 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7059 sig_type = (struct signatured_type *) this_cu;
7060 gdb_assert (sig_type->dwo_unit != NULL);
7061
2e671100
SM
7062 dwarf2_cu *cu;
7063
7064 if (existing_cu != nullptr)
6aa5f3a6 7065 {
2e671100
SM
7066 cu = existing_cu;
7067 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7068 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7069 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7070 }
7071 else
7072 {
7188ed02 7073 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7074 in per_objfile yet. */
7188ed02 7075 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7076 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7077 cu = m_new_cu.get ();
6aa5f3a6
DE
7078 }
7079
7080 /* A future optimization, if needed, would be to use an existing
7081 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7082 could share abbrev tables. */
a2ce51a0 7083
2e671100 7084 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7085 NULL /* stub_comp_unit_die */,
7086 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7087 this, &info_ptr,
3e225074 7088 &comp_unit_die,
c0ab21c2 7089 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7090 {
7091 /* Dummy die. */
c0ab21c2 7092 dummy_p = true;
a2ce51a0 7093 }
a2ce51a0
DE
7094}
7095
fd820528 7096/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7097 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7098
f4dc4d17
DE
7099 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7100 Otherwise the table specified in the comp unit header is read in and used.
7101 This is an optimization for when we already have the abbrev table.
7102
2e671100
SM
7103 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7104 allocated. */
aaa75496 7105
ab432490 7106cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7107 dwarf2_per_objfile *per_objfile,
c0ab21c2 7108 struct abbrev_table *abbrev_table,
2e671100 7109 dwarf2_cu *existing_cu,
c0ab21c2
TT
7110 bool skip_partial)
7111 : die_reader_specs {},
6751ebae 7112 m_this_cu (this_cu)
c906108c 7113{
976ca316 7114 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7115 struct dwarf2_section_info *section = this_cu->section;
96b79293 7116 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7117 const gdb_byte *begin_info_ptr;
dee91e82 7118 struct signatured_type *sig_type = NULL;
4bdcc0c1 7119 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7120 /* Non-zero if CU currently points to a DWO file and we need to
7121 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7122 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7123 int rereading_dwo_cu = 0;
c906108c 7124
b4f54984 7125 if (dwarf_die_debug)
9d8780f0 7126 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7127 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7128 sect_offset_str (this_cu->sect_off));
09406207 7129
a2ce51a0
DE
7130 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7131 file (instead of going through the stub), short-circuit all of this. */
7132 if (this_cu->reading_dwo_directly)
7133 {
7134 /* Narrow down the scope of possibilities to have to understand. */
7135 gdb_assert (this_cu->is_debug_types);
7136 gdb_assert (abbrev_table == NULL);
976ca316 7137 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7138 return;
7139 }
7140
dee91e82 7141 /* This is cheap if the section is already read in. */
96b79293 7142 section->read (objfile);
dee91e82 7143
9c541725 7144 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7145
7146 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7147
2e671100
SM
7148 dwarf2_cu *cu;
7149
7150 if (existing_cu != nullptr)
dee91e82 7151 {
2e671100 7152 cu = existing_cu;
42e7ad6c
DE
7153 /* If this CU is from a DWO file we need to start over, we need to
7154 refetch the attributes from the skeleton CU.
7155 This could be optimized by retrieving those attributes from when we
7156 were here the first time: the previous comp_unit_die was stored in
7157 comp_unit_obstack. But there's no data yet that we need this
7158 optimization. */
7159 if (cu->dwo_unit != NULL)
7160 rereading_dwo_cu = 1;
dee91e82
DE
7161 }
7162 else
7163 {
7188ed02 7164 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7165 in per_objfile yet. */
976ca316
SM
7166 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7167 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7168 cu = m_new_cu.get ();
42e7ad6c 7169 }
dee91e82 7170
b0c7bfa9 7171 /* Get the header. */
9c541725 7172 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7173 {
7174 /* We already have the header, there's no need to read it in again. */
9c541725 7175 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7176 }
7177 else
7178 {
3019eac3 7179 if (this_cu->is_debug_types)
dee91e82 7180 {
976ca316
SM
7181 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7182 section, abbrev_section,
7183 info_ptr, rcuh_kind::TYPE);
dee91e82 7184
42e7ad6c
DE
7185 /* Since per_cu is the first member of struct signatured_type,
7186 we can go from a pointer to one to a pointer to the other. */
7187 sig_type = (struct signatured_type *) this_cu;
43988095 7188 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7189 gdb_assert (sig_type->type_offset_in_tu
7190 == cu->header.type_cu_offset_in_tu);
7191 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7192
42e7ad6c
DE
7193 /* LENGTH has not been set yet for type units if we're
7194 using .gdb_index. */
4057dfde 7195 this_cu->length = cu->header.get_length ();
3019eac3
DE
7196
7197 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7198 sig_type->type_offset_in_section =
7199 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7200
7201 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7202 }
7203 else
7204 {
976ca316
SM
7205 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7206 section, abbrev_section,
43988095
JK
7207 info_ptr,
7208 rcuh_kind::COMPILE);
dee91e82 7209
9c541725 7210 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7211 if (this_cu->length == 0)
7212 this_cu->length = cu->header.get_length ();
7213 else
7214 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7215 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7216 }
7217 }
10b3939b 7218
6caca83c 7219 /* Skip dummy compilation units. */
dee91e82 7220 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7221 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7222 {
7223 dummy_p = true;
7224 return;
7225 }
6caca83c 7226
433df2d4
DE
7227 /* If we don't have them yet, read the abbrevs for this compilation unit.
7228 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7229 done. */
f4dc4d17 7230 if (abbrev_table != NULL)
685af9cd
TT
7231 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7232 else
f4dc4d17 7233 {
606decb2 7234 abbrev_section->read (objfile);
c0ab21c2 7235 m_abbrev_table_holder
606decb2 7236 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 7237 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7238 }
af703f96 7239
dee91e82 7240 /* Read the top level CU/TU die. */
c0ab21c2 7241 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7242 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7243
58f0c718 7244 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7245 {
7246 dummy_p = true;
7247 return;
7248 }
58f0c718 7249
b0c7bfa9 7250 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7251 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7252 table from the DWO file and pass the ownership over to us. It will be
7253 referenced from READER, so we must make sure to free it after we're done
7254 with READER.
7255
b0c7bfa9
DE
7256 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7257 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7258 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7259 if (dwo_name != nullptr)
3019eac3 7260 {
3019eac3 7261 struct dwo_unit *dwo_unit;
b0c7bfa9 7262 struct die_info *dwo_comp_unit_die;
3019eac3 7263
3e225074 7264 if (comp_unit_die->has_children)
6a506a2d 7265 {
b98664d3 7266 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7267 " has children (offset %s) [in module %s]"),
7268 sect_offset_str (this_cu->sect_off),
7269 bfd_get_filename (abfd));
6a506a2d 7270 }
4ab09049 7271 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7272 if (dwo_unit != NULL)
3019eac3 7273 {
4ab09049 7274 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7275 comp_unit_die, NULL,
c0ab21c2 7276 this, &info_ptr,
3e225074 7277 &dwo_comp_unit_die,
c0ab21c2 7278 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7279 {
7280 /* Dummy die. */
c0ab21c2 7281 dummy_p = true;
6a506a2d
DE
7282 return;
7283 }
7284 comp_unit_die = dwo_comp_unit_die;
7285 }
7286 else
7287 {
7288 /* Yikes, we couldn't find the rest of the DIE, we only have
7289 the stub. A complaint has already been logged. There's
7290 not much more we can do except pass on the stub DIE to
7291 die_reader_func. We don't want to throw an error on bad
7292 debug info. */
3019eac3
DE
7293 }
7294 }
c0ab21c2 7295}
3019eac3 7296
6751ebae
TT
7297void
7298cutu_reader::keep ()
c0ab21c2 7299{
b0c7bfa9 7300 /* Done, clean up. */
6751ebae
TT
7301 gdb_assert (!dummy_p);
7302 if (m_new_cu != NULL)
348e048f 7303 {
7188ed02 7304 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 7305 now. */
7188ed02
SM
7306 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7307 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7308 }
dee91e82
DE
7309}
7310
18a8505e
AT
7311/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7312 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7313 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7314
7315 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7316 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7317
7318 We fill in THIS_CU->length.
7319
dee91e82 7320 THIS_CU->cu is always freed when done.
3019eac3 7321 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7322 to care whether it refers to the "main" CU or the DWO CU.
7323
7324 When parent_cu is passed, it is used to provide a default value for
7325 str_offsets_base and addr_base from the parent. */
dee91e82 7326
ab432490 7327cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7328 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7329 struct dwarf2_cu *parent_cu,
7330 struct dwo_file *dwo_file)
7331 : die_reader_specs {},
7332 m_this_cu (this_cu)
dee91e82 7333{
976ca316 7334 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7335 struct dwarf2_section_info *section = this_cu->section;
96b79293 7336 bfd *abfd = section->get_bfd_owner ();
33e80786 7337 struct dwarf2_section_info *abbrev_section;
d521ce57 7338 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7339
b4f54984 7340 if (dwarf_die_debug)
9d8780f0 7341 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7342 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7343 sect_offset_str (this_cu->sect_off));
09406207 7344
976ca316 7345 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7346
33e80786
DE
7347 abbrev_section = (dwo_file != NULL
7348 ? &dwo_file->sections.abbrev
7349 : get_abbrev_section_for_cu (this_cu));
7350
dee91e82 7351 /* This is cheap if the section is already read in. */
96b79293 7352 section->read (objfile);
dee91e82 7353
976ca316 7354 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7355
9c541725 7356 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7357 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7358 section, abbrev_section, info_ptr,
43988095
JK
7359 (this_cu->is_debug_types
7360 ? rcuh_kind::TYPE
7361 : rcuh_kind::COMPILE));
dee91e82 7362
18a8505e
AT
7363 if (parent_cu != nullptr)
7364 {
c0ab21c2
TT
7365 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7366 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7367 }
4057dfde 7368 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7369
7370 /* Skip dummy compilation units. */
7371 if (info_ptr >= begin_info_ptr + this_cu->length
7372 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7373 {
7374 dummy_p = true;
7375 return;
7376 }
72bf9492 7377
606decb2 7378 abbrev_section->read (objfile);
c0ab21c2 7379 m_abbrev_table_holder
606decb2 7380 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7381
c0ab21c2
TT
7382 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7383 m_abbrev_table_holder.get ());
3e225074 7384 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7385}
7386
0018ea6f
DE
7387\f
7388/* Type Unit Groups.
dee91e82 7389
0018ea6f
DE
7390 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7391 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7392 so that all types coming from the same compilation (.o file) are grouped
7393 together. A future step could be to put the types in the same symtab as
7394 the CU the types ultimately came from. */
ff013f42 7395
f4dc4d17
DE
7396static hashval_t
7397hash_type_unit_group (const void *item)
7398{
9a3c8263
SM
7399 const struct type_unit_group *tu_group
7400 = (const struct type_unit_group *) item;
f4dc4d17 7401
094b34ac 7402 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7403}
348e048f
DE
7404
7405static int
f4dc4d17 7406eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7407{
9a3c8263
SM
7408 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7409 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7410
094b34ac 7411 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7412}
348e048f 7413
f4dc4d17
DE
7414/* Allocate a hash table for type unit groups. */
7415
eaa5fa8b 7416static htab_up
298e9637 7417allocate_type_unit_groups_table ()
f4dc4d17 7418{
eaa5fa8b
TT
7419 return htab_up (htab_create_alloc (3,
7420 hash_type_unit_group,
7421 eq_type_unit_group,
7422 NULL, xcalloc, xfree));
f4dc4d17 7423}
dee91e82 7424
f4dc4d17
DE
7425/* Type units that don't have DW_AT_stmt_list are grouped into their own
7426 partial symtabs. We combine several TUs per psymtab to not let the size
7427 of any one psymtab grow too big. */
7428#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7429#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7430
094b34ac 7431/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7432 Create the type_unit_group object used to hold one or more TUs. */
7433
7434static struct type_unit_group *
094b34ac 7435create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7436{
976ca316
SM
7437 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7438 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7439 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7440 struct type_unit_group *tu_group;
f4dc4d17 7441
976ca316 7442 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7443 per_cu = &tu_group->per_cu;
1859c670 7444 per_cu->per_bfd = per_bfd;
f4dc4d17 7445
1859c670 7446 if (per_bfd->using_index)
094b34ac 7447 {
1859c670 7448 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7449 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7450 }
7451 else
7452 {
9c541725 7453 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7454 dwarf2_psymtab *pst;
528e1572 7455 std::string name;
094b34ac
DE
7456
7457 /* Give the symtab a useful name for debug purposes. */
7458 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7459 name = string_printf ("<type_units_%d>",
7460 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7461 else
528e1572 7462 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7463
976ca316 7464 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7465 pst->anonymous = true;
094b34ac 7466 }
f4dc4d17 7467
094b34ac 7468 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7469 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7470
7471 return tu_group;
7472}
7473
094b34ac
DE
7474/* Look up the type_unit_group for type unit CU, and create it if necessary.
7475 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7476
7477static struct type_unit_group *
ff39bb5e 7478get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7479{
976ca316
SM
7480 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7481 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7482 struct type_unit_group *tu_group;
7483 void **slot;
7484 unsigned int line_offset;
7485 struct type_unit_group type_unit_group_for_lookup;
7486
976ca316
SM
7487 if (per_objfile->per_bfd->type_unit_groups == NULL)
7488 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7489
7490 /* Do we need to create a new group, or can we use an existing one? */
7491
529908cb 7492 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7493 {
529908cb 7494 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7495 ++tu_stats->nr_symtab_sharers;
7496 }
7497 else
7498 {
7499 /* Ugh, no stmt_list. Rare, but we have to handle it.
7500 We can do various things here like create one group per TU or
7501 spread them over multiple groups to split up the expansion work.
7502 To avoid worst case scenarios (too many groups or too large groups)
7503 we, umm, group them in bunches. */
7504 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7505 | (tu_stats->nr_stmt_less_type_units
7506 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7507 ++tu_stats->nr_stmt_less_type_units;
7508 }
7509
094b34ac 7510 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7511 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7512 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7513 &type_unit_group_for_lookup, INSERT);
7514 if (*slot != NULL)
7515 {
9a3c8263 7516 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7517 gdb_assert (tu_group != NULL);
7518 }
7519 else
7520 {
9c541725 7521 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7522 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7523 *slot = tu_group;
7524 ++tu_stats->nr_symtabs;
7525 }
7526
7527 return tu_group;
7528}
0018ea6f
DE
7529\f
7530/* Partial symbol tables. */
7531
7532/* Create a psymtab named NAME and assign it to PER_CU.
7533
7534 The caller must fill in the following details:
7535 dirname, textlow, texthigh. */
7536
891813be 7537static dwarf2_psymtab *
7aa104c4
SM
7538create_partial_symtab (dwarf2_per_cu_data *per_cu,
7539 dwarf2_per_objfile *per_objfile,
7540 const char *name)
0018ea6f 7541{
7aa104c4 7542 struct objfile *objfile = per_objfile->objfile;
891813be 7543 dwarf2_psymtab *pst;
0018ea6f 7544
9f4e76a4 7545 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7546
6d94535f 7547 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7548
7549 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7550 per_cu->v.psymtab = pst;
7551
7552 return pst;
7553}
7554
c0ab21c2 7555/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7556
7557static void
7558process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7559 const gdb_byte *info_ptr,
0018ea6f 7560 struct die_info *comp_unit_die,
c0ab21c2 7561 enum language pretend_language)
0018ea6f
DE
7562{
7563 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7564 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7565 struct objfile *objfile = per_objfile->objfile;
08feed99 7566 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7567 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7568 CORE_ADDR baseaddr;
7569 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7570 dwarf2_psymtab *pst;
3a2b436a 7571 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7572 const char *filename;
0018ea6f 7573
0018ea6f
DE
7574 gdb_assert (! per_cu->is_debug_types);
7575
c0ab21c2 7576 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7577
0018ea6f 7578 /* Allocate a new partial symbol table structure. */
2e927613
TV
7579 gdb::unique_xmalloc_ptr<char> debug_filename;
7580 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7581 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7582 if (filename == NULL)
0018ea6f 7583 filename = "";
2e927613
TV
7584 else if (strcmp (filename, artificial) == 0)
7585 {
7586 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7587 sect_offset_str (per_cu->sect_off),
7588 (char *) NULL));
2e927613
TV
7589 filename = debug_filename.get ();
7590 }
0018ea6f 7591
7aa104c4 7592 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7593
7594 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7595 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7596
b3b3bada 7597 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7598
7599 dwarf2_find_base_address (comp_unit_die, cu);
7600
7601 /* Possibly set the default values of LOWPC and HIGHPC from
7602 `DW_AT_ranges'. */
3a2b436a
JK
7603 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7604 &best_highpc, cu, pst);
7605 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7606 {
7607 CORE_ADDR low
7608 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7609 - baseaddr);
7610 CORE_ADDR high
7611 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7612 - baseaddr - 1);
7613 /* Store the contiguous range if it is not empty; it can be
7614 empty for CUs with no code. */
d320c2b5
TT
7615 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7616 low, high, pst);
79748972 7617 }
0018ea6f
DE
7618
7619 /* Check if comp unit has_children.
7620 If so, read the rest of the partial symbols from this comp unit.
7621 If not, there's no more debug_info for this comp unit. */
3e225074 7622 if (comp_unit_die->has_children)
0018ea6f
DE
7623 {
7624 struct partial_die_info *first_die;
7625 CORE_ADDR lowpc, highpc;
7626
7627 lowpc = ((CORE_ADDR) -1);
7628 highpc = ((CORE_ADDR) 0);
7629
7630 first_die = load_partial_dies (reader, info_ptr, 1);
7631
7632 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7633 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7634
7635 /* If we didn't find a lowpc, set it to highpc to avoid
7636 complaints from `maint check'. */
7637 if (lowpc == ((CORE_ADDR) -1))
7638 lowpc = highpc;
7639
7640 /* If the compilation unit didn't have an explicit address range,
7641 then use the information extracted from its child dies. */
e385593e 7642 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7643 {
7644 best_lowpc = lowpc;
7645 best_highpc = highpc;
7646 }
7647 }
4ae976d1 7648 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7649 best_lowpc + baseaddr)
7650 - baseaddr);
4ae976d1 7651 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7652 best_highpc + baseaddr)
7653 - baseaddr);
0018ea6f 7654
ae7754b2 7655 pst->end ();
0018ea6f 7656
ae640021 7657 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7658 {
7659 int i;
ae640021 7660 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7661
7662 /* Fill in 'dependencies' here; we fill in 'users' in a
7663 post-pass. */
7664 pst->number_of_dependencies = len;
a9342b62
TT
7665 pst->dependencies
7666 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7667 for (i = 0; i < len; ++i)
7668 {
7669 pst->dependencies[i]
7670 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7671 }
0018ea6f 7672
ae640021 7673 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7674 }
7675
7676 /* Get the list of files included in the current compilation unit,
7677 and build a psymtab for each of them. */
7678 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7679
6f738b01
SM
7680 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7681 ", %d global, %d static syms",
7682 per_cu->is_debug_types ? "type" : "comp",
7683 sect_offset_str (per_cu->sect_off),
7684 paddress (gdbarch, pst->text_low (objfile)),
7685 paddress (gdbarch, pst->text_high (objfile)),
7686 (int) pst->global_psymbols.size (),
7687 (int) pst->static_psymbols.size ());
0018ea6f
DE
7688}
7689
7690/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7691 Process compilation unit THIS_CU for a psymtab. */
7692
7693static void
ab432490
SM
7694process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7695 dwarf2_per_objfile *per_objfile,
135f5437 7696 bool want_partial_unit,
b93601f3 7697 enum language pretend_language)
0018ea6f
DE
7698{
7699 /* If this compilation unit was already read in, free the
7700 cached copy in order to read it in again. This is
7701 necessary because we skipped some symbols when we first
7702 read in the compilation unit (see load_partial_dies).
7703 This problem could be avoided, but the benefit is unclear. */
7188ed02 7704 per_objfile->remove_cu (this_cu);
0018ea6f 7705
2e671100 7706 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7707
58990295
TV
7708 switch (reader.comp_unit_die->tag)
7709 {
7710 case DW_TAG_compile_unit:
7711 this_cu->unit_type = DW_UT_compile;
7712 break;
7713 case DW_TAG_partial_unit:
7714 this_cu->unit_type = DW_UT_partial;
7715 break;
e77b0004
TV
7716 case DW_TAG_type_unit:
7717 this_cu->unit_type = DW_UT_type;
7718 break;
58990295
TV
7719 default:
7720 abort ();
7721 }
7722
c0ab21c2 7723 if (reader.dummy_p)
f1902523 7724 {
c0ab21c2 7725 /* Nothing. */
f1902523 7726 }
c0ab21c2 7727 else if (this_cu->is_debug_types)
3e225074
TT
7728 build_type_psymtabs_reader (&reader, reader.info_ptr,
7729 reader.comp_unit_die);
135f5437
TT
7730 else if (want_partial_unit
7731 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7732 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7733 reader.comp_unit_die,
c0ab21c2 7734 pretend_language);
0018ea6f 7735
7188ed02 7736 this_cu->lang = reader.cu->language;
58990295 7737
0018ea6f 7738 /* Age out any secondary CUs. */
7188ed02 7739 per_objfile->age_comp_units ();
0018ea6f 7740}
f4dc4d17
DE
7741
7742/* Reader function for build_type_psymtabs. */
7743
7744static void
7745build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7746 const gdb_byte *info_ptr,
3e225074 7747 struct die_info *type_unit_die)
f4dc4d17 7748{
976ca316 7749 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7750 struct dwarf2_cu *cu = reader->cu;
7751 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7752 struct signatured_type *sig_type;
f4dc4d17
DE
7753 struct type_unit_group *tu_group;
7754 struct attribute *attr;
7755 struct partial_die_info *first_die;
7756 CORE_ADDR lowpc, highpc;
891813be 7757 dwarf2_psymtab *pst;
f4dc4d17 7758
0186c6a7
DE
7759 gdb_assert (per_cu->is_debug_types);
7760 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7761
3e225074 7762 if (! type_unit_die->has_children)
f4dc4d17
DE
7763 return;
7764
052c8bb8 7765 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7766 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7767
df07e2c7 7768 if (tu_group->tus == nullptr)
a8b3b8e9 7769 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7770 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7771
7772 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7773 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7774 pst->anonymous = true;
f4dc4d17
DE
7775
7776 first_die = load_partial_dies (reader, info_ptr, 1);
7777
7778 lowpc = (CORE_ADDR) -1;
7779 highpc = (CORE_ADDR) 0;
7780 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7781
ae7754b2 7782 pst->end ();
f4dc4d17
DE
7783}
7784
73051182
DE
7785/* Struct used to sort TUs by their abbreviation table offset. */
7786
7787struct tu_abbrev_offset
7788{
b2bdb8cf
SM
7789 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7790 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7791 {}
7792
7793 signatured_type *sig_type;
73051182
DE
7794 sect_offset abbrev_offset;
7795};
7796
484cf504 7797/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7798
484cf504
TT
7799static bool
7800sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7801 const struct tu_abbrev_offset &b)
73051182 7802{
484cf504 7803 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7804}
7805
7806/* Efficiently read all the type units.
7807 This does the bulk of the work for build_type_psymtabs.
7808
7809 The efficiency is because we sort TUs by the abbrev table they use and
7810 only read each abbrev table once. In one program there are 200K TUs
7811 sharing 8K abbrev tables.
7812
7813 The main purpose of this function is to support building the
5989a64e 7814 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7815 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7816 can collapse the search space by grouping them by stmt_list.
7817 The savings can be significant, in the same program from above the 200K TUs
7818 share 8K stmt_list tables.
7819
7820 FUNC is expected to call get_type_unit_group, which will create the
7821 struct type_unit_group if necessary and add it to
5989a64e 7822 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7823
7824static void
976ca316 7825build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7826{
976ca316 7827 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7828 abbrev_table_up abbrev_table;
73051182 7829 sect_offset abbrev_offset;
73051182
DE
7830
7831 /* It's up to the caller to not call us multiple times. */
976ca316 7832 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7833
976ca316 7834 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7835 return;
7836
7837 /* TUs typically share abbrev tables, and there can be way more TUs than
7838 abbrev tables. Sort by abbrev table to reduce the number of times we
7839 read each abbrev table in.
7840 Alternatives are to punt or to maintain a cache of abbrev tables.
7841 This is simpler and efficient enough for now.
7842
7843 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7844 symtab to use). Typically TUs with the same abbrev offset have the same
7845 stmt_list value too so in practice this should work well.
7846
7847 The basic algorithm here is:
7848
7849 sort TUs by abbrev table
7850 for each TU with same abbrev table:
7851 read abbrev table if first user
7852 read TU top level DIE
7853 [IWBN if DWO skeletons had DW_AT_stmt_list]
7854 call FUNC */
7855
6f738b01 7856 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7857
7858 /* Sort in a separate table to maintain the order of all_type_units
7859 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7860 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7861 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7862
976ca316 7863 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7864 sorted_by_abbrev.emplace_back
976ca316 7865 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7866 sig_type->per_cu.sect_off));
73051182 7867
484cf504
TT
7868 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7869 sort_tu_by_abbrev_offset);
73051182 7870
9c541725 7871 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7872
b2bdb8cf 7873 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7874 {
73051182
DE
7875 /* Switch to the next abbrev table if necessary. */
7876 if (abbrev_table == NULL
b2bdb8cf 7877 || tu.abbrev_offset != abbrev_offset)
73051182 7878 {
b2bdb8cf 7879 abbrev_offset = tu.abbrev_offset;
606decb2 7880 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7881 abbrev_table =
606decb2 7882 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7883 ++tu_stats->nr_uniq_abbrev_tables;
7884 }
7885
976ca316 7886 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7887 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7888 if (!reader.dummy_p)
7889 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7890 reader.comp_unit_die);
73051182 7891 }
6aa5f3a6 7892}
73051182 7893
6aa5f3a6
DE
7894/* Print collected type unit statistics. */
7895
7896static void
976ca316 7897print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7898{
976ca316 7899 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7900
6f738b01
SM
7901 dwarf_read_debug_printf ("Type unit statistics:");
7902 dwarf_read_debug_printf (" %zu TUs",
7903 per_objfile->per_bfd->all_type_units.size ());
7904 dwarf_read_debug_printf (" %d uniq abbrev tables",
7905 tu_stats->nr_uniq_abbrev_tables);
7906 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7907 tu_stats->nr_symtabs);
7908 dwarf_read_debug_printf (" %d symtab sharers",
7909 tu_stats->nr_symtab_sharers);
7910 dwarf_read_debug_printf (" %d type units without a stmt_list",
7911 tu_stats->nr_stmt_less_type_units);
7912 dwarf_read_debug_printf (" %d all_type_units reallocs",
7913 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7914}
7915
f4dc4d17
DE
7916/* Traversal function for build_type_psymtabs. */
7917
7918static int
7919build_type_psymtab_dependencies (void **slot, void *info)
7920{
976ca316
SM
7921 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7922 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 7923 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7924 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7925 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7926 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7927 int i;
7928
7929 gdb_assert (len > 0);
197400e8 7930 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7931
7932 pst->number_of_dependencies = len;
a9342b62 7933 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7934 for (i = 0; i < len; ++i)
f4dc4d17 7935 {
df07e2c7 7936 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7937 gdb_assert (iter->per_cu.is_debug_types);
7938 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7939 iter->type_unit_group = tu_group;
f4dc4d17
DE
7940 }
7941
df07e2c7
AB
7942 delete tu_group->tus;
7943 tu_group->tus = nullptr;
348e048f
DE
7944
7945 return 1;
7946}
7947
7948/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7949 Build partial symbol tables for the .debug_types comp-units. */
7950
7951static void
976ca316 7952build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7953{
976ca316 7954 if (! create_all_type_units (per_objfile))
348e048f
DE
7955 return;
7956
976ca316 7957 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7958}
f4dc4d17 7959
6aa5f3a6
DE
7960/* Traversal function for process_skeletonless_type_unit.
7961 Read a TU in a DWO file and build partial symbols for it. */
7962
7963static int
7964process_skeletonless_type_unit (void **slot, void *info)
7965{
7966 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7967 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7968 struct signatured_type find_entry, *entry;
7969
7970 /* If this TU doesn't exist in the global table, add it and read it in. */
7971
976ca316
SM
7972 if (per_objfile->per_bfd->signatured_types == NULL)
7973 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7974
7975 find_entry.signature = dwo_unit->signature;
976ca316 7976 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7977 &find_entry, INSERT);
6aa5f3a6
DE
7978 /* If we've already seen this type there's nothing to do. What's happening
7979 is we're doing our own version of comdat-folding here. */
7980 if (*slot != NULL)
7981 return 1;
7982
7983 /* This does the job that create_all_type_units would have done for
7984 this TU. */
976ca316
SM
7985 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7986 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7987 *slot = entry;
7988
7989 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7990 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7991 if (!reader.dummy_p)
7992 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7993 reader.comp_unit_die);
6aa5f3a6
DE
7994
7995 return 1;
7996}
7997
7998/* Traversal function for process_skeletonless_type_units. */
7999
8000static int
8001process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8002{
8003 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8004
8005 if (dwo_file->tus != NULL)
b0b6a987
TT
8006 htab_traverse_noresize (dwo_file->tus.get (),
8007 process_skeletonless_type_unit, info);
6aa5f3a6
DE
8008
8009 return 1;
8010}
8011
8012/* Scan all TUs of DWO files, verifying we've processed them.
8013 This is needed in case a TU was emitted without its skeleton.
8014 Note: This can't be done until we know what all the DWO files are. */
8015
8016static void
976ca316 8017process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
8018{
8019 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
8020 if (get_dwp_file (per_objfile) == NULL
8021 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 8022 {
976ca316 8023 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 8024 process_dwo_file_for_skeletonless_type_units,
976ca316 8025 per_objfile);
6aa5f3a6 8026 }
348e048f
DE
8027}
8028
ed2dc618 8029/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8030
8031static void
976ca316 8032set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 8033{
976ca316 8034 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 8035 {
891813be 8036 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8037
36586728
TT
8038 if (pst == NULL)
8039 continue;
8040
b76e467d 8041 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8042 {
8043 /* Set the 'user' field only if it is not already set. */
8044 if (pst->dependencies[j]->user == NULL)
8045 pst->dependencies[j]->user = pst;
8046 }
8047 }
8048}
8049
93311388
DE
8050/* Build the partial symbol table by doing a quick pass through the
8051 .debug_info and .debug_abbrev sections. */
72bf9492 8052
93311388 8053static void
976ca316 8054dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8055{
976ca316 8056 struct objfile *objfile = per_objfile->objfile;
93311388 8057
6f738b01
SM
8058 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8059 objfile_name (objfile));
45cfd468 8060
76935768 8061 scoped_restore restore_reading_psyms
976ca316 8062 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 8063 true);
98bfdba5 8064
976ca316 8065 per_objfile->per_bfd->info.read (objfile);
91c24f0a 8066
93311388
DE
8067 /* Any cached compilation units will be linked by the per-objfile
8068 read_in_chain. Make sure to free them when we're done. */
976ca316 8069 free_cached_comp_units freer (per_objfile);
72bf9492 8070
976ca316 8071 build_type_psymtabs (per_objfile);
348e048f 8072
976ca316 8073 create_all_comp_units (per_objfile);
c906108c 8074
60606b2c
TT
8075 /* Create a temporary address map on a temporary obstack. We later
8076 copy this to the final obstack. */
8268c778 8077 auto_obstack temp_obstack;
791afaa2
TT
8078
8079 scoped_restore save_psymtabs_addrmap
d320c2b5 8080 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8081 addrmap_create_mutable (&temp_obstack));
72bf9492 8082
976ca316 8083 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8084 {
8085 if (per_cu->v.psymtab != NULL)
8086 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8087 continue;
976ca316 8088 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8089 language_minimal);
3d5afab3 8090 }
ff013f42 8091
6aa5f3a6 8092 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8093 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8094
8095 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8096 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8097 {
976ca316
SM
8098 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8099 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8100 }
8101
6f738b01 8102 if (dwarf_read_debug > 0)
976ca316 8103 print_tu_stats (per_objfile);
6aa5f3a6 8104
976ca316 8105 set_partial_user (per_objfile);
95554aad 8106
d320c2b5
TT
8107 objfile->partial_symtabs->psymtabs_addrmap
8108 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8109 objfile->partial_symtabs->obstack ());
791afaa2
TT
8110 /* At this point we want to keep the address map. */
8111 save_psymtabs_addrmap.release ();
ff013f42 8112
6f738b01
SM
8113 dwarf_read_debug_printf ("Done building psymtabs of %s",
8114 objfile_name (objfile));
ae038cb0
DJ
8115}
8116
dee91e82
DE
8117/* Load the partial DIEs for a secondary CU into memory.
8118 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8119
dee91e82 8120static void
ab432490 8121load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8122 dwarf2_per_objfile *per_objfile,
8123 dwarf2_cu *existing_cu)
dee91e82 8124{
2e671100 8125 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8126
8127 if (!reader.dummy_p)
8128 {
8129 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8130 language_minimal);
8131
8132 /* Check if comp unit has_children.
8133 If so, read the rest of the partial symbols from this comp unit.
8134 If not, there's no more debug_info for this comp unit. */
3e225074 8135 if (reader.comp_unit_die->has_children)
c0ab21c2 8136 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8137
8138 reader.keep ();
c0ab21c2 8139 }
ae038cb0
DJ
8140}
8141
ae038cb0 8142static void
976ca316 8143read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8144 struct dwarf2_section_info *section,
f1902523 8145 struct dwarf2_section_info *abbrev_section,
b76e467d 8146 unsigned int is_dwz)
ae038cb0 8147{
d521ce57 8148 const gdb_byte *info_ptr;
976ca316 8149 struct objfile *objfile = per_objfile->objfile;
be391dca 8150
6f738b01
SM
8151 dwarf_read_debug_printf ("Reading %s for %s",
8152 section->get_name (),
8153 section->get_file_name ());
bf6af496 8154
96b79293 8155 section->read (objfile);
ae038cb0 8156
36586728 8157 info_ptr = section->buffer;
6e70227d 8158
36586728 8159 while (info_ptr < section->buffer + section->size)
ae038cb0 8160 {
ae038cb0 8161 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8162
9c541725 8163 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8164
f1902523 8165 comp_unit_head cu_header;
976ca316 8166 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8167 abbrev_section, info_ptr,
8168 rcuh_kind::COMPILE);
ae038cb0
DJ
8169
8170 /* Save the compilation unit for later lookup. */
f1902523 8171 if (cu_header.unit_type != DW_UT_type)
976ca316 8172 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8173 else
8174 {
976ca316 8175 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8176 sig_type->signature = cu_header.signature;
8177 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8178 this_cu = &sig_type->per_cu;
8179 }
8180 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8181 this_cu->sect_off = sect_off;
f1902523 8182 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8183 this_cu->is_dwz = is_dwz;
8a0459fd 8184 this_cu->section = section;
ae038cb0 8185
976ca316 8186 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8187
8188 info_ptr = info_ptr + this_cu->length;
8189 }
36586728
TT
8190}
8191
8192/* Create a list of all compilation units in OBJFILE.
8193 This is only done for -readnow and building partial symtabs. */
8194
8195static void
976ca316 8196create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8197{
976ca316
SM
8198 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8199 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8200 &per_objfile->per_bfd->abbrev, 0);
36586728 8201
976ca316 8202 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8203 if (dwz != NULL)
976ca316 8204 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8205}
8206
5734ee8b 8207/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8208 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8209 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8210 DW_AT_ranges). See the comments of add_partial_subprogram on how
8211 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8212
72bf9492
DJ
8213static void
8214scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8215 CORE_ADDR *highpc, int set_addrmap,
8216 struct dwarf2_cu *cu)
c906108c 8217{
72bf9492 8218 struct partial_die_info *pdi;
c906108c 8219
91c24f0a
DC
8220 /* Now, march along the PDI's, descending into ones which have
8221 interesting children but skipping the children of the other ones,
8222 until we reach the end of the compilation unit. */
c906108c 8223
72bf9492 8224 pdi = first_die;
91c24f0a 8225
72bf9492
DJ
8226 while (pdi != NULL)
8227 {
52356b79 8228 pdi->fixup (cu);
c906108c 8229
f55ee35c 8230 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8231 children, so we need to look at them. Ditto for anonymous
8232 enums. */
933c6fe4 8233
7d00ffec 8234 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8235 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8236 || pdi->tag == DW_TAG_imported_unit
8237 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8238 {
72bf9492 8239 switch (pdi->tag)
c906108c
SS
8240 {
8241 case DW_TAG_subprogram:
b1dc1806 8242 case DW_TAG_inlined_subroutine:
cdc07690 8243 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8244 if (cu->language == language_cplus)
8245 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8246 set_addrmap, cu);
c906108c 8247 break;
72929c62 8248 case DW_TAG_constant:
c906108c
SS
8249 case DW_TAG_variable:
8250 case DW_TAG_typedef:
91c24f0a 8251 case DW_TAG_union_type:
317d2668
TV
8252 if (!pdi->is_declaration
8253 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8254 {
72bf9492 8255 add_partial_symbol (pdi, cu);
63d06c5c
DC
8256 }
8257 break;
c906108c 8258 case DW_TAG_class_type:
680b30c7 8259 case DW_TAG_interface_type:
c906108c 8260 case DW_TAG_structure_type:
72bf9492 8261 if (!pdi->is_declaration)
c906108c 8262 {
72bf9492 8263 add_partial_symbol (pdi, cu);
c906108c 8264 }
b7fee5a3
KS
8265 if ((cu->language == language_rust
8266 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8267 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8268 set_addrmap, cu);
c906108c 8269 break;
91c24f0a 8270 case DW_TAG_enumeration_type:
72bf9492
DJ
8271 if (!pdi->is_declaration)
8272 add_partial_enumeration (pdi, cu);
c906108c
SS
8273 break;
8274 case DW_TAG_base_type:
dda83cd7 8275 case DW_TAG_subrange_type:
c906108c 8276 /* File scope base type definitions are added to the partial
dda83cd7 8277 symbol table. */
72bf9492 8278 add_partial_symbol (pdi, cu);
c906108c 8279 break;
d9fa45fe 8280 case DW_TAG_namespace:
cdc07690 8281 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8282 break;
5d7cb8df 8283 case DW_TAG_module:
59c35742
AB
8284 if (!pdi->is_declaration)
8285 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8286 break;
95554aad
TT
8287 case DW_TAG_imported_unit:
8288 {
8289 struct dwarf2_per_cu_data *per_cu;
8290
f4dc4d17
DE
8291 /* For now we don't handle imported units in type units. */
8292 if (cu->per_cu->is_debug_types)
8293 {
8294 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8295 " supported in type units [in module %s]"),
5e22e966 8296 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8297 }
8298
e3b94546 8299 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8300 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8301
8302 /* Go read the partial unit, if needed. */
8303 if (per_cu->v.psymtab == NULL)
ab432490
SM
8304 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8305 cu->language);
95554aad 8306
ae640021 8307 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8308 }
8309 break;
74921315
KS
8310 case DW_TAG_imported_declaration:
8311 add_partial_symbol (pdi, cu);
8312 break;
c906108c
SS
8313 default:
8314 break;
8315 }
8316 }
8317
72bf9492
DJ
8318 /* If the die has a sibling, skip to the sibling. */
8319
8320 pdi = pdi->die_sibling;
8321 }
8322}
8323
8324/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8325
72bf9492 8326 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8327 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8328 Enumerators are an exception; they use the scope of their parent
8329 enumeration type, i.e. the name of the enumeration type is not
8330 prepended to the enumerator.
91c24f0a 8331
72bf9492
DJ
8332 There are two complexities. One is DW_AT_specification; in this
8333 case "parent" means the parent of the target of the specification,
8334 instead of the direct parent of the DIE. The other is compilers
8335 which do not emit DW_TAG_namespace; in this case we try to guess
8336 the fully qualified name of structure types from their members'
8337 linkage names. This must be done using the DIE's children rather
8338 than the children of any DW_AT_specification target. We only need
8339 to do this for structures at the top level, i.e. if the target of
8340 any DW_AT_specification (if any; otherwise the DIE itself) does not
8341 have a parent. */
8342
8343/* Compute the scope prefix associated with PDI's parent, in
8344 compilation unit CU. The result will be allocated on CU's
8345 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8346 field. NULL is returned if no prefix is necessary. */
15d034d0 8347static const char *
72bf9492
DJ
8348partial_die_parent_scope (struct partial_die_info *pdi,
8349 struct dwarf2_cu *cu)
8350{
15d034d0 8351 const char *grandparent_scope;
72bf9492 8352 struct partial_die_info *parent, *real_pdi;
91c24f0a 8353
72bf9492
DJ
8354 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8355 then this means the parent of the specification DIE. */
8356
8357 real_pdi = pdi;
72bf9492 8358 while (real_pdi->has_specification)
fb816e8b 8359 {
122cf0f2
AB
8360 auto res = find_partial_die (real_pdi->spec_offset,
8361 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8362 real_pdi = res.pdi;
8363 cu = res.cu;
8364 }
72bf9492
DJ
8365
8366 parent = real_pdi->die_parent;
8367 if (parent == NULL)
8368 return NULL;
8369
8370 if (parent->scope_set)
8371 return parent->scope;
8372
52356b79 8373 parent->fixup (cu);
72bf9492 8374
10b3939b 8375 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8376
acebe513
UW
8377 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8378 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8379 Work around this problem here. */
8380 if (cu->language == language_cplus
6e70227d 8381 && parent->tag == DW_TAG_namespace
7d00ffec 8382 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8383 && grandparent_scope == NULL)
8384 {
8385 parent->scope = NULL;
8386 parent->scope_set = 1;
8387 return NULL;
8388 }
8389
0a4b0913 8390 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8391 if (pdi->tag == DW_TAG_enumerator)
8392 /* Enumerators should not get the name of the enumeration as a prefix. */
8393 parent->scope = grandparent_scope;
8394 else if (parent->tag == DW_TAG_namespace
f55ee35c 8395 || parent->tag == DW_TAG_module
72bf9492
DJ
8396 || parent->tag == DW_TAG_structure_type
8397 || parent->tag == DW_TAG_class_type
680b30c7 8398 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8399 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8400 || parent->tag == DW_TAG_enumeration_type
8401 || (cu->language == language_fortran
8402 && parent->tag == DW_TAG_subprogram
8403 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8404 {
8405 if (grandparent_scope == NULL)
7d00ffec 8406 parent->scope = parent->name (cu);
72bf9492 8407 else
3e43a32a
MS
8408 parent->scope = typename_concat (&cu->comp_unit_obstack,
8409 grandparent_scope,
7d00ffec 8410 parent->name (cu), 0, cu);
72bf9492 8411 }
72bf9492
DJ
8412 else
8413 {
8414 /* FIXME drow/2004-04-01: What should we be doing with
8415 function-local names? For partial symbols, we should probably be
8416 ignoring them. */
fa9c3fa0
TT
8417 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8418 dwarf_tag_name (parent->tag),
8419 sect_offset_str (pdi->sect_off));
72bf9492 8420 parent->scope = grandparent_scope;
c906108c
SS
8421 }
8422
72bf9492
DJ
8423 parent->scope_set = 1;
8424 return parent->scope;
8425}
8426
8427/* Return the fully scoped name associated with PDI, from compilation unit
8428 CU. The result will be allocated with malloc. */
4568ecf9 8429
43816ebc 8430static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8431partial_die_full_name (struct partial_die_info *pdi,
8432 struct dwarf2_cu *cu)
8433{
15d034d0 8434 const char *parent_scope;
72bf9492 8435
98bfdba5
PA
8436 /* If this is a template instantiation, we can not work out the
8437 template arguments from partial DIEs. So, unfortunately, we have
8438 to go through the full DIEs. At least any work we do building
8439 types here will be reused if full symbols are loaded later. */
8440 if (pdi->has_template_arguments)
8441 {
52356b79 8442 pdi->fixup (cu);
98bfdba5 8443
7d00ffec 8444 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8445 {
8446 struct die_info *die;
8447 struct attribute attr;
8448 struct dwarf2_cu *ref_cu = cu;
8449
b64f50a1 8450 /* DW_FORM_ref_addr is using section offset. */
b4069958 8451 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8452 attr.form = DW_FORM_ref_addr;
9c541725 8453 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8454 die = follow_die_ref (NULL, &attr, &ref_cu);
8455
43816ebc 8456 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8457 }
8458 }
8459
72bf9492
DJ
8460 parent_scope = partial_die_parent_scope (pdi, cu);
8461 if (parent_scope == NULL)
8462 return NULL;
8463 else
43816ebc 8464 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8465 pdi->name (cu),
8466 0, cu));
c906108c
SS
8467}
8468
8469static void
72bf9492 8470add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8471{
976ca316
SM
8472 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8473 struct objfile *objfile = per_objfile->objfile;
08feed99 8474 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8475 CORE_ADDR addr = 0;
15d034d0 8476 const char *actual_name = NULL;
e142c38c
DJ
8477 CORE_ADDR baseaddr;
8478
b3b3bada 8479 baseaddr = objfile->text_section_offset ();
c906108c 8480
43816ebc
TT
8481 gdb::unique_xmalloc_ptr<char> built_actual_name
8482 = partial_die_full_name (pdi, cu);
15d034d0 8483 if (built_actual_name != NULL)
43816ebc 8484 actual_name = built_actual_name.get ();
63d06c5c 8485
72bf9492 8486 if (actual_name == NULL)
7d00ffec 8487 actual_name = pdi->name (cu);
72bf9492 8488
76e288d1
TT
8489 partial_symbol psymbol;
8490 memset (&psymbol, 0, sizeof (psymbol));
8491 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 8492 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
8493
8494 /* The code below indicates that the psymbol should be installed by
8495 setting this. */
8496 gdb::optional<psymbol_placement> where;
8497
c906108c
SS
8498 switch (pdi->tag)
8499 {
b1dc1806 8500 case DW_TAG_inlined_subroutine:
c906108c 8501 case DW_TAG_subprogram:
79748972
TT
8502 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8503 - baseaddr);
0a4b0913
AB
8504 if (pdi->is_external
8505 || cu->language == language_ada
8506 || (cu->language == language_fortran
8507 && pdi->die_parent != NULL
8508 && pdi->die_parent->tag == DW_TAG_subprogram))
8509 {
dda83cd7
SM
8510 /* Normally, only "external" DIEs are part of the global scope.
8511 But in Ada and Fortran, we want to be able to access nested
8512 procedures globally. So all Ada and Fortran subprograms are
8513 stored in the global scope. */
76e288d1 8514 where = psymbol_placement::GLOBAL;
c906108c
SS
8515 }
8516 else
76e288d1
TT
8517 where = psymbol_placement::STATIC;
8518
8519 psymbol.domain = VAR_DOMAIN;
8520 psymbol.aclass = LOC_BLOCK;
a52d653e 8521 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 8522 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8523
8524 if (pdi->main_subprogram && actual_name != NULL)
8525 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8526 break;
72929c62 8527 case DW_TAG_constant:
76e288d1
TT
8528 psymbol.domain = VAR_DOMAIN;
8529 psymbol.aclass = LOC_STATIC;
8530 where = (pdi->is_external
8531 ? psymbol_placement::GLOBAL
8532 : psymbol_placement::STATIC);
72929c62 8533 break;
c906108c 8534 case DW_TAG_variable:
95554aad
TT
8535 if (pdi->d.locdesc)
8536 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8537
95554aad 8538 if (pdi->d.locdesc
caac4577 8539 && addr == 0
976ca316 8540 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8541 {
8542 /* A global or static variable may also have been stripped
8543 out by the linker if unused, in which case its address
8544 will be nullified; do not add such variables into partial
8545 symbol table then. */
8546 }
8547 else if (pdi->is_external)
c906108c
SS
8548 {
8549 /* Global Variable.
8550 Don't enter into the minimal symbol tables as there is
8551 a minimal symbol table entry from the ELF symbols already.
8552 Enter into partial symbol table if it has a location
8553 descriptor or a type.
8554 If the location descriptor is missing, new_symbol will create
8555 a LOC_UNRESOLVED symbol, the address of the variable will then
8556 be determined from the minimal symbol table whenever the variable
8557 is referenced.
8558 The address for the partial symbol table entry is not
8559 used by GDB, but it comes in handy for debugging partial symbol
8560 table building. */
8561
95554aad 8562 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8563 {
8564 psymbol.domain = VAR_DOMAIN;
8565 psymbol.aclass = LOC_STATIC;
a52d653e 8566 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8567 psymbol.ginfo.value.address = addr;
8568 where = psymbol_placement::GLOBAL;
8569 }
c906108c
SS
8570 }
8571 else
8572 {
ff908ebf
AW
8573 int has_loc = pdi->d.locdesc != NULL;
8574
8575 /* Static Variable. Skip symbols whose value we cannot know (those
8576 without location descriptors or constant values). */
8577 if (!has_loc && !pdi->has_const_value)
43816ebc 8578 return;
ff908ebf 8579
76e288d1
TT
8580 psymbol.domain = VAR_DOMAIN;
8581 psymbol.aclass = LOC_STATIC;
a52d653e 8582 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8583 if (has_loc)
8584 psymbol.ginfo.value.address = addr;
8585 where = psymbol_placement::STATIC;
c906108c
SS
8586 }
8587 break;
d8f62e84 8588 case DW_TAG_array_type:
c906108c
SS
8589 case DW_TAG_typedef:
8590 case DW_TAG_base_type:
a02abb62 8591 case DW_TAG_subrange_type:
76e288d1
TT
8592 psymbol.domain = VAR_DOMAIN;
8593 psymbol.aclass = LOC_TYPEDEF;
8594 where = psymbol_placement::STATIC;
c906108c 8595 break;
74921315 8596 case DW_TAG_imported_declaration:
72bf9492 8597 case DW_TAG_namespace:
76e288d1
TT
8598 psymbol.domain = VAR_DOMAIN;
8599 psymbol.aclass = LOC_TYPEDEF;
8600 where = psymbol_placement::GLOBAL;
72bf9492 8601 break;
530e8392 8602 case DW_TAG_module:
a5fd13a9 8603 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8604 available without any name. If so, we skip the module as it
8605 doesn't bring any value. */
a5fd13a9 8606 if (actual_name != nullptr)
76e288d1
TT
8607 {
8608 psymbol.domain = MODULE_DOMAIN;
8609 psymbol.aclass = LOC_TYPEDEF;
8610 where = psymbol_placement::GLOBAL;
8611 }
530e8392 8612 break;
c906108c 8613 case DW_TAG_class_type:
680b30c7 8614 case DW_TAG_interface_type:
c906108c
SS
8615 case DW_TAG_structure_type:
8616 case DW_TAG_union_type:
8617 case DW_TAG_enumeration_type:
fa4028e9 8618 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8619 about "Structure, Union, and Class Type Entries": "An incomplete
8620 structure, union or class type is represented by a structure,
8621 union or class entry that does not have a byte size attribute
8622 and that has a DW_AT_declaration attribute." */
fa4028e9 8623 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8624 return;
fa4028e9 8625
63d06c5c
DC
8626 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8627 static vs. global. */
76e288d1
TT
8628 psymbol.domain = STRUCT_DOMAIN;
8629 psymbol.aclass = LOC_TYPEDEF;
8630 where = (cu->language == language_cplus
8631 ? psymbol_placement::GLOBAL
8632 : psymbol_placement::STATIC);
c906108c
SS
8633 break;
8634 case DW_TAG_enumerator:
76e288d1
TT
8635 psymbol.domain = VAR_DOMAIN;
8636 psymbol.aclass = LOC_CONST;
8637 where = (cu->language == language_cplus
8638 ? psymbol_placement::GLOBAL
8639 : psymbol_placement::STATIC);
c906108c
SS
8640 break;
8641 default:
8642 break;
8643 }
76e288d1
TT
8644
8645 if (where.has_value ())
8646 {
f049a313
TT
8647 if (built_actual_name != nullptr)
8648 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8649 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8650 psymbol.ginfo.set_linkage_name (actual_name);
8651 else
8652 {
8653 psymbol.ginfo.set_demangled_name (actual_name,
8654 &objfile->objfile_obstack);
8655 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8656 }
932539d7 8657 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
76e288d1 8658 }
c906108c
SS
8659}
8660
5c4e30ca
DC
8661/* Read a partial die corresponding to a namespace; also, add a symbol
8662 corresponding to that namespace to the symbol table. NAMESPACE is
8663 the name of the enclosing namespace. */
91c24f0a 8664
72bf9492
DJ
8665static void
8666add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8667 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8668 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8669{
72bf9492 8670 /* Add a symbol for the namespace. */
e7c27a73 8671
72bf9492 8672 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8673
8674 /* Now scan partial symbols in that namespace. */
8675
91c24f0a 8676 if (pdi->has_children)
cdc07690 8677 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8678}
8679
5d7cb8df
JK
8680/* Read a partial die corresponding to a Fortran module. */
8681
8682static void
8683add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8684 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8685{
530e8392
KB
8686 /* Add a symbol for the namespace. */
8687
8688 add_partial_symbol (pdi, cu);
8689
f55ee35c 8690 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8691
8692 if (pdi->has_children)
cdc07690 8693 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8694}
8695
b1dc1806
XR
8696/* Read a partial die corresponding to a subprogram or an inlined
8697 subprogram and create a partial symbol for that subprogram.
8698 When the CU language allows it, this routine also defines a partial
8699 symbol for each nested subprogram that this subprogram contains.
8700 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8701 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8702
cdc07690
YQ
8703 PDI may also be a lexical block, in which case we simply search
8704 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8705 Again, this is only performed when the CU language allows this
8706 type of definitions. */
8707
8708static void
8709add_partial_subprogram (struct partial_die_info *pdi,
8710 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8711 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8712{
b1dc1806 8713 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8714 {
8715 if (pdi->has_pc_info)
dda83cd7
SM
8716 {
8717 if (pdi->lowpc < *lowpc)
8718 *lowpc = pdi->lowpc;
8719 if (pdi->highpc > *highpc)
8720 *highpc = pdi->highpc;
cdc07690 8721 if (set_addrmap)
5734ee8b 8722 {
5e22e966 8723 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8724 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8725 CORE_ADDR baseaddr;
b926417a
TT
8726 CORE_ADDR this_highpc;
8727 CORE_ADDR this_lowpc;
5734ee8b 8728
b3b3bada 8729 baseaddr = objfile->text_section_offset ();
b926417a
TT
8730 this_lowpc
8731 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8732 pdi->lowpc + baseaddr)
8733 - baseaddr);
8734 this_highpc
8735 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8736 pdi->highpc + baseaddr)
8737 - baseaddr);
d320c2b5 8738 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8739 this_lowpc, this_highpc - 1,
9291a0cd 8740 cu->per_cu->v.psymtab);
5734ee8b 8741 }
dda83cd7 8742 }
481860b3
GB
8743
8744 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8745 {
dda83cd7 8746 if (!pdi->is_declaration)
e8d05480
JB
8747 /* Ignore subprogram DIEs that do not have a name, they are
8748 illegal. Do not emit a complaint at this point, we will
8749 do so when we convert this psymtab into a symtab. */
7d00ffec 8750 if (pdi->name (cu))
e8d05480 8751 add_partial_symbol (pdi, cu);
dda83cd7 8752 }
bc30ff58 8753 }
6e70227d 8754
bc30ff58
JB
8755 if (! pdi->has_children)
8756 return;
8757
0a4b0913 8758 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8759 {
8760 pdi = pdi->die_child;
8761 while (pdi != NULL)
8762 {
52356b79 8763 pdi->fixup (cu);
bc30ff58 8764 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8765 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8766 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8767 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8768 pdi = pdi->die_sibling;
8769 }
8770 }
8771}
8772
91c24f0a
DC
8773/* Read a partial die corresponding to an enumeration type. */
8774
72bf9492
DJ
8775static void
8776add_partial_enumeration (struct partial_die_info *enum_pdi,
8777 struct dwarf2_cu *cu)
91c24f0a 8778{
72bf9492 8779 struct partial_die_info *pdi;
91c24f0a 8780
7d00ffec 8781 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8782 add_partial_symbol (enum_pdi, cu);
8783
8784 pdi = enum_pdi->die_child;
8785 while (pdi)
91c24f0a 8786 {
7d00ffec 8787 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8788 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8789 else
72bf9492
DJ
8790 add_partial_symbol (pdi, cu);
8791 pdi = pdi->die_sibling;
91c24f0a 8792 }
91c24f0a
DC
8793}
8794
6caca83c
CC
8795/* Return the initial uleb128 in the die at INFO_PTR. */
8796
8797static unsigned int
d521ce57 8798peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8799{
8800 unsigned int bytes_read;
8801
8802 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8803}
8804
685af9cd
TT
8805/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8806 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8807
4bb7a0a7
DJ
8808 Return the corresponding abbrev, or NULL if the number is zero (indicating
8809 an empty DIE). In either case *BYTES_READ will be set to the length of
8810 the initial number. */
8811
7c32eebb 8812static const struct abbrev_info *
685af9cd
TT
8813peek_die_abbrev (const die_reader_specs &reader,
8814 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8815{
685af9cd 8816 dwarf2_cu *cu = reader.cu;
0280fdcc 8817 bfd *abfd = reader.abfd;
685af9cd
TT
8818 unsigned int abbrev_number
8819 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8820
8821 if (abbrev_number == 0)
8822 return NULL;
8823
7c32eebb
TT
8824 const abbrev_info *abbrev
8825 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8826 if (!abbrev)
8827 {
422b9917 8828 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8829 " at offset %s [in module %s]"),
422b9917 8830 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8831 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8832 }
8833
8834 return abbrev;
8835}
8836
93311388
DE
8837/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8838 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8839 DIE. Any children of the skipped DIEs will also be skipped. */
8840
d521ce57
TT
8841static const gdb_byte *
8842skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8843{
4bb7a0a7
DJ
8844 while (1)
8845 {
685af9cd 8846 unsigned int bytes_read;
7c32eebb
TT
8847 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8848 &bytes_read);
685af9cd 8849
4bb7a0a7
DJ
8850 if (abbrev == NULL)
8851 return info_ptr + bytes_read;
8852 else
dee91e82 8853 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8854 }
8855}
8856
93311388
DE
8857/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8858 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8859 abbrev corresponding to that skipped uleb128 should be passed in
8860 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8861 children. */
8862
d521ce57
TT
8863static const gdb_byte *
8864skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8865 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8866{
8867 unsigned int bytes_read;
8868 struct attribute attr;
dee91e82
DE
8869 bfd *abfd = reader->abfd;
8870 struct dwarf2_cu *cu = reader->cu;
d521ce57 8871 const gdb_byte *buffer = reader->buffer;
f664829e 8872 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8873 unsigned int form, i;
8874
8875 for (i = 0; i < abbrev->num_attrs; i++)
8876 {
8877 /* The only abbrev we care about is DW_AT_sibling. */
8878 if (abbrev->attrs[i].name == DW_AT_sibling)
8879 {
7a5f294d 8880 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8881 if (attr.form == DW_FORM_ref_addr)
b98664d3 8882 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8883 else
b9502d3f 8884 {
0826b30a 8885 sect_offset off = attr.get_ref_die_offset ();
9c541725 8886 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8887
8888 if (sibling_ptr < info_ptr)
b98664d3 8889 complaint (_("DW_AT_sibling points backwards"));
22869d73 8890 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8891 reader->die_section->overflow_complaint ();
b9502d3f
WN
8892 else
8893 return sibling_ptr;
8894 }
4bb7a0a7
DJ
8895 }
8896
8897 /* If it isn't DW_AT_sibling, skip this attribute. */
8898 form = abbrev->attrs[i].form;
8899 skip_attribute:
8900 switch (form)
8901 {
4bb7a0a7 8902 case DW_FORM_ref_addr:
ae411497
TT
8903 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8904 and later it is offset sized. */
8905 if (cu->header.version == 2)
8906 info_ptr += cu->header.addr_size;
8907 else
8908 info_ptr += cu->header.offset_size;
8909 break;
36586728
TT
8910 case DW_FORM_GNU_ref_alt:
8911 info_ptr += cu->header.offset_size;
8912 break;
ae411497 8913 case DW_FORM_addr:
4bb7a0a7
DJ
8914 info_ptr += cu->header.addr_size;
8915 break;
8916 case DW_FORM_data1:
8917 case DW_FORM_ref1:
8918 case DW_FORM_flag:
8fe0f950 8919 case DW_FORM_strx1:
4bb7a0a7
DJ
8920 info_ptr += 1;
8921 break;
2dc7f7b3 8922 case DW_FORM_flag_present:
43988095 8923 case DW_FORM_implicit_const:
2dc7f7b3 8924 break;
4bb7a0a7
DJ
8925 case DW_FORM_data2:
8926 case DW_FORM_ref2:
8fe0f950 8927 case DW_FORM_strx2:
4bb7a0a7
DJ
8928 info_ptr += 2;
8929 break;
8fe0f950
AT
8930 case DW_FORM_strx3:
8931 info_ptr += 3;
8932 break;
4bb7a0a7
DJ
8933 case DW_FORM_data4:
8934 case DW_FORM_ref4:
8fe0f950 8935 case DW_FORM_strx4:
4bb7a0a7
DJ
8936 info_ptr += 4;
8937 break;
8938 case DW_FORM_data8:
8939 case DW_FORM_ref8:
55f1336d 8940 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8941 info_ptr += 8;
8942 break;
0224619f
JK
8943 case DW_FORM_data16:
8944 info_ptr += 16;
8945 break;
4bb7a0a7 8946 case DW_FORM_string:
9b1c24c8 8947 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8948 info_ptr += bytes_read;
8949 break;
2dc7f7b3 8950 case DW_FORM_sec_offset:
4bb7a0a7 8951 case DW_FORM_strp:
36586728 8952 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8953 info_ptr += cu->header.offset_size;
8954 break;
2dc7f7b3 8955 case DW_FORM_exprloc:
4bb7a0a7
DJ
8956 case DW_FORM_block:
8957 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8958 info_ptr += bytes_read;
8959 break;
8960 case DW_FORM_block1:
8961 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8962 break;
8963 case DW_FORM_block2:
8964 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8965 break;
8966 case DW_FORM_block4:
8967 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8968 break;
336d760d 8969 case DW_FORM_addrx:
cf532bd1 8970 case DW_FORM_strx:
4bb7a0a7
DJ
8971 case DW_FORM_sdata:
8972 case DW_FORM_udata:
8973 case DW_FORM_ref_udata:
3019eac3
DE
8974 case DW_FORM_GNU_addr_index:
8975 case DW_FORM_GNU_str_index:
18a8505e 8976 case DW_FORM_rnglistx:
41144253 8977 case DW_FORM_loclistx:
d521ce57 8978 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8979 break;
8980 case DW_FORM_indirect:
8981 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8982 info_ptr += bytes_read;
8983 /* We need to continue parsing from here, so just go back to
8984 the top. */
8985 goto skip_attribute;
8986
8987 default:
3e43a32a
MS
8988 error (_("Dwarf Error: Cannot handle %s "
8989 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8990 dwarf_form_name (form),
8991 bfd_get_filename (abfd));
8992 }
8993 }
8994
8995 if (abbrev->has_children)
dee91e82 8996 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8997 else
8998 return info_ptr;
8999}
9000
93311388 9001/* Locate ORIG_PDI's sibling.
dee91e82 9002 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9003
d521ce57 9004static const gdb_byte *
dee91e82
DE
9005locate_pdi_sibling (const struct die_reader_specs *reader,
9006 struct partial_die_info *orig_pdi,
d521ce57 9007 const gdb_byte *info_ptr)
91c24f0a
DC
9008{
9009 /* Do we know the sibling already? */
72bf9492 9010
91c24f0a
DC
9011 if (orig_pdi->sibling)
9012 return orig_pdi->sibling;
9013
9014 /* Are there any children to deal with? */
9015
9016 if (!orig_pdi->has_children)
9017 return info_ptr;
9018
4bb7a0a7 9019 /* Skip the children the long way. */
91c24f0a 9020
dee91e82 9021 return skip_children (reader, info_ptr);
91c24f0a
DC
9022}
9023
257e7a09 9024/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9025 not NULL. */
c906108c 9026
891813be
TT
9027void
9028dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9029{
976ca316 9030 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 9031
976ca316 9032 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 9033
077cbab2
TT
9034 /* If this psymtab is constructed from a debug-only objfile, the
9035 has_section_at_zero flag will not necessarily be correct. We
9036 can get the correct value for this flag by looking at the data
9037 associated with the (presumably stripped) associated objfile. */
9038 if (objfile->separate_debug_objfile_backlink)
c906108c 9039 {
976ca316 9040 dwarf2_per_objfile *per_objfile_backlink
077cbab2 9041 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9042
976ca316
SM
9043 per_objfile->per_bfd->has_section_at_zero
9044 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 9045 }
98bfdba5 9046
8566b89b 9047 expand_psymtab (objfile);
95554aad 9048
976ca316 9049 process_cu_includes (per_objfile);
c906108c 9050}
9cdd5dbd
DE
9051\f
9052/* Reading in full CUs. */
c906108c 9053
10b3939b
DJ
9054/* Add PER_CU to the queue. */
9055
9056static void
120ce1b5
SM
9057queue_comp_unit (dwarf2_per_cu_data *per_cu,
9058 dwarf2_per_objfile *per_objfile,
95554aad 9059 enum language pretend_language)
10b3939b 9060{
10b3939b 9061 per_cu->queued = 1;
08ac5771
SM
9062
9063 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9064 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9065}
9066
616c069a
SM
9067/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9068
89e63ee4
DE
9069 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9070 dependency.
69d751e3 9071
616c069a
SM
9072 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9073 DIEs, false otherwise.
9074
9075 Explanation: there is an invariant that if a CU is queued for expansion
9076 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9077 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9078 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9079 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9080 invariant is respected.
9081
9082 The caller is therefore not required to load the CU's DIEs (we return false)
9083 if:
9084
9085 - the CU is already expanded, and therefore does not get enqueued
9086 - the CU gets enqueued for expansion, but its DIEs are already loaded
9087
9088 Note that the caller should not use this function's return value as an
9089 indicator of whether the CU's DIEs are loaded right now, it should check
9090 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
9091
9092static int
89e63ee4 9093maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9094 dwarf2_per_cu_data *per_cu,
9095 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9096 enum language pretend_language)
9097{
9098 /* We may arrive here during partial symbol reading, if we need full
9099 DIEs to process an unusual case (e.g. template arguments). Do
9100 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9101 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9102 {
7188ed02
SM
9103 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9104
9105 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9106 return 1;
9107 return 0;
9108 }
9109
9110 /* Mark the dependence relation so that we don't flush PER_CU
9111 too early. */
89e63ee4
DE
9112 if (dependent_cu != NULL)
9113 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9114
9115 /* If it's already on the queue, we have nothing to do. */
9116 if (per_cu->queued)
de53369b
SM
9117 {
9118 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9119 loaded. */
9120 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
9121
9122 /* If the CU is queued for expansion, it should not already be
9123 expanded. */
9124 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9125
9126 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
9127 return 0;
9128 }
0907af0c 9129
616c069a
SM
9130 bool queued = false;
9131 if (!per_objfile->symtab_set_p (per_cu))
9132 {
9133 /* Add it to the queue. */
9134 queue_comp_unit (per_cu, per_objfile, pretend_language);
9135 queued = true;
9136 }
9137
0907af0c
DE
9138 /* If the compilation unit is already loaded, just mark it as
9139 used. */
7188ed02
SM
9140 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9141 if (cu != nullptr)
616c069a 9142 cu->last_used = 0;
0907af0c 9143
616c069a
SM
9144 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9145 and the DIEs are not already loaded. */
9146 return queued && cu == nullptr;
0907af0c
DE
9147}
9148
10b3939b
DJ
9149/* Process the queue. */
9150
9151static void
976ca316 9152process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9153{
6f738b01
SM
9154 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9155 objfile_name (per_objfile->objfile));
45cfd468 9156
03dd20cc
DJ
9157 /* The queue starts out with one item, but following a DIE reference
9158 may load a new CU, adding it to the end of the queue. */
08ac5771 9159 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 9160 {
08ac5771 9161 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 9162 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9163
976ca316 9164 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9165 {
976ca316 9166 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9167
7188ed02
SM
9168 /* Skip dummy CUs. */
9169 if (cu != nullptr)
73be47f5 9170 {
7188ed02
SM
9171 unsigned int debug_print_threshold;
9172 char buf[100];
9173
9174 if (per_cu->is_debug_types)
9175 {
9176 struct signatured_type *sig_type =
9177 (struct signatured_type *) per_cu;
9178
9179 sprintf (buf, "TU %s at offset %s",
9180 hex_string (sig_type->signature),
9181 sect_offset_str (per_cu->sect_off));
9182 /* There can be 100s of TUs.
9183 Only print them in verbose mode. */
9184 debug_print_threshold = 2;
9185 }
9186 else
9187 {
9188 sprintf (buf, "CU at offset %s",
9189 sect_offset_str (per_cu->sect_off));
9190 debug_print_threshold = 1;
9191 }
247f5c4f 9192
7188ed02 9193 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9194 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 9195
7188ed02
SM
9196 if (per_cu->is_debug_types)
9197 process_full_type_unit (cu, item.pretend_language);
9198 else
9199 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9200
7188ed02 9201 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9202 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 9203 }
f4dc4d17 9204 }
10b3939b 9205
7188ed02 9206 per_cu->queued = 0;
08ac5771 9207 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
9208 }
9209
6f738b01
SM
9210 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9211 objfile_name (per_objfile->objfile));
10b3939b
DJ
9212}
9213
10b3939b
DJ
9214/* Read in full symbols for PST, and anything it depends on. */
9215
8566b89b
TT
9216void
9217dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9218{
af758d11 9219 gdb_assert (!readin_p (objfile));
95554aad 9220
17ee85fc
TT
9221 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9222 free_cached_comp_units freer (per_objfile);
48993951 9223 expand_dependencies (objfile);
aaa75496 9224
97a1449a 9225 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9226 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9227}
9228
af758d11
SM
9229/* See psympriv.h. */
9230
9231bool
9232dwarf2_psymtab::readin_p (struct objfile *objfile) const
9233{
9234 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9235 return per_objfile->symtab_set_p (per_cu_data);
9236}
9237
9238/* See psympriv.h. */
9239
9240compunit_symtab *
9241dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9242{
9243 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9244 return per_objfile->get_symtab (per_cu_data);
9245}
9246
dee91e82
DE
9247/* Trivial hash function for die_info: the hash value of a DIE
9248 is its offset in .debug_info for this objfile. */
10b3939b 9249
dee91e82
DE
9250static hashval_t
9251die_hash (const void *item)
10b3939b 9252{
9a3c8263 9253 const struct die_info *die = (const struct die_info *) item;
6502dd73 9254
9c541725 9255 return to_underlying (die->sect_off);
dee91e82 9256}
63d06c5c 9257
dee91e82
DE
9258/* Trivial comparison function for die_info structures: two DIEs
9259 are equal if they have the same offset. */
98bfdba5 9260
dee91e82
DE
9261static int
9262die_eq (const void *item_lhs, const void *item_rhs)
9263{
9a3c8263
SM
9264 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9265 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9266
9c541725 9267 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9268}
c906108c 9269
4a636814
SM
9270/* Load the DIEs associated with PER_CU into memory.
9271
9272 In some cases, the caller, while reading partial symbols, will need to load
9273 the full symbols for the CU for some reason. It will already have a
9274 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9275 rather than creating a new one. */
c906108c 9276
dee91e82 9277static void
ab432490
SM
9278load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9279 dwarf2_per_objfile *per_objfile,
4a636814 9280 dwarf2_cu *existing_cu,
c0ab21c2
TT
9281 bool skip_partial,
9282 enum language pretend_language)
dee91e82 9283{
c0ab21c2
TT
9284 gdb_assert (! this_cu->is_debug_types);
9285
7188ed02 9286 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9287 if (reader.dummy_p)
9288 return;
9289
9290 struct dwarf2_cu *cu = reader.cu;
9291 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9292
dee91e82
DE
9293 gdb_assert (cu->die_hash == NULL);
9294 cu->die_hash =
9295 htab_create_alloc_ex (cu->header.length / 12,
9296 die_hash,
9297 die_eq,
9298 NULL,
9299 &cu->comp_unit_obstack,
9300 hashtab_obstack_allocate,
9301 dummy_obstack_deallocate);
e142c38c 9302
3e225074 9303 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9304 reader.comp_unit_die->child
9305 = read_die_and_siblings (&reader, reader.info_ptr,
9306 &info_ptr, reader.comp_unit_die);
9307 cu->dies = reader.comp_unit_die;
dee91e82 9308 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9309
9310 /* We try not to read any attributes in this function, because not
9cdd5dbd 9311 all CUs needed for references have been loaded yet, and symbol
10b3939b 9312 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9313 or we won't be able to build types correctly.
9314 Similarly, if we do not read the producer, we can not apply
9315 producer-specific interpretation. */
c0ab21c2 9316 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9317
9318 reader.keep ();
10b3939b
DJ
9319}
9320
3da10d80
KS
9321/* Add a DIE to the delayed physname list. */
9322
9323static void
9324add_to_method_list (struct type *type, int fnfield_index, int index,
9325 const char *name, struct die_info *die,
9326 struct dwarf2_cu *cu)
9327{
9328 struct delayed_method_info mi;
9329 mi.type = type;
9330 mi.fnfield_index = fnfield_index;
9331 mi.index = index;
9332 mi.name = name;
9333 mi.die = die;
c89b44cd 9334 cu->method_list.push_back (mi);
3da10d80
KS
9335}
9336
3693fdb3
PA
9337/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9338 "const" / "volatile". If so, decrements LEN by the length of the
9339 modifier and return true. Otherwise return false. */
9340
9341template<size_t N>
9342static bool
9343check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9344{
9345 size_t mod_len = sizeof (mod) - 1;
9346 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9347 {
9348 len -= mod_len;
9349 return true;
9350 }
9351 return false;
9352}
9353
3da10d80
KS
9354/* Compute the physnames of any methods on the CU's method list.
9355
9356 The computation of method physnames is delayed in order to avoid the
9357 (bad) condition that one of the method's formal parameters is of an as yet
9358 incomplete type. */
9359
9360static void
9361compute_delayed_physnames (struct dwarf2_cu *cu)
9362{
3693fdb3 9363 /* Only C++ delays computing physnames. */
c89b44cd 9364 if (cu->method_list.empty ())
3693fdb3
PA
9365 return;
9366 gdb_assert (cu->language == language_cplus);
9367
52941706 9368 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9369 {
1d06ead6 9370 const char *physname;
3da10d80 9371 struct fn_fieldlist *fn_flp
c89b44cd
TT
9372 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9373 physname = dwarf2_physname (mi.name, mi.die, cu);
9374 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9375 = physname ? physname : "";
3693fdb3
PA
9376
9377 /* Since there's no tag to indicate whether a method is a
9378 const/volatile overload, extract that information out of the
9379 demangled name. */
9380 if (physname != NULL)
9381 {
9382 size_t len = strlen (physname);
9383
9384 while (1)
9385 {
9386 if (physname[len] == ')') /* shortcut */
9387 break;
9388 else if (check_modifier (physname, len, " const"))
c89b44cd 9389 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9390 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9391 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9392 else
9393 break;
9394 }
9395 }
3da10d80 9396 }
c89b44cd
TT
9397
9398 /* The list is no longer needed. */
9399 cu->method_list.clear ();
3da10d80
KS
9400}
9401
a766d390
DE
9402/* Go objects should be embedded in a DW_TAG_module DIE,
9403 and it's not clear if/how imported objects will appear.
9404 To keep Go support simple until that's worked out,
9405 go back through what we've read and create something usable.
9406 We could do this while processing each DIE, and feels kinda cleaner,
9407 but that way is more invasive.
9408 This is to, for example, allow the user to type "p var" or "b main"
9409 without having to specify the package name, and allow lookups
9410 of module.object to work in contexts that use the expression
9411 parser. */
9412
9413static void
9414fixup_go_packaging (struct dwarf2_cu *cu)
9415{
421d1616 9416 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9417 struct pending *list;
9418 int i;
9419
c24bdb02 9420 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9421 list != NULL;
9422 list = list->next)
a766d390
DE
9423 {
9424 for (i = 0; i < list->nsyms; ++i)
9425 {
9426 struct symbol *sym = list->symbol[i];
9427
c1b5c1eb 9428 if (sym->language () == language_go
a766d390
DE
9429 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9430 {
421d1616
TT
9431 gdb::unique_xmalloc_ptr<char> this_package_name
9432 (go_symbol_package_name (sym));
a766d390
DE
9433
9434 if (this_package_name == NULL)
9435 continue;
9436 if (package_name == NULL)
421d1616 9437 package_name = std::move (this_package_name);
a766d390
DE
9438 else
9439 {
5e22e966 9440 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9441 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9442 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9443 (symbol_symtab (sym) != NULL
9444 ? symtab_to_filename_for_display
9445 (symbol_symtab (sym))
e3b94546 9446 : objfile_name (objfile)),
421d1616 9447 this_package_name.get (), package_name.get ());
a766d390
DE
9448 }
9449 }
9450 }
9451 }
9452
9453 if (package_name != NULL)
9454 {
5e22e966 9455 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9456 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9457 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9458 saved_package_name);
a766d390
DE
9459 struct symbol *sym;
9460
8c14c3a3 9461 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9462 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9463 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9464 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9465 e.g., "main" finds the "main" module and not C's main(). */
9466 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9467 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9468 SYMBOL_TYPE (sym) = type;
9469
c24bdb02 9470 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9471 }
9472}
9473
c9317f21
TT
9474/* Allocate a fully-qualified name consisting of the two parts on the
9475 obstack. */
9476
9477static const char *
9478rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9479{
9480 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9481}
9482
9c6a1327
TT
9483/* A helper that allocates a variant part to attach to a Rust enum
9484 type. OBSTACK is where the results should be allocated. TYPE is
9485 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9486 discriminant. It must be the index of one of the fields of TYPE,
9487 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9488 DEFAULT_INDEX is the index of the default field; or -1 if there is
9489 no default. RANGES is indexed by "effective" field number (the
9490 field index, but omitting the discriminant and default fields) and
9491 must hold the discriminant values used by the variants. Note that
9492 RANGES must have a lifetime at least as long as OBSTACK -- either
9493 already allocated on it, or static. */
c9317f21 9494
9c6a1327
TT
9495static void
9496alloc_rust_variant (struct obstack *obstack, struct type *type,
9497 int discriminant_index, int default_index,
9498 gdb::array_view<discriminant_range> ranges)
9499{
57d02173
TT
9500 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9501 gdb_assert (discriminant_index == -1
9502 || (discriminant_index >= 0
9503 && discriminant_index < type->num_fields ()));
c9317f21 9504 gdb_assert (default_index == -1
1f704f76 9505 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9506
9c6a1327 9507 /* We have one variant for each non-discriminant field. */
57d02173
TT
9508 int n_variants = type->num_fields ();
9509 if (discriminant_index != -1)
9510 --n_variants;
c9317f21 9511
9c6a1327
TT
9512 variant *variants = new (obstack) variant[n_variants];
9513 int var_idx = 0;
9514 int range_idx = 0;
1f704f76 9515 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9516 {
9517 if (i == discriminant_index)
9518 continue;
c9317f21 9519
9c6a1327
TT
9520 variants[var_idx].first_field = i;
9521 variants[var_idx].last_field = i + 1;
9522
9523 /* The default field does not need a range, but other fields do.
9524 We skipped the discriminant above. */
9525 if (i != default_index)
9526 {
9527 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9528 ++range_idx;
9529 }
c9317f21 9530
9c6a1327
TT
9531 ++var_idx;
9532 }
9533
9534 gdb_assert (range_idx == ranges.size ());
9535 gdb_assert (var_idx == n_variants);
9536
9537 variant_part *part = new (obstack) variant_part;
9538 part->discriminant_index = discriminant_index;
57d02173
TT
9539 /* If there is no discriminant, then whether it is signed is of no
9540 consequence. */
9541 part->is_unsigned
9542 = (discriminant_index == -1
9543 ? false
c6d940a9 9544 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9545 part->variants = gdb::array_view<variant> (variants, n_variants);
9546
9547 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9548 gdb::array_view<variant_part> *prop_value
9549 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9550
9c6a1327 9551 struct dynamic_prop prop;
8c2e4e06 9552 prop.set_variant_parts (prop_value);
9c6a1327 9553
5c54719c 9554 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9555}
9556
9557/* Some versions of rustc emitted enums in an unusual way.
9558
9559 Ordinary enums were emitted as unions. The first element of each
9560 structure in the union was named "RUST$ENUM$DISR". This element
9561 held the discriminant.
9562
9563 These versions of Rust also implemented the "non-zero"
9564 optimization. When the enum had two values, and one is empty and
9565 the other holds a pointer that cannot be zero, the pointer is used
9566 as the discriminant, with a zero value meaning the empty variant.
9567 Here, the union's first member is of the form
9568 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9569 where the fieldnos are the indices of the fields that should be
9570 traversed in order to find the field (which may be several fields deep)
9571 and the variantname is the name of the variant of the case when the
9572 field is zero.
9573
9574 This function recognizes whether TYPE is of one of these forms,
9575 and, if so, smashes it to be a variant type. */
9576
9577static void
9578quirk_rust_enum (struct type *type, struct objfile *objfile)
9579{
78134374 9580 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9581
9582 /* We don't need to deal with empty enums. */
1f704f76 9583 if (type->num_fields () == 0)
c9317f21
TT
9584 return;
9585
9586#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9587 if (type->num_fields () == 1
c9317f21
TT
9588 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9589 {
9590 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9591
9592 /* Decode the field name to find the offset of the
9593 discriminant. */
9594 ULONGEST bit_offset = 0;
940da03e 9595 struct type *field_type = type->field (0).type ();
c9317f21
TT
9596 while (name[0] >= '0' && name[0] <= '9')
9597 {
9598 char *tail;
9599 unsigned long index = strtoul (name, &tail, 10);
9600 name = tail;
9601 if (*name != '$'
1f704f76 9602 || index >= field_type->num_fields ()
c9317f21
TT
9603 || (TYPE_FIELD_LOC_KIND (field_type, index)
9604 != FIELD_LOC_KIND_BITPOS))
9605 {
b98664d3 9606 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9607 "[in module %s]"),
9608 TYPE_FIELD_NAME (type, 0),
9609 objfile_name (objfile));
9610 return;
9611 }
9612 ++name;
9613
9614 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9615 field_type = field_type->field (index).type ();
c9317f21
TT
9616 }
9617
9c6a1327
TT
9618 /* Smash this type to be a structure type. We have to do this
9619 because the type has already been recorded. */
67607e24 9620 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9621 type->set_num_fields (3);
9c6a1327 9622 /* Save the field we care about. */
ceacbf6e 9623 struct field saved_field = type->field (0);
3cabb6b0
SM
9624 type->set_fields
9625 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9626
9c6a1327 9627 /* Put the discriminant at index 0. */
5d14b6e5 9628 type->field (0).set_type (field_type);
9c6a1327
TT
9629 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9630 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9631 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9632
9633 /* The order of fields doesn't really matter, so put the real
9634 field at index 1 and the data-less field at index 2. */
ceacbf6e 9635 type->field (1) = saved_field;
9c6a1327 9636 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9637 = rust_last_path_segment (type->field (1).type ()->name ());
9638 type->field (1).type ()->set_name
7d93a1e0 9639 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9640 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9641
9642 const char *dataless_name
7d93a1e0 9643 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9644 name);
9645 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9646 dataless_name);
5d14b6e5 9647 type->field (2).set_type (dataless_type);
c9317f21
TT
9648 /* NAME points into the original discriminant name, which
9649 already has the correct lifetime. */
9c6a1327 9650 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9651 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9652
9c6a1327
TT
9653 /* Indicate that this is a variant type. */
9654 static discriminant_range ranges[1] = { { 0, 0 } };
9655 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9656 }
77c2dba3
TT
9657 /* A union with a single anonymous field is probably an old-style
9658 univariant enum. */
1f704f76 9659 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9660 {
c9317f21
TT
9661 /* Smash this type to be a structure type. We have to do this
9662 because the type has already been recorded. */
67607e24 9663 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9664
940da03e 9665 struct type *field_type = type->field (0).type ();
c9317f21 9666 const char *variant_name
7d93a1e0 9667 = rust_last_path_segment (field_type->name ());
9c6a1327 9668 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9669 field_type->set_name
9670 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9671 type->name (), variant_name));
57d02173
TT
9672
9673 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9674 }
9675 else
9676 {
9677 struct type *disr_type = nullptr;
1f704f76 9678 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9679 {
940da03e 9680 disr_type = type->field (i).type ();
c9317f21 9681
78134374 9682 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9683 {
9684 /* All fields of a true enum will be structs. */
9685 return;
9686 }
1f704f76 9687 else if (disr_type->num_fields () == 0)
c9317f21
TT
9688 {
9689 /* Could be data-less variant, so keep going. */
a037790e 9690 disr_type = nullptr;
c9317f21
TT
9691 }
9692 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9693 "RUST$ENUM$DISR") != 0)
9694 {
9695 /* Not a Rust enum. */
9696 return;
9697 }
9698 else
9699 {
9700 /* Found one. */
9701 break;
9702 }
9703 }
9704
9705 /* If we got here without a discriminant, then it's probably
9706 just a union. */
9707 if (disr_type == nullptr)
9708 return;
9709
9710 /* Smash this type to be a structure type. We have to do this
9711 because the type has already been recorded. */
67607e24 9712 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9713
9c6a1327 9714 /* Make space for the discriminant field. */
ceacbf6e 9715 struct field *disr_field = &disr_type->field (0);
9c6a1327 9716 field *new_fields
1f704f76 9717 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9718 * sizeof (struct field)));
80fc5e77 9719 memcpy (new_fields + 1, type->fields (),
1f704f76 9720 type->num_fields () * sizeof (struct field));
3cabb6b0 9721 type->set_fields (new_fields);
1f704f76 9722 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9723
9724 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9725 type->field (0) = *disr_field;
9c6a1327
TT
9726 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9727 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9728
9729 /* We need a way to find the correct discriminant given a
9730 variant name. For convenience we build a map here. */
b6cdac4b 9731 struct type *enum_type = disr_field->type ();
c9317f21 9732 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9733 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9734 {
9735 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9736 {
9737 const char *name
9738 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9739 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9740 }
9741 }
9742
1f704f76 9743 int n_fields = type->num_fields ();
9c6a1327
TT
9744 /* We don't need a range entry for the discriminant, but we do
9745 need one for every other field, as there is no default
9746 variant. */
9747 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9748 discriminant_range,
9749 n_fields - 1);
c9317f21
TT
9750 /* Skip the discriminant here. */
9751 for (int i = 1; i < n_fields; ++i)
9752 {
9753 /* Find the final word in the name of this variant's type.
9754 That name can be used to look up the correct
9755 discriminant. */
9756 const char *variant_name
940da03e 9757 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9758
9759 auto iter = discriminant_map.find (variant_name);
9760 if (iter != discriminant_map.end ())
9c6a1327 9761 {
57d02173
TT
9762 ranges[i - 1].low = iter->second;
9763 ranges[i - 1].high = iter->second;
9c6a1327 9764 }
c9317f21 9765
57d02173
TT
9766 /* In Rust, each element should have the size of the
9767 enclosing enum. */
9768 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9769
bedda9ac 9770 /* Remove the discriminant field, if it exists. */
940da03e 9771 struct type *sub_type = type->field (i).type ();
1f704f76 9772 if (sub_type->num_fields () > 0)
bedda9ac 9773 {
5e33d5f4 9774 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9775 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9776 }
9c6a1327 9777 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9778 sub_type->set_name
9779 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9780 type->name (), variant_name));
c9317f21 9781 }
9c6a1327
TT
9782
9783 /* Indicate that this is a variant type. */
a1520ad8 9784 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9785 gdb::array_view<discriminant_range> (ranges,
9786 n_fields - 1));
c9317f21
TT
9787 }
9788}
9789
9790/* Rewrite some Rust unions to be structures with variants parts. */
9791
9792static void
9793rust_union_quirks (struct dwarf2_cu *cu)
9794{
9795 gdb_assert (cu->language == language_rust);
52941706 9796 for (type *type_ : cu->rust_unions)
5e22e966 9797 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9798 /* We don't need this any more. */
9799 cu->rust_unions.clear ();
c9317f21
TT
9800}
9801
8adb8487
TT
9802/* See read.h. */
9803
9804type_unit_group_unshareable *
9805dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9806{
9807 auto iter = this->m_type_units.find (tu_group);
9808 if (iter != this->m_type_units.end ())
9809 return iter->second.get ();
9810
9811 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9812 type_unit_group_unshareable *result = uniq.get ();
9813 this->m_type_units[tu_group] = std::move (uniq);
9814 return result;
9815}
9816
e286671b
TT
9817struct type *
9818dwarf2_per_objfile::get_type_for_signatured_type
9819 (signatured_type *sig_type) const
9820{
9821 auto iter = this->m_type_map.find (sig_type);
9822 if (iter == this->m_type_map.end ())
9823 return nullptr;
9824
9825 return iter->second;
9826}
9827
9828void dwarf2_per_objfile::set_type_for_signatured_type
9829 (signatured_type *sig_type, struct type *type)
9830{
9831 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9832
9833 this->m_type_map[sig_type] = type;
9834}
9835
95554aad
TT
9836/* A helper function for computing the list of all symbol tables
9837 included by PER_CU. */
9838
9839static void
4c39bc03 9840recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9841 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9842 dwarf2_per_cu_data *per_cu,
9843 dwarf2_per_objfile *per_objfile,
43f3e411 9844 struct compunit_symtab *immediate_parent)
95554aad 9845{
af758d11 9846 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9847 if (*slot != NULL)
9848 {
9849 /* This inclusion and its children have been processed. */
9850 return;
9851 }
9852
9853 *slot = per_cu;
af758d11 9854
95554aad 9855 /* Only add a CU if it has a symbol table. */
43182c09 9856 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9857 if (cust != NULL)
ec94af83
DE
9858 {
9859 /* If this is a type unit only add its symbol table if we haven't
9860 seen it yet (type unit per_cu's can share symtabs). */
9861 if (per_cu->is_debug_types)
9862 {
43f3e411 9863 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9864 if (*slot == NULL)
9865 {
43f3e411 9866 *slot = cust;
4c39bc03 9867 result->push_back (cust);
43f3e411
DE
9868 if (cust->user == NULL)
9869 cust->user = immediate_parent;
ec94af83
DE
9870 }
9871 }
9872 else
f9125b6c 9873 {
4c39bc03 9874 result->push_back (cust);
43f3e411
DE
9875 if (cust->user == NULL)
9876 cust->user = immediate_parent;
f9125b6c 9877 }
ec94af83 9878 }
95554aad 9879
ae640021
AB
9880 if (!per_cu->imported_symtabs_empty ())
9881 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9882 {
9883 recursively_compute_inclusions (result, all_children,
43182c09
SM
9884 all_type_symtabs, ptr, per_objfile,
9885 cust);
ae640021 9886 }
95554aad
TT
9887}
9888
43f3e411 9889/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9890 PER_CU. */
9891
9892static void
43182c09
SM
9893compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9894 dwarf2_per_objfile *per_objfile)
95554aad 9895{
f4dc4d17
DE
9896 gdb_assert (! per_cu->is_debug_types);
9897
ae640021 9898 if (!per_cu->imported_symtabs_empty ())
95554aad 9899 {
ae640021 9900 int len;
4c39bc03 9901 std::vector<compunit_symtab *> result_symtabs;
43182c09 9902 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9903
9904 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9905 if (cust == NULL)
95554aad
TT
9906 return;
9907
280a9412
TT
9908 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9909 htab_eq_pointer,
9910 NULL, xcalloc, xfree));
9911 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9912 htab_eq_pointer,
9913 NULL, xcalloc, xfree));
95554aad 9914
ae640021 9915 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9916 {
280a9412
TT
9917 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9918 all_type_symtabs.get (), ptr,
9919 per_objfile, cust);
ec94af83 9920 }
95554aad 9921
ec94af83 9922 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9923 len = result_symtabs.size ();
43f3e411 9924 cust->includes
f6e649dd 9925 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9926 struct compunit_symtab *, len + 1);
4c39bc03
TT
9927 memcpy (cust->includes, result_symtabs.data (),
9928 len * sizeof (compunit_symtab *));
43f3e411 9929 cust->includes[len] = NULL;
95554aad
TT
9930 }
9931}
9932
9933/* Compute the 'includes' field for the symtabs of all the CUs we just
9934 read. */
9935
9936static void
976ca316 9937process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9938{
976ca316 9939 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9940 {
9941 if (! iter->is_debug_types)
976ca316 9942 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9943 }
95554aad 9944
976ca316 9945 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9946}
9947
8fc0b21d 9948/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9949 already been loaded into memory. */
9950
9951static void
8fc0b21d 9952process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9953{
976ca316
SM
9954 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9955 struct objfile *objfile = per_objfile->objfile;
08feed99 9956 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9957 CORE_ADDR lowpc, highpc;
43f3e411 9958 struct compunit_symtab *cust;
10b3939b 9959 CORE_ADDR baseaddr;
4359dff1 9960 struct block *static_block;
3e29f34a 9961 CORE_ADDR addr;
10b3939b 9962
b3b3bada 9963 baseaddr = objfile->text_section_offset ();
10b3939b 9964
c89b44cd
TT
9965 /* Clear the list here in case something was left over. */
9966 cu->method_list.clear ();
10b3939b 9967
95554aad
TT
9968 cu->language = pretend_language;
9969 cu->language_defn = language_def (cu->language);
9970
1c47ec3e
TV
9971 dwarf2_find_base_address (cu->dies, cu);
9972
c906108c 9973 /* Do line number decoding in read_file_scope () */
10b3939b 9974 process_die (cu->dies, cu);
c906108c 9975
a766d390
DE
9976 /* For now fudge the Go package. */
9977 if (cu->language == language_go)
9978 fixup_go_packaging (cu);
9979
5f48f8f3 9980 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9981 should be complete, and it should now be safe to compute all of the
9982 physnames. */
9983 compute_delayed_physnames (cu);
3da10d80 9984
c9317f21
TT
9985 if (cu->language == language_rust)
9986 rust_union_quirks (cu);
9987
fae299cd
DC
9988 /* Some compilers don't define a DW_AT_high_pc attribute for the
9989 compilation unit. If the DW_AT_high_pc is missing, synthesize
9990 it, by scanning the DIE's below the compilation unit. */
10b3939b 9991 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9992
3e29f34a 9993 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9994 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9995
9996 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9997 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9998 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9999 addrmap to help ensure it has an accurate map of pc values belonging to
10000 this comp unit. */
10001 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10002
c24bdb02 10003 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10004 SECT_OFF_TEXT (objfile),
10005 0);
c906108c 10006
43f3e411 10007 if (cust != NULL)
c906108c 10008 {
df15bd07 10009 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10010
8be455d7
JK
10011 /* Set symtab language to language from DW_AT_language. If the
10012 compilation is from a C file generated by language preprocessors, do
10013 not set the language if it was already deduced by start_subfile. */
43f3e411 10014 if (!(cu->language == language_c
40e3ad0e 10015 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10016 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10017
10018 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10019 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10020 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10021 there were bugs in prologue debug info, fixed later in GCC-4.5
10022 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10023
10024 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10025 needed, it would be wrong due to missing DW_AT_producer there.
10026
10027 Still one can confuse GDB by using non-standard GCC compilation
10028 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10029 */
ab260dad 10030 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10031 cust->locations_valid = 1;
e0d00bc7
JK
10032
10033 if (gcc_4_minor >= 5)
43f3e411 10034 cust->epilogue_unwind_valid = 1;
96408a79 10035
43f3e411 10036 cust->call_site_htab = cu->call_site_htab;
c906108c 10037 }
9291a0cd 10038
976ca316 10039 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 10040
95554aad 10041 /* Push it for inclusion processing later. */
976ca316 10042 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
10043
10044 /* Not needed any more. */
c24bdb02 10045 cu->reset_builder ();
f4dc4d17 10046}
45cfd468 10047
8fc0b21d 10048/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
10049 already been loaded into memory. */
10050
10051static void
8fc0b21d 10052process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
10053 enum language pretend_language)
10054{
976ca316
SM
10055 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10056 struct objfile *objfile = per_objfile->objfile;
43f3e411 10057 struct compunit_symtab *cust;
0186c6a7
DE
10058 struct signatured_type *sig_type;
10059
8fc0b21d
SM
10060 gdb_assert (cu->per_cu->is_debug_types);
10061 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 10062
c89b44cd
TT
10063 /* Clear the list here in case something was left over. */
10064 cu->method_list.clear ();
f4dc4d17 10065
f4dc4d17
DE
10066 cu->language = pretend_language;
10067 cu->language_defn = language_def (cu->language);
10068
10069 /* The symbol tables are set up in read_type_unit_scope. */
10070 process_die (cu->dies, cu);
10071
10072 /* For now fudge the Go package. */
10073 if (cu->language == language_go)
10074 fixup_go_packaging (cu);
10075
5f48f8f3 10076 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10077 should be complete, and it should now be safe to compute all of the
10078 physnames. */
10079 compute_delayed_physnames (cu);
f4dc4d17 10080
c9317f21
TT
10081 if (cu->language == language_rust)
10082 rust_union_quirks (cu);
10083
f4dc4d17
DE
10084 /* TUs share symbol tables.
10085 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10086 of it with end_expandable_symtab. Otherwise, complete the addition of
10087 this TU's symbols to the existing symtab. */
8adb8487 10088 type_unit_group_unshareable *tug_unshare =
976ca316 10089 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 10090 if (tug_unshare->compunit_symtab == NULL)
45cfd468 10091 {
c24bdb02
KS
10092 buildsym_compunit *builder = cu->get_builder ();
10093 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 10094 tug_unshare->compunit_symtab = cust;
f4dc4d17 10095
43f3e411 10096 if (cust != NULL)
f4dc4d17
DE
10097 {
10098 /* Set symtab language to language from DW_AT_language. If the
10099 compilation is from a C file generated by language preprocessors,
10100 do not set the language if it was already deduced by
10101 start_subfile. */
43f3e411
DE
10102 if (!(cu->language == language_c
10103 && COMPUNIT_FILETABS (cust)->language != language_c))
10104 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10105 }
10106 }
10107 else
10108 {
c24bdb02 10109 cu->get_builder ()->augment_type_symtab ();
8adb8487 10110 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10111 }
10112
976ca316 10113 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10114
10115 /* Not needed any more. */
c24bdb02 10116 cu->reset_builder ();
c906108c
SS
10117}
10118
95554aad
TT
10119/* Process an imported unit DIE. */
10120
10121static void
10122process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10123{
10124 struct attribute *attr;
10125
f4dc4d17
DE
10126 /* For now we don't handle imported units in type units. */
10127 if (cu->per_cu->is_debug_types)
10128 {
10129 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10130 " supported in type units [in module %s]"),
5e22e966 10131 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10132 }
10133
95554aad
TT
10134 attr = dwarf2_attr (die, DW_AT_import, cu);
10135 if (attr != NULL)
10136 {
0826b30a 10137 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10138 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10139 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10140 dwarf2_per_cu_data *per_cu
ab432490 10141 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10142
58990295
TV
10143 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10144 into another compilation unit, at root level. Regard this as a hint,
10145 and ignore it. */
10146 if (die->parent && die->parent->parent == NULL
10147 && per_cu->unit_type == DW_UT_compile
10148 && per_cu->lang == language_cplus)
10149 return;
10150
69d751e3 10151 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10152 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
10153 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10154 false, cu->language);
95554aad 10155
ae640021 10156 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10157 }
10158}
10159
4c8aa72d
PA
10160/* RAII object that represents a process_die scope: i.e.,
10161 starts/finishes processing a DIE. */
10162class process_die_scope
adde2bff 10163{
4c8aa72d
PA
10164public:
10165 process_die_scope (die_info *die, dwarf2_cu *cu)
10166 : m_die (die), m_cu (cu)
10167 {
10168 /* We should only be processing DIEs not already in process. */
10169 gdb_assert (!m_die->in_process);
10170 m_die->in_process = true;
10171 }
8c3cb9fa 10172
4c8aa72d
PA
10173 ~process_die_scope ()
10174 {
10175 m_die->in_process = false;
10176
10177 /* If we're done processing the DIE for the CU that owns the line
10178 header, we don't need the line header anymore. */
10179 if (m_cu->line_header_die_owner == m_die)
10180 {
10181 delete m_cu->line_header;
10182 m_cu->line_header = NULL;
10183 m_cu->line_header_die_owner = NULL;
10184 }
10185 }
10186
10187private:
10188 die_info *m_die;
10189 dwarf2_cu *m_cu;
10190};
adde2bff 10191
c906108c
SS
10192/* Process a die and its children. */
10193
10194static void
e7c27a73 10195process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10196{
4c8aa72d 10197 process_die_scope scope (die, cu);
adde2bff 10198
c906108c
SS
10199 switch (die->tag)
10200 {
10201 case DW_TAG_padding:
10202 break;
10203 case DW_TAG_compile_unit:
95554aad 10204 case DW_TAG_partial_unit:
e7c27a73 10205 read_file_scope (die, cu);
c906108c 10206 break;
348e048f
DE
10207 case DW_TAG_type_unit:
10208 read_type_unit_scope (die, cu);
10209 break;
c906108c 10210 case DW_TAG_subprogram:
0a4b0913
AB
10211 /* Nested subprograms in Fortran get a prefix. */
10212 if (cu->language == language_fortran
10213 && die->parent != NULL
10214 && die->parent->tag == DW_TAG_subprogram)
10215 cu->processing_has_namespace_info = true;
10216 /* Fall through. */
c906108c 10217 case DW_TAG_inlined_subroutine:
edb3359d 10218 read_func_scope (die, cu);
c906108c
SS
10219 break;
10220 case DW_TAG_lexical_block:
14898363
L
10221 case DW_TAG_try_block:
10222 case DW_TAG_catch_block:
e7c27a73 10223 read_lexical_block_scope (die, cu);
c906108c 10224 break;
216f72a1 10225 case DW_TAG_call_site:
96408a79
SA
10226 case DW_TAG_GNU_call_site:
10227 read_call_site_scope (die, cu);
10228 break;
c906108c 10229 case DW_TAG_class_type:
680b30c7 10230 case DW_TAG_interface_type:
c906108c
SS
10231 case DW_TAG_structure_type:
10232 case DW_TAG_union_type:
134d01f1 10233 process_structure_scope (die, cu);
c906108c
SS
10234 break;
10235 case DW_TAG_enumeration_type:
134d01f1 10236 process_enumeration_scope (die, cu);
c906108c 10237 break;
134d01f1 10238
f792889a
DJ
10239 /* These dies have a type, but processing them does not create
10240 a symbol or recurse to process the children. Therefore we can
10241 read them on-demand through read_type_die. */
c906108c 10242 case DW_TAG_subroutine_type:
72019c9c 10243 case DW_TAG_set_type:
c906108c 10244 case DW_TAG_pointer_type:
c906108c 10245 case DW_TAG_ptr_to_member_type:
c906108c 10246 case DW_TAG_reference_type:
4297a3f0 10247 case DW_TAG_rvalue_reference_type:
c906108c 10248 case DW_TAG_string_type:
c906108c 10249 break;
134d01f1 10250
d8f62e84
TT
10251 case DW_TAG_array_type:
10252 /* We only need to handle this case for Ada -- in other
10253 languages, it's normal for the compiler to emit a typedef
10254 instead. */
10255 if (cu->language != language_ada)
10256 break;
10257 /* FALLTHROUGH */
c906108c 10258 case DW_TAG_base_type:
a02abb62 10259 case DW_TAG_subrange_type:
cb249c71 10260 case DW_TAG_typedef:
134d01f1 10261 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 10262 DW_AT_name. */
f792889a 10263 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10264 break;
c906108c 10265 case DW_TAG_common_block:
e7c27a73 10266 read_common_block (die, cu);
c906108c
SS
10267 break;
10268 case DW_TAG_common_inclusion:
10269 break;
d9fa45fe 10270 case DW_TAG_namespace:
9068261f 10271 cu->processing_has_namespace_info = true;
e7c27a73 10272 read_namespace (die, cu);
d9fa45fe 10273 break;
5d7cb8df 10274 case DW_TAG_module:
9068261f 10275 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10276 read_module (die, cu);
10277 break;
d9fa45fe 10278 case DW_TAG_imported_declaration:
9068261f 10279 cu->processing_has_namespace_info = true;
74921315
KS
10280 if (read_namespace_alias (die, cu))
10281 break;
86a73007
TT
10282 /* The declaration is not a global namespace alias. */
10283 /* Fall through. */
d9fa45fe 10284 case DW_TAG_imported_module:
9068261f 10285 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10286 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10287 || cu->language != language_fortran))
b98664d3 10288 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10289 dwarf_tag_name (die->tag));
10290 read_import_statement (die, cu);
d9fa45fe 10291 break;
95554aad
TT
10292
10293 case DW_TAG_imported_unit:
10294 process_imported_unit_die (die, cu);
10295 break;
10296
71a3c369
TT
10297 case DW_TAG_variable:
10298 read_variable (die, cu);
10299 break;
10300
c906108c 10301 default:
e7c27a73 10302 new_symbol (die, NULL, cu);
c906108c
SS
10303 break;
10304 }
10305}
ca69b9e6
DE
10306\f
10307/* DWARF name computation. */
c906108c 10308
94af9270
KS
10309/* A helper function for dwarf2_compute_name which determines whether DIE
10310 needs to have the name of the scope prepended to the name listed in the
10311 die. */
10312
10313static int
10314die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10315{
1c809c68
TT
10316 struct attribute *attr;
10317
94af9270
KS
10318 switch (die->tag)
10319 {
10320 case DW_TAG_namespace:
10321 case DW_TAG_typedef:
10322 case DW_TAG_class_type:
10323 case DW_TAG_interface_type:
10324 case DW_TAG_structure_type:
10325 case DW_TAG_union_type:
10326 case DW_TAG_enumeration_type:
10327 case DW_TAG_enumerator:
10328 case DW_TAG_subprogram:
08a76f8a 10329 case DW_TAG_inlined_subroutine:
94af9270 10330 case DW_TAG_member:
74921315 10331 case DW_TAG_imported_declaration:
94af9270
KS
10332 return 1;
10333
10334 case DW_TAG_variable:
c2b0a229 10335 case DW_TAG_constant:
94af9270
KS
10336 /* We only need to prefix "globally" visible variables. These include
10337 any variable marked with DW_AT_external or any variable that
10338 lives in a namespace. [Variables in anonymous namespaces
10339 require prefixing, but they are not DW_AT_external.] */
10340
10341 if (dwarf2_attr (die, DW_AT_specification, cu))
10342 {
10343 struct dwarf2_cu *spec_cu = cu;
9a619af0 10344
94af9270
KS
10345 return die_needs_namespace (die_specification (die, &spec_cu),
10346 spec_cu);
10347 }
10348
1c809c68 10349 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10350 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10351 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10352 return 0;
10353 /* A variable in a lexical block of some kind does not need a
10354 namespace, even though in C++ such variables may be external
10355 and have a mangled name. */
10356 if (die->parent->tag == DW_TAG_lexical_block
10357 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10358 || die->parent->tag == DW_TAG_catch_block
10359 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10360 return 0;
10361 return 1;
94af9270
KS
10362
10363 default:
10364 return 0;
10365 }
10366}
10367
73b9be8b
KS
10368/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10369 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10370 defined for the given DIE. */
10371
10372static struct attribute *
10373dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10374{
10375 struct attribute *attr;
10376
10377 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10378 if (attr == NULL)
10379 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10380
10381 return attr;
10382}
10383
10384/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10385 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10386 defined for the given DIE. */
10387
10388static const char *
10389dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10390{
10391 const char *linkage_name;
10392
10393 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10394 if (linkage_name == NULL)
10395 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10396
787de330
TT
10397 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10398 See https://github.com/rust-lang/rust/issues/32925. */
10399 if (cu->language == language_rust && linkage_name != NULL
10400 && strchr (linkage_name, '{') != NULL)
10401 linkage_name = NULL;
10402
73b9be8b
KS
10403 return linkage_name;
10404}
10405
94af9270 10406/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10407 compute the physname for the object, which include a method's:
9c37b5ae 10408 - formal parameters (C++),
a766d390 10409 - receiver type (Go),
a766d390
DE
10410
10411 The term "physname" is a bit confusing.
10412 For C++, for example, it is the demangled name.
10413 For Go, for example, it's the mangled name.
94af9270 10414
af6b7be1
JB
10415 For Ada, return the DIE's linkage name rather than the fully qualified
10416 name. PHYSNAME is ignored..
10417
5989a64e 10418 The result is allocated on the objfile->per_bfd's obstack and
45940949 10419 canonicalized. */
94af9270
KS
10420
10421static const char *
15d034d0
TT
10422dwarf2_compute_name (const char *name,
10423 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10424 int physname)
10425{
5e22e966 10426 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10427
94af9270
KS
10428 if (name == NULL)
10429 name = dwarf2_name (die, cu);
10430
2ee7123e
DE
10431 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10432 but otherwise compute it by typename_concat inside GDB.
10433 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10434 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10435 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10436 will set the demangled name to the result of dwarf2_full_name, and it is
10437 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10438 if (cu->language == language_ada
10439 || (cu->language == language_fortran && physname))
10440 {
10441 /* For Ada unit, we prefer the linkage name over the name, as
10442 the former contains the exported name, which the user expects
10443 to be able to reference. Ideally, we want the user to be able
10444 to reference this entity using either natural or linkage name,
10445 but we haven't started looking at this enhancement yet. */
73b9be8b 10446 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10447
2ee7123e
DE
10448 if (linkage_name != NULL)
10449 return linkage_name;
f55ee35c
JK
10450 }
10451
94af9270
KS
10452 /* These are the only languages we know how to qualify names in. */
10453 if (name != NULL
9c37b5ae 10454 && (cu->language == language_cplus
c44af4eb
TT
10455 || cu->language == language_fortran || cu->language == language_d
10456 || cu->language == language_rust))
94af9270
KS
10457 {
10458 if (die_needs_namespace (die, cu))
10459 {
0d5cff50 10460 const char *prefix;
34a68019 10461 const char *canonical_name = NULL;
94af9270 10462
d7e74731
PA
10463 string_file buf;
10464
94af9270 10465 prefix = determine_prefix (die, cu);
94af9270
KS
10466 if (*prefix != '\0')
10467 {
43816ebc
TT
10468 gdb::unique_xmalloc_ptr<char> prefixed_name
10469 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10470
43816ebc 10471 buf.puts (prefixed_name.get ());
94af9270
KS
10472 }
10473 else
d7e74731 10474 buf.puts (name);
94af9270 10475
98bfdba5
PA
10476 /* Template parameters may be specified in the DIE's DW_AT_name, or
10477 as children with DW_TAG_template_type_param or
10478 DW_TAG_value_type_param. If the latter, add them to the name
10479 here. If the name already has template parameters, then
10480 skip this step; some versions of GCC emit both, and
10481 it is more efficient to use the pre-computed name.
10482
10483 Something to keep in mind about this process: it is very
10484 unlikely, or in some cases downright impossible, to produce
10485 something that will match the mangled name of a function.
10486 If the definition of the function has the same debug info,
10487 we should be able to match up with it anyway. But fallbacks
10488 using the minimal symbol, for instance to find a method
10489 implemented in a stripped copy of libstdc++, will not work.
10490 If we do not have debug info for the definition, we will have to
10491 match them up some other way.
10492
10493 When we do name matching there is a related problem with function
10494 templates; two instantiated function templates are allowed to
10495 differ only by their return types, which we do not add here. */
10496
10497 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10498 {
10499 struct attribute *attr;
10500 struct die_info *child;
10501 int first = 1;
2c75ccb2 10502 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10503
10504 die->building_fullname = 1;
10505
10506 for (child = die->child; child != NULL; child = child->sibling)
10507 {
10508 struct type *type;
12df843f 10509 LONGEST value;
d521ce57 10510 const gdb_byte *bytes;
98bfdba5
PA
10511 struct dwarf2_locexpr_baton *baton;
10512 struct value *v;
10513
10514 if (child->tag != DW_TAG_template_type_param
10515 && child->tag != DW_TAG_template_value_param)
10516 continue;
10517
10518 if (first)
10519 {
d7e74731 10520 buf.puts ("<");
98bfdba5
PA
10521 first = 0;
10522 }
10523 else
d7e74731 10524 buf.puts (", ");
98bfdba5
PA
10525
10526 attr = dwarf2_attr (child, DW_AT_type, cu);
10527 if (attr == NULL)
10528 {
b98664d3 10529 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10530 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10531 continue;
10532 }
10533 type = die_type (child, cu);
10534
10535 if (child->tag == DW_TAG_template_type_param)
10536 {
2c75ccb2
AB
10537 cplus_lang->print_type (type, "", &buf, -1, 0,
10538 &type_print_raw_options);
98bfdba5
PA
10539 continue;
10540 }
10541
10542 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10543 if (attr == NULL)
10544 {
b98664d3 10545 complaint (_("template parameter missing "
3e43a32a 10546 "DW_AT_const_value"));
d7e74731 10547 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10548 continue;
10549 }
10550
10551 dwarf2_const_value_attr (attr, type, name,
10552 &cu->comp_unit_obstack, cu,
10553 &value, &bytes, &baton);
10554
20ce4123 10555 if (type->has_no_signedness ())
98bfdba5
PA
10556 /* GDB prints characters as NUMBER 'CHAR'. If that's
10557 changed, this can use value_print instead. */
2c75ccb2 10558 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10559 else
10560 {
10561 struct value_print_options opts;
10562
10563 if (baton != NULL)
10564 v = dwarf2_evaluate_loc_desc (type, NULL,
10565 baton->data,
10566 baton->size,
9f47c707
SM
10567 baton->per_cu,
10568 baton->per_objfile);
98bfdba5
PA
10569 else if (bytes != NULL)
10570 {
10571 v = allocate_value (type);
10572 memcpy (value_contents_writeable (v), bytes,
10573 TYPE_LENGTH (type));
10574 }
10575 else
10576 v = value_from_longest (type, value);
10577
3e43a32a
MS
10578 /* Specify decimal so that we do not depend on
10579 the radix. */
98bfdba5
PA
10580 get_formatted_print_options (&opts, 'd');
10581 opts.raw = 1;
d7e74731 10582 value_print (v, &buf, &opts);
98bfdba5 10583 release_value (v);
98bfdba5
PA
10584 }
10585 }
10586
10587 die->building_fullname = 0;
10588
10589 if (!first)
10590 {
10591 /* Close the argument list, with a space if necessary
10592 (nested templates). */
d7e74731
PA
10593 if (!buf.empty () && buf.string ().back () == '>')
10594 buf.puts (" >");
98bfdba5 10595 else
d7e74731 10596 buf.puts (">");
98bfdba5
PA
10597 }
10598 }
10599
9c37b5ae 10600 /* For C++ methods, append formal parameter type
94af9270 10601 information, if PHYSNAME. */
6e70227d 10602
94af9270 10603 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10604 && cu->language == language_cplus)
94af9270
KS
10605 {
10606 struct type *type = read_type_die (die, cu);
10607
d7e74731 10608 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10609 &type_print_raw_options);
94af9270 10610
9c37b5ae 10611 if (cu->language == language_cplus)
94af9270 10612 {
60430eff
DJ
10613 /* Assume that an artificial first parameter is
10614 "this", but do not crash if it is not. RealView
10615 marks unnamed (and thus unused) parameters as
10616 artificial; there is no way to differentiate
10617 the two cases. */
1f704f76 10618 if (type->num_fields () > 0
94af9270 10619 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10620 && type->field (0).type ()->code () == TYPE_CODE_PTR
10621 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10622 buf.puts (" const");
94af9270
KS
10623 }
10624 }
10625
d7e74731 10626 const std::string &intermediate_name = buf.string ();
94af9270
KS
10627
10628 if (cu->language == language_cplus)
34a68019 10629 canonical_name
322a8516 10630 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10631 objfile);
34a68019
TT
10632
10633 /* If we only computed INTERMEDIATE_NAME, or if
10634 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10635 intern it. */
322a8516 10636 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10637 name = objfile->intern (intermediate_name);
34a68019
TT
10638 else
10639 name = canonical_name;
94af9270
KS
10640 }
10641 }
10642
10643 return name;
10644}
10645
0114d602
DJ
10646/* Return the fully qualified name of DIE, based on its DW_AT_name.
10647 If scope qualifiers are appropriate they will be added. The result
34a68019 10648 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10649 not have a name. NAME may either be from a previous call to
10650 dwarf2_name or NULL.
10651
9c37b5ae 10652 The output string will be canonicalized (if C++). */
0114d602
DJ
10653
10654static const char *
15d034d0 10655dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10656{
94af9270
KS
10657 return dwarf2_compute_name (name, die, cu, 0);
10658}
0114d602 10659
94af9270
KS
10660/* Construct a physname for the given DIE in CU. NAME may either be
10661 from a previous call to dwarf2_name or NULL. The result will be
10662 allocated on the objfile_objstack or NULL if the DIE does not have a
10663 name.
0114d602 10664
9c37b5ae 10665 The output string will be canonicalized (if C++). */
0114d602 10666
94af9270 10667static const char *
15d034d0 10668dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10669{
5e22e966 10670 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10671 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10672 int need_copy = 1;
10673
10674 /* In this case dwarf2_compute_name is just a shortcut not building anything
10675 on its own. */
10676 if (!die_needs_namespace (die, cu))
10677 return dwarf2_compute_name (name, die, cu, 1);
10678
906bb4c5
TT
10679 if (cu->language != language_rust)
10680 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10681
10682 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10683 has computed. */
791afaa2 10684 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10685 if (mangled != NULL)
900e11f9 10686 {
900e11f9 10687
d3355e4d 10688 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10689 {
10690 /* Do nothing (do not demangle the symbol name). */
10691 }
a766d390
DE
10692 else
10693 {
0eb876f5
JB
10694 /* Use DMGL_RET_DROP for C++ template functions to suppress
10695 their return type. It is easier for GDB users to search
10696 for such functions as `name(params)' than `long name(params)'.
10697 In such case the minimal symbol names do not match the full
10698 symbol names but for template functions there is never a need
10699 to look up their definition from their declaration so
10700 the only disadvantage remains the minimal symbol variant
10701 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10702 demangled.reset (gdb_demangle (mangled,
10703 (DMGL_PARAMS | DMGL_ANSI
10704 | DMGL_RET_DROP)));
a766d390 10705 }
900e11f9 10706 if (demangled)
791afaa2 10707 canon = demangled.get ();
900e11f9
JK
10708 else
10709 {
10710 canon = mangled;
10711 need_copy = 0;
10712 }
10713 }
10714
10715 if (canon == NULL || check_physname)
10716 {
10717 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10718
10719 if (canon != NULL && strcmp (physname, canon) != 0)
10720 {
10721 /* It may not mean a bug in GDB. The compiler could also
10722 compute DW_AT_linkage_name incorrectly. But in such case
10723 GDB would need to be bug-to-bug compatible. */
10724
b98664d3 10725 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10726 "(from linkage <%s>) - DIE at %s [in module %s]"),
10727 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10728 objfile_name (objfile));
900e11f9
JK
10729
10730 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10731 is available here - over computed PHYSNAME. It is safer
10732 against both buggy GDB and buggy compilers. */
10733
10734 retval = canon;
10735 }
10736 else
10737 {
10738 retval = physname;
10739 need_copy = 0;
10740 }
10741 }
10742 else
10743 retval = canon;
10744
10745 if (need_copy)
be1e3d3e 10746 retval = objfile->intern (retval);
900e11f9 10747
900e11f9 10748 return retval;
0114d602
DJ
10749}
10750
74921315
KS
10751/* Inspect DIE in CU for a namespace alias. If one exists, record
10752 a new symbol for it.
10753
10754 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10755
10756static int
10757read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10758{
10759 struct attribute *attr;
10760
10761 /* If the die does not have a name, this is not a namespace
10762 alias. */
10763 attr = dwarf2_attr (die, DW_AT_name, cu);
10764 if (attr != NULL)
10765 {
10766 int num;
10767 struct die_info *d = die;
10768 struct dwarf2_cu *imported_cu = cu;
10769
10770 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10771 keep inspecting DIEs until we hit the underlying import. */
10772#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10773 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10774 {
10775 attr = dwarf2_attr (d, DW_AT_import, cu);
10776 if (attr == NULL)
10777 break;
10778
10779 d = follow_die_ref (d, attr, &imported_cu);
10780 if (d->tag != DW_TAG_imported_declaration)
10781 break;
10782 }
10783
10784 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10785 {
b98664d3 10786 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10787 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10788 return 0;
10789 }
10790
10791 if (attr != NULL)
10792 {
10793 struct type *type;
0826b30a 10794 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10795
aa66c379 10796 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10797 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10798 {
10799 /* This declaration is a global namespace alias. Add
10800 a symbol for it whose type is the aliased namespace. */
10801 new_symbol (die, type, cu);
10802 return 1;
10803 }
10804 }
10805 }
10806
10807 return 0;
10808}
10809
22cee43f 10810/* Return the using directives repository (global or local?) to use in the
804d2729 10811 current context for CU.
22cee43f
PMR
10812
10813 For Ada, imported declarations can materialize renamings, which *may* be
10814 global. However it is impossible (for now?) in DWARF to distinguish
10815 "external" imported declarations and "static" ones. As all imported
10816 declarations seem to be static in all other languages, make them all CU-wide
10817 global only in Ada. */
10818
10819static struct using_direct **
804d2729 10820using_directives (struct dwarf2_cu *cu)
22cee43f 10821{
c24bdb02
KS
10822 if (cu->language == language_ada
10823 && cu->get_builder ()->outermost_context_p ())
10824 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10825 else
c24bdb02 10826 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10827}
10828
27aa8d6a
SW
10829/* Read the import statement specified by the given die and record it. */
10830
10831static void
10832read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10833{
5e22e966 10834 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10835 struct attribute *import_attr;
32019081 10836 struct die_info *imported_die, *child_die;
de4affc9 10837 struct dwarf2_cu *imported_cu;
27aa8d6a 10838 const char *imported_name;
794684b6 10839 const char *imported_name_prefix;
13387711
SW
10840 const char *canonical_name;
10841 const char *import_alias;
10842 const char *imported_declaration = NULL;
794684b6 10843 const char *import_prefix;
eb1e02fd 10844 std::vector<const char *> excludes;
13387711 10845
27aa8d6a
SW
10846 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10847 if (import_attr == NULL)
10848 {
b98664d3 10849 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10850 dwarf_tag_name (die->tag));
10851 return;
10852 }
10853
de4affc9
CC
10854 imported_cu = cu;
10855 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10856 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10857 if (imported_name == NULL)
10858 {
10859 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10860
dda83cd7
SM
10861 The import in the following code:
10862 namespace A
10863 {
10864 typedef int B;
10865 }
10866
10867 int main ()
10868 {
10869 using A::B;
10870 B b;
10871 return b;
10872 }
10873
10874 ...
10875 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10876 <52> DW_AT_decl_file : 1
10877 <53> DW_AT_decl_line : 6
10878 <54> DW_AT_import : <0x75>
10879 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10880 <59> DW_AT_name : B
10881 <5b> DW_AT_decl_file : 1
10882 <5c> DW_AT_decl_line : 2
10883 <5d> DW_AT_type : <0x6e>
10884 ...
10885 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10886 <76> DW_AT_byte_size : 4
10887 <77> DW_AT_encoding : 5 (signed)
10888
10889 imports the wrong die ( 0x75 instead of 0x58 ).
10890 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10891 return;
10892 }
10893
82856980
SW
10894 /* Figure out the local name after import. */
10895 import_alias = dwarf2_name (die, cu);
27aa8d6a 10896
794684b6
SW
10897 /* Figure out where the statement is being imported to. */
10898 import_prefix = determine_prefix (die, cu);
10899
10900 /* Figure out what the scope of the imported die is and prepend it
10901 to the name of the imported die. */
de4affc9 10902 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10903
f55ee35c
JK
10904 if (imported_die->tag != DW_TAG_namespace
10905 && imported_die->tag != DW_TAG_module)
794684b6 10906 {
13387711
SW
10907 imported_declaration = imported_name;
10908 canonical_name = imported_name_prefix;
794684b6 10909 }
13387711 10910 else if (strlen (imported_name_prefix) > 0)
12aaed36 10911 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10912 imported_name_prefix,
10913 (cu->language == language_d ? "." : "::"),
10914 imported_name, (char *) NULL);
13387711
SW
10915 else
10916 canonical_name = imported_name;
794684b6 10917
32019081
JK
10918 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10919 for (child_die = die->child; child_die && child_die->tag;
436c571c 10920 child_die = child_die->sibling)
32019081
JK
10921 {
10922 /* DWARF-4: A Fortran use statement with a “rename list” may be
10923 represented by an imported module entry with an import attribute
10924 referring to the module and owned entries corresponding to those
10925 entities that are renamed as part of being imported. */
10926
10927 if (child_die->tag != DW_TAG_imported_declaration)
10928 {
b98664d3 10929 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10930 "- DIE at %s [in module %s]"),
10931 sect_offset_str (child_die->sect_off),
10932 objfile_name (objfile));
32019081
JK
10933 continue;
10934 }
10935
10936 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10937 if (import_attr == NULL)
10938 {
b98664d3 10939 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10940 dwarf_tag_name (child_die->tag));
10941 continue;
10942 }
10943
10944 imported_cu = cu;
10945 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10946 &imported_cu);
10947 imported_name = dwarf2_name (imported_die, imported_cu);
10948 if (imported_name == NULL)
10949 {
b98664d3 10950 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10951 "imported name - DIE at %s [in module %s]"),
10952 sect_offset_str (child_die->sect_off),
10953 objfile_name (objfile));
32019081
JK
10954 continue;
10955 }
10956
eb1e02fd 10957 excludes.push_back (imported_name);
32019081
JK
10958
10959 process_die (child_die, cu);
10960 }
10961
804d2729 10962 add_using_directive (using_directives (cu),
22cee43f
PMR
10963 import_prefix,
10964 canonical_name,
10965 import_alias,
10966 imported_declaration,
10967 excludes,
10968 0,
10969 &objfile->objfile_obstack);
27aa8d6a
SW
10970}
10971
5230b05a
WT
10972/* ICC<14 does not output the required DW_AT_declaration on incomplete
10973 types, but gives them a size of zero. Starting with version 14,
10974 ICC is compatible with GCC. */
10975
9068261f 10976static bool
5230b05a
WT
10977producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10978{
10979 if (!cu->checked_producer)
10980 check_producer (cu);
10981
10982 return cu->producer_is_icc_lt_14;
10983}
10984
eb77c9df
AB
10985/* ICC generates a DW_AT_type for C void functions. This was observed on
10986 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10987 which says that void functions should not have a DW_AT_type. */
10988
10989static bool
10990producer_is_icc (struct dwarf2_cu *cu)
10991{
10992 if (!cu->checked_producer)
10993 check_producer (cu);
10994
10995 return cu->producer_is_icc;
10996}
10997
1b80a9fa
JK
10998/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10999 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11000 this, it was first present in GCC release 4.3.0. */
11001
9068261f 11002static bool
1b80a9fa
JK
11003producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11004{
11005 if (!cu->checked_producer)
11006 check_producer (cu);
11007
11008 return cu->producer_is_gcc_lt_4_3;
11009}
11010
d721ba37
PA
11011static file_and_directory
11012find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11013{
d721ba37
PA
11014 file_and_directory res;
11015
9291a0cd
TT
11016 /* Find the filename. Do not use dwarf2_name here, since the filename
11017 is not a source language identifier. */
d721ba37
PA
11018 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11019 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11020
d721ba37
PA
11021 if (res.comp_dir == NULL
11022 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11023 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11024 {
d721ba37
PA
11025 res.comp_dir_storage = ldirname (res.name);
11026 if (!res.comp_dir_storage.empty ())
11027 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11028 }
d721ba37 11029 if (res.comp_dir != NULL)
9291a0cd
TT
11030 {
11031 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11032 directory, get rid of it. */
d721ba37 11033 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11034
d721ba37
PA
11035 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11036 res.comp_dir = cp + 1;
9291a0cd
TT
11037 }
11038
d721ba37
PA
11039 if (res.name == NULL)
11040 res.name = "<unknown>";
11041
11042 return res;
9291a0cd
TT
11043}
11044
f4dc4d17
DE
11045/* Handle DW_AT_stmt_list for a compilation unit.
11046 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11047 COMP_DIR is the compilation directory. LOWPC is passed to
11048 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11049
11050static void
11051handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11052 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11053{
976ca316 11054 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 11055 struct attribute *attr;
527f3840
JK
11056 struct line_header line_header_local;
11057 hashval_t line_header_local_hash;
527f3840
JK
11058 void **slot;
11059 int decode_mapping;
2ab95328 11060
f4dc4d17
DE
11061 gdb_assert (! cu->per_cu->is_debug_types);
11062
2ab95328 11063 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 11064 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
11065 return;
11066
d4df075e 11067 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
11068
11069 /* The line header hash table is only created if needed (it exists to
11070 prevent redundant reading of the line table for partial_units).
11071 If we're given a partial_unit, we'll need it. If we're given a
11072 compile_unit, then use the line header hash table if it's already
11073 created, but don't create one just yet. */
11074
976ca316 11075 if (per_objfile->line_header_hash == NULL
527f3840 11076 && die->tag == DW_TAG_partial_unit)
2ab95328 11077 {
976ca316 11078 per_objfile->line_header_hash
d15acc42
TT
11079 .reset (htab_create_alloc (127, line_header_hash_voidp,
11080 line_header_eq_voidp,
11081 free_line_header_voidp,
11082 xcalloc, xfree));
527f3840 11083 }
2ab95328 11084
9c541725 11085 line_header_local.sect_off = line_offset;
527f3840
JK
11086 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11087 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 11088 if (per_objfile->line_header_hash != NULL)
527f3840 11089 {
976ca316 11090 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11091 &line_header_local,
11092 line_header_local_hash, NO_INSERT);
11093
11094 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11095 is not present in *SLOT (since if there is something in *SLOT then
11096 it will be for a partial_unit). */
11097 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11098 {
527f3840 11099 gdb_assert (*slot != NULL);
9a3c8263 11100 cu->line_header = (struct line_header *) *slot;
527f3840 11101 return;
dee91e82 11102 }
2ab95328 11103 }
527f3840
JK
11104
11105 /* dwarf_decode_line_header does not yet provide sufficient information.
11106 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11107 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11108 if (lh == NULL)
527f3840 11109 return;
4c8aa72d
PA
11110
11111 cu->line_header = lh.release ();
11112 cu->line_header_die_owner = die;
527f3840 11113
976ca316 11114 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11115 slot = NULL;
11116 else
11117 {
976ca316 11118 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11119 &line_header_local,
11120 line_header_local_hash, INSERT);
11121 gdb_assert (slot != NULL);
11122 }
11123 if (slot != NULL && *slot == NULL)
11124 {
11125 /* This newly decoded line number information unit will be owned
11126 by line_header_hash hash table. */
11127 *slot = cu->line_header;
4c8aa72d 11128 cu->line_header_die_owner = NULL;
527f3840
JK
11129 }
11130 else
11131 {
11132 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 11133 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
11134 line_header for this CU - it may happen at most once for each line
11135 number information unit. And if we're not using line_header_hash
11136 then this is what we want as well. */
11137 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11138 }
11139 decode_mapping = (die->tag != DW_TAG_partial_unit);
11140 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11141 decode_mapping);
fff8551c 11142
2ab95328
TT
11143}
11144
95554aad 11145/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11146
c906108c 11147static void
e7c27a73 11148read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11149{
976ca316
SM
11150 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11151 struct objfile *objfile = per_objfile->objfile;
08feed99 11152 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11153 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11154 CORE_ADDR highpc = ((CORE_ADDR) 0);
11155 struct attribute *attr;
c906108c 11156 struct die_info *child_die;
e142c38c 11157 CORE_ADDR baseaddr;
6e70227d 11158
380618d6 11159 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11160 baseaddr = objfile->text_section_offset ();
c906108c 11161
fae299cd 11162 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11163
11164 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11165 from finish_block. */
2acceee2 11166 if (lowpc == ((CORE_ADDR) -1))
c906108c 11167 lowpc = highpc;
3e29f34a 11168 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11169
d721ba37 11170 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11171
f4b8a18d
KW
11172 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11173 standardised yet. As a workaround for the language detection we fall
11174 back to the DW_AT_producer string. */
11175 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11176 cu->language = language_opencl;
11177
3019eac3
DE
11178 /* Similar hack for Go. */
11179 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11180 set_cu_language (DW_LANG_Go, cu);
11181
c24bdb02 11182 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11183
11184 /* Decode line number information if present. We do this before
11185 processing child DIEs, so that the line header table is available
11186 for DW_AT_decl_file. */
d721ba37 11187 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11188
11189 /* Process all dies in compilation unit. */
11190 if (die->child != NULL)
11191 {
11192 child_die = die->child;
11193 while (child_die && child_die->tag)
11194 {
11195 process_die (child_die, cu);
436c571c 11196 child_die = child_die->sibling;
3019eac3
DE
11197 }
11198 }
11199
11200 /* Decode macro information, if present. Dwarf 2 macro information
11201 refers to information in the line number info statement program
11202 header, so we can only read it if we've read the header
11203 successfully. */
0af92d60
JK
11204 attr = dwarf2_attr (die, DW_AT_macros, cu);
11205 if (attr == NULL)
11206 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 11207 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
11208 {
11209 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11210 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11211
529908cb 11212 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
11213 }
11214 else
11215 {
11216 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 11217 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 11218 {
529908cb 11219 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 11220
43f3e411 11221 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11222 }
11223 }
3019eac3
DE
11224}
11225
c24bdb02
KS
11226void
11227dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11228{
f4dc4d17
DE
11229 struct type_unit_group *tu_group;
11230 int first_time;
3019eac3 11231 struct attribute *attr;
9c541725 11232 unsigned int i;
0186c6a7 11233 struct signatured_type *sig_type;
3019eac3 11234
f4dc4d17 11235 gdb_assert (per_cu->is_debug_types);
0186c6a7 11236 sig_type = (struct signatured_type *) per_cu;
3019eac3 11237
c24bdb02 11238 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11239
f4dc4d17 11240 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11241 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11242 if (sig_type->type_unit_group == NULL)
c24bdb02 11243 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11244 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11245
11246 /* If we've already processed this stmt_list there's no real need to
11247 do it again, we could fake it and just recreate the part we need
11248 (file name,index -> symtab mapping). If data shows this optimization
11249 is useful we can do it then. */
8adb8487
TT
11250 type_unit_group_unshareable *tug_unshare
11251 = per_objfile->get_type_unit_group_unshareable (tu_group);
11252 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11253
11254 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11255 debug info. */
fff8551c 11256 line_header_up lh;
d4df075e 11257 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11258 {
d4df075e 11259 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11260 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11261 }
11262 if (lh == NULL)
11263 {
11264 if (first_time)
c24bdb02 11265 start_symtab ("", NULL, 0);
f4dc4d17
DE
11266 else
11267 {
8adb8487 11268 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11269 gdb_assert (m_builder == nullptr);
8adb8487 11270 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11271 m_builder.reset (new struct buildsym_compunit
11272 (COMPUNIT_OBJFILE (cust), "",
11273 COMPUNIT_DIRNAME (cust),
11274 compunit_language (cust),
11275 0, cust));
770479f2 11276 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11277 }
f4dc4d17 11278 return;
3019eac3
DE
11279 }
11280
c24bdb02
KS
11281 line_header = lh.release ();
11282 line_header_die_owner = die;
3019eac3 11283
f4dc4d17
DE
11284 if (first_time)
11285 {
c24bdb02 11286 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11287
1fd60fc0
DE
11288 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11289 still initializing it, and our caller (a few levels up)
11290 process_full_type_unit still needs to know if this is the first
11291 time. */
11292
8adb8487 11293 tug_unshare->symtabs
4ac93832
TT
11294 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11295 struct symtab *, line_header->file_names_size ());
3019eac3 11296
7ba99d21
AT
11297 auto &file_names = line_header->file_names ();
11298 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11299 {
7ba99d21 11300 file_entry &fe = file_names[i];
c24bdb02
KS
11301 dwarf2_start_subfile (this, fe.name,
11302 fe.include_dir (line_header));
11303 buildsym_compunit *b = get_builder ();
11304 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11305 {
4c8aa72d
PA
11306 /* NOTE: start_subfile will recognize when it's been
11307 passed a file it has already seen. So we can't
11308 assume there's a simple mapping from
11309 cu->line_header->file_names to subfiles, plus
11310 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11311 b->get_current_subfile ()->symtab
11312 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11313 }
11314
c24bdb02 11315 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11316 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11317 }
11318 }
11319 else
3019eac3 11320 {
c24bdb02 11321 gdb_assert (m_builder == nullptr);
8adb8487 11322 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11323 m_builder.reset (new struct buildsym_compunit
11324 (COMPUNIT_OBJFILE (cust), "",
11325 COMPUNIT_DIRNAME (cust),
11326 compunit_language (cust),
11327 0, cust));
770479f2 11328 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11329
7ba99d21
AT
11330 auto &file_names = line_header->file_names ();
11331 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11332 {
7ba99d21 11333 file_entry &fe = file_names[i];
8adb8487 11334 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11335 }
3019eac3
DE
11336 }
11337
f4dc4d17
DE
11338 /* The main symtab is allocated last. Type units don't have DW_AT_name
11339 so they don't have a "real" (so to speak) symtab anyway.
11340 There is later code that will assign the main symtab to all symbols
11341 that don't have one. We need to handle the case of a symbol with a
11342 missing symtab (DW_AT_decl_file) anyway. */
11343}
3019eac3 11344
f4dc4d17
DE
11345/* Process DW_TAG_type_unit.
11346 For TUs we want to skip the first top level sibling if it's not the
11347 actual type being defined by this TU. In this case the first top
11348 level sibling is there to provide context only. */
3019eac3 11349
f4dc4d17
DE
11350static void
11351read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11352{
11353 struct die_info *child_die;
3019eac3 11354
f4dc4d17
DE
11355 prepare_one_comp_unit (cu, die, language_minimal);
11356
11357 /* Initialize (or reinitialize) the machinery for building symtabs.
11358 We do this before processing child DIEs, so that the line header table
11359 is available for DW_AT_decl_file. */
c24bdb02 11360 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11361
11362 if (die->child != NULL)
11363 {
11364 child_die = die->child;
11365 while (child_die && child_die->tag)
11366 {
11367 process_die (child_die, cu);
436c571c 11368 child_die = child_die->sibling;
f4dc4d17
DE
11369 }
11370 }
3019eac3
DE
11371}
11372\f
80626a55
DE
11373/* DWO/DWP files.
11374
11375 http://gcc.gnu.org/wiki/DebugFission
11376 http://gcc.gnu.org/wiki/DebugFissionDWP
11377
11378 To simplify handling of both DWO files ("object" files with the DWARF info)
11379 and DWP files (a file with the DWOs packaged up into one file), we treat
11380 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11381
11382static hashval_t
11383hash_dwo_file (const void *item)
11384{
9a3c8263 11385 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11386 hashval_t hash;
3019eac3 11387
a2ce51a0
DE
11388 hash = htab_hash_string (dwo_file->dwo_name);
11389 if (dwo_file->comp_dir != NULL)
11390 hash += htab_hash_string (dwo_file->comp_dir);
11391 return hash;
3019eac3
DE
11392}
11393
11394static int
11395eq_dwo_file (const void *item_lhs, const void *item_rhs)
11396{
9a3c8263
SM
11397 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11398 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11399
a2ce51a0
DE
11400 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11401 return 0;
11402 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11403 return lhs->comp_dir == rhs->comp_dir;
11404 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11405}
11406
11407/* Allocate a hash table for DWO files. */
11408
51ac9db5 11409static htab_up
298e9637 11410allocate_dwo_file_hash_table ()
3019eac3 11411{
51ac9db5
SM
11412 auto delete_dwo_file = [] (void *item)
11413 {
11414 struct dwo_file *dwo_file = (struct dwo_file *) item;
11415
11416 delete dwo_file;
11417 };
11418
bc68fb19
TT
11419 return htab_up (htab_create_alloc (41,
11420 hash_dwo_file,
11421 eq_dwo_file,
11422 delete_dwo_file,
11423 xcalloc, xfree));
3019eac3
DE
11424}
11425
80626a55
DE
11426/* Lookup DWO file DWO_NAME. */
11427
11428static void **
976ca316 11429lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11430 const char *dwo_name,
11431 const char *comp_dir)
80626a55
DE
11432{
11433 struct dwo_file find_entry;
11434 void **slot;
11435
976ca316
SM
11436 if (per_objfile->per_bfd->dwo_files == NULL)
11437 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11438
0ac5b59e
DE
11439 find_entry.dwo_name = dwo_name;
11440 find_entry.comp_dir = comp_dir;
976ca316 11441 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11442 INSERT);
80626a55
DE
11443
11444 return slot;
11445}
11446
3019eac3
DE
11447static hashval_t
11448hash_dwo_unit (const void *item)
11449{
9a3c8263 11450 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11451
11452 /* This drops the top 32 bits of the id, but is ok for a hash. */
11453 return dwo_unit->signature;
11454}
11455
11456static int
11457eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11458{
9a3c8263
SM
11459 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11460 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11461
11462 /* The signature is assumed to be unique within the DWO file.
11463 So while object file CU dwo_id's always have the value zero,
11464 that's OK, assuming each object file DWO file has only one CU,
11465 and that's the rule for now. */
11466 return lhs->signature == rhs->signature;
11467}
11468
11469/* Allocate a hash table for DWO CUs,TUs.
11470 There is one of these tables for each of CUs,TUs for each DWO file. */
11471
b0b6a987 11472static htab_up
298e9637 11473allocate_dwo_unit_table ()
3019eac3
DE
11474{
11475 /* Start out with a pretty small number.
11476 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11477 return htab_up (htab_create_alloc (3,
11478 hash_dwo_unit,
11479 eq_dwo_unit,
11480 NULL, xcalloc, xfree));
3019eac3
DE
11481}
11482
19c3d4c9 11483/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11484
11485static void
19c3d4c9
DE
11486create_dwo_cu_reader (const struct die_reader_specs *reader,
11487 const gdb_byte *info_ptr,
11488 struct die_info *comp_unit_die,
c0ab21c2
TT
11489 struct dwo_file *dwo_file,
11490 struct dwo_unit *dwo_unit)
3019eac3
DE
11491{
11492 struct dwarf2_cu *cu = reader->cu;
9c541725 11493 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11494 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11495
a084a2a6
AT
11496 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11497 if (!signature.has_value ())
3019eac3 11498 {
b98664d3 11499 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11500 " its dwo_id [in module %s]"),
9d8780f0 11501 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11502 return;
11503 }
11504
3019eac3 11505 dwo_unit->dwo_file = dwo_file;
a084a2a6 11506 dwo_unit->signature = *signature;
8a0459fd 11507 dwo_unit->section = section;
9c541725 11508 dwo_unit->sect_off = sect_off;
3019eac3
DE
11509 dwo_unit->length = cu->per_cu->length;
11510
6f738b01
SM
11511 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11512 sect_offset_str (sect_off),
11513 hex_string (dwo_unit->signature));
3019eac3
DE
11514}
11515
33c5cd75 11516/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11517 Note: This function processes DWO files only, not DWP files. */
3019eac3 11518
33c5cd75 11519static void
976ca316 11520create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11521 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11522 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11523{
976ca316
SM
11524 struct objfile *objfile = per_objfile->objfile;
11525 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11526 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11527
96b79293 11528 section.read (objfile);
33c5cd75 11529 info_ptr = section.buffer;
3019eac3
DE
11530
11531 if (info_ptr == NULL)
33c5cd75 11532 return;
3019eac3 11533
6f738b01
SM
11534 dwarf_read_debug_printf ("Reading %s for %s:",
11535 section.get_name (),
11536 section.get_file_name ());
3019eac3 11537
33c5cd75 11538 end_ptr = info_ptr + section.size;
3019eac3
DE
11539 while (info_ptr < end_ptr)
11540 {
11541 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11542 struct dwo_unit read_unit {};
33c5cd75
DB
11543 struct dwo_unit *dwo_unit;
11544 void **slot;
11545 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11546
11547 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11548 per_cu.per_bfd = per_bfd;
3019eac3 11549 per_cu.is_debug_types = 0;
33c5cd75
DB
11550 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11551 per_cu.section = &section;
11552
976ca316 11553 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11554 if (!reader.dummy_p)
11555 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11556 &dwo_file, &read_unit);
33c5cd75
DB
11557 info_ptr += per_cu.length;
11558
11559 // If the unit could not be parsed, skip it.
c0ab21c2 11560 if (read_unit.dwo_file == NULL)
33c5cd75 11561 continue;
3019eac3 11562
33c5cd75 11563 if (cus_htab == NULL)
298e9637 11564 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11565
1859c670 11566 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11567 struct dwo_unit);
c0ab21c2 11568 *dwo_unit = read_unit;
b0b6a987 11569 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11570 gdb_assert (slot != NULL);
11571 if (*slot != NULL)
19c3d4c9 11572 {
33c5cd75
DB
11573 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11574 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11575
b98664d3 11576 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11577 " the entry at offset %s, signature %s"),
11578 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11579 hex_string (dwo_unit->signature));
19c3d4c9 11580 }
33c5cd75 11581 *slot = (void *)dwo_unit;
3019eac3 11582 }
3019eac3
DE
11583}
11584
80626a55
DE
11585/* DWP file .debug_{cu,tu}_index section format:
11586 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11587 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11588
11589 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11590 officially standard DWP format was published with DWARF v5 and is called
11591 Version 5. There are no versions 3 or 4.
80626a55 11592
d2415c6c
DE
11593 DWP Version 1:
11594
80626a55
DE
11595 Both index sections have the same format, and serve to map a 64-bit
11596 signature to a set of section numbers. Each section begins with a header,
11597 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11598 indexes, and a pool of 32-bit section numbers. The index sections will be
11599 aligned at 8-byte boundaries in the file.
11600
d2415c6c
DE
11601 The index section header consists of:
11602
11603 V, 32 bit version number
11604 -, 32 bits unused
11605 N, 32 bit number of compilation units or type units in the index
11606 M, 32 bit number of slots in the hash table
80626a55 11607
d2415c6c 11608 Numbers are recorded using the byte order of the application binary.
80626a55 11609
d2415c6c
DE
11610 The hash table begins at offset 16 in the section, and consists of an array
11611 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11612 order of the application binary). Unused slots in the hash table are 0.
11613 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11614
d2415c6c
DE
11615 The parallel table begins immediately after the hash table
11616 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11617 array of 32-bit indexes (using the byte order of the application binary),
11618 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11619 table contains a 32-bit index into the pool of section numbers. For unused
11620 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11621
73869dc2
DE
11622 The pool of section numbers begins immediately following the hash table
11623 (at offset 16 + 12 * M from the beginning of the section). The pool of
11624 section numbers consists of an array of 32-bit words (using the byte order
11625 of the application binary). Each item in the array is indexed starting
11626 from 0. The hash table entry provides the index of the first section
11627 number in the set. Additional section numbers in the set follow, and the
11628 set is terminated by a 0 entry (section number 0 is not used in ELF).
11629
11630 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11631 section must be the first entry in the set, and the .debug_abbrev.dwo must
11632 be the second entry. Other members of the set may follow in any order.
11633
11634 ---
11635
d2854d8d 11636 DWP Versions 2 and 5:
73869dc2 11637
d2854d8d 11638 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11639 and the entries in the index tables are now offsets into these sections.
11640 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11641 section.
11642
11643 Index Section Contents:
11644 Header
11645 Hash Table of Signatures dwp_hash_table.hash_table
11646 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11647 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11648 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11649
11650 The index section header consists of:
11651
11652 V, 32 bit version number
11653 L, 32 bit number of columns in the table of section offsets
11654 N, 32 bit number of compilation units or type units in the index
11655 M, 32 bit number of slots in the hash table
11656
11657 Numbers are recorded using the byte order of the application binary.
11658
11659 The hash table has the same format as version 1.
11660 The parallel table of indices has the same format as version 1,
11661 except that the entries are origin-1 indices into the table of sections
11662 offsets and the table of section sizes.
11663
11664 The table of offsets begins immediately following the parallel table
11665 (at offset 16 + 12 * M from the beginning of the section). The table is
11666 a two-dimensional array of 32-bit words (using the byte order of the
11667 application binary), with L columns and N+1 rows, in row-major order.
11668 Each row in the array is indexed starting from 0. The first row provides
11669 a key to the remaining rows: each column in this row provides an identifier
11670 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11671 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11672
11673 DW_SECT_INFO 1 .debug_info.dwo
11674 DW_SECT_TYPES 2 .debug_types.dwo
11675 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11676 DW_SECT_LINE 4 .debug_line.dwo
11677 DW_SECT_LOC 5 .debug_loc.dwo
11678 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11679 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11680 DW_SECT_MACRO 8 .debug_macro.dwo
11681
d2854d8d
CT
11682 The section identifiers for Version 5 are:
11683
11684 DW_SECT_INFO_V5 1 .debug_info.dwo
11685 DW_SECT_RESERVED_V5 2 --
11686 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11687 DW_SECT_LINE_V5 4 .debug_line.dwo
11688 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11689 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11690 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11691 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11692
73869dc2
DE
11693 The offsets provided by the CU and TU index sections are the base offsets
11694 for the contributions made by each CU or TU to the corresponding section
11695 in the package file. Each CU and TU header contains an abbrev_offset
11696 field, used to find the abbreviations table for that CU or TU within the
11697 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11698 be interpreted as relative to the base offset given in the index section.
11699 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11700 should be interpreted as relative to the base offset for .debug_line.dwo,
11701 and offsets into other debug sections obtained from DWARF attributes should
11702 also be interpreted as relative to the corresponding base offset.
11703
11704 The table of sizes begins immediately following the table of offsets.
11705 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11706 with L columns and N rows, in row-major order. Each row in the array is
11707 indexed starting from 1 (row 0 is shared by the two tables).
11708
11709 ---
11710
11711 Hash table lookup is handled the same in version 1 and 2:
11712
11713 We assume that N and M will not exceed 2^32 - 1.
11714 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11715
d2415c6c
DE
11716 Given a 64-bit compilation unit signature or a type signature S, an entry
11717 in the hash table is located as follows:
80626a55 11718
d2415c6c
DE
11719 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11720 the low-order k bits all set to 1.
80626a55 11721
d2415c6c 11722 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11723
d2415c6c
DE
11724 3) If the hash table entry at index H matches the signature, use that
11725 entry. If the hash table entry at index H is unused (all zeroes),
11726 terminate the search: the signature is not present in the table.
80626a55 11727
d2415c6c 11728 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11729
d2415c6c 11730 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11731 to stop at an unused slot or find the match. */
80626a55
DE
11732
11733/* Create a hash table to map DWO IDs to their CU/TU entry in
11734 .debug_{info,types}.dwo in DWP_FILE.
11735 Returns NULL if there isn't one.
11736 Note: This function processes DWP files only, not DWO files. */
11737
11738static struct dwp_hash_table *
976ca316 11739create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11740 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11741{
976ca316 11742 struct objfile *objfile = per_objfile->objfile;
400174b1 11743 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11744 const gdb_byte *index_ptr, *index_end;
80626a55 11745 struct dwarf2_section_info *index;
73869dc2 11746 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11747 struct dwp_hash_table *htab;
11748
11749 if (is_debug_types)
11750 index = &dwp_file->sections.tu_index;
11751 else
11752 index = &dwp_file->sections.cu_index;
11753
96b79293 11754 if (index->empty ())
80626a55 11755 return NULL;
96b79293 11756 index->read (objfile);
80626a55
DE
11757
11758 index_ptr = index->buffer;
11759 index_end = index_ptr + index->size;
11760
d2854d8d
CT
11761 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11762 For now it's safe to just read 4 bytes (particularly as it's difficult to
11763 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11764 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11765 index_ptr += 4;
d2854d8d 11766 if (version == 2 || version == 5)
73869dc2
DE
11767 nr_columns = read_4_bytes (dbfd, index_ptr);
11768 else
11769 nr_columns = 0;
11770 index_ptr += 4;
80626a55
DE
11771 nr_units = read_4_bytes (dbfd, index_ptr);
11772 index_ptr += 4;
11773 nr_slots = read_4_bytes (dbfd, index_ptr);
11774 index_ptr += 4;
11775
d2854d8d 11776 if (version != 1 && version != 2 && version != 5)
80626a55 11777 {
21aa081e 11778 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11779 " [in module %s]"),
21aa081e 11780 pulongest (version), dwp_file->name);
80626a55
DE
11781 }
11782 if (nr_slots != (nr_slots & -nr_slots))
11783 {
21aa081e 11784 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11785 " is not power of 2 [in module %s]"),
21aa081e 11786 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11787 }
11788
976ca316 11789 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11790 htab->version = version;
11791 htab->nr_columns = nr_columns;
80626a55
DE
11792 htab->nr_units = nr_units;
11793 htab->nr_slots = nr_slots;
11794 htab->hash_table = index_ptr;
11795 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11796
11797 /* Exit early if the table is empty. */
11798 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11799 || (version == 2 && nr_columns == 0)
11800 || (version == 5 && nr_columns == 0))
73869dc2
DE
11801 {
11802 /* All must be zero. */
11803 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11804 || (version == 2 && nr_columns != 0)
11805 || (version == 5 && nr_columns != 0))
73869dc2 11806 {
b98664d3 11807 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11808 " all zero [in modules %s]"),
11809 dwp_file->name);
11810 }
11811 return htab;
11812 }
11813
11814 if (version == 1)
11815 {
11816 htab->section_pool.v1.indices =
11817 htab->unit_table + sizeof (uint32_t) * nr_slots;
11818 /* It's harder to decide whether the section is too small in v1.
11819 V1 is deprecated anyway so we punt. */
11820 }
d2854d8d 11821 else if (version == 2)
73869dc2
DE
11822 {
11823 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11824 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11825 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11826 /* Reverse map for error checking. */
11827 int ids_seen[DW_SECT_MAX + 1];
11828 int i;
11829
11830 if (nr_columns < 2)
11831 {
11832 error (_("Dwarf Error: bad DWP hash table, too few columns"
11833 " in section table [in module %s]"),
11834 dwp_file->name);
11835 }
11836 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11837 {
11838 error (_("Dwarf Error: bad DWP hash table, too many columns"
11839 " in section table [in module %s]"),
11840 dwp_file->name);
11841 }
04fd5eed
GB
11842 memset (ids, 255, sizeof_ids);
11843 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11844 for (i = 0; i < nr_columns; ++i)
11845 {
11846 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11847
11848 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11849 {
11850 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11851 " in section table [in module %s]"),
11852 id, dwp_file->name);
11853 }
11854 if (ids_seen[id] != -1)
11855 {
11856 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11857 " id %d in section table [in module %s]"),
11858 id, dwp_file->name);
11859 }
11860 ids_seen[id] = i;
11861 ids[i] = id;
11862 }
11863 /* Must have exactly one info or types section. */
11864 if (((ids_seen[DW_SECT_INFO] != -1)
11865 + (ids_seen[DW_SECT_TYPES] != -1))
11866 != 1)
11867 {
11868 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11869 " DWO info/types section [in module %s]"),
11870 dwp_file->name);
11871 }
11872 /* Must have an abbrev section. */
11873 if (ids_seen[DW_SECT_ABBREV] == -1)
11874 {
11875 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11876 " section [in module %s]"),
11877 dwp_file->name);
11878 }
11879 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11880 htab->section_pool.v2.sizes =
11881 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11882 * nr_units * nr_columns);
11883 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11884 * nr_units * nr_columns))
11885 > index_end)
11886 {
11887 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11888 " [in module %s]"),
11889 dwp_file->name);
11890 }
11891 }
d2854d8d
CT
11892 else /* version == 5 */
11893 {
11894 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11895 int *ids = htab->section_pool.v5.section_ids;
11896 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11897 /* Reverse map for error checking. */
11898 int ids_seen[DW_SECT_MAX_V5 + 1];
11899
11900 if (nr_columns < 2)
11901 {
11902 error (_("Dwarf Error: bad DWP hash table, too few columns"
11903 " in section table [in module %s]"),
11904 dwp_file->name);
11905 }
11906 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11907 {
11908 error (_("Dwarf Error: bad DWP hash table, too many columns"
11909 " in section table [in module %s]"),
11910 dwp_file->name);
11911 }
11912 memset (ids, 255, sizeof_ids);
11913 memset (ids_seen, 255, sizeof (ids_seen));
11914 for (int i = 0; i < nr_columns; ++i)
11915 {
11916 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11917
11918 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11919 {
11920 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11921 " in section table [in module %s]"),
11922 id, dwp_file->name);
11923 }
11924 if (ids_seen[id] != -1)
11925 {
11926 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11927 " id %d in section table [in module %s]"),
11928 id, dwp_file->name);
11929 }
11930 ids_seen[id] = i;
11931 ids[i] = id;
11932 }
11933 /* Must have seen an info section. */
11934 if (ids_seen[DW_SECT_INFO_V5] == -1)
11935 {
11936 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11937 " DWO info/types section [in module %s]"),
11938 dwp_file->name);
11939 }
11940 /* Must have an abbrev section. */
11941 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11942 {
11943 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11944 " section [in module %s]"),
11945 dwp_file->name);
11946 }
11947 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11948 htab->section_pool.v5.sizes
11949 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11950 * nr_units * nr_columns);
11951 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11952 * nr_units * nr_columns))
11953 > index_end)
11954 {
11955 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11956 " [in module %s]"),
11957 dwp_file->name);
11958 }
11959 }
80626a55
DE
11960
11961 return htab;
11962}
11963
11964/* Update SECTIONS with the data from SECTP.
11965
5bb6e9dd
TT
11966 This function is like the other "locate" section routines, but in
11967 this context the sections to read comes from the DWP V1 hash table,
11968 not the full ELF section table.
80626a55
DE
11969
11970 The result is non-zero for success, or zero if an error was found. */
11971
11972static int
73869dc2
DE
11973locate_v1_virtual_dwo_sections (asection *sectp,
11974 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11975{
11976 const struct dwop_section_names *names = &dwop_section_names;
11977
fbedd546 11978 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11979 {
11980 /* There can be only one. */
049412e3 11981 if (sections->abbrev.s.section != NULL)
80626a55 11982 return 0;
049412e3 11983 sections->abbrev.s.section = sectp;
fd361982 11984 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11985 }
fbedd546
TT
11986 else if (names->info_dwo.matches (sectp->name)
11987 || names->types_dwo.matches (sectp->name))
80626a55
DE
11988 {
11989 /* There can be only one. */
049412e3 11990 if (sections->info_or_types.s.section != NULL)
80626a55 11991 return 0;
049412e3 11992 sections->info_or_types.s.section = sectp;
fd361982 11993 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11994 }
fbedd546 11995 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
11996 {
11997 /* There can be only one. */
049412e3 11998 if (sections->line.s.section != NULL)
80626a55 11999 return 0;
049412e3 12000 sections->line.s.section = sectp;
fd361982 12001 sections->line.size = bfd_section_size (sectp);
80626a55 12002 }
fbedd546 12003 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
12004 {
12005 /* There can be only one. */
049412e3 12006 if (sections->loc.s.section != NULL)
80626a55 12007 return 0;
049412e3 12008 sections->loc.s.section = sectp;
fd361982 12009 sections->loc.size = bfd_section_size (sectp);
80626a55 12010 }
fbedd546 12011 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
12012 {
12013 /* There can be only one. */
049412e3 12014 if (sections->macinfo.s.section != NULL)
80626a55 12015 return 0;
049412e3 12016 sections->macinfo.s.section = sectp;
fd361982 12017 sections->macinfo.size = bfd_section_size (sectp);
80626a55 12018 }
fbedd546 12019 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
12020 {
12021 /* There can be only one. */
049412e3 12022 if (sections->macro.s.section != NULL)
80626a55 12023 return 0;
049412e3 12024 sections->macro.s.section = sectp;
fd361982 12025 sections->macro.size = bfd_section_size (sectp);
80626a55 12026 }
fbedd546 12027 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
12028 {
12029 /* There can be only one. */
049412e3 12030 if (sections->str_offsets.s.section != NULL)
80626a55 12031 return 0;
049412e3 12032 sections->str_offsets.s.section = sectp;
fd361982 12033 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12034 }
12035 else
12036 {
12037 /* No other kind of section is valid. */
12038 return 0;
12039 }
12040
12041 return 1;
12042}
12043
73869dc2
DE
12044/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12045 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12046 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12047 This is for DWP version 1 files. */
80626a55
DE
12048
12049static struct dwo_unit *
976ca316 12050create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 12051 struct dwp_file *dwp_file,
73869dc2
DE
12052 uint32_t unit_index,
12053 const char *comp_dir,
12054 ULONGEST signature, int is_debug_types)
80626a55 12055{
73869dc2
DE
12056 const struct dwp_hash_table *dwp_htab =
12057 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12058 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12059 const char *kind = is_debug_types ? "TU" : "CU";
12060 struct dwo_file *dwo_file;
12061 struct dwo_unit *dwo_unit;
73869dc2 12062 struct virtual_v1_dwo_sections sections;
80626a55 12063 void **dwo_file_slot;
80626a55
DE
12064 int i;
12065
73869dc2
DE
12066 gdb_assert (dwp_file->version == 1);
12067
6f738b01
SM
12068 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12069 kind, pulongest (unit_index), hex_string (signature),
12070 dwp_file->name);
80626a55 12071
19ac8c2e 12072 /* Fetch the sections of this DWO unit.
80626a55
DE
12073 Put a limit on the number of sections we look for so that bad data
12074 doesn't cause us to loop forever. */
12075
73869dc2 12076#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12077 (1 /* .debug_info or .debug_types */ \
12078 + 1 /* .debug_abbrev */ \
12079 + 1 /* .debug_line */ \
12080 + 1 /* .debug_loc */ \
12081 + 1 /* .debug_str_offsets */ \
19ac8c2e 12082 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12083 + 1 /* trailing zero */)
12084
12085 memset (&sections, 0, sizeof (sections));
80626a55 12086
73869dc2 12087 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12088 {
12089 asection *sectp;
12090 uint32_t section_nr =
12091 read_4_bytes (dbfd,
73869dc2
DE
12092 dwp_htab->section_pool.v1.indices
12093 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12094
12095 if (section_nr == 0)
12096 break;
12097 if (section_nr >= dwp_file->num_sections)
12098 {
12099 error (_("Dwarf Error: bad DWP hash table, section number too large"
12100 " [in module %s]"),
12101 dwp_file->name);
12102 }
12103
12104 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12105 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12106 {
12107 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12108 " [in module %s]"),
12109 dwp_file->name);
12110 }
12111 }
12112
12113 if (i < 2
96b79293
TT
12114 || sections.info_or_types.empty ()
12115 || sections.abbrev.empty ())
80626a55
DE
12116 {
12117 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12118 " [in module %s]"),
12119 dwp_file->name);
12120 }
73869dc2 12121 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12122 {
12123 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12124 " [in module %s]"),
12125 dwp_file->name);
12126 }
12127
12128 /* It's easier for the rest of the code if we fake a struct dwo_file and
12129 have dwo_unit "live" in that. At least for now.
12130
12131 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12132 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12133 file, we can combine them back into a virtual DWO file to save space
12134 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12135 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12136
791afaa2
TT
12137 std::string virtual_dwo_name =
12138 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12139 sections.abbrev.get_id (),
12140 sections.line.get_id (),
12141 sections.loc.get_id (),
12142 sections.str_offsets.get_id ());
80626a55 12143 /* Can we use an existing virtual DWO file? */
976ca316 12144 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12145 comp_dir);
80626a55
DE
12146 /* Create one if necessary. */
12147 if (*dwo_file_slot == NULL)
12148 {
6f738b01
SM
12149 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12150 virtual_dwo_name.c_str ());
12151
51ac9db5 12152 dwo_file = new struct dwo_file;
976ca316 12153 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 12154 dwo_file->comp_dir = comp_dir;
80626a55
DE
12155 dwo_file->sections.abbrev = sections.abbrev;
12156 dwo_file->sections.line = sections.line;
12157 dwo_file->sections.loc = sections.loc;
12158 dwo_file->sections.macinfo = sections.macinfo;
12159 dwo_file->sections.macro = sections.macro;
12160 dwo_file->sections.str_offsets = sections.str_offsets;
12161 /* The "str" section is global to the entire DWP file. */
12162 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12163 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12164 there's no need to record it in dwo_file.
12165 Also, we can't simply record type sections in dwo_file because
12166 we record a pointer into the vector in dwo_unit. As we collect more
12167 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12168 for it, invalidating all copies of pointers into the previous
12169 contents. */
80626a55
DE
12170 *dwo_file_slot = dwo_file;
12171 }
12172 else
12173 {
6f738b01
SM
12174 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12175 virtual_dwo_name.c_str ());
12176
9a3c8263 12177 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12178 }
80626a55 12179
976ca316 12180 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12181 dwo_unit->dwo_file = dwo_file;
12182 dwo_unit->signature = signature;
8d749320 12183 dwo_unit->section =
976ca316 12184 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12185 *dwo_unit->section = sections.info_or_types;
57d63ce2 12186 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12187
12188 return dwo_unit;
12189}
12190
d2854d8d
CT
12191/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12192 simplify them. Given a pointer to the containing section SECTION, and
12193 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12194 virtual section of just that piece. */
73869dc2
DE
12195
12196static struct dwarf2_section_info
d2854d8d
CT
12197create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12198 struct dwarf2_section_info *section,
12199 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
12200{
12201 struct dwarf2_section_info result;
12202 asection *sectp;
12203
12204 gdb_assert (section != NULL);
12205 gdb_assert (!section->is_virtual);
12206
12207 memset (&result, 0, sizeof (result));
12208 result.s.containing_section = section;
dc4ccb6f 12209 result.is_virtual = true;
73869dc2
DE
12210
12211 if (size == 0)
12212 return result;
12213
96b79293 12214 sectp = section->get_bfd_section ();
73869dc2
DE
12215
12216 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12217 bounds of the real section. This is a pretty-rare event, so just
12218 flag an error (easier) instead of a warning and trying to cope. */
12219 if (sectp == NULL
fd361982 12220 || offset + size > bfd_section_size (sectp))
73869dc2 12221 {
d2854d8d 12222 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 12223 " in section %s [in module %s]"),
fd361982 12224 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12225 objfile_name (per_objfile->objfile));
73869dc2
DE
12226 }
12227
12228 result.virtual_offset = offset;
12229 result.size = size;
12230 return result;
12231}
12232
12233/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12234 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12235 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12236 This is for DWP version 2 files. */
12237
12238static struct dwo_unit *
976ca316 12239create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12240 struct dwp_file *dwp_file,
73869dc2
DE
12241 uint32_t unit_index,
12242 const char *comp_dir,
12243 ULONGEST signature, int is_debug_types)
12244{
73869dc2
DE
12245 const struct dwp_hash_table *dwp_htab =
12246 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12247 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12248 const char *kind = is_debug_types ? "TU" : "CU";
12249 struct dwo_file *dwo_file;
12250 struct dwo_unit *dwo_unit;
d2854d8d 12251 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12252 void **dwo_file_slot;
73869dc2
DE
12253 int i;
12254
12255 gdb_assert (dwp_file->version == 2);
12256
6f738b01
SM
12257 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12258 kind, pulongest (unit_index), hex_string (signature),
12259 dwp_file->name);
73869dc2
DE
12260
12261 /* Fetch the section offsets of this DWO unit. */
12262
12263 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12264
12265 for (i = 0; i < dwp_htab->nr_columns; ++i)
12266 {
12267 uint32_t offset = read_4_bytes (dbfd,
12268 dwp_htab->section_pool.v2.offsets
12269 + (((unit_index - 1) * dwp_htab->nr_columns
12270 + i)
12271 * sizeof (uint32_t)));
12272 uint32_t size = read_4_bytes (dbfd,
12273 dwp_htab->section_pool.v2.sizes
12274 + (((unit_index - 1) * dwp_htab->nr_columns
12275 + i)
12276 * sizeof (uint32_t)));
12277
12278 switch (dwp_htab->section_pool.v2.section_ids[i])
12279 {
12280 case DW_SECT_INFO:
12281 case DW_SECT_TYPES:
12282 sections.info_or_types_offset = offset;
12283 sections.info_or_types_size = size;
12284 break;
12285 case DW_SECT_ABBREV:
12286 sections.abbrev_offset = offset;
12287 sections.abbrev_size = size;
12288 break;
12289 case DW_SECT_LINE:
12290 sections.line_offset = offset;
12291 sections.line_size = size;
12292 break;
12293 case DW_SECT_LOC:
12294 sections.loc_offset = offset;
12295 sections.loc_size = size;
12296 break;
12297 case DW_SECT_STR_OFFSETS:
12298 sections.str_offsets_offset = offset;
12299 sections.str_offsets_size = size;
12300 break;
12301 case DW_SECT_MACINFO:
12302 sections.macinfo_offset = offset;
12303 sections.macinfo_size = size;
12304 break;
12305 case DW_SECT_MACRO:
12306 sections.macro_offset = offset;
12307 sections.macro_size = size;
12308 break;
12309 }
12310 }
12311
12312 /* It's easier for the rest of the code if we fake a struct dwo_file and
12313 have dwo_unit "live" in that. At least for now.
12314
12315 The DWP file can be made up of a random collection of CUs and TUs.
12316 However, for each CU + set of TUs that came from the same original DWO
12317 file, we can combine them back into a virtual DWO file to save space
12318 (fewer struct dwo_file objects to allocate). Remember that for really
12319 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12320
791afaa2
TT
12321 std::string virtual_dwo_name =
12322 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12323 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12324 (long) (sections.line_size ? sections.line_offset : 0),
12325 (long) (sections.loc_size ? sections.loc_offset : 0),
12326 (long) (sections.str_offsets_size
12327 ? sections.str_offsets_offset : 0));
73869dc2 12328 /* Can we use an existing virtual DWO file? */
976ca316 12329 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12330 comp_dir);
73869dc2
DE
12331 /* Create one if necessary. */
12332 if (*dwo_file_slot == NULL)
12333 {
6f738b01
SM
12334 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12335 virtual_dwo_name.c_str ());
12336
51ac9db5 12337 dwo_file = new struct dwo_file;
976ca316 12338 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12339 dwo_file->comp_dir = comp_dir;
12340 dwo_file->sections.abbrev =
d2854d8d
CT
12341 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12342 sections.abbrev_offset,
12343 sections.abbrev_size);
73869dc2 12344 dwo_file->sections.line =
d2854d8d
CT
12345 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12346 sections.line_offset,
12347 sections.line_size);
73869dc2 12348 dwo_file->sections.loc =
d2854d8d
CT
12349 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12350 sections.loc_offset, sections.loc_size);
73869dc2 12351 dwo_file->sections.macinfo =
d2854d8d
CT
12352 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12353 sections.macinfo_offset,
12354 sections.macinfo_size);
73869dc2 12355 dwo_file->sections.macro =
d2854d8d
CT
12356 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12357 sections.macro_offset,
12358 sections.macro_size);
73869dc2 12359 dwo_file->sections.str_offsets =
d2854d8d
CT
12360 create_dwp_v2_or_v5_section (per_objfile,
12361 &dwp_file->sections.str_offsets,
12362 sections.str_offsets_offset,
12363 sections.str_offsets_size);
73869dc2
DE
12364 /* The "str" section is global to the entire DWP file. */
12365 dwo_file->sections.str = dwp_file->sections.str;
12366 /* The info or types section is assigned below to dwo_unit,
12367 there's no need to record it in dwo_file.
12368 Also, we can't simply record type sections in dwo_file because
12369 we record a pointer into the vector in dwo_unit. As we collect more
12370 types we'll grow the vector and eventually have to reallocate space
12371 for it, invalidating all copies of pointers into the previous
12372 contents. */
12373 *dwo_file_slot = dwo_file;
12374 }
12375 else
12376 {
6f738b01
SM
12377 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12378 virtual_dwo_name.c_str ());
12379
9a3c8263 12380 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12381 }
73869dc2 12382
976ca316 12383 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12384 dwo_unit->dwo_file = dwo_file;
12385 dwo_unit->signature = signature;
8d749320 12386 dwo_unit->section =
976ca316 12387 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12388 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12389 (per_objfile,
d2854d8d
CT
12390 is_debug_types
12391 ? &dwp_file->sections.types
12392 : &dwp_file->sections.info,
12393 sections.info_or_types_offset,
12394 sections.info_or_types_size);
12395 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12396
12397 return dwo_unit;
12398}
12399
12400/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12401 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12402 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12403 This is for DWP version 5 files. */
12404
12405static struct dwo_unit *
12406create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12407 struct dwp_file *dwp_file,
12408 uint32_t unit_index,
12409 const char *comp_dir,
12410 ULONGEST signature, int is_debug_types)
12411{
12412 const struct dwp_hash_table *dwp_htab
12413 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12414 bfd *dbfd = dwp_file->dbfd.get ();
12415 const char *kind = is_debug_types ? "TU" : "CU";
12416 struct dwo_file *dwo_file;
12417 struct dwo_unit *dwo_unit;
12418 struct virtual_v2_or_v5_dwo_sections sections {};
12419 void **dwo_file_slot;
12420
12421 gdb_assert (dwp_file->version == 5);
12422
6f738b01
SM
12423 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12424 kind, pulongest (unit_index), hex_string (signature),
12425 dwp_file->name);
d2854d8d
CT
12426
12427 /* Fetch the section offsets of this DWO unit. */
12428
12429 /* memset (&sections, 0, sizeof (sections)); */
12430
12431 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12432 {
12433 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12434 dwp_htab->section_pool.v5.offsets
12435 + (((unit_index - 1)
12436 * dwp_htab->nr_columns
12437 + i)
12438 * sizeof (uint32_t)));
d2854d8d 12439 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12440 dwp_htab->section_pool.v5.sizes
12441 + (((unit_index - 1) * dwp_htab->nr_columns
12442 + i)
12443 * sizeof (uint32_t)));
d2854d8d
CT
12444
12445 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12446 {
12447 case DW_SECT_ABBREV_V5:
12448 sections.abbrev_offset = offset;
12449 sections.abbrev_size = size;
12450 break;
12451 case DW_SECT_INFO_V5:
12452 sections.info_or_types_offset = offset;
12453 sections.info_or_types_size = size;
12454 break;
12455 case DW_SECT_LINE_V5:
12456 sections.line_offset = offset;
12457 sections.line_size = size;
12458 break;
12459 case DW_SECT_LOCLISTS_V5:
12460 sections.loclists_offset = offset;
12461 sections.loclists_size = size;
12462 break;
12463 case DW_SECT_MACRO_V5:
12464 sections.macro_offset = offset;
12465 sections.macro_size = size;
12466 break;
12467 case DW_SECT_RNGLISTS_V5:
12468 sections.rnglists_offset = offset;
12469 sections.rnglists_size = size;
12470 break;
12471 case DW_SECT_STR_OFFSETS_V5:
12472 sections.str_offsets_offset = offset;
12473 sections.str_offsets_size = size;
12474 break;
12475 case DW_SECT_RESERVED_V5:
12476 default:
12477 break;
12478 }
d2854d8d
CT
12479 }
12480
12481 /* It's easier for the rest of the code if we fake a struct dwo_file and
12482 have dwo_unit "live" in that. At least for now.
12483
12484 The DWP file can be made up of a random collection of CUs and TUs.
12485 However, for each CU + set of TUs that came from the same original DWO
12486 file, we can combine them back into a virtual DWO file to save space
12487 (fewer struct dwo_file objects to allocate). Remember that for really
12488 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12489
12490 std::string virtual_dwo_name =
12491 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12492 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12493 (long) (sections.line_size ? sections.line_offset : 0),
12494 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12495 (long) (sections.str_offsets_size
12496 ? sections.str_offsets_offset : 0),
12497 (long) (sections.macro_size ? sections.macro_offset : 0),
12498 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12499 /* Can we use an existing virtual DWO file? */
12500 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12501 virtual_dwo_name.c_str (),
12502 comp_dir);
d2854d8d
CT
12503 /* Create one if necessary. */
12504 if (*dwo_file_slot == NULL)
12505 {
6f738b01
SM
12506 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12507 virtual_dwo_name.c_str ());
12508
d2854d8d
CT
12509 dwo_file = new struct dwo_file;
12510 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12511 dwo_file->comp_dir = comp_dir;
12512 dwo_file->sections.abbrev =
dda83cd7
SM
12513 create_dwp_v2_or_v5_section (per_objfile,
12514 &dwp_file->sections.abbrev,
12515 sections.abbrev_offset,
12516 sections.abbrev_size);
d2854d8d 12517 dwo_file->sections.line =
dda83cd7
SM
12518 create_dwp_v2_or_v5_section (per_objfile,
12519 &dwp_file->sections.line,
12520 sections.line_offset, sections.line_size);
d2854d8d 12521 dwo_file->sections.macro =
dda83cd7
SM
12522 create_dwp_v2_or_v5_section (per_objfile,
12523 &dwp_file->sections.macro,
12524 sections.macro_offset,
12525 sections.macro_size);
d2854d8d 12526 dwo_file->sections.loclists =
dda83cd7
SM
12527 create_dwp_v2_or_v5_section (per_objfile,
12528 &dwp_file->sections.loclists,
12529 sections.loclists_offset,
12530 sections.loclists_size);
d2854d8d 12531 dwo_file->sections.rnglists =
dda83cd7
SM
12532 create_dwp_v2_or_v5_section (per_objfile,
12533 &dwp_file->sections.rnglists,
12534 sections.rnglists_offset,
12535 sections.rnglists_size);
d2854d8d 12536 dwo_file->sections.str_offsets =
dda83cd7
SM
12537 create_dwp_v2_or_v5_section (per_objfile,
12538 &dwp_file->sections.str_offsets,
12539 sections.str_offsets_offset,
12540 sections.str_offsets_size);
d2854d8d
CT
12541 /* The "str" section is global to the entire DWP file. */
12542 dwo_file->sections.str = dwp_file->sections.str;
12543 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12544 there's no need to record it in dwo_file.
12545 Also, we can't simply record type sections in dwo_file because
12546 we record a pointer into the vector in dwo_unit. As we collect more
12547 types we'll grow the vector and eventually have to reallocate space
12548 for it, invalidating all copies of pointers into the previous
12549 contents. */
d2854d8d
CT
12550 *dwo_file_slot = dwo_file;
12551 }
12552 else
12553 {
6f738b01
SM
12554 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12555 virtual_dwo_name.c_str ());
12556
d2854d8d
CT
12557 dwo_file = (struct dwo_file *) *dwo_file_slot;
12558 }
12559
12560 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12561 dwo_unit->dwo_file = dwo_file;
12562 dwo_unit->signature = signature;
12563 dwo_unit->section
12564 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12565 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12566 &dwp_file->sections.info,
12567 sections.info_or_types_offset,
12568 sections.info_or_types_size);
73869dc2
DE
12569 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12570
12571 return dwo_unit;
12572}
12573
57d63ce2
DE
12574/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12575 Returns NULL if the signature isn't found. */
80626a55
DE
12576
12577static struct dwo_unit *
976ca316 12578lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12579 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12580 ULONGEST signature, int is_debug_types)
80626a55 12581{
57d63ce2
DE
12582 const struct dwp_hash_table *dwp_htab =
12583 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12584 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12585 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12586 uint32_t hash = signature & mask;
12587 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12588 unsigned int i;
12589 void **slot;
870f88f7 12590 struct dwo_unit find_dwo_cu;
80626a55
DE
12591
12592 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12593 find_dwo_cu.signature = signature;
19ac8c2e 12594 slot = htab_find_slot (is_debug_types
48b490f2
TT
12595 ? dwp_file->loaded_tus.get ()
12596 : dwp_file->loaded_cus.get (),
19ac8c2e 12597 &find_dwo_cu, INSERT);
80626a55
DE
12598
12599 if (*slot != NULL)
9a3c8263 12600 return (struct dwo_unit *) *slot;
80626a55
DE
12601
12602 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12603 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12604 {
12605 ULONGEST signature_in_table;
12606
12607 signature_in_table =
57d63ce2 12608 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12609 if (signature_in_table == signature)
12610 {
57d63ce2
DE
12611 uint32_t unit_index =
12612 read_4_bytes (dbfd,
12613 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12614
73869dc2
DE
12615 if (dwp_file->version == 1)
12616 {
976ca316
SM
12617 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12618 unit_index, comp_dir,
12619 signature, is_debug_types);
73869dc2 12620 }
d2854d8d 12621 else if (dwp_file->version == 2)
73869dc2 12622 {
976ca316
SM
12623 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12624 unit_index, comp_dir,
12625 signature, is_debug_types);
73869dc2 12626 }
d2854d8d
CT
12627 else /* version == 5 */
12628 {
12629 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12630 unit_index, comp_dir,
12631 signature, is_debug_types);
12632 }
9a3c8263 12633 return (struct dwo_unit *) *slot;
80626a55
DE
12634 }
12635 if (signature_in_table == 0)
12636 return NULL;
12637 hash = (hash + hash2) & mask;
12638 }
12639
12640 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12641 " [in module %s]"),
12642 dwp_file->name);
12643}
12644
ab5088bf 12645/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12646 Open the file specified by FILE_NAME and hand it off to BFD for
12647 preliminary analysis. Return a newly initialized bfd *, which
12648 includes a canonicalized copy of FILE_NAME.
80626a55 12649 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12650 SEARCH_CWD is true if the current directory is to be searched.
12651 It will be searched before debug-file-directory.
13aaf454
DE
12652 If successful, the file is added to the bfd include table of the
12653 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12654 If unable to find/open the file, return NULL.
3019eac3
DE
12655 NOTE: This function is derived from symfile_bfd_open. */
12656
192b62ce 12657static gdb_bfd_ref_ptr
976ca316 12658try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12659 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12660{
24b9144d 12661 int desc;
9c02c129
DE
12662 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12663 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12664 to debug_file_directory. */
e0cc99a6 12665 const char *search_path;
9c02c129
DE
12666 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12667
e0cc99a6 12668 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12669 if (search_cwd)
12670 {
12671 if (*debug_file_directory != '\0')
e0cc99a6
TT
12672 {
12673 search_path_holder.reset (concat (".", dirname_separator_string,
12674 debug_file_directory,
12675 (char *) NULL));
12676 search_path = search_path_holder.get ();
12677 }
6ac97d4c 12678 else
e0cc99a6 12679 search_path = ".";
6ac97d4c 12680 }
9c02c129 12681 else
e0cc99a6 12682 search_path = debug_file_directory;
3019eac3 12683
24b9144d 12684 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12685 if (is_dwp)
12686 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12687
12688 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12689 desc = openp (search_path, flags, file_name,
3019eac3
DE
12690 O_RDONLY | O_BINARY, &absolute_name);
12691 if (desc < 0)
12692 return NULL;
12693
e0cc99a6
TT
12694 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12695 gnutarget, desc));
9c02c129
DE
12696 if (sym_bfd == NULL)
12697 return NULL;
192b62ce 12698 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12699
192b62ce
TT
12700 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12701 return NULL;
3019eac3 12702
13aaf454
DE
12703 /* Success. Record the bfd as having been included by the objfile's bfd.
12704 This is important because things like demangled_names_hash lives in the
12705 objfile's per_bfd space and may have references to things like symbol
12706 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12707 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12708
3019eac3
DE
12709 return sym_bfd;
12710}
12711
ab5088bf 12712/* Try to open DWO file FILE_NAME.
3019eac3
DE
12713 COMP_DIR is the DW_AT_comp_dir attribute.
12714 The result is the bfd handle of the file.
12715 If there is a problem finding or opening the file, return NULL.
12716 Upon success, the canonicalized path of the file is stored in the bfd,
12717 same as symfile_bfd_open. */
12718
192b62ce 12719static gdb_bfd_ref_ptr
976ca316 12720open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12721 const char *file_name, const char *comp_dir)
3019eac3 12722{
80626a55 12723 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12724 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12725 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12726
12727 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12728
12729 if (comp_dir != NULL)
12730 {
43816ebc
TT
12731 gdb::unique_xmalloc_ptr<char> path_to_try
12732 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12733
12734 /* NOTE: If comp_dir is a relative path, this will also try the
12735 search path, which seems useful. */
976ca316 12736 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12737 0 /*is_dwp*/,
192b62ce 12738 1 /*search_cwd*/));
3019eac3
DE
12739 if (abfd != NULL)
12740 return abfd;
12741 }
12742
12743 /* That didn't work, try debug-file-directory, which, despite its name,
12744 is a list of paths. */
12745
12746 if (*debug_file_directory == '\0')
12747 return NULL;
12748
976ca316 12749 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12750 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12751}
12752
80626a55
DE
12753/* This function is mapped across the sections and remembers the offset and
12754 size of each of the DWO debugging sections we are interested in. */
12755
12756static void
5bb6e9dd
TT
12757dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12758 dwo_sections *dwo_sections)
80626a55 12759{
80626a55
DE
12760 const struct dwop_section_names *names = &dwop_section_names;
12761
fbedd546 12762 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12763 {
049412e3 12764 dwo_sections->abbrev.s.section = sectp;
fd361982 12765 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12766 }
fbedd546 12767 else if (names->info_dwo.matches (sectp->name))
80626a55 12768 {
049412e3 12769 dwo_sections->info.s.section = sectp;
fd361982 12770 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12771 }
fbedd546 12772 else if (names->line_dwo.matches (sectp->name))
80626a55 12773 {
049412e3 12774 dwo_sections->line.s.section = sectp;
fd361982 12775 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12776 }
fbedd546 12777 else if (names->loc_dwo.matches (sectp->name))
80626a55 12778 {
049412e3 12779 dwo_sections->loc.s.section = sectp;
fd361982 12780 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12781 }
fbedd546 12782 else if (names->loclists_dwo.matches (sectp->name))
41144253 12783 {
12784 dwo_sections->loclists.s.section = sectp;
12785 dwo_sections->loclists.size = bfd_section_size (sectp);
12786 }
fbedd546 12787 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12788 {
049412e3 12789 dwo_sections->macinfo.s.section = sectp;
fd361982 12790 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12791 }
fbedd546 12792 else if (names->macro_dwo.matches (sectp->name))
80626a55 12793 {
049412e3 12794 dwo_sections->macro.s.section = sectp;
fd361982 12795 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12796 }
fbedd546 12797 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12798 {
12799 dwo_sections->rnglists.s.section = sectp;
12800 dwo_sections->rnglists.size = bfd_section_size (sectp);
12801 }
fbedd546 12802 else if (names->str_dwo.matches (sectp->name))
80626a55 12803 {
049412e3 12804 dwo_sections->str.s.section = sectp;
fd361982 12805 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12806 }
fbedd546 12807 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12808 {
049412e3 12809 dwo_sections->str_offsets.s.section = sectp;
fd361982 12810 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12811 }
fbedd546 12812 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12813 {
12814 struct dwarf2_section_info type_section;
12815
12816 memset (&type_section, 0, sizeof (type_section));
049412e3 12817 type_section.s.section = sectp;
fd361982 12818 type_section.size = bfd_section_size (sectp);
fd5866f6 12819 dwo_sections->types.push_back (type_section);
80626a55
DE
12820 }
12821}
12822
ab5088bf 12823/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12824 by PER_CU. This is for the non-DWP case.
80626a55 12825 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12826
12827static struct dwo_file *
4ab09049
SM
12828open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12829 const char *comp_dir)
3019eac3 12830{
976ca316 12831 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12832
976ca316 12833 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12834 if (dbfd == NULL)
12835 {
6f738b01
SM
12836 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12837
80626a55
DE
12838 return NULL;
12839 }
263db9a1 12840
51ac9db5 12841 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12842 dwo_file->dwo_name = dwo_name;
12843 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12844 dwo_file->dbfd = std::move (dbfd);
3019eac3 12845
5bb6e9dd
TT
12846 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12847 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12848 &dwo_file->sections);
3019eac3 12849
976ca316
SM
12850 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12851 dwo_file->cus);
3019eac3 12852
d2854d8d
CT
12853 if (cu->per_cu->dwarf_version < 5)
12854 {
12855 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12856 dwo_file->sections.types, dwo_file->tus);
12857 }
12858 else
12859 {
12860 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12861 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12862 rcuh_kind::COMPILE);
d2854d8d 12863 }
3019eac3 12864
6f738b01 12865 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12866
263db9a1 12867 return dwo_file.release ();
3019eac3
DE
12868}
12869
80626a55 12870/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12871 size of each of the DWP debugging sections common to version 1 and 2 that
12872 we are interested in. */
3019eac3 12873
80626a55 12874static void
73869dc2 12875dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12876 dwp_file *dwp_file)
3019eac3 12877{
80626a55
DE
12878 const struct dwop_section_names *names = &dwop_section_names;
12879 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12880
80626a55 12881 /* Record the ELF section number for later lookup: this is what the
73869dc2 12882 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12883 gdb_assert (elf_section_nr < dwp_file->num_sections);
12884 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12885
80626a55 12886 /* Look for specific sections that we need. */
fbedd546 12887 if (names->str_dwo.matches (sectp->name))
80626a55 12888 {
049412e3 12889 dwp_file->sections.str.s.section = sectp;
fd361982 12890 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12891 }
fbedd546 12892 else if (names->cu_index.matches (sectp->name))
80626a55 12893 {
049412e3 12894 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12895 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12896 }
fbedd546 12897 else if (names->tu_index.matches (sectp->name))
80626a55 12898 {
049412e3 12899 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12900 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12901 }
12902}
3019eac3 12903
73869dc2
DE
12904/* This function is mapped across the sections and remembers the offset and
12905 size of each of the DWP version 2 debugging sections that we are interested
12906 in. This is split into a separate function because we don't know if we
d2854d8d 12907 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12908
12909static void
12910dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12911{
9a3c8263 12912 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12913 const struct dwop_section_names *names = &dwop_section_names;
12914 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12915
12916 /* Record the ELF section number for later lookup: this is what the
12917 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12918 gdb_assert (elf_section_nr < dwp_file->num_sections);
12919 dwp_file->elf_sections[elf_section_nr] = sectp;
12920
12921 /* Look for specific sections that we need. */
fbedd546 12922 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12923 {
049412e3 12924 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12925 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12926 }
fbedd546 12927 else if (names->info_dwo.matches (sectp->name))
73869dc2 12928 {
049412e3 12929 dwp_file->sections.info.s.section = sectp;
fd361982 12930 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12931 }
fbedd546 12932 else if (names->line_dwo.matches (sectp->name))
73869dc2 12933 {
049412e3 12934 dwp_file->sections.line.s.section = sectp;
fd361982 12935 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12936 }
fbedd546 12937 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12938 {
049412e3 12939 dwp_file->sections.loc.s.section = sectp;
fd361982 12940 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12941 }
fbedd546 12942 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12943 {
049412e3 12944 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12945 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12946 }
fbedd546 12947 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12948 {
049412e3 12949 dwp_file->sections.macro.s.section = sectp;
fd361982 12950 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12951 }
fbedd546 12952 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12953 {
049412e3 12954 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12955 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12956 }
fbedd546 12957 else if (names->types_dwo.matches (sectp->name))
73869dc2 12958 {
049412e3 12959 dwp_file->sections.types.s.section = sectp;
fd361982 12960 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12961 }
12962}
12963
d2854d8d
CT
12964/* This function is mapped across the sections and remembers the offset and
12965 size of each of the DWP version 5 debugging sections that we are interested
12966 in. This is split into a separate function because we don't know if we
12967 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12968
12969static void
12970dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12971{
12972 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12973 const struct dwop_section_names *names = &dwop_section_names;
12974 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12975
12976 /* Record the ELF section number for later lookup: this is what the
12977 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12978 gdb_assert (elf_section_nr < dwp_file->num_sections);
12979 dwp_file->elf_sections[elf_section_nr] = sectp;
12980
12981 /* Look for specific sections that we need. */
fbedd546 12982 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12983 {
12984 dwp_file->sections.abbrev.s.section = sectp;
12985 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12986 }
fbedd546 12987 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12988 {
12989 dwp_file->sections.info.s.section = sectp;
12990 dwp_file->sections.info.size = bfd_section_size (sectp);
12991 }
fbedd546 12992 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12993 {
12994 dwp_file->sections.line.s.section = sectp;
12995 dwp_file->sections.line.size = bfd_section_size (sectp);
12996 }
fbedd546 12997 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
12998 {
12999 dwp_file->sections.loclists.s.section = sectp;
13000 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13001 }
fbedd546 13002 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
13003 {
13004 dwp_file->sections.macro.s.section = sectp;
13005 dwp_file->sections.macro.size = bfd_section_size (sectp);
13006 }
fbedd546 13007 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
13008 {
13009 dwp_file->sections.rnglists.s.section = sectp;
13010 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13011 }
fbedd546 13012 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
13013 {
13014 dwp_file->sections.str_offsets.s.section = sectp;
13015 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13016 }
13017}
13018
80626a55 13019/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13020
80626a55
DE
13021static hashval_t
13022hash_dwp_loaded_cutus (const void *item)
13023{
9a3c8263 13024 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13025
80626a55
DE
13026 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13027 return dwo_unit->signature;
3019eac3
DE
13028}
13029
80626a55 13030/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13031
80626a55
DE
13032static int
13033eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13034{
9a3c8263
SM
13035 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13036 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13037
80626a55
DE
13038 return dua->signature == dub->signature;
13039}
3019eac3 13040
80626a55 13041/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13042
48b490f2 13043static htab_up
298e9637 13044allocate_dwp_loaded_cutus_table ()
80626a55 13045{
48b490f2
TT
13046 return htab_up (htab_create_alloc (3,
13047 hash_dwp_loaded_cutus,
13048 eq_dwp_loaded_cutus,
13049 NULL, xcalloc, xfree));
80626a55 13050}
3019eac3 13051
ab5088bf
DE
13052/* Try to open DWP file FILE_NAME.
13053 The result is the bfd handle of the file.
13054 If there is a problem finding or opening the file, return NULL.
13055 Upon success, the canonicalized path of the file is stored in the bfd,
13056 same as symfile_bfd_open. */
13057
192b62ce 13058static gdb_bfd_ref_ptr
976ca316 13059open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 13060{
976ca316 13061 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 13062 1 /*is_dwp*/,
192b62ce 13063 1 /*search_cwd*/));
6ac97d4c
DE
13064 if (abfd != NULL)
13065 return abfd;
13066
13067 /* Work around upstream bug 15652.
13068 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13069 [Whether that's a "bug" is debatable, but it is getting in our way.]
13070 We have no real idea where the dwp file is, because gdb's realpath-ing
13071 of the executable's path may have discarded the needed info.
13072 [IWBN if the dwp file name was recorded in the executable, akin to
13073 .gnu_debuglink, but that doesn't exist yet.]
13074 Strip the directory from FILE_NAME and search again. */
13075 if (*debug_file_directory != '\0')
13076 {
13077 /* Don't implicitly search the current directory here.
13078 If the user wants to search "." to handle this case,
13079 it must be added to debug-file-directory. */
976ca316
SM
13080 return try_open_dwop_file (per_objfile, lbasename (file_name),
13081 1 /*is_dwp*/,
6ac97d4c
DE
13082 0 /*search_cwd*/);
13083 }
13084
13085 return NULL;
ab5088bf
DE
13086}
13087
80626a55
DE
13088/* Initialize the use of the DWP file for the current objfile.
13089 By convention the name of the DWP file is ${objfile}.dwp.
13090 The result is NULL if it can't be found. */
a766d390 13091
400174b1 13092static std::unique_ptr<struct dwp_file>
976ca316 13093open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 13094{
976ca316 13095 struct objfile *objfile = per_objfile->objfile;
80626a55 13096
82bf32bc
JK
13097 /* Try to find first .dwp for the binary file before any symbolic links
13098 resolving. */
6c447423
DE
13099
13100 /* If the objfile is a debug file, find the name of the real binary
13101 file and get the name of dwp file from there. */
d721ba37 13102 std::string dwp_name;
6c447423
DE
13103 if (objfile->separate_debug_objfile_backlink != NULL)
13104 {
13105 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13106 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13107
d721ba37 13108 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13109 }
13110 else
d721ba37
PA
13111 dwp_name = objfile->original_name;
13112
13113 dwp_name += ".dwp";
80626a55 13114
976ca316 13115 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13116 if (dbfd == NULL
13117 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13118 {
13119 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13120 dwp_name = objfile_name (objfile);
13121 dwp_name += ".dwp";
976ca316 13122 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
13123 }
13124
80626a55
DE
13125 if (dbfd == NULL)
13126 {
6f738b01
SM
13127 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13128
400174b1 13129 return std::unique_ptr<dwp_file> ();
3019eac3 13130 }
400174b1
TT
13131
13132 const char *name = bfd_get_filename (dbfd.get ());
13133 std::unique_ptr<struct dwp_file> dwp_file
13134 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13135
0a0f4c01 13136 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 13137 dwp_file->elf_sections =
976ca316 13138 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
13139 dwp_file->num_sections, asection *);
13140
5bb6e9dd
TT
13141 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13142 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13143 dwp_file.get ());
80626a55 13144
976ca316 13145 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 13146
976ca316 13147 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 13148
73869dc2 13149 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13150 if (dwp_file->cus && dwp_file->tus
13151 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13152 {
13153 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13154 pretty bizarre. We use pulongest here because that's the established
4d65956b 13155 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13156 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13157 " TU version %s [in DWP file %s]"),
13158 pulongest (dwp_file->cus->version),
d721ba37 13159 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13160 }
08302ed2
DE
13161
13162 if (dwp_file->cus)
13163 dwp_file->version = dwp_file->cus->version;
13164 else if (dwp_file->tus)
13165 dwp_file->version = dwp_file->tus->version;
13166 else
13167 dwp_file->version = 2;
73869dc2 13168
5bb6e9dd
TT
13169 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13170 {
13171 if (dwp_file->version == 2)
13172 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13173 dwp_file.get ());
13174 else
13175 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13176 dwp_file.get ());
13177 }
73869dc2 13178
298e9637
SM
13179 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13180 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 13181
6f738b01
SM
13182 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13183 dwarf_read_debug_printf (" %s CUs, %s TUs",
13184 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13185 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13186
13187 return dwp_file;
3019eac3 13188}
c906108c 13189
ab5088bf
DE
13190/* Wrapper around open_and_init_dwp_file, only open it once. */
13191
13192static struct dwp_file *
976ca316 13193get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 13194{
976ca316 13195 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 13196 {
976ca316
SM
13197 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13198 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 13199 }
976ca316 13200 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
13201}
13202
80626a55
DE
13203/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13204 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13205 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13206 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13207 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13208
13209 This is called, for example, when wanting to read a variable with a
13210 complex location. Therefore we don't want to do file i/o for every call.
13211 Therefore we don't want to look for a DWO file on every call.
13212 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13213 then we check if we've already seen DWO_NAME, and only THEN do we check
13214 for a DWO file.
13215
1c658ad5 13216 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13217 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13218
3019eac3 13219static struct dwo_unit *
4ab09049 13220lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 13221 ULONGEST signature, int is_debug_types)
3019eac3 13222{
976ca316
SM
13223 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13224 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
13225 const char *kind = is_debug_types ? "TU" : "CU";
13226 void **dwo_file_slot;
3019eac3 13227 struct dwo_file *dwo_file;
80626a55 13228 struct dwp_file *dwp_file;
cb1df416 13229
6a506a2d
DE
13230 /* First see if there's a DWP file.
13231 If we have a DWP file but didn't find the DWO inside it, don't
13232 look for the original DWO file. It makes gdb behave differently
13233 depending on whether one is debugging in the build tree. */
cf2c3c16 13234
976ca316 13235 dwp_file = get_dwp_file (per_objfile);
80626a55 13236 if (dwp_file != NULL)
cf2c3c16 13237 {
80626a55
DE
13238 const struct dwp_hash_table *dwp_htab =
13239 is_debug_types ? dwp_file->tus : dwp_file->cus;
13240
13241 if (dwp_htab != NULL)
13242 {
13243 struct dwo_unit *dwo_cutu =
976ca316
SM
13244 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13245 is_debug_types);
80626a55
DE
13246
13247 if (dwo_cutu != NULL)
13248 {
6f738b01
SM
13249 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13250 kind, hex_string (signature),
13251 host_address_to_string (dwo_cutu));
13252
80626a55
DE
13253 return dwo_cutu;
13254 }
13255 }
13256 }
6a506a2d 13257 else
80626a55 13258 {
6a506a2d 13259 /* No DWP file, look for the DWO file. */
80626a55 13260
976ca316 13261 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13262 if (*dwo_file_slot == NULL)
80626a55 13263 {
6a506a2d 13264 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13265 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13266 }
6a506a2d 13267 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13268 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13269
6a506a2d 13270 if (dwo_file != NULL)
19c3d4c9 13271 {
6a506a2d
DE
13272 struct dwo_unit *dwo_cutu = NULL;
13273
13274 if (is_debug_types && dwo_file->tus)
13275 {
13276 struct dwo_unit find_dwo_cutu;
13277
13278 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13279 find_dwo_cutu.signature = signature;
9a3c8263 13280 dwo_cutu
b0b6a987
TT
13281 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13282 &find_dwo_cutu);
6a506a2d 13283 }
33c5cd75 13284 else if (!is_debug_types && dwo_file->cus)
80626a55 13285 {
33c5cd75
DB
13286 struct dwo_unit find_dwo_cutu;
13287
13288 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13289 find_dwo_cutu.signature = signature;
b0b6a987 13290 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13291 &find_dwo_cutu);
6a506a2d
DE
13292 }
13293
13294 if (dwo_cutu != NULL)
13295 {
6f738b01
SM
13296 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13297 kind, dwo_name, hex_string (signature),
13298 host_address_to_string (dwo_cutu));
13299
6a506a2d 13300 return dwo_cutu;
80626a55
DE
13301 }
13302 }
2e276125 13303 }
9cdd5dbd 13304
80626a55
DE
13305 /* We didn't find it. This could mean a dwo_id mismatch, or
13306 someone deleted the DWO/DWP file, or the search path isn't set up
13307 correctly to find the file. */
13308
6f738b01
SM
13309 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13310 kind, dwo_name, hex_string (signature));
3019eac3 13311
6656a72d
DE
13312 /* This is a warning and not a complaint because it can be caused by
13313 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13314 {
13315 /* Print the name of the DWP file if we looked there, helps the user
13316 better diagnose the problem. */
791afaa2 13317 std::string dwp_text;
43942612
DE
13318
13319 if (dwp_file != NULL)
791afaa2
TT
13320 dwp_text = string_printf (" [in DWP file %s]",
13321 lbasename (dwp_file->name));
43942612 13322
9d8780f0 13323 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13324 " [in module %s]"),
4ab09049
SM
13325 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13326 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13327 }
3019eac3 13328 return NULL;
5fb290d7
DJ
13329}
13330
80626a55
DE
13331/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13332 See lookup_dwo_cutu_unit for details. */
13333
13334static struct dwo_unit *
4ab09049 13335lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13336 ULONGEST signature)
13337{
4ab09049
SM
13338 gdb_assert (!cu->per_cu->is_debug_types);
13339
13340 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13341}
13342
13343/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13344 See lookup_dwo_cutu_unit for details. */
13345
13346static struct dwo_unit *
4ab09049 13347lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13348{
4ab09049
SM
13349 gdb_assert (cu->per_cu->is_debug_types);
13350
13351 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13352
13353 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13354}
13355
89e63ee4
DE
13356/* Traversal function for queue_and_load_all_dwo_tus. */
13357
13358static int
13359queue_and_load_dwo_tu (void **slot, void *info)
13360{
13361 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13362 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13363 ULONGEST signature = dwo_unit->signature;
d460f660 13364 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13365
13366 if (sig_type != NULL)
13367 {
13368 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13369
13370 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13371 a real dependency of PER_CU on SIG_TYPE. That is detected later
13372 while processing PER_CU. */
120ce1b5 13373 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13374 load_full_type_unit (sig_cu, cu->per_objfile);
13375 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13376 }
13377
13378 return 1;
13379}
13380
1b555f17 13381/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13382 The DWO may have the only definition of the type, though it may not be
13383 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13384 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13385
13386static void
1b555f17 13387queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13388{
13389 struct dwo_unit *dwo_unit;
13390 struct dwo_file *dwo_file;
13391
1b555f17
SM
13392 gdb_assert (cu != nullptr);
13393 gdb_assert (!cu->per_cu->is_debug_types);
13394 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13395
1b555f17 13396 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13397 gdb_assert (dwo_unit != NULL);
13398
13399 dwo_file = dwo_unit->dwo_file;
13400 if (dwo_file->tus != NULL)
1b555f17 13401 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13402}
13403
3019eac3 13404/* Read in various DIEs. */
348e048f 13405
d389af10 13406/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13407 Inherit only the children of the DW_AT_abstract_origin DIE not being
13408 already referenced by DW_AT_abstract_origin from the children of the
13409 current DIE. */
d389af10
JK
13410
13411static void
13412inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13413{
13414 struct die_info *child_die;
791afaa2 13415 sect_offset *offsetp;
d389af10
JK
13416 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13417 struct die_info *origin_die;
13418 /* Iterator of the ORIGIN_DIE children. */
13419 struct die_info *origin_child_die;
d389af10 13420 struct attribute *attr;
cd02d79d
PA
13421 struct dwarf2_cu *origin_cu;
13422 struct pending **origin_previous_list_in_scope;
d389af10
JK
13423
13424 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13425 if (!attr)
13426 return;
13427
cd02d79d
PA
13428 /* Note that following die references may follow to a die in a
13429 different cu. */
13430
13431 origin_cu = cu;
13432 origin_die = follow_die_ref (die, attr, &origin_cu);
13433
13434 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13435 symbols in. */
13436 origin_previous_list_in_scope = origin_cu->list_in_scope;
13437 origin_cu->list_in_scope = cu->list_in_scope;
13438
edb3359d
DJ
13439 if (die->tag != origin_die->tag
13440 && !(die->tag == DW_TAG_inlined_subroutine
13441 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13442 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13443 sect_offset_str (die->sect_off),
13444 sect_offset_str (origin_die->sect_off));
d389af10 13445
791afaa2 13446 std::vector<sect_offset> offsets;
d389af10 13447
3ea89b92
PMR
13448 for (child_die = die->child;
13449 child_die && child_die->tag;
436c571c 13450 child_die = child_die->sibling)
3ea89b92
PMR
13451 {
13452 struct die_info *child_origin_die;
13453 struct dwarf2_cu *child_origin_cu;
13454
13455 /* We are trying to process concrete instance entries:
216f72a1 13456 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13457 it's not relevant to our analysis here. i.e. detecting DIEs that are
13458 present in the abstract instance but not referenced in the concrete
13459 one. */
216f72a1 13460 if (child_die->tag == DW_TAG_call_site
dda83cd7 13461 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13462 continue;
13463
c38f313d
DJ
13464 /* For each CHILD_DIE, find the corresponding child of
13465 ORIGIN_DIE. If there is more than one layer of
13466 DW_AT_abstract_origin, follow them all; there shouldn't be,
13467 but GCC versions at least through 4.4 generate this (GCC PR
13468 40573). */
3ea89b92
PMR
13469 child_origin_die = child_die;
13470 child_origin_cu = cu;
c38f313d
DJ
13471 while (1)
13472 {
cd02d79d
PA
13473 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13474 child_origin_cu);
c38f313d
DJ
13475 if (attr == NULL)
13476 break;
cd02d79d
PA
13477 child_origin_die = follow_die_ref (child_origin_die, attr,
13478 &child_origin_cu);
c38f313d
DJ
13479 }
13480
d389af10
JK
13481 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13482 counterpart may exist. */
c38f313d 13483 if (child_origin_die != child_die)
d389af10 13484 {
edb3359d
DJ
13485 if (child_die->tag != child_origin_die->tag
13486 && !(child_die->tag == DW_TAG_inlined_subroutine
13487 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13488 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13489 "different tags"),
9d8780f0
SM
13490 sect_offset_str (child_die->sect_off),
13491 sect_offset_str (child_origin_die->sect_off));
c38f313d 13492 if (child_origin_die->parent != origin_die)
b98664d3 13493 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13494 "different parents"),
9d8780f0
SM
13495 sect_offset_str (child_die->sect_off),
13496 sect_offset_str (child_origin_die->sect_off));
c38f313d 13497 else
791afaa2 13498 offsets.push_back (child_origin_die->sect_off);
d389af10 13499 }
d389af10 13500 }
791afaa2
TT
13501 std::sort (offsets.begin (), offsets.end ());
13502 sect_offset *offsets_end = offsets.data () + offsets.size ();
13503 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13504 if (offsetp[-1] == *offsetp)
b98664d3 13505 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13506 "to DIE %s as their abstract origin"),
13507 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13508
791afaa2 13509 offsetp = offsets.data ();
d389af10
JK
13510 origin_child_die = origin_die->child;
13511 while (origin_child_die && origin_child_die->tag)
13512 {
13513 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13514 while (offsetp < offsets_end
9c541725 13515 && *offsetp < origin_child_die->sect_off)
d389af10 13516 offsetp++;
b64f50a1 13517 if (offsetp >= offsets_end
9c541725 13518 || *offsetp > origin_child_die->sect_off)
d389af10 13519 {
adde2bff
DE
13520 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13521 Check whether we're already processing ORIGIN_CHILD_DIE.
13522 This can happen with mutually referenced abstract_origins.
13523 PR 16581. */
13524 if (!origin_child_die->in_process)
13525 process_die (origin_child_die, origin_cu);
d389af10 13526 }
436c571c 13527 origin_child_die = origin_child_die->sibling;
d389af10 13528 }
cd02d79d 13529 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13530
13531 if (cu != origin_cu)
13532 compute_delayed_physnames (origin_cu);
d389af10
JK
13533}
13534
c906108c 13535static void
e7c27a73 13536read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13537{
5e22e966 13538 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13539 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13540 struct context_stack *newobj;
c906108c
SS
13541 CORE_ADDR lowpc;
13542 CORE_ADDR highpc;
13543 struct die_info *child_die;
edb3359d 13544 struct attribute *attr, *call_line, *call_file;
15d034d0 13545 const char *name;
e142c38c 13546 CORE_ADDR baseaddr;
801e3a5b 13547 struct block *block;
edb3359d 13548 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13549 std::vector<struct symbol *> template_args;
34eaf542 13550 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13551
13552 if (inlined_func)
13553 {
13554 /* If we do not have call site information, we can't show the
13555 caller of this inlined function. That's too confusing, so
13556 only use the scope for local variables. */
13557 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13558 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13559 if (call_line == NULL || call_file == NULL)
13560 {
13561 read_lexical_block_scope (die, cu);
13562 return;
13563 }
13564 }
c906108c 13565
b3b3bada 13566 baseaddr = objfile->text_section_offset ();
e142c38c 13567
94af9270 13568 name = dwarf2_name (die, cu);
c906108c 13569
e8d05480
JB
13570 /* Ignore functions with missing or empty names. These are actually
13571 illegal according to the DWARF standard. */
13572 if (name == NULL)
13573 {
b98664d3 13574 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13575 sect_offset_str (die->sect_off));
e8d05480
JB
13576 return;
13577 }
13578
13579 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13580 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13581 <= PC_BOUNDS_INVALID)
e8d05480 13582 {
ae4d0c03 13583 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13584 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13585 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13586 "for subprogram DIE at %s"),
13587 sect_offset_str (die->sect_off));
e8d05480
JB
13588 return;
13589 }
c906108c 13590
3e29f34a
MR
13591 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13592 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13593
34eaf542
TT
13594 /* If we have any template arguments, then we must allocate a
13595 different sort of symbol. */
436c571c 13596 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13597 {
13598 if (child_die->tag == DW_TAG_template_type_param
13599 || child_die->tag == DW_TAG_template_value_param)
13600 {
8c14c3a3 13601 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13602 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13603 break;
13604 }
13605 }
13606
c24bdb02 13607 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13608 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13609 (struct symbol *) templ_func);
4c2df51b 13610
81873cc8 13611 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13612 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13613 cu->language);
13614
4cecd739
DJ
13615 /* If there is a location expression for DW_AT_frame_base, record
13616 it. */
e142c38c 13617 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13618 if (attr != nullptr)
fe978cb0 13619 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13620
63e43d3a
PMR
13621 /* If there is a location for the static link, record it. */
13622 newobj->static_link = NULL;
13623 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13624 if (attr != nullptr)
63e43d3a 13625 {
224c3ddb
SM
13626 newobj->static_link
13627 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13628 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13629 cu->addr_type ());
63e43d3a
PMR
13630 }
13631
c24bdb02 13632 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13633
639d11d3 13634 if (die->child != NULL)
c906108c 13635 {
639d11d3 13636 child_die = die->child;
c906108c
SS
13637 while (child_die && child_die->tag)
13638 {
34eaf542
TT
13639 if (child_die->tag == DW_TAG_template_type_param
13640 || child_die->tag == DW_TAG_template_value_param)
13641 {
13642 struct symbol *arg = new_symbol (child_die, NULL, cu);
13643
f1078f66 13644 if (arg != NULL)
2f4732b0 13645 template_args.push_back (arg);
34eaf542
TT
13646 }
13647 else
13648 process_die (child_die, cu);
436c571c 13649 child_die = child_die->sibling;
c906108c
SS
13650 }
13651 }
13652
d389af10
JK
13653 inherit_abstract_dies (die, cu);
13654
4a811a97
UW
13655 /* If we have a DW_AT_specification, we might need to import using
13656 directives from the context of the specification DIE. See the
13657 comment in determine_prefix. */
13658 if (cu->language == language_cplus
13659 && dwarf2_attr (die, DW_AT_specification, cu))
13660 {
13661 struct dwarf2_cu *spec_cu = cu;
13662 struct die_info *spec_die = die_specification (die, &spec_cu);
13663
13664 while (spec_die)
13665 {
13666 child_die = spec_die->child;
13667 while (child_die && child_die->tag)
13668 {
13669 if (child_die->tag == DW_TAG_imported_module)
13670 process_die (child_die, spec_cu);
436c571c 13671 child_die = child_die->sibling;
4a811a97
UW
13672 }
13673
13674 /* In some cases, GCC generates specification DIEs that
13675 themselves contain DW_AT_specification attributes. */
13676 spec_die = die_specification (spec_die, &spec_cu);
13677 }
13678 }
13679
c24bdb02 13680 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13681 /* Make a block for the local symbols within. */
c24bdb02 13682 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13683 cstk.static_link, lowpc, highpc);
801e3a5b 13684
df8a16a1 13685 /* For C++, set the block's scope. */
45280282
IB
13686 if ((cu->language == language_cplus
13687 || cu->language == language_fortran
c44af4eb
TT
13688 || cu->language == language_d
13689 || cu->language == language_rust)
4d4ec4e5 13690 && cu->processing_has_namespace_info)
195a3f6c
TT
13691 block_set_scope (block, determine_prefix (die, cu),
13692 &objfile->objfile_obstack);
df8a16a1 13693
801e3a5b
JB
13694 /* If we have address ranges, record them. */
13695 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13696
a60f3166 13697 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13698
34eaf542 13699 /* Attach template arguments to function. */
2f4732b0 13700 if (!template_args.empty ())
34eaf542
TT
13701 {
13702 gdb_assert (templ_func != NULL);
13703
2f4732b0 13704 templ_func->n_template_arguments = template_args.size ();
34eaf542 13705 templ_func->template_arguments
dda83cd7 13706 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13707 templ_func->n_template_arguments);
34eaf542 13708 memcpy (templ_func->template_arguments,
2f4732b0 13709 template_args.data (),
34eaf542 13710 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13711
13712 /* Make sure that the symtab is set on the new symbols. Even
13713 though they don't appear in this symtab directly, other parts
13714 of gdb assume that symbols do, and this is reasonably
13715 true. */
8634679f 13716 for (symbol *sym : template_args)
3e1d3d8c 13717 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13718 }
13719
208d8187
JB
13720 /* In C++, we can have functions nested inside functions (e.g., when
13721 a function declares a class that has methods). This means that
13722 when we finish processing a function scope, we may need to go
13723 back to building a containing block's symbol lists. */
c24bdb02
KS
13724 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13725 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13726
921e78cf
JB
13727 /* If we've finished processing a top-level function, subsequent
13728 symbols go in the file symbol list. */
c24bdb02
KS
13729 if (cu->get_builder ()->outermost_context_p ())
13730 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13731}
13732
13733/* Process all the DIES contained within a lexical block scope. Start
13734 a new scope, process the dies, and then close the scope. */
13735
13736static void
e7c27a73 13737read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13738{
5e22e966 13739 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13740 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13741 CORE_ADDR lowpc, highpc;
13742 struct die_info *child_die;
e142c38c
DJ
13743 CORE_ADDR baseaddr;
13744
b3b3bada 13745 baseaddr = objfile->text_section_offset ();
c906108c
SS
13746
13747 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13748 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13749 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13750 be nasty. Might be easier to properly extend generic blocks to
af34e669 13751 describe ranges. */
e385593e
JK
13752 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13753 {
13754 case PC_BOUNDS_NOT_PRESENT:
13755 /* DW_TAG_lexical_block has no attributes, process its children as if
13756 there was no wrapping by that DW_TAG_lexical_block.
13757 GCC does no longer produces such DWARF since GCC r224161. */
13758 for (child_die = die->child;
13759 child_die != NULL && child_die->tag;
436c571c 13760 child_die = child_die->sibling)
4f7bc5ed
TT
13761 {
13762 /* We might already be processing this DIE. This can happen
13763 in an unusual circumstance -- where a subroutine A
13764 appears lexically in another subroutine B, but A actually
13765 inlines B. The recursion is broken here, rather than in
13766 inherit_abstract_dies, because it seems better to simply
13767 drop concrete children here. */
13768 if (!child_die->in_process)
13769 process_die (child_die, cu);
13770 }
e385593e
JK
13771 return;
13772 case PC_BOUNDS_INVALID:
13773 return;
13774 }
3e29f34a
MR
13775 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13776 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13777
c24bdb02 13778 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13779 if (die->child != NULL)
c906108c 13780 {
639d11d3 13781 child_die = die->child;
c906108c
SS
13782 while (child_die && child_die->tag)
13783 {
e7c27a73 13784 process_die (child_die, cu);
436c571c 13785 child_die = child_die->sibling;
c906108c
SS
13786 }
13787 }
3ea89b92 13788 inherit_abstract_dies (die, cu);
c24bdb02 13789 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13790
c24bdb02
KS
13791 if (*cu->get_builder ()->get_local_symbols () != NULL
13792 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13793 {
801e3a5b 13794 struct block *block
dda83cd7 13795 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13796 cstk.start_addr, highpc);
801e3a5b
JB
13797
13798 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13799 do here, means that recording a parent's ranges entails
13800 walking across all its children's ranges as they appear in
13801 the address map, which is quadratic behavior.
13802
13803 It would be nicer to record the parent's ranges before
13804 traversing its children, simply overriding whatever you find
13805 there. But since we don't even decide whether to create a
13806 block until after we've traversed its children, that's hard
13807 to do. */
801e3a5b 13808 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13809 }
c24bdb02
KS
13810 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13811 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13812}
13813
216f72a1 13814/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13815
13816static void
13817read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13818{
5e22e966 13819 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13820 struct objfile *objfile = per_objfile->objfile;
08feed99 13821 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13822 CORE_ADDR pc, baseaddr;
13823 struct attribute *attr;
13824 struct call_site *call_site, call_site_local;
13825 void **slot;
13826 int nparams;
13827 struct die_info *child_die;
13828
b3b3bada 13829 baseaddr = objfile->text_section_offset ();
96408a79 13830
216f72a1
JK
13831 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13832 if (attr == NULL)
13833 {
13834 /* This was a pre-DWARF-5 GNU extension alias
13835 for DW_AT_call_return_pc. */
13836 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13837 }
96408a79
SA
13838 if (!attr)
13839 {
b98664d3 13840 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13841 "DIE %s [in module %s]"),
13842 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13843 return;
13844 }
95f982e5 13845 pc = attr->as_address () + baseaddr;
3e29f34a 13846 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13847
13848 if (cu->call_site_htab == NULL)
13849 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13850 NULL, &objfile->objfile_obstack,
13851 hashtab_obstack_allocate, NULL);
13852 call_site_local.pc = pc;
13853 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13854 if (*slot != NULL)
13855 {
b98664d3 13856 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13857 "DIE %s [in module %s]"),
13858 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13859 objfile_name (objfile));
96408a79
SA
13860 return;
13861 }
13862
13863 /* Count parameters at the caller. */
13864
13865 nparams = 0;
13866 for (child_die = die->child; child_die && child_die->tag;
436c571c 13867 child_die = child_die->sibling)
96408a79 13868 {
216f72a1 13869 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13870 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13871 {
b98664d3 13872 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13873 "DW_TAG_call_site child DIE %s [in module %s]"),
13874 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13875 objfile_name (objfile));
96408a79
SA
13876 continue;
13877 }
13878
13879 nparams++;
13880 }
13881
224c3ddb
SM
13882 call_site
13883 = ((struct call_site *)
13884 obstack_alloc (&objfile->objfile_obstack,
13885 sizeof (*call_site)
13886 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13887 *slot = call_site;
13888 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13889 call_site->pc = pc;
13890
216f72a1
JK
13891 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13892 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13893 {
13894 struct die_info *func_die;
13895
13896 /* Skip also over DW_TAG_inlined_subroutine. */
13897 for (func_die = die->parent;
13898 func_die && func_die->tag != DW_TAG_subprogram
13899 && func_die->tag != DW_TAG_subroutine_type;
13900 func_die = func_die->parent);
13901
216f72a1
JK
13902 /* DW_AT_call_all_calls is a superset
13903 of DW_AT_call_all_tail_calls. */
96408a79 13904 if (func_die
dda83cd7
SM
13905 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13906 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13907 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13908 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13909 {
13910 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13911 not complete. But keep CALL_SITE for look ups via call_site_htab,
13912 both the initial caller containing the real return address PC and
13913 the final callee containing the current PC of a chain of tail
13914 calls do not need to have the tail call list complete. But any
13915 function candidate for a virtual tail call frame searched via
13916 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13917 determined unambiguously. */
13918 }
13919 else
13920 {
13921 struct type *func_type = NULL;
13922
13923 if (func_die)
13924 func_type = get_die_type (func_die, cu);
13925 if (func_type != NULL)
13926 {
78134374 13927 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13928
13929 /* Enlist this call site to the function. */
13930 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13931 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13932 }
13933 else
b98664d3 13934 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13935 "DIE %s [in module %s]"),
13936 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13937 }
13938 }
13939
216f72a1
JK
13940 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13941 if (attr == NULL)
13942 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13943 if (attr == NULL)
13944 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13945 if (attr == NULL)
216f72a1
JK
13946 {
13947 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13948 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13949 }
96408a79 13950 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13951 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13952 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13953 else if (attr->form_is_block ())
96408a79
SA
13954 {
13955 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13956 struct dwarf_block *block = attr->as_block ();
96408a79 13957
8d749320 13958 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13959 dlbaton->data = block->data;
13960 dlbaton->size = block->size;
a50264ba 13961 dlbaton->per_objfile = per_objfile;
96408a79
SA
13962 dlbaton->per_cu = cu->per_cu;
13963
13964 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13965 }
cd6c91b4 13966 else if (attr->form_is_ref ())
96408a79 13967 {
96408a79
SA
13968 struct dwarf2_cu *target_cu = cu;
13969 struct die_info *target_die;
13970
ac9ec31b 13971 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13972 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13973 if (die_is_declaration (target_die, target_cu))
13974 {
7d45c7c3 13975 const char *target_physname;
9112db09
JK
13976
13977 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13978 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13979 if (target_physname == NULL)
9112db09 13980 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13981 if (target_physname == NULL)
b98664d3 13982 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13983 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13984 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13985 else
7d455152 13986 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13987 }
13988 else
13989 {
13990 CORE_ADDR lowpc;
13991
13992 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13993 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13994 <= PC_BOUNDS_INVALID)
b98664d3 13995 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13996 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13997 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13998 else
3e29f34a
MR
13999 {
14000 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14001 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14002 }
96408a79
SA
14003 }
14004 }
14005 else
b98664d3 14006 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14007 "block nor reference, for DIE %s [in module %s]"),
14008 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14009
14010 call_site->per_cu = cu->per_cu;
9f47c707 14011 call_site->per_objfile = per_objfile;
96408a79
SA
14012
14013 for (child_die = die->child;
14014 child_die && child_die->tag;
436c571c 14015 child_die = child_die->sibling)
96408a79 14016 {
96408a79 14017 struct call_site_parameter *parameter;
1788b2d3 14018 struct attribute *loc, *origin;
96408a79 14019
216f72a1 14020 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 14021 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14022 {
14023 /* Already printed the complaint above. */
14024 continue;
14025 }
14026
14027 gdb_assert (call_site->parameter_count < nparams);
14028 parameter = &call_site->parameter[call_site->parameter_count];
14029
1788b2d3
JK
14030 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14031 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14032 register is contained in DW_AT_call_value. */
96408a79 14033
24c5c679 14034 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14035 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14036 if (origin == NULL)
14037 {
14038 /* This was a pre-DWARF-5 GNU extension alias
14039 for DW_AT_call_parameter. */
14040 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14041 }
cd6c91b4 14042 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 14043 {
1788b2d3 14044 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 14045
0826b30a 14046 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 14047 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
14048 {
14049 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14050 binding can be done only inside one CU. Such referenced DIE
14051 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14052 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14053 "DW_TAG_call_site child DIE %s [in module %s]"),
14054 sect_offset_str (child_die->sect_off),
9c541725 14055 objfile_name (objfile));
d76b7dbc
JK
14056 continue;
14057 }
9c541725
PA
14058 parameter->u.param_cu_off
14059 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 14060 }
4fc6c0d5 14061 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 14062 {
b98664d3 14063 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14064 "DW_TAG_call_site child DIE %s [in module %s]"),
14065 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14066 continue;
14067 }
24c5c679 14068 else
96408a79 14069 {
9d2246fc
TT
14070 struct dwarf_block *block = loc->as_block ();
14071
24c5c679 14072 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 14073 (block->data, &block->data[block->size]);
24c5c679
JK
14074 if (parameter->u.dwarf_reg != -1)
14075 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
14076 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14077 &block->data[block->size],
24c5c679
JK
14078 &parameter->u.fb_offset))
14079 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14080 else
14081 {
b98664d3 14082 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14083 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14084 "DW_TAG_call_site child DIE %s "
24c5c679 14085 "[in module %s]"),
9d8780f0 14086 sect_offset_str (child_die->sect_off),
9c541725 14087 objfile_name (objfile));
24c5c679
JK
14088 continue;
14089 }
96408a79
SA
14090 }
14091
216f72a1
JK
14092 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14093 if (attr == NULL)
14094 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 14095 if (attr == NULL || !attr->form_is_block ())
96408a79 14096 {
b98664d3 14097 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14098 "DW_TAG_call_site child DIE %s [in module %s]"),
14099 sect_offset_str (child_die->sect_off),
9c541725 14100 objfile_name (objfile));
96408a79
SA
14101 continue;
14102 }
9d2246fc
TT
14103
14104 struct dwarf_block *block = attr->as_block ();
14105 parameter->value = block->data;
14106 parameter->value_size = block->size;
96408a79
SA
14107
14108 /* Parameters are not pre-cleared by memset above. */
14109 parameter->data_value = NULL;
14110 parameter->data_value_size = 0;
14111 call_site->parameter_count++;
14112
216f72a1
JK
14113 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14114 if (attr == NULL)
14115 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14116 if (attr != nullptr)
96408a79 14117 {
4fc6c0d5 14118 if (!attr->form_is_block ())
b98664d3 14119 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14120 "DW_TAG_call_site child DIE %s [in module %s]"),
14121 sect_offset_str (child_die->sect_off),
9c541725 14122 objfile_name (objfile));
96408a79
SA
14123 else
14124 {
9d2246fc
TT
14125 block = attr->as_block ();
14126 parameter->data_value = block->data;
14127 parameter->data_value_size = block->size;
96408a79
SA
14128 }
14129 }
14130 }
14131}
14132
71a3c369
TT
14133/* Helper function for read_variable. If DIE represents a virtual
14134 table, then return the type of the concrete object that is
14135 associated with the virtual table. Otherwise, return NULL. */
14136
14137static struct type *
14138rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14139{
14140 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14141 if (attr == NULL)
14142 return NULL;
14143
14144 /* Find the type DIE. */
14145 struct die_info *type_die = NULL;
14146 struct dwarf2_cu *type_cu = cu;
14147
cd6c91b4 14148 if (attr->form_is_ref ())
71a3c369
TT
14149 type_die = follow_die_ref (die, attr, &type_cu);
14150 if (type_die == NULL)
14151 return NULL;
14152
14153 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14154 return NULL;
14155 return die_containing_type (type_die, type_cu);
14156}
14157
14158/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14159
14160static void
14161read_variable (struct die_info *die, struct dwarf2_cu *cu)
14162{
14163 struct rust_vtable_symbol *storage = NULL;
14164
14165 if (cu->language == language_rust)
14166 {
14167 struct type *containing_type = rust_containing_type (die, cu);
14168
14169 if (containing_type != NULL)
14170 {
5e22e966 14171 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 14172
8c14c3a3 14173 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 14174 storage->concrete_type = containing_type;
cf724bc9 14175 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14176 }
14177 }
14178
e4a62c65
TV
14179 struct symbol *res = new_symbol (die, NULL, cu, storage);
14180 struct attribute *abstract_origin
14181 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14182 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14183 if (res == NULL && loc && abstract_origin)
14184 {
14185 /* We have a variable without a name, but with a location and an abstract
14186 origin. This may be a concrete instance of an abstract variable
14187 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14188 later. */
14189 struct dwarf2_cu *origin_cu = cu;
14190 struct die_info *origin_die
14191 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14192 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14193 per_objfile->per_bfd->abstract_to_concrete
14194 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14195 }
71a3c369
TT
14196}
14197
43988095
JK
14198/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14199 reading .debug_rnglists.
14200 Callback's type should be:
14201 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14202 Return true if the attributes are present and valid, otherwise,
14203 return false. */
14204
14205template <typename Callback>
14206static bool
14207dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14208 dwarf_tag tag, Callback &&callback)
43988095 14209{
976ca316
SM
14210 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14211 struct objfile *objfile = per_objfile->objfile;
43988095 14212 bfd *obfd = objfile->obfd;
43988095 14213 /* Base address selection entry. */
2b24b6e4 14214 gdb::optional<CORE_ADDR> base;
43988095 14215 const gdb_byte *buffer;
43988095
JK
14216 CORE_ADDR baseaddr;
14217 bool overflow = false;
d0ce17d8
CT
14218 ULONGEST addr_index;
14219 struct dwarf2_section_info *rnglists_section;
43988095 14220
43988095 14221 base = cu->base_address;
d0ce17d8
CT
14222 rnglists_section = cu_debug_rnglists_section (cu, tag);
14223 rnglists_section->read (objfile);
43988095 14224
d0ce17d8 14225 if (offset >= rnglists_section->size)
43988095 14226 {
b98664d3 14227 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14228 offset);
14229 return false;
14230 }
d0ce17d8 14231 buffer = rnglists_section->buffer + offset;
43988095 14232
b3b3bada 14233 baseaddr = objfile->text_section_offset ();
43988095
JK
14234
14235 while (1)
14236 {
7814882a
JK
14237 /* Initialize it due to a false compiler warning. */
14238 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14239 const gdb_byte *buf_end = (rnglists_section->buffer
14240 + rnglists_section->size);
43988095
JK
14241 unsigned int bytes_read;
14242
14243 if (buffer == buf_end)
14244 {
14245 overflow = true;
14246 break;
14247 }
14248 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14249 switch (rlet)
14250 {
14251 case DW_RLE_end_of_list:
14252 break;
14253 case DW_RLE_base_address:
14254 if (buffer + cu->header.addr_size > buf_end)
14255 {
14256 overflow = true;
14257 break;
14258 }
c8a7a66f 14259 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14260 buffer += bytes_read;
14261 break;
dda83cd7
SM
14262 case DW_RLE_base_addressx:
14263 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14264 buffer += bytes_read;
14265 base = read_addr_index (cu, addr_index);
14266 break;
43988095
JK
14267 case DW_RLE_start_length:
14268 if (buffer + cu->header.addr_size > buf_end)
14269 {
14270 overflow = true;
14271 break;
14272 }
c8a7a66f
TT
14273 range_beginning = cu->header.read_address (obfd, buffer,
14274 &bytes_read);
43988095
JK
14275 buffer += bytes_read;
14276 range_end = (range_beginning
14277 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14278 buffer += bytes_read;
14279 if (buffer > buf_end)
14280 {
14281 overflow = true;
14282 break;
14283 }
14284 break;
d0ce17d8 14285 case DW_RLE_startx_length:
dda83cd7
SM
14286 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14287 buffer += bytes_read;
14288 range_beginning = read_addr_index (cu, addr_index);
14289 if (buffer > buf_end)
14290 {
14291 overflow = true;
14292 break;
14293 }
14294 range_end = (range_beginning
14295 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14296 buffer += bytes_read;
14297 break;
43988095
JK
14298 case DW_RLE_offset_pair:
14299 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14300 buffer += bytes_read;
14301 if (buffer > buf_end)
14302 {
14303 overflow = true;
14304 break;
14305 }
14306 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14307 buffer += bytes_read;
14308 if (buffer > buf_end)
14309 {
14310 overflow = true;
14311 break;
14312 }
14313 break;
14314 case DW_RLE_start_end:
14315 if (buffer + 2 * cu->header.addr_size > buf_end)
14316 {
14317 overflow = true;
14318 break;
14319 }
c8a7a66f
TT
14320 range_beginning = cu->header.read_address (obfd, buffer,
14321 &bytes_read);
43988095 14322 buffer += bytes_read;
c8a7a66f 14323 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14324 buffer += bytes_read;
14325 break;
d0ce17d8 14326 case DW_RLE_startx_endx:
dda83cd7
SM
14327 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14328 buffer += bytes_read;
14329 range_beginning = read_addr_index (cu, addr_index);
14330 if (buffer > buf_end)
14331 {
14332 overflow = true;
14333 break;
14334 }
14335 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14336 buffer += bytes_read;
14337 range_end = read_addr_index (cu, addr_index);
14338 break;
43988095 14339 default:
b98664d3 14340 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14341 return false;
14342 }
14343 if (rlet == DW_RLE_end_of_list || overflow)
14344 break;
14345 if (rlet == DW_RLE_base_address)
14346 continue;
14347
43988095
JK
14348 if (range_beginning > range_end)
14349 {
14350 /* Inverted range entries are invalid. */
b98664d3 14351 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14352 return false;
14353 }
14354
14355 /* Empty range entries have no effect. */
14356 if (range_beginning == range_end)
14357 continue;
14358
d0ce17d8
CT
14359 /* Only DW_RLE_offset_pair needs the base address added. */
14360 if (rlet == DW_RLE_offset_pair)
14361 {
14362 if (!base.has_value ())
14363 {
14364 /* We have no valid base address for the DW_RLE_offset_pair. */
14365 complaint (_("Invalid .debug_rnglists data (no base address for "
14366 "DW_RLE_offset_pair)"));
14367 return false;
14368 }
14369
14370 range_beginning += *base;
14371 range_end += *base;
14372 }
43988095
JK
14373
14374 /* A not-uncommon case of bad debug info.
14375 Don't pollute the addrmap with bad data. */
14376 if (range_beginning + baseaddr == 0
976ca316 14377 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14378 {
b98664d3 14379 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14380 " [in module %s]"), objfile_name (objfile));
14381 continue;
14382 }
14383
14384 callback (range_beginning, range_end);
14385 }
14386
14387 if (overflow)
14388 {
b98664d3 14389 complaint (_("Offset %d is not terminated "
43988095
JK
14390 "for DW_AT_ranges attribute"),
14391 offset);
14392 return false;
14393 }
14394
14395 return true;
14396}
14397
14398/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14399 Callback's type should be:
14400 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14401 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14402
43988095 14403template <typename Callback>
43039443 14404static int
d0ce17d8 14405dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14406 Callback &&callback)
43039443 14407{
5e22e966
SM
14408 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14409 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14410 struct comp_unit_head *cu_header = &cu->header;
14411 bfd *obfd = objfile->obfd;
14412 unsigned int addr_size = cu_header->addr_size;
14413 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14414 /* Base address selection entry. */
2b24b6e4 14415 gdb::optional<CORE_ADDR> base;
43039443 14416 unsigned int dummy;
d521ce57 14417 const gdb_byte *buffer;
ff013f42 14418 CORE_ADDR baseaddr;
43039443 14419
43988095 14420 if (cu_header->version >= 5)
d0ce17d8 14421 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14422
d00adf39 14423 base = cu->base_address;
43039443 14424
5e22e966
SM
14425 per_objfile->per_bfd->ranges.read (objfile);
14426 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14427 {
b98664d3 14428 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14429 offset);
14430 return 0;
14431 }
5e22e966 14432 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14433
b3b3bada 14434 baseaddr = objfile->text_section_offset ();
ff013f42 14435
43039443
JK
14436 while (1)
14437 {
14438 CORE_ADDR range_beginning, range_end;
14439
c8a7a66f 14440 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14441 buffer += addr_size;
c8a7a66f 14442 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14443 buffer += addr_size;
14444 offset += 2 * addr_size;
14445
14446 /* An end of list marker is a pair of zero addresses. */
14447 if (range_beginning == 0 && range_end == 0)
14448 /* Found the end of list entry. */
14449 break;
14450
14451 /* Each base address selection entry is a pair of 2 values.
14452 The first is the largest possible address, the second is
14453 the base address. Check for a base address here. */
14454 if ((range_beginning & mask) == mask)
14455 {
28d2bfb9
AB
14456 /* If we found the largest possible address, then we already
14457 have the base address in range_end. */
14458 base = range_end;
43039443
JK
14459 continue;
14460 }
14461
2b24b6e4 14462 if (!base.has_value ())
43039443
JK
14463 {
14464 /* We have no valid base address for the ranges
14465 data. */
b98664d3 14466 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14467 return 0;
14468 }
14469
9277c30c
UW
14470 if (range_beginning > range_end)
14471 {
14472 /* Inverted range entries are invalid. */
b98664d3 14473 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14474 return 0;
14475 }
14476
14477 /* Empty range entries have no effect. */
14478 if (range_beginning == range_end)
14479 continue;
14480
2b24b6e4
TT
14481 range_beginning += *base;
14482 range_end += *base;
43039443 14483
01093045
DE
14484 /* A not-uncommon case of bad debug info.
14485 Don't pollute the addrmap with bad data. */
14486 if (range_beginning + baseaddr == 0
5e22e966 14487 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14488 {
b98664d3 14489 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14490 " [in module %s]"), objfile_name (objfile));
01093045
DE
14491 continue;
14492 }
14493
5f46c5a5
JK
14494 callback (range_beginning, range_end);
14495 }
14496
14497 return 1;
14498}
14499
14500/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14501 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 14502 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
14503
14504static int
14505dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14506 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14507 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14508{
5e22e966 14509 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14510 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14511 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14512 int low_set = 0;
14513 CORE_ADDR low = 0;
14514 CORE_ADDR high = 0;
14515 int retval;
14516
d0ce17d8 14517 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14518 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14519 {
9277c30c 14520 if (ranges_pst != NULL)
3e29f34a
MR
14521 {
14522 CORE_ADDR lowpc;
14523 CORE_ADDR highpc;
14524
79748972
TT
14525 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14526 range_beginning + baseaddr)
14527 - baseaddr);
14528 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14529 range_end + baseaddr)
14530 - baseaddr);
d320c2b5
TT
14531 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14532 lowpc, highpc - 1, ranges_pst);
3e29f34a 14533 }
ff013f42 14534
43039443
JK
14535 /* FIXME: This is recording everything as a low-high
14536 segment of consecutive addresses. We should have a
14537 data structure for discontiguous block ranges
14538 instead. */
14539 if (! low_set)
14540 {
14541 low = range_beginning;
14542 high = range_end;
14543 low_set = 1;
14544 }
14545 else
14546 {
14547 if (range_beginning < low)
14548 low = range_beginning;
14549 if (range_end > high)
14550 high = range_end;
14551 }
5f46c5a5
JK
14552 });
14553 if (!retval)
14554 return 0;
43039443
JK
14555
14556 if (! low_set)
14557 /* If the first entry is an end-of-list marker, the range
14558 describes an empty scope, i.e. no instructions. */
14559 return 0;
14560
14561 if (low_return)
14562 *low_return = low;
14563 if (high_return)
14564 *high_return = high;
14565 return 1;
14566}
14567
3a2b436a
JK
14568/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14569 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14570 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14571
3a2b436a 14572static enum pc_bounds_kind
af34e669 14573dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14574 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14575 dwarf2_psymtab *pst)
c906108c 14576{
976ca316 14577 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14578 struct attribute *attr;
91da1414 14579 struct attribute *attr_high;
af34e669
DJ
14580 CORE_ADDR low = 0;
14581 CORE_ADDR high = 0;
e385593e 14582 enum pc_bounds_kind ret;
c906108c 14583
91da1414
MW
14584 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14585 if (attr_high)
af34e669 14586 {
e142c38c 14587 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14588 if (attr != nullptr)
dda83cd7 14589 {
95f982e5
TT
14590 low = attr->as_address ();
14591 high = attr_high->as_address ();
cd6c91b4 14592 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14593 high += low;
91da1414 14594 }
af34e669
DJ
14595 else
14596 /* Found high w/o low attribute. */
e385593e 14597 return PC_BOUNDS_INVALID;
af34e669
DJ
14598
14599 /* Found consecutive range of addresses. */
3a2b436a 14600 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14601 }
c906108c 14602 else
af34e669 14603 {
e142c38c 14604 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14605 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14606 {
2b0c7f41
SM
14607 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14608 on DWARF version). */
14609 ULONGEST ranges_offset = attr->as_unsigned ();
14610
14611 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14612 this value. */
14613 if (die->tag != DW_TAG_compile_unit)
14614 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14615
af34e669 14616 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14617 .debug_ranges section. */
d0ce17d8
CT
14618 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14619 die->tag))
e385593e 14620 return PC_BOUNDS_INVALID;
43039443 14621 /* Found discontinuous range of addresses. */
3a2b436a 14622 ret = PC_BOUNDS_RANGES;
af34e669 14623 }
e385593e
JK
14624 else
14625 return PC_BOUNDS_NOT_PRESENT;
af34e669 14626 }
c906108c 14627
48fbe735 14628 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14629 if (high <= low)
e385593e 14630 return PC_BOUNDS_INVALID;
c906108c
SS
14631
14632 /* When using the GNU linker, .gnu.linkonce. sections are used to
14633 eliminate duplicate copies of functions and vtables and such.
14634 The linker will arbitrarily choose one and discard the others.
14635 The AT_*_pc values for such functions refer to local labels in
14636 these sections. If the section from that file was discarded, the
14637 labels are not in the output, so the relocs get a value of 0.
14638 If this is a discarded function, mark the pc bounds as invalid,
14639 so that GDB will ignore it. */
976ca316 14640 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14641 return PC_BOUNDS_INVALID;
c906108c
SS
14642
14643 *lowpc = low;
96408a79
SA
14644 if (highpc)
14645 *highpc = high;
af34e669 14646 return ret;
c906108c
SS
14647}
14648
b084d499
JB
14649/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14650 its low and high PC addresses. Do nothing if these addresses could not
14651 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14652 and HIGHPC to the high address if greater than HIGHPC. */
14653
14654static void
14655dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14656 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14657 struct dwarf2_cu *cu)
b084d499
JB
14658{
14659 CORE_ADDR low, high;
14660 struct die_info *child = die->child;
14661
e385593e 14662 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14663 {
325fac50
PA
14664 *lowpc = std::min (*lowpc, low);
14665 *highpc = std::max (*highpc, high);
b084d499
JB
14666 }
14667
14668 /* If the language does not allow nested subprograms (either inside
14669 subprograms or lexical blocks), we're done. */
14670 if (cu->language != language_ada)
14671 return;
6e70227d 14672
b084d499
JB
14673 /* Check all the children of the given DIE. If it contains nested
14674 subprograms, then check their pc bounds. Likewise, we need to
14675 check lexical blocks as well, as they may also contain subprogram
14676 definitions. */
14677 while (child && child->tag)
14678 {
14679 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14680 || child->tag == DW_TAG_lexical_block)
14681 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14682 child = child->sibling;
b084d499
JB
14683 }
14684}
14685
fae299cd
DC
14686/* Get the low and high pc's represented by the scope DIE, and store
14687 them in *LOWPC and *HIGHPC. If the correct values can't be
14688 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14689
14690static void
14691get_scope_pc_bounds (struct die_info *die,
14692 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14693 struct dwarf2_cu *cu)
14694{
14695 CORE_ADDR best_low = (CORE_ADDR) -1;
14696 CORE_ADDR best_high = (CORE_ADDR) 0;
14697 CORE_ADDR current_low, current_high;
14698
3a2b436a 14699 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14700 >= PC_BOUNDS_RANGES)
fae299cd
DC
14701 {
14702 best_low = current_low;
14703 best_high = current_high;
14704 }
14705 else
14706 {
14707 struct die_info *child = die->child;
14708
14709 while (child && child->tag)
14710 {
14711 switch (child->tag) {
14712 case DW_TAG_subprogram:
dda83cd7 14713 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14714 break;
14715 case DW_TAG_namespace:
f55ee35c 14716 case DW_TAG_module:
fae299cd
DC
14717 /* FIXME: carlton/2004-01-16: Should we do this for
14718 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14719 that current GCC's always emit the DIEs corresponding
14720 to definitions of methods of classes as children of a
14721 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14722 the DIEs giving the declarations, which could be
14723 anywhere). But I don't see any reason why the
14724 standards says that they have to be there. */
14725 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14726
14727 if (current_low != ((CORE_ADDR) -1))
14728 {
325fac50
PA
14729 best_low = std::min (best_low, current_low);
14730 best_high = std::max (best_high, current_high);
fae299cd
DC
14731 }
14732 break;
14733 default:
0963b4bd 14734 /* Ignore. */
fae299cd
DC
14735 break;
14736 }
14737
436c571c 14738 child = child->sibling;
fae299cd
DC
14739 }
14740 }
14741
14742 *lowpc = best_low;
14743 *highpc = best_high;
14744}
14745
801e3a5b
JB
14746/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14747 in DIE. */
380bca97 14748
801e3a5b
JB
14749static void
14750dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14751 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14752{
5e22e966 14753 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14754 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14755 struct attribute *attr;
91da1414 14756 struct attribute *attr_high;
801e3a5b 14757
91da1414
MW
14758 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14759 if (attr_high)
801e3a5b 14760 {
801e3a5b 14761 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14762 if (attr != nullptr)
dda83cd7 14763 {
95f982e5
TT
14764 CORE_ADDR low = attr->as_address ();
14765 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14766
cd6c91b4 14767 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14768 high += low;
9a619af0 14769
3e29f34a
MR
14770 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14771 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14772 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14773 }
801e3a5b
JB
14774 }
14775
14776 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14777 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14778 {
2b0c7f41
SM
14779 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14780 on DWARF version). */
14781 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14782
2b0c7f41
SM
14783 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14784 this value. */
14785 if (die->tag != DW_TAG_compile_unit)
14786 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14787
2d5f09ec 14788 std::vector<blockrange> blockvec;
2b0c7f41 14789 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14790 [&] (CORE_ADDR start, CORE_ADDR end)
14791 {
58fdfd2c
JK
14792 start += baseaddr;
14793 end += baseaddr;
5f46c5a5
JK
14794 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14795 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14796 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14797 blockvec.emplace_back (start, end);
5f46c5a5 14798 });
2d5f09ec
KB
14799
14800 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14801 }
14802}
14803
685b1105
JK
14804/* Check whether the producer field indicates either of GCC < 4.6, or the
14805 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14806
685b1105
JK
14807static void
14808check_producer (struct dwarf2_cu *cu)
60d5a603 14809{
38360086 14810 int major, minor;
60d5a603
JK
14811
14812 if (cu->producer == NULL)
14813 {
14814 /* For unknown compilers expect their behavior is DWARF version
14815 compliant.
14816
14817 GCC started to support .debug_types sections by -gdwarf-4 since
14818 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14819 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14820 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14821 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14822 }
b1ffba5a 14823 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14824 {
38360086
MW
14825 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14826 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14827 }
5230b05a 14828 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14829 {
14830 cu->producer_is_icc = true;
14831 cu->producer_is_icc_lt_14 = major < 14;
14832 }
c258c396
JD
14833 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14834 cu->producer_is_codewarrior = true;
685b1105
JK
14835 else
14836 {
14837 /* For other non-GCC compilers, expect their behavior is DWARF version
14838 compliant. */
60d5a603
JK
14839 }
14840
9068261f 14841 cu->checked_producer = true;
685b1105 14842}
ba919b58 14843
685b1105
JK
14844/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14845 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14846 during 4.6.0 experimental. */
14847
9068261f 14848static bool
685b1105
JK
14849producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14850{
14851 if (!cu->checked_producer)
14852 check_producer (cu);
14853
14854 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14855}
14856
c258c396
JD
14857
14858/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14859 with incorrect is_stmt attributes. */
14860
14861static bool
14862producer_is_codewarrior (struct dwarf2_cu *cu)
14863{
14864 if (!cu->checked_producer)
14865 check_producer (cu);
14866
14867 return cu->producer_is_codewarrior;
14868}
14869
bf23a268
TT
14870/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14871 If that attribute is not available, return the appropriate
14872 default. */
60d5a603
JK
14873
14874static enum dwarf_access_attribute
bf23a268 14875dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14876{
bf23a268
TT
14877 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14878 if (attr != nullptr)
14879 {
14880 LONGEST value = attr->constant_value (-1);
14881 if (value == DW_ACCESS_public
14882 || value == DW_ACCESS_protected
14883 || value == DW_ACCESS_private)
14884 return (dwarf_access_attribute) value;
14885 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14886 plongest (value));
14887 }
14888
60d5a603
JK
14889 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14890 {
14891 /* The default DWARF 2 accessibility for members is public, the default
14892 accessibility for inheritance is private. */
14893
14894 if (die->tag != DW_TAG_inheritance)
14895 return DW_ACCESS_public;
14896 else
14897 return DW_ACCESS_private;
14898 }
14899 else
14900 {
14901 /* DWARF 3+ defines the default accessibility a different way. The same
14902 rules apply now for DW_TAG_inheritance as for the members and it only
14903 depends on the container kind. */
14904
14905 if (die->parent->tag == DW_TAG_class_type)
14906 return DW_ACCESS_private;
14907 else
14908 return DW_ACCESS_public;
14909 }
14910}
14911
74ac6d43
TT
14912/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14913 offset. If the attribute was not found return 0, otherwise return
14914 1. If it was found but could not properly be handled, set *OFFSET
14915 to 0. */
14916
14917static int
14918handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14919 LONGEST *offset)
14920{
14921 struct attribute *attr;
14922
14923 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14924 if (attr != NULL)
14925 {
14926 *offset = 0;
14927
14928 /* Note that we do not check for a section offset first here.
14929 This is because DW_AT_data_member_location is new in DWARF 4,
14930 so if we see it, we can assume that a constant form is really
14931 a constant and not a section offset. */
cd6c91b4 14932 if (attr->form_is_constant ())
0826b30a 14933 *offset = attr->constant_value (0);
cd6c91b4 14934 else if (attr->form_is_section_offset ())
74ac6d43 14935 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14936 else if (attr->form_is_block ())
9d2246fc 14937 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14938 else
14939 dwarf2_complex_location_expr_complaint ();
14940
14941 return 1;
14942 }
14943
14944 return 0;
14945}
14946
7d79de9a
TT
14947/* Look for DW_AT_data_member_location and store the results in FIELD. */
14948
14949static void
14950handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14951 struct field *field)
14952{
14953 struct attribute *attr;
14954
14955 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14956 if (attr != NULL)
14957 {
14958 if (attr->form_is_constant ())
14959 {
14960 LONGEST offset = attr->constant_value (0);
14961 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14962 }
14963 else if (attr->form_is_section_offset ())
14964 dwarf2_complex_location_expr_complaint ();
14965 else if (attr->form_is_block ())
14966 {
14967 bool handled;
9d2246fc 14968 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14969 if (handled)
14970 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14971 else
14972 {
5e22e966
SM
14973 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14974 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14975 struct dwarf2_locexpr_baton *dlbaton
14976 = XOBNEW (&objfile->objfile_obstack,
14977 struct dwarf2_locexpr_baton);
9d2246fc
TT
14978 dlbaton->data = attr->as_block ()->data;
14979 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14980 /* When using this baton, we want to compute the address
14981 of the field, not the value. This is why
14982 is_reference is set to false here. */
14983 dlbaton->is_reference = false;
5e22e966 14984 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14985 dlbaton->per_cu = cu->per_cu;
14986
14987 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14988 }
14989 }
14990 else
14991 dwarf2_complex_location_expr_complaint ();
14992 }
14993}
14994
c906108c
SS
14995/* Add an aggregate field to the field list. */
14996
14997static void
107d2387 14998dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14999 struct dwarf2_cu *cu)
6e70227d 15000{
5e22e966 15001 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 15002 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
15003 struct nextfield *new_field;
15004 struct attribute *attr;
15005 struct field *fp;
15d034d0 15006 const char *fieldname = "";
c906108c 15007
7d0ccb61
DJ
15008 if (die->tag == DW_TAG_inheritance)
15009 {
be2daae6
TT
15010 fip->baseclasses.emplace_back ();
15011 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15012 }
15013 else
15014 {
be2daae6
TT
15015 fip->fields.emplace_back ();
15016 new_field = &fip->fields.back ();
7d0ccb61 15017 }
be2daae6 15018
9c6a1327
TT
15019 new_field->offset = die->sect_off;
15020
bf23a268 15021 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 15022 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 15023 fip->non_public_fields = true;
60d5a603 15024
e142c38c 15025 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15026 if (attr != nullptr)
23dca5c3 15027 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
15028 else
15029 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15030
15031 fp = &new_field->field;
a9a9bd0f 15032
e142c38c 15033 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15034 {
a9a9bd0f 15035 /* Data member other than a C++ static data member. */
6e70227d 15036
c906108c 15037 /* Get type of field. */
5d14b6e5 15038 fp->set_type (die_type (die, cu));
c906108c 15039
d6a843b5 15040 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15041
c906108c 15042 /* Get bit size of field (zero if none). */
e142c38c 15043 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15044 if (attr != nullptr)
c906108c 15045 {
529908cb 15046 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
15047 }
15048 else
15049 {
15050 FIELD_BITSIZE (*fp) = 0;
15051 }
15052
15053 /* Get bit offset of field. */
7d79de9a 15054 handle_data_member_location (die, cu, fp);
e142c38c 15055 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 15056 if (attr != nullptr && attr->form_is_constant ())
c906108c 15057 {
d5a22e77 15058 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15059 {
15060 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
15061 additional bit offset from the MSB of the containing
15062 anonymous object to the MSB of the field. We don't
15063 have to do anything special since we don't need to
15064 know the size of the anonymous object. */
529908cb 15065 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 15066 + attr->constant_value (0)));
c906108c
SS
15067 }
15068 else
15069 {
15070 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
15071 MSB of the anonymous object, subtract off the number of
15072 bits from the MSB of the field to the MSB of the
15073 object, and then subtract off the number of bits of
15074 the field itself. The result is the bit offset of
15075 the LSB of the field. */
c906108c 15076 int anonymous_size;
7c184d33 15077 int bit_offset = attr->constant_value (0);
c906108c 15078
e142c38c 15079 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 15080 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
15081 {
15082 /* The size of the anonymous object containing
15083 the bit field is explicit, so use the
15084 indicated size (in bytes). */
7c184d33 15085 anonymous_size = attr->constant_value (0);
c906108c
SS
15086 }
15087 else
15088 {
15089 /* The size of the anonymous object containing
15090 the bit field must be inferred from the type
15091 attribute of the data member containing the
15092 bit field. */
5d14b6e5 15093 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 15094 }
f41f5e61
PA
15095 SET_FIELD_BITPOS (*fp,
15096 (FIELD_BITPOS (*fp)
15097 + anonymous_size * bits_per_byte
15098 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15099 }
15100 }
da5b30da
AA
15101 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15102 if (attr != NULL)
15103 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 15104 + attr->constant_value (0)));
c906108c
SS
15105
15106 /* Get name of field. */
39cbfefa
DJ
15107 fieldname = dwarf2_name (die, cu);
15108 if (fieldname == NULL)
15109 fieldname = "";
d8151005
DJ
15110
15111 /* The name is already allocated along with this objfile, so we don't
15112 need to duplicate it for the type. */
15113 fp->name = fieldname;
c906108c
SS
15114
15115 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 15116 pointer or virtual base class pointer) to private. */
e142c38c 15117 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15118 {
d48cc9dd 15119 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 15120 new_field->accessibility = DW_ACCESS_private;
264fc0e2 15121 fip->non_public_fields = true;
c906108c
SS
15122 }
15123 }
a9a9bd0f 15124 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15125 {
a9a9bd0f
DC
15126 /* C++ static member. */
15127
15128 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15129 is a declaration, but all versions of G++ as of this writing
15130 (so through at least 3.2.1) incorrectly generate
15131 DW_TAG_variable tags. */
6e70227d 15132
ff355380 15133 const char *physname;
c906108c 15134
a9a9bd0f 15135 /* Get name of field. */
39cbfefa
DJ
15136 fieldname = dwarf2_name (die, cu);
15137 if (fieldname == NULL)
c906108c
SS
15138 return;
15139
254e6b9e 15140 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15141 if (attr
15142 /* Only create a symbol if this is an external value.
15143 new_symbol checks this and puts the value in the global symbol
15144 table, which we want. If it is not external, new_symbol
15145 will try to put the value in cu->list_in_scope which is wrong. */
15146 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15147 {
15148 /* A static const member, not much different than an enum as far as
15149 we're concerned, except that we can support more types. */
15150 new_symbol (die, NULL, cu);
15151 }
15152
2df3850c 15153 /* Get physical name. */
ff355380 15154 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15155
d8151005
DJ
15156 /* The name is already allocated along with this objfile, so we don't
15157 need to duplicate it for the type. */
15158 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 15159 fp->set_type (die_type (die, cu));
d8151005 15160 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15161 }
15162 else if (die->tag == DW_TAG_inheritance)
15163 {
74ac6d43 15164 /* C++ base class field. */
7d79de9a 15165 handle_data_member_location (die, cu, fp);
c906108c 15166 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
15167 fp->set_type (die_type (die, cu));
15168 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 15169 }
2ddeaf8a
TT
15170 else
15171 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15172}
15173
883fd55a
KS
15174/* Can the type given by DIE define another type? */
15175
15176static bool
15177type_can_define_types (const struct die_info *die)
15178{
15179 switch (die->tag)
15180 {
15181 case DW_TAG_typedef:
15182 case DW_TAG_class_type:
15183 case DW_TAG_structure_type:
15184 case DW_TAG_union_type:
15185 case DW_TAG_enumeration_type:
15186 return true;
15187
15188 default:
15189 return false;
15190 }
15191}
15192
15193/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15194
15195static void
883fd55a
KS
15196dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15197 struct dwarf2_cu *cu)
6e70227d 15198{
be2daae6
TT
15199 struct decl_field fp;
15200 memset (&fp, 0, sizeof (fp));
98751a41 15201
883fd55a 15202 gdb_assert (type_can_define_types (die));
98751a41 15203
883fd55a 15204 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15205 fp.name = dwarf2_name (die, cu);
15206 fp.type = read_type_die (die, cu);
98751a41 15207
c191a687 15208 /* Save accessibility. */
bf23a268 15209 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15210 switch (accessibility)
15211 {
15212 case DW_ACCESS_public:
15213 /* The assumed value if neither private nor protected. */
15214 break;
15215 case DW_ACCESS_private:
be2daae6 15216 fp.is_private = 1;
c191a687
KS
15217 break;
15218 case DW_ACCESS_protected:
be2daae6 15219 fp.is_protected = 1;
c191a687 15220 break;
c191a687
KS
15221 }
15222
883fd55a 15223 if (die->tag == DW_TAG_typedef)
be2daae6 15224 fip->typedef_field_list.push_back (fp);
883fd55a 15225 else
be2daae6 15226 fip->nested_types_list.push_back (fp);
98751a41
JK
15227}
15228
9c6a1327
TT
15229/* A convenience typedef that's used when finding the discriminant
15230 field for a variant part. */
1b95cdb7
SM
15231typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15232 offset_map_type;
9c6a1327
TT
15233
15234/* Compute the discriminant range for a given variant. OBSTACK is
15235 where the results will be stored. VARIANT is the variant to
15236 process. IS_UNSIGNED indicates whether the discriminant is signed
15237 or unsigned. */
15238
15239static const gdb::array_view<discriminant_range>
15240convert_variant_range (struct obstack *obstack, const variant_field &variant,
15241 bool is_unsigned)
15242{
15243 std::vector<discriminant_range> ranges;
15244
15245 if (variant.default_branch)
15246 return {};
15247
15248 if (variant.discr_list_data == nullptr)
15249 {
15250 discriminant_range r
15251 = {variant.discriminant_value, variant.discriminant_value};
15252 ranges.push_back (r);
15253 }
15254 else
15255 {
15256 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15257 variant.discr_list_data->size);
15258 while (!data.empty ())
15259 {
15260 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15261 {
15262 complaint (_("invalid discriminant marker: %d"), data[0]);
15263 break;
15264 }
15265 bool is_range = data[0] == DW_DSC_range;
15266 data = data.slice (1);
15267
15268 ULONGEST low, high;
15269 unsigned int bytes_read;
15270
15271 if (data.empty ())
15272 {
15273 complaint (_("DW_AT_discr_list missing low value"));
15274 break;
15275 }
15276 if (is_unsigned)
15277 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15278 else
15279 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15280 &bytes_read);
15281 data = data.slice (bytes_read);
15282
15283 if (is_range)
15284 {
15285 if (data.empty ())
15286 {
15287 complaint (_("DW_AT_discr_list missing high value"));
15288 break;
15289 }
15290 if (is_unsigned)
15291 high = read_unsigned_leb128 (nullptr, data.data (),
15292 &bytes_read);
15293 else
15294 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15295 &bytes_read);
15296 data = data.slice (bytes_read);
15297 }
15298 else
15299 high = low;
15300
15301 ranges.push_back ({ low, high });
15302 }
15303 }
15304
15305 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15306 ranges.size ());
15307 std::copy (ranges.begin (), ranges.end (), result);
15308 return gdb::array_view<discriminant_range> (result, ranges.size ());
15309}
15310
15311static const gdb::array_view<variant_part> create_variant_parts
15312 (struct obstack *obstack,
15313 const offset_map_type &offset_map,
15314 struct field_info *fi,
15315 const std::vector<variant_part_builder> &variant_parts);
15316
15317/* Fill in a "struct variant" for a given variant field. RESULT is
15318 the variant to fill in. OBSTACK is where any needed allocations
15319 will be done. OFFSET_MAP holds the mapping from section offsets to
15320 fields for the type. FI describes the fields of the type we're
15321 processing. FIELD is the variant field we're converting. */
15322
15323static void
15324create_one_variant (variant &result, struct obstack *obstack,
15325 const offset_map_type &offset_map,
15326 struct field_info *fi, const variant_field &field)
15327{
15328 result.discriminants = convert_variant_range (obstack, field, false);
15329 result.first_field = field.first_field + fi->baseclasses.size ();
15330 result.last_field = field.last_field + fi->baseclasses.size ();
15331 result.parts = create_variant_parts (obstack, offset_map, fi,
15332 field.variant_parts);
15333}
15334
15335/* Fill in a "struct variant_part" for a given variant part. RESULT
15336 is the variant part to fill in. OBSTACK is where any needed
15337 allocations will be done. OFFSET_MAP holds the mapping from
15338 section offsets to fields for the type. FI describes the fields of
15339 the type we're processing. BUILDER is the variant part to be
15340 converted. */
15341
15342static void
15343create_one_variant_part (variant_part &result,
15344 struct obstack *obstack,
15345 const offset_map_type &offset_map,
15346 struct field_info *fi,
15347 const variant_part_builder &builder)
15348{
15349 auto iter = offset_map.find (builder.discriminant_offset);
15350 if (iter == offset_map.end ())
15351 {
15352 result.discriminant_index = -1;
15353 /* Doesn't matter. */
15354 result.is_unsigned = false;
15355 }
15356 else
15357 {
15358 result.discriminant_index = iter->second;
15359 result.is_unsigned
c6d940a9 15360 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15361 }
15362
15363 size_t n = builder.variants.size ();
15364 variant *output = new (obstack) variant[n];
15365 for (size_t i = 0; i < n; ++i)
15366 create_one_variant (output[i], obstack, offset_map, fi,
15367 builder.variants[i]);
15368
15369 result.variants = gdb::array_view<variant> (output, n);
15370}
15371
15372/* Create a vector of variant parts that can be attached to a type.
15373 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15374 holds the mapping from section offsets to fields for the type. FI
15375 describes the fields of the type we're processing. VARIANT_PARTS
15376 is the vector to convert. */
15377
15378static const gdb::array_view<variant_part>
15379create_variant_parts (struct obstack *obstack,
15380 const offset_map_type &offset_map,
15381 struct field_info *fi,
15382 const std::vector<variant_part_builder> &variant_parts)
15383{
15384 if (variant_parts.empty ())
15385 return {};
15386
15387 size_t n = variant_parts.size ();
15388 variant_part *result = new (obstack) variant_part[n];
15389 for (size_t i = 0; i < n; ++i)
15390 create_one_variant_part (result[i], obstack, offset_map, fi,
15391 variant_parts[i]);
15392
15393 return gdb::array_view<variant_part> (result, n);
15394}
15395
15396/* Compute the variant part vector for FIP, attaching it to TYPE when
15397 done. */
15398
15399static void
15400add_variant_property (struct field_info *fip, struct type *type,
15401 struct dwarf2_cu *cu)
15402{
15403 /* Map section offsets of fields to their field index. Note the
15404 field index here does not take the number of baseclasses into
15405 account. */
15406 offset_map_type offset_map;
15407 for (int i = 0; i < fip->fields.size (); ++i)
15408 offset_map[fip->fields[i].offset] = i;
15409
5e22e966 15410 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15411 gdb::array_view<variant_part> parts
15412 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15413 fip->variant_parts);
15414
15415 struct dynamic_prop prop;
8c2e4e06
SM
15416 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15417 obstack_copy (&objfile->objfile_obstack, &parts,
15418 sizeof (parts)));
9c6a1327 15419
5c54719c 15420 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15421}
15422
c906108c
SS
15423/* Create the vector of fields, and attach it to the type. */
15424
15425static void
fba45db2 15426dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15427 struct dwarf2_cu *cu)
c906108c 15428{
317f7127 15429 int nfields = fip->nfields ();
c906108c
SS
15430
15431 /* Record the field count, allocate space for the array of fields,
15432 and create blank accessibility bitfields if necessary. */
5e33d5f4 15433 type->set_num_fields (nfields);
3cabb6b0
SM
15434 type->set_fields
15435 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15436
b4ba55a1 15437 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15438 {
15439 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15440
15441 TYPE_FIELD_PRIVATE_BITS (type) =
15442 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15443 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15444
15445 TYPE_FIELD_PROTECTED_BITS (type) =
15446 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15447 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15448
774b6a14
TT
15449 TYPE_FIELD_IGNORE_BITS (type) =
15450 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15451 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15452 }
15453
15454 /* If the type has baseclasses, allocate and clear a bit vector for
15455 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15456 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15457 {
be2daae6 15458 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15459 unsigned char *pointer;
c906108c
SS
15460
15461 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15462 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15463 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15464 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15465 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15466 }
15467
9c6a1327
TT
15468 if (!fip->variant_parts.empty ())
15469 add_variant_property (fip, type, cu);
2ddeaf8a 15470
be2daae6
TT
15471 /* Copy the saved-up fields into the field vector. */
15472 for (int i = 0; i < nfields; ++i)
c906108c 15473 {
be2daae6
TT
15474 struct nextfield &field
15475 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15476 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15477
ceacbf6e 15478 type->field (i) = field.field;
be2daae6 15479 switch (field.accessibility)
c906108c 15480 {
c5aa993b 15481 case DW_ACCESS_private:
b4ba55a1 15482 if (cu->language != language_ada)
be2daae6 15483 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15484 break;
c906108c 15485
c5aa993b 15486 case DW_ACCESS_protected:
b4ba55a1 15487 if (cu->language != language_ada)
be2daae6 15488 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15489 break;
c906108c 15490
c5aa993b
JM
15491 case DW_ACCESS_public:
15492 break;
c906108c 15493
c5aa993b
JM
15494 default:
15495 /* Unknown accessibility. Complain and treat it as public. */
15496 {
b98664d3 15497 complaint (_("unsupported accessibility %d"),
be2daae6 15498 field.accessibility);
c5aa993b
JM
15499 }
15500 break;
c906108c 15501 }
be2daae6 15502 if (i < fip->baseclasses.size ())
c906108c 15503 {
be2daae6 15504 switch (field.virtuality)
c906108c 15505 {
c5aa993b
JM
15506 case DW_VIRTUALITY_virtual:
15507 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15508 if (cu->language == language_ada)
a73c6dcd 15509 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15510 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15511 break;
c906108c
SS
15512 }
15513 }
c906108c
SS
15514 }
15515}
15516
7d27a96d
TT
15517/* Return true if this member function is a constructor, false
15518 otherwise. */
15519
15520static int
15521dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15522{
15523 const char *fieldname;
fe978cb0 15524 const char *type_name;
7d27a96d
TT
15525 int len;
15526
15527 if (die->parent == NULL)
15528 return 0;
15529
15530 if (die->parent->tag != DW_TAG_structure_type
15531 && die->parent->tag != DW_TAG_union_type
15532 && die->parent->tag != DW_TAG_class_type)
15533 return 0;
15534
15535 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15536 type_name = dwarf2_name (die->parent, cu);
15537 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15538 return 0;
15539
15540 len = strlen (fieldname);
fe978cb0
PA
15541 return (strncmp (fieldname, type_name, len) == 0
15542 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15543}
15544
c906108c
SS
15545/* Add a member function to the proper fieldlist. */
15546
15547static void
107d2387 15548dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15549 struct type *type, struct dwarf2_cu *cu)
c906108c 15550{
5e22e966 15551 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15552 struct attribute *attr;
c906108c 15553 int i;
be2daae6 15554 struct fnfieldlist *flp = nullptr;
c906108c 15555 struct fn_field *fnp;
15d034d0 15556 const char *fieldname;
f792889a 15557 struct type *this_type;
c906108c 15558
b4ba55a1 15559 if (cu->language == language_ada)
a73c6dcd 15560 error (_("unexpected member function in Ada type"));
b4ba55a1 15561
2df3850c 15562 /* Get name of member function. */
39cbfefa
DJ
15563 fieldname = dwarf2_name (die, cu);
15564 if (fieldname == NULL)
2df3850c 15565 return;
c906108c 15566
c906108c 15567 /* Look up member function name in fieldlist. */
be2daae6 15568 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15569 {
27bfe10e 15570 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15571 {
15572 flp = &fip->fnfieldlists[i];
15573 break;
15574 }
c906108c
SS
15575 }
15576
be2daae6
TT
15577 /* Create a new fnfieldlist if necessary. */
15578 if (flp == nullptr)
c906108c 15579 {
be2daae6
TT
15580 fip->fnfieldlists.emplace_back ();
15581 flp = &fip->fnfieldlists.back ();
c906108c 15582 flp->name = fieldname;
be2daae6 15583 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15584 }
15585
be2daae6
TT
15586 /* Create a new member function field and add it to the vector of
15587 fnfieldlists. */
15588 flp->fnfields.emplace_back ();
15589 fnp = &flp->fnfields.back ();
3da10d80
KS
15590
15591 /* Delay processing of the physname until later. */
9c37b5ae 15592 if (cu->language == language_cplus)
be2daae6
TT
15593 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15594 die, cu);
3da10d80
KS
15595 else
15596 {
1d06ead6 15597 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15598 fnp->physname = physname ? physname : "";
15599 }
15600
c906108c 15601 fnp->type = alloc_type (objfile);
f792889a 15602 this_type = read_type_die (die, cu);
78134374 15603 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15604 {
1f704f76 15605 int nparams = this_type->num_fields ();
c906108c 15606
f792889a 15607 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15608 of the method itself (TYPE_CODE_METHOD). */
15609 smash_to_method_type (fnp->type, type,
f792889a 15610 TYPE_TARGET_TYPE (this_type),
80fc5e77 15611 this_type->fields (),
1f704f76 15612 this_type->num_fields (),
a409645d 15613 this_type->has_varargs ());
c906108c
SS
15614
15615 /* Handle static member functions.
dda83cd7
SM
15616 Dwarf2 has no clean way to discern C++ static and non-static
15617 member functions. G++ helps GDB by marking the first
15618 parameter for non-static member functions (which is the this
15619 pointer) as artificial. We obtain this information from
15620 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15621 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15622 fnp->voffset = VOFFSET_STATIC;
15623 }
15624 else
b98664d3 15625 complaint (_("member function type missing for '%s'"),
3da10d80 15626 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15627
15628 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15629 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15630 fnp->fcontext = die_containing_type (die, cu);
c906108c 15631
3e43a32a
MS
15632 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15633 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15634
15635 /* Get accessibility. */
bf23a268 15636 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15637 switch (accessibility)
c906108c 15638 {
60d5a603
JK
15639 case DW_ACCESS_private:
15640 fnp->is_private = 1;
15641 break;
15642 case DW_ACCESS_protected:
15643 fnp->is_protected = 1;
15644 break;
c906108c
SS
15645 }
15646
b02dede2 15647 /* Check for artificial methods. */
e142c38c 15648 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15649 if (attr && attr->as_boolean ())
b02dede2
DJ
15650 fnp->is_artificial = 1;
15651
e35000a7
TBA
15652 /* Check for defaulted methods. */
15653 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15654 if (attr != nullptr)
15655 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15656
15657 /* Check for deleted methods. */
15658 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15659 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15660 fnp->is_deleted = 1;
15661
7d27a96d
TT
15662 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15663
0d564a31 15664 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15665 function. For older versions of GCC, this is an offset in the
15666 appropriate virtual table, as specified by DW_AT_containing_type.
15667 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15668 to the object address. */
15669
e142c38c 15670 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15671 if (attr != nullptr)
8e19ed76 15672 {
9d2246fc 15673 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15674 {
9d2246fc
TT
15675 struct dwarf_block *block = attr->as_block ();
15676
15677 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15678 {
15679 /* Old-style GCC. */
9d2246fc 15680 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15681 }
9d2246fc
TT
15682 else if (block->data[0] == DW_OP_deref
15683 || (block->size > 1
15684 && block->data[0] == DW_OP_deref_size
15685 && block->data[1] == cu->header.addr_size))
aec5aa8b 15686 {
9d2246fc 15687 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15688 if ((fnp->voffset % cu->header.addr_size) != 0)
15689 dwarf2_complex_location_expr_complaint ();
15690 else
15691 fnp->voffset /= cu->header.addr_size;
15692 fnp->voffset += 2;
15693 }
15694 else
15695 dwarf2_complex_location_expr_complaint ();
15696
15697 if (!fnp->fcontext)
7e993ebf
KS
15698 {
15699 /* If there is no `this' field and no DW_AT_containing_type,
15700 we cannot actually find a base class context for the
15701 vtable! */
1f704f76 15702 if (this_type->num_fields () == 0
7e993ebf
KS
15703 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15704 {
b98664d3 15705 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15706 "function \"%s\" (offset %s)"),
15707 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15708 }
15709 else
15710 {
15711 fnp->fcontext
940da03e 15712 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15713 }
15714 }
aec5aa8b 15715 }
cd6c91b4 15716 else if (attr->form_is_section_offset ())
dda83cd7 15717 {
4d3c2250 15718 dwarf2_complex_location_expr_complaint ();
dda83cd7 15719 }
8e19ed76 15720 else
dda83cd7 15721 {
4d3c2250
KB
15722 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15723 fieldname);
dda83cd7 15724 }
0d564a31 15725 }
d48cc9dd
DJ
15726 else
15727 {
15728 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15729 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15730 {
15731 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15732 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15733 "but the vtable offset is not specified"),
9d8780f0 15734 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15735 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15736 TYPE_CPLUS_DYNAMIC (type) = 1;
15737 }
15738 }
c906108c
SS
15739}
15740
15741/* Create the vector of member function fields, and attach it to the type. */
15742
15743static void
fba45db2 15744dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15745 struct dwarf2_cu *cu)
c906108c 15746{
b4ba55a1 15747 if (cu->language == language_ada)
a73c6dcd 15748 error (_("unexpected member functions in Ada type"));
b4ba55a1 15749
c906108c
SS
15750 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15751 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15752 TYPE_ALLOC (type,
15753 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15754
be2daae6 15755 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15756 {
be2daae6 15757 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15758 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15759
be2daae6
TT
15760 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15761 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15762 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15763 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15764
15765 for (int k = 0; k < nf.fnfields.size (); ++k)
15766 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15767 }
15768
be2daae6 15769 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15770}
15771
1168df01
JB
15772/* Returns non-zero if NAME is the name of a vtable member in CU's
15773 language, zero otherwise. */
15774static int
15775is_vtable_name (const char *name, struct dwarf2_cu *cu)
15776{
15777 static const char vptr[] = "_vptr";
15778
9c37b5ae
TT
15779 /* Look for the C++ form of the vtable. */
15780 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15781 return 1;
15782
15783 return 0;
15784}
15785
c0dd20ea 15786/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15787 functions, with the ABI-specified layout. If TYPE describes
15788 such a structure, smash it into a member function type.
61049d3b
DJ
15789
15790 GCC shouldn't do this; it should just output pointer to member DIEs.
15791 This is GCC PR debug/28767. */
c0dd20ea 15792
0b92b5bb
TT
15793static void
15794quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15795{
09e2d7c7 15796 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15797
15798 /* Check for a structure with no name and two children. */
1f704f76 15799 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15800 return;
c0dd20ea
DJ
15801
15802 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15803 if (TYPE_FIELD_NAME (type, 0) == NULL
15804 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15805 || TYPE_FIELD_NAME (type, 1) == NULL
15806 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15807 return;
c0dd20ea
DJ
15808
15809 /* Find the type of the method. */
940da03e 15810 pfn_type = type->field (0).type ();
c0dd20ea 15811 if (pfn_type == NULL
78134374
SM
15812 || pfn_type->code () != TYPE_CODE_PTR
15813 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15814 return;
c0dd20ea
DJ
15815
15816 /* Look for the "this" argument. */
15817 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15818 if (pfn_type->num_fields () == 0
940da03e
SM
15819 /* || pfn_type->field (0).type () == NULL */
15820 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15821 return;
c0dd20ea 15822
940da03e 15823 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15824 new_type = alloc_type (objfile);
09e2d7c7 15825 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15826 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15827 pfn_type->has_varargs ());
0b92b5bb 15828 smash_to_methodptr_type (type, new_type);
c0dd20ea 15829}
1168df01 15830
e26624c6
TT
15831/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15832 requires rewriting, then copy it and return the updated copy.
15833 Otherwise return nullptr. */
15834
15835static struct type *
15836rewrite_array_type (struct type *type)
15837{
15838 if (type->code () != TYPE_CODE_ARRAY)
15839 return nullptr;
15840
15841 struct type *index_type = type->index_type ();
15842 range_bounds *current_bounds = index_type->bounds ();
15843
15844 /* Handle multi-dimensional arrays. */
15845 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15846 if (new_target == nullptr)
15847 {
15848 /* Maybe we don't need to rewrite this array. */
15849 if (current_bounds->low.kind () == PROP_CONST
15850 && current_bounds->high.kind () == PROP_CONST)
15851 return nullptr;
15852 }
15853
15854 /* Either the target type was rewritten, or the bounds have to be
15855 updated. Either way we want to copy the type and update
15856 everything. */
15857 struct type *copy = copy_type (type);
15858 int nfields = copy->num_fields ();
15859 field *new_fields
15860 = ((struct field *) TYPE_ZALLOC (copy,
15861 nfields * sizeof (struct field)));
15862 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15863 copy->set_fields (new_fields);
15864 if (new_target != nullptr)
15865 TYPE_TARGET_TYPE (copy) = new_target;
15866
15867 struct type *index_copy = copy_type (index_type);
15868 range_bounds *bounds
15869 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15870 sizeof (range_bounds));
15871 *bounds = *current_bounds;
15872 bounds->low.set_const_val (1);
15873 bounds->high.set_const_val (0);
15874 index_copy->set_bounds (bounds);
15875 copy->set_index_type (index_copy);
15876
15877 return copy;
15878}
15879
57567375
TT
15880/* While some versions of GCC will generate complicated DWARF for an
15881 array (see quirk_ada_thick_pointer), more recent versions were
15882 modified to emit an explicit thick pointer structure. However, in
15883 this case, the array still has DWARF expressions for its ranges,
15884 and these must be ignored. */
15885
15886static void
15887quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15888 struct type *type)
15889{
15890 gdb_assert (cu->language == language_ada);
15891
15892 /* Check for a structure with two children. */
15893 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15894 return;
15895
15896 /* Check for P_ARRAY and P_BOUNDS members. */
15897 if (TYPE_FIELD_NAME (type, 0) == NULL
15898 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15899 || TYPE_FIELD_NAME (type, 1) == NULL
15900 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15901 return;
15902
15903 /* Make sure we're looking at a pointer to an array. */
15904 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15905 return;
57567375 15906
e26624c6
TT
15907 /* The Ada code already knows how to handle these types, so all that
15908 we need to do is turn the bounds into static bounds. However, we
15909 don't want to rewrite existing array or index types in-place,
15910 because those may be referenced in other contexts where this
15911 rewriting is undesirable. */
15912 struct type *new_ary_type
15913 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15914 if (new_ary_type != nullptr)
15915 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15916}
15917
2b4424c3
TT
15918/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15919 appropriate error checking and issuing complaints if there is a
15920 problem. */
15921
15922static ULONGEST
15923get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15924{
15925 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15926
15927 if (attr == nullptr)
15928 return 0;
15929
cd6c91b4 15930 if (!attr->form_is_constant ())
2b4424c3 15931 {
b98664d3 15932 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15933 " - DIE at %s [in module %s]"),
15934 sect_offset_str (die->sect_off),
5e22e966 15935 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15936 return 0;
15937 }
15938
529908cb
TT
15939 LONGEST val = attr->constant_value (0);
15940 if (val < 0)
2b4424c3 15941 {
529908cb
TT
15942 complaint (_("DW_AT_alignment value must not be negative"
15943 " - DIE at %s [in module %s]"),
15944 sect_offset_str (die->sect_off),
15945 objfile_name (cu->per_objfile->objfile));
15946 return 0;
2b4424c3 15947 }
529908cb 15948 ULONGEST align = val;
2b4424c3
TT
15949
15950 if (align == 0)
15951 {
b98664d3 15952 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15953 " - DIE at %s [in module %s]"),
15954 sect_offset_str (die->sect_off),
5e22e966 15955 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15956 return 0;
15957 }
15958 if ((align & (align - 1)) != 0)
15959 {
b98664d3 15960 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15961 " - DIE at %s [in module %s]"),
15962 sect_offset_str (die->sect_off),
5e22e966 15963 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15964 return 0;
15965 }
15966
15967 return align;
15968}
15969
15970/* If the DIE has a DW_AT_alignment attribute, use its value to set
15971 the alignment for TYPE. */
15972
15973static void
15974maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15975 struct type *type)
15976{
15977 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15978 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15979 " - DIE at %s [in module %s]"),
15980 sect_offset_str (die->sect_off),
5e22e966 15981 objfile_name (cu->per_objfile->objfile));
2b4424c3 15982}
685b1105 15983
e35000a7
TBA
15984/* Check if the given VALUE is a valid enum dwarf_calling_convention
15985 constant for a type, according to DWARF5 spec, Table 5.5. */
15986
15987static bool
15988is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15989{
15990 switch (value)
15991 {
15992 case DW_CC_normal:
15993 case DW_CC_pass_by_reference:
15994 case DW_CC_pass_by_value:
15995 return true;
15996
15997 default:
15998 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15999 "(%s) for a type"), pulongest (value));
e35000a7
TBA
16000 return false;
16001 }
16002}
16003
d0922fcf
TBA
16004/* Check if the given VALUE is a valid enum dwarf_calling_convention
16005 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16006 also according to GNU-specific values (see include/dwarf2.h). */
16007
16008static bool
16009is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16010{
16011 switch (value)
16012 {
16013 case DW_CC_normal:
16014 case DW_CC_program:
16015 case DW_CC_nocall:
16016 return true;
16017
16018 case DW_CC_GNU_renesas_sh:
16019 case DW_CC_GNU_borland_fastcall_i386:
16020 case DW_CC_GDB_IBM_OpenCL:
16021 return true;
16022
16023 default:
16024 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16025 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
16026 return false;
16027 }
16028}
16029
c906108c 16030/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
16031 (definition) to create a type for the structure or union. Fill in
16032 the type's name and general properties; the members will not be
83655187
DE
16033 processed until process_structure_scope. A symbol table entry for
16034 the type will also not be done until process_structure_scope (assuming
16035 the type has a name).
c906108c 16036
c767944b
DJ
16037 NOTE: we need to call these functions regardless of whether or not the
16038 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 16039 structure or union. This gets the type entered into our set of
83655187 16040 user defined types. */
c906108c 16041
f792889a 16042static struct type *
134d01f1 16043read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16044{
5e22e966 16045 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
16046 struct type *type;
16047 struct attribute *attr;
15d034d0 16048 const char *name;
c906108c 16049
348e048f
DE
16050 /* If the definition of this type lives in .debug_types, read that type.
16051 Don't follow DW_AT_specification though, that will take us back up
16052 the chain and we want to go down. */
052c8bb8 16053 attr = die->attr (DW_AT_signature);
435d3d88 16054 if (attr != nullptr)
348e048f 16055 {
ac9ec31b 16056 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16057
ac9ec31b 16058 /* The type's CU may not be the same as CU.
02142a6c 16059 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16060 return set_die_type (die, type, cu);
16061 }
16062
c0dd20ea 16063 type = alloc_type (objfile);
c906108c 16064 INIT_CPLUS_SPECIFIC (type);
93311388 16065
39cbfefa
DJ
16066 name = dwarf2_name (die, cu);
16067 if (name != NULL)
c906108c 16068 {
987504bb 16069 if (cu->language == language_cplus
c44af4eb
TT
16070 || cu->language == language_d
16071 || cu->language == language_rust)
63d06c5c 16072 {
15d034d0 16073 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
16074
16075 /* dwarf2_full_name might have already finished building the DIE's
16076 type. If so, there is no need to continue. */
16077 if (get_die_type (die, cu) != NULL)
16078 return get_die_type (die, cu);
16079
d0e39ea2 16080 type->set_name (full_name);
63d06c5c
DC
16081 }
16082 else
16083 {
d8151005
DJ
16084 /* The name is already allocated along with this objfile, so
16085 we don't need to duplicate it for the type. */
d0e39ea2 16086 type->set_name (name);
63d06c5c 16087 }
c906108c
SS
16088 }
16089
16090 if (die->tag == DW_TAG_structure_type)
16091 {
67607e24 16092 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16093 }
16094 else if (die->tag == DW_TAG_union_type)
16095 {
67607e24 16096 type->set_code (TYPE_CODE_UNION);
c906108c
SS
16097 }
16098 else
16099 {
67607e24 16100 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16101 }
16102
0cc2414c
TT
16103 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16104 TYPE_DECLARED_CLASS (type) = 1;
16105
e35000a7
TBA
16106 /* Store the calling convention in the type if it's available in
16107 the die. Otherwise the calling convention remains set to
16108 the default value DW_CC_normal. */
16109 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16110 if (attr != nullptr
529908cb 16111 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
16112 {
16113 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16114 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 16115 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
16116 }
16117
e142c38c 16118 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16119 if (attr != nullptr)
c906108c 16120 {
cd6c91b4 16121 if (attr->form_is_constant ())
dda83cd7 16122 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
16123 else
16124 {
f8e89861 16125 struct dynamic_prop prop;
293e7e51 16126 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 16127 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 16128 TYPE_LENGTH (type) = 0;
155bfbd3 16129 }
c906108c
SS
16130 }
16131 else
16132 {
16133 TYPE_LENGTH (type) = 0;
16134 }
16135
2b4424c3
TT
16136 maybe_set_alignment (cu, die, type);
16137
5230b05a 16138 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16139 {
5230b05a
WT
16140 /* ICC<14 does not output the required DW_AT_declaration on
16141 incomplete types, but gives them a size of zero. */
b4b73759 16142 type->set_is_stub (true);
685b1105
JK
16143 }
16144 else
9baccff6 16145 type->set_stub_is_supported (true);
685b1105 16146
dc718098 16147 if (die_is_declaration (die, cu))
b4b73759 16148 type->set_is_stub (true);
a6c727b2
DJ
16149 else if (attr == NULL && die->child == NULL
16150 && producer_is_realview (cu->producer))
16151 /* RealView does not output the required DW_AT_declaration
16152 on incomplete types. */
b4b73759 16153 type->set_is_stub (true);
dc718098 16154
c906108c
SS
16155 /* We need to add the type field to the die immediately so we don't
16156 infinitely recurse when dealing with pointers to the structure
0963b4bd 16157 type within the structure itself. */
1c379e20 16158 set_die_type (die, type, cu);
c906108c 16159
7e314c57
JK
16160 /* set_die_type should be already done. */
16161 set_descriptive_type (type, die, cu);
16162
c767944b
DJ
16163 return type;
16164}
16165
9c6a1327
TT
16166static void handle_struct_member_die
16167 (struct die_info *child_die,
16168 struct type *type,
16169 struct field_info *fi,
16170 std::vector<struct symbol *> *template_args,
16171 struct dwarf2_cu *cu);
16172
16173/* A helper for handle_struct_member_die that handles
16174 DW_TAG_variant_part. */
16175
16176static void
16177handle_variant_part (struct die_info *die, struct type *type,
16178 struct field_info *fi,
16179 std::vector<struct symbol *> *template_args,
16180 struct dwarf2_cu *cu)
16181{
16182 variant_part_builder *new_part;
16183 if (fi->current_variant_part == nullptr)
16184 {
16185 fi->variant_parts.emplace_back ();
16186 new_part = &fi->variant_parts.back ();
16187 }
16188 else if (!fi->current_variant_part->processing_variant)
16189 {
16190 complaint (_("nested DW_TAG_variant_part seen "
16191 "- DIE at %s [in module %s]"),
16192 sect_offset_str (die->sect_off),
5e22e966 16193 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16194 return;
16195 }
16196 else
16197 {
16198 variant_field &current = fi->current_variant_part->variants.back ();
16199 current.variant_parts.emplace_back ();
16200 new_part = &current.variant_parts.back ();
16201 }
16202
16203 /* When we recurse, we want callees to add to this new variant
16204 part. */
16205 scoped_restore save_current_variant_part
16206 = make_scoped_restore (&fi->current_variant_part, new_part);
16207
16208 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16209 if (discr == NULL)
16210 {
16211 /* It's a univariant form, an extension we support. */
16212 }
16213 else if (discr->form_is_ref ())
16214 {
16215 struct dwarf2_cu *target_cu = cu;
16216 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16217
16218 new_part->discriminant_offset = target_die->sect_off;
16219 }
16220 else
16221 {
16222 complaint (_("DW_AT_discr does not have DIE reference form"
16223 " - DIE at %s [in module %s]"),
16224 sect_offset_str (die->sect_off),
5e22e966 16225 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16226 }
16227
16228 for (die_info *child_die = die->child;
16229 child_die != NULL;
16230 child_die = child_die->sibling)
16231 handle_struct_member_die (child_die, type, fi, template_args, cu);
16232}
16233
16234/* A helper for handle_struct_member_die that handles
16235 DW_TAG_variant. */
16236
16237static void
16238handle_variant (struct die_info *die, struct type *type,
16239 struct field_info *fi,
16240 std::vector<struct symbol *> *template_args,
16241 struct dwarf2_cu *cu)
16242{
16243 if (fi->current_variant_part == nullptr)
16244 {
16245 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16246 "- DIE at %s [in module %s]"),
16247 sect_offset_str (die->sect_off),
5e22e966 16248 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16249 return;
16250 }
16251 if (fi->current_variant_part->processing_variant)
16252 {
16253 complaint (_("nested DW_TAG_variant seen "
16254 "- DIE at %s [in module %s]"),
16255 sect_offset_str (die->sect_off),
5e22e966 16256 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16257 return;
16258 }
16259
16260 scoped_restore save_processing_variant
16261 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16262 true);
16263
16264 fi->current_variant_part->variants.emplace_back ();
16265 variant_field &variant = fi->current_variant_part->variants.back ();
16266 variant.first_field = fi->fields.size ();
16267
16268 /* In a variant we want to get the discriminant and also add a
16269 field for our sole member child. */
16270 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16271 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16272 {
16273 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16274 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16275 variant.default_branch = true;
16276 else
9d2246fc 16277 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16278 }
16279 else
cae21f8e 16280 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16281
16282 for (die_info *variant_child = die->child;
16283 variant_child != NULL;
16284 variant_child = variant_child->sibling)
16285 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16286
16287 variant.last_field = fi->fields.size ();
16288}
16289
2ddeaf8a
TT
16290/* A helper for process_structure_scope that handles a single member
16291 DIE. */
16292
16293static void
16294handle_struct_member_die (struct die_info *child_die, struct type *type,
16295 struct field_info *fi,
16296 std::vector<struct symbol *> *template_args,
16297 struct dwarf2_cu *cu)
16298{
16299 if (child_die->tag == DW_TAG_member
9c6a1327 16300 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16301 {
16302 /* NOTE: carlton/2002-11-05: A C++ static data member
16303 should be a DW_TAG_member that is a declaration, but
16304 all versions of G++ as of this writing (so through at
16305 least 3.2.1) incorrectly generate DW_TAG_variable
16306 tags for them instead. */
16307 dwarf2_add_field (fi, child_die, cu);
16308 }
16309 else if (child_die->tag == DW_TAG_subprogram)
16310 {
16311 /* Rust doesn't have member functions in the C++ sense.
16312 However, it does emit ordinary functions as children
16313 of a struct DIE. */
16314 if (cu->language == language_rust)
16315 read_func_scope (child_die, cu);
16316 else
16317 {
16318 /* C++ member function. */
16319 dwarf2_add_member_fn (fi, child_die, type, cu);
16320 }
16321 }
16322 else if (child_die->tag == DW_TAG_inheritance)
16323 {
16324 /* C++ base class field. */
16325 dwarf2_add_field (fi, child_die, cu);
16326 }
16327 else if (type_can_define_types (child_die))
16328 dwarf2_add_type_defn (fi, child_die, cu);
16329 else if (child_die->tag == DW_TAG_template_type_param
16330 || child_die->tag == DW_TAG_template_value_param)
16331 {
16332 struct symbol *arg = new_symbol (child_die, NULL, cu);
16333
16334 if (arg != NULL)
16335 template_args->push_back (arg);
16336 }
9c6a1327
TT
16337 else if (child_die->tag == DW_TAG_variant_part)
16338 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16339 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16340 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16341}
16342
c767944b
DJ
16343/* Finish creating a structure or union type, including filling in
16344 its members and creating a symbol for it. */
16345
16346static void
16347process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16348{
5e22e966 16349 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16350 struct die_info *child_die;
c767944b
DJ
16351 struct type *type;
16352
16353 type = get_die_type (die, cu);
16354 if (type == NULL)
16355 type = read_structure_type (die, cu);
16356
3e1d3d8c 16357 bool has_template_parameters = false;
e142c38c 16358 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16359 {
16360 struct field_info fi;
2f4732b0 16361 std::vector<struct symbol *> template_args;
c906108c 16362
639d11d3 16363 child_die = die->child;
c906108c
SS
16364
16365 while (child_die && child_die->tag)
16366 {
2ddeaf8a 16367 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16368 child_die = child_die->sibling;
c906108c
SS
16369 }
16370
34eaf542 16371 /* Attach template arguments to type. */
2f4732b0 16372 if (!template_args.empty ())
34eaf542 16373 {
3e1d3d8c 16374 has_template_parameters = true;
34eaf542 16375 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16376 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16377 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16378 = XOBNEWVEC (&objfile->objfile_obstack,
16379 struct symbol *,
16380 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16381 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16382 template_args.data (),
34eaf542
TT
16383 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16384 * sizeof (struct symbol *)));
34eaf542
TT
16385 }
16386
c906108c 16387 /* Attach fields and member functions to the type. */
317f7127 16388 if (fi.nfields () > 0)
e7c27a73 16389 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16390 if (!fi.fnfieldlists.empty ())
c906108c 16391 {
e7c27a73 16392 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16393
c5aa993b 16394 /* Get the type which refers to the base class (possibly this
c906108c 16395 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16396 class from the DW_AT_containing_type attribute. This use of
16397 DW_AT_containing_type is a GNU extension. */
c906108c 16398
e142c38c 16399 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16400 {
e7c27a73 16401 struct type *t = die_containing_type (die, cu);
c906108c 16402
ae6ae975 16403 set_type_vptr_basetype (type, t);
c906108c
SS
16404 if (type == t)
16405 {
c906108c
SS
16406 int i;
16407
16408 /* Our own class provides vtbl ptr. */
1f704f76 16409 for (i = t->num_fields () - 1;
c906108c
SS
16410 i >= TYPE_N_BASECLASSES (t);
16411 --i)
16412 {
0d5cff50 16413 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16414
dda83cd7 16415 if (is_vtable_name (fieldname, cu))
c906108c 16416 {
ae6ae975 16417 set_type_vptr_fieldno (type, i);
c906108c
SS
16418 break;
16419 }
16420 }
16421
16422 /* Complain if virtual function table field not found. */
16423 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16424 complaint (_("virtual function table pointer "
3e43a32a 16425 "not found when defining class '%s'"),
7d93a1e0 16426 type->name () ? type->name () : "");
c906108c
SS
16427 }
16428 else
16429 {
ae6ae975 16430 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16431 }
16432 }
f6235d4c 16433 else if (cu->producer
61012eef 16434 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16435 {
16436 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16437 of the containing type, but the vtable pointer is
16438 always named __vfp. */
f6235d4c
EZ
16439
16440 int i;
16441
1f704f76 16442 for (i = type->num_fields () - 1;
f6235d4c
EZ
16443 i >= TYPE_N_BASECLASSES (type);
16444 --i)
16445 {
16446 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16447 {
ae6ae975
DE
16448 set_type_vptr_fieldno (type, i);
16449 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16450 break;
16451 }
16452 }
16453 }
c906108c 16454 }
98751a41
JK
16455
16456 /* Copy fi.typedef_field_list linked list elements content into the
16457 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16458 if (!fi.typedef_field_list.empty ())
98751a41 16459 {
be2daae6 16460 int count = fi.typedef_field_list.size ();
98751a41 16461
a0d7a4ff 16462 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16463 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16464 = ((struct decl_field *)
be2daae6
TT
16465 TYPE_ALLOC (type,
16466 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16467 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16468
be2daae6
TT
16469 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16470 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16471 }
c767944b 16472
883fd55a
KS
16473 /* Copy fi.nested_types_list linked list elements content into the
16474 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16475 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16476 {
be2daae6 16477 int count = fi.nested_types_list.size ();
883fd55a
KS
16478
16479 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16480 TYPE_NESTED_TYPES_ARRAY (type)
16481 = ((struct decl_field *)
be2daae6
TT
16482 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16483 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16484
be2daae6
TT
16485 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16486 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16487 }
c906108c 16488 }
63d06c5c 16489
bb5ed363 16490 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16491 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16492 cu->rust_unions.push_back (type);
57567375
TT
16493 else if (cu->language == language_ada)
16494 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16495
90aeadfc
DC
16496 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16497 snapshots) has been known to create a die giving a declaration
16498 for a class that has, as a child, a die giving a definition for a
16499 nested class. So we have to process our children even if the
16500 current die is a declaration. Normally, of course, a declaration
16501 won't have any children at all. */
134d01f1 16502
ca040673
DE
16503 child_die = die->child;
16504
90aeadfc
DC
16505 while (child_die != NULL && child_die->tag)
16506 {
16507 if (child_die->tag == DW_TAG_member
16508 || child_die->tag == DW_TAG_variable
34eaf542
TT
16509 || child_die->tag == DW_TAG_inheritance
16510 || child_die->tag == DW_TAG_template_value_param
16511 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16512 {
90aeadfc 16513 /* Do nothing. */
134d01f1 16514 }
90aeadfc
DC
16515 else
16516 process_die (child_die, cu);
134d01f1 16517
436c571c 16518 child_die = child_die->sibling;
134d01f1
DJ
16519 }
16520
fa4028e9
JB
16521 /* Do not consider external references. According to the DWARF standard,
16522 these DIEs are identified by the fact that they have no byte_size
16523 attribute, and a declaration attribute. */
16524 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16525 || !die_is_declaration (die, cu)
16526 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16527 {
16528 struct symbol *sym = new_symbol (die, type, cu);
16529
16530 if (has_template_parameters)
16531 {
a776957c
TT
16532 struct symtab *symtab;
16533 if (sym != nullptr)
16534 symtab = symbol_symtab (sym);
16535 else if (cu->line_header != nullptr)
16536 {
16537 /* Any related symtab will do. */
16538 symtab
7ba99d21 16539 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16540 }
16541 else
16542 {
16543 symtab = nullptr;
16544 complaint (_("could not find suitable "
16545 "symtab for template parameter"
16546 " - DIE at %s [in module %s]"),
16547 sect_offset_str (die->sect_off),
16548 objfile_name (objfile));
16549 }
16550
16551 if (symtab != nullptr)
16552 {
16553 /* Make sure that the symtab is set on the new symbols.
16554 Even though they don't appear in this symtab directly,
16555 other parts of gdb assume that symbols do, and this is
16556 reasonably true. */
16557 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16558 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16559 }
3e1d3d8c
TT
16560 }
16561 }
134d01f1
DJ
16562}
16563
ed6acedd
TT
16564/* Assuming DIE is an enumeration type, and TYPE is its associated
16565 type, update TYPE using some information only available in DIE's
16566 children. In particular, the fields are computed. */
55426c9d
JB
16567
16568static void
16569update_enumeration_type_from_children (struct die_info *die,
16570 struct type *type,
16571 struct dwarf2_cu *cu)
16572{
60f7655a 16573 struct die_info *child_die;
55426c9d
JB
16574 int unsigned_enum = 1;
16575 int flag_enum = 1;
55426c9d 16576
8268c778 16577 auto_obstack obstack;
ed6acedd 16578 std::vector<struct field> fields;
55426c9d 16579
60f7655a
DE
16580 for (child_die = die->child;
16581 child_die != NULL && child_die->tag;
436c571c 16582 child_die = child_die->sibling)
55426c9d
JB
16583 {
16584 struct attribute *attr;
16585 LONGEST value;
16586 const gdb_byte *bytes;
16587 struct dwarf2_locexpr_baton *baton;
16588 const char *name;
60f7655a 16589
55426c9d
JB
16590 if (child_die->tag != DW_TAG_enumerator)
16591 continue;
16592
16593 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16594 if (attr == NULL)
16595 continue;
16596
16597 name = dwarf2_name (child_die, cu);
16598 if (name == NULL)
16599 name = "<anonymous enumerator>";
16600
16601 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16602 &value, &bytes, &baton);
16603 if (value < 0)
16604 {
16605 unsigned_enum = 0;
16606 flag_enum = 0;
16607 }
55426c9d 16608 else
edd45eb0
SM
16609 {
16610 if (count_one_bits_ll (value) >= 2)
16611 flag_enum = 0;
edd45eb0 16612 }
55426c9d 16613
ed6acedd
TT
16614 fields.emplace_back ();
16615 struct field &field = fields.back ();
16616 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16617 SET_FIELD_ENUMVAL (field, value);
16618 }
16619
16620 if (!fields.empty ())
16621 {
5e33d5f4 16622 type->set_num_fields (fields.size ());
3cabb6b0
SM
16623 type->set_fields
16624 ((struct field *)
16625 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16626 memcpy (type->fields (), fields.data (),
ed6acedd 16627 sizeof (struct field) * fields.size ());
55426c9d
JB
16628 }
16629
16630 if (unsigned_enum)
653223d3
SM
16631 type->set_is_unsigned (true);
16632
55426c9d
JB
16633 if (flag_enum)
16634 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16635}
16636
134d01f1
DJ
16637/* Given a DW_AT_enumeration_type die, set its type. We do not
16638 complete the type's fields yet, or create any symbols. */
c906108c 16639
f792889a 16640static struct type *
134d01f1 16641read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16642{
5e22e966 16643 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16644 struct type *type;
c906108c 16645 struct attribute *attr;
0114d602 16646 const char *name;
134d01f1 16647
348e048f
DE
16648 /* If the definition of this type lives in .debug_types, read that type.
16649 Don't follow DW_AT_specification though, that will take us back up
16650 the chain and we want to go down. */
052c8bb8 16651 attr = die->attr (DW_AT_signature);
435d3d88 16652 if (attr != nullptr)
348e048f 16653 {
ac9ec31b 16654 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16655
ac9ec31b 16656 /* The type's CU may not be the same as CU.
02142a6c 16657 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16658 return set_die_type (die, type, cu);
16659 }
16660
c906108c
SS
16661 type = alloc_type (objfile);
16662
67607e24 16663 type->set_code (TYPE_CODE_ENUM);
94af9270 16664 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16665 if (name != NULL)
d0e39ea2 16666 type->set_name (name);
c906108c 16667
0626fc76
TT
16668 attr = dwarf2_attr (die, DW_AT_type, cu);
16669 if (attr != NULL)
16670 {
16671 struct type *underlying_type = die_type (die, cu);
16672
16673 TYPE_TARGET_TYPE (type) = underlying_type;
16674 }
16675
e142c38c 16676 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16677 if (attr != nullptr)
c906108c 16678 {
529908cb 16679 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16680 }
16681 else
16682 {
16683 TYPE_LENGTH (type) = 0;
16684 }
16685
2b4424c3
TT
16686 maybe_set_alignment (cu, die, type);
16687
137033e9
JB
16688 /* The enumeration DIE can be incomplete. In Ada, any type can be
16689 declared as private in the package spec, and then defined only
16690 inside the package body. Such types are known as Taft Amendment
16691 Types. When another package uses such a type, an incomplete DIE
16692 may be generated by the compiler. */
02eb380e 16693 if (die_is_declaration (die, cu))
b4b73759 16694 type->set_is_stub (true);
02eb380e 16695
0626fc76
TT
16696 /* If this type has an underlying type that is not a stub, then we
16697 may use its attributes. We always use the "unsigned" attribute
16698 in this situation, because ordinarily we guess whether the type
16699 is unsigned -- but the guess can be wrong and the underlying type
16700 can tell us the reality. However, we defer to a local size
16701 attribute if one exists, because this lets the compiler override
16702 the underlying type if needed. */
e46d3488 16703 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16704 {
9e7c9a03
HD
16705 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16706 underlying_type = check_typedef (underlying_type);
653223d3
SM
16707
16708 type->set_is_unsigned (underlying_type->is_unsigned ());
16709
0626fc76 16710 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16711 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16712
2b4424c3 16713 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16714 && TYPE_RAW_ALIGN (underlying_type) != 0)
16715 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16716 }
16717
3d567982
TT
16718 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16719
ed6acedd
TT
16720 set_die_type (die, type, cu);
16721
16722 /* Finish the creation of this type by using the enum's children.
16723 Note that, as usual, this must come after set_die_type to avoid
16724 infinite recursion when trying to compute the names of the
16725 enumerators. */
16726 update_enumeration_type_from_children (die, type, cu);
16727
16728 return type;
134d01f1
DJ
16729}
16730
16731/* Given a pointer to a die which begins an enumeration, process all
16732 the dies that define the members of the enumeration, and create the
16733 symbol for the enumeration type.
16734
16735 NOTE: We reverse the order of the element list. */
16736
16737static void
16738process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16739{
f792889a 16740 struct type *this_type;
134d01f1 16741
f792889a
DJ
16742 this_type = get_die_type (die, cu);
16743 if (this_type == NULL)
16744 this_type = read_enumeration_type (die, cu);
9dc481d3 16745
639d11d3 16746 if (die->child != NULL)
c906108c 16747 {
9dc481d3 16748 struct die_info *child_die;
15d034d0 16749 const char *name;
9dc481d3 16750
639d11d3 16751 child_die = die->child;
c906108c
SS
16752 while (child_die && child_die->tag)
16753 {
16754 if (child_die->tag != DW_TAG_enumerator)
16755 {
e7c27a73 16756 process_die (child_die, cu);
c906108c
SS
16757 }
16758 else
16759 {
39cbfefa
DJ
16760 name = dwarf2_name (child_die, cu);
16761 if (name)
ed6acedd 16762 new_symbol (child_die, this_type, cu);
c906108c
SS
16763 }
16764
436c571c 16765 child_die = child_die->sibling;
c906108c 16766 }
c906108c 16767 }
134d01f1 16768
6c83ed52
TT
16769 /* If we are reading an enum from a .debug_types unit, and the enum
16770 is a declaration, and the enum is not the signatured type in the
16771 unit, then we do not want to add a symbol for it. Adding a
16772 symbol would in some cases obscure the true definition of the
16773 enum, giving users an incomplete type when the definition is
16774 actually available. Note that we do not want to do this for all
16775 enums which are just declarations, because C++0x allows forward
16776 enum declarations. */
3019eac3 16777 if (cu->per_cu->is_debug_types
6c83ed52
TT
16778 && die_is_declaration (die, cu))
16779 {
52dc124a 16780 struct signatured_type *sig_type;
6c83ed52 16781
c0f78cd4 16782 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16783 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16784 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16785 return;
16786 }
16787
f792889a 16788 new_symbol (die, this_type, cu);
c906108c
SS
16789}
16790
57567375
TT
16791/* Helper function for quirk_ada_thick_pointer that examines a bounds
16792 expression for an index type and finds the corresponding field
16793 offset in the hidden "P_BOUNDS" structure. Returns true on success
16794 and updates *FIELD, false if it fails to recognize an
16795 expression. */
16796
16797static bool
16798recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16799 int *bounds_offset, struct field *field,
16800 struct dwarf2_cu *cu)
16801{
16802 struct attribute *attr = dwarf2_attr (die, name, cu);
16803 if (attr == nullptr || !attr->form_is_block ())
16804 return false;
16805
16806 const struct dwarf_block *block = attr->as_block ();
16807 const gdb_byte *start = block->data;
16808 const gdb_byte *end = block->data + block->size;
16809
16810 /* The expression to recognize generally looks like:
16811
16812 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16813 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16814
16815 However, the second "plus_uconst" may be missing:
16816
16817 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16818 DW_OP_deref_size: 4)
16819
16820 This happens when the field is at the start of the structure.
16821
16822 Also, the final deref may not be sized:
16823
16824 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16825 DW_OP_deref)
16826
16827 This happens when the size of the index type happens to be the
16828 same as the architecture's word size. This can occur with or
16829 without the second plus_uconst. */
16830
16831 if (end - start < 2)
16832 return false;
16833 if (*start++ != DW_OP_push_object_address)
16834 return false;
16835 if (*start++ != DW_OP_plus_uconst)
16836 return false;
16837
16838 uint64_t this_bound_off;
16839 start = gdb_read_uleb128 (start, end, &this_bound_off);
16840 if (start == nullptr || (int) this_bound_off != this_bound_off)
16841 return false;
16842 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16843 is consistent among all bounds. */
16844 if (*bounds_offset == -1)
16845 *bounds_offset = this_bound_off;
16846 else if (*bounds_offset != this_bound_off)
16847 return false;
16848
16849 if (start == end || *start++ != DW_OP_deref)
16850 return false;
16851
16852 int offset = 0;
16853 if (start ==end)
16854 return false;
16855 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16856 {
16857 /* This means an offset of 0. */
16858 }
16859 else if (*start++ != DW_OP_plus_uconst)
16860 return false;
16861 else
16862 {
16863 /* The size is the parameter to DW_OP_plus_uconst. */
16864 uint64_t val;
16865 start = gdb_read_uleb128 (start, end, &val);
16866 if (start == nullptr)
16867 return false;
16868 if ((int) val != val)
16869 return false;
16870 offset = val;
16871 }
16872
16873 if (start == end)
16874 return false;
16875
16876 uint64_t size;
16877 if (*start == DW_OP_deref_size)
16878 {
16879 start = gdb_read_uleb128 (start + 1, end, &size);
16880 if (start == nullptr)
16881 return false;
16882 }
16883 else if (*start == DW_OP_deref)
16884 {
16885 size = cu->header.addr_size;
16886 ++start;
16887 }
16888 else
16889 return false;
16890
16891 SET_FIELD_BITPOS (*field, 8 * offset);
16892 if (size != TYPE_LENGTH (field->type ()))
16893 FIELD_BITSIZE (*field) = 8 * size;
16894
16895 return true;
16896}
16897
16898/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16899 some kinds of Ada arrays:
16900
16901 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16902 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16903 <11e0> DW_AT_data_location: 2 byte block: 97 6
16904 (DW_OP_push_object_address; DW_OP_deref)
16905 <11e3> DW_AT_type : <0x1173>
16906 <11e7> DW_AT_sibling : <0x1201>
16907 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16908 <11ec> DW_AT_type : <0x1206>
16909 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16910 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16911 DW_OP_deref_size: 4)
16912 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16913 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16914 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16915
16916 This actually represents a "thick pointer", which is a structure
16917 with two elements: one that is a pointer to the array data, and one
16918 that is a pointer to another structure; this second structure holds
16919 the array bounds.
16920
16921 This returns a new type on success, or nullptr if this didn't
16922 recognize the type. */
16923
16924static struct type *
16925quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16926 struct type *type)
16927{
16928 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16929 /* So far we've only seen this with block form. */
16930 if (attr == nullptr || !attr->form_is_block ())
16931 return nullptr;
16932
16933 /* Note that this will fail if the structure layout is changed by
16934 the compiler. However, we have no good way to recognize some
16935 other layout, because we don't know what expression the compiler
16936 might choose to emit should this happen. */
16937 struct dwarf_block *blk = attr->as_block ();
16938 if (blk->size != 2
16939 || blk->data[0] != DW_OP_push_object_address
16940 || blk->data[1] != DW_OP_deref)
16941 return nullptr;
16942
16943 int bounds_offset = -1;
16944 int max_align = -1;
16945 std::vector<struct field> range_fields;
16946 for (struct die_info *child_die = die->child;
16947 child_die;
16948 child_die = child_die->sibling)
16949 {
16950 if (child_die->tag == DW_TAG_subrange_type)
16951 {
16952 struct type *underlying = read_subrange_index_type (child_die, cu);
16953
16954 int this_align = type_align (underlying);
16955 if (this_align > max_align)
16956 max_align = this_align;
16957
16958 range_fields.emplace_back ();
16959 range_fields.emplace_back ();
16960
16961 struct field &lower = range_fields[range_fields.size () - 2];
16962 struct field &upper = range_fields[range_fields.size () - 1];
16963
16964 lower.set_type (underlying);
16965 FIELD_ARTIFICIAL (lower) = 1;
16966
16967 upper.set_type (underlying);
16968 FIELD_ARTIFICIAL (upper) = 1;
16969
16970 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16971 &bounds_offset, &lower, cu)
16972 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16973 &bounds_offset, &upper, cu))
16974 return nullptr;
16975 }
16976 }
16977
16978 /* This shouldn't really happen, but double-check that we found
16979 where the bounds are stored. */
16980 if (bounds_offset == -1)
16981 return nullptr;
16982
16983 struct objfile *objfile = cu->per_objfile->objfile;
16984 for (int i = 0; i < range_fields.size (); i += 2)
16985 {
16986 char name[20];
16987
16988 /* Set the name of each field in the bounds. */
16989 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16990 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16991 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16992 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16993 }
16994
16995 struct type *bounds = alloc_type (objfile);
16996 bounds->set_code (TYPE_CODE_STRUCT);
16997
16998 bounds->set_num_fields (range_fields.size ());
16999 bounds->set_fields
17000 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17001 * sizeof (struct field))));
17002 memcpy (bounds->fields (), range_fields.data (),
17003 bounds->num_fields () * sizeof (struct field));
17004
17005 int last_fieldno = range_fields.size () - 1;
17006 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17007 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17008 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17009
17010 /* Rewrite the existing array type in place. Specifically, we
17011 remove any dynamic properties we might have read, and we replace
17012 the index types. */
17013 struct type *iter = type;
17014 for (int i = 0; i < range_fields.size (); i += 2)
17015 {
17016 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17017 iter->main_type->dyn_prop_list = nullptr;
17018 iter->set_index_type
17019 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17020 iter = TYPE_TARGET_TYPE (iter);
17021 }
17022
17023 struct type *result = alloc_type (objfile);
17024 result->set_code (TYPE_CODE_STRUCT);
17025
17026 result->set_num_fields (2);
17027 result->set_fields
17028 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17029 * sizeof (struct field))));
17030
17031 /* The names are chosen to coincide with what the compiler does with
17032 -fgnat-encodings=all, which the Ada code in gdb already
17033 understands. */
17034 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17035 result->field (0).set_type (lookup_pointer_type (type));
17036
17037 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17038 result->field (1).set_type (lookup_pointer_type (bounds));
17039 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17040
17041 result->set_name (type->name ());
17042 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17043 + TYPE_LENGTH (result->field (1).type ()));
17044
17045 return result;
17046}
17047
c906108c
SS
17048/* Extract all information from a DW_TAG_array_type DIE and put it in
17049 the DIE's type field. For now, this only handles one dimensional
17050 arrays. */
17051
f792889a 17052static struct type *
e7c27a73 17053read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17054{
5e22e966 17055 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 17056 struct die_info *child_die;
7e314c57 17057 struct type *type;
c906108c 17058 struct type *element_type, *range_type, *index_type;
c906108c 17059 struct attribute *attr;
15d034d0 17060 const char *name;
a405673c 17061 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 17062 unsigned int bit_stride = 0;
c906108c 17063
e7c27a73 17064 element_type = die_type (die, cu);
c906108c 17065
7e314c57
JK
17066 /* The die_type call above may have already set the type for this DIE. */
17067 type = get_die_type (die, cu);
17068 if (type)
17069 return type;
17070
dc53a7ad
JB
17071 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17072 if (attr != NULL)
a405673c
JB
17073 {
17074 int stride_ok;
293e7e51 17075 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
17076
17077 byte_stride_prop
17078 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
17079 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17080 prop_type);
a405673c
JB
17081 if (!stride_ok)
17082 {
b98664d3 17083 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
17084 " - DIE at %s [in module %s]"),
17085 sect_offset_str (die->sect_off),
5e22e966 17086 objfile_name (cu->per_objfile->objfile));
a405673c
JB
17087 /* Ignore this attribute. We will likely not be able to print
17088 arrays of this type correctly, but there is little we can do
17089 to help if we cannot read the attribute's value. */
17090 byte_stride_prop = NULL;
17091 }
17092 }
dc53a7ad
JB
17093
17094 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17095 if (attr != NULL)
529908cb 17096 bit_stride = attr->constant_value (0);
dc53a7ad 17097
c906108c
SS
17098 /* Irix 6.2 native cc creates array types without children for
17099 arrays with unspecified length. */
639d11d3 17100 if (die->child == NULL)
c906108c 17101 {
46bf5051 17102 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17103 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 17104 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 17105 byte_stride_prop, bit_stride);
f792889a 17106 return set_die_type (die, type, cu);
c906108c
SS
17107 }
17108
791afaa2 17109 std::vector<struct type *> range_types;
639d11d3 17110 child_die = die->child;
c906108c
SS
17111 while (child_die && child_die->tag)
17112 {
17113 if (child_die->tag == DW_TAG_subrange_type)
17114 {
f792889a 17115 struct type *child_type = read_type_die (child_die, cu);
9a619af0 17116
dda83cd7
SM
17117 if (child_type != NULL)
17118 {
0963b4bd 17119 /* The range type was succesfully read. Save it for the
dda83cd7 17120 array type creation. */
791afaa2 17121 range_types.push_back (child_type);
dda83cd7 17122 }
c906108c 17123 }
436c571c 17124 child_die = child_die->sibling;
c906108c
SS
17125 }
17126
cf2b2075
TV
17127 if (range_types.empty ())
17128 {
17129 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17130 sect_offset_str (die->sect_off),
17131 objfile_name (cu->per_objfile->objfile));
17132 return NULL;
17133 }
17134
c906108c
SS
17135 /* Dwarf2 dimensions are output from left to right, create the
17136 necessary array types in backwards order. */
7ca2d3a3 17137
c906108c 17138 type = element_type;
7ca2d3a3
DL
17139
17140 if (read_array_order (die, cu) == DW_ORD_col_major)
17141 {
17142 int i = 0;
9a619af0 17143
791afaa2 17144 while (i < range_types.size ())
10f6a3ad
TT
17145 {
17146 type = create_array_type_with_stride (NULL, type, range_types[i++],
17147 byte_stride_prop, bit_stride);
17148 bit_stride = 0;
17149 byte_stride_prop = nullptr;
17150 }
7ca2d3a3
DL
17151 }
17152 else
17153 {
791afaa2 17154 size_t ndim = range_types.size ();
7ca2d3a3 17155 while (ndim-- > 0)
10f6a3ad
TT
17156 {
17157 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17158 byte_stride_prop, bit_stride);
17159 bit_stride = 0;
17160 byte_stride_prop = nullptr;
17161 }
7ca2d3a3 17162 }
c906108c 17163
cf2b2075
TV
17164 gdb_assert (type != element_type);
17165
f5f8a009
EZ
17166 /* Understand Dwarf2 support for vector types (like they occur on
17167 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17168 array type. This is not part of the Dwarf2/3 standard yet, but a
17169 custom vendor extension. The main difference between a regular
17170 array and the vector variant is that vectors are passed by value
17171 to functions. */
e142c38c 17172 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 17173 if (attr != nullptr)
ea37ba09 17174 make_vector_type (type);
f5f8a009 17175
dbc98a8b
KW
17176 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17177 implementation may choose to implement triple vectors using this
17178 attribute. */
17179 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 17180 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 17181 {
529908cb
TT
17182 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17183 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 17184 else
b98664d3 17185 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 17186 "than the total size of elements"));
dbc98a8b
KW
17187 }
17188
39cbfefa
DJ
17189 name = dwarf2_name (die, cu);
17190 if (name)
d0e39ea2 17191 type->set_name (name);
6e70227d 17192
2b4424c3
TT
17193 maybe_set_alignment (cu, die, type);
17194
57567375
TT
17195 struct type *replacement_type = nullptr;
17196 if (cu->language == language_ada)
17197 {
17198 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17199 if (replacement_type != nullptr)
17200 type = replacement_type;
17201 }
17202
0963b4bd 17203 /* Install the type in the die. */
57567375 17204 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
17205
17206 /* set_die_type should be already done. */
b4ba55a1
JB
17207 set_descriptive_type (type, die, cu);
17208
7e314c57 17209 return type;
c906108c
SS
17210}
17211
7ca2d3a3 17212static enum dwarf_array_dim_ordering
6e70227d 17213read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17214{
17215 struct attribute *attr;
17216
17217 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17218
435d3d88 17219 if (attr != nullptr)
1bc397c5
TT
17220 {
17221 LONGEST val = attr->constant_value (-1);
17222 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17223 return (enum dwarf_array_dim_ordering) val;
17224 }
7ca2d3a3 17225
0963b4bd
MS
17226 /* GNU F77 is a special case, as at 08/2004 array type info is the
17227 opposite order to the dwarf2 specification, but data is still
17228 laid out as per normal fortran.
7ca2d3a3 17229
0963b4bd
MS
17230 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17231 version checking. */
7ca2d3a3 17232
905e0470
PM
17233 if (cu->language == language_fortran
17234 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17235 {
17236 return DW_ORD_row_major;
17237 }
17238
3a3440fb 17239 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
17240 {
17241 case array_column_major:
17242 return DW_ORD_col_major;
17243 case array_row_major:
17244 default:
17245 return DW_ORD_row_major;
17246 };
17247}
17248
72019c9c 17249/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17250 the DIE's type field. */
72019c9c 17251
f792889a 17252static struct type *
72019c9c
GM
17253read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17254{
7e314c57
JK
17255 struct type *domain_type, *set_type;
17256 struct attribute *attr;
f792889a 17257
7e314c57
JK
17258 domain_type = die_type (die, cu);
17259
17260 /* The die_type call above may have already set the type for this DIE. */
17261 set_type = get_die_type (die, cu);
17262 if (set_type)
17263 return set_type;
17264
17265 set_type = create_set_type (NULL, domain_type);
17266
17267 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17268 if (attr != nullptr && attr->form_is_unsigned ())
17269 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17270
2b4424c3
TT
17271 maybe_set_alignment (cu, die, set_type);
17272
f792889a 17273 return set_die_type (die, set_type, cu);
72019c9c 17274}
7ca2d3a3 17275
0971de02
TT
17276/* A helper for read_common_block that creates a locexpr baton.
17277 SYM is the symbol which we are marking as computed.
17278 COMMON_DIE is the DIE for the common block.
17279 COMMON_LOC is the location expression attribute for the common
17280 block itself.
17281 MEMBER_LOC is the location expression attribute for the particular
17282 member of the common block that we are processing.
17283 CU is the CU from which the above come. */
17284
17285static void
17286mark_common_block_symbol_computed (struct symbol *sym,
17287 struct die_info *common_die,
17288 struct attribute *common_loc,
17289 struct attribute *member_loc,
17290 struct dwarf2_cu *cu)
17291{
5e22e966 17292 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17293 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17294 struct dwarf2_locexpr_baton *baton;
17295 gdb_byte *ptr;
17296 unsigned int cu_off;
08feed99 17297 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17298 LONGEST offset = 0;
17299
17300 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17301 gdb_assert (common_loc->form_is_block ());
17302 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17303 || member_loc->form_is_constant ());
0971de02 17304
8d749320 17305 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17306 baton->per_objfile = per_objfile;
0971de02
TT
17307 baton->per_cu = cu->per_cu;
17308 gdb_assert (baton->per_cu);
17309
17310 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17311
cd6c91b4 17312 if (member_loc->form_is_constant ())
0971de02 17313 {
0826b30a 17314 offset = member_loc->constant_value (0);
0971de02
TT
17315 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17316 }
17317 else
9d2246fc 17318 baton->size += member_loc->as_block ()->size;
0971de02 17319
224c3ddb 17320 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17321 baton->data = ptr;
17322
17323 *ptr++ = DW_OP_call4;
9c541725 17324 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17325 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17326 ptr += 4;
17327
cd6c91b4 17328 if (member_loc->form_is_constant ())
0971de02
TT
17329 {
17330 *ptr++ = DW_OP_addr;
17331 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17332 ptr += cu->header.addr_size;
17333 }
17334 else
17335 {
17336 /* We have to copy the data here, because DW_OP_call4 will only
17337 use a DW_AT_location attribute. */
9d2246fc
TT
17338 struct dwarf_block *block = member_loc->as_block ();
17339 memcpy (ptr, block->data, block->size);
17340 ptr += block->size;
0971de02
TT
17341 }
17342
17343 *ptr++ = DW_OP_plus;
17344 gdb_assert (ptr - baton->data == baton->size);
17345
0971de02 17346 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17347 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17348}
17349
4357ac6c
TT
17350/* Create appropriate locally-scoped variables for all the
17351 DW_TAG_common_block entries. Also create a struct common_block
17352 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17353 is used to separate the common blocks name namespace from regular
4357ac6c 17354 variable names. */
c906108c
SS
17355
17356static void
e7c27a73 17357read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17358{
0971de02
TT
17359 struct attribute *attr;
17360
17361 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17362 if (attr != nullptr)
0971de02
TT
17363 {
17364 /* Support the .debug_loc offsets. */
4fc6c0d5 17365 if (attr->form_is_block ())
dda83cd7 17366 {
0971de02 17367 /* Ok. */
dda83cd7 17368 }
cd6c91b4 17369 else if (attr->form_is_section_offset ())
dda83cd7 17370 {
0971de02
TT
17371 dwarf2_complex_location_expr_complaint ();
17372 attr = NULL;
dda83cd7 17373 }
0971de02 17374 else
dda83cd7 17375 {
0971de02
TT
17376 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17377 "common block member");
17378 attr = NULL;
dda83cd7 17379 }
0971de02
TT
17380 }
17381
639d11d3 17382 if (die->child != NULL)
c906108c 17383 {
5e22e966 17384 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17385 struct die_info *child_die;
17386 size_t n_entries = 0, size;
17387 struct common_block *common_block;
17388 struct symbol *sym;
74ac6d43 17389
4357ac6c
TT
17390 for (child_die = die->child;
17391 child_die && child_die->tag;
436c571c 17392 child_die = child_die->sibling)
4357ac6c
TT
17393 ++n_entries;
17394
17395 size = (sizeof (struct common_block)
17396 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17397 common_block
17398 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17399 size);
4357ac6c
TT
17400 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17401 common_block->n_entries = 0;
17402
17403 for (child_die = die->child;
17404 child_die && child_die->tag;
436c571c 17405 child_die = child_die->sibling)
4357ac6c
TT
17406 {
17407 /* Create the symbol in the DW_TAG_common_block block in the current
17408 symbol scope. */
e7c27a73 17409 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17410 if (sym != NULL)
17411 {
17412 struct attribute *member_loc;
17413
17414 common_block->contents[common_block->n_entries++] = sym;
17415
17416 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17417 cu);
17418 if (member_loc)
17419 {
17420 /* GDB has handled this for a long time, but it is
17421 not specified by DWARF. It seems to have been
17422 emitted by gfortran at least as recently as:
17423 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17424 complaint (_("Variable in common block has "
0971de02 17425 "DW_AT_data_member_location "
9d8780f0
SM
17426 "- DIE at %s [in module %s]"),
17427 sect_offset_str (child_die->sect_off),
518817b3 17428 objfile_name (objfile));
0971de02 17429
cd6c91b4 17430 if (member_loc->form_is_section_offset ())
0971de02 17431 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17432 else if (member_loc->form_is_constant ()
4fc6c0d5 17433 || member_loc->form_is_block ())
0971de02 17434 {
435d3d88 17435 if (attr != nullptr)
0971de02
TT
17436 mark_common_block_symbol_computed (sym, die, attr,
17437 member_loc, cu);
17438 }
17439 else
17440 dwarf2_complex_location_expr_complaint ();
17441 }
17442 }
c906108c 17443 }
4357ac6c
TT
17444
17445 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17446 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17447 }
17448}
17449
0114d602 17450/* Create a type for a C++ namespace. */
d9fa45fe 17451
0114d602
DJ
17452static struct type *
17453read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17454{
5e22e966 17455 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17456 const char *previous_prefix, *name;
9219021c 17457 int is_anonymous;
0114d602
DJ
17458 struct type *type;
17459
17460 /* For extensions, reuse the type of the original namespace. */
17461 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17462 {
17463 struct die_info *ext_die;
17464 struct dwarf2_cu *ext_cu = cu;
9a619af0 17465
0114d602
DJ
17466 ext_die = dwarf2_extension (die, &ext_cu);
17467 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17468
17469 /* EXT_CU may not be the same as CU.
02142a6c 17470 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17471 return set_die_type (die, type, cu);
17472 }
9219021c 17473
e142c38c 17474 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17475
17476 /* Now build the name of the current namespace. */
17477
0114d602
DJ
17478 previous_prefix = determine_prefix (die, cu);
17479 if (previous_prefix[0] != '\0')
17480 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17481 previous_prefix, name, 0, cu);
0114d602
DJ
17482
17483 /* Create the type. */
19f392bc 17484 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17485
60531b24 17486 return set_die_type (die, type, cu);
0114d602
DJ
17487}
17488
22cee43f 17489/* Read a namespace scope. */
0114d602
DJ
17490
17491static void
17492read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17493{
5e22e966 17494 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17495 int is_anonymous;
9219021c 17496
5c4e30ca
DC
17497 /* Add a symbol associated to this if we haven't seen the namespace
17498 before. Also, add a using directive if it's an anonymous
17499 namespace. */
9219021c 17500
f2f0e013 17501 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17502 {
17503 struct type *type;
17504
0114d602 17505 type = read_type_die (die, cu);
e7c27a73 17506 new_symbol (die, type, cu);
5c4e30ca 17507
e8e80198 17508 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17509 if (is_anonymous)
0114d602
DJ
17510 {
17511 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17512
eb1e02fd 17513 std::vector<const char *> excludes;
804d2729 17514 add_using_directive (using_directives (cu),
7d93a1e0 17515 previous_prefix, type->name (), NULL,
eb1e02fd 17516 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17517 }
5c4e30ca 17518 }
9219021c 17519
639d11d3 17520 if (die->child != NULL)
d9fa45fe 17521 {
639d11d3 17522 struct die_info *child_die = die->child;
6e70227d 17523
d9fa45fe
DC
17524 while (child_die && child_die->tag)
17525 {
e7c27a73 17526 process_die (child_die, cu);
436c571c 17527 child_die = child_die->sibling;
d9fa45fe
DC
17528 }
17529 }
38d518c9
EZ
17530}
17531
f55ee35c
JK
17532/* Read a Fortran module as type. This DIE can be only a declaration used for
17533 imported module. Still we need that type as local Fortran "use ... only"
17534 declaration imports depend on the created type in determine_prefix. */
17535
17536static struct type *
17537read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17538{
5e22e966 17539 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17540 const char *module_name;
f55ee35c
JK
17541 struct type *type;
17542
17543 module_name = dwarf2_name (die, cu);
19f392bc 17544 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17545
f55ee35c
JK
17546 return set_die_type (die, type, cu);
17547}
17548
5d7cb8df
JK
17549/* Read a Fortran module. */
17550
17551static void
17552read_module (struct die_info *die, struct dwarf2_cu *cu)
17553{
17554 struct die_info *child_die = die->child;
530e8392
KB
17555 struct type *type;
17556
17557 type = read_type_die (die, cu);
17558 new_symbol (die, type, cu);
5d7cb8df 17559
5d7cb8df
JK
17560 while (child_die && child_die->tag)
17561 {
17562 process_die (child_die, cu);
436c571c 17563 child_die = child_die->sibling;
5d7cb8df
JK
17564 }
17565}
17566
38d518c9
EZ
17567/* Return the name of the namespace represented by DIE. Set
17568 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17569 namespace. */
17570
17571static const char *
e142c38c 17572namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17573{
17574 struct die_info *current_die;
17575 const char *name = NULL;
17576
17577 /* Loop through the extensions until we find a name. */
17578
17579 for (current_die = die;
17580 current_die != NULL;
f2f0e013 17581 current_die = dwarf2_extension (die, &cu))
38d518c9 17582 {
96553a0c
DE
17583 /* We don't use dwarf2_name here so that we can detect the absence
17584 of a name -> anonymous namespace. */
7d45c7c3 17585 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17586
38d518c9
EZ
17587 if (name != NULL)
17588 break;
17589 }
17590
17591 /* Is it an anonymous namespace? */
17592
17593 *is_anonymous = (name == NULL);
17594 if (*is_anonymous)
2b1dbab0 17595 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17596
17597 return name;
d9fa45fe
DC
17598}
17599
c906108c
SS
17600/* Extract all information from a DW_TAG_pointer_type DIE and add to
17601 the user defined type vector. */
17602
f792889a 17603static struct type *
e7c27a73 17604read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17605{
5e22e966 17606 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17607 struct comp_unit_head *cu_header = &cu->header;
c906108c 17608 struct type *type;
8b2dbe47
KB
17609 struct attribute *attr_byte_size;
17610 struct attribute *attr_address_class;
17611 int byte_size, addr_class;
7e314c57
JK
17612 struct type *target_type;
17613
17614 target_type = die_type (die, cu);
c906108c 17615
7e314c57
JK
17616 /* The die_type call above may have already set the type for this DIE. */
17617 type = get_die_type (die, cu);
17618 if (type)
17619 return type;
17620
17621 type = lookup_pointer_type (target_type);
8b2dbe47 17622
e142c38c 17623 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17624 if (attr_byte_size)
529908cb 17625 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17626 else
8b2dbe47
KB
17627 byte_size = cu_header->addr_size;
17628
e142c38c 17629 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17630 if (attr_address_class)
529908cb 17631 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17632 else
17633 addr_class = DW_ADDR_none;
17634
2b4424c3
TT
17635 ULONGEST alignment = get_alignment (cu, die);
17636
17637 /* If the pointer size, alignment, or address class is different
17638 than the default, create a type variant marked as such and set
17639 the length accordingly. */
17640 if (TYPE_LENGTH (type) != byte_size
17641 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17642 && alignment != TYPE_RAW_ALIGN (type))
17643 || addr_class != DW_ADDR_none)
c906108c 17644 {
5e2b427d 17645 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17646 {
314ad88d
PA
17647 type_instance_flags type_flags
17648 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17649 addr_class);
876cecd0
TT
17650 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17651 == 0);
8b2dbe47
KB
17652 type = make_type_with_address_space (type, type_flags);
17653 }
17654 else if (TYPE_LENGTH (type) != byte_size)
17655 {
b98664d3 17656 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17657 }
2b4424c3
TT
17658 else if (TYPE_RAW_ALIGN (type) != alignment)
17659 {
b98664d3 17660 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17661 " - DIE at %s [in module %s]"),
17662 sect_offset_str (die->sect_off),
5e22e966 17663 objfile_name (cu->per_objfile->objfile));
2b4424c3 17664 }
6e70227d 17665 else
9a619af0
MS
17666 {
17667 /* Should we also complain about unhandled address classes? */
17668 }
c906108c 17669 }
8b2dbe47
KB
17670
17671 TYPE_LENGTH (type) = byte_size;
2b4424c3 17672 set_type_align (type, alignment);
f792889a 17673 return set_die_type (die, type, cu);
c906108c
SS
17674}
17675
17676/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17677 the user defined type vector. */
17678
f792889a 17679static struct type *
e7c27a73 17680read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17681{
17682 struct type *type;
17683 struct type *to_type;
17684 struct type *domain;
17685
e7c27a73
DJ
17686 to_type = die_type (die, cu);
17687 domain = die_containing_type (die, cu);
0d5de010 17688
7e314c57
JK
17689 /* The calls above may have already set the type for this DIE. */
17690 type = get_die_type (die, cu);
17691 if (type)
17692 return type;
17693
78134374 17694 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17695 type = lookup_methodptr_type (to_type);
78134374 17696 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17697 {
5e22e966 17698 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17699
17700 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17701 to_type->fields (), to_type->num_fields (),
a409645d 17702 to_type->has_varargs ());
7078baeb
TT
17703 type = lookup_methodptr_type (new_type);
17704 }
0d5de010
DJ
17705 else
17706 type = lookup_memberptr_type (to_type, domain);
c906108c 17707
f792889a 17708 return set_die_type (die, type, cu);
c906108c
SS
17709}
17710
4297a3f0 17711/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17712 the user defined type vector. */
17713
f792889a 17714static struct type *
4297a3f0 17715read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17716 enum type_code refcode)
c906108c 17717{
e7c27a73 17718 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17719 struct type *type, *target_type;
c906108c
SS
17720 struct attribute *attr;
17721
4297a3f0
AV
17722 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17723
7e314c57
JK
17724 target_type = die_type (die, cu);
17725
17726 /* The die_type call above may have already set the type for this DIE. */
17727 type = get_die_type (die, cu);
17728 if (type)
17729 return type;
17730
4297a3f0 17731 type = lookup_reference_type (target_type, refcode);
e142c38c 17732 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17733 if (attr != nullptr)
c906108c 17734 {
529908cb 17735 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17736 }
17737 else
17738 {
107d2387 17739 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17740 }
2b4424c3 17741 maybe_set_alignment (cu, die, type);
f792889a 17742 return set_die_type (die, type, cu);
c906108c
SS
17743}
17744
cf363f18
MW
17745/* Add the given cv-qualifiers to the element type of the array. GCC
17746 outputs DWARF type qualifiers that apply to an array, not the
17747 element type. But GDB relies on the array element type to carry
17748 the cv-qualifiers. This mimics section 6.7.3 of the C99
17749 specification. */
17750
17751static struct type *
17752add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17753 struct type *base_type, int cnst, int voltl)
17754{
17755 struct type *el_type, *inner_array;
17756
17757 base_type = copy_type (base_type);
17758 inner_array = base_type;
17759
78134374 17760 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17761 {
17762 TYPE_TARGET_TYPE (inner_array) =
17763 copy_type (TYPE_TARGET_TYPE (inner_array));
17764 inner_array = TYPE_TARGET_TYPE (inner_array);
17765 }
17766
17767 el_type = TYPE_TARGET_TYPE (inner_array);
17768 cnst |= TYPE_CONST (el_type);
17769 voltl |= TYPE_VOLATILE (el_type);
17770 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17771
17772 return set_die_type (die, base_type, cu);
17773}
17774
f792889a 17775static struct type *
e7c27a73 17776read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17777{
f792889a 17778 struct type *base_type, *cv_type;
c906108c 17779
e7c27a73 17780 base_type = die_type (die, cu);
7e314c57
JK
17781
17782 /* The die_type call above may have already set the type for this DIE. */
17783 cv_type = get_die_type (die, cu);
17784 if (cv_type)
17785 return cv_type;
17786
2f608a3a
KW
17787 /* In case the const qualifier is applied to an array type, the element type
17788 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17789 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17790 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17791
f792889a
DJ
17792 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17793 return set_die_type (die, cv_type, cu);
c906108c
SS
17794}
17795
f792889a 17796static struct type *
e7c27a73 17797read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17798{
f792889a 17799 struct type *base_type, *cv_type;
c906108c 17800
e7c27a73 17801 base_type = die_type (die, cu);
7e314c57
JK
17802
17803 /* The die_type call above may have already set the type for this DIE. */
17804 cv_type = get_die_type (die, cu);
17805 if (cv_type)
17806 return cv_type;
17807
cf363f18
MW
17808 /* In case the volatile qualifier is applied to an array type, the
17809 element type is so qualified, not the array type (section 6.7.3
17810 of C99). */
78134374 17811 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17812 return add_array_cv_type (die, cu, base_type, 0, 1);
17813
f792889a
DJ
17814 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17815 return set_die_type (die, cv_type, cu);
c906108c
SS
17816}
17817
06d66ee9
TT
17818/* Handle DW_TAG_restrict_type. */
17819
17820static struct type *
17821read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17822{
17823 struct type *base_type, *cv_type;
17824
17825 base_type = die_type (die, cu);
17826
17827 /* The die_type call above may have already set the type for this DIE. */
17828 cv_type = get_die_type (die, cu);
17829 if (cv_type)
17830 return cv_type;
17831
17832 cv_type = make_restrict_type (base_type);
17833 return set_die_type (die, cv_type, cu);
17834}
17835
a2c2acaf
MW
17836/* Handle DW_TAG_atomic_type. */
17837
17838static struct type *
17839read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17840{
17841 struct type *base_type, *cv_type;
17842
17843 base_type = die_type (die, cu);
17844
17845 /* The die_type call above may have already set the type for this DIE. */
17846 cv_type = get_die_type (die, cu);
17847 if (cv_type)
17848 return cv_type;
17849
17850 cv_type = make_atomic_type (base_type);
17851 return set_die_type (die, cv_type, cu);
17852}
17853
c906108c
SS
17854/* Extract all information from a DW_TAG_string_type DIE and add to
17855 the user defined type vector. It isn't really a user defined type,
17856 but it behaves like one, with other DIE's using an AT_user_def_type
17857 attribute to reference it. */
17858
f792889a 17859static struct type *
e7c27a73 17860read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17861{
5e22e966 17862 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17863 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17864 struct type *type, *range_type, *index_type, *char_type;
17865 struct attribute *attr;
216a7e6b
AB
17866 struct dynamic_prop prop;
17867 bool length_is_constant = true;
17868 LONGEST length;
17869
17870 /* There are a couple of places where bit sizes might be made use of
17871 when parsing a DW_TAG_string_type, however, no producer that we know
17872 of make use of these. Handling bit sizes that are a multiple of the
17873 byte size is easy enough, but what about other bit sizes? Lets deal
17874 with that problem when we have to. Warn about these attributes being
17875 unsupported, then parse the type and ignore them like we always
17876 have. */
17877 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17878 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17879 {
17880 static bool warning_printed = false;
17881 if (!warning_printed)
17882 {
17883 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17884 "currently supported on DW_TAG_string_type."));
17885 warning_printed = true;
17886 }
17887 }
c906108c 17888
e142c38c 17889 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17890 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17891 {
17892 /* The string length describes the location at which the length of
17893 the string can be found. The size of the length field can be
17894 specified with one of the attributes below. */
17895 struct type *prop_type;
17896 struct attribute *len
17897 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17898 if (len == nullptr)
17899 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17900 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17901 {
17902 /* Pass 0 as the default as we know this attribute is constant
17903 and the default value will not be returned. */
0826b30a 17904 LONGEST sz = len->constant_value (0);
293e7e51 17905 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17906 }
17907 else
17908 {
17909 /* If the size is not specified then we assume it is the size of
17910 an address on this target. */
293e7e51 17911 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17912 }
17913
17914 /* Convert the attribute into a dynamic property. */
17915 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17916 length = 1;
17917 else
17918 length_is_constant = false;
17919 }
17920 else if (attr != nullptr)
17921 {
17922 /* This DW_AT_string_length just contains the length with no
17923 indirection. There's no need to create a dynamic property in this
17924 case. Pass 0 for the default value as we know it will not be
17925 returned in this case. */
0826b30a 17926 length = attr->constant_value (0);
216a7e6b
AB
17927 }
17928 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17929 {
216a7e6b 17930 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17931 length = attr->constant_value (1);
c906108c
SS
17932 }
17933 else
17934 {
216a7e6b
AB
17935 /* Use 1 as a fallback length if we have nothing else. */
17936 length = 1;
c906108c 17937 }
6ccb9162 17938
46bf5051 17939 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17940 if (length_is_constant)
17941 range_type = create_static_range_type (NULL, index_type, 1, length);
17942 else
17943 {
17944 struct dynamic_prop low_bound;
17945
8c2e4e06 17946 low_bound.set_const_val (1);
216a7e6b
AB
17947 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17948 }
3b7538c0
UW
17949 char_type = language_string_char_type (cu->language_defn, gdbarch);
17950 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17951
f792889a 17952 return set_die_type (die, type, cu);
c906108c
SS
17953}
17954
4d804846
JB
17955/* Assuming that DIE corresponds to a function, returns nonzero
17956 if the function is prototyped. */
17957
17958static int
17959prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17960{
17961 struct attribute *attr;
17962
17963 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17964 if (attr && attr->as_boolean ())
4d804846
JB
17965 return 1;
17966
17967 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17968 is only meaningful for C, but the concept also extends to other
4d804846
JB
17969 languages that allow unprototyped functions (Eg: Objective C).
17970 For all other languages, assume that functions are always
17971 prototyped. */
17972 if (cu->language != language_c
17973 && cu->language != language_objc
17974 && cu->language != language_opencl)
17975 return 1;
17976
17977 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17978 prototyped and unprototyped functions; default to prototyped,
17979 since that is more common in modern code (and RealView warns
17980 about unprototyped functions). */
17981 if (producer_is_realview (cu->producer))
17982 return 1;
17983
17984 return 0;
17985}
17986
c906108c
SS
17987/* Handle DIES due to C code like:
17988
17989 struct foo
c5aa993b
JM
17990 {
17991 int (*funcp)(int a, long l);
17992 int b;
17993 };
c906108c 17994
0963b4bd 17995 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17996
f792889a 17997static struct type *
e7c27a73 17998read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17999{
5e22e966 18000 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
18001 struct type *type; /* Type that this function returns. */
18002 struct type *ftype; /* Function that returns above type. */
c906108c
SS
18003 struct attribute *attr;
18004
e7c27a73 18005 type = die_type (die, cu);
7e314c57
JK
18006
18007 /* The die_type call above may have already set the type for this DIE. */
18008 ftype = get_die_type (die, cu);
18009 if (ftype)
18010 return ftype;
18011
0c8b41f1 18012 ftype = lookup_function_type (type);
c906108c 18013
4d804846 18014 if (prototyped_function_p (die, cu))
27e69b7a 18015 ftype->set_is_prototyped (true);
c906108c 18016
c055b101
CV
18017 /* Store the calling convention in the type if it's available in
18018 the subroutine die. Otherwise set the calling convention to
18019 the default value DW_CC_normal. */
18020 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 18021 if (attr != nullptr
529908cb 18022 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 18023 TYPE_CALLING_CONVENTION (ftype)
529908cb 18024 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
18025 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18026 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18027 else
18028 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 18029
743649fd
MW
18030 /* Record whether the function returns normally to its caller or not
18031 if the DWARF producer set that information. */
18032 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 18033 if (attr && attr->as_boolean ())
743649fd
MW
18034 TYPE_NO_RETURN (ftype) = 1;
18035
76c10ea2
GM
18036 /* We need to add the subroutine type to the die immediately so
18037 we don't infinitely recurse when dealing with parameters
0963b4bd 18038 declared as the same subroutine type. */
76c10ea2 18039 set_die_type (die, ftype, cu);
6e70227d 18040
639d11d3 18041 if (die->child != NULL)
c906108c 18042 {
bb5ed363 18043 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 18044 struct die_info *child_die;
8072405b 18045 int nparams, iparams;
c906108c
SS
18046
18047 /* Count the number of parameters.
dda83cd7
SM
18048 FIXME: GDB currently ignores vararg functions, but knows about
18049 vararg member functions. */
8072405b 18050 nparams = 0;
639d11d3 18051 child_die = die->child;
c906108c
SS
18052 while (child_die && child_die->tag)
18053 {
18054 if (child_die->tag == DW_TAG_formal_parameter)
18055 nparams++;
18056 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
18057 ftype->set_has_varargs (true);
18058
436c571c 18059 child_die = child_die->sibling;
c906108c
SS
18060 }
18061
18062 /* Allocate storage for parameters and fill them in. */
5e33d5f4 18063 ftype->set_num_fields (nparams);
3cabb6b0
SM
18064 ftype->set_fields
18065 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 18066
8072405b
JK
18067 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18068 even if we error out during the parameters reading below. */
18069 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 18070 ftype->field (iparams).set_type (void_type);
8072405b
JK
18071
18072 iparams = 0;
639d11d3 18073 child_die = die->child;
c906108c
SS
18074 while (child_die && child_die->tag)
18075 {
18076 if (child_die->tag == DW_TAG_formal_parameter)
18077 {
3ce3b1ba
PA
18078 struct type *arg_type;
18079
18080 /* DWARF version 2 has no clean way to discern C++
18081 static and non-static member functions. G++ helps
18082 GDB by marking the first parameter for non-static
18083 member functions (which is the this pointer) as
18084 artificial. We pass this information to
18085 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18086
18087 DWARF version 3 added DW_AT_object_pointer, which GCC
18088 4.5 does not yet generate. */
e142c38c 18089 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 18090 if (attr != nullptr)
c45bc3f8 18091 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 18092 else
9c37b5ae 18093 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
18094 arg_type = die_type (child_die, cu);
18095
18096 /* RealView does not mark THIS as const, which the testsuite
18097 expects. GCC marks THIS as const in method definitions,
18098 but not in the class specifications (GCC PR 43053). */
18099 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18100 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18101 {
18102 int is_this = 0;
18103 struct dwarf2_cu *arg_cu = cu;
18104 const char *name = dwarf2_name (child_die, cu);
18105
18106 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 18107 if (attr != nullptr)
3ce3b1ba
PA
18108 {
18109 /* If the compiler emits this, use it. */
18110 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18111 is_this = 1;
18112 }
18113 else if (name && strcmp (name, "this") == 0)
18114 /* Function definitions will have the argument names. */
18115 is_this = 1;
18116 else if (name == NULL && iparams == 0)
18117 /* Declarations may not have the names, so like
18118 elsewhere in GDB, assume an artificial first
18119 argument is "this". */
18120 is_this = 1;
18121
18122 if (is_this)
18123 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18124 arg_type, 0);
18125 }
18126
5d14b6e5 18127 ftype->field (iparams).set_type (arg_type);
c906108c
SS
18128 iparams++;
18129 }
436c571c 18130 child_die = child_die->sibling;
c906108c
SS
18131 }
18132 }
18133
76c10ea2 18134 return ftype;
c906108c
SS
18135}
18136
f792889a 18137static struct type *
e7c27a73 18138read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18139{
5e22e966 18140 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 18141 const char *name = NULL;
3c8e0968 18142 struct type *this_type, *target_type;
c906108c 18143
94af9270 18144 name = dwarf2_full_name (NULL, die, cu);
19f392bc 18145 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 18146 this_type->set_target_is_stub (true);
f792889a 18147 set_die_type (die, this_type, cu);
3c8e0968
DE
18148 target_type = die_type (die, cu);
18149 if (target_type != this_type)
18150 TYPE_TARGET_TYPE (this_type) = target_type;
18151 else
18152 {
18153 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18154 spec and cause infinite loops in GDB. */
b98664d3 18155 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
18156 "- DIE at %s [in module %s]"),
18157 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
18158 TYPE_TARGET_TYPE (this_type) = NULL;
18159 }
e4003a34
TV
18160 if (name == NULL)
18161 {
18162 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18163 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18164 Handle these by just returning the target type, rather than
18165 constructing an anonymous typedef type and trying to handle this
18166 elsewhere. */
18167 set_die_type (die, target_type, cu);
18168 return target_type;
18169 }
f792889a 18170 return this_type;
c906108c
SS
18171}
18172
a625a8c9
TT
18173/* Helper for get_dwarf2_rational_constant that computes the value of
18174 a given gmp_mpz given an attribute. */
18175
18176static void
18177get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18178{
18179 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18180 location expression that pushes an implicit value. */
18181 if (attr->form == DW_FORM_exprloc)
18182 {
18183 dwarf_block *blk = attr->as_block ();
18184 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18185 {
18186 uint64_t len;
18187 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18188 blk->data + blk->size,
18189 &len);
18190 if (ptr - blk->data + len <= blk->size)
18191 {
18192 mpz_import (value->val, len,
18193 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18194 1, 0, 0, ptr);
18195 return;
18196 }
18197 }
18198
18199 /* On failure set it to 1. */
18200 *value = gdb_mpz (1);
18201 }
18202 else if (attr->form_is_block ())
18203 {
18204 dwarf_block *blk = attr->as_block ();
18205 mpz_import (value->val, blk->size,
18206 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18207 1, 0, 0, blk->data);
18208 }
18209 else
18210 *value = gdb_mpz (attr->constant_value (1));
18211}
18212
09584414
JB
18213/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18214 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18215
18216 If the numerator and/or numerator attribute is missing,
18217 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18218 untouched. */
18219
18220static void
18221get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 18222 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
18223{
18224 struct attribute *num_attr, *denom_attr;
18225
18226 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18227 if (num_attr == nullptr)
18228 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18229 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18230
18231 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18232 if (denom_attr == nullptr)
18233 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18234 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18235
18236 if (num_attr == nullptr || denom_attr == nullptr)
18237 return;
18238
a625a8c9
TT
18239 get_mpz (cu, numerator, num_attr);
18240 get_mpz (cu, denominator, denom_attr);
09584414
JB
18241}
18242
18243/* Same as get_dwarf2_rational_constant, but extracting an unsigned
18244 rational constant, rather than a signed one.
18245
18246 If the rational constant has a negative value, a complaint
18247 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18248
18249static void
18250get_dwarf2_unsigned_rational_constant (struct die_info *die,
18251 struct dwarf2_cu *cu,
5cde1d82
TT
18252 gdb_mpz *numerator,
18253 gdb_mpz *denominator)
09584414 18254{
5cde1d82
TT
18255 gdb_mpz num (1);
18256 gdb_mpz denom (1);
09584414
JB
18257
18258 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 18259 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 18260 {
5cde1d82
TT
18261 mpz_neg (num.val, num.val);
18262 mpz_neg (denom.val, denom.val);
09584414 18263 }
5cde1d82 18264 else if (mpz_sgn (num.val) == -1)
09584414
JB
18265 {
18266 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18267 " in DIE at %s"),
18268 sect_offset_str (die->sect_off));
18269 return;
18270 }
5cde1d82 18271 else if (mpz_sgn (denom.val) == -1)
09584414
JB
18272 {
18273 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18274 " in DIE at %s"),
18275 sect_offset_str (die->sect_off));
18276 return;
18277 }
18278
5cde1d82
TT
18279 *numerator = std::move (num);
18280 *denominator = std::move (denom);
09584414
JB
18281}
18282
bbcdf9ab
TT
18283/* Assuming that ENCODING is a string whose contents starting at the
18284 K'th character is "_nn" where "nn" is a decimal number, scan that
18285 number and set RESULT to the value. K is updated to point to the
18286 character immediately following the number.
18287
18288 If the string does not conform to the format described above, false
18289 is returned, and K may or may not be changed. */
18290
18291static bool
18292ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18293{
18294 /* The next character should be an underscore ('_') followed
18295 by a digit. */
18296 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18297 return false;
18298
18299 /* Skip the underscore. */
18300 k++;
18301 int start = k;
18302
18303 /* Determine the number of digits for our number. */
18304 while (isdigit (encoding[k]))
18305 k++;
18306 if (k == start)
18307 return false;
18308
18309 std::string copy (&encoding[start], k - start);
18310 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18311 return false;
18312
18313 return true;
18314}
18315
18316/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18317 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18318 DENOM, update OFFSET, and return true on success. Return false on
18319 failure. */
18320
18321static bool
18322ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18323 gdb_mpz *num, gdb_mpz *denom)
18324{
18325 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18326 return false;
18327 return ada_get_gnat_encoded_number (encoding, offset, denom);
18328}
18329
09584414 18330/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
18331 of the corresponding TYPE by setting its type-specific data. CU is
18332 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18333 encodings. It is nullptr if the GNAT encoding should be
18334 ignored. */
09584414
JB
18335
18336static void
bbcdf9ab
TT
18337finish_fixed_point_type (struct type *type, const char *suffix,
18338 struct die_info *die, struct dwarf2_cu *cu)
09584414 18339{
09584414
JB
18340 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18341 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18342
bbcdf9ab
TT
18343 /* If GNAT encodings are preferred, don't examine the
18344 attributes. */
18345 struct attribute *attr = nullptr;
18346 if (suffix == nullptr)
18347 {
18348 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18349 if (attr == nullptr)
18350 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18351 if (attr == nullptr)
18352 attr = dwarf2_attr (die, DW_AT_small, cu);
18353 }
09584414 18354
5cde1d82
TT
18355 /* Numerator and denominator of our fixed-point type's scaling factor.
18356 The default is a scaling factor of 1, which we use as a fallback
18357 when we are not able to decode it (problem with the debugging info,
18358 unsupported forms, bug in GDB, etc...). Using that as the default
18359 allows us to at least print the unscaled value, which might still
18360 be useful to a user. */
18361 gdb_mpz scale_num (1);
18362 gdb_mpz scale_denom (1);
18363
09584414
JB
18364 if (attr == nullptr)
18365 {
bbcdf9ab
TT
18366 int offset = 0;
18367 if (suffix != nullptr
18368 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18369 &scale_denom)
18370 /* The number might be encoded as _nn_dd_nn_dd, where the
18371 second ratio is the 'small value. In this situation, we
18372 want the second value. */
18373 && (suffix[offset] != '_'
18374 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18375 &scale_denom)))
18376 {
18377 /* Found it. */
18378 }
18379 else
18380 {
18381 /* Scaling factor not found. Assume a scaling factor of 1,
18382 and hope for the best. At least the user will be able to
18383 see the encoded value. */
18384 scale_num = 1;
18385 scale_denom = 1;
18386 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18387 sect_offset_str (die->sect_off));
18388 }
09584414
JB
18389 }
18390 else if (attr->name == DW_AT_binary_scale)
18391 {
18392 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18393 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18394
5cde1d82 18395 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18396 }
18397 else if (attr->name == DW_AT_decimal_scale)
18398 {
18399 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18400 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18401
5cde1d82 18402 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18403 }
18404 else if (attr->name == DW_AT_small)
18405 {
18406 struct die_info *scale_die;
18407 struct dwarf2_cu *scale_cu = cu;
18408
18409 scale_die = follow_die_ref (die, attr, &scale_cu);
18410 if (scale_die->tag == DW_TAG_constant)
18411 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18412 &scale_num, &scale_denom);
18413 else
18414 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18415 " (DIE at %s)"),
18416 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18417 }
18418 else
18419 {
18420 complaint (_("unsupported scale attribute %s for fixed-point type"
18421 " (DIE at %s)"),
18422 dwarf_attr_name (attr->name),
18423 sect_offset_str (die->sect_off));
18424 }
18425
2a12c336 18426 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18427 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18428 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18429 mpq_canonicalize (scaling_factor.val);
18430}
18431
bbcdf9ab
TT
18432/* The gnat-encoding suffix for fixed point. */
18433
18434#define GNAT_FIXED_POINT_SUFFIX "___XF_"
18435
18436/* If NAME encodes an Ada fixed-point type, return a pointer to the
18437 "XF" suffix of the name. The text after this is what encodes the
18438 'small and 'delta information. Otherwise, return nullptr. */
18439
18440static const char *
18441gnat_encoded_fixed_point_type_info (const char *name)
18442{
18443 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18444}
18445
9b790ce7
UW
18446/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18447 (which may be different from NAME) to the architecture back-end to allow
18448 it to guess the correct format if necessary. */
18449
18450static struct type *
18451dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18452 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18453{
08feed99 18454 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18455 const struct floatformat **format;
18456 struct type *type;
18457
18458 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18459 if (format)
103a685e 18460 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18461 else
77b7c781 18462 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18463
18464 return type;
18465}
18466
eb77c9df
AB
18467/* Allocate an integer type of size BITS and name NAME. */
18468
18469static struct type *
18470dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18471 int bits, int unsigned_p, const char *name)
18472{
18473 struct type *type;
18474
18475 /* Versions of Intel's C Compiler generate an integer type called "void"
18476 instead of using DW_TAG_unspecified_type. This has been seen on
18477 at least versions 14, 17, and 18. */
35ee2dc2
AB
18478 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18479 && strcmp (name, "void") == 0)
eb77c9df
AB
18480 type = objfile_type (objfile)->builtin_void;
18481 else
18482 type = init_integer_type (objfile, bits, unsigned_p, name);
18483
18484 return type;
18485}
18486
09584414
JB
18487/* Return true if DIE has a DW_AT_small attribute whose value is
18488 a constant rational, where both the numerator and denominator
18489 are equal to zero.
18490
18491 CU is the DIE's Compilation Unit. */
18492
18493static bool
18494has_zero_over_zero_small_attribute (struct die_info *die,
18495 struct dwarf2_cu *cu)
18496{
18497 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18498 if (attr == nullptr)
18499 return false;
18500
18501 struct dwarf2_cu *scale_cu = cu;
18502 struct die_info *scale_die
18503 = follow_die_ref (die, attr, &scale_cu);
18504
18505 if (scale_die->tag != DW_TAG_constant)
18506 return false;
18507
5cde1d82 18508 gdb_mpz num (1), denom (1);
09584414 18509 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18510 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18511}
18512
8bdc1658
AB
18513/* Initialise and return a floating point type of size BITS suitable for
18514 use as a component of a complex number. The NAME_HINT is passed through
18515 when initialising the floating point type and is the name of the complex
18516 type.
18517
18518 As DWARF doesn't currently provide an explicit name for the components
18519 of a complex number, but it can be helpful to have these components
18520 named, we try to select a suitable name based on the size of the
18521 component. */
18522static struct type *
18523dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18524 struct objfile *objfile,
103a685e
TT
18525 int bits, const char *name_hint,
18526 enum bfd_endian byte_order)
8bdc1658 18527{
08feed99 18528 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18529 struct type *tt = nullptr;
18530
35add35e
AB
18531 /* Try to find a suitable floating point builtin type of size BITS.
18532 We're going to use the name of this type as the name for the complex
18533 target type that we are about to create. */
1db455a7 18534 switch (cu->language)
8bdc1658 18535 {
1db455a7
AB
18536 case language_fortran:
18537 switch (bits)
18538 {
18539 case 32:
18540 tt = builtin_f_type (gdbarch)->builtin_real;
18541 break;
18542 case 64:
18543 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18544 break;
18545 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18546 case 128:
18547 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18548 break;
18549 }
8bdc1658 18550 break;
1db455a7
AB
18551 default:
18552 switch (bits)
18553 {
18554 case 32:
18555 tt = builtin_type (gdbarch)->builtin_float;
18556 break;
18557 case 64:
18558 tt = builtin_type (gdbarch)->builtin_double;
18559 break;
18560 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18561 case 128:
18562 tt = builtin_type (gdbarch)->builtin_long_double;
18563 break;
18564 }
8bdc1658
AB
18565 break;
18566 }
18567
35add35e
AB
18568 /* If the type we found doesn't match the size we were looking for, then
18569 pretend we didn't find a type at all, the complex target type we
18570 create will then be nameless. */
a12e5744 18571 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18572 tt = nullptr;
18573
7d93a1e0 18574 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18575 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18576}
18577
c906108c
SS
18578/* Find a representation of a given base type and install
18579 it in the TYPE field of the die. */
18580
f792889a 18581static struct type *
e7c27a73 18582read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18583{
5e22e966 18584 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18585 struct type *type;
18586 struct attribute *attr;
19f392bc 18587 int encoding = 0, bits = 0;
15d034d0 18588 const char *name;
34877895 18589 gdbarch *arch;
c906108c 18590
e142c38c 18591 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18592 if (attr != nullptr && attr->form_is_constant ())
18593 encoding = attr->constant_value (0);
e142c38c 18594 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18595 if (attr != nullptr)
529908cb 18596 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18597 name = dwarf2_name (die, cu);
6ccb9162 18598 if (!name)
34877895 18599 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18600
08feed99 18601 arch = objfile->arch ();
103a685e
TT
18602 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18603
34877895 18604 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18605 if (attr != nullptr && attr->form_is_constant ())
103a685e 18606 {
529908cb 18607 int endianity = attr->constant_value (0);
103a685e
TT
18608
18609 switch (endianity)
18610 {
18611 case DW_END_big:
18612 byte_order = BFD_ENDIAN_BIG;
18613 break;
18614 case DW_END_little:
18615 byte_order = BFD_ENDIAN_LITTLE;
18616 break;
18617 default:
18618 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18619 break;
18620 }
18621 }
6ccb9162 18622
09584414
JB
18623 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18624 && cu->language == language_ada
18625 && has_zero_over_zero_small_attribute (die, cu))
18626 {
18627 /* brobecker/2018-02-24: This is a fixed point type for which
18628 the scaling factor is represented as fraction whose value
18629 does not make sense (zero divided by zero), so we should
18630 normally never see these. However, there is a small category
18631 of fixed point types for which GNAT is unable to provide
18632 the scaling factor via the standard DWARF mechanisms, and
18633 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18634 This is likely what this DIE is about. */
09584414
JB
18635 encoding = (encoding == DW_ATE_signed_fixed
18636 ? DW_ATE_signed
18637 : DW_ATE_unsigned);
18638 }
18639
bbcdf9ab
TT
18640 /* With GNAT encodings, fixed-point information will be encoded in
18641 the type name. Note that this can also occur with the above
18642 zero-over-zero case, which is why this is a separate "if" rather
18643 than an "else if". */
18644 const char *gnat_encoding_suffix = nullptr;
18645 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18646 && cu->language == language_ada
18647 && name != nullptr)
18648 {
18649 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18650 if (gnat_encoding_suffix != nullptr)
18651 {
18652 gdb_assert (startswith (gnat_encoding_suffix,
18653 GNAT_FIXED_POINT_SUFFIX));
18654 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18655 name, gnat_encoding_suffix - name);
18656 /* Use -1 here so that SUFFIX points at the "_" after the
18657 "XF". */
18658 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18659
18660 encoding = (encoding == DW_ATE_signed
18661 ? DW_ATE_signed_fixed
18662 : DW_ATE_unsigned_fixed);
18663 }
18664 }
18665
6ccb9162 18666 switch (encoding)
c906108c 18667 {
6ccb9162
UW
18668 case DW_ATE_address:
18669 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18670 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18671 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18672 break;
18673 case DW_ATE_boolean:
19f392bc 18674 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18675 break;
18676 case DW_ATE_complex_float:
103a685e
TT
18677 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18678 byte_order);
78134374 18679 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18680 {
18681 if (name == nullptr)
18682 {
18683 struct obstack *obstack
5e22e966 18684 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18685 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18686 nullptr);
18687 }
18688 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18689 }
18690 else
18691 type = init_complex_type (name, type);
6ccb9162
UW
18692 break;
18693 case DW_ATE_decimal_float:
19f392bc 18694 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18695 break;
18696 case DW_ATE_float:
103a685e 18697 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18698 break;
18699 case DW_ATE_signed:
eb77c9df 18700 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18701 break;
18702 case DW_ATE_unsigned:
3b2b8fea
TT
18703 if (cu->language == language_fortran
18704 && name
61012eef 18705 && startswith (name, "character("))
19f392bc
UW
18706 type = init_character_type (objfile, bits, 1, name);
18707 else
eb77c9df 18708 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18709 break;
18710 case DW_ATE_signed_char:
6e70227d 18711 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18712 || cu->language == language_pascal
18713 || cu->language == language_fortran)
19f392bc
UW
18714 type = init_character_type (objfile, bits, 0, name);
18715 else
eb77c9df 18716 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18717 break;
18718 case DW_ATE_unsigned_char:
868a0084 18719 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18720 || cu->language == language_pascal
c44af4eb
TT
18721 || cu->language == language_fortran
18722 || cu->language == language_rust)
19f392bc
UW
18723 type = init_character_type (objfile, bits, 1, name);
18724 else
eb77c9df 18725 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18726 break;
75079b2b 18727 case DW_ATE_UTF:
53e710ac 18728 {
53e710ac
PA
18729 if (bits == 16)
18730 type = builtin_type (arch)->builtin_char16;
18731 else if (bits == 32)
18732 type = builtin_type (arch)->builtin_char32;
18733 else
18734 {
b98664d3 18735 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18736 bits);
eb77c9df 18737 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18738 }
18739 return set_die_type (die, type, cu);
18740 }
75079b2b 18741 break;
09584414
JB
18742 case DW_ATE_signed_fixed:
18743 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18744 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18745 break;
18746 case DW_ATE_unsigned_fixed:
18747 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18748 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18749 break;
75079b2b 18750
6ccb9162 18751 default:
b98664d3 18752 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18753 dwarf_type_encoding_name (encoding));
77b7c781 18754 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18755 break;
c906108c 18756 }
6ccb9162 18757
0114d602 18758 if (name && strcmp (name, "char") == 0)
15152a54 18759 type->set_has_no_signedness (true);
0114d602 18760
2b4424c3
TT
18761 maybe_set_alignment (cu, die, type);
18762
db558e34 18763 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18764
20a5fcbd
TT
18765 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18766 {
18767 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18768 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18769 {
529908cb 18770 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18771 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18772 /* Only use the attributes if they make sense together. */
18773 if (attr == nullptr
529908cb
TT
18774 || (attr->as_unsigned () + real_bit_size
18775 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18776 {
18777 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18778 = real_bit_size;
18779 if (attr != nullptr)
18780 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18781 = attr->as_unsigned ();
20a5fcbd
TT
18782 }
18783 }
18784 }
18785
f792889a 18786 return set_die_type (die, type, cu);
c906108c
SS
18787}
18788
80180f79
SA
18789/* Parse dwarf attribute if it's a block, reference or constant and put the
18790 resulting value of the attribute into struct bound_prop.
18791 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18792
18793static int
18794attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18795 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18796 struct type *default_type)
80180f79
SA
18797{
18798 struct dwarf2_property_baton *baton;
5e22e966 18799 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18800 struct objfile *objfile = per_objfile->objfile;
18801 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18802
9a49df9d
AB
18803 gdb_assert (default_type != NULL);
18804
80180f79
SA
18805 if (attr == NULL || prop == NULL)
18806 return 0;
18807
4fc6c0d5 18808 if (attr->form_is_block ())
80180f79 18809 {
8d749320 18810 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18811 baton->property_type = default_type;
80180f79 18812 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18813 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18814
18815 struct dwarf_block *block = attr->as_block ();
18816 baton->locexpr.size = block->size;
18817 baton->locexpr.data = block->data;
216a7e6b
AB
18818 switch (attr->name)
18819 {
18820 case DW_AT_string_length:
18821 baton->locexpr.is_reference = true;
18822 break;
18823 default:
18824 baton->locexpr.is_reference = false;
18825 break;
18826 }
8c2e4e06
SM
18827
18828 prop->set_locexpr (baton);
18829 gdb_assert (prop->baton () != NULL);
80180f79 18830 }
cd6c91b4 18831 else if (attr->form_is_ref ())
80180f79
SA
18832 {
18833 struct dwarf2_cu *target_cu = cu;
18834 struct die_info *target_die;
18835 struct attribute *target_attr;
18836
18837 target_die = follow_die_ref (die, attr, &target_cu);
18838 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18839 if (target_attr == NULL)
18840 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18841 target_cu);
80180f79
SA
18842 if (target_attr == NULL)
18843 return 0;
18844
df25ebbd 18845 switch (target_attr->name)
80180f79 18846 {
df25ebbd 18847 case DW_AT_location:
cd6c91b4 18848 if (target_attr->form_is_section_offset ())
df25ebbd 18849 {
8d749320 18850 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18851 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18852 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18853 prop->set_loclist (baton);
18854 gdb_assert (prop->baton () != NULL);
df25ebbd 18855 }
4fc6c0d5 18856 else if (target_attr->form_is_block ())
df25ebbd 18857 {
8d749320 18858 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18859 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18860 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18861 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18862 struct dwarf_block *block = target_attr->as_block ();
18863 baton->locexpr.size = block->size;
18864 baton->locexpr.data = block->data;
9a49df9d 18865 baton->locexpr.is_reference = true;
8c2e4e06
SM
18866 prop->set_locexpr (baton);
18867 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18868 }
18869 else
18870 {
18871 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18872 "dynamic property");
18873 return 0;
18874 }
18875 break;
18876 case DW_AT_data_member_location:
18877 {
18878 LONGEST offset;
18879
18880 if (!handle_data_member_location (target_die, target_cu,
18881 &offset))
18882 return 0;
18883
8d749320 18884 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18885 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18886 target_cu);
df25ebbd
JB
18887 baton->offset_info.offset = offset;
18888 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18889 prop->set_addr_offset (baton);
df25ebbd
JB
18890 break;
18891 }
80180f79
SA
18892 }
18893 }
cd6c91b4 18894 else if (attr->form_is_constant ())
8c2e4e06 18895 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18896 else
18897 {
18898 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18899 dwarf2_name (die, cu));
18900 return 0;
18901 }
18902
18903 return 1;
18904}
18905
09ba997f 18906/* See read.h. */
9a49df9d 18907
09ba997f 18908struct type *
293e7e51 18909dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18910{
9a49df9d
AB
18911 struct type *int_type;
18912
18913 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18914#define TRY_TYPE(F) \
18915 int_type = (unsigned_p \
18916 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18917 : objfile_type (objfile)->builtin_ ## F); \
18918 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18919 return int_type
18920
18921 TRY_TYPE (char);
18922 TRY_TYPE (short);
18923 TRY_TYPE (int);
18924 TRY_TYPE (long);
18925 TRY_TYPE (long_long);
18926
18927#undef TRY_TYPE
18928
18929 gdb_assert_not_reached ("unable to find suitable integer type");
18930}
18931
09ba997f 18932/* See read.h. */
11a8b164 18933
09ba997f 18934struct type *
293e7e51 18935dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18936{
293e7e51
SM
18937 int addr_size = this->per_cu->addr_size ();
18938 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18939}
18940
b86352cf
AB
18941/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18942 present (which is valid) then compute the default type based on the
18943 compilation units address size. */
18944
18945static struct type *
18946read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18947{
18948 struct type *index_type = die_type (die, cu);
18949
18950 /* Dwarf-2 specifications explicitly allows to create subrange types
18951 without specifying a base type.
18952 In that case, the base type must be set to the type of
18953 the lower bound, upper bound or count, in that order, if any of these
18954 three attributes references an object that has a type.
18955 If no base type is found, the Dwarf-2 specifications say that
18956 a signed integer type of size equal to the size of an address should
18957 be used.
18958 For the following C code: `extern char gdb_int [];'
18959 GCC produces an empty range DIE.
18960 FIXME: muller/2010-05-28: Possible references to object for low bound,
18961 high bound or count are not yet handled by this code. */
78134374 18962 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18963 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18964
18965 return index_type;
18966}
18967
a02abb62
JB
18968/* Read the given DW_AT_subrange DIE. */
18969
f792889a 18970static struct type *
a02abb62
JB
18971read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18972{
4c9ad8c2 18973 struct type *base_type, *orig_base_type;
a02abb62
JB
18974 struct type *range_type;
18975 struct attribute *attr;
729efb13 18976 struct dynamic_prop low, high;
4fae6e18 18977 int low_default_is_valid;
c451ebe5 18978 int high_bound_is_count = 0;
15d034d0 18979 const char *name;
d359392f 18980 ULONGEST negative_mask;
e77813c8 18981
b86352cf
AB
18982 orig_base_type = read_subrange_index_type (die, cu);
18983
4c9ad8c2
TT
18984 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18985 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18986 creating the range type, but we use the result of check_typedef
18987 when examining properties of the type. */
18988 base_type = check_typedef (orig_base_type);
a02abb62 18989
7e314c57
JK
18990 /* The die_type call above may have already set the type for this DIE. */
18991 range_type = get_die_type (die, cu);
18992 if (range_type)
18993 return range_type;
18994
8c2e4e06 18995 high.set_const_val (0);
729efb13 18996
4fae6e18
JK
18997 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18998 omitting DW_AT_lower_bound. */
18999 switch (cu->language)
6e70227d 19000 {
4fae6e18
JK
19001 case language_c:
19002 case language_cplus:
8c2e4e06 19003 low.set_const_val (0);
4fae6e18
JK
19004 low_default_is_valid = 1;
19005 break;
19006 case language_fortran:
8c2e4e06 19007 low.set_const_val (1);
4fae6e18
JK
19008 low_default_is_valid = 1;
19009 break;
19010 case language_d:
4fae6e18 19011 case language_objc:
c44af4eb 19012 case language_rust:
8c2e4e06 19013 low.set_const_val (0);
4fae6e18
JK
19014 low_default_is_valid = (cu->header.version >= 4);
19015 break;
19016 case language_ada:
19017 case language_m2:
19018 case language_pascal:
8c2e4e06 19019 low.set_const_val (1);
4fae6e18
JK
19020 low_default_is_valid = (cu->header.version >= 4);
19021 break;
19022 default:
8c2e4e06 19023 low.set_const_val (0);
4fae6e18
JK
19024 low_default_is_valid = 0;
19025 break;
a02abb62
JB
19026 }
19027
e142c38c 19028 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 19029 if (attr != nullptr)
9a49df9d 19030 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 19031 else if (!low_default_is_valid)
b98664d3 19032 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
19033 "- DIE at %s [in module %s]"),
19034 sect_offset_str (die->sect_off),
5e22e966 19035 objfile_name (cu->per_objfile->objfile));
a02abb62 19036
506f5c41
TV
19037 struct attribute *attr_ub, *attr_count;
19038 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 19039 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 19040 {
506f5c41 19041 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 19042 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 19043 {
c451ebe5 19044 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
19045 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19046 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
19047 else
19048 high_bound_is_count = 1;
c2ff108b 19049 }
506f5c41
TV
19050 else
19051 {
19052 if (attr_ub != NULL)
19053 complaint (_("Unresolved DW_AT_upper_bound "
19054 "- DIE at %s [in module %s]"),
19055 sect_offset_str (die->sect_off),
5e22e966 19056 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
19057 if (attr_count != NULL)
19058 complaint (_("Unresolved DW_AT_count "
19059 "- DIE at %s [in module %s]"),
19060 sect_offset_str (die->sect_off),
5e22e966 19061 objfile_name (cu->per_objfile->objfile));
506f5c41 19062 }
e77813c8 19063 }
a02abb62 19064
4e962e74
TT
19065 LONGEST bias = 0;
19066 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 19067 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 19068 bias = bias_attr->constant_value (0);
4e962e74 19069
dbb9c2b1
JB
19070 /* Normally, the DWARF producers are expected to use a signed
19071 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19072 But this is unfortunately not always the case, as witnessed
19073 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19074 is used instead. To work around that ambiguity, we treat
19075 the bounds as signed, and thus sign-extend their values, when
19076 the base type is signed. */
6e70227d 19077 negative_mask =
d359392f 19078 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 19079 if (low.kind () == PROP_CONST
c6d940a9 19080 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
19081 low.set_const_val (low.const_val () | negative_mask);
19082 if (high.kind () == PROP_CONST
c6d940a9 19083 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 19084 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 19085
5bbd8269
AB
19086 /* Check for bit and byte strides. */
19087 struct dynamic_prop byte_stride_prop;
19088 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19089 if (attr_byte_stride != nullptr)
19090 {
293e7e51 19091 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19092 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19093 prop_type);
19094 }
19095
19096 struct dynamic_prop bit_stride_prop;
19097 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19098 if (attr_bit_stride != nullptr)
19099 {
19100 /* It only makes sense to have either a bit or byte stride. */
19101 if (attr_byte_stride != nullptr)
19102 {
19103 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19104 "- DIE at %s [in module %s]"),
19105 sect_offset_str (die->sect_off),
5e22e966 19106 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
19107 attr_bit_stride = nullptr;
19108 }
19109 else
19110 {
293e7e51 19111 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19112 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19113 prop_type);
19114 }
19115 }
19116
19117 if (attr_byte_stride != nullptr
19118 || attr_bit_stride != nullptr)
19119 {
19120 bool byte_stride_p = (attr_byte_stride != nullptr);
19121 struct dynamic_prop *stride
19122 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19123
19124 range_type
19125 = create_range_type_with_stride (NULL, orig_base_type, &low,
19126 &high, bias, stride, byte_stride_p);
19127 }
19128 else
19129 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 19130
c451ebe5 19131 if (high_bound_is_count)
599088e3 19132 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 19133
c2ff108b
JK
19134 /* Ada expects an empty array on no boundary attributes. */
19135 if (attr == NULL && cu->language != language_ada)
8c2e4e06 19136 range_type->bounds ()->high.set_undefined ();
c2ff108b 19137
39cbfefa
DJ
19138 name = dwarf2_name (die, cu);
19139 if (name)
d0e39ea2 19140 range_type->set_name (name);
6e70227d 19141
e142c38c 19142 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 19143 if (attr != nullptr)
529908cb 19144 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 19145
2b4424c3
TT
19146 maybe_set_alignment (cu, die, range_type);
19147
7e314c57
JK
19148 set_die_type (die, range_type, cu);
19149
19150 /* set_die_type should be already done. */
b4ba55a1
JB
19151 set_descriptive_type (range_type, die, cu);
19152
7e314c57 19153 return range_type;
a02abb62 19154}
6e70227d 19155
f792889a 19156static struct type *
81a17f79
JB
19157read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19158{
19159 struct type *type;
81a17f79 19160
5e22e966 19161 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 19162 type->set_name (dwarf2_name (die, cu));
81a17f79 19163
74a2f8ff 19164 /* In Ada, an unspecified type is typically used when the description
85102364 19165 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
19166 such a type, we treat it as a stub, and try to resolve it later on,
19167 when needed. */
19168 if (cu->language == language_ada)
b4b73759 19169 type->set_is_stub (true);
74a2f8ff 19170
f792889a 19171 return set_die_type (die, type, cu);
81a17f79 19172}
a02abb62 19173
639d11d3
DC
19174/* Read a single die and all its descendents. Set the die's sibling
19175 field to NULL; set other fields in the die correctly, and set all
19176 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19177 location of the info_ptr after reading all of those dies. PARENT
19178 is the parent of the die in question. */
19179
19180static struct die_info *
dee91e82 19181read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
19182 const gdb_byte *info_ptr,
19183 const gdb_byte **new_info_ptr,
dee91e82 19184 struct die_info *parent)
639d11d3
DC
19185{
19186 struct die_info *die;
d521ce57 19187 const gdb_byte *cur_ptr;
639d11d3 19188
3e225074 19189 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
19190 if (die == NULL)
19191 {
19192 *new_info_ptr = cur_ptr;
19193 return NULL;
19194 }
93311388 19195 store_in_ref_table (die, reader->cu);
639d11d3 19196
3e225074 19197 if (die->has_children)
bf6af496 19198 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
19199 else
19200 {
19201 die->child = NULL;
19202 *new_info_ptr = cur_ptr;
19203 }
19204
19205 die->sibling = NULL;
19206 die->parent = parent;
19207 return die;
19208}
19209
19210/* Read a die, all of its descendents, and all of its siblings; set
19211 all of the fields of all of the dies correctly. Arguments are as
19212 in read_die_and_children. */
19213
19214static struct die_info *
bf6af496 19215read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
19216 const gdb_byte *info_ptr,
19217 const gdb_byte **new_info_ptr,
bf6af496 19218 struct die_info *parent)
639d11d3
DC
19219{
19220 struct die_info *first_die, *last_sibling;
d521ce57 19221 const gdb_byte *cur_ptr;
639d11d3 19222
c906108c 19223 cur_ptr = info_ptr;
639d11d3
DC
19224 first_die = last_sibling = NULL;
19225
19226 while (1)
c906108c 19227 {
639d11d3 19228 struct die_info *die
dee91e82 19229 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 19230
1d325ec1 19231 if (die == NULL)
c906108c 19232 {
639d11d3
DC
19233 *new_info_ptr = cur_ptr;
19234 return first_die;
c906108c 19235 }
1d325ec1
DJ
19236
19237 if (!first_die)
19238 first_die = die;
c906108c 19239 else
1d325ec1
DJ
19240 last_sibling->sibling = die;
19241
19242 last_sibling = die;
c906108c 19243 }
c906108c
SS
19244}
19245
bf6af496
DE
19246/* Read a die, all of its descendents, and all of its siblings; set
19247 all of the fields of all of the dies correctly. Arguments are as
19248 in read_die_and_children.
19249 This the main entry point for reading a DIE and all its children. */
19250
19251static struct die_info *
19252read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
19253 const gdb_byte *info_ptr,
19254 const gdb_byte **new_info_ptr,
bf6af496
DE
19255 struct die_info *parent)
19256{
19257 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19258 new_info_ptr, parent);
19259
b4f54984 19260 if (dwarf_die_debug)
bf6af496
DE
19261 {
19262 fprintf_unfiltered (gdb_stdlog,
19263 "Read die from %s@0x%x of %s:\n",
96b79293 19264 reader->die_section->get_name (),
bf6af496
DE
19265 (unsigned) (info_ptr - reader->die_section->buffer),
19266 bfd_get_filename (reader->abfd));
b4f54984 19267 dump_die (die, dwarf_die_debug);
bf6af496
DE
19268 }
19269
19270 return die;
19271}
19272
3019eac3
DE
19273/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19274 attributes.
19275 The caller is responsible for filling in the extra attributes
19276 and updating (*DIEP)->num_attrs.
19277 Set DIEP to point to a newly allocated die with its information,
3e225074 19278 except for its child, sibling, and parent fields. */
93311388 19279
d521ce57 19280static const gdb_byte *
3019eac3 19281read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19282 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19283 int num_extra_attrs)
93311388 19284{
b64f50a1 19285 unsigned int abbrev_number, bytes_read, i;
7c32eebb 19286 const struct abbrev_info *abbrev;
93311388
DE
19287 struct die_info *die;
19288 struct dwarf2_cu *cu = reader->cu;
19289 bfd *abfd = reader->abfd;
19290
9c541725 19291 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19292 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19293 info_ptr += bytes_read;
19294 if (!abbrev_number)
19295 {
19296 *diep = NULL;
93311388
DE
19297 return info_ptr;
19298 }
19299
685af9cd 19300 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19301 if (!abbrev)
348e048f
DE
19302 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19303 abbrev_number,
19304 bfd_get_filename (abfd));
19305
3019eac3 19306 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19307 die->sect_off = sect_off;
93311388
DE
19308 die->tag = abbrev->tag;
19309 die->abbrev = abbrev_number;
3e225074 19310 die->has_children = abbrev->has_children;
93311388 19311
3019eac3
DE
19312 /* Make the result usable.
19313 The caller needs to update num_attrs after adding the extra
19314 attributes. */
93311388
DE
19315 die->num_attrs = abbrev->num_attrs;
19316
7a5f294d 19317 bool any_need_reprocess = false;
93311388 19318 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19319 {
7a5f294d
TT
19320 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19321 info_ptr);
19322 if (die->attrs[i].requires_reprocessing_p ())
19323 any_need_reprocess = true;
18a8505e
AT
19324 }
19325
052c8bb8 19326 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19327 if (attr != nullptr && attr->form_is_unsigned ())
19328 cu->str_offsets_base = attr->as_unsigned ();
93311388 19329
41144253 19330 attr = die->attr (DW_AT_loclists_base);
19331 if (attr != nullptr)
529908cb 19332 cu->loclist_base = attr->as_unsigned ();
41144253 19333
a39fdb41 19334 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19335 if (maybe_addr_base.has_value ())
19336 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19337
19338 attr = die->attr (DW_AT_rnglists_base);
19339 if (attr != nullptr)
2b0c7f41 19340 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 19341
7a5f294d
TT
19342 if (any_need_reprocess)
19343 {
19344 for (i = 0; i < abbrev->num_attrs; ++i)
19345 {
19346 if (die->attrs[i].requires_reprocessing_p ())
19347 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19348 }
19349 }
93311388 19350 *diep = die;
93311388
DE
19351 return info_ptr;
19352}
19353
3019eac3
DE
19354/* Read a die and all its attributes.
19355 Set DIEP to point to a newly allocated die with its information,
3e225074 19356 except for its child, sibling, and parent fields. */
3019eac3 19357
d521ce57 19358static const gdb_byte *
3019eac3 19359read_full_die (const struct die_reader_specs *reader,
3e225074 19360 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19361{
d521ce57 19362 const gdb_byte *result;
bf6af496 19363
3e225074 19364 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19365
b4f54984 19366 if (dwarf_die_debug)
bf6af496
DE
19367 {
19368 fprintf_unfiltered (gdb_stdlog,
19369 "Read die from %s@0x%x of %s:\n",
96b79293 19370 reader->die_section->get_name (),
bf6af496
DE
19371 (unsigned) (info_ptr - reader->die_section->buffer),
19372 bfd_get_filename (reader->abfd));
b4f54984 19373 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19374 }
19375
19376 return result;
3019eac3 19377}
433df2d4 19378\f
c906108c 19379
72bf9492
DJ
19380/* Returns nonzero if TAG represents a type that we might generate a partial
19381 symbol for. */
19382
19383static int
d8f62e84 19384is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19385{
19386 switch (tag)
19387 {
19388#if 0
19389 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19390 that we don't at present. Note that normally this does not
19391 matter, mainly because C compilers don't give names to these
19392 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19393 case DW_TAG_file_type:
19394 case DW_TAG_ptr_to_member_type:
19395 case DW_TAG_set_type:
19396 case DW_TAG_string_type:
19397 case DW_TAG_subroutine_type:
19398#endif
d8f62e84
TT
19399
19400 /* GNAT may emit an array with a name, but no typedef, so we
19401 need to make a symbol in this case. */
19402 case DW_TAG_array_type:
19403 return lang == language_ada;
19404
72bf9492
DJ
19405 case DW_TAG_base_type:
19406 case DW_TAG_class_type:
680b30c7 19407 case DW_TAG_interface_type:
72bf9492
DJ
19408 case DW_TAG_enumeration_type:
19409 case DW_TAG_structure_type:
19410 case DW_TAG_subrange_type:
19411 case DW_TAG_typedef:
19412 case DW_TAG_union_type:
19413 return 1;
19414 default:
19415 return 0;
19416 }
19417}
19418
19419/* Load all DIEs that are interesting for partial symbols into memory. */
19420
19421static struct partial_die_info *
dee91e82 19422load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19423 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19424{
dee91e82 19425 struct dwarf2_cu *cu = reader->cu;
5e22e966 19426 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19427 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19428 unsigned int bytes_read;
5afb4e99 19429 unsigned int load_all = 0;
72bf9492
DJ
19430 int nesting_level = 1;
19431
19432 parent_die = NULL;
19433 last_die = NULL;
19434
7adf1e79
DE
19435 gdb_assert (cu->per_cu != NULL);
19436 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19437 load_all = 1;
19438
72bf9492
DJ
19439 cu->partial_dies
19440 = htab_create_alloc_ex (cu->header.length / 12,
19441 partial_die_hash,
19442 partial_die_eq,
19443 NULL,
19444 &cu->comp_unit_obstack,
19445 hashtab_obstack_allocate,
19446 dummy_obstack_deallocate);
19447
72bf9492
DJ
19448 while (1)
19449 {
7c32eebb
TT
19450 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19451 &bytes_read);
72bf9492
DJ
19452
19453 /* A NULL abbrev means the end of a series of children. */
19454 if (abbrev == NULL)
19455 {
19456 if (--nesting_level == 0)
cd9983dd
YQ
19457 return first_die;
19458
72bf9492
DJ
19459 info_ptr += bytes_read;
19460 last_die = parent_die;
19461 parent_die = parent_die->die_parent;
19462 continue;
19463 }
19464
98bfdba5
PA
19465 /* Check for template arguments. We never save these; if
19466 they're seen, we just mark the parent, and go on our way. */
19467 if (parent_die != NULL
19468 && cu->language == language_cplus
19469 && (abbrev->tag == DW_TAG_template_type_param
19470 || abbrev->tag == DW_TAG_template_value_param))
19471 {
19472 parent_die->has_template_arguments = 1;
19473
19474 if (!load_all)
19475 {
19476 /* We don't need a partial DIE for the template argument. */
dee91e82 19477 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19478 continue;
19479 }
19480 }
19481
0d99eb77 19482 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19483 Skip their other children. */
19484 if (!load_all
19485 && cu->language == language_cplus
19486 && parent_die != NULL
f9b5d5ea
TV
19487 && parent_die->tag == DW_TAG_subprogram
19488 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19489 {
dee91e82 19490 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19491 continue;
19492 }
19493
5afb4e99
DJ
19494 /* Check whether this DIE is interesting enough to save. Normally
19495 we would not be interested in members here, but there may be
19496 later variables referencing them via DW_AT_specification (for
19497 static members). */
19498 if (!load_all
d8f62e84 19499 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19500 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19501 && abbrev->tag != DW_TAG_enumerator
19502 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19503 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19504 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19505 && abbrev->tag != DW_TAG_variable
5afb4e99 19506 && abbrev->tag != DW_TAG_namespace
f55ee35c 19507 && abbrev->tag != DW_TAG_module
95554aad 19508 && abbrev->tag != DW_TAG_member
74921315
KS
19509 && abbrev->tag != DW_TAG_imported_unit
19510 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19511 {
19512 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19513 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19514 continue;
19515 }
19516
6f06d47b
YQ
19517 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19518 abbrev);
cd9983dd 19519
48fbe735 19520 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19521
19522 /* This two-pass algorithm for processing partial symbols has a
19523 high cost in cache pressure. Thus, handle some simple cases
19524 here which cover the majority of C partial symbols. DIEs
19525 which neither have specification tags in them, nor could have
19526 specification tags elsewhere pointing at them, can simply be
19527 processed and discarded.
19528
19529 This segment is also optional; scan_partial_symbols and
19530 add_partial_symbol will handle these DIEs if we chain
19531 them in normally. When compilers which do not emit large
19532 quantities of duplicate debug information are more common,
19533 this code can probably be removed. */
19534
19535 /* Any complete simple types at the top level (pretty much all
19536 of them, for a language without namespaces), can be processed
19537 directly. */
19538 if (parent_die == NULL
cd9983dd
YQ
19539 && pdi.has_specification == 0
19540 && pdi.is_declaration == 0
19541 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19542 || pdi.tag == DW_TAG_base_type
d8f62e84 19543 || pdi.tag == DW_TAG_array_type
cd9983dd 19544 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19545 {
7d00ffec 19546 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19547 add_partial_symbol (&pdi, cu);
19548
cd9983dd 19549 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19550 continue;
19551 }
19552
d8228535
JK
19553 /* The exception for DW_TAG_typedef with has_children above is
19554 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19555 type_name_or_error will error on such types later.
d8228535
JK
19556
19557 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19558 it could not find the child DIEs referenced later, this is checked
19559 above. In correct DWARF DW_TAG_typedef should have no children. */
19560
cd9983dd 19561 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19562 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19563 "- DIE at %s [in module %s]"),
cd9983dd 19564 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19565
72bf9492
DJ
19566 /* If we're at the second level, and we're an enumerator, and
19567 our parent has no specification (meaning possibly lives in a
19568 namespace elsewhere), then we can add the partial symbol now
19569 instead of queueing it. */
cd9983dd 19570 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19571 && parent_die != NULL
19572 && parent_die->die_parent == NULL
19573 && parent_die->tag == DW_TAG_enumeration_type
19574 && parent_die->has_specification == 0)
19575 {
7d00ffec 19576 if (pdi.raw_name == NULL)
b98664d3 19577 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19578 else if (building_psymtab)
f0fbb768 19579 add_partial_symbol (&pdi, cu);
72bf9492 19580
cd9983dd 19581 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19582 continue;
19583 }
19584
cd9983dd 19585 struct partial_die_info *part_die
6f06d47b 19586 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19587
72bf9492
DJ
19588 /* We'll save this DIE so link it in. */
19589 part_die->die_parent = parent_die;
19590 part_die->die_sibling = NULL;
19591 part_die->die_child = NULL;
19592
19593 if (last_die && last_die == parent_die)
19594 last_die->die_child = part_die;
19595 else if (last_die)
19596 last_die->die_sibling = part_die;
19597
19598 last_die = part_die;
19599
19600 if (first_die == NULL)
19601 first_die = part_die;
19602
19603 /* Maybe add the DIE to the hash table. Not all DIEs that we
19604 find interesting need to be in the hash table, because we
19605 also have the parent/sibling/child chains; only those that we
19606 might refer to by offset later during partial symbol reading.
19607
19608 For now this means things that might have be the target of a
19609 DW_AT_specification, DW_AT_abstract_origin, or
19610 DW_AT_extension. DW_AT_extension will refer only to
19611 namespaces; DW_AT_abstract_origin refers to functions (and
19612 many things under the function DIE, but we do not recurse
19613 into function DIEs during partial symbol reading) and
19614 possibly variables as well; DW_AT_specification refers to
19615 declarations. Declarations ought to have the DW_AT_declaration
19616 flag. It happens that GCC forgets to put it in sometimes, but
19617 only for functions, not for types.
19618
19619 Adding more things than necessary to the hash table is harmless
19620 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19621 wasted time in find_partial_die, when we reread the compilation
19622 unit with load_all_dies set. */
72bf9492 19623
5afb4e99 19624 if (load_all
72929c62 19625 || abbrev->tag == DW_TAG_constant
5afb4e99 19626 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19627 || abbrev->tag == DW_TAG_variable
19628 || abbrev->tag == DW_TAG_namespace
19629 || part_die->is_declaration)
19630 {
19631 void **slot;
19632
19633 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19634 to_underlying (part_die->sect_off),
19635 INSERT);
72bf9492
DJ
19636 *slot = part_die;
19637 }
19638
72bf9492 19639 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19640 we have no reason to follow the children of structures; for other
98bfdba5
PA
19641 languages we have to, so that we can get at method physnames
19642 to infer fully qualified class names, for DW_AT_specification,
19643 and for C++ template arguments. For C++, we also look one level
19644 inside functions to find template arguments (if the name of the
19645 function does not already contain the template arguments).
bc30ff58 19646
0a4b0913
AB
19647 For Ada and Fortran, we need to scan the children of subprograms
19648 and lexical blocks as well because these languages allow the
19649 definition of nested entities that could be interesting for the
19650 debugger, such as nested subprograms for instance. */
72bf9492 19651 if (last_die->has_children
5afb4e99
DJ
19652 && (load_all
19653 || last_die->tag == DW_TAG_namespace
f55ee35c 19654 || last_die->tag == DW_TAG_module
72bf9492 19655 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19656 || (cu->language == language_cplus
19657 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19658 && (last_die->raw_name == NULL
19659 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19660 || (cu->language != language_c
19661 && (last_die->tag == DW_TAG_class_type
680b30c7 19662 || last_die->tag == DW_TAG_interface_type
72bf9492 19663 || last_die->tag == DW_TAG_structure_type
bc30ff58 19664 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19665 || ((cu->language == language_ada
19666 || cu->language == language_fortran)
bc30ff58
JB
19667 && (last_die->tag == DW_TAG_subprogram
19668 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19669 {
19670 nesting_level++;
19671 parent_die = last_die;
19672 continue;
19673 }
19674
19675 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19676 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19677
19678 /* Back to the top, do it again. */
19679 }
19680}
19681
6f06d47b 19682partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19683 const struct abbrev_info *abbrev)
6f06d47b
YQ
19684 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19685{
19686}
19687
7d00ffec
TT
19688/* See class definition. */
19689
19690const char *
19691partial_die_info::name (dwarf2_cu *cu)
19692{
19693 if (!canonical_name && raw_name != nullptr)
19694 {
19695 struct objfile *objfile = cu->per_objfile->objfile;
19696 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19697 canonical_name = 1;
19698 }
19699
19700 return raw_name;
19701}
19702
35cc7ed7
YQ
19703/* Read a minimal amount of information into the minimal die structure.
19704 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19705
48fbe735
YQ
19706const gdb_byte *
19707partial_die_info::read (const struct die_reader_specs *reader,
19708 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19709{
dee91e82 19710 struct dwarf2_cu *cu = reader->cu;
976ca316 19711 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19712 unsigned int i;
c5aa993b 19713 int has_low_pc_attr = 0;
c906108c 19714 int has_high_pc_attr = 0;
91da1414 19715 int high_pc_relative = 0;
c906108c 19716
fd0a254f 19717 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19718 {
e7da7f8f 19719 attribute attr;
7a5f294d 19720 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19721 /* String and address offsets that need to do the reprocessing have
dda83cd7 19722 already been read at this point, so there is no need to wait until
18a8505e 19723 the loop terminates to do the reprocessing. */
7a5f294d 19724 if (attr.requires_reprocessing_p ())
d0ce17d8 19725 read_attribute_reprocess (reader, &attr, tag);
c906108c 19726 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19727 partial symbol table. */
c906108c
SS
19728 switch (attr.name)
19729 {
19730 case DW_AT_name:
48fbe735 19731 switch (tag)
71c25dea
TT
19732 {
19733 case DW_TAG_compile_unit:
95554aad 19734 case DW_TAG_partial_unit:
348e048f 19735 case DW_TAG_type_unit:
71c25dea
TT
19736 /* Compilation units have a DW_AT_name that is a filename, not
19737 a source language identifier. */
19738 case DW_TAG_enumeration_type:
19739 case DW_TAG_enumerator:
19740 /* These tags always have simple identifiers already; no need
19741 to canonicalize them. */
7d00ffec 19742 canonical_name = 1;
2c830f54 19743 raw_name = attr.as_string ();
71c25dea
TT
19744 break;
19745 default:
7d00ffec 19746 canonical_name = 0;
2c830f54 19747 raw_name = attr.as_string ();
71c25dea
TT
19748 break;
19749 }
c906108c 19750 break;
31ef98ae 19751 case DW_AT_linkage_name:
c906108c 19752 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19753 /* Note that both forms of linkage name might appear. We
19754 assume they will be the same, and we only store the last
19755 one we see. */
95f982e5 19756 linkage_name = attr.as_string ();
c906108c
SS
19757 break;
19758 case DW_AT_low_pc:
19759 has_low_pc_attr = 1;
95f982e5 19760 lowpc = attr.as_address ();
c906108c
SS
19761 break;
19762 case DW_AT_high_pc:
19763 has_high_pc_attr = 1;
95f982e5 19764 highpc = attr.as_address ();
cd6c91b4 19765 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19766 high_pc_relative = 1;
c906108c
SS
19767 break;
19768 case DW_AT_location:
dda83cd7
SM
19769 /* Support the .debug_loc offsets. */
19770 if (attr.form_is_block ())
19771 {
9d2246fc 19772 d.locdesc = attr.as_block ();
dda83cd7
SM
19773 }
19774 else if (attr.form_is_section_offset ())
19775 {
4d3c2250 19776 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19777 }
19778 else
19779 {
4d3c2250
KB
19780 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19781 "partial symbol information");
dda83cd7 19782 }
c906108c 19783 break;
c906108c 19784 case DW_AT_external:
c45bc3f8 19785 is_external = attr.as_boolean ();
c906108c
SS
19786 break;
19787 case DW_AT_declaration:
c45bc3f8 19788 is_declaration = attr.as_boolean ();
c906108c
SS
19789 break;
19790 case DW_AT_type:
48fbe735 19791 has_type = 1;
c906108c
SS
19792 break;
19793 case DW_AT_abstract_origin:
19794 case DW_AT_specification:
72bf9492 19795 case DW_AT_extension:
48fbe735 19796 has_specification = 1;
0826b30a 19797 spec_offset = attr.get_ref_die_offset ();
48fbe735 19798 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19799 || cu->per_cu->is_dwz);
c906108c
SS
19800 break;
19801 case DW_AT_sibling:
19802 /* Ignore absolute siblings, they might point outside of
19803 the current compile unit. */
19804 if (attr.form == DW_FORM_ref_addr)
b98664d3 19805 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19806 else
b9502d3f 19807 {
48fbe735 19808 const gdb_byte *buffer = reader->buffer;
0826b30a 19809 sect_offset off = attr.get_ref_die_offset ();
9c541725 19810 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19811
19812 if (sibling_ptr < info_ptr)
b98664d3 19813 complaint (_("DW_AT_sibling points backwards"));
22869d73 19814 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19815 reader->die_section->overflow_complaint ();
b9502d3f 19816 else
48fbe735 19817 sibling = sibling_ptr;
b9502d3f 19818 }
c906108c 19819 break;
dda83cd7
SM
19820 case DW_AT_byte_size:
19821 has_byte_size = 1;
19822 break;
19823 case DW_AT_const_value:
19824 has_const_value = 1;
19825 break;
68511cec
CES
19826 case DW_AT_calling_convention:
19827 /* DWARF doesn't provide a way to identify a program's source-level
19828 entry point. DW_AT_calling_convention attributes are only meant
19829 to describe functions' calling conventions.
19830
19831 However, because it's a necessary piece of information in
0c1b455e
TT
19832 Fortran, and before DWARF 4 DW_CC_program was the only
19833 piece of debugging information whose definition refers to
19834 a 'main program' at all, several compilers marked Fortran
19835 main programs with DW_CC_program --- even when those
19836 functions use the standard calling conventions.
19837
19838 Although DWARF now specifies a way to provide this
19839 information, we support this practice for backward
19840 compatibility. */
529908cb 19841 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19842 && cu->language == language_fortran)
48fbe735 19843 main_subprogram = 1;
68511cec 19844 break;
481860b3 19845 case DW_AT_inline:
529908cb
TT
19846 {
19847 LONGEST value = attr.constant_value (-1);
19848 if (value == DW_INL_inlined
19849 || value == DW_INL_declared_inlined)
19850 may_be_inlined = 1;
19851 }
481860b3 19852 break;
95554aad
TT
19853
19854 case DW_AT_import:
48fbe735 19855 if (tag == DW_TAG_imported_unit)
36586728 19856 {
0826b30a 19857 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19858 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19859 || cu->per_cu->is_dwz);
19860 }
95554aad
TT
19861 break;
19862
0c1b455e 19863 case DW_AT_main_subprogram:
c45bc3f8 19864 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19865 break;
19866
05caa1d2
TT
19867 case DW_AT_ranges:
19868 {
2b0c7f41
SM
19869 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19870 on DWARF version). */
19871 ULONGEST ranges_offset = attr.as_unsigned ();
19872
19873 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19874 this value. */
19875 if (tag != DW_TAG_compile_unit)
19876 ranges_offset += cu->gnu_ranges_base;
19877
05caa1d2 19878 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19879 nullptr, tag))
05caa1d2
TT
19880 has_pc_info = 1;
19881 }
19882 break;
19883
c906108c
SS
19884 default:
19885 break;
19886 }
19887 }
19888
10d06d82
TT
19889 /* For Ada, if both the name and the linkage name appear, we prefer
19890 the latter. This lets "catch exception" work better, regardless
19891 of the order in which the name and linkage name were emitted.
19892 Really, though, this is just a workaround for the fact that gdb
19893 doesn't store both the name and the linkage name. */
19894 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19895 raw_name = linkage_name;
10d06d82 19896
91da1414 19897 if (high_pc_relative)
48fbe735 19898 highpc += lowpc;
91da1414 19899
9373cf26
JK
19900 if (has_low_pc_attr && has_high_pc_attr)
19901 {
19902 /* When using the GNU linker, .gnu.linkonce. sections are used to
19903 eliminate duplicate copies of functions and vtables and such.
19904 The linker will arbitrarily choose one and discard the others.
19905 The AT_*_pc values for such functions refer to local labels in
19906 these sections. If the section from that file was discarded, the
19907 labels are not in the output, so the relocs get a value of 0.
19908 If this is a discarded function, mark the pc bounds as invalid,
19909 so that GDB will ignore it. */
976ca316 19910 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19911 {
976ca316 19912 struct objfile *objfile = per_objfile->objfile;
08feed99 19913 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19914
b98664d3 19915 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19916 "for DIE at %s [in module %s]"),
48fbe735
YQ
19917 paddress (gdbarch, lowpc),
19918 sect_offset_str (sect_off),
9d8780f0 19919 objfile_name (objfile));
9373cf26
JK
19920 }
19921 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19922 else if (lowpc >= highpc)
9373cf26 19923 {
976ca316 19924 struct objfile *objfile = per_objfile->objfile;
08feed99 19925 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19926
b98664d3 19927 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19928 "for DIE at %s [in module %s]"),
48fbe735
YQ
19929 paddress (gdbarch, lowpc),
19930 paddress (gdbarch, highpc),
19931 sect_offset_str (sect_off),
9c541725 19932 objfile_name (objfile));
9373cf26
JK
19933 }
19934 else
48fbe735 19935 has_pc_info = 1;
9373cf26 19936 }
85cbf3d3 19937
c906108c
SS
19938 return info_ptr;
19939}
19940
72bf9492
DJ
19941/* Find a cached partial DIE at OFFSET in CU. */
19942
d590ff25
YQ
19943struct partial_die_info *
19944dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19945{
19946 struct partial_die_info *lookup_die = NULL;
6f06d47b 19947 struct partial_die_info part_die (sect_off);
72bf9492 19948
9a3c8263 19949 lookup_die = ((struct partial_die_info *)
d590ff25 19950 htab_find_with_hash (partial_dies, &part_die,
9c541725 19951 to_underlying (sect_off)));
72bf9492 19952
72bf9492
DJ
19953 return lookup_die;
19954}
19955
348e048f
DE
19956/* Find a partial DIE at OFFSET, which may or may not be in CU,
19957 except in the case of .debug_types DIEs which do not reference
19958 outside their CU (they do however referencing other types via
55f1336d 19959 DW_FORM_ref_sig8). */
72bf9492 19960
122cf0f2 19961static const struct cu_partial_die_info
9c541725 19962find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19963{
976ca316
SM
19964 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19965 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19966 struct partial_die_info *pd = NULL;
72bf9492 19967
36586728 19968 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19969 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19970 {
d590ff25 19971 pd = cu->find_partial_die (sect_off);
5afb4e99 19972 if (pd != NULL)
fb816e8b 19973 return { cu, pd };
0d99eb77
DE
19974 /* We missed recording what we needed.
19975 Load all dies and try again. */
5afb4e99 19976 }
0d99eb77
DE
19977 else
19978 {
19979 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19980 if (cu->per_cu->is_debug_types)
0d99eb77 19981 {
9d8780f0
SM
19982 error (_("Dwarf Error: Type Unit at offset %s contains"
19983 " external reference to offset %s [in module %s].\n"),
19984 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19985 bfd_get_filename (objfile->obfd));
19986 }
7188ed02
SM
19987 dwarf2_per_cu_data *per_cu
19988 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19989 per_objfile);
72bf9492 19990
976ca316 19991 cu = per_objfile->get_cu (per_cu);
7188ed02 19992 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19993 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19994
976ca316 19995 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19996
19997 cu->last_used = 0;
19998 pd = cu->find_partial_die (sect_off);
0d99eb77 19999 }
5afb4e99 20000
dee91e82
DE
20001 /* If we didn't find it, and not all dies have been loaded,
20002 load them all and try again. */
20003
7188ed02 20004 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 20005 {
7188ed02 20006 cu->per_cu->load_all_dies = 1;
fd820528
DE
20007
20008 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20009 THIS_CU->cu may already be in use. So we can't just free it and
20010 replace its DIEs with the ones we read in. Instead, we leave those
20011 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20012 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20013 set. */
976ca316 20014 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 20015
7188ed02 20016 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
20017 }
20018
20019 if (pd == NULL)
521894aa 20020 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 20021 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 20022 return { cu, pd };
72bf9492
DJ
20023}
20024
abc72ce4
DE
20025/* See if we can figure out if the class lives in a namespace. We do
20026 this by looking for a member function; its demangled name will
20027 contain namespace info, if there is any. */
20028
20029static void
20030guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20031 struct dwarf2_cu *cu)
20032{
20033 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20034 what template types look like, because the demangler
20035 frequently doesn't give the same name as the debug info. We
20036 could fix this by only using the demangled name to get the
20037 prefix (but see comment in read_structure_type). */
20038
20039 struct partial_die_info *real_pdi;
20040 struct partial_die_info *child_pdi;
20041
20042 /* If this DIE (this DIE's specification, if any) has a parent, then
20043 we should not do this. We'll prepend the parent's fully qualified
20044 name when we create the partial symbol. */
20045
20046 real_pdi = struct_pdi;
20047 while (real_pdi->has_specification)
fb816e8b 20048 {
122cf0f2
AB
20049 auto res = find_partial_die (real_pdi->spec_offset,
20050 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
20051 real_pdi = res.pdi;
20052 cu = res.cu;
20053 }
abc72ce4
DE
20054
20055 if (real_pdi->die_parent != NULL)
20056 return;
20057
20058 for (child_pdi = struct_pdi->die_child;
20059 child_pdi != NULL;
20060 child_pdi = child_pdi->die_sibling)
20061 {
20062 if (child_pdi->tag == DW_TAG_subprogram
20063 && child_pdi->linkage_name != NULL)
20064 {
43816ebc 20065 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
20066 (cu->language_defn->class_name_from_physname
20067 (child_pdi->linkage_name));
abc72ce4
DE
20068 if (actual_class_name != NULL)
20069 {
5e22e966 20070 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20071 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20072 struct_pdi->canonical_name = 1;
abc72ce4
DE
20073 }
20074 break;
20075 }
20076 }
20077}
20078
25c11aca
TV
20079/* Return true if a DIE with TAG may have the DW_AT_const_value
20080 attribute. */
20081
20082static bool
20083can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20084{
20085 switch (tag)
20086 {
20087 case DW_TAG_constant:
20088 case DW_TAG_enumerator:
20089 case DW_TAG_formal_parameter:
20090 case DW_TAG_template_value_param:
20091 case DW_TAG_variable:
20092 return true;
20093 }
20094
20095 return false;
20096}
20097
52356b79
YQ
20098void
20099partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 20100{
abc72ce4
DE
20101 /* Once we've fixed up a die, there's no point in doing so again.
20102 This also avoids a memory leak if we were to call
20103 guess_partial_die_structure_name multiple times. */
52356b79 20104 if (fixup_called)
abc72ce4
DE
20105 return;
20106
72bf9492
DJ
20107 /* If we found a reference attribute and the DIE has no name, try
20108 to find a name in the referred to DIE. */
20109
7d00ffec 20110 if (raw_name == NULL && has_specification)
72bf9492
DJ
20111 {
20112 struct partial_die_info *spec_die;
72bf9492 20113
122cf0f2 20114 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
20115 spec_die = res.pdi;
20116 cu = res.cu;
72bf9492 20117
52356b79 20118 spec_die->fixup (cu);
72bf9492 20119
7d00ffec 20120 if (spec_die->raw_name)
72bf9492 20121 {
7d00ffec
TT
20122 raw_name = spec_die->raw_name;
20123 canonical_name = spec_die->canonical_name;
72bf9492
DJ
20124
20125 /* Copy DW_AT_external attribute if it is set. */
20126 if (spec_die->is_external)
52356b79 20127 is_external = spec_die->is_external;
72bf9492
DJ
20128 }
20129 }
20130
25c11aca
TV
20131 if (!has_const_value && has_specification
20132 && can_have_DW_AT_const_value_p (tag))
20133 {
20134 struct partial_die_info *spec_die;
20135
20136 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20137 spec_die = res.pdi;
20138 cu = res.cu;
20139
20140 spec_die->fixup (cu);
20141
20142 if (spec_die->has_const_value)
20143 {
20144 /* Copy DW_AT_const_value attribute if it is set. */
20145 has_const_value = spec_die->has_const_value;
20146 }
20147 }
20148
72bf9492 20149 /* Set default names for some unnamed DIEs. */
72bf9492 20150
7d00ffec
TT
20151 if (raw_name == NULL && tag == DW_TAG_namespace)
20152 {
20153 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20154 canonical_name = 1;
20155 }
72bf9492 20156
abc72ce4
DE
20157 /* If there is no parent die to provide a namespace, and there are
20158 children, see if we can determine the namespace from their linkage
122d1940 20159 name. */
abc72ce4 20160 if (cu->language == language_cplus
5e22e966 20161 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
20162 && die_parent == NULL
20163 && has_children
20164 && (tag == DW_TAG_class_type
20165 || tag == DW_TAG_structure_type
20166 || tag == DW_TAG_union_type))
20167 guess_partial_die_structure_name (this, cu);
abc72ce4 20168
53832f31
TT
20169 /* GCC might emit a nameless struct or union that has a linkage
20170 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 20171 if (raw_name == NULL
52356b79
YQ
20172 && (tag == DW_TAG_class_type
20173 || tag == DW_TAG_interface_type
20174 || tag == DW_TAG_structure_type
20175 || tag == DW_TAG_union_type)
20176 && linkage_name != NULL)
53832f31 20177 {
43816ebc
TT
20178 gdb::unique_xmalloc_ptr<char> demangled
20179 (gdb_demangle (linkage_name, DMGL_TYPES));
20180 if (demangled != nullptr)
53832f31 20181 {
96408a79
SA
20182 const char *base;
20183
20184 /* Strip any leading namespaces/classes, keep only the base name.
20185 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
20186 base = strrchr (demangled.get (), ':');
20187 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
20188 base++;
20189 else
43816ebc 20190 base = demangled.get ();
96408a79 20191
5e22e966 20192 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20193 raw_name = objfile->intern (base);
20194 canonical_name = 1;
53832f31
TT
20195 }
20196 }
20197
52356b79 20198 fixup_called = 1;
72bf9492
DJ
20199}
20200
d0ce17d8 20201/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
20202 contents from the given SECTION in the HEADER.
20203
20204 HEADER_OFFSET is the offset of the header in the section. */
41144253 20205static void
d0ce17d8 20206read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
20207 struct dwarf2_section_info *section,
20208 sect_offset header_offset)
41144253 20209{
20210 unsigned int bytes_read;
20211 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
20212 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20213
41144253 20214 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20215 info_ptr += bytes_read;
a1c40103 20216
41144253 20217 header->version = read_2_bytes (abfd, info_ptr);
20218 info_ptr += 2;
a1c40103 20219
41144253 20220 header->addr_size = read_1_byte (abfd, info_ptr);
20221 info_ptr += 1;
a1c40103 20222
41144253 20223 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20224 info_ptr += 1;
a1c40103 20225
41144253 20226 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20227}
20228
20229/* Return the DW_AT_loclists_base value for the CU. */
20230static ULONGEST
20231lookup_loclist_base (struct dwarf2_cu *cu)
20232{
20233 /* For the .dwo unit, the loclist_base points to the first offset following
20234 the header. The header consists of the following entities-
20235 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20236 bit format)
20237 2. version (2 bytes)
20238 3. address size (1 byte)
20239 4. segment selector size (1 byte)
20240 5. offset entry count (4 bytes)
20241 These sizes are derived as per the DWARFv5 standard. */
20242 if (cu->dwo_unit != nullptr)
20243 {
20244 if (cu->header.initial_length_size == 4)
20245 return LOCLIST_HEADER_SIZE32;
20246 return LOCLIST_HEADER_SIZE64;
20247 }
20248 return cu->loclist_base;
20249}
20250
20251/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20252 array of offsets in the .debug_loclists section. */
e57933dc
SM
20253
20254static sect_offset
41144253 20255read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20256{
976ca316
SM
20257 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20258 struct objfile *objfile = per_objfile->objfile;
41144253 20259 bfd *abfd = objfile->obfd;
a1c40103
SM
20260 ULONGEST loclist_header_size =
20261 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20262 : LOCLIST_HEADER_SIZE64);
41144253 20263 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
20264
20265 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20266 ULONGEST start_offset =
20267 loclist_base + loclist_index * cu->header.offset_size;
20268
a1c40103 20269 /* Get loclists section. */
41144253 20270 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20271
a1c40103 20272 /* Read the loclists section content. */
41144253 20273 section->read (objfile);
20274 if (section->buffer == NULL)
a0c1eeba
SM
20275 error (_("DW_FORM_loclistx used without .debug_loclists "
20276 "section [in module %s]"), objfile_name (objfile));
20277
a1c40103
SM
20278 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20279 so if loclist_base is smaller than the header size, we have a problem. */
20280 if (loclist_base < loclist_header_size)
20281 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20282 objfile_name (objfile));
20283
20284 /* Read the header of the loclists contribution. */
d0ce17d8 20285 struct loclists_rnglists_header header;
a1c40103
SM
20286 read_loclists_rnglists_header (&header, section,
20287 (sect_offset) (loclist_base - loclist_header_size));
20288
20289 /* Verify the loclist index is valid. */
41144253 20290 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
20291 error (_("DW_FORM_loclistx pointing outside of "
20292 ".debug_loclists offset array [in module %s]"),
20293 objfile_name (objfile));
20294
05787bad
SM
20295 /* Validate that reading won't go beyond the end of the section. */
20296 if (start_offset + cu->header.offset_size > section->size)
20297 error (_("Reading DW_FORM_loclistx index beyond end of"
20298 ".debug_loclists section [in module %s]"),
20299 objfile_name (objfile));
20300
20301 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 20302
20303 if (cu->header.offset_size == 4)
e57933dc 20304 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 20305 else
e57933dc 20306 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 20307}
20308
d0ce17d8
CT
20309/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20310 array of offsets in the .debug_rnglists section. */
e57933dc
SM
20311
20312static sect_offset
d0ce17d8
CT
20313read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20314 dwarf_tag tag)
20315{
20316 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20317 struct objfile *objfile = dwarf2_per_objfile->objfile;
20318 bfd *abfd = objfile->obfd;
20319 ULONGEST rnglist_header_size =
20320 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20321 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
20322
20323 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20324 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20325 doesn't apply. */
d0ce17d8 20326 ULONGEST rnglist_base =
2b0c7f41 20327 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
20328
20329 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
20330 ULONGEST start_offset =
20331 rnglist_base + rnglist_index * cu->header.offset_size;
20332
20333 /* Get rnglists section. */
20334 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20335
20336 /* Read the rnglists section content. */
20337 section->read (objfile);
20338 if (section->buffer == nullptr)
20339 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20340 "[in module %s]"),
20341 objfile_name (objfile));
20342
a1c40103
SM
20343 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20344 so if rnglist_base is smaller than the header size, we have a problem. */
20345 if (rnglist_base < rnglist_header_size)
20346 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20347 objfile_name (objfile));
20348
20349 /* Read the header of the rnglists contribution. */
d0ce17d8 20350 struct loclists_rnglists_header header;
a1c40103
SM
20351 read_loclists_rnglists_header (&header, section,
20352 (sect_offset) (rnglist_base - rnglist_header_size));
20353
20354 /* Verify the rnglist index is valid. */
d0ce17d8
CT
20355 if (rnglist_index >= header.offset_entry_count)
20356 error (_("DW_FORM_rnglistx index pointing outside of "
20357 ".debug_rnglists offset array [in module %s]"),
20358 objfile_name (objfile));
20359
d0ce17d8 20360 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20361 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20362 error (_("Reading DW_FORM_rnglistx index beyond end of"
20363 ".debug_rnglists section [in module %s]"),
20364 objfile_name (objfile));
20365
20366 const gdb_byte *info_ptr = section->buffer + start_offset;
20367
20368 if (cu->header.offset_size == 4)
e57933dc 20369 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 20370 else
e57933dc 20371 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
20372}
20373
18a8505e
AT
20374/* Process the attributes that had to be skipped in the first round. These
20375 attributes are the ones that need str_offsets_base or addr_base attributes.
20376 They could not have been processed in the first round, because at the time
20377 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20378static void
20379read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20380 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20381{
20382 struct dwarf2_cu *cu = reader->cu;
20383 switch (attr->form)
20384 {
20385 case DW_FORM_addrx:
20386 case DW_FORM_GNU_addr_index:
36d378cf
TT
20387 attr->set_address (read_addr_index (cu,
20388 attr->as_unsigned_reprocess ()));
dda83cd7 20389 break;
41144253 20390 case DW_FORM_loclistx:
b1829e1b 20391 {
e57933dc 20392 sect_offset loclists_sect_off
b1829e1b
SM
20393 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20394
e57933dc 20395 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
20396 }
20397 break;
d0ce17d8 20398 case DW_FORM_rnglistx:
b1829e1b 20399 {
e57933dc 20400 sect_offset rnglists_sect_off
b1829e1b
SM
20401 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20402
e57933dc 20403 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 20404 }
dda83cd7 20405 break;
18a8505e
AT
20406 case DW_FORM_strx:
20407 case DW_FORM_strx1:
20408 case DW_FORM_strx2:
20409 case DW_FORM_strx3:
20410 case DW_FORM_strx4:
20411 case DW_FORM_GNU_str_index:
20412 {
fe56917a 20413 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20414 gdb_assert (!attr->canonical_string_p ());
18a8505e 20415 if (reader->dwo_file != NULL)
c6481205
TT
20416 attr->set_string_noncanonical (read_dwo_str_index (reader,
20417 str_index));
18a8505e 20418 else
c6481205
TT
20419 attr->set_string_noncanonical (read_stub_str_index (cu,
20420 str_index));
18a8505e
AT
20421 break;
20422 }
20423 default:
20424 gdb_assert_not_reached (_("Unexpected DWARF form."));
20425 }
20426}
20427
a8329558 20428/* Read an attribute value described by an attribute form. */
c906108c 20429
d521ce57 20430static const gdb_byte *
dee91e82
DE
20431read_attribute_value (const struct die_reader_specs *reader,
20432 struct attribute *attr, unsigned form,
7a5f294d 20433 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20434{
dee91e82 20435 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20436 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20437 struct objfile *objfile = per_objfile->objfile;
dee91e82 20438 bfd *abfd = reader->abfd;
e7c27a73 20439 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20440 unsigned int bytes_read;
20441 struct dwarf_block *blk;
20442
aead7601 20443 attr->form = (enum dwarf_form) form;
a8329558 20444 switch (form)
c906108c 20445 {
c906108c 20446 case DW_FORM_ref_addr:
7c290a04
TT
20447 if (cu_header->version == 2)
20448 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 20449 &bytes_read));
ae411497 20450 else
7c290a04 20451 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20452 &bytes_read));
ae411497
TT
20453 info_ptr += bytes_read;
20454 break;
36586728 20455 case DW_FORM_GNU_ref_alt:
7c290a04 20456 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20457 &bytes_read));
36586728
TT
20458 info_ptr += bytes_read;
20459 break;
ae411497 20460 case DW_FORM_addr:
08feed99
TT
20461 {
20462 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 20463 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
20464 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20465 attr->set_address (addr);
08feed99
TT
20466 info_ptr += bytes_read;
20467 }
c906108c
SS
20468 break;
20469 case DW_FORM_block2:
7b5a2f43 20470 blk = dwarf_alloc_block (cu);
c906108c
SS
20471 blk->size = read_2_bytes (abfd, info_ptr);
20472 info_ptr += 2;
20473 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20474 info_ptr += blk->size;
9d2246fc 20475 attr->set_block (blk);
c906108c
SS
20476 break;
20477 case DW_FORM_block4:
7b5a2f43 20478 blk = dwarf_alloc_block (cu);
c906108c
SS
20479 blk->size = read_4_bytes (abfd, info_ptr);
20480 info_ptr += 4;
20481 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20482 info_ptr += blk->size;
9d2246fc 20483 attr->set_block (blk);
c906108c
SS
20484 break;
20485 case DW_FORM_data2:
414ad644 20486 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20487 info_ptr += 2;
20488 break;
20489 case DW_FORM_data4:
414ad644 20490 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20491 info_ptr += 4;
20492 break;
20493 case DW_FORM_data8:
414ad644 20494 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20495 info_ptr += 8;
20496 break;
0224619f
JK
20497 case DW_FORM_data16:
20498 blk = dwarf_alloc_block (cu);
20499 blk->size = 16;
20500 blk->data = read_n_bytes (abfd, info_ptr, 16);
20501 info_ptr += 16;
9d2246fc 20502 attr->set_block (blk);
0224619f 20503 break;
2dc7f7b3 20504 case DW_FORM_sec_offset:
7c290a04 20505 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20506 &bytes_read));
2dc7f7b3
TT
20507 info_ptr += bytes_read;
20508 break;
41144253 20509 case DW_FORM_loclistx:
20510 {
7a5f294d
TT
20511 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20512 &bytes_read));
20513 info_ptr += bytes_read;
41144253 20514 }
20515 break;
c906108c 20516 case DW_FORM_string:
c6481205
TT
20517 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20518 &bytes_read));
c906108c
SS
20519 info_ptr += bytes_read;
20520 break;
4bdf3d34 20521 case DW_FORM_strp:
36586728
TT
20522 if (!cu->per_cu->is_dwz)
20523 {
c6481205
TT
20524 attr->set_string_noncanonical
20525 (read_indirect_string (per_objfile,
20526 abfd, info_ptr, cu_header,
20527 &bytes_read));
36586728
TT
20528 info_ptr += bytes_read;
20529 break;
20530 }
20531 /* FALLTHROUGH */
43988095
JK
20532 case DW_FORM_line_strp:
20533 if (!cu->per_cu->is_dwz)
20534 {
c6481205
TT
20535 attr->set_string_noncanonical
20536 (per_objfile->read_line_string (info_ptr, cu_header,
20537 &bytes_read));
43988095
JK
20538 info_ptr += bytes_read;
20539 break;
20540 }
20541 /* FALLTHROUGH */
36586728
TT
20542 case DW_FORM_GNU_strp_alt:
20543 {
a7308ce0 20544 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
20545 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20546 &bytes_read);
36586728 20547
c6481205
TT
20548 attr->set_string_noncanonical
20549 (dwz->read_string (objfile, str_offset));
36586728
TT
20550 info_ptr += bytes_read;
20551 }
4bdf3d34 20552 break;
2dc7f7b3 20553 case DW_FORM_exprloc:
c906108c 20554 case DW_FORM_block:
7b5a2f43 20555 blk = dwarf_alloc_block (cu);
c906108c
SS
20556 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20557 info_ptr += bytes_read;
20558 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20559 info_ptr += blk->size;
9d2246fc 20560 attr->set_block (blk);
c906108c
SS
20561 break;
20562 case DW_FORM_block1:
7b5a2f43 20563 blk = dwarf_alloc_block (cu);
c906108c
SS
20564 blk->size = read_1_byte (abfd, info_ptr);
20565 info_ptr += 1;
20566 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20567 info_ptr += blk->size;
9d2246fc 20568 attr->set_block (blk);
c906108c
SS
20569 break;
20570 case DW_FORM_data1:
c906108c 20571 case DW_FORM_flag:
414ad644 20572 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20573 info_ptr += 1;
20574 break;
2dc7f7b3 20575 case DW_FORM_flag_present:
414ad644 20576 attr->set_unsigned (1);
2dc7f7b3 20577 break;
c906108c 20578 case DW_FORM_sdata:
1bc397c5 20579 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20580 info_ptr += bytes_read;
20581 break;
18a8505e 20582 case DW_FORM_rnglistx:
7a5f294d
TT
20583 {
20584 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20585 &bytes_read));
20586 info_ptr += bytes_read;
20587 }
20588 break;
d0ce17d8 20589 case DW_FORM_udata:
414ad644 20590 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20591 info_ptr += bytes_read;
20592 break;
20593 case DW_FORM_ref1:
7c290a04 20594 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20595 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20596 info_ptr += 1;
20597 break;
20598 case DW_FORM_ref2:
7c290a04 20599 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20600 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20601 info_ptr += 2;
20602 break;
20603 case DW_FORM_ref4:
7c290a04 20604 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20605 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20606 info_ptr += 4;
20607 break;
613e1657 20608 case DW_FORM_ref8:
7c290a04 20609 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20610 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20611 info_ptr += 8;
20612 break;
55f1336d 20613 case DW_FORM_ref_sig8:
630ed6b9 20614 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20615 info_ptr += 8;
20616 break;
c906108c 20617 case DW_FORM_ref_udata:
7c290a04 20618 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20619 + read_unsigned_leb128 (abfd, info_ptr,
20620 &bytes_read)));
c906108c
SS
20621 info_ptr += bytes_read;
20622 break;
c906108c 20623 case DW_FORM_indirect:
a8329558
KW
20624 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20625 info_ptr += bytes_read;
43988095
JK
20626 if (form == DW_FORM_implicit_const)
20627 {
20628 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20629 info_ptr += bytes_read;
20630 }
20631 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20632 info_ptr);
43988095
JK
20633 break;
20634 case DW_FORM_implicit_const:
1bc397c5 20635 attr->set_signed (implicit_const);
a8329558 20636 break;
336d760d 20637 case DW_FORM_addrx:
3019eac3 20638 case DW_FORM_GNU_addr_index:
fe56917a
TT
20639 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20640 &bytes_read));
3019eac3
DE
20641 info_ptr += bytes_read;
20642 break;
cf532bd1 20643 case DW_FORM_strx:
15f18d14
AT
20644 case DW_FORM_strx1:
20645 case DW_FORM_strx2:
20646 case DW_FORM_strx3:
20647 case DW_FORM_strx4:
3019eac3 20648 case DW_FORM_GNU_str_index:
3019eac3 20649 {
15f18d14
AT
20650 ULONGEST str_index;
20651 if (form == DW_FORM_strx1)
20652 {
20653 str_index = read_1_byte (abfd, info_ptr);
20654 info_ptr += 1;
20655 }
20656 else if (form == DW_FORM_strx2)
20657 {
20658 str_index = read_2_bytes (abfd, info_ptr);
20659 info_ptr += 2;
20660 }
20661 else if (form == DW_FORM_strx3)
20662 {
20663 str_index = read_3_bytes (abfd, info_ptr);
20664 info_ptr += 3;
20665 }
20666 else if (form == DW_FORM_strx4)
20667 {
20668 str_index = read_4_bytes (abfd, info_ptr);
20669 info_ptr += 4;
20670 }
20671 else
20672 {
20673 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20674 info_ptr += bytes_read;
20675 }
fe56917a 20676 attr->set_unsigned_reprocess (str_index);
7a5f294d 20677 }
3019eac3 20678 break;
c906108c 20679 default:
8a3fe4f8 20680 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20681 dwarf_form_name (form),
20682 bfd_get_filename (abfd));
c906108c 20683 }
28e94949 20684
36586728 20685 /* Super hack. */
cd6c91b4 20686 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20687 attr->form = DW_FORM_GNU_ref_alt;
20688
28e94949
JB
20689 /* We have seen instances where the compiler tried to emit a byte
20690 size attribute of -1 which ended up being encoded as an unsigned
20691 0xffffffff. Although 0xffffffff is technically a valid size value,
20692 an object of this size seems pretty unlikely so we can relatively
20693 safely treat these cases as if the size attribute was invalid and
20694 treat them as zero by default. */
20695 if (attr->name == DW_AT_byte_size
20696 && form == DW_FORM_data4
529908cb 20697 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20698 {
20699 complaint
dda83cd7
SM
20700 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20701 hex_string (attr->as_unsigned ()));
414ad644 20702 attr->set_unsigned (0);
01c66ae6 20703 }
28e94949 20704
c906108c
SS
20705 return info_ptr;
20706}
20707
a8329558
KW
20708/* Read an attribute described by an abbreviated attribute. */
20709
d521ce57 20710static const gdb_byte *
dee91e82 20711read_attribute (const struct die_reader_specs *reader,
4444f407 20712 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20713 const gdb_byte *info_ptr)
a8329558
KW
20714{
20715 attr->name = abbrev->name;
c6481205 20716 attr->string_is_canonical = 0;
fe56917a 20717 attr->requires_reprocessing = 0;
43988095 20718 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20719 abbrev->implicit_const, info_ptr);
a8329558
KW
20720}
20721
43988095
JK
20722/* Return pointer to string at .debug_str offset STR_OFFSET. */
20723
20724static const char *
976ca316 20725read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20726 LONGEST str_offset)
43988095 20727{
976ca316
SM
20728 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20729 str_offset, "DW_FORM_strp");
c906108c
SS
20730}
20731
43988095
JK
20732/* Return pointer to string at .debug_str offset as read from BUF.
20733 BUF is assumed to be in a compilation unit described by CU_HEADER.
20734 Return *BYTES_READ_PTR count of bytes read from BUF. */
20735
d521ce57 20736static const char *
976ca316 20737read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20738 const gdb_byte *buf,
cf2c3c16
TT
20739 const struct comp_unit_head *cu_header,
20740 unsigned int *bytes_read_ptr)
20741{
8266302d 20742 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20743
976ca316 20744 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20745}
20746
86c0bb4c 20747/* See read.h. */
43988095 20748
86c0bb4c
TT
20749const char *
20750dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20751 const struct comp_unit_head *cu_header,
20752 unsigned int *bytes_read_ptr)
43988095 20753{
86c0bb4c 20754 bfd *abfd = objfile->obfd;
8266302d 20755 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20756
5989a64e 20757 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20758}
20759
3019eac3 20760/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20761 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20762 ADDR_SIZE is the size of addresses from the CU header. */
20763
20764static CORE_ADDR
976ca316
SM
20765read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20766 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20767{
976ca316 20768 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20769 bfd *abfd = objfile->obfd;
20770 const gdb_byte *info_ptr;
18a8505e 20771 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20772
976ca316
SM
20773 per_objfile->per_bfd->addr.read (objfile);
20774 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20775 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20776 objfile_name (objfile));
18a8505e 20777 if (addr_base_or_zero + addr_index * addr_size
976ca316 20778 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20779 error (_("DW_FORM_addr_index pointing outside of "
20780 ".debug_addr section [in module %s]"),
4262abfb 20781 objfile_name (objfile));
976ca316
SM
20782 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20783 + addr_index * addr_size);
3019eac3
DE
20784 if (addr_size == 4)
20785 return bfd_get_32 (abfd, info_ptr);
20786 else
20787 return bfd_get_64 (abfd, info_ptr);
20788}
20789
20790/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20791
20792static CORE_ADDR
20793read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20794{
5e22e966 20795 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20796 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20797}
20798
20799/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20800
20801static CORE_ADDR
d521ce57 20802read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20803 unsigned int *bytes_read)
20804{
5e22e966 20805 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20806 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20807
20808 return read_addr_index (cu, addr_index);
20809}
20810
450a1bfc 20811/* See read.h. */
3019eac3
DE
20812
20813CORE_ADDR
82ca3f51 20814dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20815 dwarf2_per_objfile *per_objfile,
82ca3f51 20816 unsigned int addr_index)
3019eac3 20817{
976ca316 20818 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20819 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20820 int addr_size;
20821
3019eac3
DE
20822 /* We need addr_base and addr_size.
20823 If we don't have PER_CU->cu, we have to get it.
20824 Nasty, but the alternative is storing the needed info in PER_CU,
20825 which at this point doesn't seem justified: it's not clear how frequently
20826 it would get used and it would increase the size of every PER_CU.
20827 Entry points like dwarf2_per_cu_addr_size do a similar thing
20828 so we're not in uncharted territory here.
20829 Alas we need to be a bit more complicated as addr_base is contained
20830 in the DIE.
20831
20832 We don't need to read the entire CU(/TU).
20833 We just need the header and top level die.
a1b64ce1 20834
3019eac3 20835 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20836 For now we skip this optimization. */
3019eac3
DE
20837
20838 if (cu != NULL)
20839 {
20840 addr_base = cu->addr_base;
20841 addr_size = cu->header.addr_size;
20842 }
20843 else
20844 {
976ca316 20845 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20846 addr_base = reader.cu->addr_base;
20847 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20848 }
20849
976ca316 20850 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20851}
20852
18a8505e
AT
20853/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20854 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20855 DWO file. */
3019eac3 20856
d521ce57 20857static const char *
18a8505e
AT
20858read_str_index (struct dwarf2_cu *cu,
20859 struct dwarf2_section_info *str_section,
20860 struct dwarf2_section_info *str_offsets_section,
20861 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20862{
976ca316
SM
20863 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20864 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20865 const char *objf_name = objfile_name (objfile);
3019eac3 20866 bfd *abfd = objfile->obfd;
d521ce57 20867 const gdb_byte *info_ptr;
3019eac3 20868 ULONGEST str_offset;
cf532bd1 20869 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20870
96b79293
TT
20871 str_section->read (objfile);
20872 str_offsets_section->read (objfile);
73869dc2 20873 if (str_section->buffer == NULL)
18a8505e 20874 error (_("%s used without %s section"
9d8780f0 20875 " in CU at offset %s [in module %s]"),
96b79293 20876 form_name, str_section->get_name (),
dda83cd7 20877 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20878 if (str_offsets_section->buffer == NULL)
18a8505e 20879 error (_("%s used without %s section"
9d8780f0 20880 " in CU at offset %s [in module %s]"),
96b79293 20881 form_name, str_section->get_name (),
dda83cd7 20882 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20883 info_ptr = (str_offsets_section->buffer
18a8505e 20884 + str_offsets_base
3019eac3
DE
20885 + str_index * cu->header.offset_size);
20886 if (cu->header.offset_size == 4)
20887 str_offset = bfd_get_32 (abfd, info_ptr);
20888 else
20889 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20890 if (str_offset >= str_section->size)
57d63ce2 20891 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20892 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20893 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20894 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20895}
20896
18a8505e
AT
20897/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20898
20899static const char *
20900read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20901{
20902 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20903 ? reader->cu->header.addr_size : 0;
20904 return read_str_index (reader->cu,
20905 &reader->dwo_file->sections.str,
20906 &reader->dwo_file->sections.str_offsets,
20907 str_offsets_base, str_index);
20908}
20909
20910/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20911
20912static const char *
20913read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20914{
5e22e966 20915 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20916 const char *objf_name = objfile_name (objfile);
20917 static const char form_name[] = "DW_FORM_GNU_str_index";
20918 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20919
20920 if (!cu->str_offsets_base.has_value ())
20921 error (_("%s used in Fission stub without %s"
20922 " in CU at offset 0x%lx [in module %s]"),
20923 form_name, str_offsets_attr_name,
20924 (long) cu->header.offset_size, objf_name);
20925
20926 return read_str_index (cu,
5e22e966
SM
20927 &cu->per_objfile->per_bfd->str,
20928 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20929 *cu->str_offsets_base, str_index);
20930}
20931
3019eac3
DE
20932/* Return the length of an LEB128 number in BUF. */
20933
20934static int
20935leb128_size (const gdb_byte *buf)
20936{
20937 const gdb_byte *begin = buf;
20938 gdb_byte byte;
20939
20940 while (1)
20941 {
20942 byte = *buf++;
20943 if ((byte & 128) == 0)
20944 return buf - begin;
20945 }
20946}
20947
c906108c 20948static void
e142c38c 20949set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20950{
20951 switch (lang)
20952 {
20953 case DW_LANG_C89:
76bee0cc 20954 case DW_LANG_C99:
0cfd832f 20955 case DW_LANG_C11:
c906108c 20956 case DW_LANG_C:
d1be3247 20957 case DW_LANG_UPC:
e142c38c 20958 cu->language = language_c;
c906108c 20959 break;
9c37b5ae 20960 case DW_LANG_Java:
c906108c 20961 case DW_LANG_C_plus_plus:
0cfd832f
MW
20962 case DW_LANG_C_plus_plus_11:
20963 case DW_LANG_C_plus_plus_14:
e142c38c 20964 cu->language = language_cplus;
c906108c 20965 break;
6aecb9c2
JB
20966 case DW_LANG_D:
20967 cu->language = language_d;
20968 break;
c906108c
SS
20969 case DW_LANG_Fortran77:
20970 case DW_LANG_Fortran90:
b21b22e0 20971 case DW_LANG_Fortran95:
f7de9aab
MW
20972 case DW_LANG_Fortran03:
20973 case DW_LANG_Fortran08:
e142c38c 20974 cu->language = language_fortran;
c906108c 20975 break;
a766d390
DE
20976 case DW_LANG_Go:
20977 cu->language = language_go;
20978 break;
c906108c 20979 case DW_LANG_Mips_Assembler:
e142c38c 20980 cu->language = language_asm;
c906108c
SS
20981 break;
20982 case DW_LANG_Ada83:
8aaf0b47 20983 case DW_LANG_Ada95:
bc5f45f8
JB
20984 cu->language = language_ada;
20985 break;
72019c9c
GM
20986 case DW_LANG_Modula2:
20987 cu->language = language_m2;
20988 break;
fe8e67fd
PM
20989 case DW_LANG_Pascal83:
20990 cu->language = language_pascal;
20991 break;
22566fbd
DJ
20992 case DW_LANG_ObjC:
20993 cu->language = language_objc;
20994 break;
c44af4eb
TT
20995 case DW_LANG_Rust:
20996 case DW_LANG_Rust_old:
20997 cu->language = language_rust;
20998 break;
c906108c
SS
20999 case DW_LANG_Cobol74:
21000 case DW_LANG_Cobol85:
c906108c 21001 default:
e142c38c 21002 cu->language = language_minimal;
c906108c
SS
21003 break;
21004 }
e142c38c 21005 cu->language_defn = language_def (cu->language);
c906108c
SS
21006}
21007
21008/* Return the named attribute or NULL if not there. */
21009
21010static struct attribute *
e142c38c 21011dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 21012{
a48e046c 21013 for (;;)
c906108c 21014 {
a48e046c
TT
21015 unsigned int i;
21016 struct attribute *spec = NULL;
21017
21018 for (i = 0; i < die->num_attrs; ++i)
21019 {
21020 if (die->attrs[i].name == name)
21021 return &die->attrs[i];
21022 if (die->attrs[i].name == DW_AT_specification
21023 || die->attrs[i].name == DW_AT_abstract_origin)
21024 spec = &die->attrs[i];
21025 }
21026
21027 if (!spec)
21028 break;
c906108c 21029
f2f0e013 21030 die = follow_die_ref (die, spec, &cu);
f2f0e013 21031 }
c5aa993b 21032
c906108c
SS
21033 return NULL;
21034}
21035
7d45c7c3
KB
21036/* Return the string associated with a string-typed attribute, or NULL if it
21037 is either not found or is of an incorrect type. */
21038
21039static const char *
21040dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21041{
21042 struct attribute *attr;
21043 const char *str = NULL;
21044
21045 attr = dwarf2_attr (die, name, cu);
21046
21047 if (attr != NULL)
21048 {
95f982e5 21049 str = attr->as_string ();
e61108c9 21050 if (str == nullptr)
dda83cd7 21051 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
21052 "DIE at %s in module %s"),
21053 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 21054 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
21055 }
21056
21057 return str;
21058}
21059
a084a2a6 21060/* Return the dwo name or NULL if not present. If present, it is in either
85102364 21061 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
21062static const char *
21063dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21064{
21065 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21066 if (dwo_name == nullptr)
21067 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21068 return dwo_name;
21069}
21070
05cf31d1
JB
21071/* Return non-zero iff the attribute NAME is defined for the given DIE,
21072 and holds a non-zero value. This function should only be used for
2dc7f7b3 21073 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
21074
21075static int
21076dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21077{
21078 struct attribute *attr = dwarf2_attr (die, name, cu);
21079
c45bc3f8 21080 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
21081}
21082
3ca72b44 21083static int
e142c38c 21084die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 21085{
05cf31d1
JB
21086 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21087 which value is non-zero. However, we have to be careful with
21088 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21089 (via dwarf2_flag_true_p) follows this attribute. So we may
21090 end up accidently finding a declaration attribute that belongs
21091 to a different DIE referenced by the specification attribute,
21092 even though the given DIE does not have a declaration attribute. */
21093 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21094 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
21095}
21096
63d06c5c 21097/* Return the die giving the specification for DIE, if there is
f2f0e013 21098 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
21099 containing the return value on output. If there is no
21100 specification, but there is an abstract origin, that is
21101 returned. */
63d06c5c
DC
21102
21103static struct die_info *
f2f0e013 21104die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 21105{
f2f0e013
DJ
21106 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21107 *spec_cu);
63d06c5c 21108
edb3359d
DJ
21109 if (spec_attr == NULL)
21110 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21111
63d06c5c
DC
21112 if (spec_attr == NULL)
21113 return NULL;
21114 else
f2f0e013 21115 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 21116}
c906108c 21117
527f3840
JK
21118/* Stub for free_line_header to match void * callback types. */
21119
21120static void
21121free_line_header_voidp (void *arg)
21122{
9a3c8263 21123 struct line_header *lh = (struct line_header *) arg;
527f3840 21124
fff8551c 21125 delete lh;
527f3840
JK
21126}
21127
83769d0b 21128/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
21129
21130static struct dwarf2_section_info *
21131get_debug_line_section (struct dwarf2_cu *cu)
21132{
21133 struct dwarf2_section_info *section;
976ca316 21134 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
21135
21136 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21137 DWO file. */
21138 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21139 section = &cu->dwo_unit->dwo_file->sections.line;
21140 else if (cu->per_cu->is_dwz)
21141 {
a7308ce0 21142 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
21143
21144 section = &dwz->line;
21145 }
21146 else
976ca316 21147 section = &per_objfile->per_bfd->line;
36586728
TT
21148
21149 return section;
21150}
21151
debd256d 21152/* Read the statement program header starting at OFFSET in
3019eac3 21153 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 21154 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
21155 Returns NULL if there is a problem reading the header, e.g., if it
21156 has a version we don't understand.
debd256d
JB
21157
21158 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
21159 the returned object point into the dwarf line section buffer,
21160 and must not be freed. */
ae2de4f8 21161
fff8551c 21162static line_header_up
9c541725 21163dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 21164{
3019eac3 21165 struct dwarf2_section_info *section;
976ca316 21166 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 21167
36586728 21168 section = get_debug_line_section (cu);
976ca316 21169 section->read (per_objfile->objfile);
3019eac3 21170 if (section->buffer == NULL)
debd256d 21171 {
3019eac3 21172 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 21173 complaint (_("missing .debug_line.dwo section"));
3019eac3 21174 else
b98664d3 21175 complaint (_("missing .debug_line section"));
debd256d
JB
21176 return 0;
21177 }
21178
0df7ad3a 21179 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 21180 per_objfile, section, &cu->header);
debd256d 21181}
c906108c 21182
c6da4cef 21183/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 21184 Return the file name of the psymtab for the given file_entry.
c6da4cef 21185 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
21186 If space for the result is malloc'd, *NAME_HOLDER will be set.
21187 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 21188
d521ce57 21189static const char *
7ba99d21 21190psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 21191 const dwarf2_psymtab *pst,
c89b44cd
TT
21192 const char *comp_dir,
21193 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 21194{
d521ce57
TT
21195 const char *include_name = fe.name;
21196 const char *include_name_to_compare = include_name;
72b9f47f 21197 const char *pst_filename;
c6da4cef
DE
21198 int file_is_pst;
21199
8c43009f 21200 const char *dir_name = fe.include_dir (lh);
c6da4cef 21201
c89b44cd 21202 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
21203 if (!IS_ABSOLUTE_PATH (include_name)
21204 && (dir_name != NULL || comp_dir != NULL))
21205 {
21206 /* Avoid creating a duplicate psymtab for PST.
21207 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21208 Before we do the comparison, however, we need to account
21209 for DIR_NAME and COMP_DIR.
21210 First prepend dir_name (if non-NULL). If we still don't
21211 have an absolute path prepend comp_dir (if non-NULL).
21212 However, the directory we record in the include-file's
21213 psymtab does not contain COMP_DIR (to match the
21214 corresponding symtab(s)).
21215
21216 Example:
21217
21218 bash$ cd /tmp
21219 bash$ gcc -g ./hello.c
21220 include_name = "hello.c"
21221 dir_name = "."
21222 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
21223 DW_AT_name = "./hello.c"
21224
21225 */
c6da4cef
DE
21226
21227 if (dir_name != NULL)
21228 {
c89b44cd
TT
21229 name_holder->reset (concat (dir_name, SLASH_STRING,
21230 include_name, (char *) NULL));
21231 include_name = name_holder->get ();
c6da4cef 21232 include_name_to_compare = include_name;
c6da4cef
DE
21233 }
21234 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21235 {
c89b44cd
TT
21236 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21237 include_name, (char *) NULL));
21238 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
21239 }
21240 }
21241
21242 pst_filename = pst->filename;
c89b44cd 21243 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
21244 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21245 {
c89b44cd
TT
21246 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21247 pst_filename, (char *) NULL));
21248 pst_filename = copied_name.get ();
c6da4cef
DE
21249 }
21250
1e3fad37 21251 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 21252
c6da4cef
DE
21253 if (file_is_pst)
21254 return NULL;
21255 return include_name;
21256}
21257
d9b3de22
DE
21258/* State machine to track the state of the line number program. */
21259
6f77053d 21260class lnp_state_machine
d9b3de22 21261{
6f77053d
PA
21262public:
21263 /* Initialize a machine state for the start of a line number
21264 program. */
804d2729
TT
21265 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21266 bool record_lines_p);
6f77053d 21267
8c43009f
PA
21268 file_entry *current_file ()
21269 {
21270 /* lh->file_names is 0-based, but the file name numbers in the
21271 statement program are 1-based. */
6f77053d
PA
21272 return m_line_header->file_name_at (m_file);
21273 }
21274
21275 /* Record the line in the state machine. END_SEQUENCE is true if
21276 we're processing the end of a sequence. */
21277 void record_line (bool end_sequence);
21278
a8caed5d 21279 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21280 nop-out rest of the lines in this sequence. */
6f77053d
PA
21281 void check_line_address (struct dwarf2_cu *cu,
21282 const gdb_byte *line_ptr,
7ab6656f 21283 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21284
21285 void handle_set_discriminator (unsigned int discriminator)
21286 {
21287 m_discriminator = discriminator;
21288 m_line_has_non_zero_discriminator |= discriminator != 0;
21289 }
21290
21291 /* Handle DW_LNE_set_address. */
21292 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21293 {
21294 m_op_index = 0;
21295 address += baseaddr;
21296 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21297 }
21298
21299 /* Handle DW_LNS_advance_pc. */
21300 void handle_advance_pc (CORE_ADDR adjust);
21301
21302 /* Handle a special opcode. */
21303 void handle_special_opcode (unsigned char op_code);
21304
21305 /* Handle DW_LNS_advance_line. */
21306 void handle_advance_line (int line_delta)
21307 {
21308 advance_line (line_delta);
21309 }
21310
21311 /* Handle DW_LNS_set_file. */
21312 void handle_set_file (file_name_index file);
21313
21314 /* Handle DW_LNS_negate_stmt. */
21315 void handle_negate_stmt ()
21316 {
21317 m_is_stmt = !m_is_stmt;
21318 }
21319
21320 /* Handle DW_LNS_const_add_pc. */
21321 void handle_const_add_pc ();
21322
21323 /* Handle DW_LNS_fixed_advance_pc. */
21324 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21325 {
21326 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21327 m_op_index = 0;
21328 }
21329
21330 /* Handle DW_LNS_copy. */
21331 void handle_copy ()
21332 {
21333 record_line (false);
21334 m_discriminator = 0;
21335 }
21336
21337 /* Handle DW_LNE_end_sequence. */
21338 void handle_end_sequence ()
21339 {
804d2729 21340 m_currently_recording_lines = true;
6f77053d
PA
21341 }
21342
21343private:
21344 /* Advance the line by LINE_DELTA. */
21345 void advance_line (int line_delta)
21346 {
21347 m_line += line_delta;
21348
21349 if (line_delta != 0)
21350 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21351 }
21352
804d2729
TT
21353 struct dwarf2_cu *m_cu;
21354
6f77053d
PA
21355 gdbarch *m_gdbarch;
21356
21357 /* True if we're recording lines.
21358 Otherwise we're building partial symtabs and are just interested in
21359 finding include files mentioned by the line number program. */
21360 bool m_record_lines_p;
21361
8c43009f 21362 /* The line number header. */
6f77053d 21363 line_header *m_line_header;
8c43009f 21364
6f77053d
PA
21365 /* These are part of the standard DWARF line number state machine,
21366 and initialized according to the DWARF spec. */
d9b3de22 21367
6f77053d 21368 unsigned char m_op_index = 0;
7ba99d21
AT
21369 /* The line table index of the current file. */
21370 file_name_index m_file = 1;
6f77053d
PA
21371 unsigned int m_line = 1;
21372
21373 /* These are initialized in the constructor. */
21374
21375 CORE_ADDR m_address;
21376 bool m_is_stmt;
21377 unsigned int m_discriminator;
d9b3de22
DE
21378
21379 /* Additional bits of state we need to track. */
21380
21381 /* The last file that we called dwarf2_start_subfile for.
21382 This is only used for TLLs. */
6f77053d 21383 unsigned int m_last_file = 0;
d9b3de22 21384 /* The last file a line number was recorded for. */
6f77053d 21385 struct subfile *m_last_subfile = NULL;
d9b3de22 21386
1313c56e
AB
21387 /* The address of the last line entry. */
21388 CORE_ADDR m_last_address;
21389
21390 /* Set to true when a previous line at the same address (using
21391 m_last_address) had m_is_stmt true. This is reset to false when a
21392 line entry at a new address (m_address different to m_last_address) is
21393 processed. */
21394 bool m_stmt_at_address = false;
21395
804d2729
TT
21396 /* When true, record the lines we decode. */
21397 bool m_currently_recording_lines = false;
d9b3de22
DE
21398
21399 /* The last line number that was recorded, used to coalesce
21400 consecutive entries for the same line. This can happen, for
21401 example, when discriminators are present. PR 17276. */
6f77053d
PA
21402 unsigned int m_last_line = 0;
21403 bool m_line_has_non_zero_discriminator = false;
8c43009f 21404};
d9b3de22 21405
6f77053d
PA
21406void
21407lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21408{
21409 CORE_ADDR addr_adj = (((m_op_index + adjust)
21410 / m_line_header->maximum_ops_per_instruction)
21411 * m_line_header->minimum_instruction_length);
21412 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21413 m_op_index = ((m_op_index + adjust)
21414 % m_line_header->maximum_ops_per_instruction);
21415}
d9b3de22 21416
6f77053d
PA
21417void
21418lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21419{
6f77053d 21420 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21421 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21422 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21423 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21424 / m_line_header->maximum_ops_per_instruction)
21425 * m_line_header->minimum_instruction_length);
21426 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21427 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21428 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21429
258bf0ee 21430 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21431 advance_line (line_delta);
21432 record_line (false);
21433 m_discriminator = 0;
21434}
d9b3de22 21435
6f77053d
PA
21436void
21437lnp_state_machine::handle_set_file (file_name_index file)
21438{
21439 m_file = file;
21440
21441 const file_entry *fe = current_file ();
21442 if (fe == NULL)
21443 dwarf2_debug_line_missing_file_complaint ();
21444 else if (m_record_lines_p)
21445 {
21446 const char *dir = fe->include_dir (m_line_header);
21447
c24bdb02 21448 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21449 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21450 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21451 }
21452}
21453
21454void
21455lnp_state_machine::handle_const_add_pc ()
21456{
21457 CORE_ADDR adjust
21458 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21459
21460 CORE_ADDR addr_adj
21461 = (((m_op_index + adjust)
21462 / m_line_header->maximum_ops_per_instruction)
21463 * m_line_header->minimum_instruction_length);
21464
21465 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21466 m_op_index = ((m_op_index + adjust)
21467 % m_line_header->maximum_ops_per_instruction);
21468}
d9b3de22 21469
a05a36a5
DE
21470/* Return non-zero if we should add LINE to the line number table.
21471 LINE is the line to add, LAST_LINE is the last line that was added,
21472 LAST_SUBFILE is the subfile for LAST_LINE.
21473 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21474 had a non-zero discriminator.
21475
21476 We have to be careful in the presence of discriminators.
21477 E.g., for this line:
21478
21479 for (i = 0; i < 100000; i++);
21480
21481 clang can emit four line number entries for that one line,
21482 each with a different discriminator.
21483 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21484
21485 However, we want gdb to coalesce all four entries into one.
21486 Otherwise the user could stepi into the middle of the line and
21487 gdb would get confused about whether the pc really was in the
21488 middle of the line.
21489
21490 Things are further complicated by the fact that two consecutive
21491 line number entries for the same line is a heuristic used by gcc
21492 to denote the end of the prologue. So we can't just discard duplicate
21493 entries, we have to be selective about it. The heuristic we use is
21494 that we only collapse consecutive entries for the same line if at least
21495 one of those entries has a non-zero discriminator. PR 17276.
21496
21497 Note: Addresses in the line number state machine can never go backwards
21498 within one sequence, thus this coalescing is ok. */
21499
21500static int
804d2729
TT
21501dwarf_record_line_p (struct dwarf2_cu *cu,
21502 unsigned int line, unsigned int last_line,
a05a36a5
DE
21503 int line_has_non_zero_discriminator,
21504 struct subfile *last_subfile)
21505{
c24bdb02 21506 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21507 return 1;
21508 if (line != last_line)
21509 return 1;
21510 /* Same line for the same file that we've seen already.
21511 As a last check, for pr 17276, only record the line if the line
21512 has never had a non-zero discriminator. */
21513 if (!line_has_non_zero_discriminator)
21514 return 1;
21515 return 0;
21516}
21517
804d2729
TT
21518/* Use the CU's builder to record line number LINE beginning at
21519 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21520
21521static void
d9b3de22 21522dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21523 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21524 struct dwarf2_cu *cu)
252a6764
DE
21525{
21526 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21527
27e0867f
DE
21528 if (dwarf_line_debug)
21529 {
21530 fprintf_unfiltered (gdb_stdlog,
21531 "Recording line %u, file %s, address %s\n",
21532 line, lbasename (subfile->name),
21533 paddress (gdbarch, address));
21534 }
21535
804d2729 21536 if (cu != nullptr)
8c95582d 21537 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21538}
21539
21540/* Subroutine of dwarf_decode_lines_1 to simplify it.
21541 Mark the end of a set of line number records.
d9b3de22 21542 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21543 If SUBFILE is NULL the request is ignored. */
21544
21545static void
21546dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21547 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21548{
27e0867f
DE
21549 if (subfile == NULL)
21550 return;
21551
21552 if (dwarf_line_debug)
21553 {
21554 fprintf_unfiltered (gdb_stdlog,
21555 "Finishing current line, file %s, address %s\n",
21556 lbasename (subfile->name),
21557 paddress (gdbarch, address));
21558 }
21559
8c95582d 21560 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21561}
21562
6f77053d
PA
21563void
21564lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21565{
d9b3de22
DE
21566 if (dwarf_line_debug)
21567 {
21568 fprintf_unfiltered (gdb_stdlog,
21569 "Processing actual line %u: file %u,"
94a72be7 21570 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21571 m_line, m_file,
6f77053d 21572 paddress (m_gdbarch, m_address),
94a72be7
AB
21573 m_is_stmt, m_discriminator,
21574 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21575 }
21576
6f77053d 21577 file_entry *fe = current_file ();
8c43009f
PA
21578
21579 if (fe == NULL)
d9b3de22
DE
21580 dwarf2_debug_line_missing_file_complaint ();
21581 /* For now we ignore lines not starting on an instruction boundary.
21582 But not when processing end_sequence for compatibility with the
21583 previous version of the code. */
6f77053d 21584 else if (m_op_index == 0 || end_sequence)
d9b3de22 21585 {
8c43009f 21586 fe->included_p = 1;
8c95582d 21587 if (m_record_lines_p)
d9b3de22 21588 {
1313c56e
AB
21589 /* When we switch files we insert an end maker in the first file,
21590 switch to the second file and add a new line entry. The
21591 problem is that the end marker inserted in the first file will
21592 discard any previous line entries at the same address. If the
21593 line entries in the first file are marked as is-stmt, while
21594 the new line in the second file is non-stmt, then this means
21595 the end marker will discard is-stmt lines so we can have a
21596 non-stmt line. This means that there are less addresses at
21597 which the user can insert a breakpoint.
21598
21599 To improve this we track the last address in m_last_address,
21600 and whether we have seen an is-stmt at this address. Then
21601 when switching files, if we have seen a stmt at the current
21602 address, and we are switching to create a non-stmt line, then
21603 discard the new line. */
21604 bool file_changed
21605 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21606 bool ignore_this_line
dda83cd7
SM
21607 = ((file_changed && !end_sequence && m_last_address == m_address
21608 && !m_is_stmt && m_stmt_at_address)
21609 || (!end_sequence && m_line == 0));
1313c56e
AB
21610
21611 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21612 {
804d2729
TT
21613 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21614 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21615 }
21616
1313c56e 21617 if (!end_sequence && !ignore_this_line)
d9b3de22 21618 {
8c95582d
AB
21619 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21620
804d2729 21621 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21622 m_line_has_non_zero_discriminator,
21623 m_last_subfile))
d9b3de22 21624 {
c24bdb02 21625 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21626 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21627 builder->get_current_subfile (),
8c95582d 21628 m_line, m_address, is_stmt,
804d2729 21629 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21630 }
c24bdb02 21631 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21632 m_last_line = m_line;
d9b3de22
DE
21633 }
21634 }
21635 }
1313c56e
AB
21636
21637 /* Track whether we have seen any m_is_stmt true at m_address in case we
21638 have multiple line table entries all at m_address. */
21639 if (m_last_address != m_address)
21640 {
21641 m_stmt_at_address = false;
21642 m_last_address = m_address;
21643 }
21644 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21645}
21646
804d2729
TT
21647lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21648 line_header *lh, bool record_lines_p)
d9b3de22 21649{
804d2729 21650 m_cu = cu;
6f77053d
PA
21651 m_gdbarch = arch;
21652 m_record_lines_p = record_lines_p;
21653 m_line_header = lh;
d9b3de22 21654
804d2729 21655 m_currently_recording_lines = true;
d9b3de22 21656
d9b3de22
DE
21657 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21658 was a line entry for it so that the backend has a chance to adjust it
21659 and also record it in case it needs it. This is currently used by MIPS
21660 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21661 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21662 m_is_stmt = lh->default_is_stmt;
21663 m_discriminator = 0;
1313c56e
AB
21664
21665 m_last_address = m_address;
21666 m_stmt_at_address = false;
252a6764
DE
21667}
21668
6f77053d
PA
21669void
21670lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21671 const gdb_byte *line_ptr,
7ab6656f 21672 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21673{
a8caed5d
FS
21674 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21675 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21676 located at 0x0. In this case, additionally check that if
21677 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21678
a8caed5d
FS
21679 if ((address == 0 && address < unrelocated_lowpc)
21680 || address == (CORE_ADDR) -1)
924c2928
DE
21681 {
21682 /* This line table is for a function which has been
21683 GCd by the linker. Ignore it. PR gdb/12528 */
21684
5e22e966 21685 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21686 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21687
b98664d3 21688 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21689 line_offset, objfile_name (objfile));
804d2729
TT
21690 m_currently_recording_lines = false;
21691 /* Note: m_currently_recording_lines is left as false until we see
21692 DW_LNE_end_sequence. */
924c2928
DE
21693 }
21694}
21695
f3f5162e 21696/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21697 Process the line number information in LH.
21698 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21699 program in order to set included_p for every referenced header. */
debd256d 21700
c906108c 21701static void
43f3e411
DE
21702dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21703 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21704{
d521ce57
TT
21705 const gdb_byte *line_ptr, *extended_end;
21706 const gdb_byte *line_end;
a8c50c1f 21707 unsigned int bytes_read, extended_len;
699ca60a 21708 unsigned char op_code, extended_op;
e142c38c 21709 CORE_ADDR baseaddr;
5e22e966 21710 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21711 bfd *abfd = objfile->obfd;
08feed99 21712 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21713 /* True if we're recording line info (as opposed to building partial
21714 symtabs and just interested in finding include files mentioned by
21715 the line number program). */
21716 bool record_lines_p = !decode_for_pst_p;
e142c38c 21717
b3b3bada 21718 baseaddr = objfile->text_section_offset ();
c906108c 21719
debd256d
JB
21720 line_ptr = lh->statement_program_start;
21721 line_end = lh->statement_program_end;
c906108c
SS
21722
21723 /* Read the statement sequences until there's nothing left. */
21724 while (line_ptr < line_end)
21725 {
6f77053d
PA
21726 /* The DWARF line number program state machine. Reset the state
21727 machine at the start of each sequence. */
804d2729 21728 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21729 bool end_sequence = false;
d9b3de22 21730
8c43009f 21731 if (record_lines_p)
c906108c 21732 {
8c43009f
PA
21733 /* Start a subfile for the current file of the state
21734 machine. */
21735 const file_entry *fe = state_machine.current_file ();
21736
21737 if (fe != NULL)
804d2729 21738 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21739 }
21740
a738430d 21741 /* Decode the table. */
d9b3de22 21742 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21743 {
21744 op_code = read_1_byte (abfd, line_ptr);
21745 line_ptr += 1;
9aa1fe7e 21746
debd256d 21747 if (op_code >= lh->opcode_base)
6e70227d 21748 {
8e07a239 21749 /* Special opcode. */
6f77053d 21750 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21751 }
21752 else switch (op_code)
c906108c
SS
21753 {
21754 case DW_LNS_extended_op:
3e43a32a
MS
21755 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21756 &bytes_read);
473b7be6 21757 line_ptr += bytes_read;
a8c50c1f 21758 extended_end = line_ptr + extended_len;
c906108c
SS
21759 extended_op = read_1_byte (abfd, line_ptr);
21760 line_ptr += 1;
8f34b746
TV
21761 if (DW_LNE_lo_user <= extended_op
21762 && extended_op <= DW_LNE_hi_user)
21763 {
21764 /* Vendor extension, ignore. */
21765 line_ptr = extended_end;
21766 break;
21767 }
c906108c
SS
21768 switch (extended_op)
21769 {
21770 case DW_LNE_end_sequence:
6f77053d
PA
21771 state_machine.handle_end_sequence ();
21772 end_sequence = true;
c906108c
SS
21773 break;
21774 case DW_LNE_set_address:
d9b3de22
DE
21775 {
21776 CORE_ADDR address
c8a7a66f 21777 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21778 line_ptr += bytes_read;
6f77053d
PA
21779
21780 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21781 lowpc - baseaddr, address);
6f77053d 21782 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21783 }
c906108c
SS
21784 break;
21785 case DW_LNE_define_file:
dda83cd7
SM
21786 {
21787 const char *cur_file;
ecfb656c
PA
21788 unsigned int mod_time, length;
21789 dir_index dindex;
6e70227d 21790
dda83cd7 21791 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21792 &bytes_read);
dda83cd7
SM
21793 line_ptr += bytes_read;
21794 dindex = (dir_index)
21795 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21796 line_ptr += bytes_read;
21797 mod_time =
21798 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21799 line_ptr += bytes_read;
21800 length =
21801 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21802 line_ptr += bytes_read;
21803 lh->add_file_name (cur_file, dindex, mod_time, length);
21804 }
c906108c 21805 break;
d0c6ba3d 21806 case DW_LNE_set_discriminator:
6f77053d
PA
21807 {
21808 /* The discriminator is not interesting to the
21809 debugger; just ignore it. We still need to
21810 check its value though:
21811 if there are consecutive entries for the same
21812 (non-prologue) line we want to coalesce them.
21813 PR 17276. */
21814 unsigned int discr
21815 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21816 line_ptr += bytes_read;
21817
21818 state_machine.handle_set_discriminator (discr);
21819 }
d0c6ba3d 21820 break;
c906108c 21821 default:
b98664d3 21822 complaint (_("mangled .debug_line section"));
debd256d 21823 return;
c906108c 21824 }
a8c50c1f
DJ
21825 /* Make sure that we parsed the extended op correctly. If e.g.
21826 we expected a different address size than the producer used,
21827 we may have read the wrong number of bytes. */
21828 if (line_ptr != extended_end)
21829 {
b98664d3 21830 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21831 return;
21832 }
c906108c
SS
21833 break;
21834 case DW_LNS_copy:
6f77053d 21835 state_machine.handle_copy ();
c906108c
SS
21836 break;
21837 case DW_LNS_advance_pc:
2dc7f7b3
TT
21838 {
21839 CORE_ADDR adjust
21840 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21841 line_ptr += bytes_read;
6f77053d
PA
21842
21843 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21844 }
c906108c
SS
21845 break;
21846 case DW_LNS_advance_line:
a05a36a5
DE
21847 {
21848 int line_delta
21849 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21850 line_ptr += bytes_read;
6f77053d
PA
21851
21852 state_machine.handle_advance_line (line_delta);
a05a36a5 21853 }
c906108c
SS
21854 break;
21855 case DW_LNS_set_file:
d9b3de22 21856 {
6f77053d 21857 file_name_index file
ecfb656c
PA
21858 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21859 &bytes_read);
d9b3de22 21860 line_ptr += bytes_read;
8c43009f 21861
6f77053d 21862 state_machine.handle_set_file (file);
d9b3de22 21863 }
c906108c
SS
21864 break;
21865 case DW_LNS_set_column:
0ad93d4f 21866 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21867 line_ptr += bytes_read;
21868 break;
21869 case DW_LNS_negate_stmt:
6f77053d 21870 state_machine.handle_negate_stmt ();
c906108c
SS
21871 break;
21872 case DW_LNS_set_basic_block:
c906108c 21873 break;
c2c6d25f
JM
21874 /* Add to the address register of the state machine the
21875 address increment value corresponding to special opcode
a738430d
MK
21876 255. I.e., this value is scaled by the minimum
21877 instruction length since special opcode 255 would have
b021a221 21878 scaled the increment. */
c906108c 21879 case DW_LNS_const_add_pc:
6f77053d 21880 state_machine.handle_const_add_pc ();
c906108c
SS
21881 break;
21882 case DW_LNS_fixed_advance_pc:
3e29f34a 21883 {
6f77053d 21884 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21885 line_ptr += 2;
6f77053d
PA
21886
21887 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21888 }
c906108c 21889 break;
9aa1fe7e 21890 default:
a738430d
MK
21891 {
21892 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21893 int i;
a738430d 21894
debd256d 21895 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21896 {
21897 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21898 line_ptr += bytes_read;
21899 }
21900 }
c906108c
SS
21901 }
21902 }
d9b3de22
DE
21903
21904 if (!end_sequence)
21905 dwarf2_debug_line_missing_end_sequence_complaint ();
21906
21907 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21908 in which case we still finish recording the last line). */
6f77053d 21909 state_machine.record_line (true);
c906108c 21910 }
f3f5162e
DE
21911}
21912
21913/* Decode the Line Number Program (LNP) for the given line_header
21914 structure and CU. The actual information extracted and the type
21915 of structures created from the LNP depends on the value of PST.
21916
21917 1. If PST is NULL, then this procedure uses the data from the program
21918 to create all necessary symbol tables, and their linetables.
21919
21920 2. If PST is not NULL, this procedure reads the program to determine
21921 the list of files included by the unit represented by PST, and
21922 builds all the associated partial symbol tables.
21923
21924 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21925 It is used for relative paths in the line table.
21926 NOTE: When processing partial symtabs (pst != NULL),
21927 comp_dir == pst->dirname.
21928
21929 NOTE: It is important that psymtabs have the same file name (via strcmp)
21930 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21931 symtab we don't use it in the name of the psymtabs we create.
21932 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21933 A good testcase for this is mb-inline.exp.
21934
527f3840
JK
21935 LOWPC is the lowest address in CU (or 0 if not known).
21936
21937 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21938 for its PC<->lines mapping information. Otherwise only the filename
21939 table is read in. */
f3f5162e
DE
21940
21941static void
21942dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21943 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21944 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21945{
5e22e966 21946 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21947 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21948
527f3840
JK
21949 if (decode_mapping)
21950 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21951
21952 if (decode_for_pst_p)
21953 {
aaa75496 21954 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21955 create the psymtab of each included file. */
7ba99d21 21956 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21957 if (file_entry.included_p == 1)
21958 {
c89b44cd 21959 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21960 const char *include_name =
7ba99d21
AT
21961 psymtab_include_file_name (lh, file_entry, pst,
21962 comp_dir, &name_holder);
c6da4cef 21963 if (include_name != NULL)
dda83cd7
SM
21964 dwarf2_create_include_psymtab (include_name, pst, objfile);
21965 }
aaa75496 21966 }
cb1df416
DJ
21967 else
21968 {
21969 /* Make sure a symtab is created for every file, even files
21970 which contain only variables (i.e. no code with associated
21971 line numbers). */
c24bdb02
KS
21972 buildsym_compunit *builder = cu->get_builder ();
21973 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21974
7ba99d21 21975 for (auto &fe : lh->file_names ())
cb1df416 21976 {
804d2729 21977 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21978 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21979 {
c24bdb02 21980 builder->get_current_subfile ()->symtab
804d2729 21981 = allocate_symtab (cust,
c24bdb02 21982 builder->get_current_subfile ()->name);
43f3e411 21983 }
c24bdb02 21984 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21985 }
21986 }
c906108c
SS
21987}
21988
21989/* Start a subfile for DWARF. FILENAME is the name of the file and
21990 DIRNAME the name of the source directory which contains FILENAME
4d663531 21991 or NULL if not known.
c906108c
SS
21992 This routine tries to keep line numbers from identical absolute and
21993 relative file names in a common subfile.
21994
21995 Using the `list' example from the GDB testsuite, which resides in
21996 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21997 of /srcdir/list0.c yields the following debugging information for list0.c:
21998
c5aa993b 21999 DW_AT_name: /srcdir/list0.c
4d663531 22000 DW_AT_comp_dir: /compdir
357e46e7 22001 files.files[0].name: list0.h
c5aa993b 22002 files.files[0].dir: /srcdir
357e46e7 22003 files.files[1].name: list0.c
c5aa993b 22004 files.files[1].dir: /srcdir
c906108c
SS
22005
22006 The line number information for list0.c has to end up in a single
4f1520fb
FR
22007 subfile, so that `break /srcdir/list0.c:1' works as expected.
22008 start_subfile will ensure that this happens provided that we pass the
22009 concatenation of files.files[1].dir and files.files[1].name as the
22010 subfile's name. */
c906108c
SS
22011
22012static void
804d2729
TT
22013dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22014 const char *dirname)
c906108c 22015{
43816ebc 22016 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 22017
4d663531 22018 /* In order not to lose the line information directory,
4f1520fb
FR
22019 we concatenate it to the filename when it makes sense.
22020 Note that the Dwarf3 standard says (speaking of filenames in line
22021 information): ``The directory index is ignored for file names
22022 that represent full path names''. Thus ignoring dirname in the
22023 `else' branch below isn't an issue. */
c906108c 22024
d5166ae1 22025 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 22026 {
43816ebc
TT
22027 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22028 filename = copy.get ();
d521ce57 22029 }
c906108c 22030
c24bdb02 22031 cu->get_builder ()->start_subfile (filename);
c906108c
SS
22032}
22033
804d2729
TT
22034/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22035 buildsym_compunit constructor. */
f4dc4d17 22036
c24bdb02
KS
22037struct compunit_symtab *
22038dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22039 CORE_ADDR low_pc)
f4dc4d17 22040{
c24bdb02 22041 gdb_assert (m_builder == nullptr);
43f3e411 22042
c24bdb02 22043 m_builder.reset (new struct buildsym_compunit
f6e649dd 22044 (this->per_objfile->objfile,
c24bdb02 22045 name, comp_dir, language, low_pc));
93b8bea4 22046
c24bdb02 22047 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 22048
c24bdb02
KS
22049 get_builder ()->record_debugformat ("DWARF 2");
22050 get_builder ()->record_producer (producer);
f4dc4d17 22051
c24bdb02 22052 processing_has_namespace_info = false;
43f3e411 22053
c24bdb02 22054 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
22055}
22056
4c2df51b
DJ
22057static void
22058var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 22059 struct dwarf2_cu *cu)
4c2df51b 22060{
5e22e966 22061 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
22062 struct comp_unit_head *cu_header = &cu->header;
22063
4c2df51b
DJ
22064 /* NOTE drow/2003-01-30: There used to be a comment and some special
22065 code here to turn a symbol with DW_AT_external and a
22066 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22067 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22068 with some versions of binutils) where shared libraries could have
22069 relocations against symbols in their debug information - the
22070 minimal symbol would have the right address, but the debug info
22071 would not. It's no longer necessary, because we will explicitly
22072 apply relocations when we read in the debug information now. */
22073
22074 /* A DW_AT_location attribute with no contents indicates that a
22075 variable has been optimized away. */
9d2246fc 22076 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 22077 {
f1e6e072 22078 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
22079 return;
22080 }
22081
22082 /* Handle one degenerate form of location expression specially, to
22083 preserve GDB's previous behavior when section offsets are
336d760d
AT
22084 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22085 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 22086
9d2246fc
TT
22087 if (attr->form_is_block ())
22088 {
22089 struct dwarf_block *block = attr->as_block ();
22090
22091 if ((block->data[0] == DW_OP_addr
22092 && block->size == 1 + cu_header->addr_size)
22093 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 22094 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
22095 && (block->size
22096 == 1 + leb128_size (&block->data[1]))))
22097 {
22098 unsigned int dummy;
22099
22100 if (block->data[0] == DW_OP_addr)
22101 SET_SYMBOL_VALUE_ADDRESS
22102 (sym, cu->header.read_address (objfile->obfd,
22103 block->data + 1,
38583298 22104 &dummy));
9d2246fc
TT
22105 else
22106 SET_SYMBOL_VALUE_ADDRESS
22107 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22108 &dummy));
22109 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22110 fixup_symbol_section (sym, objfile);
22111 SET_SYMBOL_VALUE_ADDRESS
22112 (sym,
22113 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 22114 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
22115 return;
22116 }
4c2df51b
DJ
22117 }
22118
22119 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22120 expression evaluator, and use LOC_COMPUTED only when necessary
22121 (i.e. when the value of a register or memory location is
22122 referenced, or a thread-local block, etc.). Then again, it might
22123 not be worthwhile. I'm assuming that it isn't unless performance
22124 or memory numbers show me otherwise. */
22125
f1e6e072 22126 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 22127
f1e6e072 22128 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 22129 cu->has_loclist = true;
4c2df51b
DJ
22130}
22131
c906108c
SS
22132/* Given a pointer to a DWARF information entry, figure out if we need
22133 to make a symbol table entry for it, and if so, create a new entry
22134 and return a pointer to it.
22135 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
22136 used the passed type.
22137 If SPACE is not NULL, use it to hold the new symbol. If it is
22138 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
22139
22140static struct symbol *
5e2db402
TT
22141new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22142 struct symbol *space)
c906108c 22143{
976ca316
SM
22144 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22145 struct objfile *objfile = per_objfile->objfile;
08feed99 22146 struct gdbarch *gdbarch = objfile->arch ();
c906108c 22147 struct symbol *sym = NULL;
15d034d0 22148 const char *name;
c906108c
SS
22149 struct attribute *attr = NULL;
22150 struct attribute *attr2 = NULL;
e142c38c 22151 CORE_ADDR baseaddr;
e37fd15a
SW
22152 struct pending **list_to_add = NULL;
22153
edb3359d 22154 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 22155
b3b3bada 22156 baseaddr = objfile->text_section_offset ();
c906108c 22157
94af9270 22158 name = dwarf2_name (die, cu);
c906108c
SS
22159 if (name)
22160 {
34eaf542 22161 int suppress_add = 0;
94af9270 22162
34eaf542
TT
22163 if (space)
22164 sym = space;
22165 else
8c14c3a3 22166 sym = new (&objfile->objfile_obstack) symbol;
c906108c 22167 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
22168
22169 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 22170 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
22171 /* Fortran does not have mangling standard and the mangling does differ
22172 between gfortran, iFort etc. */
bcfe6157
TT
22173 const char *physname
22174 = (cu->language == language_fortran
22175 ? dwarf2_full_name (name, die, cu)
22176 : dwarf2_physname (name, die, cu));
22177 const char *linkagename = dw2_linkage_name (die, cu);
22178
22179 if (linkagename == nullptr || cu->language == language_ada)
22180 sym->set_linkage_name (physname);
22181 else
22182 {
22183 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22184 sym->set_linkage_name (linkagename);
22185 }
f55ee35c 22186
c906108c 22187 /* Default assumptions.
dda83cd7 22188 Use the passed type or decode it from the die. */
176620f1 22189 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 22190 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
22191 if (type != NULL)
22192 SYMBOL_TYPE (sym) = type;
22193 else
e7c27a73 22194 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
22195 attr = dwarf2_attr (die,
22196 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22197 cu);
435d3d88 22198 if (attr != nullptr)
529908cb 22199 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 22200
edb3359d
DJ
22201 attr = dwarf2_attr (die,
22202 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22203 cu);
aa659cfa 22204 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 22205 {
529908cb 22206 file_name_index file_index
aa659cfa 22207 = (file_name_index) attr->as_nonnegative ();
8c43009f 22208 struct file_entry *fe;
9a619af0 22209
ecfb656c
PA
22210 if (cu->line_header != NULL)
22211 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
22212 else
22213 fe = NULL;
22214
22215 if (fe == NULL)
b98664d3 22216 complaint (_("file index out of range"));
8c43009f
PA
22217 else
22218 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
22219 }
22220
c906108c
SS
22221 switch (die->tag)
22222 {
22223 case DW_TAG_label:
e142c38c 22224 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 22225 if (attr != nullptr)
3e29f34a
MR
22226 {
22227 CORE_ADDR addr;
22228
95f982e5 22229 addr = attr->as_address ();
3e29f34a 22230 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 22231 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 22232 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 22233 }
8f5c6526
TV
22234 else
22235 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
22236 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22237 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 22238 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
22239 break;
22240 case DW_TAG_subprogram:
22241 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22242 finish_block. */
f1e6e072 22243 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 22244 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 22245 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
22246 || cu->language == language_ada
22247 || cu->language == language_fortran)
c906108c 22248 {
dda83cd7
SM
22249 /* Subprograms marked external are stored as a global symbol.
22250 Ada and Fortran subprograms, whether marked external or
22251 not, are always stored as a global symbol, because we want
22252 to be able to access them globally. For instance, we want
22253 to be able to break on a nested subprogram without having
22254 to specify the context. */
c24bdb02 22255 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
22256 }
22257 else
22258 {
e37fd15a 22259 list_to_add = cu->list_in_scope;
c906108c
SS
22260 }
22261 break;
edb3359d
DJ
22262 case DW_TAG_inlined_subroutine:
22263 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22264 finish_block. */
f1e6e072 22265 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22266 SYMBOL_INLINED (sym) = 1;
481860b3 22267 list_to_add = cu->list_in_scope;
edb3359d 22268 break;
34eaf542
TT
22269 case DW_TAG_template_value_param:
22270 suppress_add = 1;
22271 /* Fall through. */
72929c62 22272 case DW_TAG_constant:
c906108c 22273 case DW_TAG_variable:
254e6b9e 22274 case DW_TAG_member:
0963b4bd
MS
22275 /* Compilation with minimal debug info may result in
22276 variables with missing type entries. Change the
22277 misleading `void' type to something sensible. */
78134374 22278 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22279 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22280
e142c38c 22281 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22282 /* In the case of DW_TAG_member, we should only be called for
22283 static const members. */
22284 if (die->tag == DW_TAG_member)
22285 {
3863f96c
DE
22286 /* dwarf2_add_field uses die_is_declaration,
22287 so we do the same. */
254e6b9e
DE
22288 gdb_assert (die_is_declaration (die, cu));
22289 gdb_assert (attr);
22290 }
435d3d88 22291 if (attr != nullptr)
c906108c 22292 {
e7c27a73 22293 dwarf2_const_value (attr, sym, cu);
e142c38c 22294 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22295 if (!suppress_add)
34eaf542 22296 {
c45bc3f8 22297 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22298 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22299 else
e37fd15a 22300 list_to_add = cu->list_in_scope;
34eaf542 22301 }
c906108c
SS
22302 break;
22303 }
e142c38c 22304 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22305 if (attr != nullptr)
c906108c 22306 {
e7c27a73 22307 var_decode_location (attr, sym, cu);
e142c38c 22308 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22309
22310 /* Fortran explicitly imports any global symbols to the local
22311 scope by DW_TAG_common_block. */
22312 if (cu->language == language_fortran && die->parent
22313 && die->parent->tag == DW_TAG_common_block)
22314 attr2 = NULL;
22315
caac4577
JG
22316 if (SYMBOL_CLASS (sym) == LOC_STATIC
22317 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22318 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22319 {
22320 /* When a static variable is eliminated by the linker,
22321 the corresponding debug information is not stripped
22322 out, but the variable address is set to null;
22323 do not add such variables into symbol table. */
22324 }
c45bc3f8 22325 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22326 {
4b610737
TT
22327 if (SYMBOL_CLASS (sym) == LOC_STATIC
22328 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22329 && per_objfile->per_bfd->can_copy)
4b610737
TT
22330 {
22331 /* A global static variable might be subject to
22332 copy relocation. We first check for a local
22333 minsym, though, because maybe the symbol was
22334 marked hidden, in which case this would not
22335 apply. */
22336 bound_minimal_symbol found
22337 = (lookup_minimal_symbol_linkage
987012b8 22338 (sym->linkage_name (), objfile));
4b610737
TT
22339 if (found.minsym != nullptr)
22340 sym->maybe_copied = 1;
22341 }
f55ee35c 22342
1c809c68
TT
22343 /* A variable with DW_AT_external is never static,
22344 but it may be block-scoped. */
804d2729 22345 list_to_add
c24bdb02
KS
22346 = ((cu->list_in_scope
22347 == cu->get_builder ()->get_file_symbols ())
22348 ? cu->get_builder ()->get_global_symbols ()
804d2729 22349 : cu->list_in_scope);
1c809c68 22350 }
c906108c 22351 else
e37fd15a 22352 list_to_add = cu->list_in_scope;
c906108c
SS
22353 }
22354 else
22355 {
22356 /* We do not know the address of this symbol.
dda83cd7
SM
22357 If it is an external symbol and we have type information
22358 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22359 The address of the variable will then be determined from
22360 the minimal symbol table whenever the variable is
22361 referenced. */
e142c38c 22362 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22363
22364 /* Fortran explicitly imports any global symbols to the local
22365 scope by DW_TAG_common_block. */
22366 if (cu->language == language_fortran && die->parent
22367 && die->parent->tag == DW_TAG_common_block)
22368 {
22369 /* SYMBOL_CLASS doesn't matter here because
22370 read_common_block is going to reset it. */
22371 if (!suppress_add)
22372 list_to_add = cu->list_in_scope;
22373 }
c45bc3f8 22374 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22375 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22376 {
0fe7935b
DJ
22377 /* A variable with DW_AT_external is never static, but it
22378 may be block-scoped. */
804d2729 22379 list_to_add
c24bdb02
KS
22380 = ((cu->list_in_scope
22381 == cu->get_builder ()->get_file_symbols ())
22382 ? cu->get_builder ()->get_global_symbols ()
804d2729 22383 : cu->list_in_scope);
0fe7935b 22384
f1e6e072 22385 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22386 }
442ddf59
JK
22387 else if (!die_is_declaration (die, cu))
22388 {
22389 /* Use the default LOC_OPTIMIZED_OUT class. */
22390 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22391 if (!suppress_add)
22392 list_to_add = cu->list_in_scope;
442ddf59 22393 }
c906108c
SS
22394 }
22395 break;
22396 case DW_TAG_formal_parameter:
a60f3166
TT
22397 {
22398 /* If we are inside a function, mark this as an argument. If
22399 not, we might be looking at an argument to an inlined function
22400 when we do not have enough information to show inlined frames;
22401 pretend it's a local variable in that case so that the user can
22402 still see it. */
804d2729 22403 struct context_stack *curr
c24bdb02 22404 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22405 if (curr != nullptr && curr->name != nullptr)
22406 SYMBOL_IS_ARGUMENT (sym) = 1;
22407 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22408 if (attr != nullptr)
a60f3166
TT
22409 {
22410 var_decode_location (attr, sym, cu);
22411 }
22412 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22413 if (attr != nullptr)
a60f3166
TT
22414 {
22415 dwarf2_const_value (attr, sym, cu);
22416 }
f346a30d 22417
a60f3166
TT
22418 list_to_add = cu->list_in_scope;
22419 }
c906108c
SS
22420 break;
22421 case DW_TAG_unspecified_parameters:
22422 /* From varargs functions; gdb doesn't seem to have any
22423 interest in this information, so just ignore it for now.
22424 (FIXME?) */
22425 break;
34eaf542
TT
22426 case DW_TAG_template_type_param:
22427 suppress_add = 1;
22428 /* Fall through. */
c906108c 22429 case DW_TAG_class_type:
680b30c7 22430 case DW_TAG_interface_type:
c906108c
SS
22431 case DW_TAG_structure_type:
22432 case DW_TAG_union_type:
72019c9c 22433 case DW_TAG_set_type:
c906108c 22434 case DW_TAG_enumeration_type:
f1e6e072 22435 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22436 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22437
63d06c5c 22438 {
9c37b5ae 22439 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22440 really ever be static objects: otherwise, if you try
22441 to, say, break of a class's method and you're in a file
22442 which doesn't mention that class, it won't work unless
22443 the check for all static symbols in lookup_symbol_aux
22444 saves you. See the OtherFileClass tests in
22445 gdb.c++/namespace.exp. */
22446
e37fd15a 22447 if (!suppress_add)
34eaf542 22448 {
c24bdb02 22449 buildsym_compunit *builder = cu->get_builder ();
804d2729 22450 list_to_add
c24bdb02 22451 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22452 && cu->language == language_cplus
c24bdb02 22453 ? builder->get_global_symbols ()
804d2729 22454 : cu->list_in_scope);
63d06c5c 22455
64382290 22456 /* The semantics of C++ state that "struct foo {
9c37b5ae 22457 ... }" also defines a typedef for "foo". */
64382290 22458 if (cu->language == language_cplus
45280282 22459 || cu->language == language_ada
c44af4eb
TT
22460 || cu->language == language_d
22461 || cu->language == language_rust)
64382290
TT
22462 {
22463 /* The symbol's name is already allocated along
22464 with this objfile, so we don't need to
22465 duplicate it for the type. */
7d93a1e0 22466 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22467 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22468 }
63d06c5c
DC
22469 }
22470 }
c906108c
SS
22471 break;
22472 case DW_TAG_typedef:
f1e6e072 22473 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22474 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22475 list_to_add = cu->list_in_scope;
63d06c5c 22476 break;
d8f62e84 22477 case DW_TAG_array_type:
c906108c 22478 case DW_TAG_base_type:
dda83cd7 22479 case DW_TAG_subrange_type:
f1e6e072 22480 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22481 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22482 list_to_add = cu->list_in_scope;
c906108c
SS
22483 break;
22484 case DW_TAG_enumerator:
e142c38c 22485 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22486 if (attr != nullptr)
c906108c 22487 {
e7c27a73 22488 dwarf2_const_value (attr, sym, cu);
c906108c 22489 }
63d06c5c
DC
22490 {
22491 /* NOTE: carlton/2003-11-10: See comment above in the
22492 DW_TAG_class_type, etc. block. */
22493
804d2729 22494 list_to_add
c24bdb02 22495 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22496 && cu->language == language_cplus
c24bdb02 22497 ? cu->get_builder ()->get_global_symbols ()
804d2729 22498 : cu->list_in_scope);
63d06c5c 22499 }
c906108c 22500 break;
74921315 22501 case DW_TAG_imported_declaration:
5c4e30ca 22502 case DW_TAG_namespace:
f1e6e072 22503 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22504 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22505 break;
530e8392
KB
22506 case DW_TAG_module:
22507 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22508 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22509 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22510 break;
4357ac6c 22511 case DW_TAG_common_block:
f1e6e072 22512 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22513 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22514 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22515 break;
c906108c
SS
22516 default:
22517 /* Not a tag we recognize. Hopefully we aren't processing
22518 trash data, but since we must specifically ignore things
22519 we don't recognize, there is nothing else we should do at
0963b4bd 22520 this point. */
b98664d3 22521 complaint (_("unsupported tag: '%s'"),
4d3c2250 22522 dwarf_tag_name (die->tag));
c906108c
SS
22523 break;
22524 }
df8a16a1 22525
e37fd15a
SW
22526 if (suppress_add)
22527 {
22528 sym->hash_next = objfile->template_symbols;
22529 objfile->template_symbols = sym;
22530 list_to_add = NULL;
22531 }
22532
22533 if (list_to_add != NULL)
d3cb6808 22534 add_symbol_to_list (sym, list_to_add);
e37fd15a 22535
df8a16a1
DJ
22536 /* For the benefit of old versions of GCC, check for anonymous
22537 namespaces based on the demangled name. */
4d4ec4e5 22538 if (!cu->processing_has_namespace_info
94af9270 22539 && cu->language == language_cplus)
c24bdb02 22540 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22541 }
22542 return (sym);
22543}
22544
98bfdba5
PA
22545/* Given an attr with a DW_FORM_dataN value in host byte order,
22546 zero-extend it as appropriate for the symbol's type. The DWARF
22547 standard (v4) is not entirely clear about the meaning of using
22548 DW_FORM_dataN for a constant with a signed type, where the type is
22549 wider than the data. The conclusion of a discussion on the DWARF
22550 list was that this is unspecified. We choose to always zero-extend
22551 because that is the interpretation long in use by GCC. */
c906108c 22552
98bfdba5 22553static gdb_byte *
ff39bb5e 22554dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22555 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22556{
5e22e966 22557 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22558 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22559 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22560 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22561
22562 if (bits < sizeof (*value) * 8)
22563 {
22564 l &= ((LONGEST) 1 << bits) - 1;
22565 *value = l;
22566 }
22567 else if (bits == sizeof (*value) * 8)
22568 *value = l;
22569 else
22570 {
224c3ddb 22571 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22572 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22573 return bytes;
22574 }
22575
22576 return NULL;
22577}
22578
22579/* Read a constant value from an attribute. Either set *VALUE, or if
22580 the value does not fit in *VALUE, set *BYTES - either already
22581 allocated on the objfile obstack, or newly allocated on OBSTACK,
22582 or, set *BATON, if we translated the constant to a location
22583 expression. */
22584
22585static void
ff39bb5e 22586dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22587 const char *name, struct obstack *obstack,
22588 struct dwarf2_cu *cu,
d521ce57 22589 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22590 struct dwarf2_locexpr_baton **baton)
22591{
5e22e966 22592 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22593 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22594 struct comp_unit_head *cu_header = &cu->header;
c906108c 22595 struct dwarf_block *blk;
98bfdba5
PA
22596 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22597 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22598
22599 *value = 0;
22600 *bytes = NULL;
22601 *baton = NULL;
c906108c
SS
22602
22603 switch (attr->form)
22604 {
22605 case DW_FORM_addr:
336d760d 22606 case DW_FORM_addrx:
3019eac3 22607 case DW_FORM_GNU_addr_index:
ac56253d 22608 {
ac56253d
TT
22609 gdb_byte *data;
22610
98bfdba5
PA
22611 if (TYPE_LENGTH (type) != cu_header->addr_size)
22612 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22613 cu_header->addr_size,
98bfdba5 22614 TYPE_LENGTH (type));
ac56253d
TT
22615 /* Symbols of this form are reasonably rare, so we just
22616 piggyback on the existing location code rather than writing
22617 a new implementation of symbol_computed_ops. */
8d749320 22618 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22619 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22620 (*baton)->per_cu = cu->per_cu;
22621 gdb_assert ((*baton)->per_cu);
ac56253d 22622
98bfdba5 22623 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22624 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22625 (*baton)->data = data;
ac56253d
TT
22626
22627 data[0] = DW_OP_addr;
22628 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22629 byte_order, attr->as_address ());
ac56253d 22630 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22631 }
c906108c 22632 break;
4ac36638 22633 case DW_FORM_string:
93b5768b 22634 case DW_FORM_strp:
cf532bd1 22635 case DW_FORM_strx:
3019eac3 22636 case DW_FORM_GNU_str_index:
36586728 22637 case DW_FORM_GNU_strp_alt:
c6481205 22638 /* The string is already allocated on the objfile obstack, point
98bfdba5 22639 directly to it. */
2c830f54 22640 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22641 break;
c906108c
SS
22642 case DW_FORM_block1:
22643 case DW_FORM_block2:
22644 case DW_FORM_block4:
22645 case DW_FORM_block:
2dc7f7b3 22646 case DW_FORM_exprloc:
0224619f 22647 case DW_FORM_data16:
9d2246fc 22648 blk = attr->as_block ();
98bfdba5
PA
22649 if (TYPE_LENGTH (type) != blk->size)
22650 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22651 TYPE_LENGTH (type));
22652 *bytes = blk->data;
c906108c 22653 break;
2df3850c
JM
22654
22655 /* The DW_AT_const_value attributes are supposed to carry the
22656 symbol's value "represented as it would be on the target
22657 architecture." By the time we get here, it's already been
22658 converted to host endianness, so we just need to sign- or
22659 zero-extend it as appropriate. */
22660 case DW_FORM_data1:
3aef2284 22661 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22662 break;
c906108c 22663 case DW_FORM_data2:
3aef2284 22664 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22665 break;
c906108c 22666 case DW_FORM_data4:
3aef2284 22667 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22668 break;
c906108c 22669 case DW_FORM_data8:
3aef2284 22670 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22671 break;
22672
c906108c 22673 case DW_FORM_sdata:
663c44ac 22674 case DW_FORM_implicit_const:
1bc397c5 22675 *value = attr->as_signed ();
2df3850c
JM
22676 break;
22677
c906108c 22678 case DW_FORM_udata:
529908cb 22679 *value = attr->as_unsigned ();
c906108c 22680 break;
2df3850c 22681
c906108c 22682 default:
b98664d3 22683 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22684 dwarf_form_name (attr->form));
98bfdba5 22685 *value = 0;
c906108c
SS
22686 break;
22687 }
22688}
22689
2df3850c 22690
98bfdba5
PA
22691/* Copy constant value from an attribute to a symbol. */
22692
2df3850c 22693static void
ff39bb5e 22694dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22695 struct dwarf2_cu *cu)
2df3850c 22696{
5e22e966 22697 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22698 LONGEST value;
d521ce57 22699 const gdb_byte *bytes;
98bfdba5 22700 struct dwarf2_locexpr_baton *baton;
2df3850c 22701
98bfdba5 22702 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22703 sym->print_name (),
98bfdba5
PA
22704 &objfile->objfile_obstack, cu,
22705 &value, &bytes, &baton);
2df3850c 22706
98bfdba5
PA
22707 if (baton != NULL)
22708 {
98bfdba5 22709 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22710 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22711 }
22712 else if (bytes != NULL)
22713 {
22714 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22715 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22716 }
22717 else
22718 {
22719 SYMBOL_VALUE (sym) = value;
f1e6e072 22720 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22721 }
2df3850c
JM
22722}
22723
c906108c
SS
22724/* Return the type of the die in question using its DW_AT_type attribute. */
22725
22726static struct type *
e7c27a73 22727die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22728{
c906108c 22729 struct attribute *type_attr;
c906108c 22730
e142c38c 22731 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22732 if (!type_attr)
22733 {
5e22e966 22734 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22735 /* A missing DW_AT_type represents a void type. */
518817b3 22736 return objfile_type (objfile)->builtin_void;
c906108c 22737 }
348e048f 22738
673bfd45 22739 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22740}
22741
b4ba55a1
JB
22742/* True iff CU's producer generates GNAT Ada auxiliary information
22743 that allows to find parallel types through that information instead
22744 of having to do expensive parallel lookups by type name. */
22745
22746static int
22747need_gnat_info (struct dwarf2_cu *cu)
22748{
de4cb04a
JB
22749 /* Assume that the Ada compiler was GNAT, which always produces
22750 the auxiliary information. */
22751 return (cu->language == language_ada);
b4ba55a1
JB
22752}
22753
b4ba55a1
JB
22754/* Return the auxiliary type of the die in question using its
22755 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22756 attribute is not present. */
22757
22758static struct type *
22759die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22760{
b4ba55a1 22761 struct attribute *type_attr;
b4ba55a1
JB
22762
22763 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22764 if (!type_attr)
22765 return NULL;
22766
673bfd45 22767 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22768}
22769
22770/* If DIE has a descriptive_type attribute, then set the TYPE's
22771 descriptive type accordingly. */
22772
22773static void
22774set_descriptive_type (struct type *type, struct die_info *die,
22775 struct dwarf2_cu *cu)
22776{
22777 struct type *descriptive_type = die_descriptive_type (die, cu);
22778
22779 if (descriptive_type)
22780 {
22781 ALLOCATE_GNAT_AUX_TYPE (type);
22782 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22783 }
22784}
22785
c906108c
SS
22786/* Return the containing type of the die in question using its
22787 DW_AT_containing_type attribute. */
22788
22789static struct type *
e7c27a73 22790die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22791{
c906108c 22792 struct attribute *type_attr;
5e22e966 22793 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22794
e142c38c 22795 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22796 if (!type_attr)
22797 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22798 "[in module %s]"), objfile_name (objfile));
33ac96f0 22799
673bfd45 22800 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22801}
22802
ac9ec31b
DE
22803/* Return an error marker type to use for the ill formed type in DIE/CU. */
22804
22805static struct type *
22806build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22807{
976ca316
SM
22808 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22809 struct objfile *objfile = per_objfile->objfile;
528e1572 22810 char *saved;
ac9ec31b 22811
528e1572
SM
22812 std::string message
22813 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22814 objfile_name (objfile),
22815 sect_offset_str (cu->header.sect_off),
22816 sect_offset_str (die->sect_off));
efba19b0 22817 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22818
19f392bc 22819 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22820}
22821
673bfd45 22822/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22823 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22824 DW_AT_containing_type.
673bfd45
DE
22825 If there is no type substitute an error marker. */
22826
c906108c 22827static struct type *
ff39bb5e 22828lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22829 struct dwarf2_cu *cu)
c906108c 22830{
976ca316
SM
22831 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22832 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22833 struct type *this_type;
22834
ac9ec31b
DE
22835 gdb_assert (attr->name == DW_AT_type
22836 || attr->name == DW_AT_GNAT_descriptive_type
22837 || attr->name == DW_AT_containing_type);
22838
673bfd45
DE
22839 /* First see if we have it cached. */
22840
36586728
TT
22841 if (attr->form == DW_FORM_GNU_ref_alt)
22842 {
22843 struct dwarf2_per_cu_data *per_cu;
0826b30a 22844 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22845
976ca316
SM
22846 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22847 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22848 }
cd6c91b4 22849 else if (attr->form_is_ref ())
673bfd45 22850 {
0826b30a 22851 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22852
976ca316 22853 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22854 }
55f1336d 22855 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22856 {
630ed6b9 22857 ULONGEST signature = attr->as_signature ();
673bfd45 22858
ac9ec31b 22859 return get_signatured_type (die, signature, cu);
673bfd45
DE
22860 }
22861 else
22862 {
b98664d3 22863 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22864 " at %s [in module %s]"),
22865 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22866 objfile_name (objfile));
ac9ec31b 22867 return build_error_marker_type (cu, die);
673bfd45
DE
22868 }
22869
22870 /* If not cached we need to read it in. */
22871
22872 if (this_type == NULL)
22873 {
ac9ec31b 22874 struct die_info *type_die = NULL;
673bfd45
DE
22875 struct dwarf2_cu *type_cu = cu;
22876
cd6c91b4 22877 if (attr->form_is_ref ())
ac9ec31b
DE
22878 type_die = follow_die_ref (die, attr, &type_cu);
22879 if (type_die == NULL)
22880 return build_error_marker_type (cu, die);
22881 /* If we find the type now, it's probably because the type came
3019eac3
DE
22882 from an inter-CU reference and the type's CU got expanded before
22883 ours. */
ac9ec31b 22884 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22885 }
22886
22887 /* If we still don't have a type use an error marker. */
22888
22889 if (this_type == NULL)
ac9ec31b 22890 return build_error_marker_type (cu, die);
673bfd45 22891
f792889a 22892 return this_type;
c906108c
SS
22893}
22894
673bfd45
DE
22895/* Return the type in DIE, CU.
22896 Returns NULL for invalid types.
22897
02142a6c 22898 This first does a lookup in die_type_hash,
673bfd45
DE
22899 and only reads the die in if necessary.
22900
22901 NOTE: This can be called when reading in partial or full symbols. */
22902
f792889a 22903static struct type *
e7c27a73 22904read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22905{
f792889a
DJ
22906 struct type *this_type;
22907
22908 this_type = get_die_type (die, cu);
22909 if (this_type)
22910 return this_type;
22911
673bfd45
DE
22912 return read_type_die_1 (die, cu);
22913}
22914
22915/* Read the type in DIE, CU.
22916 Returns NULL for invalid types. */
22917
22918static struct type *
22919read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22920{
22921 struct type *this_type = NULL;
22922
c906108c
SS
22923 switch (die->tag)
22924 {
22925 case DW_TAG_class_type:
680b30c7 22926 case DW_TAG_interface_type:
c906108c
SS
22927 case DW_TAG_structure_type:
22928 case DW_TAG_union_type:
f792889a 22929 this_type = read_structure_type (die, cu);
c906108c
SS
22930 break;
22931 case DW_TAG_enumeration_type:
f792889a 22932 this_type = read_enumeration_type (die, cu);
c906108c
SS
22933 break;
22934 case DW_TAG_subprogram:
22935 case DW_TAG_subroutine_type:
edb3359d 22936 case DW_TAG_inlined_subroutine:
f792889a 22937 this_type = read_subroutine_type (die, cu);
c906108c
SS
22938 break;
22939 case DW_TAG_array_type:
f792889a 22940 this_type = read_array_type (die, cu);
c906108c 22941 break;
72019c9c 22942 case DW_TAG_set_type:
f792889a 22943 this_type = read_set_type (die, cu);
72019c9c 22944 break;
c906108c 22945 case DW_TAG_pointer_type:
f792889a 22946 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22947 break;
22948 case DW_TAG_ptr_to_member_type:
f792889a 22949 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22950 break;
22951 case DW_TAG_reference_type:
4297a3f0
AV
22952 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22953 break;
22954 case DW_TAG_rvalue_reference_type:
22955 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22956 break;
22957 case DW_TAG_const_type:
f792889a 22958 this_type = read_tag_const_type (die, cu);
c906108c
SS
22959 break;
22960 case DW_TAG_volatile_type:
f792889a 22961 this_type = read_tag_volatile_type (die, cu);
c906108c 22962 break;
06d66ee9
TT
22963 case DW_TAG_restrict_type:
22964 this_type = read_tag_restrict_type (die, cu);
22965 break;
c906108c 22966 case DW_TAG_string_type:
f792889a 22967 this_type = read_tag_string_type (die, cu);
c906108c
SS
22968 break;
22969 case DW_TAG_typedef:
f792889a 22970 this_type = read_typedef (die, cu);
c906108c 22971 break;
a02abb62 22972 case DW_TAG_subrange_type:
f792889a 22973 this_type = read_subrange_type (die, cu);
a02abb62 22974 break;
c906108c 22975 case DW_TAG_base_type:
f792889a 22976 this_type = read_base_type (die, cu);
c906108c 22977 break;
81a17f79 22978 case DW_TAG_unspecified_type:
f792889a 22979 this_type = read_unspecified_type (die, cu);
81a17f79 22980 break;
0114d602
DJ
22981 case DW_TAG_namespace:
22982 this_type = read_namespace_type (die, cu);
22983 break;
f55ee35c
JK
22984 case DW_TAG_module:
22985 this_type = read_module_type (die, cu);
22986 break;
a2c2acaf
MW
22987 case DW_TAG_atomic_type:
22988 this_type = read_tag_atomic_type (die, cu);
22989 break;
c906108c 22990 default:
b98664d3 22991 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22992 dwarf_tag_name (die->tag));
c906108c
SS
22993 break;
22994 }
63d06c5c 22995
f792889a 22996 return this_type;
63d06c5c
DC
22997}
22998
abc72ce4
DE
22999/* See if we can figure out if the class lives in a namespace. We do
23000 this by looking for a member function; its demangled name will
23001 contain namespace info, if there is any.
23002 Return the computed name or NULL.
23003 Space for the result is allocated on the objfile's obstack.
23004 This is the full-die version of guess_partial_die_structure_name.
23005 In this case we know DIE has no useful parent. */
23006
43816ebc 23007static const char *
abc72ce4
DE
23008guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23009{
23010 struct die_info *spec_die;
23011 struct dwarf2_cu *spec_cu;
23012 struct die_info *child;
5e22e966 23013 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
23014
23015 spec_cu = cu;
23016 spec_die = die_specification (die, &spec_cu);
23017 if (spec_die != NULL)
23018 {
23019 die = spec_die;
23020 cu = spec_cu;
23021 }
23022
23023 for (child = die->child;
23024 child != NULL;
23025 child = child->sibling)
23026 {
23027 if (child->tag == DW_TAG_subprogram)
23028 {
73b9be8b 23029 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 23030
7d45c7c3 23031 if (linkage_name != NULL)
abc72ce4 23032 {
43816ebc 23033 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 23034 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 23035 const char *name = NULL;
abc72ce4
DE
23036
23037 if (actual_name != NULL)
23038 {
15d034d0 23039 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
23040
23041 if (die_name != NULL
43816ebc 23042 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
23043 {
23044 /* Strip off the class name from the full name.
23045 We want the prefix. */
23046 int die_name_len = strlen (die_name);
43816ebc
TT
23047 int actual_name_len = strlen (actual_name.get ());
23048 const char *ptr = actual_name.get ();
abc72ce4
DE
23049
23050 /* Test for '::' as a sanity check. */
23051 if (actual_name_len > die_name_len + 2
43816ebc 23052 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 23053 name = obstack_strndup (
e3b94546 23054 &objfile->per_bfd->storage_obstack,
43816ebc 23055 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
23056 }
23057 }
abc72ce4
DE
23058 return name;
23059 }
23060 }
23061 }
23062
23063 return NULL;
23064}
23065
96408a79
SA
23066/* GCC might emit a nameless typedef that has a linkage name. Determine the
23067 prefix part in such case. See
23068 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23069
a121b7c1 23070static const char *
96408a79
SA
23071anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23072{
23073 struct attribute *attr;
e6a959d6 23074 const char *base;
96408a79
SA
23075
23076 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23077 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23078 return NULL;
23079
7d45c7c3 23080 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
23081 return NULL;
23082
73b9be8b 23083 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
23084 const char *attr_name = attr->as_string ();
23085 if (attr == NULL || attr_name == NULL)
96408a79
SA
23086 return NULL;
23087
23088 /* dwarf2_name had to be already called. */
3b64bf15 23089 gdb_assert (attr->canonical_string_p ());
96408a79
SA
23090
23091 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
23092 base = strrchr (attr_name, ':');
23093 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
23094 return "";
23095
5e22e966 23096 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 23097 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
23098 attr_name,
23099 &base[-1] - attr_name);
96408a79
SA
23100}
23101
fdde2d81 23102/* Return the name of the namespace/class that DIE is defined within,
0114d602 23103 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 23104
0114d602
DJ
23105 For example, if we're within the method foo() in the following
23106 code:
23107
23108 namespace N {
23109 class C {
23110 void foo () {
23111 }
23112 };
23113 }
23114
23115 then determine_prefix on foo's die will return "N::C". */
fdde2d81 23116
0d5cff50 23117static const char *
e142c38c 23118determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 23119{
976ca316 23120 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
23121 struct die_info *parent, *spec_die;
23122 struct dwarf2_cu *spec_cu;
23123 struct type *parent_type;
a121b7c1 23124 const char *retval;
63d06c5c 23125
9c37b5ae 23126 if (cu->language != language_cplus
c44af4eb
TT
23127 && cu->language != language_fortran && cu->language != language_d
23128 && cu->language != language_rust)
0114d602
DJ
23129 return "";
23130
96408a79
SA
23131 retval = anonymous_struct_prefix (die, cu);
23132 if (retval)
23133 return retval;
23134
0114d602
DJ
23135 /* We have to be careful in the presence of DW_AT_specification.
23136 For example, with GCC 3.4, given the code
23137
23138 namespace N {
23139 void foo() {
23140 // Definition of N::foo.
23141 }
23142 }
23143
23144 then we'll have a tree of DIEs like this:
23145
23146 1: DW_TAG_compile_unit
23147 2: DW_TAG_namespace // N
23148 3: DW_TAG_subprogram // declaration of N::foo
23149 4: DW_TAG_subprogram // definition of N::foo
23150 DW_AT_specification // refers to die #3
23151
23152 Thus, when processing die #4, we have to pretend that we're in
23153 the context of its DW_AT_specification, namely the contex of die
23154 #3. */
23155 spec_cu = cu;
23156 spec_die = die_specification (die, &spec_cu);
23157 if (spec_die == NULL)
23158 parent = die->parent;
23159 else
63d06c5c 23160 {
0114d602
DJ
23161 parent = spec_die->parent;
23162 cu = spec_cu;
63d06c5c 23163 }
0114d602
DJ
23164
23165 if (parent == NULL)
23166 return "";
98bfdba5
PA
23167 else if (parent->building_fullname)
23168 {
23169 const char *name;
23170 const char *parent_name;
23171
23172 /* It has been seen on RealView 2.2 built binaries,
23173 DW_TAG_template_type_param types actually _defined_ as
23174 children of the parent class:
23175
23176 enum E {};
23177 template class <class Enum> Class{};
23178 Class<enum E> class_e;
23179
dda83cd7
SM
23180 1: DW_TAG_class_type (Class)
23181 2: DW_TAG_enumeration_type (E)
23182 3: DW_TAG_enumerator (enum1:0)
23183 3: DW_TAG_enumerator (enum2:1)
23184 ...
23185 2: DW_TAG_template_type_param
23186 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
23187
23188 Besides being broken debug info, it can put GDB into an
23189 infinite loop. Consider:
23190
23191 When we're building the full name for Class<E>, we'll start
23192 at Class, and go look over its template type parameters,
23193 finding E. We'll then try to build the full name of E, and
23194 reach here. We're now trying to build the full name of E,
23195 and look over the parent DIE for containing scope. In the
23196 broken case, if we followed the parent DIE of E, we'd again
23197 find Class, and once again go look at its template type
23198 arguments, etc., etc. Simply don't consider such parent die
23199 as source-level parent of this die (it can't be, the language
23200 doesn't allow it), and break the loop here. */
23201 name = dwarf2_name (die, cu);
23202 parent_name = dwarf2_name (parent, cu);
b98664d3 23203 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
23204 name ? name : "<unknown>",
23205 parent_name ? parent_name : "<unknown>");
23206 return "";
23207 }
63d06c5c 23208 else
0114d602
DJ
23209 switch (parent->tag)
23210 {
63d06c5c 23211 case DW_TAG_namespace:
0114d602 23212 parent_type = read_type_die (parent, cu);
acebe513
UW
23213 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23214 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23215 Work around this problem here. */
23216 if (cu->language == language_cplus
7d93a1e0 23217 && strcmp (parent_type->name (), "::") == 0)
acebe513 23218 return "";
0114d602 23219 /* We give a name to even anonymous namespaces. */
7d93a1e0 23220 return parent_type->name ();
63d06c5c 23221 case DW_TAG_class_type:
680b30c7 23222 case DW_TAG_interface_type:
63d06c5c 23223 case DW_TAG_structure_type:
0114d602 23224 case DW_TAG_union_type:
f55ee35c 23225 case DW_TAG_module:
0114d602 23226 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
23227 if (parent_type->name () != NULL)
23228 return parent_type->name ();
0114d602
DJ
23229 else
23230 /* An anonymous structure is only allowed non-static data
23231 members; no typedefs, no member functions, et cetera.
23232 So it does not need a prefix. */
23233 return "";
abc72ce4 23234 case DW_TAG_compile_unit:
95554aad 23235 case DW_TAG_partial_unit:
abc72ce4
DE
23236 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23237 if (cu->language == language_cplus
976ca316 23238 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
23239 && die->child != NULL
23240 && (die->tag == DW_TAG_class_type
23241 || die->tag == DW_TAG_structure_type
23242 || die->tag == DW_TAG_union_type))
23243 {
43816ebc 23244 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
23245 if (name != NULL)
23246 return name;
23247 }
23248 return "";
0a4b0913
AB
23249 case DW_TAG_subprogram:
23250 /* Nested subroutines in Fortran get a prefix with the name
23251 of the parent's subroutine. */
23252 if (cu->language == language_fortran)
23253 {
23254 if ((die->tag == DW_TAG_subprogram)
23255 && (dwarf2_name (parent, cu) != NULL))
23256 return dwarf2_name (parent, cu);
23257 }
23258 return determine_prefix (parent, cu);
3d567982
TT
23259 case DW_TAG_enumeration_type:
23260 parent_type = read_type_die (parent, cu);
23261 if (TYPE_DECLARED_CLASS (parent_type))
23262 {
7d93a1e0
SM
23263 if (parent_type->name () != NULL)
23264 return parent_type->name ();
3d567982
TT
23265 return "";
23266 }
23267 /* Fall through. */
63d06c5c 23268 default:
8176b9b8 23269 return determine_prefix (parent, cu);
63d06c5c 23270 }
63d06c5c
DC
23271}
23272
3e43a32a
MS
23273/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23274 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23275 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23276 an obconcat, otherwise allocate storage for the result. The CU argument is
23277 used to determine the language and hence, the appropriate separator. */
987504bb 23278
f55ee35c 23279#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23280
23281static char *
f55ee35c 23282typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23283 int physname, struct dwarf2_cu *cu)
63d06c5c 23284{
f55ee35c 23285 const char *lead = "";
5c315b68 23286 const char *sep;
63d06c5c 23287
3e43a32a
MS
23288 if (suffix == NULL || suffix[0] == '\0'
23289 || prefix == NULL || prefix[0] == '\0')
987504bb 23290 sep = "";
45280282
IB
23291 else if (cu->language == language_d)
23292 {
23293 /* For D, the 'main' function could be defined in any module, but it
23294 should never be prefixed. */
23295 if (strcmp (suffix, "D main") == 0)
23296 {
23297 prefix = "";
23298 sep = "";
23299 }
23300 else
23301 sep = ".";
23302 }
f55ee35c
JK
23303 else if (cu->language == language_fortran && physname)
23304 {
23305 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23306 DW_AT_MIPS_linkage_name is preferred and used instead. */
23307
23308 lead = "__";
23309 sep = "_MOD_";
23310 }
987504bb
JJ
23311 else
23312 sep = "::";
63d06c5c 23313
6dd47d34
DE
23314 if (prefix == NULL)
23315 prefix = "";
23316 if (suffix == NULL)
23317 suffix = "";
23318
987504bb
JJ
23319 if (obs == NULL)
23320 {
3e43a32a 23321 char *retval
224c3ddb
SM
23322 = ((char *)
23323 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23324
f55ee35c
JK
23325 strcpy (retval, lead);
23326 strcat (retval, prefix);
6dd47d34
DE
23327 strcat (retval, sep);
23328 strcat (retval, suffix);
63d06c5c
DC
23329 return retval;
23330 }
987504bb
JJ
23331 else
23332 {
23333 /* We have an obstack. */
f55ee35c 23334 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23335 }
63d06c5c
DC
23336}
23337
71c25dea
TT
23338/* Get name of a die, return NULL if not found. */
23339
15d034d0
TT
23340static const char *
23341dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23342 struct objfile *objfile)
71c25dea
TT
23343{
23344 if (name && cu->language == language_cplus)
23345 {
596dc4ad
TT
23346 gdb::unique_xmalloc_ptr<char> canon_name
23347 = cp_canonicalize_string (name);
71c25dea 23348
596dc4ad
TT
23349 if (canon_name != nullptr)
23350 name = objfile->intern (canon_name.get ());
71c25dea
TT
23351 }
23352
23353 return name;
c906108c
SS
23354}
23355
96553a0c
DE
23356/* Get name of a die, return NULL if not found.
23357 Anonymous namespaces are converted to their magic string. */
9219021c 23358
15d034d0 23359static const char *
e142c38c 23360dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23361{
23362 struct attribute *attr;
5e22e966 23363 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23364
e142c38c 23365 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23366 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23367 if (attr_name == nullptr
96553a0c 23368 && die->tag != DW_TAG_namespace
53832f31
TT
23369 && die->tag != DW_TAG_class_type
23370 && die->tag != DW_TAG_interface_type
23371 && die->tag != DW_TAG_structure_type
23372 && die->tag != DW_TAG_union_type)
71c25dea
TT
23373 return NULL;
23374
23375 switch (die->tag)
23376 {
23377 case DW_TAG_compile_unit:
95554aad 23378 case DW_TAG_partial_unit:
71c25dea
TT
23379 /* Compilation units have a DW_AT_name that is a filename, not
23380 a source language identifier. */
23381 case DW_TAG_enumeration_type:
23382 case DW_TAG_enumerator:
23383 /* These tags always have simple identifiers already; no need
23384 to canonicalize them. */
2c830f54 23385 return attr_name;
907af001 23386
96553a0c 23387 case DW_TAG_namespace:
2c830f54
TT
23388 if (attr_name != nullptr)
23389 return attr_name;
96553a0c
DE
23390 return CP_ANONYMOUS_NAMESPACE_STR;
23391
907af001
UW
23392 case DW_TAG_class_type:
23393 case DW_TAG_interface_type:
23394 case DW_TAG_structure_type:
23395 case DW_TAG_union_type:
23396 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23397 structures or unions. These were of the form "._%d" in GCC 4.1,
23398 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23399 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23400 if (attr_name != nullptr
23401 && (startswith (attr_name, "._")
23402 || startswith (attr_name, "<anonymous")))
907af001 23403 return NULL;
53832f31
TT
23404
23405 /* GCC might emit a nameless typedef that has a linkage name. See
23406 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23407 if (!attr || attr_name == NULL)
53832f31 23408 {
73b9be8b 23409 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23410 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23411 if (attr == NULL || attr_name == NULL)
53832f31
TT
23412 return NULL;
23413
2c830f54 23414 /* Avoid demangling attr_name the second time on a second
df5c6c50 23415 call for the same DIE. */
3b64bf15 23416 if (!attr->canonical_string_p ())
53832f31 23417 {
43816ebc 23418 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23419 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23420 if (demangled == nullptr)
23421 return nullptr;
43816ebc 23422
c6481205 23423 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23424 attr_name = attr->as_string ();
53832f31 23425 }
67430cd0 23426
2c830f54
TT
23427 /* Strip any leading namespaces/classes, keep only the
23428 base name. DW_AT_name for named DIEs does not
23429 contain the prefixes. */
23430 const char *base = strrchr (attr_name, ':');
23431 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23432 return &base[1];
23433 else
2c830f54 23434 return attr_name;
53832f31 23435 }
907af001
UW
23436 break;
23437
71c25dea 23438 default:
907af001
UW
23439 break;
23440 }
23441
3b64bf15 23442 if (!attr->canonical_string_p ())
c6481205
TT
23443 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23444 objfile));
2c830f54 23445 return attr->as_string ();
9219021c
DC
23446}
23447
23448/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23449 is none. *EXT_CU is the CU containing DIE on input, and the CU
23450 containing the return value on output. */
9219021c
DC
23451
23452static struct die_info *
f2f0e013 23453dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23454{
23455 struct attribute *attr;
9219021c 23456
f2f0e013 23457 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23458 if (attr == NULL)
23459 return NULL;
23460
f2f0e013 23461 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23462}
23463
f9aca02d 23464static void
d97bc12b 23465dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23466{
23467 unsigned int i;
23468
d97bc12b 23469 print_spaces (indent, f);
9d8780f0 23470 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23471 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23472 sect_offset_str (die->sect_off));
d97bc12b
DE
23473
23474 if (die->parent != NULL)
23475 {
23476 print_spaces (indent, f);
9d8780f0
SM
23477 fprintf_unfiltered (f, " parent at offset: %s\n",
23478 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23479 }
23480
23481 print_spaces (indent, f);
23482 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23483 dwarf_bool_name (die->child != NULL));
c906108c 23484
d97bc12b
DE
23485 print_spaces (indent, f);
23486 fprintf_unfiltered (f, " attributes:\n");
23487
c906108c
SS
23488 for (i = 0; i < die->num_attrs; ++i)
23489 {
d97bc12b
DE
23490 print_spaces (indent, f);
23491 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23492 dwarf_attr_name (die->attrs[i].name),
23493 dwarf_form_name (die->attrs[i].form));
d97bc12b 23494
c906108c
SS
23495 switch (die->attrs[i].form)
23496 {
c906108c 23497 case DW_FORM_addr:
336d760d 23498 case DW_FORM_addrx:
3019eac3 23499 case DW_FORM_GNU_addr_index:
d97bc12b 23500 fprintf_unfiltered (f, "address: ");
36d378cf 23501 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23502 break;
23503 case DW_FORM_block2:
23504 case DW_FORM_block4:
23505 case DW_FORM_block:
23506 case DW_FORM_block1:
56eb65bd 23507 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23508 pulongest (die->attrs[i].as_block ()->size));
c906108c 23509 break;
2dc7f7b3 23510 case DW_FORM_exprloc:
56eb65bd 23511 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23512 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23513 break;
0224619f
JK
23514 case DW_FORM_data16:
23515 fprintf_unfiltered (f, "constant of 16 bytes");
23516 break;
4568ecf9
DE
23517 case DW_FORM_ref_addr:
23518 fprintf_unfiltered (f, "ref address: ");
529908cb 23519 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23520 break;
36586728
TT
23521 case DW_FORM_GNU_ref_alt:
23522 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23523 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23524 break;
10b3939b
DJ
23525 case DW_FORM_ref1:
23526 case DW_FORM_ref2:
23527 case DW_FORM_ref4:
4568ecf9
DE
23528 case DW_FORM_ref8:
23529 case DW_FORM_ref_udata:
d97bc12b 23530 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23531 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23532 break;
c906108c
SS
23533 case DW_FORM_data1:
23534 case DW_FORM_data2:
23535 case DW_FORM_data4:
ce5d95e1 23536 case DW_FORM_data8:
c906108c 23537 case DW_FORM_udata:
43bbcdc2 23538 fprintf_unfiltered (f, "constant: %s",
529908cb 23539 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23540 break;
2dc7f7b3
TT
23541 case DW_FORM_sec_offset:
23542 fprintf_unfiltered (f, "section offset: %s",
529908cb 23543 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23544 break;
55f1336d 23545 case DW_FORM_ref_sig8:
ac9ec31b 23546 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23547 hex_string (die->attrs[i].as_signature ()));
348e048f 23548 break;
c906108c 23549 case DW_FORM_string:
4bdf3d34 23550 case DW_FORM_strp:
43988095 23551 case DW_FORM_line_strp:
cf532bd1 23552 case DW_FORM_strx:
3019eac3 23553 case DW_FORM_GNU_str_index:
36586728 23554 case DW_FORM_GNU_strp_alt:
8285870a 23555 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23556 die->attrs[i].as_string ()
23557 ? die->attrs[i].as_string () : "",
23558 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23559 break;
23560 case DW_FORM_flag:
c45bc3f8 23561 if (die->attrs[i].as_boolean ())
d97bc12b 23562 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23563 else
d97bc12b 23564 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23565 break;
2dc7f7b3
TT
23566 case DW_FORM_flag_present:
23567 fprintf_unfiltered (f, "flag: TRUE");
23568 break;
a8329558 23569 case DW_FORM_indirect:
0963b4bd
MS
23570 /* The reader will have reduced the indirect form to
23571 the "base form" so this form should not occur. */
5f48f8f3 23572 fprintf_unfiltered (f,
3e43a32a 23573 "unexpected attribute form: DW_FORM_indirect");
a8329558 23574 break;
1bc397c5 23575 case DW_FORM_sdata:
663c44ac
JK
23576 case DW_FORM_implicit_const:
23577 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23578 plongest (die->attrs[i].as_signed ()));
663c44ac 23579 break;
c906108c 23580 default:
d97bc12b 23581 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23582 die->attrs[i].form);
d97bc12b 23583 break;
c906108c 23584 }
d97bc12b 23585 fprintf_unfiltered (f, "\n");
c906108c
SS
23586 }
23587}
23588
f9aca02d 23589static void
d97bc12b 23590dump_die_for_error (struct die_info *die)
c906108c 23591{
d97bc12b
DE
23592 dump_die_shallow (gdb_stderr, 0, die);
23593}
23594
23595static void
23596dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23597{
23598 int indent = level * 4;
23599
23600 gdb_assert (die != NULL);
23601
23602 if (level >= max_level)
23603 return;
23604
23605 dump_die_shallow (f, indent, die);
23606
23607 if (die->child != NULL)
c906108c 23608 {
d97bc12b
DE
23609 print_spaces (indent, f);
23610 fprintf_unfiltered (f, " Children:");
23611 if (level + 1 < max_level)
23612 {
23613 fprintf_unfiltered (f, "\n");
23614 dump_die_1 (f, level + 1, max_level, die->child);
23615 }
23616 else
23617 {
3e43a32a
MS
23618 fprintf_unfiltered (f,
23619 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23620 }
23621 }
23622
23623 if (die->sibling != NULL && level > 0)
23624 {
23625 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23626 }
23627}
23628
d97bc12b
DE
23629/* This is called from the pdie macro in gdbinit.in.
23630 It's not static so gcc will keep a copy callable from gdb. */
23631
23632void
23633dump_die (struct die_info *die, int max_level)
23634{
23635 dump_die_1 (gdb_stdlog, 0, max_level, die);
23636}
23637
f9aca02d 23638static void
51545339 23639store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23640{
51545339 23641 void **slot;
c906108c 23642
9c541725
PA
23643 slot = htab_find_slot_with_hash (cu->die_hash, die,
23644 to_underlying (die->sect_off),
b64f50a1 23645 INSERT);
51545339
DJ
23646
23647 *slot = die;
c906108c
SS
23648}
23649
348e048f
DE
23650/* Follow reference or signature attribute ATTR of SRC_DIE.
23651 On entry *REF_CU is the CU of SRC_DIE.
23652 On exit *REF_CU is the CU of the result. */
23653
23654static struct die_info *
ff39bb5e 23655follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23656 struct dwarf2_cu **ref_cu)
23657{
23658 struct die_info *die;
23659
cd6c91b4 23660 if (attr->form_is_ref ())
348e048f 23661 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23662 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23663 die = follow_die_sig (src_die, attr, ref_cu);
23664 else
23665 {
23666 dump_die_for_error (src_die);
23667 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23668 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23669 }
23670
23671 return die;
03dd20cc
DJ
23672}
23673
5c631832 23674/* Follow reference OFFSET.
673bfd45
DE
23675 On entry *REF_CU is the CU of the source die referencing OFFSET.
23676 On exit *REF_CU is the CU of the result.
23677 Returns NULL if OFFSET is invalid. */
f504f079 23678
f9aca02d 23679static struct die_info *
9c541725 23680follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23681 struct dwarf2_cu **ref_cu)
c906108c 23682{
10b3939b 23683 struct die_info temp_die;
f2f0e013 23684 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23685 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23686
348e048f
DE
23687 gdb_assert (cu->per_cu != NULL);
23688
98bfdba5
PA
23689 target_cu = cu;
23690
17e593e9
SM
23691 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23692 "source CU contains target offset: %d",
23693 sect_offset_str (cu->per_cu->sect_off),
23694 sect_offset_str (sect_off),
23695 cu->header.offset_in_cu_p (sect_off));
23696
3019eac3 23697 if (cu->per_cu->is_debug_types)
348e048f
DE
23698 {
23699 /* .debug_types CUs cannot reference anything outside their CU.
23700 If they need to, they have to reference a signatured type via
55f1336d 23701 DW_FORM_ref_sig8. */
4057dfde 23702 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23703 return NULL;
348e048f 23704 }
36586728 23705 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23706 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23707 {
23708 struct dwarf2_per_cu_data *per_cu;
9a619af0 23709
9c541725 23710 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23711 per_objfile);
03dd20cc 23712
17e593e9
SM
23713 dwarf_read_debug_printf_v ("target CU offset: %s, "
23714 "target CU DIEs loaded: %d",
23715 sect_offset_str (per_cu->sect_off),
23716 per_objfile->get_cu (per_cu) != nullptr);
23717
616c069a
SM
23718 /* If necessary, add it to the queue and load its DIEs.
23719
23720 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23721 it doesn't mean they are currently loaded. Since we require them
23722 to be loaded, we must check for ourselves. */
23723 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23724 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23725 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23726 false, cu->language);
03dd20cc 23727
976ca316 23728 target_cu = per_objfile->get_cu (per_cu);
616c069a 23729 gdb_assert (target_cu != nullptr);
10b3939b 23730 }
98bfdba5
PA
23731 else if (cu->dies == NULL)
23732 {
23733 /* We're loading full DIEs during partial symbol reading. */
976ca316 23734 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23735 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23736 language_minimal);
98bfdba5 23737 }
c906108c 23738
f2f0e013 23739 *ref_cu = target_cu;
9c541725 23740 temp_die.sect_off = sect_off;
c24bdb02
KS
23741
23742 if (target_cu != cu)
23743 target_cu->ancestor = cu;
23744
9a3c8263 23745 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23746 &temp_die,
23747 to_underlying (sect_off));
5c631832 23748}
10b3939b 23749
5c631832
JK
23750/* Follow reference attribute ATTR of SRC_DIE.
23751 On entry *REF_CU is the CU of SRC_DIE.
23752 On exit *REF_CU is the CU of the result. */
23753
23754static struct die_info *
ff39bb5e 23755follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23756 struct dwarf2_cu **ref_cu)
23757{
0826b30a 23758 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23759 struct dwarf2_cu *cu = *ref_cu;
23760 struct die_info *die;
23761
9c541725 23762 die = follow_die_offset (sect_off,
36586728
TT
23763 (attr->form == DW_FORM_GNU_ref_alt
23764 || cu->per_cu->is_dwz),
23765 ref_cu);
5c631832 23766 if (!die)
9d8780f0
SM
23767 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23768 "at %s [in module %s]"),
23769 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23770 objfile_name (cu->per_objfile->objfile));
348e048f 23771
5c631832
JK
23772 return die;
23773}
23774
d4c9a4f8 23775/* See read.h. */
5c631832
JK
23776
23777struct dwarf2_locexpr_baton
9c541725 23778dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23779 dwarf2_per_cu_data *per_cu,
976ca316 23780 dwarf2_per_objfile *per_objfile,
041d9819
SM
23781 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23782 bool resolve_abstract_p)
5c631832 23783{
5c631832
JK
23784 struct die_info *die;
23785 struct attribute *attr;
23786 struct dwarf2_locexpr_baton retval;
976ca316 23787 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23788
976ca316 23789 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23790 if (cu == nullptr)
976ca316 23791 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23792
23793 if (cu == nullptr)
cc12ce38
DE
23794 {
23795 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23796 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23797 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23798 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23799 }
918dd910 23800
9c541725 23801 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23802 if (!die)
9d8780f0
SM
23803 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23804 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23805
23806 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23807 if (!attr && resolve_abstract_p
976ca316
SM
23808 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23809 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23810 {
041d9819 23811 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23812 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23813 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23814
3360b6e7 23815 for (const auto &cand_off
976ca316 23816 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23817 {
3360b6e7
TV
23818 struct dwarf2_cu *cand_cu = cu;
23819 struct die_info *cand
23820 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23821 if (!cand
23822 || !cand->parent
e4a62c65
TV
23823 || cand->parent->tag != DW_TAG_subprogram)
23824 continue;
23825
23826 CORE_ADDR pc_low, pc_high;
23827 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23828 if (pc_low == ((CORE_ADDR) -1))
23829 continue;
23830 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23831 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23832 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23833 continue;
23834
23835 die = cand;
23836 attr = dwarf2_attr (die, DW_AT_location, cu);
23837 break;
23838 }
23839 }
23840
5c631832
JK
23841 if (!attr)
23842 {
e103e986
JK
23843 /* DWARF: "If there is no such attribute, then there is no effect.".
23844 DATA is ignored if SIZE is 0. */
5c631832 23845
e103e986 23846 retval.data = NULL;
5c631832
JK
23847 retval.size = 0;
23848 }
cd6c91b4 23849 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23850 {
23851 struct dwarf2_loclist_baton loclist_baton;
041d9819 23852 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23853 size_t size;
23854
23855 fill_in_loclist_baton (cu, &loclist_baton, attr);
23856
23857 retval.data = dwarf2_find_location_expression (&loclist_baton,
23858 &size, pc);
23859 retval.size = size;
23860 }
5c631832
JK
23861 else
23862 {
4fc6c0d5 23863 if (!attr->form_is_block ())
9d8780f0 23864 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23865 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23866 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23867
9d2246fc
TT
23868 struct dwarf_block *block = attr->as_block ();
23869 retval.data = block->data;
23870 retval.size = block->size;
5c631832 23871 }
976ca316 23872 retval.per_objfile = per_objfile;
5c631832 23873 retval.per_cu = cu->per_cu;
918dd910 23874
976ca316 23875 per_objfile->age_comp_units ();
918dd910 23876
5c631832 23877 return retval;
348e048f
DE
23878}
23879
d4c9a4f8 23880/* See read.h. */
8b9737bf
TT
23881
23882struct dwarf2_locexpr_baton
23883dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23884 dwarf2_per_cu_data *per_cu,
14095eb3 23885 dwarf2_per_objfile *per_objfile,
041d9819 23886 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23887{
9c541725 23888 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23889
14095eb3 23890 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23891 get_frame_pc);
8b9737bf
TT
23892}
23893
b6807d98
TT
23894/* Write a constant of a given type as target-ordered bytes into
23895 OBSTACK. */
23896
23897static const gdb_byte *
23898write_constant_as_bytes (struct obstack *obstack,
23899 enum bfd_endian byte_order,
23900 struct type *type,
23901 ULONGEST value,
23902 LONGEST *len)
23903{
23904 gdb_byte *result;
23905
23906 *len = TYPE_LENGTH (type);
224c3ddb 23907 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23908 store_unsigned_integer (result, *len, byte_order, value);
23909
23910 return result;
23911}
23912
d4c9a4f8 23913/* See read.h. */
b6807d98
TT
23914
23915const gdb_byte *
9c541725 23916dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23917 dwarf2_per_cu_data *per_cu,
14095eb3 23918 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23919 obstack *obstack,
b6807d98
TT
23920 LONGEST *len)
23921{
b6807d98
TT
23922 struct die_info *die;
23923 struct attribute *attr;
23924 const gdb_byte *result = NULL;
23925 struct type *type;
23926 LONGEST value;
23927 enum bfd_endian byte_order;
14095eb3 23928 struct objfile *objfile = per_objfile->objfile;
b6807d98 23929
7188ed02 23930 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23931 if (cu == nullptr)
23932 cu = load_cu (per_cu, per_objfile, false);
23933
23934 if (cu == nullptr)
cc12ce38
DE
23935 {
23936 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23937 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23938 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23939 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23940 }
b6807d98 23941
9c541725 23942 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23943 if (!die)
9d8780f0
SM
23944 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23945 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23946
23947 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23948 if (attr == NULL)
23949 return NULL;
23950
e3b94546 23951 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23952 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23953
23954 switch (attr->form)
23955 {
23956 case DW_FORM_addr:
336d760d 23957 case DW_FORM_addrx:
b6807d98
TT
23958 case DW_FORM_GNU_addr_index:
23959 {
23960 gdb_byte *tem;
23961
23962 *len = cu->header.addr_size;
224c3ddb 23963 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23964 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23965 result = tem;
23966 }
23967 break;
23968 case DW_FORM_string:
23969 case DW_FORM_strp:
cf532bd1 23970 case DW_FORM_strx:
b6807d98
TT
23971 case DW_FORM_GNU_str_index:
23972 case DW_FORM_GNU_strp_alt:
c6481205 23973 /* The string is already allocated on the objfile obstack, point
b6807d98 23974 directly to it. */
2c830f54
TT
23975 {
23976 const char *attr_name = attr->as_string ();
23977 result = (const gdb_byte *) attr_name;
23978 *len = strlen (attr_name);
23979 }
b6807d98
TT
23980 break;
23981 case DW_FORM_block1:
23982 case DW_FORM_block2:
23983 case DW_FORM_block4:
23984 case DW_FORM_block:
23985 case DW_FORM_exprloc:
0224619f 23986 case DW_FORM_data16:
9d2246fc
TT
23987 {
23988 struct dwarf_block *block = attr->as_block ();
23989 result = block->data;
23990 *len = block->size;
23991 }
b6807d98
TT
23992 break;
23993
23994 /* The DW_AT_const_value attributes are supposed to carry the
23995 symbol's value "represented as it would be on the target
23996 architecture." By the time we get here, it's already been
23997 converted to host endianness, so we just need to sign- or
23998 zero-extend it as appropriate. */
23999 case DW_FORM_data1:
24000 type = die_type (die, cu);
24001 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24002 if (result == NULL)
24003 result = write_constant_as_bytes (obstack, byte_order,
24004 type, value, len);
24005 break;
24006 case DW_FORM_data2:
24007 type = die_type (die, cu);
24008 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24009 if (result == NULL)
24010 result = write_constant_as_bytes (obstack, byte_order,
24011 type, value, len);
24012 break;
24013 case DW_FORM_data4:
24014 type = die_type (die, cu);
24015 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24016 if (result == NULL)
24017 result = write_constant_as_bytes (obstack, byte_order,
24018 type, value, len);
24019 break;
24020 case DW_FORM_data8:
24021 type = die_type (die, cu);
24022 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24023 if (result == NULL)
24024 result = write_constant_as_bytes (obstack, byte_order,
24025 type, value, len);
24026 break;
24027
24028 case DW_FORM_sdata:
663c44ac 24029 case DW_FORM_implicit_const:
b6807d98
TT
24030 type = die_type (die, cu);
24031 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 24032 type, attr->as_signed (), len);
b6807d98
TT
24033 break;
24034
24035 case DW_FORM_udata:
24036 type = die_type (die, cu);
24037 result = write_constant_as_bytes (obstack, byte_order,
529908cb 24038 type, attr->as_unsigned (), len);
b6807d98
TT
24039 break;
24040
24041 default:
b98664d3 24042 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
24043 dwarf_form_name (attr->form));
24044 break;
24045 }
24046
24047 return result;
24048}
24049
d4c9a4f8 24050/* See read.h. */
7942e96e
AA
24051
24052struct type *
9c541725 24053dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
24054 dwarf2_per_cu_data *per_cu,
24055 dwarf2_per_objfile *per_objfile)
7942e96e 24056{
7942e96e
AA
24057 struct die_info *die;
24058
7188ed02 24059 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
24060 if (cu == nullptr)
24061 cu = load_cu (per_cu, per_objfile, false);
24062
24063 if (cu == nullptr)
24064 return nullptr;
7942e96e 24065
9c541725 24066 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
24067 if (!die)
24068 return NULL;
24069
24070 return die_type (die, cu);
24071}
24072
8cb5117c 24073/* See read.h. */
8a9b8146
TT
24074
24075struct type *
b64f50a1 24076dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
24077 dwarf2_per_cu_data *per_cu,
24078 dwarf2_per_objfile *per_objfile)
8a9b8146 24079{
9c541725 24080 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 24081 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
24082}
24083
ac9ec31b 24084/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 24085 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
24086 On exit *REF_CU is the CU of the result.
24087 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
24088
24089static struct die_info *
ac9ec31b
DE
24090follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24091 struct dwarf2_cu **ref_cu)
348e048f 24092{
348e048f 24093 struct die_info temp_die;
c24bdb02 24094 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 24095 struct die_info *die;
976ca316 24096 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 24097
348e048f 24098
ac9ec31b
DE
24099 /* While it might be nice to assert sig_type->type == NULL here,
24100 we can get here for DW_AT_imported_declaration where we need
24101 the DIE not the type. */
348e048f 24102
616c069a 24103 /* If necessary, add it to the queue and load its DIEs.
348e048f 24104
616c069a
SM
24105 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24106 it doesn't mean they are currently loaded. Since we require them
24107 to be loaded, we must check for ourselves. */
976ca316 24108 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
616c069a
SM
24109 language_minimal)
24110 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
976ca316 24111 read_signatured_type (sig_type, per_objfile);
348e048f 24112
976ca316 24113 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 24114 gdb_assert (sig_cu != NULL);
9c541725
PA
24115 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24116 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 24117 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 24118 to_underlying (temp_die.sect_off));
348e048f
DE
24119 if (die)
24120 {
796a7ff8
DE
24121 /* For .gdb_index version 7 keep track of included TUs.
24122 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
24123 if (per_objfile->per_bfd->index_table != NULL
24124 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 24125 {
ae640021 24126 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
24127 }
24128
348e048f 24129 *ref_cu = sig_cu;
c24bdb02
KS
24130 if (sig_cu != cu)
24131 sig_cu->ancestor = cu;
24132
348e048f
DE
24133 return die;
24134 }
24135
ac9ec31b
DE
24136 return NULL;
24137}
24138
24139/* Follow signatured type referenced by ATTR in SRC_DIE.
24140 On entry *REF_CU is the CU of SRC_DIE.
24141 On exit *REF_CU is the CU of the result.
24142 The result is the DIE of the type.
24143 If the referenced type cannot be found an error is thrown. */
24144
24145static struct die_info *
ff39bb5e 24146follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
24147 struct dwarf2_cu **ref_cu)
24148{
630ed6b9 24149 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
24150 struct signatured_type *sig_type;
24151 struct die_info *die;
24152
24153 gdb_assert (attr->form == DW_FORM_ref_sig8);
24154
a2ce51a0 24155 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
24156 /* sig_type will be NULL if the signatured type is missing from
24157 the debug info. */
24158 if (sig_type == NULL)
24159 {
24160 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
24161 " from DIE at %s [in module %s]"),
24162 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24163 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24164 }
24165
24166 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24167 if (die == NULL)
24168 {
24169 dump_die_for_error (src_die);
24170 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24171 " from DIE at %s [in module %s]"),
24172 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24173 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24174 }
24175
24176 return die;
24177}
24178
24179/* Get the type specified by SIGNATURE referenced in DIE/CU,
24180 reading in and processing the type unit if necessary. */
24181
24182static struct type *
24183get_signatured_type (struct die_info *die, ULONGEST signature,
24184 struct dwarf2_cu *cu)
24185{
976ca316 24186 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
24187 struct signatured_type *sig_type;
24188 struct dwarf2_cu *type_cu;
24189 struct die_info *type_die;
24190 struct type *type;
24191
a2ce51a0 24192 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
24193 /* sig_type will be NULL if the signatured type is missing from
24194 the debug info. */
24195 if (sig_type == NULL)
24196 {
b98664d3 24197 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24198 " from DIE at %s [in module %s]"),
24199 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24200 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24201 return build_error_marker_type (cu, die);
24202 }
24203
24204 /* If we already know the type we're done. */
976ca316 24205 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
24206 if (type != nullptr)
24207 return type;
ac9ec31b
DE
24208
24209 type_cu = cu;
24210 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24211 if (type_die != NULL)
24212 {
24213 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24214 is created. This is important, for example, because for c++ classes
24215 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24216 type = read_type_die (type_die, type_cu);
24217 if (type == NULL)
24218 {
b98664d3 24219 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24220 " referenced from DIE at %s [in module %s]"),
24221 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24222 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24223 type = build_error_marker_type (cu, die);
24224 }
24225 }
24226 else
24227 {
b98664d3 24228 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24229 " from DIE at %s [in module %s]"),
24230 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24231 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24232 type = build_error_marker_type (cu, die);
24233 }
e286671b 24234
976ca316 24235 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
24236
24237 return type;
24238}
24239
24240/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24241 reading in and processing the type unit if necessary. */
24242
24243static struct type *
ff39bb5e 24244get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24245 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24246{
24247 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 24248 if (attr->form_is_ref ())
ac9ec31b
DE
24249 {
24250 struct dwarf2_cu *type_cu = cu;
24251 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24252
24253 return read_type_die (type_die, type_cu);
24254 }
24255 else if (attr->form == DW_FORM_ref_sig8)
24256 {
630ed6b9 24257 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
24258 }
24259 else
24260 {
976ca316 24261 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24262
b98664d3 24263 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24264 " at %s [in module %s]"),
24265 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 24266 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24267 return build_error_marker_type (cu, die);
24268 }
348e048f
DE
24269}
24270
e5fe5e75 24271/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24272
24273static void
ab432490
SM
24274load_full_type_unit (dwarf2_per_cu_data *per_cu,
24275 dwarf2_per_objfile *per_objfile)
348e048f 24276{
52dc124a 24277 struct signatured_type *sig_type;
348e048f 24278
f4dc4d17 24279 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24280 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24281
6721b2ec
DE
24282 /* We have the per_cu, but we need the signatured_type.
24283 Fortunately this is an easy translation. */
24284 gdb_assert (per_cu->is_debug_types);
24285 sig_type = (struct signatured_type *) per_cu;
348e048f 24286
7188ed02 24287 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24288
ab432490 24289 read_signatured_type (sig_type, per_objfile);
348e048f 24290
7188ed02 24291 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24292}
24293
3019eac3
DE
24294/* Read in a signatured type and build its CU and DIEs.
24295 If the type is a stub for the real type in a DWO file,
24296 read in the real type from the DWO file as well. */
dee91e82
DE
24297
24298static void
ab432490
SM
24299read_signatured_type (signatured_type *sig_type,
24300 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24301{
24302 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24303
3019eac3 24304 gdb_assert (per_cu->is_debug_types);
7188ed02 24305 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24306
2e671100 24307 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24308
24309 if (!reader.dummy_p)
24310 {
24311 struct dwarf2_cu *cu = reader.cu;
24312 const gdb_byte *info_ptr = reader.info_ptr;
24313
24314 gdb_assert (cu->die_hash == NULL);
24315 cu->die_hash =
24316 htab_create_alloc_ex (cu->header.length / 12,
24317 die_hash,
24318 die_eq,
24319 NULL,
24320 &cu->comp_unit_obstack,
24321 hashtab_obstack_allocate,
24322 dummy_obstack_deallocate);
24323
3e225074 24324 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24325 reader.comp_unit_die->child
24326 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24327 reader.comp_unit_die);
24328 cu->dies = reader.comp_unit_die;
24329 /* comp_unit_die is not stored in die_hash, no need. */
24330
24331 /* We try not to read any attributes in this function, because
24332 not all CUs needed for references have been loaded yet, and
24333 symbol table processing isn't initialized. But we have to
24334 set the CU language, or we won't be able to build types
24335 correctly. Similarly, if we do not read the producer, we can
24336 not apply producer-specific interpretation. */
24337 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24338
24339 reader.keep ();
c0ab21c2
TT
24340 }
24341
7ee85ab1 24342 sig_type->per_cu.tu_read = 1;
c906108c
SS
24343}
24344
c906108c
SS
24345/* Decode simple location descriptions.
24346 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24347 the location and return the value. If COMPUTED is non-null, it is
24348 set to true to indicate that decoding was successful, and false
24349 otherwise. If COMPUTED is null, then this function may emit a
24350 complaint. */
c906108c
SS
24351
24352static CORE_ADDR
7d79de9a 24353decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24354{
5e22e966 24355 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24356 size_t i;
24357 size_t size = blk->size;
d521ce57 24358 const gdb_byte *data = blk->data;
21ae7a4d
JK
24359 CORE_ADDR stack[64];
24360 int stacki;
24361 unsigned int bytes_read, unsnd;
24362 gdb_byte op;
c906108c 24363
7d79de9a
TT
24364 if (computed != nullptr)
24365 *computed = false;
24366
21ae7a4d
JK
24367 i = 0;
24368 stacki = 0;
24369 stack[stacki] = 0;
24370 stack[++stacki] = 0;
24371
24372 while (i < size)
24373 {
24374 op = data[i++];
24375 switch (op)
24376 {
24377 case DW_OP_lit0:
24378 case DW_OP_lit1:
24379 case DW_OP_lit2:
24380 case DW_OP_lit3:
24381 case DW_OP_lit4:
24382 case DW_OP_lit5:
24383 case DW_OP_lit6:
24384 case DW_OP_lit7:
24385 case DW_OP_lit8:
24386 case DW_OP_lit9:
24387 case DW_OP_lit10:
24388 case DW_OP_lit11:
24389 case DW_OP_lit12:
24390 case DW_OP_lit13:
24391 case DW_OP_lit14:
24392 case DW_OP_lit15:
24393 case DW_OP_lit16:
24394 case DW_OP_lit17:
24395 case DW_OP_lit18:
24396 case DW_OP_lit19:
24397 case DW_OP_lit20:
24398 case DW_OP_lit21:
24399 case DW_OP_lit22:
24400 case DW_OP_lit23:
24401 case DW_OP_lit24:
24402 case DW_OP_lit25:
24403 case DW_OP_lit26:
24404 case DW_OP_lit27:
24405 case DW_OP_lit28:
24406 case DW_OP_lit29:
24407 case DW_OP_lit30:
24408 case DW_OP_lit31:
24409 stack[++stacki] = op - DW_OP_lit0;
24410 break;
f1bea926 24411
21ae7a4d
JK
24412 case DW_OP_reg0:
24413 case DW_OP_reg1:
24414 case DW_OP_reg2:
24415 case DW_OP_reg3:
24416 case DW_OP_reg4:
24417 case DW_OP_reg5:
24418 case DW_OP_reg6:
24419 case DW_OP_reg7:
24420 case DW_OP_reg8:
24421 case DW_OP_reg9:
24422 case DW_OP_reg10:
24423 case DW_OP_reg11:
24424 case DW_OP_reg12:
24425 case DW_OP_reg13:
24426 case DW_OP_reg14:
24427 case DW_OP_reg15:
24428 case DW_OP_reg16:
24429 case DW_OP_reg17:
24430 case DW_OP_reg18:
24431 case DW_OP_reg19:
24432 case DW_OP_reg20:
24433 case DW_OP_reg21:
24434 case DW_OP_reg22:
24435 case DW_OP_reg23:
24436 case DW_OP_reg24:
24437 case DW_OP_reg25:
24438 case DW_OP_reg26:
24439 case DW_OP_reg27:
24440 case DW_OP_reg28:
24441 case DW_OP_reg29:
24442 case DW_OP_reg30:
24443 case DW_OP_reg31:
24444 stack[++stacki] = op - DW_OP_reg0;
24445 if (i < size)
7d79de9a
TT
24446 {
24447 if (computed == nullptr)
24448 dwarf2_complex_location_expr_complaint ();
24449 else
24450 return 0;
24451 }
21ae7a4d 24452 break;
c906108c 24453
21ae7a4d
JK
24454 case DW_OP_regx:
24455 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24456 i += bytes_read;
24457 stack[++stacki] = unsnd;
24458 if (i < size)
7d79de9a
TT
24459 {
24460 if (computed == nullptr)
24461 dwarf2_complex_location_expr_complaint ();
24462 else
24463 return 0;
24464 }
21ae7a4d 24465 break;
c906108c 24466
21ae7a4d 24467 case DW_OP_addr:
c8a7a66f
TT
24468 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24469 &bytes_read);
21ae7a4d
JK
24470 i += bytes_read;
24471 break;
d53d4ac5 24472
21ae7a4d
JK
24473 case DW_OP_const1u:
24474 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24475 i += 1;
24476 break;
24477
24478 case DW_OP_const1s:
24479 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24480 i += 1;
24481 break;
24482
24483 case DW_OP_const2u:
24484 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24485 i += 2;
24486 break;
24487
24488 case DW_OP_const2s:
24489 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24490 i += 2;
24491 break;
d53d4ac5 24492
21ae7a4d
JK
24493 case DW_OP_const4u:
24494 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24495 i += 4;
24496 break;
24497
24498 case DW_OP_const4s:
24499 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24500 i += 4;
24501 break;
24502
585861ea
JK
24503 case DW_OP_const8u:
24504 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24505 i += 8;
24506 break;
24507
21ae7a4d
JK
24508 case DW_OP_constu:
24509 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24510 &bytes_read);
24511 i += bytes_read;
24512 break;
24513
24514 case DW_OP_consts:
24515 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24516 i += bytes_read;
24517 break;
24518
24519 case DW_OP_dup:
24520 stack[stacki + 1] = stack[stacki];
24521 stacki++;
24522 break;
24523
24524 case DW_OP_plus:
24525 stack[stacki - 1] += stack[stacki];
24526 stacki--;
24527 break;
24528
24529 case DW_OP_plus_uconst:
24530 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24531 &bytes_read);
24532 i += bytes_read;
24533 break;
24534
24535 case DW_OP_minus:
24536 stack[stacki - 1] -= stack[stacki];
24537 stacki--;
24538 break;
24539
24540 case DW_OP_deref:
24541 /* If we're not the last op, then we definitely can't encode
24542 this using GDB's address_class enum. This is valid for partial
24543 global symbols, although the variable's address will be bogus
24544 in the psymtab. */
24545 if (i < size)
7d79de9a
TT
24546 {
24547 if (computed == nullptr)
24548 dwarf2_complex_location_expr_complaint ();
24549 else
24550 return 0;
24551 }
21ae7a4d
JK
24552 break;
24553
dda83cd7 24554 case DW_OP_GNU_push_tls_address:
4aa4e28b 24555 case DW_OP_form_tls_address:
21ae7a4d
JK
24556 /* The top of the stack has the offset from the beginning
24557 of the thread control block at which the variable is located. */
24558 /* Nothing should follow this operator, so the top of stack would
24559 be returned. */
24560 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24561 address will be bogus in the psymtab. Make it always at least
24562 non-zero to not look as a variable garbage collected by linker
24563 which have DW_OP_addr 0. */
21ae7a4d 24564 if (i < size)
7d79de9a
TT
24565 {
24566 if (computed == nullptr)
24567 dwarf2_complex_location_expr_complaint ();
24568 else
24569 return 0;
24570 }
585861ea 24571 stack[stacki]++;
dda83cd7 24572 break;
21ae7a4d
JK
24573
24574 case DW_OP_GNU_uninit:
7d79de9a
TT
24575 if (computed != nullptr)
24576 return 0;
21ae7a4d
JK
24577 break;
24578
336d760d 24579 case DW_OP_addrx:
3019eac3 24580 case DW_OP_GNU_addr_index:
49f6c839 24581 case DW_OP_GNU_const_index:
3019eac3
DE
24582 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24583 &bytes_read);
24584 i += bytes_read;
24585 break;
24586
21ae7a4d 24587 default:
7d79de9a
TT
24588 if (computed == nullptr)
24589 {
24590 const char *name = get_DW_OP_name (op);
21ae7a4d 24591
7d79de9a
TT
24592 if (name)
24593 complaint (_("unsupported stack op: '%s'"),
24594 name);
24595 else
24596 complaint (_("unsupported stack op: '%02x'"),
24597 op);
24598 }
21ae7a4d
JK
24599
24600 return (stack[stacki]);
d53d4ac5 24601 }
3c6e0cb3 24602
21ae7a4d 24603 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24604 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24605 if (stacki >= ARRAY_SIZE (stack) - 1)
24606 {
7d79de9a
TT
24607 if (computed == nullptr)
24608 complaint (_("location description stack overflow"));
21ae7a4d
JK
24609 return 0;
24610 }
24611
24612 if (stacki <= 0)
24613 {
7d79de9a
TT
24614 if (computed == nullptr)
24615 complaint (_("location description stack underflow"));
21ae7a4d
JK
24616 return 0;
24617 }
24618 }
7d79de9a
TT
24619
24620 if (computed != nullptr)
24621 *computed = true;
21ae7a4d 24622 return (stack[stacki]);
c906108c
SS
24623}
24624
24625/* memory allocation interface */
24626
c906108c 24627static struct dwarf_block *
7b5a2f43 24628dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24629{
8d749320 24630 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24631}
24632
c906108c 24633static struct die_info *
b60c80d6 24634dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24635{
24636 struct die_info *die;
b60c80d6
DJ
24637 size_t size = sizeof (struct die_info);
24638
24639 if (num_attrs > 1)
24640 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24641
b60c80d6 24642 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24643 memset (die, 0, sizeof (struct die_info));
24644 return (die);
24645}
2e276125
JB
24646
24647\f
a036ba48 24648
c90ec28a 24649/* Macro support. */
cf2c3c16 24650
9eac9650
TT
24651/* An overload of dwarf_decode_macros that finds the correct section
24652 and ensures it is read in before calling the other overload. */
24653
24654static void
24655dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24656 int section_is_gnu)
24657{
976ca316
SM
24658 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24659 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24660 const struct line_header *lh = cu->line_header;
9eac9650
TT
24661 unsigned int offset_size = cu->header.offset_size;
24662 struct dwarf2_section_info *section;
24663 const char *section_name;
24664
24665 if (cu->dwo_unit != nullptr)
24666 {
24667 if (section_is_gnu)
24668 {
24669 section = &cu->dwo_unit->dwo_file->sections.macro;
24670 section_name = ".debug_macro.dwo";
24671 }
24672 else
24673 {
24674 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24675 section_name = ".debug_macinfo.dwo";
24676 }
24677 }
24678 else
24679 {
24680 if (section_is_gnu)
24681 {
976ca316 24682 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24683 section_name = ".debug_macro";
24684 }
24685 else
24686 {
976ca316 24687 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24688 section_name = ".debug_macinfo";
24689 }
24690 }
24691
24692 section->read (objfile);
24693 if (section->buffer == nullptr)
24694 {
24695 complaint (_("missing %s section"), section_name);
24696 return;
24697 }
24698
24699 buildsym_compunit *builder = cu->get_builder ();
24700
048fde1e 24701 struct dwarf2_section_info *str_offsets_section;
24702 struct dwarf2_section_info *str_section;
24703 ULONGEST str_offsets_base;
24704
24705 if (cu->dwo_unit != nullptr)
24706 {
24707 str_offsets_section = &cu->dwo_unit->dwo_file
24708 ->sections.str_offsets;
24709 str_section = &cu->dwo_unit->dwo_file->sections.str;
24710 str_offsets_base = cu->header.addr_size;
24711 }
24712 else
24713 {
24714 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24715 str_section = &per_objfile->per_bfd->str;
24716 str_offsets_base = *cu->str_offsets_base;
24717 }
24718
976ca316 24719 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24720 offset_size, offset, str_section, str_offsets_section,
24721 str_offsets_base, section_is_gnu);
9eac9650
TT
24722}
24723
3019eac3
DE
24724/* Return the .debug_loc section to use for CU.
24725 For DWO files use .debug_loc.dwo. */
24726
24727static struct dwarf2_section_info *
24728cu_debug_loc_section (struct dwarf2_cu *cu)
24729{
976ca316 24730 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24731
3019eac3 24732 if (cu->dwo_unit)
43988095
JK
24733 {
24734 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24735
43988095
JK
24736 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24737 }
976ca316
SM
24738 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24739 : &per_objfile->per_bfd->loc);
3019eac3
DE
24740}
24741
d0ce17d8
CT
24742/* Return the .debug_rnglists section to use for CU. */
24743static struct dwarf2_section_info *
24744cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24745{
24746 if (cu->header.version < 5)
24747 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24748 cu->header.version);
24749 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24750
24751 /* Make sure we read the .debug_rnglists section from the file that
24752 contains the DW_AT_ranges attribute we are reading. Normally that
24753 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24754 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24755 program. */
24756 if (cu->dwo_unit != nullptr
24757 && tag != DW_TAG_compile_unit
24758 && tag != DW_TAG_skeleton_unit)
24759 {
24760 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24761
24762 if (sections->rnglists.size > 0)
24763 return &sections->rnglists;
24764 else
24765 error (_(".debug_rnglists section is missing from .dwo file."));
24766 }
24767 return &dwarf2_per_objfile->per_bfd->rnglists;
24768}
24769
8cf6f0b1
TT
24770/* A helper function that fills in a dwarf2_loclist_baton. */
24771
24772static void
24773fill_in_loclist_baton (struct dwarf2_cu *cu,
24774 struct dwarf2_loclist_baton *baton,
ff39bb5e 24775 const struct attribute *attr)
8cf6f0b1 24776{
976ca316 24777 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24778 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24779
976ca316 24780 section->read (per_objfile->objfile);
8cf6f0b1 24781
976ca316 24782 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24783 baton->per_cu = cu->per_cu;
24784 gdb_assert (baton->per_cu);
24785 /* We don't know how long the location list is, but make sure we
24786 don't run off the edge of the section. */
d4df075e
TT
24787 baton->size = section->size - attr->as_unsigned ();
24788 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24789 if (cu->base_address.has_value ())
24790 baton->base_address = *cu->base_address;
24791 else
24792 baton->base_address = 0;
f664829e 24793 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24794}
24795
4c2df51b 24796static void
ff39bb5e 24797dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24798 struct dwarf2_cu *cu, int is_block)
4c2df51b 24799{
976ca316
SM
24800 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24801 struct objfile *objfile = per_objfile->objfile;
3019eac3 24802 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24803
cd6c91b4 24804 if (attr->form_is_section_offset ()
3019eac3 24805 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24806 the section. If so, fall through to the complaint in the
24807 other branch. */
d4df075e 24808 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24809 {
0d53c4c4 24810 struct dwarf2_loclist_baton *baton;
4c2df51b 24811
8d749320 24812 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24813
8cf6f0b1 24814 fill_in_loclist_baton (cu, baton, attr);
be391dca 24815
2b24b6e4 24816 if (!cu->base_address.has_value ())
b98664d3 24817 complaint (_("Location list used without "
3e43a32a 24818 "specifying the CU base address."));
4c2df51b 24819
f1e6e072
TT
24820 SYMBOL_ACLASS_INDEX (sym) = (is_block
24821 ? dwarf2_loclist_block_index
24822 : dwarf2_loclist_index);
0d53c4c4
DJ
24823 SYMBOL_LOCATION_BATON (sym) = baton;
24824 }
24825 else
24826 {
24827 struct dwarf2_locexpr_baton *baton;
24828
8d749320 24829 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24830 baton->per_objfile = per_objfile;
ae0d2f24
UW
24831 baton->per_cu = cu->per_cu;
24832 gdb_assert (baton->per_cu);
0d53c4c4 24833
4fc6c0d5 24834 if (attr->form_is_block ())
0d53c4c4
DJ
24835 {
24836 /* Note that we're just copying the block's data pointer
24837 here, not the actual data. We're still pointing into the
6502dd73
DJ
24838 info_buffer for SYM's objfile; right now we never release
24839 that buffer, but when we do clean up properly this may
24840 need to change. */
9d2246fc
TT
24841 struct dwarf_block *block = attr->as_block ();
24842 baton->size = block->size;
24843 baton->data = block->data;
0d53c4c4
DJ
24844 }
24845 else
24846 {
24847 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24848 sym->natural_name ());
0d53c4c4 24849 baton->size = 0;
0d53c4c4 24850 }
6e70227d 24851
f1e6e072
TT
24852 SYMBOL_ACLASS_INDEX (sym) = (is_block
24853 ? dwarf2_locexpr_block_index
24854 : dwarf2_locexpr_index);
0d53c4c4
DJ
24855 SYMBOL_LOCATION_BATON (sym) = baton;
24856 }
4c2df51b 24857}
6502dd73 24858
2e6a9f79 24859/* See read.h. */
96408a79 24860
2e6a9f79
SM
24861const comp_unit_head *
24862dwarf2_per_cu_data::get_header () const
96408a79 24863{
2e6a9f79
SM
24864 if (!m_header_read_in)
24865 {
24866 const gdb_byte *info_ptr
24867 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24868
2e6a9f79 24869 memset (&m_header, 0, sizeof (m_header));
96408a79 24870
2e6a9f79
SM
24871 read_comp_unit_head (&m_header, info_ptr, this->section,
24872 rcuh_kind::COMPILE);
a9f172c6
TT
24873
24874 m_header_read_in = true;
2e6a9f79 24875 }
96408a79 24876
2e6a9f79 24877 return &m_header;
96408a79
SA
24878}
24879
09ba997f 24880/* See read.h. */
ae0d2f24 24881
98714339 24882int
09ba997f 24883dwarf2_per_cu_data::addr_size () const
ae0d2f24 24884{
2e6a9f79 24885 return this->get_header ()->addr_size;
ae0d2f24
UW
24886}
24887
09ba997f 24888/* See read.h. */
9eae7c52
TT
24889
24890int
09ba997f 24891dwarf2_per_cu_data::offset_size () const
9eae7c52 24892{
2e6a9f79 24893 return this->get_header ()->offset_size;
96408a79
SA
24894}
24895
09ba997f 24896/* See read.h. */
96408a79
SA
24897
24898int
09ba997f 24899dwarf2_per_cu_data::ref_addr_size () const
96408a79 24900{
2e6a9f79 24901 const comp_unit_head *header = this->get_header ();
96408a79 24902
2e6a9f79
SM
24903 if (header->version == 2)
24904 return header->addr_size;
96408a79 24905 else
2e6a9f79 24906 return header->offset_size;
181cebd4
JK
24907}
24908
09ba997f 24909/* See read.h. */
9aa1f1e3 24910
09ba997f 24911struct type *
293e7e51 24912dwarf2_cu::addr_type () const
9a49df9d 24913{
293e7e51 24914 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24915 struct type *void_type = objfile_type (objfile)->builtin_void;
24916 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24917 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24918
24919 if (TYPE_LENGTH (addr_type) == addr_size)
24920 return addr_type;
24921
c6d940a9 24922 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24923 return addr_type;
24924}
24925
22b6cd70
TT
24926/* A helper function for dwarf2_find_containing_comp_unit that returns
24927 the index of the result, and that searches a vector. It will
24928 return a result even if the offset in question does not actually
24929 occur in any CU. This is separate so that it can be unit
24930 tested. */
ae038cb0 24931
22b6cd70
TT
24932static int
24933dwarf2_find_containing_comp_unit
24934 (sect_offset sect_off,
24935 unsigned int offset_in_dwz,
24936 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24937{
ae038cb0
DJ
24938 int low, high;
24939
ae038cb0 24940 low = 0;
22b6cd70 24941 high = all_comp_units.size () - 1;
ae038cb0
DJ
24942 while (high > low)
24943 {
36586728 24944 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24945 int mid = low + (high - low) / 2;
9a619af0 24946
22b6cd70 24947 mid_cu = all_comp_units[mid];
36586728 24948 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24949 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24950 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24951 high = mid;
24952 else
24953 low = mid + 1;
24954 }
24955 gdb_assert (low == high);
22b6cd70
TT
24956 return low;
24957}
24958
24959/* Locate the .debug_info compilation unit from CU's objfile which contains
24960 the DIE at OFFSET. Raises an error on failure. */
24961
24962static struct dwarf2_per_cu_data *
24963dwarf2_find_containing_comp_unit (sect_offset sect_off,
24964 unsigned int offset_in_dwz,
976ca316 24965 dwarf2_per_objfile *per_objfile)
22b6cd70 24966{
976ca316
SM
24967 int low = dwarf2_find_containing_comp_unit
24968 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24969 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24970
45b8ae0c 24971 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24972 {
36586728 24973 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24974 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24975 "offset %s [in module %s]"),
24976 sect_offset_str (sect_off),
976ca316 24977 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24978
976ca316 24979 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24980 <= sect_off);
976ca316 24981 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
24982 }
24983 else
24984 {
976ca316 24985 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24986 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24987 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24988 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24989 return this_cu;
24990 }
24991}
24992
22b6cd70
TT
24993#if GDB_SELF_TEST
24994
24995namespace selftests {
24996namespace find_containing_comp_unit {
24997
24998static void
24999run_test ()
25000{
25001 struct dwarf2_per_cu_data one {};
25002 struct dwarf2_per_cu_data two {};
25003 struct dwarf2_per_cu_data three {};
25004 struct dwarf2_per_cu_data four {};
25005
25006 one.length = 5;
25007 two.sect_off = sect_offset (one.length);
25008 two.length = 7;
25009
25010 three.length = 5;
25011 three.is_dwz = 1;
25012 four.sect_off = sect_offset (three.length);
25013 four.length = 7;
25014 four.is_dwz = 1;
25015
25016 std::vector<dwarf2_per_cu_data *> units;
25017 units.push_back (&one);
25018 units.push_back (&two);
25019 units.push_back (&three);
25020 units.push_back (&four);
25021
25022 int result;
25023
25024 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25025 SELF_CHECK (units[result] == &one);
25026 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25027 SELF_CHECK (units[result] == &one);
25028 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25029 SELF_CHECK (units[result] == &two);
25030
25031 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25032 SELF_CHECK (units[result] == &three);
25033 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25034 SELF_CHECK (units[result] == &three);
25035 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25036 SELF_CHECK (units[result] == &four);
25037}
25038
25039}
25040}
25041
25042#endif /* GDB_SELF_TEST */
25043
9e021579 25044/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 25045
9e021579
SM
25046dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25047 dwarf2_per_objfile *per_objfile)
25048 : per_cu (per_cu),
25049 per_objfile (per_objfile),
9068261f
AB
25050 mark (false),
25051 has_loclist (false),
25052 checked_producer (false),
25053 producer_is_gxx_lt_4_6 (false),
25054 producer_is_gcc_lt_4_3 (false),
eb77c9df 25055 producer_is_icc (false),
9068261f 25056 producer_is_icc_lt_14 (false),
c258c396 25057 producer_is_codewarrior (false),
9068261f 25058 processing_has_namespace_info (false)
93311388 25059{
9816fde3
JK
25060}
25061
25062/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25063
25064static void
95554aad
TT
25065prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25066 enum language pretend_language)
9816fde3
JK
25067{
25068 struct attribute *attr;
25069
25070 /* Set the language we're debugging. */
25071 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25072 if (attr != nullptr)
529908cb 25073 set_cu_language (attr->constant_value (0), cu);
9816fde3 25074 else
9cded63f 25075 {
95554aad 25076 cu->language = pretend_language;
9cded63f
TT
25077 cu->language_defn = language_def (cu->language);
25078 }
dee91e82 25079
7d45c7c3 25080 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25081}
25082
7188ed02 25083/* See read.h. */
ae038cb0 25084
7188ed02
SM
25085dwarf2_cu *
25086dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25087{
7188ed02
SM
25088 auto it = m_dwarf2_cus.find (per_cu);
25089 if (it == m_dwarf2_cus.end ())
25090 return nullptr;
ae038cb0 25091
7188ed02
SM
25092 return it->second;
25093}
25094
25095/* See read.h. */
25096
25097void
25098dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25099{
25100 gdb_assert (this->get_cu (per_cu) == nullptr);
25101
25102 m_dwarf2_cus[per_cu] = cu;
25103}
25104
25105/* See read.h. */
25106
25107void
25108dwarf2_per_objfile::age_comp_units ()
25109{
17e593e9
SM
25110 dwarf_read_debug_printf_v ("running");
25111
08ac5771
SM
25112 /* This is not expected to be called in the middle of CU expansion. There is
25113 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25114 loaded in memory. Calling age_comp_units while the queue is in use could
25115 make us free the DIEs for a CU that is in the queue and therefore break
25116 that invariant. */
25117 gdb_assert (!this->per_bfd->queue.has_value ());
25118
7188ed02
SM
25119 /* Start by clearing all marks. */
25120 for (auto pair : m_dwarf2_cus)
25121 pair.second->mark = false;
25122
25123 /* Traverse all CUs, mark them and their dependencies if used recently
25124 enough. */
25125 for (auto pair : m_dwarf2_cus)
ae038cb0 25126 {
7188ed02
SM
25127 dwarf2_cu *cu = pair.second;
25128
25129 cu->last_used++;
25130 if (cu->last_used <= dwarf_max_cache_age)
25131 dwarf2_mark (cu);
ae038cb0
DJ
25132 }
25133
7188ed02
SM
25134 /* Delete all CUs still not marked. */
25135 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 25136 {
7188ed02 25137 dwarf2_cu *cu = it->second;
ae038cb0 25138
7188ed02 25139 if (!cu->mark)
ae038cb0 25140 {
17e593e9
SM
25141 dwarf_read_debug_printf_v ("deleting old CU %s",
25142 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
25143 delete cu;
25144 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
25145 }
25146 else
7188ed02 25147 it++;
ae038cb0
DJ
25148 }
25149}
25150
7188ed02 25151/* See read.h. */
ae038cb0 25152
7188ed02
SM
25153void
25154dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25155{
7188ed02
SM
25156 auto it = m_dwarf2_cus.find (per_cu);
25157 if (it == m_dwarf2_cus.end ())
25158 return;
ae038cb0 25159
7188ed02 25160 delete it->second;
ae038cb0 25161
7188ed02
SM
25162 m_dwarf2_cus.erase (it);
25163}
ae038cb0 25164
7188ed02
SM
25165dwarf2_per_objfile::~dwarf2_per_objfile ()
25166{
25167 remove_all_cus ();
ae038cb0
DJ
25168}
25169
dee91e82
DE
25170/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25171 We store these in a hash table separate from the DIEs, and preserve them
25172 when the DIEs are flushed out of cache.
25173
25174 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25175 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25176 or the type may come from a DWO file. Furthermore, while it's more logical
25177 to use per_cu->section+offset, with Fission the section with the data is in
25178 the DWO file but we don't know that section at the point we need it.
25179 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25180 because we can enter the lookup routine, get_die_type_at_offset, from
25181 outside this file, and thus won't necessarily have PER_CU->cu.
25182 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25183
dee91e82 25184struct dwarf2_per_cu_offset_and_type
1c379e20 25185{
dee91e82 25186 const struct dwarf2_per_cu_data *per_cu;
9c541725 25187 sect_offset sect_off;
1c379e20
DJ
25188 struct type *type;
25189};
25190
dee91e82 25191/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25192
25193static hashval_t
dee91e82 25194per_cu_offset_and_type_hash (const void *item)
1c379e20 25195{
9a3c8263
SM
25196 const struct dwarf2_per_cu_offset_and_type *ofs
25197 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25198
9c541725 25199 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25200}
25201
dee91e82 25202/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25203
25204static int
dee91e82 25205per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25206{
9a3c8263
SM
25207 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25208 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25209 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25210 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25211
dee91e82 25212 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25213 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25214}
25215
25216/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25217 table if necessary. For convenience, return TYPE.
25218
25219 The DIEs reading must have careful ordering to:
85102364 25220 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25221 reading current DIE.
25222 * Not trying to dereference contents of still incompletely read in types
25223 while reading in other DIEs.
25224 * Enable referencing still incompletely read in types just by a pointer to
25225 the type without accessing its fields.
25226
25227 Therefore caller should follow these rules:
25228 * Try to fetch any prerequisite types we may need to build this DIE type
25229 before building the type and calling set_die_type.
e71ec853 25230 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25231 possible before fetching more types to complete the current type.
25232 * Make the type as complete as possible before fetching more types. */
1c379e20 25233
f792889a 25234static struct type *
57567375
TT
25235set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25236 bool skip_data_location)
1c379e20 25237{
976ca316 25238 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 25239 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 25240 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
25241 struct attribute *attr;
25242 struct dynamic_prop prop;
1c379e20 25243
b4ba55a1
JB
25244 /* For Ada types, make sure that the gnat-specific data is always
25245 initialized (if not already set). There are a few types where
25246 we should not be doing so, because the type-specific area is
25247 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25248 where the type-specific area is used to store the floatformat).
25249 But this is not a problem, because the gnat-specific information
25250 is actually not needed for these types. */
25251 if (need_gnat_info (cu)
78134374
SM
25252 && type->code () != TYPE_CODE_FUNC
25253 && type->code () != TYPE_CODE_FLT
25254 && type->code () != TYPE_CODE_METHODPTR
25255 && type->code () != TYPE_CODE_MEMBERPTR
25256 && type->code () != TYPE_CODE_METHOD
09584414 25257 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
25258 && !HAVE_GNAT_AUX_INFO (type))
25259 INIT_GNAT_SPECIFIC (type);
25260
3f2f83dd
KB
25261 /* Read DW_AT_allocated and set in type. */
25262 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 25263 if (attr != NULL)
3f2f83dd 25264 {
293e7e51 25265 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25266 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25267 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 25268 }
3f2f83dd
KB
25269
25270 /* Read DW_AT_associated and set in type. */
25271 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 25272 if (attr != NULL)
3f2f83dd 25273 {
293e7e51 25274 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25275 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25276 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 25277 }
3f2f83dd 25278
3cdcd0ce 25279 /* Read DW_AT_data_location and set in type. */
57567375
TT
25280 if (!skip_data_location)
25281 {
25282 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25283 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25284 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25285 }
3cdcd0ce 25286
976ca316
SM
25287 if (per_objfile->die_type_hash == NULL)
25288 per_objfile->die_type_hash
0335378b
TT
25289 = htab_up (htab_create_alloc (127,
25290 per_cu_offset_and_type_hash,
25291 per_cu_offset_and_type_eq,
25292 NULL, xcalloc, xfree));
1c379e20 25293
dee91e82 25294 ofs.per_cu = cu->per_cu;
9c541725 25295 ofs.sect_off = die->sect_off;
1c379e20 25296 ofs.type = type;
dee91e82 25297 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25298 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25299 if (*slot)
b98664d3 25300 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25301 sect_offset_str (die->sect_off));
8d749320
SM
25302 *slot = XOBNEW (&objfile->objfile_obstack,
25303 struct dwarf2_per_cu_offset_and_type);
1c379e20 25304 **slot = ofs;
f792889a 25305 return type;
1c379e20
DJ
25306}
25307
9c541725 25308/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25309 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25310
25311static struct type *
9c541725 25312get_die_type_at_offset (sect_offset sect_off,
aa66c379 25313 dwarf2_per_cu_data *per_cu,
976ca316 25314 dwarf2_per_objfile *per_objfile)
1c379e20 25315{
dee91e82 25316 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25317
976ca316 25318 if (per_objfile->die_type_hash == NULL)
f792889a 25319 return NULL;
1c379e20 25320
dee91e82 25321 ofs.per_cu = per_cu;
9c541725 25322 ofs.sect_off = sect_off;
9a3c8263 25323 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25324 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25325 if (slot)
25326 return slot->type;
25327 else
25328 return NULL;
25329}
25330
02142a6c 25331/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25332 or return NULL if DIE does not have a saved type. */
25333
25334static struct type *
25335get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25336{
aa66c379 25337 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25338}
25339
10b3939b
DJ
25340/* Add a dependence relationship from CU to REF_PER_CU. */
25341
25342static void
25343dwarf2_add_dependence (struct dwarf2_cu *cu,
25344 struct dwarf2_per_cu_data *ref_per_cu)
25345{
25346 void **slot;
25347
25348 if (cu->dependencies == NULL)
25349 cu->dependencies
25350 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25351 NULL, &cu->comp_unit_obstack,
25352 hashtab_obstack_allocate,
25353 dummy_obstack_deallocate);
25354
25355 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25356 if (*slot == NULL)
25357 *slot = ref_per_cu;
25358}
1c379e20 25359
f504f079
DE
25360/* Subroutine of dwarf2_mark to pass to htab_traverse.
25361 Set the mark field in every compilation unit in the
7188ed02
SM
25362 cache that we must keep because we are keeping CU.
25363
25364 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25365
10b3939b
DJ
25366static int
25367dwarf2_mark_helper (void **slot, void *data)
25368{
7188ed02
SM
25369 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25370 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25371 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25372
25373 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25374 reading of the chain. As such dependencies remain valid it is not much
25375 useful to track and undo them during QUIT cleanups. */
7188ed02 25376 if (cu == nullptr)
d07ed419
JK
25377 return 1;
25378
7188ed02 25379 if (cu->mark)
10b3939b 25380 return 1;
10b3939b 25381
7188ed02
SM
25382 cu->mark = true;
25383
25384 if (cu->dependencies != nullptr)
25385 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25386
25387 return 1;
25388}
25389
f504f079
DE
25390/* Set the mark field in CU and in every other compilation unit in the
25391 cache that we must keep because we are keeping CU. */
25392
ae038cb0
DJ
25393static void
25394dwarf2_mark (struct dwarf2_cu *cu)
25395{
25396 if (cu->mark)
25397 return;
7188ed02 25398
9068261f 25399 cu->mark = true;
ae038cb0 25400
7188ed02
SM
25401 if (cu->dependencies != nullptr)
25402 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25403}
25404
72bf9492
DJ
25405/* Trivial hash function for partial_die_info: the hash value of a DIE
25406 is its offset in .debug_info for this objfile. */
25407
25408static hashval_t
25409partial_die_hash (const void *item)
25410{
9a3c8263
SM
25411 const struct partial_die_info *part_die
25412 = (const struct partial_die_info *) item;
9a619af0 25413
9c541725 25414 return to_underlying (part_die->sect_off);
72bf9492
DJ
25415}
25416
25417/* Trivial comparison function for partial_die_info structures: two DIEs
25418 are equal if they have the same offset. */
25419
25420static int
25421partial_die_eq (const void *item_lhs, const void *item_rhs)
25422{
9a3c8263
SM
25423 const struct partial_die_info *part_die_lhs
25424 = (const struct partial_die_info *) item_lhs;
25425 const struct partial_die_info *part_die_rhs
25426 = (const struct partial_die_info *) item_rhs;
9a619af0 25427
9c541725 25428 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25429}
25430
3c3bb058
AB
25431struct cmd_list_element *set_dwarf_cmdlist;
25432struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25433
9291a0cd 25434static void
cd4fb1b2
SM
25435show_check_physname (struct ui_file *file, int from_tty,
25436 struct cmd_list_element *c, const char *value)
9291a0cd 25437{
cd4fb1b2
SM
25438 fprintf_filtered (file,
25439 _("Whether to check \"physname\" is %s.\n"),
25440 value);
9291a0cd
TT
25441}
25442
6c265988 25443void _initialize_dwarf2_read ();
cd4fb1b2 25444void
6c265988 25445_initialize_dwarf2_read ()
9291a0cd 25446{
0743fc83 25447 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25448Set DWARF specific variables.\n\
590042fc 25449Configure DWARF variables such as the cache size."),
0743fc83
TT
25450 &set_dwarf_cmdlist, "maintenance set dwarf ",
25451 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25452
0743fc83 25453 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25454Show DWARF specific variables.\n\
25455Show DWARF variables such as the cache size."),
0743fc83
TT
25456 &show_dwarf_cmdlist, "maintenance show dwarf ",
25457 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25458
cd4fb1b2
SM
25459 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25460 &dwarf_max_cache_age, _("\
25461Set the upper bound on the age of cached DWARF compilation units."), _("\
25462Show the upper bound on the age of cached DWARF compilation units."), _("\
25463A higher limit means that cached compilation units will be stored\n\
25464in memory longer, and more total memory will be used. Zero disables\n\
25465caching, which can slow down startup."),
25466 NULL,
25467 show_dwarf_max_cache_age,
25468 &set_dwarf_cmdlist,
25469 &show_dwarf_cmdlist);
156942c7 25470
cd4fb1b2
SM
25471 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25472Set debugging of the DWARF reader."), _("\
25473Show debugging of the DWARF reader."), _("\
25474When enabled (non-zero), debugging messages are printed during DWARF\n\
25475reading and symtab expansion. A value of 1 (one) provides basic\n\
25476information. A value greater than 1 provides more verbose information."),
25477 NULL,
25478 NULL,
25479 &setdebuglist, &showdebuglist);
9291a0cd 25480
cd4fb1b2
SM
25481 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25482Set debugging of the DWARF DIE reader."), _("\
25483Show debugging of the DWARF DIE reader."), _("\
25484When enabled (non-zero), DIEs are dumped after they are read in.\n\
25485The value is the maximum depth to print."),
25486 NULL,
25487 NULL,
25488 &setdebuglist, &showdebuglist);
9291a0cd 25489
cd4fb1b2
SM
25490 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25491Set debugging of the dwarf line reader."), _("\
25492Show debugging of the dwarf line reader."), _("\
25493When enabled (non-zero), line number entries are dumped as they are read in.\n\
25494A value of 1 (one) provides basic information.\n\
25495A value greater than 1 provides more verbose information."),
25496 NULL,
25497 NULL,
25498 &setdebuglist, &showdebuglist);
437afbb8 25499
cd4fb1b2
SM
25500 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25501Set cross-checking of \"physname\" code against demangler."), _("\
25502Show cross-checking of \"physname\" code against demangler."), _("\
25503When enabled, GDB's internal \"physname\" code is checked against\n\
25504the demangler."),
25505 NULL, show_check_physname,
25506 &setdebuglist, &showdebuglist);
900e11f9 25507
e615022a
DE
25508 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25509 no_class, &use_deprecated_index_sections, _("\
25510Set whether to use deprecated gdb_index sections."), _("\
25511Show whether to use deprecated gdb_index sections."), _("\
25512When enabled, deprecated .gdb_index sections are used anyway.\n\
25513Normally they are ignored either because of a missing feature or\n\
25514performance issue.\n\
25515Warning: This option must be enabled before gdb reads the file."),
25516 NULL,
25517 NULL,
25518 &setlist, &showlist);
25519
f1e6e072
TT
25520 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25521 &dwarf2_locexpr_funcs);
25522 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25523 &dwarf2_loclist_funcs);
25524
25525 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25526 &dwarf2_block_frame_base_locexpr_funcs);
25527 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25528 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25529
25530#if GDB_SELF_TEST
25531 selftests::register_test ("dw2_expand_symtabs_matching",
25532 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25533 selftests::register_test ("dwarf2_find_containing_comp_unit",
25534 selftests::find_containing_comp_unit::run_test);
c62446b1 25535#endif
6502dd73 25536}
This page took 8.777817 seconds and 4 git commands to generate.