Simplify quick_symbol_functions::map_matching_symbols
[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
8a91fbdf
SM
1954 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1955 BFD doesn't require relocations.
1956
1957 We don't share with objfiles for which -readnow was requested,
1958 because it would complicate things when loading the same BFD with
1959 -readnow and then without -readnow. */
1960 if (!gdb_bfd_requires_relocations (objfile->obfd)
1961 && (objfile->flags & OBJF_READNOW) == 0)
17ee85fc
TT
1962 {
1963 /* See if one has been created for this BFD yet. */
1964 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1965
1966 if (per_bfd == nullptr)
1967 {
1968 /* No, create it now. */
1969 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1970 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1971 }
1972 }
1973 else
1974 {
1975 /* No sharing possible, create one specifically for this objfile. */
1976 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1977 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1978 }
5989a64e 1979
976ca316 1980 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1981 }
5bfd760d 1982
976ca316
SM
1983 return (!per_objfile->per_bfd->info.is_virtual
1984 && per_objfile->per_bfd->info.s.section != NULL
1985 && !per_objfile->per_bfd->abbrev.is_virtual
1986 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1987}
1988
330cdd98 1989/* See declaration. */
c906108c 1990
330cdd98 1991void
5989a64e
SM
1992dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1993 const dwarf2_debug_sections &names)
c906108c 1994{
fd361982 1995 flagword aflag = bfd_section_flags (sectp);
251d32d9 1996
dc7650b8
JK
1997 if ((aflag & SEC_HAS_CONTENTS) == 0)
1998 {
1999 }
950b7495
KS
2000 else if (elf_section_data (sectp)->this_hdr.sh_size
2001 > bfd_get_file_size (abfd))
2002 {
2003 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2004 warning (_("Discarding section %s which has a section size (%s"
2005 ") larger than the file size [in module %s]"),
2006 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2007 bfd_get_filename (abfd));
2008 }
fbedd546 2009 else if (names.info.matches (sectp->name))
c906108c 2010 {
330cdd98 2011 this->info.s.section = sectp;
fd361982 2012 this->info.size = bfd_section_size (sectp);
c906108c 2013 }
fbedd546 2014 else if (names.abbrev.matches (sectp->name))
c906108c 2015 {
330cdd98 2016 this->abbrev.s.section = sectp;
fd361982 2017 this->abbrev.size = bfd_section_size (sectp);
c906108c 2018 }
fbedd546 2019 else if (names.line.matches (sectp->name))
c906108c 2020 {
330cdd98 2021 this->line.s.section = sectp;
fd361982 2022 this->line.size = bfd_section_size (sectp);
c906108c 2023 }
fbedd546 2024 else if (names.loc.matches (sectp->name))
c906108c 2025 {
330cdd98 2026 this->loc.s.section = sectp;
fd361982 2027 this->loc.size = bfd_section_size (sectp);
c906108c 2028 }
fbedd546 2029 else if (names.loclists.matches (sectp->name))
43988095 2030 {
330cdd98 2031 this->loclists.s.section = sectp;
fd361982 2032 this->loclists.size = bfd_section_size (sectp);
43988095 2033 }
fbedd546 2034 else if (names.macinfo.matches (sectp->name))
c906108c 2035 {
330cdd98 2036 this->macinfo.s.section = sectp;
fd361982 2037 this->macinfo.size = bfd_section_size (sectp);
c906108c 2038 }
fbedd546 2039 else if (names.macro.matches (sectp->name))
cf2c3c16 2040 {
330cdd98 2041 this->macro.s.section = sectp;
fd361982 2042 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2043 }
fbedd546 2044 else if (names.str.matches (sectp->name))
c906108c 2045 {
330cdd98 2046 this->str.s.section = sectp;
fd361982 2047 this->str.size = bfd_section_size (sectp);
c906108c 2048 }
fbedd546 2049 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
2050 {
2051 this->str_offsets.s.section = sectp;
2052 this->str_offsets.size = bfd_section_size (sectp);
2053 }
fbedd546 2054 else if (names.line_str.matches (sectp->name))
43988095 2055 {
330cdd98 2056 this->line_str.s.section = sectp;
fd361982 2057 this->line_str.size = bfd_section_size (sectp);
43988095 2058 }
fbedd546 2059 else if (names.addr.matches (sectp->name))
3019eac3 2060 {
330cdd98 2061 this->addr.s.section = sectp;
fd361982 2062 this->addr.size = bfd_section_size (sectp);
3019eac3 2063 }
fbedd546 2064 else if (names.frame.matches (sectp->name))
b6af0555 2065 {
330cdd98 2066 this->frame.s.section = sectp;
fd361982 2067 this->frame.size = bfd_section_size (sectp);
b6af0555 2068 }
fbedd546 2069 else if (names.eh_frame.matches (sectp->name))
b6af0555 2070 {
330cdd98 2071 this->eh_frame.s.section = sectp;
fd361982 2072 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2073 }
fbedd546 2074 else if (names.ranges.matches (sectp->name))
af34e669 2075 {
330cdd98 2076 this->ranges.s.section = sectp;
fd361982 2077 this->ranges.size = bfd_section_size (sectp);
af34e669 2078 }
fbedd546 2079 else if (names.rnglists.matches (sectp->name))
43988095 2080 {
330cdd98 2081 this->rnglists.s.section = sectp;
fd361982 2082 this->rnglists.size = bfd_section_size (sectp);
43988095 2083 }
fbedd546 2084 else if (names.types.matches (sectp->name))
348e048f 2085 {
8b70b953
TT
2086 struct dwarf2_section_info type_section;
2087
2088 memset (&type_section, 0, sizeof (type_section));
049412e3 2089 type_section.s.section = sectp;
fd361982 2090 type_section.size = bfd_section_size (sectp);
8b70b953 2091
fd5866f6 2092 this->types.push_back (type_section);
348e048f 2093 }
fbedd546 2094 else if (names.gdb_index.matches (sectp->name))
9291a0cd 2095 {
330cdd98 2096 this->gdb_index.s.section = sectp;
fd361982 2097 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2098 }
fbedd546 2099 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
2100 {
2101 this->debug_names.s.section = sectp;
fd361982 2102 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 2103 }
fbedd546 2104 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
2105 {
2106 this->debug_aranges.s.section = sectp;
fd361982 2107 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2108 }
dce234bc 2109
fd361982
AM
2110 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2111 && bfd_section_vma (sectp) == 0)
330cdd98 2112 this->has_section_at_zero = true;
c906108c
SS
2113}
2114
dce234bc 2115/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2116 SECTION_NAME. */
af34e669 2117
dce234bc 2118void
3017a003 2119dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2120 enum dwarf2_section_enum sect,
2121 asection **sectp, const gdb_byte **bufp,
2122 bfd_size_type *sizep)
dce234bc 2123{
976ca316 2124 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2125 struct dwarf2_section_info *info;
a3b2a86b
TT
2126
2127 /* We may see an objfile without any DWARF, in which case we just
2128 return nothing. */
976ca316 2129 if (per_objfile == NULL)
a3b2a86b
TT
2130 {
2131 *sectp = NULL;
2132 *bufp = NULL;
2133 *sizep = 0;
2134 return;
2135 }
3017a003
TG
2136 switch (sect)
2137 {
2138 case DWARF2_DEBUG_FRAME:
976ca316 2139 info = &per_objfile->per_bfd->frame;
3017a003
TG
2140 break;
2141 case DWARF2_EH_FRAME:
976ca316 2142 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2143 break;
2144 default:
2145 gdb_assert_not_reached ("unexpected section");
2146 }
dce234bc 2147
96b79293 2148 info->read (objfile);
dce234bc 2149
96b79293 2150 *sectp = info->get_bfd_section ();
dce234bc
PP
2151 *bufp = info->buffer;
2152 *sizep = info->size;
2153}
2154
9291a0cd 2155\f
39298a5d 2156/* DWARF quick_symbol_functions support. */
7b9f3c50
DE
2157
2158/* TUs can share .debug_line entries, and there can be a lot more TUs than
2159 unique line tables, so we maintain a separate table of all .debug_line
2160 derived entries to support the sharing.
2161 All the quick functions need is the list of file names. We discard the
2162 line_header when we're done and don't need to record it here. */
2163struct quick_file_names
2164{
094b34ac
DE
2165 /* The data used to construct the hash key. */
2166 struct stmt_list_hash hash;
7b9f3c50
DE
2167
2168 /* The number of entries in file_names, real_names. */
2169 unsigned int num_file_names;
2170
2171 /* The file names from the line table, after being run through
2172 file_full_name. */
2173 const char **file_names;
2174
2175 /* The file names from the line table after being run through
2176 gdb_realpath. These are computed lazily. */
2177 const char **real_names;
2178};
2179
2180/* When using the index (and thus not using psymtabs), each CU has an
2181 object of this type. This is used to hold information needed by
2182 the various "quick" methods. */
2183struct dwarf2_per_cu_quick_data
2184{
2185 /* The file table. This can be NULL if there was no file table
2186 or it's currently not read in.
5989a64e 2187 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2188 struct quick_file_names *file_names;
2189
7b9f3c50
DE
2190 /* A temporary mark bit used when iterating over all CUs in
2191 expand_symtabs_matching. */
2192 unsigned int mark : 1;
2193
2194 /* True if we've tried to read the file table and found there isn't one.
2195 There will be no point in trying to read it again next time. */
2196 unsigned int no_file_data : 1;
2197};
2198
edc02ceb
TT
2199/* A subclass of psymbol_functions that arranges to read the DWARF
2200 partial symbols when needed. */
2201struct lazy_dwarf_reader : public psymbol_functions
2202{
2203 using psymbol_functions::psymbol_functions;
2204
2205 bool can_lazily_read_symbols () override
2206 {
2207 return true;
2208 }
2209
2210 void read_partial_symbols (struct objfile *objfile) override
2211 {
2212 if (dwarf2_has_info (objfile, nullptr))
2213 dwarf2_build_psymtabs (objfile, this);
2214 }
2215};
2216
2217static quick_symbol_functions_up
2218make_lazy_dwarf_reader ()
2219{
2220 return quick_symbol_functions_up (new lazy_dwarf_reader);
2221}
2222
39298a5d
TT
2223struct dwarf2_base_index_functions : public quick_symbol_functions
2224{
2225 bool has_symbols (struct objfile *objfile) override;
2226
2227 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2228
2229 void forget_cached_source_info (struct objfile *objfile) override;
2230
39298a5d
TT
2231 enum language lookup_global_symbol_language (struct objfile *objfile,
2232 const char *name,
2233 domain_enum domain,
2234 bool *symbol_found_p) override
2235 {
2236 *symbol_found_p = false;
2237 return language_unknown;
2238 }
2239
4829711b 2240 void print_stats (struct objfile *objfile, bool print_bcache) override;
39298a5d
TT
2241
2242 void expand_all_symtabs (struct objfile *objfile) override;
2243
39298a5d
TT
2244 struct compunit_symtab *find_pc_sect_compunit_symtab
2245 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2246 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2247
2248 struct compunit_symtab *find_compunit_symtab_by_address
2249 (struct objfile *objfile, CORE_ADDR address) override
2250 {
2251 return nullptr;
2252 }
2253
2254 void map_symbol_filenames (struct objfile *objfile,
f4655dee
TT
2255 gdb::function_view<symbol_filename_ftype> fun,
2256 bool need_fullname) override;
39298a5d
TT
2257};
2258
2259struct dwarf2_gdb_index : public dwarf2_base_index_functions
2260{
39298a5d
TT
2261 void dump (struct objfile *objfile) override;
2262
0b7b2c2a 2263 void expand_matching_symbols
39298a5d
TT
2264 (struct objfile *,
2265 const lookup_name_info &lookup_name,
2266 domain_enum domain,
2267 int global,
39298a5d
TT
2268 symbol_compare_ftype *ordered_compare) override;
2269
df35e626 2270 bool expand_symtabs_matching
39298a5d
TT
2271 (struct objfile *objfile,
2272 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2273 const lookup_name_info *lookup_name,
2274 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2275 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2276 block_search_flags search_flags,
3bfa51a7 2277 domain_enum domain,
39298a5d
TT
2278 enum search_domain kind) override;
2279};
2280
2281struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2282{
39298a5d
TT
2283 void dump (struct objfile *objfile) override;
2284
0b7b2c2a 2285 void expand_matching_symbols
39298a5d
TT
2286 (struct objfile *,
2287 const lookup_name_info &lookup_name,
2288 domain_enum domain,
2289 int global,
39298a5d
TT
2290 symbol_compare_ftype *ordered_compare) override;
2291
df35e626 2292 bool expand_symtabs_matching
39298a5d
TT
2293 (struct objfile *objfile,
2294 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2295 const lookup_name_info *lookup_name,
2296 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2297 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2298 block_search_flags search_flags,
3bfa51a7 2299 domain_enum domain,
39298a5d
TT
2300 enum search_domain kind) override;
2301};
2302
edc02ceb 2303static quick_symbol_functions_up
39298a5d
TT
2304make_dwarf_gdb_index ()
2305{
2306 return quick_symbol_functions_up (new dwarf2_gdb_index);
2307}
2308
edc02ceb 2309static quick_symbol_functions_up
39298a5d
TT
2310make_dwarf_debug_names ()
2311{
2312 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2313}
2314
094b34ac
DE
2315/* Utility hash function for a stmt_list_hash. */
2316
2317static hashval_t
2318hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2319{
2320 hashval_t v = 0;
2321
2322 if (stmt_list_hash->dwo_unit != NULL)
2323 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2324 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2325 return v;
2326}
2327
2328/* Utility equality function for a stmt_list_hash. */
2329
2330static int
2331eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2332 const struct stmt_list_hash *rhs)
2333{
2334 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2335 return 0;
2336 if (lhs->dwo_unit != NULL
2337 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2338 return 0;
2339
9c541725 2340 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2341}
2342
7b9f3c50
DE
2343/* Hash function for a quick_file_names. */
2344
2345static hashval_t
2346hash_file_name_entry (const void *e)
2347{
9a3c8263
SM
2348 const struct quick_file_names *file_data
2349 = (const struct quick_file_names *) e;
7b9f3c50 2350
094b34ac 2351 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2352}
2353
2354/* Equality function for a quick_file_names. */
2355
2356static int
2357eq_file_name_entry (const void *a, const void *b)
2358{
9a3c8263
SM
2359 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2360 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2361
094b34ac 2362 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2363}
2364
2365/* Delete function for a quick_file_names. */
2366
2367static void
2368delete_file_name_entry (void *e)
2369{
9a3c8263 2370 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2371 int i;
2372
2373 for (i = 0; i < file_data->num_file_names; ++i)
2374 {
2375 xfree ((void*) file_data->file_names[i]);
2376 if (file_data->real_names)
2377 xfree ((void*) file_data->real_names[i]);
2378 }
2379
45940949
TT
2380 /* The space for the struct itself lives on the obstack, so we don't
2381 free it here. */
7b9f3c50
DE
2382}
2383
2384/* Create a quick_file_names hash table. */
2385
5895093f 2386static htab_up
7b9f3c50
DE
2387create_quick_file_names_table (unsigned int nr_initial_entries)
2388{
5895093f
TT
2389 return htab_up (htab_create_alloc (nr_initial_entries,
2390 hash_file_name_entry, eq_file_name_entry,
2391 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2392}
9291a0cd 2393
ab432490
SM
2394/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2395 function is unrelated to symtabs, symtab would have to be created afterwards.
2396 You should call age_cached_comp_units after processing the CU. */
918dd910 2397
1b555f17 2398static dwarf2_cu *
ab432490
SM
2399load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2400 bool skip_partial)
918dd910 2401{
3019eac3 2402 if (per_cu->is_debug_types)
ab432490 2403 load_full_type_unit (per_cu, per_objfile);
918dd910 2404 else
4a636814
SM
2405 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2406 skip_partial, language_minimal);
918dd910 2407
7188ed02
SM
2408 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2409 if (cu == nullptr)
1b555f17 2410 return nullptr; /* Dummy CU. */
2dc860c0 2411
7188ed02 2412 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2413
7188ed02 2414 return cu;
918dd910
JK
2415}
2416
1350c3b4 2417/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2418
9291a0cd 2419static void
97a1449a 2420dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2421 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2422{
f4dc4d17
DE
2423 /* Skip type_unit_groups, reading the type units they contain
2424 is handled elsewhere. */
197400e8 2425 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2426 return;
2427
08ac5771
SM
2428 {
2429 /* The destructor of dwarf2_queue_guard frees any entries left on
2430 the queue. After this point we're guaranteed to leave this function
2431 with the dwarf queue empty. */
2432 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2433
08ac5771
SM
2434 if (!per_objfile->symtab_set_p (per_cu))
2435 {
2436 queue_comp_unit (per_cu, per_objfile, language_minimal);
2437 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2438
2439 /* If we just loaded a CU from a DWO, and we're working with an index
2440 that may badly handle TUs, load all the TUs in that DWO as well.
2441 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2442 if (!per_cu->is_debug_types
2443 && cu != NULL
2444 && cu->dwo_unit != NULL
2445 && per_objfile->per_bfd->index_table != NULL
2446 && per_objfile->per_bfd->index_table->version <= 7
2447 /* DWP files aren't supported yet. */
2448 && get_dwp_file (per_objfile) == NULL)
2449 queue_and_load_all_dwo_tus (cu);
2450 }
9291a0cd 2451
08ac5771
SM
2452 process_queue (per_objfile);
2453 }
9291a0cd
TT
2454
2455 /* Age the cache, releasing compilation units that have not
2456 been used recently. */
976ca316 2457 per_objfile->age_comp_units ();
9291a0cd
TT
2458}
2459
97a1449a
SM
2460/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2461 the per-objfile for which this symtab is instantiated.
2462
2463 Returns the resulting symbol table. */
2fdf6df6 2464
43f3e411 2465static struct compunit_symtab *
97a1449a 2466dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2467 dwarf2_per_objfile *per_objfile,
97a1449a 2468 bool skip_partial)
9291a0cd 2469{
976ca316 2470 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2471
976ca316 2472 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2473 {
976ca316 2474 free_cached_comp_units freer (per_objfile);
c83dd867 2475 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2476 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2477 process_cu_includes (per_objfile);
9291a0cd 2478 }
f194fefb 2479
976ca316 2480 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2481}
2482
ff4c9fec 2483/* See declaration. */
f4dc4d17 2484
ff4c9fec 2485dwarf2_per_cu_data *
5989a64e 2486dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2487{
b76e467d 2488 if (index >= this->all_comp_units.size ())
ff4c9fec 2489 {
b76e467d 2490 index -= this->all_comp_units.size ();
b2bdb8cf 2491 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2492 return &this->all_type_units[index]->per_cu;
2493 }
f4dc4d17 2494
ff4c9fec
SM
2495 return this->all_comp_units[index];
2496}
f4dc4d17 2497
ff4c9fec 2498/* See declaration. */
2fdf6df6 2499
ff4c9fec 2500dwarf2_per_cu_data *
5989a64e 2501dwarf2_per_bfd::get_cu (int index)
1fd400ff 2502{
b76e467d 2503 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2504
ff4c9fec 2505 return this->all_comp_units[index];
f4dc4d17
DE
2506}
2507
ff4c9fec 2508/* See declaration. */
f4dc4d17 2509
ff4c9fec 2510signatured_type *
5989a64e 2511dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2512{
b2bdb8cf 2513 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2514
ff4c9fec 2515 return this->all_type_units[index];
1fd400ff
TT
2516}
2517
d3473f0c
TT
2518/* See read.h. */
2519
2520dwarf2_per_cu_data *
5989a64e 2521dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2522{
2523 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2524 result->per_bfd = this;
d3473f0c
TT
2525 result->index = m_num_psymtabs++;
2526 return result;
2527}
2528
2529/* See read.h. */
2530
2531signatured_type *
5989a64e 2532dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2533{
2534 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2535 result->per_cu.per_bfd = this;
d3473f0c
TT
2536 result->per_cu.index = m_num_psymtabs++;
2537 return result;
2538}
2539
168c9250 2540/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2541 obstack, and constructed with the specified field values. */
4b514bc8
JK
2542
2543static dwarf2_per_cu_data *
168c9250
SM
2544create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2545 struct dwarf2_section_info *section,
2546 int is_dwz,
2547 sect_offset sect_off, ULONGEST length)
4b514bc8 2548{
168c9250 2549 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2550 the_cu->sect_off = sect_off;
2551 the_cu->length = length;
4b514bc8 2552 the_cu->section = section;
168c9250 2553 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2554 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2555 the_cu->is_dwz = is_dwz;
2556 return the_cu;
2557}
2558
2ec9a5e0
TT
2559/* A helper for create_cus_from_index that handles a given list of
2560 CUs. */
2fdf6df6 2561
74a0d9f6 2562static void
168c9250 2563create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2564 const gdb_byte *cu_list, offset_type n_elements,
2565 struct dwarf2_section_info *section,
b76e467d 2566 int is_dwz)
9291a0cd 2567{
12359b5e 2568 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2569 {
74a0d9f6 2570 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2571
2572 sect_offset sect_off
2573 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2574 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2575 cu_list += 2 * 8;
2576
b76e467d 2577 dwarf2_per_cu_data *per_cu
168c9250
SM
2578 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2579 length);
2580 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2581 }
9291a0cd
TT
2582}
2583
2ec9a5e0 2584/* Read the CU list from the mapped index, and use it to create all
168c9250 2585 the CU objects for PER_BFD. */
2ec9a5e0 2586
74a0d9f6 2587static void
168c9250 2588create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2589 const gdb_byte *cu_list, offset_type cu_list_elements,
2590 const gdb_byte *dwz_list, offset_type dwz_elements)
2591{
168c9250
SM
2592 gdb_assert (per_bfd->all_comp_units.empty ());
2593 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2594
168c9250
SM
2595 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2596 &per_bfd->info, 0);
2ec9a5e0
TT
2597
2598 if (dwz_elements == 0)
74a0d9f6 2599 return;
2ec9a5e0 2600
168c9250
SM
2601 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2602 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2603 &dwz->info, 1);
2ec9a5e0
TT
2604}
2605
1fd400ff 2606/* Create the signatured type hash table from the index. */
673bfd45 2607
74a0d9f6 2608static void
12359b5e 2609create_signatured_type_table_from_index
168c9250
SM
2610 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2611 const gdb_byte *bytes, offset_type elements)
1fd400ff 2612{
168c9250
SM
2613 gdb_assert (per_bfd->all_type_units.empty ());
2614 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2615
298e9637 2616 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2617
12359b5e 2618 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2619 {
52dc124a 2620 struct signatured_type *sig_type;
9c541725 2621 ULONGEST signature;
1fd400ff 2622 void **slot;
9c541725 2623 cu_offset type_offset_in_tu;
1fd400ff 2624
74a0d9f6 2625 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2626 sect_offset sect_off
2627 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2628 type_offset_in_tu
2629 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2630 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2631 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2632 bytes += 3 * 8;
2633
168c9250 2634 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2635 sig_type->signature = signature;
9c541725 2636 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2637 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2638 sig_type->per_cu.section = section;
9c541725 2639 sig_type->per_cu.sect_off = sect_off;
52dc124a 2640 sig_type->per_cu.v.quick
168c9250 2641 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2642 struct dwarf2_per_cu_quick_data);
2643
b0b6a987 2644 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2645 *slot = sig_type;
1fd400ff 2646
168c9250 2647 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2648 }
2649
168c9250 2650 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2651}
2652
927aa2e7
JK
2653/* Create the signatured type hash table from .debug_names. */
2654
2655static void
2656create_signatured_type_table_from_debug_names
976ca316 2657 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2658 const mapped_debug_names &map,
2659 struct dwarf2_section_info *section,
2660 struct dwarf2_section_info *abbrev_section)
2661{
976ca316 2662 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2663
96b79293
TT
2664 section->read (objfile);
2665 abbrev_section->read (objfile);
927aa2e7 2666
976ca316
SM
2667 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2668 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2669
298e9637 2670 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2671
2672 for (uint32_t i = 0; i < map.tu_count; ++i)
2673 {
2674 struct signatured_type *sig_type;
927aa2e7 2675 void **slot;
927aa2e7
JK
2676
2677 sect_offset sect_off
2678 = (sect_offset) (extract_unsigned_integer
2679 (map.tu_table_reordered + i * map.offset_size,
2680 map.offset_size,
2681 map.dwarf5_byte_order));
2682
2683 comp_unit_head cu_header;
976ca316 2684 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2685 abbrev_section,
927aa2e7
JK
2686 section->buffer + to_underlying (sect_off),
2687 rcuh_kind::TYPE);
2688
976ca316 2689 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2690 sig_type->signature = cu_header.signature;
2691 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2692 sig_type->per_cu.is_debug_types = 1;
2693 sig_type->per_cu.section = section;
2694 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2695 sig_type->per_cu.v.quick
976ca316 2696 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2697 struct dwarf2_per_cu_quick_data);
2698
b0b6a987 2699 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2700 *slot = sig_type;
2701
976ca316 2702 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2703 }
2704
976ca316 2705 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2706}
2707
9291a0cd 2708/* Read the address map data from the mapped index, and use it to
efd7398e 2709 populate the psymtabs_addrmap. */
2fdf6df6 2710
9291a0cd 2711static void
976ca316 2712create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2713 struct mapped_index *index)
9291a0cd 2714{
976ca316 2715 struct objfile *objfile = per_objfile->objfile;
efd7398e 2716 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2717 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2718 const gdb_byte *iter, *end;
9291a0cd 2719 struct addrmap *mutable_map;
9291a0cd
TT
2720 CORE_ADDR baseaddr;
2721
8268c778
PA
2722 auto_obstack temp_obstack;
2723
9291a0cd
TT
2724 mutable_map = addrmap_create_mutable (&temp_obstack);
2725
f00a2de2
PA
2726 iter = index->address_table.data ();
2727 end = iter + index->address_table.size ();
9291a0cd 2728
b3b3bada 2729 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2730
2731 while (iter < end)
2732 {
2733 ULONGEST hi, lo, cu_index;
2734 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2735 iter += 8;
2736 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2737 iter += 8;
2738 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2739 iter += 4;
f652bce2 2740
24a55014 2741 if (lo > hi)
f652bce2 2742 {
b98664d3 2743 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2744 hex_string (lo), hex_string (hi));
24a55014 2745 continue;
f652bce2 2746 }
24a55014 2747
efd7398e 2748 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2749 {
b98664d3 2750 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2751 (unsigned) cu_index);
24a55014 2752 continue;
f652bce2 2753 }
24a55014 2754
79748972
TT
2755 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2756 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2757 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2758 per_bfd->get_cu (cu_index));
9291a0cd
TT
2759 }
2760
efd7398e
TT
2761 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2762 &per_bfd->obstack);
9291a0cd
TT
2763}
2764
927aa2e7 2765/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2766 populate the psymtabs_addrmap. */
927aa2e7
JK
2767
2768static void
976ca316 2769create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2770 struct dwarf2_section_info *section)
2771{
976ca316 2772 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2773 bfd *abfd = objfile->obfd;
08feed99 2774 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2775 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2776 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2777
2778 auto_obstack temp_obstack;
2779 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2780
2781 std::unordered_map<sect_offset,
2782 dwarf2_per_cu_data *,
2783 gdb::hash_enum<sect_offset>>
2784 debug_info_offset_to_per_cu;
bab287cd 2785 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
927aa2e7 2786 {
927aa2e7
JK
2787 const auto insertpair
2788 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2789 if (!insertpair.second)
2790 {
2791 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2792 "debug_info_offset %s, ignoring .debug_aranges."),
2793 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2794 return;
2795 }
2796 }
2797
96b79293 2798 section->read (objfile);
927aa2e7
JK
2799
2800 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2801
2802 const gdb_byte *addr = section->buffer;
2803
2804 while (addr < section->buffer + section->size)
2805 {
2806 const gdb_byte *const entry_addr = addr;
2807 unsigned int bytes_read;
2808
2809 const LONGEST entry_length = read_initial_length (abfd, addr,
2810 &bytes_read);
2811 addr += bytes_read;
2812
2813 const gdb_byte *const entry_end = addr + entry_length;
2814 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2815 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2816 if (addr + entry_length > section->buffer + section->size)
2817 {
47e3f474 2818 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2819 "length %s exceeds section length %s, "
927aa2e7 2820 "ignoring .debug_aranges."),
47e3f474
TV
2821 objfile_name (objfile),
2822 plongest (entry_addr - section->buffer),
927aa2e7
JK
2823 plongest (bytes_read + entry_length),
2824 pulongest (section->size));
2825 return;
2826 }
2827
2828 /* The version number. */
2829 const uint16_t version = read_2_bytes (abfd, addr);
2830 addr += 2;
2831 if (version != 2)
2832 {
47e3f474 2833 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2834 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2835 objfile_name (objfile),
2836 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2837 return;
2838 }
2839
2840 const uint64_t debug_info_offset
2841 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2842 addr += offset_size;
2843 const auto per_cu_it
2844 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2845 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2846 {
47e3f474 2847 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2848 "debug_info_offset %s does not exists, "
2849 "ignoring .debug_aranges."),
47e3f474
TV
2850 objfile_name (objfile),
2851 plongest (entry_addr - section->buffer),
927aa2e7
JK
2852 pulongest (debug_info_offset));
2853 return;
2854 }
2855 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2856
2857 const uint8_t address_size = *addr++;
2858 if (address_size < 1 || address_size > 8)
2859 {
47e3f474 2860 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2861 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2862 objfile_name (objfile),
2863 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2864 return;
2865 }
2866
2867 const uint8_t segment_selector_size = *addr++;
2868 if (segment_selector_size != 0)
2869 {
47e3f474 2870 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2871 "segment_selector_size %u is not supported, "
2872 "ignoring .debug_aranges."),
47e3f474
TV
2873 objfile_name (objfile),
2874 plongest (entry_addr - section->buffer),
927aa2e7
JK
2875 segment_selector_size);
2876 return;
2877 }
2878
2879 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2880 size. It is undocumented by the DWARF standard but GCC does
2881 use it. */
927aa2e7
JK
2882 for (size_t padding = ((-(addr - section->buffer))
2883 & (2 * address_size - 1));
dda83cd7 2884 padding > 0; padding--)
927aa2e7
JK
2885 if (*addr++ != 0)
2886 {
47e3f474 2887 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2888 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2889 objfile_name (objfile),
2890 plongest (entry_addr - section->buffer));
927aa2e7
JK
2891 return;
2892 }
2893
2894 for (;;)
2895 {
2896 if (addr + 2 * address_size > entry_end)
2897 {
47e3f474 2898 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2899 "address list is not properly terminated, "
2900 "ignoring .debug_aranges."),
47e3f474
TV
2901 objfile_name (objfile),
2902 plongest (entry_addr - section->buffer));
927aa2e7
JK
2903 return;
2904 }
2905 ULONGEST start = extract_unsigned_integer (addr, address_size,
2906 dwarf5_byte_order);
2907 addr += address_size;
2908 ULONGEST length = extract_unsigned_integer (addr, address_size,
2909 dwarf5_byte_order);
2910 addr += address_size;
2911 if (start == 0 && length == 0)
2912 break;
efd7398e 2913 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2914 {
2915 /* Symbol was eliminated due to a COMDAT group. */
2916 continue;
2917 }
2918 ULONGEST end = start + length;
79748972
TT
2919 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2920 - baseaddr);
2921 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2922 - baseaddr);
927aa2e7
JK
2923 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2924 }
2925 }
2926
efd7398e
TT
2927 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2928 &per_bfd->obstack);
927aa2e7
JK
2929}
2930
4485a1c1
SM
2931/* A helper function that reads the .gdb_index from BUFFER and fills
2932 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2933 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2934 ok to use deprecated sections.
2935
2936 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2937 out parameters that are filled in with information about the CU and
2938 TU lists in the section.
2939
4485a1c1 2940 Returns true if all went well, false otherwise. */
2fdf6df6 2941
d33bc52e 2942static bool
3810f182 2943read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2944 bool deprecated_ok,
2945 gdb::array_view<const gdb_byte> buffer,
2946 struct mapped_index *map,
2947 const gdb_byte **cu_list,
2948 offset_type *cu_list_elements,
2949 const gdb_byte **types_list,
2950 offset_type *types_list_elements)
2951{
2952 const gdb_byte *addr = &buffer[0];
82430852 2953
9291a0cd 2954 /* Version check. */
4485a1c1 2955 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2956 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2957 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2958 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2959 indices. */
831adc1f 2960 if (version < 4)
481860b3
GB
2961 {
2962 static int warning_printed = 0;
2963 if (!warning_printed)
2964 {
2965 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2966 filename);
481860b3
GB
2967 warning_printed = 1;
2968 }
2969 return 0;
2970 }
2971 /* Index version 4 uses a different hash function than index version
2972 5 and later.
2973
2974 Versions earlier than 6 did not emit psymbols for inlined
2975 functions. Using these files will cause GDB not to be able to
2976 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2977 indices unless the user has done
2978 "set use-deprecated-index-sections on". */
2ec9a5e0 2979 if (version < 6 && !deprecated_ok)
481860b3
GB
2980 {
2981 static int warning_printed = 0;
2982 if (!warning_printed)
2983 {
e615022a
DE
2984 warning (_("\
2985Skipping deprecated .gdb_index section in %s.\n\
2986Do \"set use-deprecated-index-sections on\" before the file is read\n\
2987to use the section anyway."),
2ec9a5e0 2988 filename);
481860b3
GB
2989 warning_printed = 1;
2990 }
2991 return 0;
2992 }
796a7ff8 2993 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2994 of the TU (for symbols coming from TUs),
2995 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2996 Plus gold-generated indices can have duplicate entries for global symbols,
2997 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2998 These are just performance bugs, and we can't distinguish gdb-generated
2999 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3000
481860b3 3001 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3002 longer backward compatible. */
796a7ff8 3003 if (version > 8)
594e8718 3004 return 0;
9291a0cd 3005
559a7a62 3006 map->version = version;
9291a0cd 3007
4485a1c1 3008 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3009
4485a1c1 3010 int i = 0;
2ec9a5e0
TT
3011 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3012 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3013 / 8);
1fd400ff
TT
3014 ++i;
3015
2ec9a5e0
TT
3016 *types_list = addr + MAYBE_SWAP (metadata[i]);
3017 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3018 - MAYBE_SWAP (metadata[i]))
3019 / 8);
987d643c 3020 ++i;
1fd400ff 3021
f00a2de2
PA
3022 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3023 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3024 map->address_table
3025 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3026 ++i;
3027
f00a2de2
PA
3028 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3029 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3030 map->symbol_table
3031 = gdb::array_view<mapped_index::symbol_table_slot>
3032 ((mapped_index::symbol_table_slot *) symbol_table,
3033 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3034
f00a2de2 3035 ++i;
f9d83a0b 3036 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3037
2ec9a5e0
TT
3038 return 1;
3039}
3040
4485a1c1
SM
3041/* Callback types for dwarf2_read_gdb_index. */
3042
3043typedef gdb::function_view
5989a64e 3044 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3045 get_gdb_index_contents_ftype;
3046typedef gdb::function_view
3047 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3048 get_gdb_index_contents_dwz_ftype;
3049
927aa2e7 3050/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3051 elements of all the CUs and return 1. Otherwise, return 0. */
3052
3053static int
4485a1c1 3054dwarf2_read_gdb_index
976ca316 3055 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3056 get_gdb_index_contents_ftype get_gdb_index_contents,
3057 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3058{
2ec9a5e0
TT
3059 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3060 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3061 struct dwz_file *dwz;
976ca316 3062 struct objfile *objfile = per_objfile->objfile;
f8c41851 3063 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3064
4485a1c1 3065 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3066 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3067
3068 if (main_index_contents.empty ())
3069 return 0;
3070
3063847f 3071 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3072 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3073 use_deprecated_index_sections,
3074 main_index_contents, map.get (), &cu_list,
3075 &cu_list_elements, &types_list,
3076 &types_list_elements))
2ec9a5e0
TT
3077 return 0;
3078
0fefef59 3079 /* Don't use the index if it's empty. */
3063847f 3080 if (map->symbol_table.empty ())
0fefef59
DE
3081 return 0;
3082
2ec9a5e0
TT
3083 /* If there is a .dwz file, read it so we can get its CU list as
3084 well. */
f8c41851 3085 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3086 if (dwz != NULL)
2ec9a5e0 3087 {
2ec9a5e0
TT
3088 struct mapped_index dwz_map;
3089 const gdb_byte *dwz_types_ignore;
3090 offset_type dwz_types_elements_ignore;
3091
4485a1c1
SM
3092 gdb::array_view<const gdb_byte> dwz_index_content
3093 = get_gdb_index_contents_dwz (objfile, dwz);
3094
3095 if (dwz_index_content.empty ())
3096 return 0;
3097
3810f182 3098 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3099 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3100 &dwz_list, &dwz_list_elements,
3101 &dwz_types_ignore,
3102 &dwz_types_elements_ignore))
2ec9a5e0
TT
3103 {
3104 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3105 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3106 return 0;
3107 }
3108 }
3109
f8c41851
SM
3110 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3111 dwz_list_elements);
1fd400ff 3112
8b70b953
TT
3113 if (types_list_elements)
3114 {
8b70b953
TT
3115 /* We can only handle a single .debug_types when we have an
3116 index. */
f8c41851 3117 if (per_bfd->types.size () != 1)
8b70b953
TT
3118 return 0;
3119
f8c41851 3120 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3121
f8c41851 3122 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3123 types_list_elements);
8b70b953 3124 }
9291a0cd 3125
976ca316 3126 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3127
f8c41851
SM
3128 per_bfd->index_table = std::move (map);
3129 per_bfd->using_index = 1;
3130 per_bfd->quick_file_names_table =
3131 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3132
9291a0cd
TT
3133 return 1;
3134}
3135
dee91e82 3136/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3137
dee91e82
DE
3138static void
3139dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 3140 struct die_info *comp_unit_die)
9291a0cd 3141{
dee91e82 3142 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3143 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3144 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3145 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3146 struct attribute *attr;
7b9f3c50
DE
3147 void **slot;
3148 struct quick_file_names *qfn;
9291a0cd 3149
0186c6a7
DE
3150 gdb_assert (! this_cu->is_debug_types);
3151
07261596
TT
3152 /* Our callers never want to match partial units -- instead they
3153 will match the enclosing full CU. */
3154 if (comp_unit_die->tag == DW_TAG_partial_unit)
3155 {
3156 this_cu->v.quick->no_file_data = 1;
3157 return;
3158 }
3159
0186c6a7 3160 lh_cu = this_cu;
7b9f3c50 3161 slot = NULL;
dee91e82 3162
fff8551c 3163 line_header_up lh;
9c541725 3164 sect_offset line_offset {};
fff8551c 3165
dee91e82 3166 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3167 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3168 {
7b9f3c50
DE
3169 struct quick_file_names find_entry;
3170
d4df075e 3171 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3172
3173 /* We may have already read in this line header (TU line header sharing).
3174 If we have we're done. */
094b34ac 3175 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3176 find_entry.hash.line_sect_off = line_offset;
976ca316 3177 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3178 &find_entry, INSERT);
3179 if (*slot != NULL)
3180 {
9a3c8263 3181 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3182 return;
7b9f3c50
DE
3183 }
3184
3019eac3 3185 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3186 }
3187 if (lh == NULL)
3188 {
094b34ac 3189 lh_cu->v.quick->no_file_data = 1;
dee91e82 3190 return;
9291a0cd
TT
3191 }
3192
976ca316 3193 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3194 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3195 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3196 gdb_assert (slot != NULL);
3197 *slot = qfn;
9291a0cd 3198
d721ba37 3199 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3200
aa391654
TT
3201 int offset = 0;
3202 if (strcmp (fnd.name, "<unknown>") != 0)
3203 ++offset;
3204
7ba99d21 3205 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3206 qfn->file_names =
976ca316 3207 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3208 qfn->num_file_names);
aa391654
TT
3209 if (offset != 0)
3210 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3211 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3212 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3213 fnd.comp_dir).release ();
7b9f3c50 3214 qfn->real_names = NULL;
9291a0cd 3215
094b34ac 3216 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3217}
3218
3219/* A helper for the "quick" functions which attempts to read the line
3220 table for THIS_CU. */
3221
3222static struct quick_file_names *
ab432490
SM
3223dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3224 dwarf2_per_objfile *per_objfile)
dee91e82 3225{
0186c6a7
DE
3226 /* This should never be called for TUs. */
3227 gdb_assert (! this_cu->is_debug_types);
3228 /* Nor type unit groups. */
197400e8 3229 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3230
dee91e82
DE
3231 if (this_cu->v.quick->file_names != NULL)
3232 return this_cu->v.quick->file_names;
3233 /* If we know there is no line data, no point in looking again. */
3234 if (this_cu->v.quick->no_file_data)
3235 return NULL;
3236
ab432490 3237 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3238 if (!reader.dummy_p)
eb5dd737 3239 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
3240
3241 if (this_cu->v.quick->no_file_data)
3242 return NULL;
3243 return this_cu->v.quick->file_names;
9291a0cd
TT
3244}
3245
3246/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3247 real path for a given file name from the line table. */
2fdf6df6 3248
9291a0cd 3249static const char *
976ca316 3250dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3251 struct quick_file_names *qfn, int index)
9291a0cd 3252{
7b9f3c50 3253 if (qfn->real_names == NULL)
976ca316 3254 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3255 qfn->num_file_names, const char *);
9291a0cd 3256
7b9f3c50 3257 if (qfn->real_names[index] == NULL)
14278e1f 3258 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3259
7b9f3c50 3260 return qfn->real_names[index];
9291a0cd
TT
3261}
3262
39298a5d
TT
3263struct symtab *
3264dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3265{
976ca316
SM
3266 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3267 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3268 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3269
43f3e411
DE
3270 if (cust == NULL)
3271 return NULL;
ed2dc618 3272
43f3e411 3273 return compunit_primary_filetab (cust);
9291a0cd
TT
3274}
3275
7b9f3c50
DE
3276/* Traversal function for dw2_forget_cached_source_info. */
3277
3278static int
3279dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3280{
7b9f3c50 3281 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3282
7b9f3c50 3283 if (file_data->real_names)
9291a0cd 3284 {
7b9f3c50 3285 int i;
9291a0cd 3286
7b9f3c50 3287 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3288 {
7b9f3c50
DE
3289 xfree ((void*) file_data->real_names[i]);
3290 file_data->real_names[i] = NULL;
9291a0cd
TT
3291 }
3292 }
7b9f3c50
DE
3293
3294 return 1;
3295}
3296
39298a5d
TT
3297void
3298dwarf2_base_index_functions::forget_cached_source_info
3299 (struct objfile *objfile)
7b9f3c50 3300{
976ca316 3301 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3302
976ca316 3303 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3304 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3305}
3306
da51c347
DE
3307/* Struct used to manage iterating over all CUs looking for a symbol. */
3308
3309struct dw2_symtab_iterator
9291a0cd 3310{
ed2dc618 3311 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3312 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3313 /* If set, only look for symbols that match that block. Valid values are
3314 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3315 gdb::optional<block_enum> block_index;
da51c347
DE
3316 /* The kind of symbol we're looking for. */
3317 domain_enum domain;
3318 /* The list of CUs from the index entry of the symbol,
3319 or NULL if not found. */
3320 offset_type *vec;
3321 /* The next element in VEC to look at. */
3322 int next;
3323 /* The number of elements in VEC, or zero if there is no match. */
3324 int length;
8943b874
DE
3325 /* Have we seen a global version of the symbol?
3326 If so we can ignore all further global instances.
3327 This is to work around gold/15646, inefficient gold-generated
3328 indices. */
3329 int global_seen;
da51c347 3330};
9291a0cd 3331
7089bd88 3332/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2fdf6df6 3333
9291a0cd 3334static void
7089bd88
TT
3335dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3336 dwarf2_per_objfile *per_objfile,
3337 gdb::optional<block_enum> block_index,
3338 domain_enum domain, offset_type namei)
da51c347 3339{
976ca316 3340 iter->per_objfile = per_objfile;
da51c347
DE
3341 iter->block_index = block_index;
3342 iter->domain = domain;
3343 iter->next = 0;
8943b874 3344 iter->global_seen = 0;
e5f3ece2
TV
3345 iter->vec = NULL;
3346 iter->length = 0;
e5f3ece2
TV
3347
3348 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3349 /* index is NULL if OBJF_READNOW. */
3350 if (index == NULL)
3351 return;
3352
3353 gdb_assert (!index->symbol_name_slot_invalid (namei));
3354 const auto &bucket = index->symbol_table[namei];
3355
3356 iter->vec = (offset_type *) (index->constant_pool
3357 + MAYBE_SWAP (bucket.vec));
3358 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3359}
3360
3361/* Return the next matching CU or NULL if there are no more. */
3362
3363static struct dwarf2_per_cu_data *
3364dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3365{
976ca316 3366 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3367
da51c347
DE
3368 for ( ; iter->next < iter->length; ++iter->next)
3369 {
3370 offset_type cu_index_and_attrs =
3371 MAYBE_SWAP (iter->vec[iter->next + 1]);
3372 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3373 gdb_index_symbol_kind symbol_kind =
3374 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3375 /* Only check the symbol attributes if they're present.
3376 Indices prior to version 7 don't record them,
3377 and indices >= 7 may elide them for certain symbols
3378 (gold does this). */
3379 int attrs_valid =
976ca316 3380 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3381 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3382
3190f0c6 3383 /* Don't crash on bad data. */
976ca316
SM
3384 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3385 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3386 {
b98664d3 3387 complaint (_(".gdb_index entry has bad CU index"
976ca316 3388 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3389 continue;
3390 }
3391
976ca316 3392 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3393
da51c347 3394 /* Skip if already read in. */
976ca316 3395 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3396 continue;
3397
8943b874
DE
3398 /* Check static vs global. */
3399 if (attrs_valid)
3400 {
2b79f376
SM
3401 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3402
3403 if (iter->block_index.has_value ())
3404 {
3405 bool want_static = *iter->block_index == STATIC_BLOCK;
3406
3407 if (is_static != want_static)
3408 continue;
3409 }
3410
8943b874 3411 /* Work around gold/15646. */
f030440d
TV
3412 if (!is_static
3413 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3414 {
3415 if (iter->global_seen)
3416 continue;
3417
3418 iter->global_seen = 1;
3419 }
8943b874 3420 }
da51c347
DE
3421
3422 /* Only check the symbol's kind if it has one. */
3423 if (attrs_valid)
3424 {
3425 switch (iter->domain)
3426 {
3427 case VAR_DOMAIN:
3428 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3429 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3430 /* Some types are also in VAR_DOMAIN. */
3431 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3432 continue;
3433 break;
3434 case STRUCT_DOMAIN:
3435 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3436 continue;
3437 break;
3438 case LABEL_DOMAIN:
3439 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3440 continue;
3441 break;
59c35742
AB
3442 case MODULE_DOMAIN:
3443 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3444 continue;
3445 break;
da51c347
DE
3446 default:
3447 break;
3448 }
3449 }
3450
3451 ++iter->next;
3452 return per_cu;
3453 }
3454
3455 return NULL;
3456}
3457
39298a5d 3458void
4829711b
TT
3459dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3460 bool print_bcache)
9291a0cd 3461{
4829711b
TT
3462 if (print_bcache)
3463 return;
3464
976ca316
SM
3465 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3466 int total = (per_objfile->per_bfd->all_comp_units.size ()
3467 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3468 int count = 0;
9291a0cd 3469
ed2dc618 3470 for (int i = 0; i < total; ++i)
9291a0cd 3471 {
976ca316 3472 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3473
976ca316 3474 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3475 ++count;
3476 }
e4a48d9d 3477 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3478 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3479}
3480
779bd270
DE
3481/* This dumps minimal information about the index.
3482 It is called via "mt print objfiles".
3483 One use is to verify .gdb_index has been loaded by the
3484 gdb.dwarf2/gdb-index.exp testcase. */
3485
39298a5d
TT
3486void
3487dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3488{
976ca316 3489 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3490
976ca316 3491 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3492 printf_filtered (".gdb_index:");
976ca316 3493 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3494 {
3495 printf_filtered (" version %d\n",
976ca316 3496 per_objfile->per_bfd->index_table->version);
779bd270
DE
3497 }
3498 else
3499 printf_filtered (" faked for \"readnow\"\n");
3500 printf_filtered ("\n");
9291a0cd
TT
3501}
3502
39298a5d
TT
3503void
3504dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3505{
976ca316
SM
3506 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3507 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3508 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3509
ed2dc618 3510 for (int i = 0; i < total_units; ++i)
9291a0cd 3511 {
976ca316 3512 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3513
58f0c718
TT
3514 /* We don't want to directly expand a partial CU, because if we
3515 read it with the wrong language, then assertion failures can
3516 be triggered later on. See PR symtab/23010. So, tell
3517 dw2_instantiate_symtab to skip partial CUs -- any important
3518 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3519 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3520 }
3521}
3522
df35e626 3523static bool
9a0bacfb
TV
3524dw2_expand_symtabs_matching_symbol
3525 (mapped_index_base &index,
3526 const lookup_name_info &lookup_name_in,
3527 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3528 gdb::function_view<bool (offset_type)> match_callback,
3529 dwarf2_per_objfile *per_objfile);
9a0bacfb 3530
df35e626 3531static bool
9a0bacfb 3532dw2_expand_symtabs_matching_one
97a1449a
SM
3533 (dwarf2_per_cu_data *per_cu,
3534 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3535 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3536 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3537
af82f89d 3538void
0b7b2c2a 3539dwarf2_gdb_index::expand_matching_symbols
199b4314 3540 (struct objfile *objfile,
b054970d 3541 const lookup_name_info &name, domain_enum domain,
199b4314 3542 int global,
199b4314 3543 symbol_compare_ftype *ordered_compare)
9291a0cd 3544{
1aa98955 3545 /* Used for Ada. */
976ca316 3546 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3547
9a0bacfb
TV
3548 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3549
976ca316 3550 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3551 {
976ca316 3552 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3553
9a0bacfb
TV
3554 const char *match_name = name.ada ().lookup_name ().c_str ();
3555 auto matcher = [&] (const char *symname)
3556 {
3557 if (ordered_compare == nullptr)
3558 return true;
3559 return ordered_compare (symname, match_name) == 0;
3560 };
3561
b64f703b 3562 dw2_expand_symtabs_matching_symbol (index, name, matcher,
9a0bacfb
TV
3563 [&] (offset_type namei)
3564 {
3565 struct dw2_symtab_iterator iter;
3566 struct dwarf2_per_cu_data *per_cu;
3567
976ca316 3568 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3569 namei);
9a0bacfb 3570 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3571 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3572 nullptr);
9a0bacfb 3573 return true;
976ca316 3574 }, per_objfile);
9a0bacfb
TV
3575 }
3576 else
3577 {
3578 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3579 proceed assuming all symtabs have been read in. */
3580 }
9291a0cd
TT
3581}
3582
e1ef7d7a
PA
3583/* Starting from a search name, return the string that finds the upper
3584 bound of all strings that start with SEARCH_NAME in a sorted name
3585 list. Returns the empty string to indicate that the upper bound is
3586 the end of the list. */
3587
3588static std::string
3589make_sort_after_prefix_name (const char *search_name)
3590{
3591 /* When looking to complete "func", we find the upper bound of all
3592 symbols that start with "func" by looking for where we'd insert
3593 the closest string that would follow "func" in lexicographical
3594 order. Usually, that's "func"-with-last-character-incremented,
3595 i.e. "fund". Mind non-ASCII characters, though. Usually those
3596 will be UTF-8 multi-byte sequences, but we can't be certain.
3597 Especially mind the 0xff character, which is a valid character in
3598 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3599 rule out compilers allowing it in identifiers. Note that
3600 conveniently, strcmp/strcasecmp are specified to compare
3601 characters interpreted as unsigned char. So what we do is treat
3602 the whole string as a base 256 number composed of a sequence of
3603 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3604 to 0, and carries 1 to the following more-significant position.
3605 If the very first character in SEARCH_NAME ends up incremented
3606 and carries/overflows, then the upper bound is the end of the
3607 list. The string after the empty string is also the empty
3608 string.
3609
3610 Some examples of this operation:
3611
3612 SEARCH_NAME => "+1" RESULT
3613
3614 "abc" => "abd"
3615 "ab\xff" => "ac"
3616 "\xff" "a" "\xff" => "\xff" "b"
3617 "\xff" => ""
3618 "\xff\xff" => ""
3619 "" => ""
3620
3621 Then, with these symbols for example:
3622
3623 func
3624 func1
3625 fund
3626
3627 completing "func" looks for symbols between "func" and
3628 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3629 which finds "func" and "func1", but not "fund".
3630
3631 And with:
3632
3633 funcÿ (Latin1 'ÿ' [0xff])
3634 funcÿ1
3635 fund
3636
3637 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3638 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3639
3640 And with:
3641
3642 ÿÿ (Latin1 'ÿ' [0xff])
3643 ÿÿ1
3644
3645 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3646 the end of the list.
3647 */
3648 std::string after = search_name;
3649 while (!after.empty () && (unsigned char) after.back () == 0xff)
3650 after.pop_back ();
3651 if (!after.empty ())
3652 after.back () = (unsigned char) after.back () + 1;
3653 return after;
3654}
3655
5c58de74 3656/* See declaration. */
61d96d7e 3657
5c58de74
PA
3658std::pair<std::vector<name_component>::const_iterator,
3659 std::vector<name_component>::const_iterator>
44ed8f3e 3660mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3661 (const lookup_name_info &lookup_name_without_params, language lang,
3662 dwarf2_per_objfile *per_objfile) const
3f563c84 3663{
5c58de74
PA
3664 auto *name_cmp
3665 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3666
3b00ef10 3667 const char *lang_name
e0802d59 3668 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3669
3f563c84
PA
3670 /* Comparison function object for lower_bound that matches against a
3671 given symbol name. */
3672 auto lookup_compare_lower = [&] (const name_component &elem,
3673 const char *name)
3674 {
fcf23d5b 3675 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3676 const char *elem_name = elem_qualified + elem.name_offset;
3677 return name_cmp (elem_name, name) < 0;
3678 };
3679
3680 /* Comparison function object for upper_bound that matches against a
3681 given symbol name. */
3682 auto lookup_compare_upper = [&] (const char *name,
3683 const name_component &elem)
3684 {
fcf23d5b 3685 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3686 const char *elem_name = elem_qualified + elem.name_offset;
3687 return name_cmp (name, elem_name) < 0;
3688 };
3689
5c58de74
PA
3690 auto begin = this->name_components.begin ();
3691 auto end = this->name_components.end ();
3f563c84
PA
3692
3693 /* Find the lower bound. */
3694 auto lower = [&] ()
3695 {
3b00ef10 3696 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3697 return begin;
3698 else
3b00ef10 3699 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3700 } ();
3701
3702 /* Find the upper bound. */
3703 auto upper = [&] ()
3704 {
5c58de74 3705 if (lookup_name_without_params.completion_mode ())
3f563c84 3706 {
e1ef7d7a
PA
3707 /* In completion mode, we want UPPER to point past all
3708 symbols names that have the same prefix. I.e., with
3709 these symbols, and completing "func":
3710
3711 function << lower bound
3712 function1
3713 other_function << upper bound
3714
3715 We find the upper bound by looking for the insertion
3716 point of "func"-with-last-character-incremented,
3717 i.e. "fund". */
3b00ef10 3718 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3719 if (after.empty ())
3f563c84 3720 return end;
e6b2f5ef
PA
3721 return std::lower_bound (lower, end, after.c_str (),
3722 lookup_compare_lower);
3f563c84
PA
3723 }
3724 else
3b00ef10 3725 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3726 } ();
3727
5c58de74
PA
3728 return {lower, upper};
3729}
3730
3731/* See declaration. */
3732
3733void
fcf23d5b 3734mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3735{
3736 if (!this->name_components.empty ())
3737 return;
3738
3739 this->name_components_casing = case_sensitivity;
3740 auto *name_cmp
3741 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3742
3743 /* The code below only knows how to break apart components of C++
3744 symbol names (and other languages that use '::' as
3b00ef10 3745 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3746 auto count = this->symbol_name_count ();
3747 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3748 {
44ed8f3e 3749 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3750 continue;
3751
fcf23d5b 3752 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3753
3754 /* Add each name component to the name component table. */
3755 unsigned int previous_len = 0;
3b00ef10
TT
3756
3757 if (strstr (name, "::") != nullptr)
3758 {
3759 for (unsigned int current_len = cp_find_first_component (name);
3760 name[current_len] != '\0';
3761 current_len += cp_find_first_component (name + current_len))
3762 {
3763 gdb_assert (name[current_len] == ':');
3764 this->name_components.push_back ({previous_len, idx});
3765 /* Skip the '::'. */
3766 current_len += 2;
3767 previous_len = current_len;
3768 }
3769 }
3770 else
5c58de74 3771 {
3b00ef10
TT
3772 /* Handle the Ada encoded (aka mangled) form here. */
3773 for (const char *iter = strstr (name, "__");
3774 iter != nullptr;
3775 iter = strstr (iter, "__"))
3776 {
3777 this->name_components.push_back ({previous_len, idx});
3778 iter += 2;
3779 previous_len = iter - name;
3780 }
5c58de74 3781 }
3b00ef10 3782
5c58de74
PA
3783 this->name_components.push_back ({previous_len, idx});
3784 }
3785
3786 /* Sort name_components elements by name. */
3787 auto name_comp_compare = [&] (const name_component &left,
3788 const name_component &right)
3789 {
fcf23d5b
SM
3790 const char *left_qualified
3791 = this->symbol_name_at (left.idx, per_objfile);
3792 const char *right_qualified
3793 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
3794
3795 const char *left_name = left_qualified + left.name_offset;
3796 const char *right_name = right_qualified + right.name_offset;
3797
3798 return name_cmp (left_name, right_name) < 0;
3799 };
3800
3801 std::sort (this->name_components.begin (),
3802 this->name_components.end (),
3803 name_comp_compare);
3804}
3805
3806/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3807 mapped_index_base instead of the containing objfile. This is split
3808 to a separate function in order to be able to unit test the
3809 name_components matching using a mock mapped_index_base. For each
5c58de74 3810 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3811 symbol's index in the mapped_index_base symbol table. */
5c58de74 3812
df35e626 3813static bool
5c58de74 3814dw2_expand_symtabs_matching_symbol
44ed8f3e 3815 (mapped_index_base &index,
5c58de74
PA
3816 const lookup_name_info &lookup_name_in,
3817 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3818 gdb::function_view<bool (offset_type)> match_callback,
3819 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3820{
3821 lookup_name_info lookup_name_without_params
3822 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3823
3824 /* Build the symbol name component sorted vector, if we haven't
3825 yet. */
fcf23d5b 3826 index.build_name_components (per_objfile);
5c58de74 3827
3f563c84
PA
3828 /* The same symbol may appear more than once in the range though.
3829 E.g., if we're looking for symbols that complete "w", and we have
3830 a symbol named "w1::w2", we'll find the two name components for
3831 that same symbol in the range. To be sure we only call the
3832 callback once per symbol, we first collect the symbol name
3833 indexes that matched in a temporary vector and ignore
3834 duplicates. */
3835 std::vector<offset_type> matches;
3f563c84 3836
3b00ef10
TT
3837 struct name_and_matcher
3838 {
3839 symbol_name_matcher_ftype *matcher;
ecc6c606 3840 const char *name;
3b00ef10
TT
3841
3842 bool operator== (const name_and_matcher &other) const
3f563c84 3843 {
ecc6c606 3844 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3845 }
3846 };
3847
3848 /* A vector holding all the different symbol name matchers, for all
3849 languages. */
3850 std::vector<name_and_matcher> matchers;
3851
3852 for (int i = 0; i < nr_languages; i++)
3853 {
3854 enum language lang_e = (enum language) i;
3855
3856 const language_defn *lang = language_def (lang_e);
3857 symbol_name_matcher_ftype *name_matcher
c9debfb9 3858 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 3859
3b00ef10 3860 name_and_matcher key {
dda83cd7 3861 name_matcher,
3b00ef10
TT
3862 lookup_name_without_params.language_lookup_name (lang_e)
3863 };
3864
3865 /* Don't insert the same comparison routine more than once.
3866 Note that we do this linear walk. This is not a problem in
3867 practice because the number of supported languages is
3868 low. */
3869 if (std::find (matchers.begin (), matchers.end (), key)
3870 != matchers.end ())
9291a0cd 3871 continue;
3b00ef10
TT
3872 matchers.push_back (std::move (key));
3873
3874 auto bounds
3875 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 3876 lang_e, per_objfile);
3b00ef10
TT
3877
3878 /* Now for each symbol name in range, check to see if we have a name
3879 match, and if so, call the MATCH_CALLBACK callback. */
3880
3881 for (; bounds.first != bounds.second; ++bounds.first)
3882 {
fcf23d5b
SM
3883 const char *qualified
3884 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
3885
3886 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3887 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3888 continue;
9291a0cd 3889
3b00ef10
TT
3890 matches.push_back (bounds.first->idx);
3891 }
3f563c84
PA
3892 }
3893
3894 std::sort (matches.begin (), matches.end ());
3895
3896 /* Finally call the callback, once per match. */
3897 ULONGEST prev = -1;
df35e626 3898 bool result = true;
3f563c84
PA
3899 for (offset_type idx : matches)
3900 {
3901 if (prev != idx)
3902 {
3b00ef10 3903 if (!match_callback (idx))
df35e626
TT
3904 {
3905 result = false;
3906 break;
3907 }
3f563c84
PA
3908 prev = idx;
3909 }
3910 }
3911
3912 /* Above we use a type wider than idx's for 'prev', since 0 and
3913 (offset_type)-1 are both possible values. */
3914 static_assert (sizeof (prev) > sizeof (offset_type), "");
df35e626
TT
3915
3916 return result;
3f563c84
PA
3917}
3918
c62446b1
PA
3919#if GDB_SELF_TEST
3920
3921namespace selftests { namespace dw2_expand_symtabs_matching {
3922
a3c5fafd
PA
3923/* A mock .gdb_index/.debug_names-like name index table, enough to
3924 exercise dw2_expand_symtabs_matching_symbol, which works with the
3925 mapped_index_base interface. Builds an index from the symbol list
3926 passed as parameter to the constructor. */
3927class mock_mapped_index : public mapped_index_base
c62446b1
PA
3928{
3929public:
a3c5fafd
PA
3930 mock_mapped_index (gdb::array_view<const char *> symbols)
3931 : m_symbol_table (symbols)
c62446b1
PA
3932 {}
3933
a3c5fafd 3934 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3935
a3c5fafd 3936 /* Return the number of names in the symbol table. */
632e107b 3937 size_t symbol_name_count () const override
c62446b1 3938 {
a3c5fafd 3939 return m_symbol_table.size ();
c62446b1
PA
3940 }
3941
a3c5fafd 3942 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
3943 const char *symbol_name_at
3944 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
3945 {
3946 return m_symbol_table[idx];
3947 }
c62446b1 3948
a3c5fafd
PA
3949private:
3950 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
3951};
3952
3953/* Convenience function that converts a NULL pointer to a "<null>"
3954 string, to pass to print routines. */
3955
3956static const char *
3957string_or_null (const char *str)
3958{
3959 return str != NULL ? str : "<null>";
3960}
3961
3962/* Check if a lookup_name_info built from
3963 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3964 index. EXPECTED_LIST is the list of expected matches, in expected
3965 matching order. If no match expected, then an empty list is
3966 specified. Returns true on success. On failure prints a warning
3967 indicating the file:line that failed, and returns false. */
3968
3969static bool
3970check_match (const char *file, int line,
3971 mock_mapped_index &mock_index,
3972 const char *name, symbol_name_match_type match_type,
3973 bool completion_mode,
fcf23d5b
SM
3974 std::initializer_list<const char *> expected_list,
3975 dwarf2_per_objfile *per_objfile)
c62446b1
PA
3976{
3977 lookup_name_info lookup_name (name, match_type, completion_mode);
3978
3979 bool matched = true;
3980
3981 auto mismatch = [&] (const char *expected_str,
3982 const char *got)
3983 {
3984 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3985 "expected=\"%s\", got=\"%s\"\n"),
3986 file, line,
3987 (match_type == symbol_name_match_type::FULL
3988 ? "FULL" : "WILD"),
3989 name, string_or_null (expected_str), string_or_null (got));
3990 matched = false;
3991 };
3992
3993 auto expected_it = expected_list.begin ();
3994 auto expected_end = expected_list.end ();
3995
a3c5fafd 3996 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
b64f703b 3997 nullptr,
c62446b1
PA
3998 [&] (offset_type idx)
3999 {
fcf23d5b 4000 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4001 const char *expected_str
4002 = expected_it == expected_end ? NULL : *expected_it++;
4003
4004 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4005 mismatch (expected_str, matched_name);
3b00ef10 4006 return true;
fcf23d5b 4007 }, per_objfile);
c62446b1
PA
4008
4009 const char *expected_str
4010 = expected_it == expected_end ? NULL : *expected_it++;
4011 if (expected_str != NULL)
4012 mismatch (expected_str, NULL);
4013
4014 return matched;
4015}
4016
4017/* The symbols added to the mock mapped_index for testing (in
4018 canonical form). */
4019static const char *test_symbols[] = {
4020 "function",
4021 "std::bar",
4022 "std::zfunction",
4023 "std::zfunction2",
4024 "w1::w2",
4025 "ns::foo<char*>",
4026 "ns::foo<int>",
4027 "ns::foo<long>",
a20714ff
PA
4028 "ns2::tmpl<int>::foo2",
4029 "(anonymous namespace)::A::B::C",
c62446b1 4030
e1ef7d7a
PA
4031 /* These are used to check that the increment-last-char in the
4032 matching algorithm for completion doesn't match "t1_fund" when
4033 completing "t1_func". */
4034 "t1_func",
4035 "t1_func1",
4036 "t1_fund",
4037 "t1_fund1",
4038
4039 /* A UTF-8 name with multi-byte sequences to make sure that
4040 cp-name-parser understands this as a single identifier ("função"
4041 is "function" in PT). */
4042 u8"u8função",
4043
4044 /* \377 (0xff) is Latin1 'ÿ'. */
4045 "yfunc\377",
4046
4047 /* \377 (0xff) is Latin1 'ÿ'. */
4048 "\377",
4049 "\377\377123",
4050
c62446b1
PA
4051 /* A name with all sorts of complications. Starts with "z" to make
4052 it easier for the completion tests below. */
4053#define Z_SYM_NAME \
4054 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4055 "::tuple<(anonymous namespace)::ui*, " \
4056 "std::default_delete<(anonymous namespace)::ui>, void>"
4057
4058 Z_SYM_NAME
4059};
4060
a3c5fafd
PA
4061/* Returns true if the mapped_index_base::find_name_component_bounds
4062 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4063 in completion mode. */
5c58de74
PA
4064
4065static bool
a3c5fafd 4066check_find_bounds_finds (mapped_index_base &index,
5c58de74 4067 const char *search_name,
fcf23d5b
SM
4068 gdb::array_view<const char *> expected_syms,
4069 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4070{
4071 lookup_name_info lookup_name (search_name,
4072 symbol_name_match_type::FULL, true);
4073
3b00ef10 4074 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4075 language_cplus,
4076 per_objfile);
5c58de74
PA
4077
4078 size_t distance = std::distance (bounds.first, bounds.second);
4079 if (distance != expected_syms.size ())
4080 return false;
4081
4082 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4083 {
4084 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4085 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4086 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4087 return false;
4088 }
4089
4090 return true;
4091}
4092
4093/* Test the lower-level mapped_index::find_name_component_bounds
4094 method. */
4095
c62446b1 4096static void
5c58de74
PA
4097test_mapped_index_find_name_component_bounds ()
4098{
4099 mock_mapped_index mock_index (test_symbols);
4100
fcf23d5b 4101 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4102
4103 /* Test the lower-level mapped_index::find_name_component_bounds
4104 method in completion mode. */
4105 {
4106 static const char *expected_syms[] = {
4107 "t1_func",
4108 "t1_func1",
5c58de74
PA
4109 };
4110
fcf23d5b
SM
4111 SELF_CHECK (check_find_bounds_finds
4112 (mock_index, "t1_func", expected_syms,
4113 NULL /* per_objfile */));
5c58de74
PA
4114 }
4115
4116 /* Check that the increment-last-char in the name matching algorithm
4117 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4118 {
4119 static const char *expected_syms1[] = {
4120 "\377",
4121 "\377\377123",
4122 };
fcf23d5b
SM
4123 SELF_CHECK (check_find_bounds_finds
4124 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4125
4126 static const char *expected_syms2[] = {
4127 "\377\377123",
4128 };
fcf23d5b
SM
4129 SELF_CHECK (check_find_bounds_finds
4130 (mock_index, "\377\377", expected_syms2,
4131 NULL /* per_objfile */));
5c58de74
PA
4132 }
4133}
4134
4135/* Test dw2_expand_symtabs_matching_symbol. */
4136
4137static void
4138test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4139{
4140 mock_mapped_index mock_index (test_symbols);
4141
4142 /* We let all tests run until the end even if some fails, for debug
4143 convenience. */
4144 bool any_mismatch = false;
4145
4146 /* Create the expected symbols list (an initializer_list). Needed
4147 because lists have commas, and we need to pass them to CHECK,
4148 which is a macro. */
4149#define EXPECT(...) { __VA_ARGS__ }
4150
4151 /* Wrapper for check_match that passes down the current
4152 __FILE__/__LINE__. */
4153#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4154 any_mismatch |= !check_match (__FILE__, __LINE__, \
4155 mock_index, \
4156 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4157 EXPECTED_LIST, NULL)
c62446b1
PA
4158
4159 /* Identity checks. */
4160 for (const char *sym : test_symbols)
4161 {
4162 /* Should be able to match all existing symbols. */
4163 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4164 EXPECT (sym));
4165
4166 /* Should be able to match all existing symbols with
4167 parameters. */
4168 std::string with_params = std::string (sym) + "(int)";
4169 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4170 EXPECT (sym));
4171
4172 /* Should be able to match all existing symbols with
4173 parameters and qualifiers. */
4174 with_params = std::string (sym) + " ( int ) const";
4175 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4176 EXPECT (sym));
4177
4178 /* This should really find sym, but cp-name-parser.y doesn't
4179 know about lvalue/rvalue qualifiers yet. */
4180 with_params = std::string (sym) + " ( int ) &&";
4181 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4182 {});
4183 }
4184
e1ef7d7a
PA
4185 /* Check that the name matching algorithm for completion doesn't get
4186 confused with Latin1 'ÿ' / 0xff. */
4187 {
4188 static const char str[] = "\377";
4189 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4190 EXPECT ("\377", "\377\377123"));
4191 }
4192
4193 /* Check that the increment-last-char in the matching algorithm for
4194 completion doesn't match "t1_fund" when completing "t1_func". */
4195 {
4196 static const char str[] = "t1_func";
4197 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4198 EXPECT ("t1_func", "t1_func1"));
4199 }
4200
c62446b1
PA
4201 /* Check that completion mode works at each prefix of the expected
4202 symbol name. */
4203 {
4204 static const char str[] = "function(int)";
4205 size_t len = strlen (str);
4206 std::string lookup;
4207
4208 for (size_t i = 1; i < len; i++)
4209 {
4210 lookup.assign (str, i);
4211 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4212 EXPECT ("function"));
4213 }
4214 }
4215
4216 /* While "w" is a prefix of both components, the match function
4217 should still only be called once. */
4218 {
4219 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4220 EXPECT ("w1::w2"));
a20714ff
PA
4221 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4222 EXPECT ("w1::w2"));
c62446b1
PA
4223 }
4224
4225 /* Same, with a "complicated" symbol. */
4226 {
4227 static const char str[] = Z_SYM_NAME;
4228 size_t len = strlen (str);
4229 std::string lookup;
4230
4231 for (size_t i = 1; i < len; i++)
4232 {
4233 lookup.assign (str, i);
4234 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4235 EXPECT (Z_SYM_NAME));
4236 }
4237 }
4238
4239 /* In FULL mode, an incomplete symbol doesn't match. */
4240 {
4241 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4242 {});
4243 }
4244
4245 /* A complete symbol with parameters matches any overload, since the
4246 index has no overload info. */
4247 {
4248 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4249 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4250 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4251 EXPECT ("std::zfunction", "std::zfunction2"));
4252 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4253 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4254 }
4255
4256 /* Check that whitespace is ignored appropriately. A symbol with a
4257 template argument list. */
4258 {
4259 static const char expected[] = "ns::foo<int>";
4260 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4261 EXPECT (expected));
a20714ff
PA
4262 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4263 EXPECT (expected));
c62446b1
PA
4264 }
4265
4266 /* Check that whitespace is ignored appropriately. A symbol with a
4267 template argument list that includes a pointer. */
4268 {
4269 static const char expected[] = "ns::foo<char*>";
4270 /* Try both completion and non-completion modes. */
4271 static const bool completion_mode[2] = {false, true};
4272 for (size_t i = 0; i < 2; i++)
4273 {
4274 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4275 completion_mode[i], EXPECT (expected));
a20714ff
PA
4276 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4277 completion_mode[i], EXPECT (expected));
c62446b1
PA
4278
4279 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4280 completion_mode[i], EXPECT (expected));
a20714ff
PA
4281 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4282 completion_mode[i], EXPECT (expected));
c62446b1
PA
4283 }
4284 }
4285
4286 {
4287 /* Check method qualifiers are ignored. */
4288 static const char expected[] = "ns::foo<char*>";
4289 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4290 symbol_name_match_type::FULL, true, EXPECT (expected));
4291 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4292 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4293 CHECK_MATCH ("foo < char * > ( int ) const",
4294 symbol_name_match_type::WILD, true, EXPECT (expected));
4295 CHECK_MATCH ("foo < char * > ( int ) &&",
4296 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4297 }
4298
4299 /* Test lookup names that don't match anything. */
4300 {
a20714ff
PA
4301 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4302 {});
4303
c62446b1
PA
4304 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4305 {});
4306 }
4307
a20714ff
PA
4308 /* Some wild matching tests, exercising "(anonymous namespace)",
4309 which should not be confused with a parameter list. */
4310 {
4311 static const char *syms[] = {
4312 "A::B::C",
4313 "B::C",
4314 "C",
4315 "A :: B :: C ( int )",
4316 "B :: C ( int )",
4317 "C ( int )",
4318 };
4319
4320 for (const char *s : syms)
4321 {
4322 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4323 EXPECT ("(anonymous namespace)::A::B::C"));
4324 }
4325 }
4326
4327 {
4328 static const char expected[] = "ns2::tmpl<int>::foo2";
4329 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4330 EXPECT (expected));
4331 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4332 EXPECT (expected));
4333 }
4334
c62446b1
PA
4335 SELF_CHECK (!any_mismatch);
4336
4337#undef EXPECT
4338#undef CHECK_MATCH
4339}
4340
5c58de74
PA
4341static void
4342run_test ()
4343{
4344 test_mapped_index_find_name_component_bounds ();
4345 test_dw2_expand_symtabs_matching_symbol ();
4346}
4347
c62446b1
PA
4348}} // namespace selftests::dw2_expand_symtabs_matching
4349
4350#endif /* GDB_SELF_TEST */
4351
4b514bc8
JK
4352/* If FILE_MATCHER is NULL or if PER_CU has
4353 dwarf2_per_cu_quick_data::MARK set (see
4354 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4355 EXPANSION_NOTIFY on it. */
4356
df35e626 4357static bool
4b514bc8 4358dw2_expand_symtabs_matching_one
97a1449a
SM
4359 (dwarf2_per_cu_data *per_cu,
4360 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4361 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4362 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4363{
4364 if (file_matcher == NULL || per_cu->v.quick->mark)
4365 {
af758d11 4366 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4367
97a1449a
SM
4368 compunit_symtab *symtab
4369 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4370 gdb_assert (symtab != nullptr);
4b514bc8 4371
af758d11 4372 if (expansion_notify != NULL && symtab_was_null)
df35e626 4373 return expansion_notify (symtab);
4b514bc8 4374 }
df35e626 4375 return true;
4b514bc8
JK
4376}
4377
3f563c84
PA
4378/* Helper for dw2_expand_matching symtabs. Called on each symbol
4379 matched, to expand corresponding CUs that were marked. IDX is the
4380 index of the symbol name that matched. */
4381
df35e626 4382static bool
3f563c84 4383dw2_expand_marked_cus
976ca316 4384 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4385 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4386 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4387 block_search_flags search_flags,
3f563c84
PA
4388 search_domain kind)
4389{
3f563c84
PA
4390 offset_type *vec, vec_len, vec_idx;
4391 bool global_seen = false;
976ca316 4392 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4393
61920122 4394 vec = (offset_type *) (index.constant_pool
f00a2de2 4395 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4396 vec_len = MAYBE_SWAP (vec[0]);
4397 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4398 {
61920122
PA
4399 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4400 /* This value is only valid for index versions >= 7. */
4401 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4402 gdb_index_symbol_kind symbol_kind =
4403 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4404 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4405 /* Only check the symbol attributes if they're present.
4406 Indices prior to version 7 don't record them,
4407 and indices >= 7 may elide them for certain symbols
4408 (gold does this). */
4409 int attrs_valid =
4410 (index.version >= 7
4411 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4412
4413 /* Work around gold/15646. */
f030440d
TV
4414 if (attrs_valid
4415 && !is_static
4416 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4417 {
f030440d 4418 if (global_seen)
61920122 4419 continue;
f030440d
TV
4420
4421 global_seen = true;
61920122 4422 }
3190f0c6 4423
61920122
PA
4424 /* Only check the symbol's kind if it has one. */
4425 if (attrs_valid)
4426 {
03a8ea51
TT
4427 if (is_static)
4428 {
4429 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4430 continue;
4431 }
4432 else
4433 {
4434 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4435 continue;
4436 }
4437
61920122 4438 switch (kind)
8943b874 4439 {
61920122
PA
4440 case VARIABLES_DOMAIN:
4441 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4442 continue;
4443 break;
4444 case FUNCTIONS_DOMAIN:
4445 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4446 continue;
61920122
PA
4447 break;
4448 case TYPES_DOMAIN:
4449 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4450 continue;
4451 break;
59c35742
AB
4452 case MODULES_DOMAIN:
4453 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4454 continue;
4455 break;
61920122
PA
4456 default:
4457 break;
8943b874 4458 }
61920122 4459 }
8943b874 4460
61920122 4461 /* Don't crash on bad data. */
976ca316
SM
4462 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4463 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4464 {
b98664d3 4465 complaint (_(".gdb_index entry has bad CU index"
976ca316 4466 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4467 continue;
4468 }
4469
976ca316 4470 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
df35e626
TT
4471 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4472 expansion_notify))
4473 return false;
61920122 4474 }
df35e626
TT
4475
4476 return true;
61920122
PA
4477}
4478
4b514bc8
JK
4479/* If FILE_MATCHER is non-NULL, set all the
4480 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4481 that match FILE_MATCHER. */
4482
61920122 4483static void
4b514bc8 4484dw_expand_symtabs_matching_file_matcher
976ca316 4485 (dwarf2_per_objfile *per_objfile,
ed2dc618 4486 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4487{
4b514bc8 4488 if (file_matcher == NULL)
61920122
PA
4489 return;
4490
4b514bc8
JK
4491 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4492 htab_eq_pointer,
4493 NULL, xcalloc, xfree));
4494 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4495 htab_eq_pointer,
4496 NULL, xcalloc, xfree));
61920122 4497
4b514bc8
JK
4498 /* The rule is CUs specify all the files, including those used by
4499 any TU, so there's no need to scan TUs here. */
61920122 4500
976ca316 4501 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4502 {
927aa2e7
JK
4503 QUIT;
4504
4505 per_cu->v.quick->mark = 0;
4506
4507 /* We only need to look at symtabs not already expanded. */
976ca316 4508 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4509 continue;
4510
976ca316 4511 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4512 if (file_data == NULL)
4513 continue;
4514
4515 if (htab_find (visited_not_found.get (), file_data) != NULL)
4516 continue;
4517 else if (htab_find (visited_found.get (), file_data) != NULL)
4518 {
4519 per_cu->v.quick->mark = 1;
4520 continue;
4521 }
4522
b76e467d 4523 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4524 {
4525 const char *this_real_name;
4526
4527 if (file_matcher (file_data->file_names[j], false))
4528 {
4529 per_cu->v.quick->mark = 1;
4530 break;
4531 }
4532
4533 /* Before we invoke realpath, which can get expensive when many
4534 files are involved, do a quick comparison of the basenames. */
4535 if (!basenames_may_differ
4536 && !file_matcher (lbasename (file_data->file_names[j]),
4537 true))
4538 continue;
4539
976ca316 4540 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4541 if (file_matcher (this_real_name, false))
4542 {
4543 per_cu->v.quick->mark = 1;
4544 break;
4545 }
4546 }
4547
b76e467d
SM
4548 void **slot = htab_find_slot (per_cu->v.quick->mark
4549 ? visited_found.get ()
4550 : visited_not_found.get (),
4551 file_data, INSERT);
927aa2e7
JK
4552 *slot = file_data;
4553 }
4554}
4555
df35e626 4556bool
af82f89d
TT
4557dwarf2_gdb_index::expand_symtabs_matching
4558 (struct objfile *objfile,
4559 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4560 const lookup_name_info *lookup_name,
4561 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4562 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4563 block_search_flags search_flags,
3bfa51a7 4564 domain_enum domain,
af82f89d 4565 enum search_domain kind)
927aa2e7 4566{
976ca316 4567 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4568
4569 /* index_table is NULL if OBJF_READNOW. */
976ca316 4570 if (!per_objfile->per_bfd->index_table)
df35e626 4571 return true;
927aa2e7 4572
976ca316 4573 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4574
c1a66c06
TV
4575 if (symbol_matcher == NULL && lookup_name == NULL)
4576 {
976ca316 4577 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4578 {
4579 QUIT;
4580
df35e626
TT
4581 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4582 file_matcher,
4583 expansion_notify))
4584 return false;
c1a66c06 4585 }
df35e626 4586 return true;
c1a66c06
TV
4587 }
4588
976ca316 4589 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4590
df35e626
TT
4591 bool result
4592 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4593 symbol_matcher,
4594 [&] (offset_type idx)
927aa2e7 4595 {
df35e626 4596 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
03a8ea51 4597 expansion_notify, search_flags, kind))
df35e626 4598 return false;
3b00ef10 4599 return true;
976ca316 4600 }, per_objfile);
df35e626
TT
4601
4602 return result;
927aa2e7
JK
4603}
4604
4605/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4606 symtab. */
4607
4608static struct compunit_symtab *
4609recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4610 CORE_ADDR pc)
4611{
4612 int i;
4613
4614 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4615 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4616 return cust;
4617
4618 if (cust->includes == NULL)
4619 return NULL;
4620
4621 for (i = 0; cust->includes[i]; ++i)
4622 {
4623 struct compunit_symtab *s = cust->includes[i];
4624
4625 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4626 if (s != NULL)
4627 return s;
4628 }
4629
4630 return NULL;
4631}
4632
39298a5d
TT
4633struct compunit_symtab *
4634dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4635 (struct objfile *objfile,
4636 struct bound_minimal_symbol msymbol,
4637 CORE_ADDR pc,
4638 struct obj_section *section,
4639 int warn_if_readin)
927aa2e7
JK
4640{
4641 struct dwarf2_per_cu_data *data;
4642 struct compunit_symtab *result;
4643
efd7398e
TT
4644 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4645 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4646 return NULL;
4647
b3b3bada 4648 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4649 data = ((struct dwarf2_per_cu_data *)
4650 addrmap_find (per_objfile->per_bfd->index_addrmap,
4651 pc - baseaddr));
927aa2e7
JK
4652 if (!data)
4653 return NULL;
4654
af758d11 4655 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4656 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4657 paddress (objfile->arch (), pc));
927aa2e7 4658
97a1449a
SM
4659 result = recursively_find_pc_sect_compunit_symtab
4660 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4661
927aa2e7
JK
4662 gdb_assert (result != NULL);
4663 return result;
4664}
4665
39298a5d 4666void
f4655dee
TT
4667dwarf2_base_index_functions::map_symbol_filenames
4668 (struct objfile *objfile,
4669 gdb::function_view<symbol_filename_ftype> fun,
4670 bool need_fullname)
927aa2e7 4671{
976ca316 4672 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4673
976ca316 4674 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4675 {
976ca316 4676 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4677
4678 htab_up visited (htab_create_alloc (10,
4679 htab_hash_pointer, htab_eq_pointer,
4680 NULL, xcalloc, xfree));
4681
4682 /* The rule is CUs specify all the files, including those used
4683 by any TU, so there's no need to scan TUs here. We can
4684 ignore file names coming from already-expanded CUs. */
4685
976ca316 4686 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4687 {
976ca316 4688 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4689 {
4690 void **slot = htab_find_slot (visited.get (),
4691 per_cu->v.quick->file_names,
4692 INSERT);
4693
4694 *slot = per_cu->v.quick->file_names;
4695 }
4696 }
4697
976ca316 4698 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4699 {
927aa2e7 4700 /* We only need to look at symtabs not already expanded. */
976ca316 4701 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4702 continue;
4703
ab432490 4704 quick_file_names *file_data
976ca316 4705 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4706 if (file_data == NULL)
4707 continue;
4708
b76e467d 4709 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4710 if (*slot)
4711 {
4712 /* Already visited. */
4713 continue;
4714 }
4715 *slot = file_data;
4716
4717 for (int j = 0; j < file_data->num_file_names; ++j)
4718 {
4719 const char *filename = file_data->file_names[j];
976ca316 4720 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4721 }
4722 }
4723 }
4724
976ca316 4725 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4726 {
4727 gdb::unique_xmalloc_ptr<char> this_real_name;
4728
4729 if (need_fullname)
4730 this_real_name = gdb_realpath (filename);
f4655dee 4731 fun (filename, this_real_name.get ());
927aa2e7
JK
4732 });
4733}
4734
39298a5d
TT
4735bool
4736dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 4737{
fae2120b 4738 return true;
927aa2e7
JK
4739}
4740
927aa2e7
JK
4741/* DWARF-5 debug_names reader. */
4742
4743/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4744static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4745
4746/* A helper function that reads the .debug_names section in SECTION
4747 and fills in MAP. FILENAME is the name of the file containing the
4748 section; it is used for error reporting.
4749
4750 Returns true if all went well, false otherwise. */
4751
4752static bool
4753read_debug_names_from_section (struct objfile *objfile,
4754 const char *filename,
4755 struct dwarf2_section_info *section,
4756 mapped_debug_names &map)
4757{
96b79293 4758 if (section->empty ())
927aa2e7
JK
4759 return false;
4760
4761 /* Older elfutils strip versions could keep the section in the main
4762 executable while splitting it for the separate debug info file. */
96b79293 4763 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4764 return false;
4765
96b79293 4766 section->read (objfile);
927aa2e7 4767
08feed99 4768 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4769
4770 const gdb_byte *addr = section->buffer;
4771
96b79293 4772 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4773
4774 unsigned int bytes_read;
4775 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4776 addr += bytes_read;
4777
4778 map.dwarf5_is_dwarf64 = bytes_read != 4;
4779 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4780 if (bytes_read + length != section->size)
4781 {
4782 /* There may be multiple per-CU indices. */
4783 warning (_("Section .debug_names in %s length %s does not match "
4784 "section length %s, ignoring .debug_names."),
4785 filename, plongest (bytes_read + length),
4786 pulongest (section->size));
4787 return false;
4788 }
4789
4790 /* The version number. */
4791 uint16_t version = read_2_bytes (abfd, addr);
4792 addr += 2;
4793 if (version != 5)
4794 {
4795 warning (_("Section .debug_names in %s has unsupported version %d, "
4796 "ignoring .debug_names."),
4797 filename, version);
4798 return false;
4799 }
4800
4801 /* Padding. */
4802 uint16_t padding = read_2_bytes (abfd, addr);
4803 addr += 2;
4804 if (padding != 0)
4805 {
4806 warning (_("Section .debug_names in %s has unsupported padding %d, "
4807 "ignoring .debug_names."),
4808 filename, padding);
4809 return false;
4810 }
4811
4812 /* comp_unit_count - The number of CUs in the CU list. */
4813 map.cu_count = read_4_bytes (abfd, addr);
4814 addr += 4;
4815
4816 /* local_type_unit_count - The number of TUs in the local TU
4817 list. */
4818 map.tu_count = read_4_bytes (abfd, addr);
4819 addr += 4;
4820
4821 /* foreign_type_unit_count - The number of TUs in the foreign TU
4822 list. */
4823 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4824 addr += 4;
4825 if (foreign_tu_count != 0)
4826 {
4827 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4828 "ignoring .debug_names."),
4829 filename, static_cast<unsigned long> (foreign_tu_count));
4830 return false;
4831 }
4832
4833 /* bucket_count - The number of hash buckets in the hash lookup
4834 table. */
4835 map.bucket_count = read_4_bytes (abfd, addr);
4836 addr += 4;
4837
4838 /* name_count - The number of unique names in the index. */
4839 map.name_count = read_4_bytes (abfd, addr);
4840 addr += 4;
4841
4842 /* abbrev_table_size - The size in bytes of the abbreviations
4843 table. */
4844 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4845 addr += 4;
4846
4847 /* augmentation_string_size - The size in bytes of the augmentation
4848 string. This value is rounded up to a multiple of 4. */
4849 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4850 addr += 4;
4851 map.augmentation_is_gdb = ((augmentation_string_size
4852 == sizeof (dwarf5_augmentation))
4853 && memcmp (addr, dwarf5_augmentation,
4854 sizeof (dwarf5_augmentation)) == 0);
4855 augmentation_string_size += (-augmentation_string_size) & 3;
4856 addr += augmentation_string_size;
4857
4858 /* List of CUs */
4859 map.cu_table_reordered = addr;
4860 addr += map.cu_count * map.offset_size;
4861
4862 /* List of Local TUs */
4863 map.tu_table_reordered = addr;
4864 addr += map.tu_count * map.offset_size;
4865
4866 /* Hash Lookup Table */
4867 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4868 addr += map.bucket_count * 4;
4869 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4870 addr += map.name_count * 4;
4871
4872 /* Name Table */
4873 map.name_table_string_offs_reordered = addr;
4874 addr += map.name_count * map.offset_size;
4875 map.name_table_entry_offs_reordered = addr;
4876 addr += map.name_count * map.offset_size;
4877
4878 const gdb_byte *abbrev_table_start = addr;
4879 for (;;)
4880 {
927aa2e7
JK
4881 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4882 addr += bytes_read;
4883 if (index_num == 0)
4884 break;
4885
4886 const auto insertpair
4887 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4888 if (!insertpair.second)
4889 {
4890 warning (_("Section .debug_names in %s has duplicate index %s, "
4891 "ignoring .debug_names."),
4892 filename, pulongest (index_num));
4893 return false;
4894 }
4895 mapped_debug_names::index_val &indexval = insertpair.first->second;
4896 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4897 addr += bytes_read;
4898
4899 for (;;)
4900 {
4901 mapped_debug_names::index_val::attr attr;
4902 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4903 addr += bytes_read;
4904 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4905 addr += bytes_read;
4906 if (attr.form == DW_FORM_implicit_const)
4907 {
4908 attr.implicit_const = read_signed_leb128 (abfd, addr,
4909 &bytes_read);
4910 addr += bytes_read;
4911 }
4912 if (attr.dw_idx == 0 && attr.form == 0)
4913 break;
4914 indexval.attr_vec.push_back (std::move (attr));
4915 }
4916 }
4917 if (addr != abbrev_table_start + abbrev_table_size)
4918 {
4919 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4920 "of size %s vs. written as %u, ignoring .debug_names."),
4921 filename, plongest (addr - abbrev_table_start),
4922 abbrev_table_size);
927aa2e7
JK
4923 return false;
4924 }
4925 map.entry_pool = addr;
4926
4927 return true;
4928}
4929
4930/* A helper for create_cus_from_debug_names that handles the MAP's CU
4931 list. */
4932
4933static void
168c9250 4934create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4935 const mapped_debug_names &map,
4936 dwarf2_section_info &section,
b76e467d 4937 bool is_dwz)
927aa2e7 4938{
3ee6bb11
TV
4939 if (!map.augmentation_is_gdb)
4940 {
fc9a13fb
TV
4941 for (uint32_t i = 0; i < map.cu_count; ++i)
4942 {
4943 sect_offset sect_off
4944 = (sect_offset) (extract_unsigned_integer
4945 (map.cu_table_reordered + i * map.offset_size,
4946 map.offset_size,
4947 map.dwarf5_byte_order));
4948 /* We don't know the length of the CU, because the CU list in a
4949 .debug_names index can be incomplete, so we can't use the start
4950 of the next CU as end of this CU. We create the CUs here with
4951 length 0, and in cutu_reader::cutu_reader we'll fill in the
4952 actual length. */
4953 dwarf2_per_cu_data *per_cu
4954 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4955 sect_off, 0);
4956 per_bfd->all_comp_units.push_back (per_cu);
4957 }
d3b54e63 4958 return;
3ee6bb11
TV
4959 }
4960
927aa2e7
JK
4961 sect_offset sect_off_prev;
4962 for (uint32_t i = 0; i <= map.cu_count; ++i)
4963 {
4964 sect_offset sect_off_next;
4965 if (i < map.cu_count)
4966 {
4967 sect_off_next
4968 = (sect_offset) (extract_unsigned_integer
4969 (map.cu_table_reordered + i * map.offset_size,
4970 map.offset_size,
4971 map.dwarf5_byte_order));
4972 }
4973 else
4974 sect_off_next = (sect_offset) section.size;
4975 if (i >= 1)
4976 {
4977 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 4978 dwarf2_per_cu_data *per_cu
168c9250 4979 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 4980 sect_off_prev, length);
168c9250 4981 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
4982 }
4983 sect_off_prev = sect_off_next;
4984 }
4985}
4986
4987/* Read the CU list from the mapped index, and use it to create all
ed2dc618 4988 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
4989
4990static void
168c9250 4991create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4992 const mapped_debug_names &map,
4993 const mapped_debug_names &dwz_map)
4994{
168c9250
SM
4995 gdb_assert (per_bfd->all_comp_units.empty ());
4996 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 4997
168c9250 4998 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 4999 false /* is_dwz */);
927aa2e7
JK
5000
5001 if (dwz_map.cu_count == 0)
5002 return;
5003
168c9250
SM
5004 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5005 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5006 true /* is_dwz */);
927aa2e7
JK
5007}
5008
5009/* Read .debug_names. If everything went ok, initialize the "quick"
5010 elements of all the CUs and return true. Otherwise, return false. */
5011
5012static bool
976ca316 5013dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5014{
fcf23d5b
SM
5015 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5016 mapped_debug_names dwz_map;
976ca316 5017 struct objfile *objfile = per_objfile->objfile;
f8c41851 5018 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5019
5020 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
bab287cd 5021 &per_bfd->debug_names, *map))
927aa2e7
JK
5022 return false;
5023
5024 /* Don't use the index if it's empty. */
22ca247e 5025 if (map->name_count == 0)
927aa2e7
JK
5026 return false;
5027
5028 /* If there is a .dwz file, read it so we can get its CU list as
5029 well. */
f8c41851 5030 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5031 if (dwz != NULL)
5032 {
5033 if (!read_debug_names_from_section (objfile,
00f93c44 5034 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5035 &dwz->debug_names, dwz_map))
5036 {
5037 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5038 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5039 return false;
5040 }
5041 }
5042
f8c41851 5043 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5044
22ca247e 5045 if (map->tu_count != 0)
927aa2e7
JK
5046 {
5047 /* We can only handle a single .debug_types when we have an
5048 index. */
f8c41851 5049 if (per_bfd->types.size () != 1)
927aa2e7
JK
5050 return false;
5051
f8c41851 5052 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5053
5054 create_signatured_type_table_from_debug_names
f8c41851 5055 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5056 }
5057
f8c41851 5058 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5059
f8c41851
SM
5060 per_bfd->debug_names_table = std::move (map);
5061 per_bfd->using_index = 1;
5062 per_bfd->quick_file_names_table =
bab287cd 5063 create_quick_file_names_table (per_bfd->all_comp_units.size ());
927aa2e7
JK
5064
5065 return true;
5066}
5067
927aa2e7
JK
5068/* Type used to manage iterating over all CUs looking for a symbol for
5069 .debug_names. */
5070
5071class dw2_debug_names_iterator
5072{
5073public:
927aa2e7 5074 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5075 block_search_flags block_index,
2b79f376 5076 domain_enum domain,
fcf23d5b 5077 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5078 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5079 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5080 m_per_objfile (per_objfile)
927aa2e7
JK
5081 {}
5082
5083 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5084 search_domain search, uint32_t namei,
3bfa51a7
TT
5085 dwarf2_per_objfile *per_objfile,
5086 domain_enum domain = UNDEF_DOMAIN)
927aa2e7 5087 : m_map (map),
3bfa51a7 5088 m_domain (domain),
927aa2e7 5089 m_search (search),
fcf23d5b
SM
5090 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5091 m_per_objfile (per_objfile)
927aa2e7
JK
5092 {}
5093
3b00ef10 5094 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5095 block_search_flags block_index, domain_enum domain,
fcf23d5b 5096 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5097 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5098 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5099 m_per_objfile (per_objfile)
3b00ef10
TT
5100 {}
5101
927aa2e7
JK
5102 /* Return the next matching CU or NULL if there are no more. */
5103 dwarf2_per_cu_data *next ();
5104
5105private:
5106 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5107 const char *name,
5108 dwarf2_per_objfile *per_objfile);
927aa2e7 5109 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5110 uint32_t namei,
5111 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5112
5113 /* The internalized form of .debug_names. */
5114 const mapped_debug_names &m_map;
5115
03a8ea51
TT
5116 /* Restrict the search to these blocks. */
5117 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
5118 | SEARCH_STATIC_BLOCK);
927aa2e7
JK
5119
5120 /* The kind of symbol we're looking for. */
5121 const domain_enum m_domain = UNDEF_DOMAIN;
5122 const search_domain m_search = ALL_DOMAIN;
5123
5124 /* The list of CUs from the index entry of the symbol, or NULL if
5125 not found. */
5126 const gdb_byte *m_addr;
fcf23d5b
SM
5127
5128 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5129};
5130
5131const char *
fcf23d5b 5132mapped_debug_names::namei_to_name
976ca316 5133 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5134{
5135 const ULONGEST namei_string_offs
5136 = extract_unsigned_integer ((name_table_string_offs_reordered
5137 + namei * offset_size),
5138 offset_size,
5139 dwarf5_byte_order);
976ca316 5140 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5141}
5142
5143/* Find a slot in .debug_names for the object named NAME. If NAME is
5144 found, return pointer to its pool data. If NAME cannot be found,
5145 return NULL. */
5146
5147const gdb_byte *
5148dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5149 (const mapped_debug_names &map, const char *name,
5150 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5151{
5152 int (*cmp) (const char *, const char *);
5153
54ee4252 5154 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5155 if (current_language->la_language == language_cplus
5156 || current_language->la_language == language_fortran
5157 || current_language->la_language == language_d)
5158 {
5159 /* NAME is already canonical. Drop any qualifiers as
5160 .debug_names does not contain any. */
5161
5162 if (strchr (name, '(') != NULL)
5163 {
54ee4252 5164 without_params = cp_remove_params (name);
927aa2e7 5165 if (without_params != NULL)
54ee4252 5166 name = without_params.get ();
927aa2e7
JK
5167 }
5168 }
5169
5170 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5171
5172 const uint32_t full_hash = dwarf5_djb_hash (name);
5173 uint32_t namei
5174 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5175 (map.bucket_table_reordered
5176 + (full_hash % map.bucket_count)), 4,
5177 map.dwarf5_byte_order);
5178 if (namei == 0)
5179 return NULL;
5180 --namei;
5181 if (namei >= map.name_count)
5182 {
b98664d3 5183 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5184 "[in module %s]"),
5185 namei, map.name_count,
fcf23d5b 5186 objfile_name (per_objfile->objfile));
927aa2e7
JK
5187 return NULL;
5188 }
5189
5190 for (;;)
5191 {
5192 const uint32_t namei_full_hash
5193 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5194 (map.hash_table_reordered + namei), 4,
5195 map.dwarf5_byte_order);
5196 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5197 return NULL;
5198
5199 if (full_hash == namei_full_hash)
5200 {
fcf23d5b 5201 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5202
5203#if 0 /* An expensive sanity check. */
5204 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5205 {
b98664d3 5206 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5207 "[in module %s]"),
5208 namei, objfile_name (dwarf2_per_objfile->objfile));
5209 return NULL;
5210 }
5211#endif
5212
5213 if (cmp (namei_string, name) == 0)
5214 {
5215 const ULONGEST namei_entry_offs
5216 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5217 + namei * map.offset_size),
5218 map.offset_size, map.dwarf5_byte_order);
5219 return map.entry_pool + namei_entry_offs;
5220 }
5221 }
5222
5223 ++namei;
5224 if (namei >= map.name_count)
5225 return NULL;
5226 }
5227}
5228
5229const gdb_byte *
5230dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5231 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5232{
5233 if (namei >= map.name_count)
5234 {
b98664d3 5235 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5236 "[in module %s]"),
5237 namei, map.name_count,
fcf23d5b 5238 objfile_name (per_objfile->objfile));
927aa2e7
JK
5239 return NULL;
5240 }
5241
5242 const ULONGEST namei_entry_offs
5243 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5244 + namei * map.offset_size),
5245 map.offset_size, map.dwarf5_byte_order);
5246 return map.entry_pool + namei_entry_offs;
5247}
5248
5249/* See dw2_debug_names_iterator. */
5250
5251dwarf2_per_cu_data *
5252dw2_debug_names_iterator::next ()
5253{
5254 if (m_addr == NULL)
5255 return NULL;
5256
fcf23d5b
SM
5257 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5258 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5259 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5260
5261 again:
5262
5263 unsigned int bytes_read;
5264 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5265 m_addr += bytes_read;
5266 if (abbrev == 0)
5267 return NULL;
5268
5269 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5270 if (indexval_it == m_map.abbrev_map.cend ())
5271 {
b98664d3 5272 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5273 "[in module %s]"),
ed2dc618 5274 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5275 return NULL;
5276 }
5277 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5278 enum class symbol_linkage {
5279 unknown,
5280 static_,
5281 extern_,
23c13d42 5282 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5283 dwarf2_per_cu_data *per_cu = NULL;
5284 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5285 {
5286 ULONGEST ull;
5287 switch (attr.form)
5288 {
5289 case DW_FORM_implicit_const:
5290 ull = attr.implicit_const;
5291 break;
5292 case DW_FORM_flag_present:
5293 ull = 1;
5294 break;
5295 case DW_FORM_udata:
5296 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5297 m_addr += bytes_read;
5298 break;
6dc55ce9 5299 case DW_FORM_ref4:
5300 ull = read_4_bytes (abfd, m_addr);
5301 m_addr += 4;
5302 break;
5303 case DW_FORM_ref8:
5304 ull = read_8_bytes (abfd, m_addr);
5305 m_addr += 8;
5306 break;
5307 case DW_FORM_ref_sig8:
5308 ull = read_8_bytes (abfd, m_addr);
5309 m_addr += 8;
5310 break;
927aa2e7 5311 default:
b98664d3 5312 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5313 dwarf_form_name (attr.form),
ed2dc618 5314 objfile_name (objfile));
927aa2e7
JK
5315 return NULL;
5316 }
5317 switch (attr.dw_idx)
5318 {
5319 case DW_IDX_compile_unit:
5320 /* Don't crash on bad data. */
bab287cd 5321 if (ull >= per_bfd->all_comp_units.size ())
927aa2e7 5322 {
b98664d3 5323 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5324 " [in module %s]"),
5325 pulongest (ull),
fcf23d5b 5326 objfile_name (objfile));
927aa2e7
JK
5327 continue;
5328 }
fcf23d5b 5329 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5330 break;
8af5c486
JK
5331 case DW_IDX_type_unit:
5332 /* Don't crash on bad data. */
fcf23d5b 5333 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5334 {
b98664d3 5335 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5336 " [in module %s]"),
5337 pulongest (ull),
fcf23d5b 5338 objfile_name (objfile));
8af5c486
JK
5339 continue;
5340 }
fcf23d5b 5341 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5342 break;
6dc55ce9 5343 case DW_IDX_die_offset:
5344 /* In a per-CU index (as opposed to a per-module index), index
5345 entries without CU attribute implicitly refer to the single CU. */
5346 if (per_cu == NULL)
fcf23d5b 5347 per_cu = per_bfd->get_cu (0);
6dc55ce9 5348 break;
927aa2e7
JK
5349 case DW_IDX_GNU_internal:
5350 if (!m_map.augmentation_is_gdb)
5351 break;
23c13d42 5352 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5353 break;
5354 case DW_IDX_GNU_external:
5355 if (!m_map.augmentation_is_gdb)
5356 break;
23c13d42 5357 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5358 break;
5359 }
5360 }
5361
5362 /* Skip if already read in. */
fcf23d5b 5363 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5364 goto again;
5365
5366 /* Check static vs global. */
03a8ea51 5367 if (symbol_linkage_ != symbol_linkage::unknown)
927aa2e7 5368 {
03a8ea51
TT
5369 if (symbol_linkage_ == symbol_linkage::static_)
5370 {
5371 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5372 goto again;
5373 }
5374 else
5375 {
5376 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5377 goto again;
5378 }
927aa2e7
JK
5379 }
5380
5381 /* Match dw2_symtab_iter_next, symbol_kind
5382 and debug_names::psymbol_tag. */
5383 switch (m_domain)
5384 {
5385 case VAR_DOMAIN:
5386 switch (indexval.dwarf_tag)
5387 {
5388 case DW_TAG_variable:
5389 case DW_TAG_subprogram:
5390 /* Some types are also in VAR_DOMAIN. */
5391 case DW_TAG_typedef:
5392 case DW_TAG_structure_type:
5393 break;
5394 default:
5395 goto again;
5396 }
5397 break;
5398 case STRUCT_DOMAIN:
5399 switch (indexval.dwarf_tag)
5400 {
5401 case DW_TAG_typedef:
5402 case DW_TAG_structure_type:
5403 break;
5404 default:
5405 goto again;
5406 }
5407 break;
5408 case LABEL_DOMAIN:
5409 switch (indexval.dwarf_tag)
5410 {
5411 case 0:
5412 case DW_TAG_variable:
5413 break;
5414 default:
5415 goto again;
5416 }
5417 break;
59c35742
AB
5418 case MODULE_DOMAIN:
5419 switch (indexval.dwarf_tag)
5420 {
5421 case DW_TAG_module:
5422 break;
5423 default:
5424 goto again;
5425 }
5426 break;
927aa2e7
JK
5427 default:
5428 break;
5429 }
5430
5431 /* Match dw2_expand_symtabs_matching, symbol_kind and
5432 debug_names::psymbol_tag. */
5433 switch (m_search)
4b514bc8 5434 {
927aa2e7
JK
5435 case VARIABLES_DOMAIN:
5436 switch (indexval.dwarf_tag)
4b514bc8 5437 {
927aa2e7
JK
5438 case DW_TAG_variable:
5439 break;
5440 default:
5441 goto again;
4b514bc8 5442 }
927aa2e7
JK
5443 break;
5444 case FUNCTIONS_DOMAIN:
5445 switch (indexval.dwarf_tag)
4b514bc8 5446 {
927aa2e7
JK
5447 case DW_TAG_subprogram:
5448 break;
5449 default:
5450 goto again;
4b514bc8 5451 }
927aa2e7
JK
5452 break;
5453 case TYPES_DOMAIN:
5454 switch (indexval.dwarf_tag)
5455 {
5456 case DW_TAG_typedef:
5457 case DW_TAG_structure_type:
5458 break;
5459 default:
5460 goto again;
5461 }
5462 break;
59c35742
AB
5463 case MODULES_DOMAIN:
5464 switch (indexval.dwarf_tag)
5465 {
5466 case DW_TAG_module:
5467 break;
5468 default:
5469 goto again;
5470 }
927aa2e7
JK
5471 default:
5472 break;
4b514bc8 5473 }
927aa2e7
JK
5474
5475 return per_cu;
4b514bc8 5476}
61920122 5477
927aa2e7
JK
5478/* This dumps minimal information about .debug_names. It is called
5479 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5480 uses this to verify that .debug_names has been loaded. */
9291a0cd 5481
39298a5d
TT
5482void
5483dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5484{
976ca316 5485 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5486
976ca316 5487 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5488 printf_filtered (".debug_names:");
976ca316 5489 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5490 printf_filtered (" exists\n");
5491 else
5492 printf_filtered (" faked for \"readnow\"\n");
5493 printf_filtered ("\n");
9291a0cd
TT
5494}
5495
39298a5d 5496void
0b7b2c2a 5497dwarf2_debug_names_index::expand_matching_symbols
3b00ef10
TT
5498 (struct objfile *objfile,
5499 const lookup_name_info &name, domain_enum domain,
5500 int global,
3b00ef10
TT
5501 symbol_compare_ftype *ordered_compare)
5502{
976ca316 5503 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5504
5505 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5506 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5507 return;
5508
976ca316 5509 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
03a8ea51
TT
5510 const block_search_flags block_flags
5511 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
3b00ef10
TT
5512
5513 const char *match_name = name.ada ().lookup_name ().c_str ();
5514 auto matcher = [&] (const char *symname)
5515 {
5516 if (ordered_compare == nullptr)
5517 return true;
5518 return ordered_compare (symname, match_name) == 0;
5519 };
5520
b64f703b 5521 dw2_expand_symtabs_matching_symbol (map, name, matcher,
3b00ef10
TT
5522 [&] (offset_type namei)
5523 {
5524 /* The name was matched, now expand corresponding CUs that were
5525 marked. */
03a8ea51 5526 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
976ca316 5527 per_objfile);
3b00ef10
TT
5528
5529 struct dwarf2_per_cu_data *per_cu;
5530 while ((per_cu = iter.next ()) != NULL)
976ca316 5531 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5532 nullptr);
3b00ef10 5533 return true;
976ca316 5534 }, per_objfile);
3b00ef10
TT
5535}
5536
df35e626 5537bool
39298a5d 5538dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5539 (struct objfile *objfile,
5540 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5541 const lookup_name_info *lookup_name,
927aa2e7
JK
5542 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5543 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 5544 block_search_flags search_flags,
3bfa51a7 5545 domain_enum domain,
927aa2e7
JK
5546 enum search_domain kind)
5547{
976ca316 5548 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5549
927aa2e7 5550 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5551 if (!per_objfile->per_bfd->debug_names_table)
df35e626 5552 return true;
9291a0cd 5553
976ca316 5554 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5555
c1a66c06
TV
5556 if (symbol_matcher == NULL && lookup_name == NULL)
5557 {
976ca316 5558 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5559 {
5560 QUIT;
5561
df35e626
TT
5562 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5563 file_matcher,
5564 expansion_notify))
5565 return false;
c1a66c06 5566 }
df35e626 5567 return true;
c1a66c06
TV
5568 }
5569
976ca316 5570 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5571
df35e626
TT
5572 bool result
5573 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5574 symbol_matcher,
5575 [&] (offset_type namei)
927aa2e7 5576 {
927aa2e7
JK
5577 /* The name was matched, now expand corresponding CUs that were
5578 marked. */
3bfa51a7 5579 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
bbf2f4df 5580
927aa2e7
JK
5581 struct dwarf2_per_cu_data *per_cu;
5582 while ((per_cu = iter.next ()) != NULL)
df35e626
TT
5583 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5584 file_matcher,
5585 expansion_notify))
5586 return false;
3b00ef10 5587 return true;
976ca316 5588 }, per_objfile);
df35e626
TT
5589
5590 return result;
9291a0cd
TT
5591}
5592
4485a1c1 5593/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5594 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5595
5596template <typename T>
5597static gdb::array_view<const gdb_byte>
5598get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5599{
5600 dwarf2_section_info *section = &section_owner->gdb_index;
5601
96b79293 5602 if (section->empty ())
4485a1c1
SM
5603 return {};
5604
5605 /* Older elfutils strip versions could keep the section in the main
5606 executable while splitting it for the separate debug info file. */
96b79293 5607 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5608 return {};
5609
96b79293 5610 section->read (obj);
4485a1c1 5611
8bebfcda
PA
5612 /* dwarf2_section_info::size is a bfd_size_type, while
5613 gdb::array_view works with size_t. On 32-bit hosts, with
5614 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5615 is 32-bit. So we need an explicit narrowing conversion here.
5616 This is fine, because it's impossible to allocate or mmap an
5617 array/buffer larger than what size_t can represent. */
5618 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5619}
5620
87d6a7aa
SM
5621/* Lookup the index cache for the contents of the index associated to
5622 DWARF2_OBJ. */
5623
5624static gdb::array_view<const gdb_byte>
5989a64e 5625get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5626{
5627 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5628 if (build_id == nullptr)
5629 return {};
5630
5631 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5632 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5633}
5634
5635/* Same as the above, but for DWZ. */
5636
5637static gdb::array_view<const gdb_byte>
5638get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5639{
5640 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5641 if (build_id == nullptr)
5642 return {};
5643
5644 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5645}
5646
edc02ceb 5647/* See dwarf2/public.h. */
9291a0cd 5648
edc02ceb
TT
5649void
5650dwarf2_initialize_objfile (struct objfile *objfile)
9291a0cd 5651{
976ca316
SM
5652 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5653 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5654
850ed749
SM
5655 dwarf_read_debug_printf ("called");
5656
9291a0cd
TT
5657 /* If we're about to read full symbols, don't bother with the
5658 indices. In this case we also don't care if some other debug
5659 format is making psymtabs, because they are all about to be
5660 expanded anyway. */
5661 if ((objfile->flags & OBJF_READNOW))
5662 {
850ed749
SM
5663 dwarf_read_debug_printf ("readnow requested");
5664
17ee85fc
TT
5665 /* When using READNOW, the using_index flag (set below) indicates that
5666 PER_BFD was already initialized, when we loaded some other objfile. */
5667 if (per_bfd->using_index)
5668 {
850ed749 5669 dwarf_read_debug_printf ("using_index already set");
976ca316 5670 per_objfile->resize_symtabs ();
edc02ceb
TT
5671 objfile->qf.push_front (make_dwarf_gdb_index ());
5672 return;
17ee85fc
TT
5673 }
5674
5675 per_bfd->using_index = 1;
976ca316
SM
5676 create_all_comp_units (per_objfile);
5677 create_all_type_units (per_objfile);
17ee85fc
TT
5678 per_bfd->quick_file_names_table
5679 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5680 per_objfile->resize_symtabs ();
9291a0cd 5681
17ee85fc
TT
5682 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5683 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 5684 {
17ee85fc 5685 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 5686
17ee85fc 5687 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5688 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5689 }
5690
edc02ceb
TT
5691 /* Arrange for gdb to see the "quick" functions. However, these
5692 functions will be no-ops because we will have expanded all
5693 symtabs. */
5694 objfile->qf.push_front (make_dwarf_gdb_index ());
5695 return;
9291a0cd
TT
5696 }
5697
17ee85fc
TT
5698 /* Was a debug names index already read when we processed an objfile sharing
5699 PER_BFD? */
5700 if (per_bfd->debug_names_table != nullptr)
5701 {
850ed749 5702 dwarf_read_debug_printf ("re-using shared debug names table");
976ca316 5703 per_objfile->resize_symtabs ();
edc02ceb
TT
5704 objfile->qf.push_front (make_dwarf_debug_names ());
5705 return;
17ee85fc
TT
5706 }
5707
5708 /* Was a GDB index already read when we processed an objfile sharing
5709 PER_BFD? */
5710 if (per_bfd->index_table != nullptr)
5711 {
850ed749 5712 dwarf_read_debug_printf ("re-using shared index table");
976ca316 5713 per_objfile->resize_symtabs ();
edc02ceb
TT
5714 objfile->qf.push_front (make_dwarf_gdb_index ());
5715 return;
17ee85fc
TT
5716 }
5717
efb763a5
SM
5718 /* There might already be partial symtabs built for this BFD. This happens
5719 when loading the same binary twice with the index-cache enabled. If so,
5720 don't try to read an index. The objfile / per_objfile initialization will
5721 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5722 code path. */
5723 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
5724 {
5725 dwarf_read_debug_printf ("re-using shared partial symtabs");
edc02ceb
TT
5726 objfile->qf.push_front (make_lazy_dwarf_reader ());
5727 return;
850ed749 5728 }
efb763a5 5729
976ca316 5730 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 5731 {
850ed749 5732 dwarf_read_debug_printf ("found debug names");
976ca316 5733 per_objfile->resize_symtabs ();
edc02ceb
TT
5734 objfile->qf.push_front (make_dwarf_debug_names ());
5735 return;
3c0aa29a 5736 }
927aa2e7 5737
976ca316 5738 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5739 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5740 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 5741 {
850ed749 5742 dwarf_read_debug_printf ("found gdb index from file");
976ca316 5743 per_objfile->resize_symtabs ();
edc02ceb
TT
5744 objfile->qf.push_front (make_dwarf_gdb_index ());
5745 return;
3c0aa29a 5746 }
9291a0cd 5747
87d6a7aa 5748 /* ... otherwise, try to find the index in the index cache. */
976ca316 5749 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
5750 get_gdb_index_contents_from_cache,
5751 get_gdb_index_contents_from_cache_dwz))
5752 {
850ed749 5753 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa 5754 global_index_cache.hit ();
976ca316 5755 per_objfile->resize_symtabs ();
edc02ceb
TT
5756 objfile->qf.push_front (make_dwarf_gdb_index ());
5757 return;
87d6a7aa
SM
5758 }
5759
5760 global_index_cache.miss ();
edc02ceb 5761 objfile->qf.push_front (make_lazy_dwarf_reader ());
9291a0cd
TT
5762}
5763
5764\f
5765
dce234bc
PP
5766/* Build a partial symbol table. */
5767
5768void
eb36a3eb 5769dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
dce234bc 5770{
976ca316
SM
5771 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5772 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
5773
5774 if (per_bfd->partial_symtabs != nullptr)
5775 {
5776 /* Partial symbols were already read, so now we can simply
5777 attach them. */
eb36a3eb
TT
5778 if (psf == nullptr)
5779 {
5780 psf = new psymbol_functions (per_bfd->partial_symtabs);
5781 objfile->qf.emplace_front (psf);
5782 }
5783 else
5784 psf->set_partial_symtabs (per_bfd->partial_symtabs);
976ca316 5785 per_objfile->resize_symtabs ();
17ee85fc
TT
5786 return;
5787 }
c9bf0622 5788
eb36a3eb
TT
5789 if (psf == nullptr)
5790 {
5791 psf = new psymbol_functions;
5792 objfile->qf.emplace_front (psf);
5793 }
5794 const std::shared_ptr<psymtab_storage> &partial_symtabs
5795 = psf->get_partial_symtabs ();
5796
51962708
TT
5797 /* Set the local reference to partial symtabs, so that we don't try
5798 to read them again if reading another objfile with the same BFD.
5799 If we can't in fact share, this won't make a difference anyway as
5800 the dwarf2_per_bfd object won't be shared. */
eb36a3eb 5801 per_bfd->partial_symtabs = partial_symtabs;
51962708 5802
a70b8144 5803 try
c9bf0622
TT
5804 {
5805 /* This isn't really ideal: all the data we allocate on the
5806 objfile's obstack is still uselessly kept around. However,
5807 freeing it seems unsafe. */
eb36a3eb 5808 psymtab_discarder psymtabs (partial_symtabs.get ());
976ca316 5809 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 5810 psymtabs.keep ();
87d6a7aa 5811
976ca316 5812 per_objfile->resize_symtabs ();
af758d11 5813
87d6a7aa 5814 /* (maybe) store an index in the cache. */
976ca316 5815 global_index_cache.store (per_objfile);
c9bf0622 5816 }
230d2906 5817 catch (const gdb_exception_error &except)
492d29ea
PA
5818 {
5819 exception_print (gdb_stderr, except);
5820 }
c906108c 5821}
c906108c 5822
3b80fe9b
DE
5823/* Find the base address of the compilation unit for range lists and
5824 location lists. It will normally be specified by DW_AT_low_pc.
5825 In DWARF-3 draft 4, the base address could be overridden by
5826 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5827 compilation units with discontinuous ranges. */
5828
5829static void
5830dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5831{
5832 struct attribute *attr;
5833
2b24b6e4 5834 cu->base_address.reset ();
3b80fe9b
DE
5835
5836 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5837 if (attr != nullptr)
95f982e5 5838 cu->base_address = attr->as_address ();
3b80fe9b
DE
5839 else
5840 {
5841 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5842 if (attr != nullptr)
95f982e5 5843 cu->base_address = attr->as_address ();
3b80fe9b
DE
5844 }
5845}
5846
36586728
TT
5847/* Helper function that returns the proper abbrev section for
5848 THIS_CU. */
5849
5850static struct dwarf2_section_info *
5851get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5852{
5853 struct dwarf2_section_info *abbrev;
c3699833 5854 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
5855
5856 if (this_cu->is_dwz)
a7308ce0 5857 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 5858 else
c3699833 5859 abbrev = &per_bfd->abbrev;
36586728
TT
5860
5861 return abbrev;
5862}
5863
f4dc4d17
DE
5864/* Fetch the abbreviation table offset from a comp or type unit header. */
5865
5866static sect_offset
976ca316 5867read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 5868 struct dwarf2_section_info *section,
9c541725 5869 sect_offset sect_off)
f4dc4d17 5870{
96b79293 5871 bfd *abfd = section->get_bfd_owner ();
d521ce57 5872 const gdb_byte *info_ptr;
ac298888 5873 unsigned int initial_length_size, offset_size;
43988095 5874 uint16_t version;
f4dc4d17 5875
976ca316 5876 section->read (per_objfile->objfile);
9c541725 5877 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5878 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5879 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5880 info_ptr += initial_length_size;
5881
5882 version = read_2_bytes (abfd, info_ptr);
5883 info_ptr += 2;
5884 if (version >= 5)
5885 {
5886 /* Skip unit type and address size. */
5887 info_ptr += 2;
5888 }
5889
24aa364d 5890 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5891}
5892
b83470bf
TT
5893/* A partial symtab that is used only for include files. */
5894struct dwarf2_include_psymtab : public partial_symtab
5895{
7b249e47
TT
5896 dwarf2_include_psymtab (const char *filename,
5897 psymtab_storage *partial_symtabs,
0072c873
SM
5898 objfile_per_bfd_storage *objfile_per_bfd)
5899 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
b83470bf
TT
5900 {
5901 }
5902
5903 void read_symtab (struct objfile *objfile) override
5904 {
194d088f
TV
5905 /* It's an include file, no symbols to read for it.
5906 Everything is in the includer symtab. */
5907
5908 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5909 expansion of the includer psymtab. We use the dependencies[0] field to
5910 model the includer. But if we go the regular route of calling
5911 expand_psymtab here, and having expand_psymtab call expand_dependencies
5912 to expand the includer, we'll only use expand_psymtab on the includer
5913 (making it a non-toplevel psymtab), while if we expand the includer via
5914 another path, we'll use read_symtab (making it a toplevel psymtab).
5915 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5916 psymtab, and trigger read_symtab on the includer here directly. */
5917 includer ()->read_symtab (objfile);
b83470bf
TT
5918 }
5919
5920 void expand_psymtab (struct objfile *objfile) override
5921 {
194d088f
TV
5922 /* This is not called by read_symtab, and should not be called by any
5923 expand_dependencies. */
5924 gdb_assert (false);
b83470bf
TT
5925 }
5926
5717c425 5927 bool readin_p (struct objfile *objfile) const override
b83470bf 5928 {
5717c425 5929 return includer ()->readin_p (objfile);
b83470bf
TT
5930 }
5931
5717c425 5932 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
5933 {
5934 return nullptr;
5935 }
5936
5937private:
194d088f
TV
5938 partial_symtab *includer () const
5939 {
5940 /* An include psymtab has exactly one dependency: the psymtab that
5941 includes it. */
5942 gdb_assert (this->number_of_dependencies == 1);
5943 return this->dependencies[0];
5944 }
b83470bf
TT
5945};
5946
aaa75496
JB
5947/* Allocate a new partial symtab for file named NAME and mark this new
5948 partial symtab as being an include of PST. */
5949
5950static void
84685904 5951dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
7b249e47
TT
5952 const char *name,
5953 dwarf2_psymtab *pst,
5954 psymtab_storage *partial_symtabs,
0072c873 5955 objfile_per_bfd_storage *objfile_per_bfd)
aaa75496 5956{
7b249e47 5957 dwarf2_include_psymtab *subpst
0072c873 5958 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
aaa75496 5959
fbd9ab74 5960 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 5961 subpst->dirname = pst->dirname;
fbd9ab74 5962
84685904 5963 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5964 subpst->dependencies[0] = pst;
5965 subpst->number_of_dependencies = 1;
aaa75496
JB
5966}
5967
5968/* Read the Line Number Program data and extract the list of files
5969 included by the source file represented by PST. Build an include
d85a05f0 5970 partial symtab for each of these included files. */
aaa75496
JB
5971
5972static void
5973dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5974 struct die_info *die,
891813be 5975 dwarf2_psymtab *pst)
aaa75496 5976{
fff8551c 5977 line_header_up lh;
d85a05f0 5978 struct attribute *attr;
aaa75496 5979
d85a05f0 5980 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
5981 if (attr != nullptr && attr->form_is_unsigned ())
5982 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
5983 if (lh == NULL)
5984 return; /* No linetable, so no includes. */
5985
79748972
TT
5986 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5987 that we pass in the raw text_low here; that is ok because we're
5988 only decoding the line table to make include partial symtabs, and
5989 so the addresses aren't really used. */
4ae976d1 5990 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5991 pst->raw_text_low (), 1);
aaa75496
JB
5992}
5993
348e048f 5994static hashval_t
52dc124a 5995hash_signatured_type (const void *item)
348e048f 5996{
9a3c8263
SM
5997 const struct signatured_type *sig_type
5998 = (const struct signatured_type *) item;
9a619af0 5999
348e048f 6000 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6001 return sig_type->signature;
348e048f
DE
6002}
6003
6004static int
52dc124a 6005eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6006{
9a3c8263
SM
6007 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6008 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6009
348e048f
DE
6010 return lhs->signature == rhs->signature;
6011}
6012
1fd400ff
TT
6013/* Allocate a hash table for signatured types. */
6014
b0b6a987 6015static htab_up
298e9637 6016allocate_signatured_type_table ()
1fd400ff 6017{
b0b6a987
TT
6018 return htab_up (htab_create_alloc (41,
6019 hash_signatured_type,
6020 eq_signatured_type,
6021 NULL, xcalloc, xfree));
1fd400ff
TT
6022}
6023
d467dd73 6024/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6025
6026static int
d467dd73 6027add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6028{
9a3c8263 6029 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6030 std::vector<signatured_type *> *all_type_units
6031 = (std::vector<signatured_type *> *) datum;
1fd400ff 6032
b2bdb8cf 6033 all_type_units->push_back (sigt);
1fd400ff
TT
6034
6035 return 1;
6036}
6037
78d4d2c5 6038/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6039 and fill them into TYPES_HTAB. It will process only type units,
6040 therefore DW_UT_type. */
c88ee1f0 6041
78d4d2c5 6042static void
976ca316 6043create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6044 struct dwo_file *dwo_file,
b0b6a987 6045 dwarf2_section_info *section, htab_up &types_htab,
43988095 6046 rcuh_kind section_kind)
348e048f 6047{
976ca316 6048 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6049 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6050 bfd *abfd;
6051 const gdb_byte *info_ptr, *end_ptr;
348e048f 6052
4bdcc0c1
DE
6053 abbrev_section = (dwo_file != NULL
6054 ? &dwo_file->sections.abbrev
976ca316 6055 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6056
4800761a 6057 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
6058 section->get_name (),
6059 abbrev_section->get_file_name ());
09406207 6060
96b79293 6061 section->read (objfile);
78d4d2c5 6062 info_ptr = section->buffer;
348e048f 6063
78d4d2c5
JK
6064 if (info_ptr == NULL)
6065 return;
348e048f 6066
78d4d2c5
JK
6067 /* We can't set abfd until now because the section may be empty or
6068 not present, in which case the bfd is unknown. */
96b79293 6069 abfd = section->get_bfd_owner ();
348e048f 6070
c0ab21c2
TT
6071 /* We don't use cutu_reader here because we don't need to read
6072 any dies: the signature is in the header. */
3019eac3 6073
78d4d2c5
JK
6074 end_ptr = info_ptr + section->size;
6075 while (info_ptr < end_ptr)
6076 {
78d4d2c5
JK
6077 struct signatured_type *sig_type;
6078 struct dwo_unit *dwo_tu;
6079 void **slot;
6080 const gdb_byte *ptr = info_ptr;
6081 struct comp_unit_head header;
6082 unsigned int length;
8b70b953 6083
9c541725 6084 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6085
a49dd8dd
JK
6086 /* Initialize it due to a false compiler warning. */
6087 header.signature = -1;
9c541725 6088 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6089
78d4d2c5
JK
6090 /* We need to read the type's signature in order to build the hash
6091 table, but we don't need anything else just yet. */
348e048f 6092
976ca316 6093 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6094 abbrev_section, ptr, section_kind);
348e048f 6095
4057dfde 6096 length = header.get_length ();
6caca83c 6097
78d4d2c5
JK
6098 /* Skip dummy type units. */
6099 if (ptr >= info_ptr + length
43988095 6100 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6101 || (header.unit_type != DW_UT_type
6102 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6103 {
6104 info_ptr += length;
6105 continue;
6106 }
dee91e82 6107
78d4d2c5
JK
6108 if (types_htab == NULL)
6109 {
6110 if (dwo_file)
298e9637 6111 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6112 else
298e9637 6113 types_htab = allocate_signatured_type_table ();
78d4d2c5 6114 }
8b70b953 6115
78d4d2c5
JK
6116 if (dwo_file)
6117 {
6118 sig_type = NULL;
976ca316 6119 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6120 dwo_tu->dwo_file = dwo_file;
43988095 6121 dwo_tu->signature = header.signature;
9c541725 6122 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6123 dwo_tu->section = section;
9c541725 6124 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6125 dwo_tu->length = length;
6126 }
6127 else
6128 {
6129 /* N.B.: type_offset is not usable if this type uses a DWO file.
6130 The real type_offset is in the DWO file. */
6131 dwo_tu = NULL;
976ca316 6132 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6133 sig_type->signature = header.signature;
9c541725 6134 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6135 sig_type->per_cu.is_debug_types = 1;
6136 sig_type->per_cu.section = section;
9c541725 6137 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6138 sig_type->per_cu.length = length;
6139 }
6140
b0b6a987 6141 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6142 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6143 INSERT);
6144 gdb_assert (slot != NULL);
6145 if (*slot != NULL)
6146 {
9c541725 6147 sect_offset dup_sect_off;
0349ea22 6148
3019eac3
DE
6149 if (dwo_file)
6150 {
78d4d2c5
JK
6151 const struct dwo_unit *dup_tu
6152 = (const struct dwo_unit *) *slot;
6153
9c541725 6154 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6155 }
6156 else
6157 {
78d4d2c5
JK
6158 const struct signatured_type *dup_tu
6159 = (const struct signatured_type *) *slot;
6160
9c541725 6161 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6162 }
8b70b953 6163
b98664d3 6164 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6165 " the entry at offset %s, signature %s"),
6166 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6167 hex_string (header.signature));
78d4d2c5
JK
6168 }
6169 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6170
6f738b01
SM
6171 dwarf_read_debug_printf_v (" offset %s, signature %s",
6172 sect_offset_str (sect_off),
6173 hex_string (header.signature));
3019eac3 6174
78d4d2c5
JK
6175 info_ptr += length;
6176 }
6177}
3019eac3 6178
78d4d2c5
JK
6179/* Create the hash table of all entries in the .debug_types
6180 (or .debug_types.dwo) section(s).
6181 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6182 otherwise it is NULL.
b3c8eb43 6183
78d4d2c5 6184 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6185
78d4d2c5 6186 Note: This function processes DWO files only, not DWP files. */
348e048f 6187
78d4d2c5 6188static void
976ca316 6189create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6190 struct dwo_file *dwo_file,
fd5866f6 6191 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6192 htab_up &types_htab)
78d4d2c5 6193{
fd5866f6 6194 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6195 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6196 rcuh_kind::TYPE);
3019eac3
DE
6197}
6198
6199/* Create the hash table of all entries in the .debug_types section,
6200 and initialize all_type_units.
6201 The result is zero if there is an error (e.g. missing .debug_types section),
6202 otherwise non-zero. */
6203
6204static int
976ca316 6205create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6206{
b0b6a987 6207 htab_up types_htab;
3019eac3 6208
976ca316
SM
6209 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6210 types_htab, rcuh_kind::COMPILE);
6211 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6212 types_htab);
3019eac3
DE
6213 if (types_htab == NULL)
6214 {
976ca316 6215 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6216 return 0;
6217 }
6218
976ca316 6219 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6220
976ca316
SM
6221 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6222 per_objfile->per_bfd->all_type_units.reserve
6223 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6224
976ca316 6225 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6226 add_signatured_type_cu_to_table,
976ca316 6227 &per_objfile->per_bfd->all_type_units);
1fd400ff 6228
348e048f
DE
6229 return 1;
6230}
6231
5989a64e 6232/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6233 If SLOT is non-NULL, it is the entry to use in the hash table.
6234 Otherwise we find one. */
6235
6236static struct signatured_type *
976ca316 6237add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6238{
976ca316
SM
6239 if (per_objfile->per_bfd->all_type_units.size ()
6240 == per_objfile->per_bfd->all_type_units.capacity ())
6241 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6242
976ca316 6243 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6244
976ca316 6245 per_objfile->resize_symtabs ();
af758d11 6246
976ca316 6247 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6248 sig_type->signature = sig;
6249 sig_type->per_cu.is_debug_types = 1;
976ca316 6250 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6251 {
6252 sig_type->per_cu.v.quick =
976ca316 6253 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6254 struct dwarf2_per_cu_quick_data);
6255 }
6256
6257 if (slot == NULL)
6258 {
976ca316 6259 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6260 sig_type, INSERT);
6261 }
6262 gdb_assert (*slot == NULL);
6263 *slot = sig_type;
6264 /* The rest of sig_type must be filled in by the caller. */
6265 return sig_type;
6266}
6267
a2ce51a0
DE
6268/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6269 Fill in SIG_ENTRY with DWO_ENTRY. */
6270
6271static void
976ca316 6272fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6273 struct signatured_type *sig_entry,
6274 struct dwo_unit *dwo_entry)
6275{
976ca316 6276 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6277
7ee85ab1 6278 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6279 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6280 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6281 if (per_bfd->using_index)
6aa5f3a6
DE
6282 {
6283 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6284 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6285 }
6286 else
6287 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6288 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6289 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6290 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6291 gdb_assert (sig_entry->dwo_unit == NULL);
6292
6293 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6294 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6295 sig_entry->per_cu.length = dwo_entry->length;
6296 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6297 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6298 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6299 sig_entry->dwo_unit = dwo_entry;
6300}
6301
6302/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6303 If we haven't read the TU yet, create the signatured_type data structure
6304 for a TU to be read in directly from a DWO file, bypassing the stub.
6305 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6306 using .gdb_index, then when reading a CU we want to stay in the DWO file
6307 containing that CU. Otherwise we could end up reading several other DWO
6308 files (due to comdat folding) to process the transitive closure of all the
6309 mentioned TUs, and that can be slow. The current DWO file will have every
6310 type signature that it needs.
a2ce51a0
DE
6311 We only do this for .gdb_index because in the psymtab case we already have
6312 to read all the DWOs to build the type unit groups. */
6313
6314static struct signatured_type *
6315lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6316{
976ca316 6317 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6318 struct dwo_file *dwo_file;
6319 struct dwo_unit find_dwo_entry, *dwo_entry;
6320 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6321 void **slot;
a2ce51a0 6322
976ca316 6323 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6324
6aa5f3a6
DE
6325 /* If TU skeletons have been removed then we may not have read in any
6326 TUs yet. */
976ca316
SM
6327 if (per_objfile->per_bfd->signatured_types == NULL)
6328 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6329
6330 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6331 Use the global signatured_types array to do our own comdat-folding
6332 of types. If this is the first time we're reading this TU, and
6333 the TU has an entry in .gdb_index, replace the recorded data from
6334 .gdb_index with this TU. */
a2ce51a0 6335
a2ce51a0 6336 find_sig_entry.signature = sig;
976ca316 6337 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6338 &find_sig_entry, INSERT);
9a3c8263 6339 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6340
6341 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6342 read. Don't reassign the global entry to point to this DWO if that's
6343 the case. Also note that if the TU is already being read, it may not
6344 have come from a DWO, the program may be a mix of Fission-compiled
6345 code and non-Fission-compiled code. */
6346
6347 /* Have we already tried to read this TU?
6348 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6349 needn't exist in the global table yet). */
6350 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6351 return sig_entry;
6352
6aa5f3a6
DE
6353 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6354 dwo_unit of the TU itself. */
6355 dwo_file = cu->dwo_unit->dwo_file;
6356
a2ce51a0
DE
6357 /* Ok, this is the first time we're reading this TU. */
6358 if (dwo_file->tus == NULL)
6359 return NULL;
6360 find_dwo_entry.signature = sig;
b0b6a987
TT
6361 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6362 &find_dwo_entry);
a2ce51a0
DE
6363 if (dwo_entry == NULL)
6364 return NULL;
6365
6aa5f3a6
DE
6366 /* If the global table doesn't have an entry for this TU, add one. */
6367 if (sig_entry == NULL)
976ca316 6368 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6369
976ca316 6370 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6371 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6372 return sig_entry;
6373}
6374
a2ce51a0
DE
6375/* Subroutine of lookup_signatured_type.
6376 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6377 then try the DWP file. If the TU stub (skeleton) has been removed then
6378 it won't be in .gdb_index. */
a2ce51a0
DE
6379
6380static struct signatured_type *
6381lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6382{
976ca316
SM
6383 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6384 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6385 struct dwo_unit *dwo_entry;
6386 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6387 void **slot;
a2ce51a0 6388
976ca316 6389 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6390 gdb_assert (dwp_file != NULL);
6391
6aa5f3a6
DE
6392 /* If TU skeletons have been removed then we may not have read in any
6393 TUs yet. */
976ca316
SM
6394 if (per_objfile->per_bfd->signatured_types == NULL)
6395 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6396
6aa5f3a6 6397 find_sig_entry.signature = sig;
976ca316 6398 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6399 &find_sig_entry, INSERT);
9a3c8263 6400 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6401
6402 /* Have we already tried to read this TU?
6403 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6404 needn't exist in the global table yet). */
6405 if (sig_entry != NULL)
6406 return sig_entry;
6407
a2ce51a0
DE
6408 if (dwp_file->tus == NULL)
6409 return NULL;
976ca316
SM
6410 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6411 1 /* is_debug_types */);
a2ce51a0
DE
6412 if (dwo_entry == NULL)
6413 return NULL;
6414
976ca316
SM
6415 sig_entry = add_type_unit (per_objfile, sig, slot);
6416 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6417
a2ce51a0
DE
6418 return sig_entry;
6419}
6420
380bca97 6421/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6422 Returns NULL if signature SIG is not present in the table.
6423 It is up to the caller to complain about this. */
348e048f
DE
6424
6425static struct signatured_type *
a2ce51a0 6426lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6427{
976ca316 6428 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6429
976ca316 6430 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6431 {
6432 /* We're in a DWO/DWP file, and we're using .gdb_index.
6433 These cases require special processing. */
976ca316 6434 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6435 return lookup_dwo_signatured_type (cu, sig);
6436 else
6437 return lookup_dwp_signatured_type (cu, sig);
6438 }
6439 else
6440 {
6441 struct signatured_type find_entry, *entry;
348e048f 6442
976ca316 6443 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6444 return NULL;
6445 find_entry.signature = sig;
9a3c8263 6446 entry = ((struct signatured_type *)
976ca316 6447 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6448 &find_entry));
a2ce51a0
DE
6449 return entry;
6450 }
348e048f 6451}
18a8505e 6452
42e7ad6c 6453/* Low level DIE reading support. */
348e048f 6454
d85a05f0
DJ
6455/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6456
6457static void
6458init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6459 struct dwarf2_cu *cu,
3019eac3 6460 struct dwarf2_section_info *section,
685af9cd
TT
6461 struct dwo_file *dwo_file,
6462 struct abbrev_table *abbrev_table)
d85a05f0 6463{
fceca515 6464 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6465 reader->abfd = section->get_bfd_owner ();
d85a05f0 6466 reader->cu = cu;
3019eac3 6467 reader->dwo_file = dwo_file;
dee91e82
DE
6468 reader->die_section = section;
6469 reader->buffer = section->buffer;
f664829e 6470 reader->buffer_end = section->buffer + section->size;
685af9cd 6471 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6472}
6473
c0ab21c2 6474/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6475 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6476 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6477 already.
6478
6479 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6480 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6481 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6482 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6483 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6484 STUB_COMP_DIR may be non-NULL.
3e225074 6485 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6486 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6487 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6488 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6489 kept around for at least as long as *RESULT_READER.
6490
b0c7bfa9
DE
6491 The result is non-zero if a valid (non-dummy) DIE was found. */
6492
6493static int
4ab09049 6494read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6495 struct dwo_unit *dwo_unit,
b0c7bfa9 6496 struct die_info *stub_comp_unit_die,
a2ce51a0 6497 const char *stub_comp_dir,
b0c7bfa9 6498 struct die_reader_specs *result_reader,
d521ce57 6499 const gdb_byte **result_info_ptr,
b0c7bfa9 6500 struct die_info **result_comp_unit_die,
685af9cd 6501 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6502{
976ca316 6503 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6504 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6505 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6506 bfd *abfd;
d521ce57 6507 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6508 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6509 int i,num_extra_attrs;
6510 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6511 struct die_info *comp_unit_die;
6512
b0aeadb3
DE
6513 /* At most one of these may be provided. */
6514 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6515
b0c7bfa9
DE
6516 /* These attributes aren't processed until later:
6517 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6518 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6519 referenced later. However, these attributes are found in the stub
6520 which we won't have later. In order to not impose this complication
6521 on the rest of the code, we read them here and copy them to the
6522 DWO CU/TU die. */
b0c7bfa9
DE
6523
6524 stmt_list = NULL;
6525 low_pc = NULL;
6526 high_pc = NULL;
6527 ranges = NULL;
6528 comp_dir = NULL;
6529
6530 if (stub_comp_unit_die != NULL)
6531 {
6532 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6533 DWO file. */
4ab09049 6534 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6535 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6536 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6537 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6538 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6539 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6540
a39fdb41 6541 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6542
2b0c7f41
SM
6543 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6544 We need the value before we can process DW_AT_ranges values from the
6545 DWO. */
6546 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6547
6548 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6549 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6550 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6551 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6552 section. */
6553 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6554 }
a2ce51a0
DE
6555 else if (stub_comp_dir != NULL)
6556 {
6557 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6558 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6559 comp_dir->name = DW_AT_comp_dir;
6560 comp_dir->form = DW_FORM_string;
c6481205 6561 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6562 }
b0c7bfa9
DE
6563
6564 /* Set up for reading the DWO CU/TU. */
6565 cu->dwo_unit = dwo_unit;
685af9cd 6566 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6567 section->read (objfile);
6568 abfd = section->get_bfd_owner ();
9c541725
PA
6569 begin_info_ptr = info_ptr = (section->buffer
6570 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6571 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6572
4ab09049 6573 if (per_cu->is_debug_types)
b0c7bfa9 6574 {
4ab09049 6575 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6576
976ca316
SM
6577 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6578 section, dwo_abbrev_section,
43988095 6579 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6580 /* This is not an assert because it can be caused by bad debug info. */
43988095 6581 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6582 {
6583 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6584 " TU at offset %s [in module %s]"),
a2ce51a0 6585 hex_string (sig_type->signature),
43988095 6586 hex_string (cu->header.signature),
9d8780f0 6587 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6588 bfd_get_filename (abfd));
6589 }
9c541725 6590 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6591 /* For DWOs coming from DWP files, we don't know the CU length
6592 nor the type's offset in the TU until now. */
4057dfde 6593 dwo_unit->length = cu->header.get_length ();
9c541725 6594 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6595
6596 /* Establish the type offset that can be used to lookup the type.
6597 For DWO files, we don't know it until now. */
9c541725
PA
6598 sig_type->type_offset_in_section
6599 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6600 }
6601 else
6602 {
976ca316
SM
6603 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6604 section, dwo_abbrev_section,
43988095 6605 info_ptr, rcuh_kind::COMPILE);
9c541725 6606 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6607 /* For DWOs coming from DWP files, we don't know the CU length
6608 until now. */
4057dfde 6609 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6610 }
6611
606decb2 6612 dwo_abbrev_section->read (objfile);
685af9cd 6613 *result_dwo_abbrev_table
606decb2 6614 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6615 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6616 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6617
6618 /* Read in the die, but leave space to copy over the attributes
6619 from the stub. This has the benefit of simplifying the rest of
6620 the code - all the work to maintain the illusion of a single
6621 DW_TAG_{compile,type}_unit DIE is done here. */
6622 num_extra_attrs = ((stmt_list != NULL)
6623 + (low_pc != NULL)
6624 + (high_pc != NULL)
6625 + (ranges != NULL)
6626 + (comp_dir != NULL));
6627 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6628 num_extra_attrs);
b0c7bfa9
DE
6629
6630 /* Copy over the attributes from the stub to the DIE we just read in. */
6631 comp_unit_die = *result_comp_unit_die;
6632 i = comp_unit_die->num_attrs;
6633 if (stmt_list != NULL)
6634 comp_unit_die->attrs[i++] = *stmt_list;
6635 if (low_pc != NULL)
6636 comp_unit_die->attrs[i++] = *low_pc;
6637 if (high_pc != NULL)
6638 comp_unit_die->attrs[i++] = *high_pc;
6639 if (ranges != NULL)
6640 comp_unit_die->attrs[i++] = *ranges;
6641 if (comp_dir != NULL)
6642 comp_unit_die->attrs[i++] = *comp_dir;
6643 comp_unit_die->num_attrs += num_extra_attrs;
6644
b4f54984 6645 if (dwarf_die_debug)
bf6af496
DE
6646 {
6647 fprintf_unfiltered (gdb_stdlog,
6648 "Read die from %s@0x%x of %s:\n",
96b79293 6649 section->get_name (),
bf6af496
DE
6650 (unsigned) (begin_info_ptr - section->buffer),
6651 bfd_get_filename (abfd));
b4f54984 6652 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6653 }
6654
b0c7bfa9
DE
6655 /* Skip dummy compilation units. */
6656 if (info_ptr >= begin_info_ptr + dwo_unit->length
6657 || peek_abbrev_code (abfd, info_ptr) == 0)
6658 return 0;
6659
6660 *result_info_ptr = info_ptr;
6661 return 1;
6662}
6663
a084a2a6
AT
6664/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6665 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6666 signature is part of the header. */
6667static gdb::optional<ULONGEST>
6668lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6669{
6670 if (cu->header.version >= 5)
6671 return cu->header.signature;
6672 struct attribute *attr;
6673 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 6674 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 6675 return gdb::optional<ULONGEST> ();
529908cb 6676 return attr->as_unsigned ();
a084a2a6
AT
6677}
6678
c0ab21c2 6679/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6680 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6681 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6682
6683static struct dwo_unit *
4ab09049 6684lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6685{
4ab09049 6686 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6687 struct dwo_unit *dwo_unit;
c0ab21c2 6688 const char *comp_dir;
b0c7bfa9 6689
a2ce51a0
DE
6690 gdb_assert (cu != NULL);
6691
b0c7bfa9 6692 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6693 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6694 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6695
4ab09049
SM
6696 if (per_cu->is_debug_types)
6697 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6698 else
6699 {
a084a2a6 6700 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6701
a084a2a6 6702 if (!signature.has_value ())
b0c7bfa9
DE
6703 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6704 " [in module %s]"),
4ab09049
SM
6705 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6706
6707 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6708 }
6709
b0c7bfa9
DE
6710 return dwo_unit;
6711}
6712
c0ab21c2 6713/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6714 See it for a description of the parameters.
fcd3b13d 6715 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6716
c0ab21c2 6717void
9e021579
SM
6718cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6719 dwarf2_per_objfile *per_objfile,
2e671100 6720 dwarf2_cu *existing_cu)
a2ce51a0 6721{
a2ce51a0 6722 struct signatured_type *sig_type;
a2ce51a0
DE
6723
6724 /* Verify we can do the following downcast, and that we have the
6725 data we need. */
6726 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6727 sig_type = (struct signatured_type *) this_cu;
6728 gdb_assert (sig_type->dwo_unit != NULL);
6729
2e671100
SM
6730 dwarf2_cu *cu;
6731
6732 if (existing_cu != nullptr)
6aa5f3a6 6733 {
2e671100
SM
6734 cu = existing_cu;
6735 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6736 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6737 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6738 }
6739 else
6740 {
7188ed02 6741 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6742 in per_objfile yet. */
7188ed02 6743 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6744 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6745 cu = m_new_cu.get ();
6aa5f3a6
DE
6746 }
6747
6748 /* A future optimization, if needed, would be to use an existing
6749 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6750 could share abbrev tables. */
a2ce51a0 6751
2e671100 6752 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6753 NULL /* stub_comp_unit_die */,
6754 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6755 this, &info_ptr,
3e225074 6756 &comp_unit_die,
c0ab21c2 6757 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6758 {
6759 /* Dummy die. */
c0ab21c2 6760 dummy_p = true;
a2ce51a0 6761 }
a2ce51a0
DE
6762}
6763
fd820528 6764/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6765 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6766
f4dc4d17
DE
6767 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6768 Otherwise the table specified in the comp unit header is read in and used.
6769 This is an optimization for when we already have the abbrev table.
6770
2e671100
SM
6771 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6772 allocated. */
aaa75496 6773
ab432490 6774cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6775 dwarf2_per_objfile *per_objfile,
c0ab21c2 6776 struct abbrev_table *abbrev_table,
2e671100 6777 dwarf2_cu *existing_cu,
c0ab21c2
TT
6778 bool skip_partial)
6779 : die_reader_specs {},
6751ebae 6780 m_this_cu (this_cu)
c906108c 6781{
976ca316 6782 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6783 struct dwarf2_section_info *section = this_cu->section;
96b79293 6784 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 6785 const gdb_byte *begin_info_ptr;
dee91e82 6786 struct signatured_type *sig_type = NULL;
4bdcc0c1 6787 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6788 /* Non-zero if CU currently points to a DWO file and we need to
6789 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6790 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6791 int rereading_dwo_cu = 0;
c906108c 6792
b4f54984 6793 if (dwarf_die_debug)
9d8780f0 6794 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6795 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6796 sect_offset_str (this_cu->sect_off));
09406207 6797
a2ce51a0
DE
6798 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6799 file (instead of going through the stub), short-circuit all of this. */
6800 if (this_cu->reading_dwo_directly)
6801 {
6802 /* Narrow down the scope of possibilities to have to understand. */
6803 gdb_assert (this_cu->is_debug_types);
6804 gdb_assert (abbrev_table == NULL);
976ca316 6805 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
6806 return;
6807 }
6808
dee91e82 6809 /* This is cheap if the section is already read in. */
96b79293 6810 section->read (objfile);
dee91e82 6811
9c541725 6812 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6813
6814 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 6815
2e671100
SM
6816 dwarf2_cu *cu;
6817
6818 if (existing_cu != nullptr)
dee91e82 6819 {
2e671100 6820 cu = existing_cu;
42e7ad6c
DE
6821 /* If this CU is from a DWO file we need to start over, we need to
6822 refetch the attributes from the skeleton CU.
6823 This could be optimized by retrieving those attributes from when we
6824 were here the first time: the previous comp_unit_die was stored in
6825 comp_unit_obstack. But there's no data yet that we need this
6826 optimization. */
6827 if (cu->dwo_unit != NULL)
6828 rereading_dwo_cu = 1;
dee91e82
DE
6829 }
6830 else
6831 {
7188ed02 6832 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6833 in per_objfile yet. */
976ca316
SM
6834 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6835 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 6836 cu = m_new_cu.get ();
42e7ad6c 6837 }
dee91e82 6838
b0c7bfa9 6839 /* Get the header. */
9c541725 6840 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6841 {
6842 /* We already have the header, there's no need to read it in again. */
9c541725 6843 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6844 }
6845 else
6846 {
3019eac3 6847 if (this_cu->is_debug_types)
dee91e82 6848 {
976ca316
SM
6849 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6850 section, abbrev_section,
6851 info_ptr, rcuh_kind::TYPE);
dee91e82 6852
42e7ad6c
DE
6853 /* Since per_cu is the first member of struct signatured_type,
6854 we can go from a pointer to one to a pointer to the other. */
6855 sig_type = (struct signatured_type *) this_cu;
43988095 6856 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6857 gdb_assert (sig_type->type_offset_in_tu
6858 == cu->header.type_cu_offset_in_tu);
6859 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6860
42e7ad6c
DE
6861 /* LENGTH has not been set yet for type units if we're
6862 using .gdb_index. */
4057dfde 6863 this_cu->length = cu->header.get_length ();
3019eac3
DE
6864
6865 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6866 sig_type->type_offset_in_section =
6867 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6868
6869 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6870 }
6871 else
6872 {
976ca316
SM
6873 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6874 section, abbrev_section,
43988095
JK
6875 info_ptr,
6876 rcuh_kind::COMPILE);
dee91e82 6877
9c541725 6878 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
6879 if (this_cu->length == 0)
6880 this_cu->length = cu->header.get_length ();
6881 else
6882 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6883 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6884 }
6885 }
10b3939b 6886
6caca83c 6887 /* Skip dummy compilation units. */
dee91e82 6888 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6889 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6890 {
6891 dummy_p = true;
6892 return;
6893 }
6caca83c 6894
433df2d4
DE
6895 /* If we don't have them yet, read the abbrevs for this compilation unit.
6896 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6897 done. */
f4dc4d17 6898 if (abbrev_table != NULL)
685af9cd
TT
6899 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6900 else
f4dc4d17 6901 {
606decb2 6902 abbrev_section->read (objfile);
c0ab21c2 6903 m_abbrev_table_holder
606decb2 6904 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 6905 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6906 }
af703f96 6907
dee91e82 6908 /* Read the top level CU/TU die. */
c0ab21c2 6909 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6910 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6911
58f0c718 6912 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6913 {
6914 dummy_p = true;
6915 return;
6916 }
58f0c718 6917
b0c7bfa9 6918 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6919 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6920 table from the DWO file and pass the ownership over to us. It will be
6921 referenced from READER, so we must make sure to free it after we're done
6922 with READER.
6923
b0c7bfa9
DE
6924 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6925 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6926 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6927 if (dwo_name != nullptr)
3019eac3 6928 {
3019eac3 6929 struct dwo_unit *dwo_unit;
b0c7bfa9 6930 struct die_info *dwo_comp_unit_die;
3019eac3 6931
3e225074 6932 if (comp_unit_die->has_children)
6a506a2d 6933 {
b98664d3 6934 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6935 " has children (offset %s) [in module %s]"),
6936 sect_offset_str (this_cu->sect_off),
6937 bfd_get_filename (abfd));
6a506a2d 6938 }
4ab09049 6939 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 6940 if (dwo_unit != NULL)
3019eac3 6941 {
4ab09049 6942 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 6943 comp_unit_die, NULL,
c0ab21c2 6944 this, &info_ptr,
3e225074 6945 &dwo_comp_unit_die,
c0ab21c2 6946 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6947 {
6948 /* Dummy die. */
c0ab21c2 6949 dummy_p = true;
6a506a2d
DE
6950 return;
6951 }
6952 comp_unit_die = dwo_comp_unit_die;
6953 }
6954 else
6955 {
6956 /* Yikes, we couldn't find the rest of the DIE, we only have
6957 the stub. A complaint has already been logged. There's
6958 not much more we can do except pass on the stub DIE to
6959 die_reader_func. We don't want to throw an error on bad
6960 debug info. */
3019eac3
DE
6961 }
6962 }
c0ab21c2 6963}
3019eac3 6964
6751ebae
TT
6965void
6966cutu_reader::keep ()
c0ab21c2 6967{
b0c7bfa9 6968 /* Done, clean up. */
6751ebae
TT
6969 gdb_assert (!dummy_p);
6970 if (m_new_cu != NULL)
348e048f 6971 {
7188ed02 6972 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 6973 now. */
7188ed02
SM
6974 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6975 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 6976 }
dee91e82
DE
6977}
6978
18a8505e
AT
6979/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6980 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6981 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6982
6983 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6984 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6985
6986 We fill in THIS_CU->length.
6987
dee91e82 6988 THIS_CU->cu is always freed when done.
3019eac3 6989 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6990 to care whether it refers to the "main" CU or the DWO CU.
6991
6992 When parent_cu is passed, it is used to provide a default value for
6993 str_offsets_base and addr_base from the parent. */
dee91e82 6994
ab432490 6995cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6996 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
6997 struct dwarf2_cu *parent_cu,
6998 struct dwo_file *dwo_file)
6999 : die_reader_specs {},
7000 m_this_cu (this_cu)
dee91e82 7001{
976ca316 7002 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7003 struct dwarf2_section_info *section = this_cu->section;
96b79293 7004 bfd *abfd = section->get_bfd_owner ();
33e80786 7005 struct dwarf2_section_info *abbrev_section;
d521ce57 7006 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7007
b4f54984 7008 if (dwarf_die_debug)
9d8780f0 7009 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7010 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7011 sect_offset_str (this_cu->sect_off));
09406207 7012
976ca316 7013 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7014
33e80786
DE
7015 abbrev_section = (dwo_file != NULL
7016 ? &dwo_file->sections.abbrev
7017 : get_abbrev_section_for_cu (this_cu));
7018
dee91e82 7019 /* This is cheap if the section is already read in. */
96b79293 7020 section->read (objfile);
dee91e82 7021
976ca316 7022 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7023
9c541725 7024 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7025 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7026 section, abbrev_section, info_ptr,
43988095
JK
7027 (this_cu->is_debug_types
7028 ? rcuh_kind::TYPE
7029 : rcuh_kind::COMPILE));
dee91e82 7030
18a8505e
AT
7031 if (parent_cu != nullptr)
7032 {
c0ab21c2
TT
7033 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7034 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7035 }
4057dfde 7036 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7037
7038 /* Skip dummy compilation units. */
7039 if (info_ptr >= begin_info_ptr + this_cu->length
7040 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7041 {
7042 dummy_p = true;
7043 return;
7044 }
72bf9492 7045
606decb2 7046 abbrev_section->read (objfile);
c0ab21c2 7047 m_abbrev_table_holder
606decb2 7048 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7049
c0ab21c2
TT
7050 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7051 m_abbrev_table_holder.get ());
3e225074 7052 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7053}
7054
0018ea6f
DE
7055\f
7056/* Type Unit Groups.
dee91e82 7057
0018ea6f
DE
7058 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7059 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7060 so that all types coming from the same compilation (.o file) are grouped
7061 together. A future step could be to put the types in the same symtab as
7062 the CU the types ultimately came from. */
ff013f42 7063
f4dc4d17
DE
7064static hashval_t
7065hash_type_unit_group (const void *item)
7066{
9a3c8263
SM
7067 const struct type_unit_group *tu_group
7068 = (const struct type_unit_group *) item;
f4dc4d17 7069
094b34ac 7070 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7071}
348e048f
DE
7072
7073static int
f4dc4d17 7074eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7075{
9a3c8263
SM
7076 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7077 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7078
094b34ac 7079 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7080}
348e048f 7081
f4dc4d17
DE
7082/* Allocate a hash table for type unit groups. */
7083
eaa5fa8b 7084static htab_up
298e9637 7085allocate_type_unit_groups_table ()
f4dc4d17 7086{
eaa5fa8b
TT
7087 return htab_up (htab_create_alloc (3,
7088 hash_type_unit_group,
7089 eq_type_unit_group,
7090 NULL, xcalloc, xfree));
f4dc4d17 7091}
dee91e82 7092
f4dc4d17
DE
7093/* Type units that don't have DW_AT_stmt_list are grouped into their own
7094 partial symtabs. We combine several TUs per psymtab to not let the size
7095 of any one psymtab grow too big. */
7096#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7097#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7098
094b34ac 7099/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7100 Create the type_unit_group object used to hold one or more TUs. */
7101
7102static struct type_unit_group *
094b34ac 7103create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7104{
976ca316
SM
7105 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7106 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7107 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7108 struct type_unit_group *tu_group;
f4dc4d17 7109
bab287cd 7110 tu_group = OBSTACK_ZALLOC (&per_bfd->obstack, type_unit_group);
094b34ac 7111 per_cu = &tu_group->per_cu;
1859c670 7112 per_cu->per_bfd = per_bfd;
f4dc4d17 7113
1859c670 7114 if (per_bfd->using_index)
094b34ac 7115 {
1859c670 7116 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7117 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7118 }
7119 else
7120 {
9c541725 7121 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7122 dwarf2_psymtab *pst;
528e1572 7123 std::string name;
094b34ac
DE
7124
7125 /* Give the symtab a useful name for debug purposes. */
7126 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7127 name = string_printf ("<type_units_%d>",
7128 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7129 else
528e1572 7130 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7131
976ca316 7132 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7133 pst->anonymous = true;
094b34ac 7134 }
f4dc4d17 7135
094b34ac 7136 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7137 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7138
7139 return tu_group;
7140}
7141
094b34ac
DE
7142/* Look up the type_unit_group for type unit CU, and create it if necessary.
7143 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7144
7145static struct type_unit_group *
ff39bb5e 7146get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7147{
976ca316
SM
7148 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7149 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7150 struct type_unit_group *tu_group;
7151 void **slot;
7152 unsigned int line_offset;
7153 struct type_unit_group type_unit_group_for_lookup;
7154
976ca316
SM
7155 if (per_objfile->per_bfd->type_unit_groups == NULL)
7156 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7157
7158 /* Do we need to create a new group, or can we use an existing one? */
7159
529908cb 7160 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7161 {
529908cb 7162 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7163 ++tu_stats->nr_symtab_sharers;
7164 }
7165 else
7166 {
7167 /* Ugh, no stmt_list. Rare, but we have to handle it.
7168 We can do various things here like create one group per TU or
7169 spread them over multiple groups to split up the expansion work.
7170 To avoid worst case scenarios (too many groups or too large groups)
7171 we, umm, group them in bunches. */
7172 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7173 | (tu_stats->nr_stmt_less_type_units
7174 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7175 ++tu_stats->nr_stmt_less_type_units;
7176 }
7177
094b34ac 7178 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7179 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7180 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7181 &type_unit_group_for_lookup, INSERT);
7182 if (*slot != NULL)
7183 {
9a3c8263 7184 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7185 gdb_assert (tu_group != NULL);
7186 }
7187 else
7188 {
9c541725 7189 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7190 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7191 *slot = tu_group;
7192 ++tu_stats->nr_symtabs;
7193 }
7194
7195 return tu_group;
7196}
0018ea6f
DE
7197\f
7198/* Partial symbol tables. */
7199
7200/* Create a psymtab named NAME and assign it to PER_CU.
7201
7202 The caller must fill in the following details:
7203 dirname, textlow, texthigh. */
7204
891813be 7205static dwarf2_psymtab *
7aa104c4
SM
7206create_partial_symtab (dwarf2_per_cu_data *per_cu,
7207 dwarf2_per_objfile *per_objfile,
7208 const char *name)
0018ea6f 7209{
0072c873
SM
7210 dwarf2_psymtab *pst
7211 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7212 per_objfile->objfile->per_bfd, per_cu);
0018ea6f 7213
6d94535f 7214 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7215
7216 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7217 per_cu->v.psymtab = pst;
7218
7219 return pst;
7220}
7221
c0ab21c2 7222/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7223
7224static void
7225process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7226 const gdb_byte *info_ptr,
0018ea6f 7227 struct die_info *comp_unit_die,
c0ab21c2 7228 enum language pretend_language)
0018ea6f
DE
7229{
7230 struct dwarf2_cu *cu = reader->cu;
7aa104c4 7231 dwarf2_per_objfile *per_objfile = cu->per_objfile;
84685904 7232 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7aa104c4 7233 struct objfile *objfile = per_objfile->objfile;
08feed99 7234 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7235 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7236 CORE_ADDR baseaddr;
7237 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7238 dwarf2_psymtab *pst;
3a2b436a 7239 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7240 const char *filename;
0018ea6f 7241
0018ea6f
DE
7242 gdb_assert (! per_cu->is_debug_types);
7243
c0ab21c2 7244 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7245
0018ea6f 7246 /* Allocate a new partial symbol table structure. */
2e927613
TV
7247 gdb::unique_xmalloc_ptr<char> debug_filename;
7248 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7249 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7250 if (filename == NULL)
0018ea6f 7251 filename = "";
2e927613
TV
7252 else if (strcmp (filename, artificial) == 0)
7253 {
7254 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7255 sect_offset_str (per_cu->sect_off),
7256 (char *) NULL));
2e927613
TV
7257 filename = debug_filename.get ();
7258 }
0018ea6f 7259
7aa104c4 7260 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7261
7262 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7263 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7264
b3b3bada 7265 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7266
7267 dwarf2_find_base_address (comp_unit_die, cu);
7268
7269 /* Possibly set the default values of LOWPC and HIGHPC from
7270 `DW_AT_ranges'. */
3a2b436a
JK
7271 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7272 &best_highpc, cu, pst);
7273 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7274 {
7275 CORE_ADDR low
7276 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7277 - baseaddr);
7278 CORE_ADDR high
7279 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7280 - baseaddr - 1);
7281 /* Store the contiguous range if it is not empty; it can be
7282 empty for CUs with no code. */
84685904 7283 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 7284 low, high, pst);
79748972 7285 }
0018ea6f
DE
7286
7287 /* Check if comp unit has_children.
7288 If so, read the rest of the partial symbols from this comp unit.
7289 If not, there's no more debug_info for this comp unit. */
3e225074 7290 if (comp_unit_die->has_children)
0018ea6f
DE
7291 {
7292 struct partial_die_info *first_die;
7293 CORE_ADDR lowpc, highpc;
7294
7295 lowpc = ((CORE_ADDR) -1);
7296 highpc = ((CORE_ADDR) 0);
7297
7298 first_die = load_partial_dies (reader, info_ptr, 1);
7299
7300 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7301 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7302
7303 /* If we didn't find a lowpc, set it to highpc to avoid
7304 complaints from `maint check'. */
7305 if (lowpc == ((CORE_ADDR) -1))
7306 lowpc = highpc;
7307
7308 /* If the compilation unit didn't have an explicit address range,
7309 then use the information extracted from its child dies. */
e385593e 7310 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7311 {
7312 best_lowpc = lowpc;
7313 best_highpc = highpc;
7314 }
7315 }
4ae976d1 7316 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7317 best_lowpc + baseaddr)
7318 - baseaddr);
4ae976d1 7319 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7320 best_highpc + baseaddr)
7321 - baseaddr);
0018ea6f 7322
ae7754b2 7323 pst->end ();
0018ea6f 7324
ae640021 7325 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7326 {
7327 int i;
ae640021 7328 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7329
7330 /* Fill in 'dependencies' here; we fill in 'users' in a
7331 post-pass. */
7332 pst->number_of_dependencies = len;
a9342b62 7333 pst->dependencies
84685904 7334 = per_bfd->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7335 for (i = 0; i < len; ++i)
7336 {
7337 pst->dependencies[i]
7338 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7339 }
0018ea6f 7340
ae640021 7341 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7342 }
7343
7344 /* Get the list of files included in the current compilation unit,
7345 and build a psymtab for each of them. */
7346 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7347
6f738b01
SM
7348 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7349 ", %d global, %d static syms",
7350 per_cu->is_debug_types ? "type" : "comp",
7351 sect_offset_str (per_cu->sect_off),
7352 paddress (gdbarch, pst->text_low (objfile)),
7353 paddress (gdbarch, pst->text_high (objfile)),
7354 (int) pst->global_psymbols.size (),
7355 (int) pst->static_psymbols.size ());
0018ea6f
DE
7356}
7357
7358/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7359 Process compilation unit THIS_CU for a psymtab. */
7360
7361static void
ab432490
SM
7362process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7363 dwarf2_per_objfile *per_objfile,
135f5437 7364 bool want_partial_unit,
b93601f3 7365 enum language pretend_language)
0018ea6f
DE
7366{
7367 /* If this compilation unit was already read in, free the
7368 cached copy in order to read it in again. This is
7369 necessary because we skipped some symbols when we first
7370 read in the compilation unit (see load_partial_dies).
7371 This problem could be avoided, but the benefit is unclear. */
7188ed02 7372 per_objfile->remove_cu (this_cu);
0018ea6f 7373
2e671100 7374 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7375
58990295
TV
7376 switch (reader.comp_unit_die->tag)
7377 {
7378 case DW_TAG_compile_unit:
7379 this_cu->unit_type = DW_UT_compile;
7380 break;
7381 case DW_TAG_partial_unit:
7382 this_cu->unit_type = DW_UT_partial;
7383 break;
e77b0004
TV
7384 case DW_TAG_type_unit:
7385 this_cu->unit_type = DW_UT_type;
7386 break;
58990295 7387 default:
702cf3f5
AB
7388 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7389 dwarf_tag_name (reader.comp_unit_die->tag),
7390 sect_offset_str (reader.cu->per_cu->sect_off),
7391 objfile_name (per_objfile->objfile));
58990295
TV
7392 }
7393
c0ab21c2 7394 if (reader.dummy_p)
f1902523 7395 {
c0ab21c2 7396 /* Nothing. */
f1902523 7397 }
c0ab21c2 7398 else if (this_cu->is_debug_types)
3e225074
TT
7399 build_type_psymtabs_reader (&reader, reader.info_ptr,
7400 reader.comp_unit_die);
135f5437
TT
7401 else if (want_partial_unit
7402 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7403 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7404 reader.comp_unit_die,
c0ab21c2 7405 pretend_language);
0018ea6f 7406
7188ed02 7407 this_cu->lang = reader.cu->language;
58990295 7408
0018ea6f 7409 /* Age out any secondary CUs. */
7188ed02 7410 per_objfile->age_comp_units ();
0018ea6f 7411}
f4dc4d17
DE
7412
7413/* Reader function for build_type_psymtabs. */
7414
7415static void
7416build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7417 const gdb_byte *info_ptr,
3e225074 7418 struct die_info *type_unit_die)
f4dc4d17 7419{
976ca316 7420 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7421 struct dwarf2_cu *cu = reader->cu;
7422 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7423 struct signatured_type *sig_type;
f4dc4d17
DE
7424 struct type_unit_group *tu_group;
7425 struct attribute *attr;
7426 struct partial_die_info *first_die;
7427 CORE_ADDR lowpc, highpc;
891813be 7428 dwarf2_psymtab *pst;
f4dc4d17 7429
0186c6a7
DE
7430 gdb_assert (per_cu->is_debug_types);
7431 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7432
3e225074 7433 if (! type_unit_die->has_children)
f4dc4d17
DE
7434 return;
7435
052c8bb8 7436 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7437 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7438
df07e2c7 7439 if (tu_group->tus == nullptr)
a8b3b8e9 7440 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7441 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7442
7443 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7444 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7445 pst->anonymous = true;
f4dc4d17
DE
7446
7447 first_die = load_partial_dies (reader, info_ptr, 1);
7448
7449 lowpc = (CORE_ADDR) -1;
7450 highpc = (CORE_ADDR) 0;
7451 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7452
ae7754b2 7453 pst->end ();
f4dc4d17
DE
7454}
7455
73051182
DE
7456/* Struct used to sort TUs by their abbreviation table offset. */
7457
7458struct tu_abbrev_offset
7459{
b2bdb8cf
SM
7460 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7461 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7462 {}
7463
7464 signatured_type *sig_type;
73051182
DE
7465 sect_offset abbrev_offset;
7466};
7467
484cf504 7468/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7469
484cf504
TT
7470static bool
7471sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7472 const struct tu_abbrev_offset &b)
73051182 7473{
484cf504 7474 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7475}
7476
7477/* Efficiently read all the type units.
7478 This does the bulk of the work for build_type_psymtabs.
7479
7480 The efficiency is because we sort TUs by the abbrev table they use and
7481 only read each abbrev table once. In one program there are 200K TUs
7482 sharing 8K abbrev tables.
7483
7484 The main purpose of this function is to support building the
5989a64e 7485 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7486 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7487 can collapse the search space by grouping them by stmt_list.
7488 The savings can be significant, in the same program from above the 200K TUs
7489 share 8K stmt_list tables.
7490
7491 FUNC is expected to call get_type_unit_group, which will create the
7492 struct type_unit_group if necessary and add it to
5989a64e 7493 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7494
7495static void
976ca316 7496build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7497{
976ca316 7498 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7499 abbrev_table_up abbrev_table;
73051182 7500 sect_offset abbrev_offset;
73051182
DE
7501
7502 /* It's up to the caller to not call us multiple times. */
976ca316 7503 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7504
976ca316 7505 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7506 return;
7507
7508 /* TUs typically share abbrev tables, and there can be way more TUs than
7509 abbrev tables. Sort by abbrev table to reduce the number of times we
7510 read each abbrev table in.
7511 Alternatives are to punt or to maintain a cache of abbrev tables.
7512 This is simpler and efficient enough for now.
7513
7514 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7515 symtab to use). Typically TUs with the same abbrev offset have the same
7516 stmt_list value too so in practice this should work well.
7517
7518 The basic algorithm here is:
7519
7520 sort TUs by abbrev table
7521 for each TU with same abbrev table:
7522 read abbrev table if first user
7523 read TU top level DIE
7524 [IWBN if DWO skeletons had DW_AT_stmt_list]
7525 call FUNC */
7526
6f738b01 7527 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7528
7529 /* Sort in a separate table to maintain the order of all_type_units
7530 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7531 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7532 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7533
976ca316 7534 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7535 sorted_by_abbrev.emplace_back
976ca316 7536 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7537 sig_type->per_cu.sect_off));
73051182 7538
484cf504
TT
7539 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7540 sort_tu_by_abbrev_offset);
73051182 7541
9c541725 7542 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7543
b2bdb8cf 7544 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7545 {
73051182
DE
7546 /* Switch to the next abbrev table if necessary. */
7547 if (abbrev_table == NULL
b2bdb8cf 7548 || tu.abbrev_offset != abbrev_offset)
73051182 7549 {
b2bdb8cf 7550 abbrev_offset = tu.abbrev_offset;
606decb2 7551 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7552 abbrev_table =
606decb2 7553 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7554 ++tu_stats->nr_uniq_abbrev_tables;
7555 }
7556
976ca316 7557 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7558 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7559 if (!reader.dummy_p)
7560 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7561 reader.comp_unit_die);
73051182 7562 }
6aa5f3a6 7563}
73051182 7564
6aa5f3a6
DE
7565/* Print collected type unit statistics. */
7566
7567static void
976ca316 7568print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7569{
976ca316 7570 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7571
6f738b01
SM
7572 dwarf_read_debug_printf ("Type unit statistics:");
7573 dwarf_read_debug_printf (" %zu TUs",
7574 per_objfile->per_bfd->all_type_units.size ());
7575 dwarf_read_debug_printf (" %d uniq abbrev tables",
7576 tu_stats->nr_uniq_abbrev_tables);
7577 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7578 tu_stats->nr_symtabs);
7579 dwarf_read_debug_printf (" %d symtab sharers",
7580 tu_stats->nr_symtab_sharers);
7581 dwarf_read_debug_printf (" %d type units without a stmt_list",
7582 tu_stats->nr_stmt_less_type_units);
7583 dwarf_read_debug_printf (" %d all_type_units reallocs",
7584 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7585}
7586
f4dc4d17
DE
7587/* Traversal function for build_type_psymtabs. */
7588
7589static int
7590build_type_psymtab_dependencies (void **slot, void *info)
7591{
976ca316 7592 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
84685904 7593 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 7594 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7595 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7596 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7597 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7598 int i;
7599
7600 gdb_assert (len > 0);
197400e8 7601 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7602
7603 pst->number_of_dependencies = len;
84685904 7604 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
df07e2c7 7605 for (i = 0; i < len; ++i)
f4dc4d17 7606 {
df07e2c7 7607 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7608 gdb_assert (iter->per_cu.is_debug_types);
7609 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7610 iter->type_unit_group = tu_group;
f4dc4d17
DE
7611 }
7612
df07e2c7
AB
7613 delete tu_group->tus;
7614 tu_group->tus = nullptr;
348e048f
DE
7615
7616 return 1;
7617}
7618
7619/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7620 Build partial symbol tables for the .debug_types comp-units. */
7621
7622static void
976ca316 7623build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7624{
976ca316 7625 if (! create_all_type_units (per_objfile))
348e048f
DE
7626 return;
7627
976ca316 7628 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7629}
f4dc4d17 7630
6aa5f3a6
DE
7631/* Traversal function for process_skeletonless_type_unit.
7632 Read a TU in a DWO file and build partial symbols for it. */
7633
7634static int
7635process_skeletonless_type_unit (void **slot, void *info)
7636{
7637 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7638 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7639 struct signatured_type find_entry, *entry;
7640
7641 /* If this TU doesn't exist in the global table, add it and read it in. */
7642
976ca316
SM
7643 if (per_objfile->per_bfd->signatured_types == NULL)
7644 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7645
7646 find_entry.signature = dwo_unit->signature;
976ca316 7647 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7648 &find_entry, INSERT);
6aa5f3a6
DE
7649 /* If we've already seen this type there's nothing to do. What's happening
7650 is we're doing our own version of comdat-folding here. */
7651 if (*slot != NULL)
7652 return 1;
7653
7654 /* This does the job that create_all_type_units would have done for
7655 this TU. */
976ca316
SM
7656 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7657 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7658 *slot = entry;
7659
7660 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7661 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7662 if (!reader.dummy_p)
7663 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7664 reader.comp_unit_die);
6aa5f3a6
DE
7665
7666 return 1;
7667}
7668
7669/* Traversal function for process_skeletonless_type_units. */
7670
7671static int
7672process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7673{
7674 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7675
7676 if (dwo_file->tus != NULL)
b0b6a987
TT
7677 htab_traverse_noresize (dwo_file->tus.get (),
7678 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7679
7680 return 1;
7681}
7682
7683/* Scan all TUs of DWO files, verifying we've processed them.
7684 This is needed in case a TU was emitted without its skeleton.
7685 Note: This can't be done until we know what all the DWO files are. */
7686
7687static void
976ca316 7688process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7689{
7690 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7691 if (get_dwp_file (per_objfile) == NULL
7692 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7693 {
976ca316 7694 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7695 process_dwo_file_for_skeletonless_type_units,
976ca316 7696 per_objfile);
6aa5f3a6 7697 }
348e048f
DE
7698}
7699
ed2dc618 7700/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7701
7702static void
976ca316 7703set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7704{
976ca316 7705 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7706 {
891813be 7707 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7708
36586728
TT
7709 if (pst == NULL)
7710 continue;
7711
b76e467d 7712 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7713 {
7714 /* Set the 'user' field only if it is not already set. */
7715 if (pst->dependencies[j]->user == NULL)
7716 pst->dependencies[j]->user = pst;
7717 }
7718 }
7719}
7720
93311388
DE
7721/* Build the partial symbol table by doing a quick pass through the
7722 .debug_info and .debug_abbrev sections. */
72bf9492 7723
93311388 7724static void
976ca316 7725dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7726{
976ca316 7727 struct objfile *objfile = per_objfile->objfile;
84685904 7728 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
93311388 7729
6f738b01
SM
7730 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7731 objfile_name (objfile));
45cfd468 7732
76935768 7733 scoped_restore restore_reading_psyms
bab287cd 7734 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
98bfdba5 7735
84685904 7736 per_bfd->info.read (objfile);
91c24f0a 7737
93311388
DE
7738 /* Any cached compilation units will be linked by the per-objfile
7739 read_in_chain. Make sure to free them when we're done. */
976ca316 7740 free_cached_comp_units freer (per_objfile);
72bf9492 7741
976ca316 7742 build_type_psymtabs (per_objfile);
348e048f 7743
976ca316 7744 create_all_comp_units (per_objfile);
c906108c 7745
60606b2c
TT
7746 /* Create a temporary address map on a temporary obstack. We later
7747 copy this to the final obstack. */
8268c778 7748 auto_obstack temp_obstack;
791afaa2
TT
7749
7750 scoped_restore save_psymtabs_addrmap
84685904 7751 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
791afaa2 7752 addrmap_create_mutable (&temp_obstack));
72bf9492 7753
84685904 7754 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
3d5afab3
TV
7755 {
7756 if (per_cu->v.psymtab != NULL)
7757 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7758 continue;
976ca316 7759 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 7760 language_minimal);
3d5afab3 7761 }
ff013f42 7762
6aa5f3a6 7763 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 7764 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
7765
7766 /* Now that all TUs have been processed we can fill in the dependencies. */
84685904 7767 if (per_bfd->type_unit_groups != NULL)
6aa5f3a6 7768 {
84685904 7769 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
976ca316 7770 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
7771 }
7772
6f738b01 7773 if (dwarf_read_debug > 0)
976ca316 7774 print_tu_stats (per_objfile);
6aa5f3a6 7775
976ca316 7776 set_partial_user (per_objfile);
95554aad 7777
84685904
TT
7778 per_bfd->partial_symtabs->psymtabs_addrmap
7779 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7780 per_bfd->partial_symtabs->obstack ());
791afaa2
TT
7781 /* At this point we want to keep the address map. */
7782 save_psymtabs_addrmap.release ();
ff013f42 7783
6f738b01
SM
7784 dwarf_read_debug_printf ("Done building psymtabs of %s",
7785 objfile_name (objfile));
ae038cb0
DJ
7786}
7787
dee91e82
DE
7788/* Load the partial DIEs for a secondary CU into memory.
7789 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7790
dee91e82 7791static void
ab432490 7792load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
7793 dwarf2_per_objfile *per_objfile,
7794 dwarf2_cu *existing_cu)
dee91e82 7795{
2e671100 7796 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
7797
7798 if (!reader.dummy_p)
7799 {
7800 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7801 language_minimal);
7802
7803 /* Check if comp unit has_children.
7804 If so, read the rest of the partial symbols from this comp unit.
7805 If not, there's no more debug_info for this comp unit. */
3e225074 7806 if (reader.comp_unit_die->has_children)
c0ab21c2 7807 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7808
7809 reader.keep ();
c0ab21c2 7810 }
ae038cb0
DJ
7811}
7812
ae038cb0 7813static void
976ca316 7814read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 7815 struct dwarf2_section_info *section,
f1902523 7816 struct dwarf2_section_info *abbrev_section,
b76e467d 7817 unsigned int is_dwz)
ae038cb0 7818{
d521ce57 7819 const gdb_byte *info_ptr;
976ca316 7820 struct objfile *objfile = per_objfile->objfile;
be391dca 7821
6f738b01
SM
7822 dwarf_read_debug_printf ("Reading %s for %s",
7823 section->get_name (),
7824 section->get_file_name ());
bf6af496 7825
96b79293 7826 section->read (objfile);
ae038cb0 7827
36586728 7828 info_ptr = section->buffer;
6e70227d 7829
36586728 7830 while (info_ptr < section->buffer + section->size)
ae038cb0 7831 {
ae038cb0 7832 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7833
9c541725 7834 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7835
f1902523 7836 comp_unit_head cu_header;
976ca316 7837 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
7838 abbrev_section, info_ptr,
7839 rcuh_kind::COMPILE);
ae038cb0
DJ
7840
7841 /* Save the compilation unit for later lookup. */
f1902523 7842 if (cu_header.unit_type != DW_UT_type)
976ca316 7843 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
7844 else
7845 {
976ca316 7846 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
7847 sig_type->signature = cu_header.signature;
7848 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7849 this_cu = &sig_type->per_cu;
7850 }
7851 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7852 this_cu->sect_off = sect_off;
f1902523 7853 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7854 this_cu->is_dwz = is_dwz;
8a0459fd 7855 this_cu->section = section;
ae038cb0 7856
976ca316 7857 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7858
7859 info_ptr = info_ptr + this_cu->length;
7860 }
36586728
TT
7861}
7862
7863/* Create a list of all compilation units in OBJFILE.
7864 This is only done for -readnow and building partial symtabs. */
7865
7866static void
976ca316 7867create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 7868{
976ca316
SM
7869 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
7870 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7871 &per_objfile->per_bfd->abbrev, 0);
36586728 7872
976ca316 7873 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 7874 if (dwz != NULL)
976ca316 7875 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
7876}
7877
5734ee8b 7878/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7879 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7880 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7881 DW_AT_ranges). See the comments of add_partial_subprogram on how
7882 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7883
72bf9492
DJ
7884static void
7885scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7886 CORE_ADDR *highpc, int set_addrmap,
7887 struct dwarf2_cu *cu)
c906108c 7888{
72bf9492 7889 struct partial_die_info *pdi;
c906108c 7890
91c24f0a
DC
7891 /* Now, march along the PDI's, descending into ones which have
7892 interesting children but skipping the children of the other ones,
7893 until we reach the end of the compilation unit. */
c906108c 7894
72bf9492 7895 pdi = first_die;
91c24f0a 7896
72bf9492
DJ
7897 while (pdi != NULL)
7898 {
52356b79 7899 pdi->fixup (cu);
c906108c 7900
f55ee35c 7901 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7902 children, so we need to look at them. Ditto for anonymous
7903 enums. */
933c6fe4 7904
7d00ffec 7905 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7906 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7907 || pdi->tag == DW_TAG_imported_unit
7908 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7909 {
72bf9492 7910 switch (pdi->tag)
c906108c
SS
7911 {
7912 case DW_TAG_subprogram:
b1dc1806 7913 case DW_TAG_inlined_subroutine:
cdc07690 7914 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
7915 if (cu->language == language_cplus)
7916 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7917 set_addrmap, cu);
c906108c 7918 break;
72929c62 7919 case DW_TAG_constant:
c906108c
SS
7920 case DW_TAG_variable:
7921 case DW_TAG_typedef:
91c24f0a 7922 case DW_TAG_union_type:
317d2668
TV
7923 if (!pdi->is_declaration
7924 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 7925 {
72bf9492 7926 add_partial_symbol (pdi, cu);
63d06c5c
DC
7927 }
7928 break;
c906108c 7929 case DW_TAG_class_type:
680b30c7 7930 case DW_TAG_interface_type:
c906108c 7931 case DW_TAG_structure_type:
72bf9492 7932 if (!pdi->is_declaration)
c906108c 7933 {
72bf9492 7934 add_partial_symbol (pdi, cu);
c906108c 7935 }
b7fee5a3
KS
7936 if ((cu->language == language_rust
7937 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7938 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7939 set_addrmap, cu);
c906108c 7940 break;
91c24f0a 7941 case DW_TAG_enumeration_type:
72bf9492
DJ
7942 if (!pdi->is_declaration)
7943 add_partial_enumeration (pdi, cu);
c906108c
SS
7944 break;
7945 case DW_TAG_base_type:
dda83cd7 7946 case DW_TAG_subrange_type:
c906108c 7947 /* File scope base type definitions are added to the partial
dda83cd7 7948 symbol table. */
72bf9492 7949 add_partial_symbol (pdi, cu);
c906108c 7950 break;
d9fa45fe 7951 case DW_TAG_namespace:
cdc07690 7952 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7953 break;
5d7cb8df 7954 case DW_TAG_module:
59c35742
AB
7955 if (!pdi->is_declaration)
7956 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7957 break;
95554aad
TT
7958 case DW_TAG_imported_unit:
7959 {
7960 struct dwarf2_per_cu_data *per_cu;
7961
f4dc4d17
DE
7962 /* For now we don't handle imported units in type units. */
7963 if (cu->per_cu->is_debug_types)
7964 {
7965 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7966 " supported in type units [in module %s]"),
5e22e966 7967 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
7968 }
7969
e3b94546 7970 per_cu = dwarf2_find_containing_comp_unit
5e22e966 7971 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
7972
7973 /* Go read the partial unit, if needed. */
7974 if (per_cu->v.psymtab == NULL)
ab432490
SM
7975 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7976 cu->language);
95554aad 7977
ae640021 7978 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7979 }
7980 break;
74921315
KS
7981 case DW_TAG_imported_declaration:
7982 add_partial_symbol (pdi, cu);
7983 break;
c906108c
SS
7984 default:
7985 break;
7986 }
7987 }
7988
72bf9492
DJ
7989 /* If the die has a sibling, skip to the sibling. */
7990
7991 pdi = pdi->die_sibling;
7992 }
7993}
7994
7995/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7996
72bf9492 7997 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7998 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7999 Enumerators are an exception; they use the scope of their parent
8000 enumeration type, i.e. the name of the enumeration type is not
8001 prepended to the enumerator.
91c24f0a 8002
72bf9492
DJ
8003 There are two complexities. One is DW_AT_specification; in this
8004 case "parent" means the parent of the target of the specification,
8005 instead of the direct parent of the DIE. The other is compilers
8006 which do not emit DW_TAG_namespace; in this case we try to guess
8007 the fully qualified name of structure types from their members'
8008 linkage names. This must be done using the DIE's children rather
8009 than the children of any DW_AT_specification target. We only need
8010 to do this for structures at the top level, i.e. if the target of
8011 any DW_AT_specification (if any; otherwise the DIE itself) does not
8012 have a parent. */
8013
8014/* Compute the scope prefix associated with PDI's parent, in
8015 compilation unit CU. The result will be allocated on CU's
8016 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8017 field. NULL is returned if no prefix is necessary. */
15d034d0 8018static const char *
72bf9492
DJ
8019partial_die_parent_scope (struct partial_die_info *pdi,
8020 struct dwarf2_cu *cu)
8021{
15d034d0 8022 const char *grandparent_scope;
72bf9492 8023 struct partial_die_info *parent, *real_pdi;
91c24f0a 8024
72bf9492
DJ
8025 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8026 then this means the parent of the specification DIE. */
8027
8028 real_pdi = pdi;
72bf9492 8029 while (real_pdi->has_specification)
fb816e8b 8030 {
122cf0f2
AB
8031 auto res = find_partial_die (real_pdi->spec_offset,
8032 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8033 real_pdi = res.pdi;
8034 cu = res.cu;
8035 }
72bf9492
DJ
8036
8037 parent = real_pdi->die_parent;
8038 if (parent == NULL)
8039 return NULL;
8040
8041 if (parent->scope_set)
8042 return parent->scope;
8043
52356b79 8044 parent->fixup (cu);
72bf9492 8045
10b3939b 8046 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8047
acebe513
UW
8048 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8049 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8050 Work around this problem here. */
8051 if (cu->language == language_cplus
6e70227d 8052 && parent->tag == DW_TAG_namespace
7d00ffec 8053 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8054 && grandparent_scope == NULL)
8055 {
8056 parent->scope = NULL;
8057 parent->scope_set = 1;
8058 return NULL;
8059 }
8060
0a4b0913 8061 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8062 if (pdi->tag == DW_TAG_enumerator)
8063 /* Enumerators should not get the name of the enumeration as a prefix. */
8064 parent->scope = grandparent_scope;
8065 else if (parent->tag == DW_TAG_namespace
f55ee35c 8066 || parent->tag == DW_TAG_module
72bf9492
DJ
8067 || parent->tag == DW_TAG_structure_type
8068 || parent->tag == DW_TAG_class_type
680b30c7 8069 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8070 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8071 || parent->tag == DW_TAG_enumeration_type
8072 || (cu->language == language_fortran
8073 && parent->tag == DW_TAG_subprogram
8074 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8075 {
8076 if (grandparent_scope == NULL)
7d00ffec 8077 parent->scope = parent->name (cu);
72bf9492 8078 else
3e43a32a
MS
8079 parent->scope = typename_concat (&cu->comp_unit_obstack,
8080 grandparent_scope,
7d00ffec 8081 parent->name (cu), 0, cu);
72bf9492 8082 }
72bf9492
DJ
8083 else
8084 {
8085 /* FIXME drow/2004-04-01: What should we be doing with
8086 function-local names? For partial symbols, we should probably be
8087 ignoring them. */
fa9c3fa0
TT
8088 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8089 dwarf_tag_name (parent->tag),
8090 sect_offset_str (pdi->sect_off));
72bf9492 8091 parent->scope = grandparent_scope;
c906108c
SS
8092 }
8093
72bf9492
DJ
8094 parent->scope_set = 1;
8095 return parent->scope;
8096}
8097
8098/* Return the fully scoped name associated with PDI, from compilation unit
8099 CU. The result will be allocated with malloc. */
4568ecf9 8100
43816ebc 8101static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8102partial_die_full_name (struct partial_die_info *pdi,
8103 struct dwarf2_cu *cu)
8104{
15d034d0 8105 const char *parent_scope;
72bf9492 8106
98bfdba5
PA
8107 /* If this is a template instantiation, we can not work out the
8108 template arguments from partial DIEs. So, unfortunately, we have
8109 to go through the full DIEs. At least any work we do building
8110 types here will be reused if full symbols are loaded later. */
8111 if (pdi->has_template_arguments)
8112 {
52356b79 8113 pdi->fixup (cu);
98bfdba5 8114
7d00ffec 8115 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8116 {
8117 struct die_info *die;
8118 struct attribute attr;
8119 struct dwarf2_cu *ref_cu = cu;
8120
b64f50a1 8121 /* DW_FORM_ref_addr is using section offset. */
b4069958 8122 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8123 attr.form = DW_FORM_ref_addr;
9c541725 8124 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8125 die = follow_die_ref (NULL, &attr, &ref_cu);
8126
43816ebc 8127 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8128 }
8129 }
8130
72bf9492
DJ
8131 parent_scope = partial_die_parent_scope (pdi, cu);
8132 if (parent_scope == NULL)
8133 return NULL;
8134 else
43816ebc 8135 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8136 pdi->name (cu),
8137 0, cu));
c906108c
SS
8138}
8139
8140static void
72bf9492 8141add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8142{
976ca316
SM
8143 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8144 struct objfile *objfile = per_objfile->objfile;
08feed99 8145 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8146 CORE_ADDR addr = 0;
15d034d0 8147 const char *actual_name = NULL;
e142c38c
DJ
8148 CORE_ADDR baseaddr;
8149
b3b3bada 8150 baseaddr = objfile->text_section_offset ();
c906108c 8151
43816ebc
TT
8152 gdb::unique_xmalloc_ptr<char> built_actual_name
8153 = partial_die_full_name (pdi, cu);
15d034d0 8154 if (built_actual_name != NULL)
43816ebc 8155 actual_name = built_actual_name.get ();
63d06c5c 8156
72bf9492 8157 if (actual_name == NULL)
7d00ffec 8158 actual_name = pdi->name (cu);
72bf9492 8159
76e288d1
TT
8160 partial_symbol psymbol;
8161 memset (&psymbol, 0, sizeof (psymbol));
8162 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 8163 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
8164
8165 /* The code below indicates that the psymbol should be installed by
8166 setting this. */
8167 gdb::optional<psymbol_placement> where;
8168
c906108c
SS
8169 switch (pdi->tag)
8170 {
b1dc1806 8171 case DW_TAG_inlined_subroutine:
c906108c 8172 case DW_TAG_subprogram:
79748972
TT
8173 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8174 - baseaddr);
0a4b0913
AB
8175 if (pdi->is_external
8176 || cu->language == language_ada
8177 || (cu->language == language_fortran
8178 && pdi->die_parent != NULL
8179 && pdi->die_parent->tag == DW_TAG_subprogram))
8180 {
dda83cd7
SM
8181 /* Normally, only "external" DIEs are part of the global scope.
8182 But in Ada and Fortran, we want to be able to access nested
8183 procedures globally. So all Ada and Fortran subprograms are
8184 stored in the global scope. */
76e288d1 8185 where = psymbol_placement::GLOBAL;
c906108c
SS
8186 }
8187 else
76e288d1
TT
8188 where = psymbol_placement::STATIC;
8189
8190 psymbol.domain = VAR_DOMAIN;
8191 psymbol.aclass = LOC_BLOCK;
a52d653e 8192 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 8193 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8194
8195 if (pdi->main_subprogram && actual_name != NULL)
8196 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8197 break;
72929c62 8198 case DW_TAG_constant:
76e288d1
TT
8199 psymbol.domain = VAR_DOMAIN;
8200 psymbol.aclass = LOC_STATIC;
8201 where = (pdi->is_external
8202 ? psymbol_placement::GLOBAL
8203 : psymbol_placement::STATIC);
72929c62 8204 break;
c906108c 8205 case DW_TAG_variable:
95554aad
TT
8206 if (pdi->d.locdesc)
8207 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8208
95554aad 8209 if (pdi->d.locdesc
caac4577 8210 && addr == 0
976ca316 8211 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8212 {
8213 /* A global or static variable may also have been stripped
8214 out by the linker if unused, in which case its address
8215 will be nullified; do not add such variables into partial
8216 symbol table then. */
8217 }
8218 else if (pdi->is_external)
c906108c
SS
8219 {
8220 /* Global Variable.
8221 Don't enter into the minimal symbol tables as there is
8222 a minimal symbol table entry from the ELF symbols already.
8223 Enter into partial symbol table if it has a location
8224 descriptor or a type.
8225 If the location descriptor is missing, new_symbol will create
8226 a LOC_UNRESOLVED symbol, the address of the variable will then
8227 be determined from the minimal symbol table whenever the variable
8228 is referenced.
8229 The address for the partial symbol table entry is not
8230 used by GDB, but it comes in handy for debugging partial symbol
8231 table building. */
8232
95554aad 8233 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8234 {
8235 psymbol.domain = VAR_DOMAIN;
8236 psymbol.aclass = LOC_STATIC;
a52d653e 8237 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8238 psymbol.ginfo.value.address = addr;
8239 where = psymbol_placement::GLOBAL;
8240 }
c906108c
SS
8241 }
8242 else
8243 {
ff908ebf
AW
8244 int has_loc = pdi->d.locdesc != NULL;
8245
8246 /* Static Variable. Skip symbols whose value we cannot know (those
8247 without location descriptors or constant values). */
8248 if (!has_loc && !pdi->has_const_value)
43816ebc 8249 return;
ff908ebf 8250
76e288d1
TT
8251 psymbol.domain = VAR_DOMAIN;
8252 psymbol.aclass = LOC_STATIC;
a52d653e 8253 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8254 if (has_loc)
8255 psymbol.ginfo.value.address = addr;
8256 where = psymbol_placement::STATIC;
c906108c
SS
8257 }
8258 break;
d8f62e84 8259 case DW_TAG_array_type:
c906108c
SS
8260 case DW_TAG_typedef:
8261 case DW_TAG_base_type:
a02abb62 8262 case DW_TAG_subrange_type:
76e288d1
TT
8263 psymbol.domain = VAR_DOMAIN;
8264 psymbol.aclass = LOC_TYPEDEF;
8265 where = psymbol_placement::STATIC;
c906108c 8266 break;
74921315 8267 case DW_TAG_imported_declaration:
72bf9492 8268 case DW_TAG_namespace:
76e288d1
TT
8269 psymbol.domain = VAR_DOMAIN;
8270 psymbol.aclass = LOC_TYPEDEF;
8271 where = psymbol_placement::GLOBAL;
72bf9492 8272 break;
530e8392 8273 case DW_TAG_module:
a5fd13a9 8274 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8275 available without any name. If so, we skip the module as it
8276 doesn't bring any value. */
a5fd13a9 8277 if (actual_name != nullptr)
76e288d1
TT
8278 {
8279 psymbol.domain = MODULE_DOMAIN;
8280 psymbol.aclass = LOC_TYPEDEF;
8281 where = psymbol_placement::GLOBAL;
8282 }
530e8392 8283 break;
c906108c 8284 case DW_TAG_class_type:
680b30c7 8285 case DW_TAG_interface_type:
c906108c
SS
8286 case DW_TAG_structure_type:
8287 case DW_TAG_union_type:
8288 case DW_TAG_enumeration_type:
fa4028e9 8289 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8290 about "Structure, Union, and Class Type Entries": "An incomplete
8291 structure, union or class type is represented by a structure,
8292 union or class entry that does not have a byte size attribute
8293 and that has a DW_AT_declaration attribute." */
fa4028e9 8294 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8295 return;
fa4028e9 8296
63d06c5c
DC
8297 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8298 static vs. global. */
76e288d1
TT
8299 psymbol.domain = STRUCT_DOMAIN;
8300 psymbol.aclass = LOC_TYPEDEF;
8301 where = (cu->language == language_cplus
8302 ? psymbol_placement::GLOBAL
8303 : psymbol_placement::STATIC);
c906108c
SS
8304 break;
8305 case DW_TAG_enumerator:
76e288d1
TT
8306 psymbol.domain = VAR_DOMAIN;
8307 psymbol.aclass = LOC_CONST;
8308 where = (cu->language == language_cplus
8309 ? psymbol_placement::GLOBAL
8310 : psymbol_placement::STATIC);
c906108c
SS
8311 break;
8312 default:
8313 break;
8314 }
76e288d1
TT
8315
8316 if (where.has_value ())
8317 {
f049a313
TT
8318 if (built_actual_name != nullptr)
8319 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8320 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8321 psymbol.ginfo.set_linkage_name (actual_name);
8322 else
8323 {
8324 psymbol.ginfo.set_demangled_name (actual_name,
8325 &objfile->objfile_obstack);
8326 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8327 }
7b249e47
TT
8328 cu->per_cu->v.psymtab->add_psymbol
8329 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8330 objfile);
76e288d1 8331 }
c906108c
SS
8332}
8333
5c4e30ca
DC
8334/* Read a partial die corresponding to a namespace; also, add a symbol
8335 corresponding to that namespace to the symbol table. NAMESPACE is
8336 the name of the enclosing namespace. */
91c24f0a 8337
72bf9492
DJ
8338static void
8339add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8340 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8341 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8342{
72bf9492 8343 /* Add a symbol for the namespace. */
e7c27a73 8344
72bf9492 8345 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8346
8347 /* Now scan partial symbols in that namespace. */
8348
91c24f0a 8349 if (pdi->has_children)
cdc07690 8350 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8351}
8352
5d7cb8df
JK
8353/* Read a partial die corresponding to a Fortran module. */
8354
8355static void
8356add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8357 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8358{
530e8392
KB
8359 /* Add a symbol for the namespace. */
8360
8361 add_partial_symbol (pdi, cu);
8362
f55ee35c 8363 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8364
8365 if (pdi->has_children)
cdc07690 8366 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8367}
8368
b1dc1806
XR
8369/* Read a partial die corresponding to a subprogram or an inlined
8370 subprogram and create a partial symbol for that subprogram.
8371 When the CU language allows it, this routine also defines a partial
8372 symbol for each nested subprogram that this subprogram contains.
8373 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8374 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8375
cdc07690
YQ
8376 PDI may also be a lexical block, in which case we simply search
8377 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8378 Again, this is only performed when the CU language allows this
8379 type of definitions. */
8380
8381static void
8382add_partial_subprogram (struct partial_die_info *pdi,
8383 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8384 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8385{
b1dc1806 8386 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8387 {
8388 if (pdi->has_pc_info)
dda83cd7
SM
8389 {
8390 if (pdi->lowpc < *lowpc)
8391 *lowpc = pdi->lowpc;
8392 if (pdi->highpc > *highpc)
8393 *highpc = pdi->highpc;
cdc07690 8394 if (set_addrmap)
5734ee8b 8395 {
5e22e966 8396 struct objfile *objfile = cu->per_objfile->objfile;
84685904 8397 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 8398 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8399 CORE_ADDR baseaddr;
b926417a
TT
8400 CORE_ADDR this_highpc;
8401 CORE_ADDR this_lowpc;
5734ee8b 8402
b3b3bada 8403 baseaddr = objfile->text_section_offset ();
b926417a
TT
8404 this_lowpc
8405 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8406 pdi->lowpc + baseaddr)
8407 - baseaddr);
8408 this_highpc
8409 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8410 pdi->highpc + baseaddr)
8411 - baseaddr);
84685904 8412 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
b926417a 8413 this_lowpc, this_highpc - 1,
9291a0cd 8414 cu->per_cu->v.psymtab);
5734ee8b 8415 }
dda83cd7 8416 }
481860b3
GB
8417
8418 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8419 {
dda83cd7 8420 if (!pdi->is_declaration)
e8d05480
JB
8421 /* Ignore subprogram DIEs that do not have a name, they are
8422 illegal. Do not emit a complaint at this point, we will
8423 do so when we convert this psymtab into a symtab. */
7d00ffec 8424 if (pdi->name (cu))
e8d05480 8425 add_partial_symbol (pdi, cu);
dda83cd7 8426 }
bc30ff58 8427 }
6e70227d 8428
bc30ff58
JB
8429 if (! pdi->has_children)
8430 return;
8431
0a4b0913 8432 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8433 {
8434 pdi = pdi->die_child;
8435 while (pdi != NULL)
8436 {
52356b79 8437 pdi->fixup (cu);
bc30ff58 8438 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8439 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8440 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8441 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8442 pdi = pdi->die_sibling;
8443 }
8444 }
8445}
8446
91c24f0a
DC
8447/* Read a partial die corresponding to an enumeration type. */
8448
72bf9492
DJ
8449static void
8450add_partial_enumeration (struct partial_die_info *enum_pdi,
8451 struct dwarf2_cu *cu)
91c24f0a 8452{
72bf9492 8453 struct partial_die_info *pdi;
91c24f0a 8454
7d00ffec 8455 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8456 add_partial_symbol (enum_pdi, cu);
8457
8458 pdi = enum_pdi->die_child;
8459 while (pdi)
91c24f0a 8460 {
7d00ffec 8461 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8462 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8463 else
72bf9492
DJ
8464 add_partial_symbol (pdi, cu);
8465 pdi = pdi->die_sibling;
91c24f0a 8466 }
91c24f0a
DC
8467}
8468
6caca83c
CC
8469/* Return the initial uleb128 in the die at INFO_PTR. */
8470
8471static unsigned int
d521ce57 8472peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8473{
8474 unsigned int bytes_read;
8475
8476 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8477}
8478
685af9cd
TT
8479/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8480 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8481
4bb7a0a7
DJ
8482 Return the corresponding abbrev, or NULL if the number is zero (indicating
8483 an empty DIE). In either case *BYTES_READ will be set to the length of
8484 the initial number. */
8485
7c32eebb 8486static const struct abbrev_info *
685af9cd
TT
8487peek_die_abbrev (const die_reader_specs &reader,
8488 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8489{
685af9cd 8490 dwarf2_cu *cu = reader.cu;
0280fdcc 8491 bfd *abfd = reader.abfd;
685af9cd
TT
8492 unsigned int abbrev_number
8493 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8494
8495 if (abbrev_number == 0)
8496 return NULL;
8497
7c32eebb
TT
8498 const abbrev_info *abbrev
8499 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8500 if (!abbrev)
8501 {
422b9917 8502 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8503 " at offset %s [in module %s]"),
422b9917 8504 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8505 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8506 }
8507
8508 return abbrev;
8509}
8510
93311388
DE
8511/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8512 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8513 DIE. Any children of the skipped DIEs will also be skipped. */
8514
d521ce57
TT
8515static const gdb_byte *
8516skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8517{
4bb7a0a7
DJ
8518 while (1)
8519 {
685af9cd 8520 unsigned int bytes_read;
7c32eebb
TT
8521 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8522 &bytes_read);
685af9cd 8523
4bb7a0a7
DJ
8524 if (abbrev == NULL)
8525 return info_ptr + bytes_read;
8526 else
dee91e82 8527 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8528 }
8529}
8530
93311388
DE
8531/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8532 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8533 abbrev corresponding to that skipped uleb128 should be passed in
8534 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8535 children. */
8536
d521ce57
TT
8537static const gdb_byte *
8538skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8539 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8540{
8541 unsigned int bytes_read;
8542 struct attribute attr;
dee91e82
DE
8543 bfd *abfd = reader->abfd;
8544 struct dwarf2_cu *cu = reader->cu;
d521ce57 8545 const gdb_byte *buffer = reader->buffer;
f664829e 8546 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8547 unsigned int form, i;
8548
8549 for (i = 0; i < abbrev->num_attrs; i++)
8550 {
8551 /* The only abbrev we care about is DW_AT_sibling. */
8552 if (abbrev->attrs[i].name == DW_AT_sibling)
8553 {
7a5f294d 8554 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8555 if (attr.form == DW_FORM_ref_addr)
b98664d3 8556 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8557 else
b9502d3f 8558 {
0826b30a 8559 sect_offset off = attr.get_ref_die_offset ();
9c541725 8560 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8561
8562 if (sibling_ptr < info_ptr)
b98664d3 8563 complaint (_("DW_AT_sibling points backwards"));
22869d73 8564 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8565 reader->die_section->overflow_complaint ();
b9502d3f
WN
8566 else
8567 return sibling_ptr;
8568 }
4bb7a0a7
DJ
8569 }
8570
8571 /* If it isn't DW_AT_sibling, skip this attribute. */
8572 form = abbrev->attrs[i].form;
8573 skip_attribute:
8574 switch (form)
8575 {
4bb7a0a7 8576 case DW_FORM_ref_addr:
ae411497
TT
8577 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8578 and later it is offset sized. */
8579 if (cu->header.version == 2)
8580 info_ptr += cu->header.addr_size;
8581 else
8582 info_ptr += cu->header.offset_size;
8583 break;
36586728
TT
8584 case DW_FORM_GNU_ref_alt:
8585 info_ptr += cu->header.offset_size;
8586 break;
ae411497 8587 case DW_FORM_addr:
4bb7a0a7
DJ
8588 info_ptr += cu->header.addr_size;
8589 break;
8590 case DW_FORM_data1:
8591 case DW_FORM_ref1:
8592 case DW_FORM_flag:
8fe0f950 8593 case DW_FORM_strx1:
4bb7a0a7
DJ
8594 info_ptr += 1;
8595 break;
2dc7f7b3 8596 case DW_FORM_flag_present:
43988095 8597 case DW_FORM_implicit_const:
2dc7f7b3 8598 break;
4bb7a0a7
DJ
8599 case DW_FORM_data2:
8600 case DW_FORM_ref2:
8fe0f950 8601 case DW_FORM_strx2:
4bb7a0a7
DJ
8602 info_ptr += 2;
8603 break;
8fe0f950
AT
8604 case DW_FORM_strx3:
8605 info_ptr += 3;
8606 break;
4bb7a0a7
DJ
8607 case DW_FORM_data4:
8608 case DW_FORM_ref4:
8fe0f950 8609 case DW_FORM_strx4:
4bb7a0a7
DJ
8610 info_ptr += 4;
8611 break;
8612 case DW_FORM_data8:
8613 case DW_FORM_ref8:
55f1336d 8614 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8615 info_ptr += 8;
8616 break;
0224619f
JK
8617 case DW_FORM_data16:
8618 info_ptr += 16;
8619 break;
4bb7a0a7 8620 case DW_FORM_string:
9b1c24c8 8621 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8622 info_ptr += bytes_read;
8623 break;
2dc7f7b3 8624 case DW_FORM_sec_offset:
4bb7a0a7 8625 case DW_FORM_strp:
36586728 8626 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8627 info_ptr += cu->header.offset_size;
8628 break;
2dc7f7b3 8629 case DW_FORM_exprloc:
4bb7a0a7
DJ
8630 case DW_FORM_block:
8631 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8632 info_ptr += bytes_read;
8633 break;
8634 case DW_FORM_block1:
8635 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8636 break;
8637 case DW_FORM_block2:
8638 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8639 break;
8640 case DW_FORM_block4:
8641 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8642 break;
336d760d 8643 case DW_FORM_addrx:
cf532bd1 8644 case DW_FORM_strx:
4bb7a0a7
DJ
8645 case DW_FORM_sdata:
8646 case DW_FORM_udata:
8647 case DW_FORM_ref_udata:
3019eac3
DE
8648 case DW_FORM_GNU_addr_index:
8649 case DW_FORM_GNU_str_index:
18a8505e 8650 case DW_FORM_rnglistx:
41144253 8651 case DW_FORM_loclistx:
d521ce57 8652 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8653 break;
8654 case DW_FORM_indirect:
8655 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8656 info_ptr += bytes_read;
8657 /* We need to continue parsing from here, so just go back to
8658 the top. */
8659 goto skip_attribute;
8660
8661 default:
3e43a32a
MS
8662 error (_("Dwarf Error: Cannot handle %s "
8663 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8664 dwarf_form_name (form),
8665 bfd_get_filename (abfd));
8666 }
8667 }
8668
8669 if (abbrev->has_children)
dee91e82 8670 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8671 else
8672 return info_ptr;
8673}
8674
93311388 8675/* Locate ORIG_PDI's sibling.
dee91e82 8676 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8677
d521ce57 8678static const gdb_byte *
dee91e82
DE
8679locate_pdi_sibling (const struct die_reader_specs *reader,
8680 struct partial_die_info *orig_pdi,
d521ce57 8681 const gdb_byte *info_ptr)
91c24f0a
DC
8682{
8683 /* Do we know the sibling already? */
72bf9492 8684
91c24f0a
DC
8685 if (orig_pdi->sibling)
8686 return orig_pdi->sibling;
8687
8688 /* Are there any children to deal with? */
8689
8690 if (!orig_pdi->has_children)
8691 return info_ptr;
8692
4bb7a0a7 8693 /* Skip the children the long way. */
91c24f0a 8694
dee91e82 8695 return skip_children (reader, info_ptr);
91c24f0a
DC
8696}
8697
257e7a09 8698/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8699 not NULL. */
c906108c 8700
891813be
TT
8701void
8702dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8703{
976ca316 8704 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8705
976ca316 8706 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8707
077cbab2
TT
8708 /* If this psymtab is constructed from a debug-only objfile, the
8709 has_section_at_zero flag will not necessarily be correct. We
8710 can get the correct value for this flag by looking at the data
8711 associated with the (presumably stripped) associated objfile. */
8712 if (objfile->separate_debug_objfile_backlink)
c906108c 8713 {
976ca316 8714 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8715 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8716
976ca316
SM
8717 per_objfile->per_bfd->has_section_at_zero
8718 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8719 }
98bfdba5 8720
8566b89b 8721 expand_psymtab (objfile);
95554aad 8722
976ca316 8723 process_cu_includes (per_objfile);
c906108c 8724}
9cdd5dbd
DE
8725\f
8726/* Reading in full CUs. */
c906108c 8727
10b3939b
DJ
8728/* Add PER_CU to the queue. */
8729
8730static void
120ce1b5
SM
8731queue_comp_unit (dwarf2_per_cu_data *per_cu,
8732 dwarf2_per_objfile *per_objfile,
95554aad 8733 enum language pretend_language)
10b3939b 8734{
10b3939b 8735 per_cu->queued = 1;
08ac5771
SM
8736
8737 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8738 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8739}
8740
616c069a
SM
8741/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8742
89e63ee4
DE
8743 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8744 dependency.
69d751e3 8745
616c069a
SM
8746 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8747 DIEs, false otherwise.
8748
8749 Explanation: there is an invariant that if a CU is queued for expansion
8750 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8751 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8752 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8753 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8754 invariant is respected.
8755
8756 The caller is therefore not required to load the CU's DIEs (we return false)
8757 if:
8758
8759 - the CU is already expanded, and therefore does not get enqueued
8760 - the CU gets enqueued for expansion, but its DIEs are already loaded
8761
8762 Note that the caller should not use this function's return value as an
8763 indicator of whether the CU's DIEs are loaded right now, it should check
8764 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
8765
8766static int
89e63ee4 8767maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8768 dwarf2_per_cu_data *per_cu,
8769 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8770 enum language pretend_language)
8771{
8772 /* We may arrive here during partial symbol reading, if we need full
8773 DIEs to process an unusual case (e.g. template arguments). Do
8774 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8775 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8776 {
7188ed02
SM
8777 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8778
8779 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8780 return 1;
8781 return 0;
8782 }
8783
8784 /* Mark the dependence relation so that we don't flush PER_CU
8785 too early. */
89e63ee4
DE
8786 if (dependent_cu != NULL)
8787 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8788
8789 /* If it's already on the queue, we have nothing to do. */
8790 if (per_cu->queued)
de53369b
SM
8791 {
8792 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8793 loaded. */
8794 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
8795
8796 /* If the CU is queued for expansion, it should not already be
8797 expanded. */
8798 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8799
8800 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
8801 return 0;
8802 }
0907af0c 8803
616c069a
SM
8804 bool queued = false;
8805 if (!per_objfile->symtab_set_p (per_cu))
8806 {
8807 /* Add it to the queue. */
8808 queue_comp_unit (per_cu, per_objfile, pretend_language);
8809 queued = true;
8810 }
8811
0907af0c
DE
8812 /* If the compilation unit is already loaded, just mark it as
8813 used. */
7188ed02
SM
8814 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8815 if (cu != nullptr)
616c069a 8816 cu->last_used = 0;
0907af0c 8817
616c069a
SM
8818 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8819 and the DIEs are not already loaded. */
8820 return queued && cu == nullptr;
0907af0c
DE
8821}
8822
10b3939b
DJ
8823/* Process the queue. */
8824
8825static void
976ca316 8826process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 8827{
6f738b01
SM
8828 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8829 objfile_name (per_objfile->objfile));
45cfd468 8830
03dd20cc
DJ
8831 /* The queue starts out with one item, but following a DIE reference
8832 may load a new CU, adding it to the end of the queue. */
08ac5771 8833 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 8834 {
08ac5771 8835 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 8836 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 8837
976ca316 8838 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 8839 {
976ca316 8840 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 8841
7188ed02
SM
8842 /* Skip dummy CUs. */
8843 if (cu != nullptr)
73be47f5 8844 {
7188ed02
SM
8845 unsigned int debug_print_threshold;
8846 char buf[100];
8847
8848 if (per_cu->is_debug_types)
8849 {
8850 struct signatured_type *sig_type =
8851 (struct signatured_type *) per_cu;
8852
8853 sprintf (buf, "TU %s at offset %s",
8854 hex_string (sig_type->signature),
8855 sect_offset_str (per_cu->sect_off));
8856 /* There can be 100s of TUs.
8857 Only print them in verbose mode. */
8858 debug_print_threshold = 2;
8859 }
8860 else
8861 {
8862 sprintf (buf, "CU at offset %s",
8863 sect_offset_str (per_cu->sect_off));
8864 debug_print_threshold = 1;
8865 }
247f5c4f 8866
7188ed02 8867 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8868 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 8869
7188ed02
SM
8870 if (per_cu->is_debug_types)
8871 process_full_type_unit (cu, item.pretend_language);
8872 else
8873 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 8874
7188ed02 8875 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8876 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 8877 }
f4dc4d17 8878 }
10b3939b 8879
7188ed02 8880 per_cu->queued = 0;
08ac5771 8881 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
8882 }
8883
6f738b01
SM
8884 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8885 objfile_name (per_objfile->objfile));
10b3939b
DJ
8886}
8887
10b3939b
DJ
8888/* Read in full symbols for PST, and anything it depends on. */
8889
8566b89b
TT
8890void
8891dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8892{
af758d11 8893 gdb_assert (!readin_p (objfile));
95554aad 8894
17ee85fc
TT
8895 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8896 free_cached_comp_units freer (per_objfile);
48993951 8897 expand_dependencies (objfile);
aaa75496 8898
97a1449a 8899 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 8900 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
8901}
8902
af758d11
SM
8903/* See psympriv.h. */
8904
8905bool
8906dwarf2_psymtab::readin_p (struct objfile *objfile) const
8907{
8908 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8909 return per_objfile->symtab_set_p (per_cu_data);
8910}
8911
8912/* See psympriv.h. */
8913
8914compunit_symtab *
8915dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8916{
8917 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8918 return per_objfile->get_symtab (per_cu_data);
8919}
8920
dee91e82
DE
8921/* Trivial hash function for die_info: the hash value of a DIE
8922 is its offset in .debug_info for this objfile. */
10b3939b 8923
dee91e82
DE
8924static hashval_t
8925die_hash (const void *item)
10b3939b 8926{
9a3c8263 8927 const struct die_info *die = (const struct die_info *) item;
6502dd73 8928
9c541725 8929 return to_underlying (die->sect_off);
dee91e82 8930}
63d06c5c 8931
dee91e82
DE
8932/* Trivial comparison function for die_info structures: two DIEs
8933 are equal if they have the same offset. */
98bfdba5 8934
dee91e82
DE
8935static int
8936die_eq (const void *item_lhs, const void *item_rhs)
8937{
9a3c8263
SM
8938 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8939 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8940
9c541725 8941 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8942}
c906108c 8943
4a636814
SM
8944/* Load the DIEs associated with PER_CU into memory.
8945
8946 In some cases, the caller, while reading partial symbols, will need to load
8947 the full symbols for the CU for some reason. It will already have a
8948 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8949 rather than creating a new one. */
c906108c 8950
dee91e82 8951static void
ab432490
SM
8952load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8953 dwarf2_per_objfile *per_objfile,
4a636814 8954 dwarf2_cu *existing_cu,
c0ab21c2
TT
8955 bool skip_partial,
8956 enum language pretend_language)
dee91e82 8957{
c0ab21c2
TT
8958 gdb_assert (! this_cu->is_debug_types);
8959
7188ed02 8960 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
8961 if (reader.dummy_p)
8962 return;
8963
8964 struct dwarf2_cu *cu = reader.cu;
8965 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8966
dee91e82
DE
8967 gdb_assert (cu->die_hash == NULL);
8968 cu->die_hash =
8969 htab_create_alloc_ex (cu->header.length / 12,
8970 die_hash,
8971 die_eq,
8972 NULL,
8973 &cu->comp_unit_obstack,
8974 hashtab_obstack_allocate,
8975 dummy_obstack_deallocate);
e142c38c 8976
3e225074 8977 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8978 reader.comp_unit_die->child
8979 = read_die_and_siblings (&reader, reader.info_ptr,
8980 &info_ptr, reader.comp_unit_die);
8981 cu->dies = reader.comp_unit_die;
dee91e82 8982 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8983
8984 /* We try not to read any attributes in this function, because not
9cdd5dbd 8985 all CUs needed for references have been loaded yet, and symbol
10b3939b 8986 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8987 or we won't be able to build types correctly.
8988 Similarly, if we do not read the producer, we can not apply
8989 producer-specific interpretation. */
c0ab21c2 8990 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8991
8992 reader.keep ();
10b3939b
DJ
8993}
8994
3da10d80
KS
8995/* Add a DIE to the delayed physname list. */
8996
8997static void
8998add_to_method_list (struct type *type, int fnfield_index, int index,
8999 const char *name, struct die_info *die,
9000 struct dwarf2_cu *cu)
9001{
9002 struct delayed_method_info mi;
9003 mi.type = type;
9004 mi.fnfield_index = fnfield_index;
9005 mi.index = index;
9006 mi.name = name;
9007 mi.die = die;
c89b44cd 9008 cu->method_list.push_back (mi);
3da10d80
KS
9009}
9010
3693fdb3
PA
9011/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9012 "const" / "volatile". If so, decrements LEN by the length of the
9013 modifier and return true. Otherwise return false. */
9014
9015template<size_t N>
9016static bool
9017check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9018{
9019 size_t mod_len = sizeof (mod) - 1;
9020 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9021 {
9022 len -= mod_len;
9023 return true;
9024 }
9025 return false;
9026}
9027
3da10d80
KS
9028/* Compute the physnames of any methods on the CU's method list.
9029
9030 The computation of method physnames is delayed in order to avoid the
9031 (bad) condition that one of the method's formal parameters is of an as yet
9032 incomplete type. */
9033
9034static void
9035compute_delayed_physnames (struct dwarf2_cu *cu)
9036{
3693fdb3 9037 /* Only C++ delays computing physnames. */
c89b44cd 9038 if (cu->method_list.empty ())
3693fdb3
PA
9039 return;
9040 gdb_assert (cu->language == language_cplus);
9041
52941706 9042 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9043 {
1d06ead6 9044 const char *physname;
3da10d80 9045 struct fn_fieldlist *fn_flp
c89b44cd
TT
9046 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9047 physname = dwarf2_physname (mi.name, mi.die, cu);
9048 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9049 = physname ? physname : "";
3693fdb3
PA
9050
9051 /* Since there's no tag to indicate whether a method is a
9052 const/volatile overload, extract that information out of the
9053 demangled name. */
9054 if (physname != NULL)
9055 {
9056 size_t len = strlen (physname);
9057
9058 while (1)
9059 {
9060 if (physname[len] == ')') /* shortcut */
9061 break;
9062 else if (check_modifier (physname, len, " const"))
c89b44cd 9063 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9064 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9065 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9066 else
9067 break;
9068 }
9069 }
3da10d80 9070 }
c89b44cd
TT
9071
9072 /* The list is no longer needed. */
9073 cu->method_list.clear ();
3da10d80
KS
9074}
9075
a766d390
DE
9076/* Go objects should be embedded in a DW_TAG_module DIE,
9077 and it's not clear if/how imported objects will appear.
9078 To keep Go support simple until that's worked out,
9079 go back through what we've read and create something usable.
9080 We could do this while processing each DIE, and feels kinda cleaner,
9081 but that way is more invasive.
9082 This is to, for example, allow the user to type "p var" or "b main"
9083 without having to specify the package name, and allow lookups
9084 of module.object to work in contexts that use the expression
9085 parser. */
9086
9087static void
9088fixup_go_packaging (struct dwarf2_cu *cu)
9089{
421d1616 9090 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9091 struct pending *list;
9092 int i;
9093
c24bdb02 9094 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9095 list != NULL;
9096 list = list->next)
a766d390
DE
9097 {
9098 for (i = 0; i < list->nsyms; ++i)
9099 {
9100 struct symbol *sym = list->symbol[i];
9101
c1b5c1eb 9102 if (sym->language () == language_go
a766d390
DE
9103 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9104 {
421d1616
TT
9105 gdb::unique_xmalloc_ptr<char> this_package_name
9106 (go_symbol_package_name (sym));
a766d390
DE
9107
9108 if (this_package_name == NULL)
9109 continue;
9110 if (package_name == NULL)
421d1616 9111 package_name = std::move (this_package_name);
a766d390
DE
9112 else
9113 {
5e22e966 9114 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9115 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9116 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9117 (symbol_symtab (sym) != NULL
9118 ? symtab_to_filename_for_display
9119 (symbol_symtab (sym))
e3b94546 9120 : objfile_name (objfile)),
421d1616 9121 this_package_name.get (), package_name.get ());
a766d390
DE
9122 }
9123 }
9124 }
9125 }
9126
9127 if (package_name != NULL)
9128 {
5e22e966 9129 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9130 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9131 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9132 saved_package_name);
a766d390
DE
9133 struct symbol *sym;
9134
8c14c3a3 9135 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9136 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9137 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9138 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9139 e.g., "main" finds the "main" module and not C's main(). */
9140 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9141 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9142 SYMBOL_TYPE (sym) = type;
9143
c24bdb02 9144 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9145 }
9146}
9147
c9317f21
TT
9148/* Allocate a fully-qualified name consisting of the two parts on the
9149 obstack. */
9150
9151static const char *
9152rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9153{
9154 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9155}
9156
9c6a1327
TT
9157/* A helper that allocates a variant part to attach to a Rust enum
9158 type. OBSTACK is where the results should be allocated. TYPE is
9159 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9160 discriminant. It must be the index of one of the fields of TYPE,
9161 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9162 DEFAULT_INDEX is the index of the default field; or -1 if there is
9163 no default. RANGES is indexed by "effective" field number (the
9164 field index, but omitting the discriminant and default fields) and
9165 must hold the discriminant values used by the variants. Note that
9166 RANGES must have a lifetime at least as long as OBSTACK -- either
9167 already allocated on it, or static. */
c9317f21 9168
9c6a1327
TT
9169static void
9170alloc_rust_variant (struct obstack *obstack, struct type *type,
9171 int discriminant_index, int default_index,
9172 gdb::array_view<discriminant_range> ranges)
9173{
57d02173
TT
9174 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9175 gdb_assert (discriminant_index == -1
9176 || (discriminant_index >= 0
9177 && discriminant_index < type->num_fields ()));
c9317f21 9178 gdb_assert (default_index == -1
1f704f76 9179 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9180
9c6a1327 9181 /* We have one variant for each non-discriminant field. */
57d02173
TT
9182 int n_variants = type->num_fields ();
9183 if (discriminant_index != -1)
9184 --n_variants;
c9317f21 9185
9c6a1327
TT
9186 variant *variants = new (obstack) variant[n_variants];
9187 int var_idx = 0;
9188 int range_idx = 0;
1f704f76 9189 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9190 {
9191 if (i == discriminant_index)
9192 continue;
c9317f21 9193
9c6a1327
TT
9194 variants[var_idx].first_field = i;
9195 variants[var_idx].last_field = i + 1;
9196
9197 /* The default field does not need a range, but other fields do.
9198 We skipped the discriminant above. */
9199 if (i != default_index)
9200 {
9201 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9202 ++range_idx;
9203 }
c9317f21 9204
9c6a1327
TT
9205 ++var_idx;
9206 }
9207
9208 gdb_assert (range_idx == ranges.size ());
9209 gdb_assert (var_idx == n_variants);
9210
9211 variant_part *part = new (obstack) variant_part;
9212 part->discriminant_index = discriminant_index;
57d02173
TT
9213 /* If there is no discriminant, then whether it is signed is of no
9214 consequence. */
9215 part->is_unsigned
9216 = (discriminant_index == -1
9217 ? false
c6d940a9 9218 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9219 part->variants = gdb::array_view<variant> (variants, n_variants);
9220
9221 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9222 gdb::array_view<variant_part> *prop_value
9223 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9224
9c6a1327 9225 struct dynamic_prop prop;
8c2e4e06 9226 prop.set_variant_parts (prop_value);
9c6a1327 9227
5c54719c 9228 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9229}
9230
9231/* Some versions of rustc emitted enums in an unusual way.
9232
9233 Ordinary enums were emitted as unions. The first element of each
9234 structure in the union was named "RUST$ENUM$DISR". This element
9235 held the discriminant.
9236
9237 These versions of Rust also implemented the "non-zero"
9238 optimization. When the enum had two values, and one is empty and
9239 the other holds a pointer that cannot be zero, the pointer is used
9240 as the discriminant, with a zero value meaning the empty variant.
9241 Here, the union's first member is of the form
9242 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9243 where the fieldnos are the indices of the fields that should be
9244 traversed in order to find the field (which may be several fields deep)
9245 and the variantname is the name of the variant of the case when the
9246 field is zero.
9247
9248 This function recognizes whether TYPE is of one of these forms,
9249 and, if so, smashes it to be a variant type. */
9250
9251static void
9252quirk_rust_enum (struct type *type, struct objfile *objfile)
9253{
78134374 9254 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9255
9256 /* We don't need to deal with empty enums. */
1f704f76 9257 if (type->num_fields () == 0)
c9317f21
TT
9258 return;
9259
9260#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9261 if (type->num_fields () == 1
c9317f21
TT
9262 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9263 {
9264 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9265
9266 /* Decode the field name to find the offset of the
9267 discriminant. */
9268 ULONGEST bit_offset = 0;
940da03e 9269 struct type *field_type = type->field (0).type ();
c9317f21
TT
9270 while (name[0] >= '0' && name[0] <= '9')
9271 {
9272 char *tail;
9273 unsigned long index = strtoul (name, &tail, 10);
9274 name = tail;
9275 if (*name != '$'
1f704f76 9276 || index >= field_type->num_fields ()
c9317f21
TT
9277 || (TYPE_FIELD_LOC_KIND (field_type, index)
9278 != FIELD_LOC_KIND_BITPOS))
9279 {
b98664d3 9280 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9281 "[in module %s]"),
9282 TYPE_FIELD_NAME (type, 0),
9283 objfile_name (objfile));
9284 return;
9285 }
9286 ++name;
9287
9288 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9289 field_type = field_type->field (index).type ();
c9317f21
TT
9290 }
9291
9c6a1327
TT
9292 /* Smash this type to be a structure type. We have to do this
9293 because the type has already been recorded. */
67607e24 9294 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9295 type->set_num_fields (3);
9c6a1327 9296 /* Save the field we care about. */
ceacbf6e 9297 struct field saved_field = type->field (0);
3cabb6b0
SM
9298 type->set_fields
9299 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9300
9c6a1327 9301 /* Put the discriminant at index 0. */
5d14b6e5 9302 type->field (0).set_type (field_type);
9c6a1327
TT
9303 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9304 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9305 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9306
9307 /* The order of fields doesn't really matter, so put the real
9308 field at index 1 and the data-less field at index 2. */
ceacbf6e 9309 type->field (1) = saved_field;
9c6a1327 9310 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9311 = rust_last_path_segment (type->field (1).type ()->name ());
9312 type->field (1).type ()->set_name
7d93a1e0 9313 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9314 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9315
9316 const char *dataless_name
7d93a1e0 9317 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9318 name);
9319 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9320 dataless_name);
5d14b6e5 9321 type->field (2).set_type (dataless_type);
c9317f21
TT
9322 /* NAME points into the original discriminant name, which
9323 already has the correct lifetime. */
9c6a1327 9324 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9325 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9326
9c6a1327
TT
9327 /* Indicate that this is a variant type. */
9328 static discriminant_range ranges[1] = { { 0, 0 } };
9329 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9330 }
77c2dba3
TT
9331 /* A union with a single anonymous field is probably an old-style
9332 univariant enum. */
1f704f76 9333 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9334 {
c9317f21
TT
9335 /* Smash this type to be a structure type. We have to do this
9336 because the type has already been recorded. */
67607e24 9337 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9338
940da03e 9339 struct type *field_type = type->field (0).type ();
c9317f21 9340 const char *variant_name
7d93a1e0 9341 = rust_last_path_segment (field_type->name ());
9c6a1327 9342 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9343 field_type->set_name
9344 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9345 type->name (), variant_name));
57d02173
TT
9346
9347 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9348 }
9349 else
9350 {
9351 struct type *disr_type = nullptr;
1f704f76 9352 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9353 {
940da03e 9354 disr_type = type->field (i).type ();
c9317f21 9355
78134374 9356 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9357 {
9358 /* All fields of a true enum will be structs. */
9359 return;
9360 }
1f704f76 9361 else if (disr_type->num_fields () == 0)
c9317f21
TT
9362 {
9363 /* Could be data-less variant, so keep going. */
a037790e 9364 disr_type = nullptr;
c9317f21
TT
9365 }
9366 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9367 "RUST$ENUM$DISR") != 0)
9368 {
9369 /* Not a Rust enum. */
9370 return;
9371 }
9372 else
9373 {
9374 /* Found one. */
9375 break;
9376 }
9377 }
9378
9379 /* If we got here without a discriminant, then it's probably
9380 just a union. */
9381 if (disr_type == nullptr)
9382 return;
9383
9384 /* Smash this type to be a structure type. We have to do this
9385 because the type has already been recorded. */
67607e24 9386 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9387
9c6a1327 9388 /* Make space for the discriminant field. */
ceacbf6e 9389 struct field *disr_field = &disr_type->field (0);
9c6a1327 9390 field *new_fields
1f704f76 9391 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9392 * sizeof (struct field)));
80fc5e77 9393 memcpy (new_fields + 1, type->fields (),
1f704f76 9394 type->num_fields () * sizeof (struct field));
3cabb6b0 9395 type->set_fields (new_fields);
1f704f76 9396 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9397
9398 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9399 type->field (0) = *disr_field;
9c6a1327
TT
9400 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9401 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9402
9403 /* We need a way to find the correct discriminant given a
9404 variant name. For convenience we build a map here. */
b6cdac4b 9405 struct type *enum_type = disr_field->type ();
c9317f21 9406 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9407 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9408 {
9409 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9410 {
9411 const char *name
9412 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9413 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9414 }
9415 }
9416
1f704f76 9417 int n_fields = type->num_fields ();
9c6a1327
TT
9418 /* We don't need a range entry for the discriminant, but we do
9419 need one for every other field, as there is no default
9420 variant. */
9421 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9422 discriminant_range,
9423 n_fields - 1);
c9317f21
TT
9424 /* Skip the discriminant here. */
9425 for (int i = 1; i < n_fields; ++i)
9426 {
9427 /* Find the final word in the name of this variant's type.
9428 That name can be used to look up the correct
9429 discriminant. */
9430 const char *variant_name
940da03e 9431 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9432
9433 auto iter = discriminant_map.find (variant_name);
9434 if (iter != discriminant_map.end ())
9c6a1327 9435 {
57d02173
TT
9436 ranges[i - 1].low = iter->second;
9437 ranges[i - 1].high = iter->second;
9c6a1327 9438 }
c9317f21 9439
57d02173
TT
9440 /* In Rust, each element should have the size of the
9441 enclosing enum. */
9442 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9443
bedda9ac 9444 /* Remove the discriminant field, if it exists. */
940da03e 9445 struct type *sub_type = type->field (i).type ();
1f704f76 9446 if (sub_type->num_fields () > 0)
bedda9ac 9447 {
5e33d5f4 9448 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9449 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9450 }
9c6a1327 9451 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9452 sub_type->set_name
9453 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9454 type->name (), variant_name));
c9317f21 9455 }
9c6a1327
TT
9456
9457 /* Indicate that this is a variant type. */
a1520ad8 9458 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9459 gdb::array_view<discriminant_range> (ranges,
9460 n_fields - 1));
c9317f21
TT
9461 }
9462}
9463
9464/* Rewrite some Rust unions to be structures with variants parts. */
9465
9466static void
9467rust_union_quirks (struct dwarf2_cu *cu)
9468{
9469 gdb_assert (cu->language == language_rust);
52941706 9470 for (type *type_ : cu->rust_unions)
5e22e966 9471 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9472 /* We don't need this any more. */
9473 cu->rust_unions.clear ();
c9317f21
TT
9474}
9475
8adb8487
TT
9476/* See read.h. */
9477
9478type_unit_group_unshareable *
9479dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9480{
9481 auto iter = this->m_type_units.find (tu_group);
9482 if (iter != this->m_type_units.end ())
9483 return iter->second.get ();
9484
9485 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9486 type_unit_group_unshareable *result = uniq.get ();
9487 this->m_type_units[tu_group] = std::move (uniq);
9488 return result;
9489}
9490
e286671b
TT
9491struct type *
9492dwarf2_per_objfile::get_type_for_signatured_type
9493 (signatured_type *sig_type) const
9494{
9495 auto iter = this->m_type_map.find (sig_type);
9496 if (iter == this->m_type_map.end ())
9497 return nullptr;
9498
9499 return iter->second;
9500}
9501
9502void dwarf2_per_objfile::set_type_for_signatured_type
9503 (signatured_type *sig_type, struct type *type)
9504{
9505 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9506
9507 this->m_type_map[sig_type] = type;
9508}
9509
95554aad
TT
9510/* A helper function for computing the list of all symbol tables
9511 included by PER_CU. */
9512
9513static void
4c39bc03 9514recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9515 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9516 dwarf2_per_cu_data *per_cu,
9517 dwarf2_per_objfile *per_objfile,
43f3e411 9518 struct compunit_symtab *immediate_parent)
95554aad 9519{
af758d11 9520 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9521 if (*slot != NULL)
9522 {
9523 /* This inclusion and its children have been processed. */
9524 return;
9525 }
9526
9527 *slot = per_cu;
af758d11 9528
95554aad 9529 /* Only add a CU if it has a symbol table. */
43182c09 9530 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9531 if (cust != NULL)
ec94af83
DE
9532 {
9533 /* If this is a type unit only add its symbol table if we haven't
9534 seen it yet (type unit per_cu's can share symtabs). */
9535 if (per_cu->is_debug_types)
9536 {
43f3e411 9537 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9538 if (*slot == NULL)
9539 {
43f3e411 9540 *slot = cust;
4c39bc03 9541 result->push_back (cust);
43f3e411
DE
9542 if (cust->user == NULL)
9543 cust->user = immediate_parent;
ec94af83
DE
9544 }
9545 }
9546 else
f9125b6c 9547 {
4c39bc03 9548 result->push_back (cust);
43f3e411
DE
9549 if (cust->user == NULL)
9550 cust->user = immediate_parent;
f9125b6c 9551 }
ec94af83 9552 }
95554aad 9553
ae640021
AB
9554 if (!per_cu->imported_symtabs_empty ())
9555 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9556 {
9557 recursively_compute_inclusions (result, all_children,
43182c09
SM
9558 all_type_symtabs, ptr, per_objfile,
9559 cust);
ae640021 9560 }
95554aad
TT
9561}
9562
43f3e411 9563/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9564 PER_CU. */
9565
9566static void
43182c09
SM
9567compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9568 dwarf2_per_objfile *per_objfile)
95554aad 9569{
f4dc4d17
DE
9570 gdb_assert (! per_cu->is_debug_types);
9571
ae640021 9572 if (!per_cu->imported_symtabs_empty ())
95554aad 9573 {
ae640021 9574 int len;
4c39bc03 9575 std::vector<compunit_symtab *> result_symtabs;
43182c09 9576 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9577
9578 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9579 if (cust == NULL)
95554aad
TT
9580 return;
9581
280a9412
TT
9582 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9583 htab_eq_pointer,
9584 NULL, xcalloc, xfree));
9585 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9586 htab_eq_pointer,
9587 NULL, xcalloc, xfree));
95554aad 9588
ae640021 9589 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9590 {
280a9412
TT
9591 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9592 all_type_symtabs.get (), ptr,
9593 per_objfile, cust);
ec94af83 9594 }
95554aad 9595
ec94af83 9596 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9597 len = result_symtabs.size ();
43f3e411 9598 cust->includes
f6e649dd 9599 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9600 struct compunit_symtab *, len + 1);
4c39bc03
TT
9601 memcpy (cust->includes, result_symtabs.data (),
9602 len * sizeof (compunit_symtab *));
43f3e411 9603 cust->includes[len] = NULL;
95554aad
TT
9604 }
9605}
9606
9607/* Compute the 'includes' field for the symtabs of all the CUs we just
9608 read. */
9609
9610static void
976ca316 9611process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9612{
976ca316 9613 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9614 {
9615 if (! iter->is_debug_types)
976ca316 9616 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9617 }
95554aad 9618
976ca316 9619 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9620}
9621
8fc0b21d 9622/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9623 already been loaded into memory. */
9624
9625static void
8fc0b21d 9626process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9627{
976ca316
SM
9628 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9629 struct objfile *objfile = per_objfile->objfile;
08feed99 9630 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9631 CORE_ADDR lowpc, highpc;
43f3e411 9632 struct compunit_symtab *cust;
10b3939b 9633 CORE_ADDR baseaddr;
4359dff1 9634 struct block *static_block;
3e29f34a 9635 CORE_ADDR addr;
10b3939b 9636
b3b3bada 9637 baseaddr = objfile->text_section_offset ();
10b3939b 9638
c89b44cd
TT
9639 /* Clear the list here in case something was left over. */
9640 cu->method_list.clear ();
10b3939b 9641
95554aad
TT
9642 cu->language = pretend_language;
9643 cu->language_defn = language_def (cu->language);
9644
1c47ec3e
TV
9645 dwarf2_find_base_address (cu->dies, cu);
9646
702cf3f5
AB
9647 /* Before we start reading the top-level DIE, ensure it has a valid tag
9648 type. */
9649 switch (cu->dies->tag)
9650 {
9651 case DW_TAG_compile_unit:
9652 case DW_TAG_partial_unit:
9653 case DW_TAG_type_unit:
9654 break;
9655 default:
9656 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9657 dwarf_tag_name (cu->dies->tag),
9658 sect_offset_str (cu->per_cu->sect_off),
9659 objfile_name (per_objfile->objfile));
9660 }
9661
c906108c 9662 /* Do line number decoding in read_file_scope () */
10b3939b 9663 process_die (cu->dies, cu);
c906108c 9664
a766d390
DE
9665 /* For now fudge the Go package. */
9666 if (cu->language == language_go)
9667 fixup_go_packaging (cu);
9668
5f48f8f3 9669 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9670 should be complete, and it should now be safe to compute all of the
9671 physnames. */
9672 compute_delayed_physnames (cu);
3da10d80 9673
c9317f21
TT
9674 if (cu->language == language_rust)
9675 rust_union_quirks (cu);
9676
fae299cd
DC
9677 /* Some compilers don't define a DW_AT_high_pc attribute for the
9678 compilation unit. If the DW_AT_high_pc is missing, synthesize
9679 it, by scanning the DIE's below the compilation unit. */
10b3939b 9680 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9681
3e29f34a 9682 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9683 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9684
9685 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9686 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9687 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9688 addrmap to help ensure it has an accurate map of pc values belonging to
9689 this comp unit. */
9690 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9691
c24bdb02 9692 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9693 SECT_OFF_TEXT (objfile),
9694 0);
c906108c 9695
43f3e411 9696 if (cust != NULL)
c906108c 9697 {
df15bd07 9698 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9699
8be455d7
JK
9700 /* Set symtab language to language from DW_AT_language. If the
9701 compilation is from a C file generated by language preprocessors, do
9702 not set the language if it was already deduced by start_subfile. */
43f3e411 9703 if (!(cu->language == language_c
40e3ad0e 9704 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9705 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9706
9707 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9708 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9709 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9710 there were bugs in prologue debug info, fixed later in GCC-4.5
9711 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9712
9713 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9714 needed, it would be wrong due to missing DW_AT_producer there.
9715
9716 Still one can confuse GDB by using non-standard GCC compilation
9717 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9718 */
ab260dad 9719 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9720 cust->locations_valid = 1;
e0d00bc7
JK
9721
9722 if (gcc_4_minor >= 5)
43f3e411 9723 cust->epilogue_unwind_valid = 1;
96408a79 9724
43f3e411 9725 cust->call_site_htab = cu->call_site_htab;
c906108c 9726 }
9291a0cd 9727
976ca316 9728 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9729
95554aad 9730 /* Push it for inclusion processing later. */
976ca316 9731 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9732
9733 /* Not needed any more. */
c24bdb02 9734 cu->reset_builder ();
f4dc4d17 9735}
45cfd468 9736
8fc0b21d 9737/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9738 already been loaded into memory. */
9739
9740static void
8fc0b21d 9741process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9742 enum language pretend_language)
9743{
976ca316
SM
9744 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9745 struct objfile *objfile = per_objfile->objfile;
43f3e411 9746 struct compunit_symtab *cust;
0186c6a7
DE
9747 struct signatured_type *sig_type;
9748
8fc0b21d
SM
9749 gdb_assert (cu->per_cu->is_debug_types);
9750 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9751
c89b44cd
TT
9752 /* Clear the list here in case something was left over. */
9753 cu->method_list.clear ();
f4dc4d17 9754
f4dc4d17
DE
9755 cu->language = pretend_language;
9756 cu->language_defn = language_def (cu->language);
9757
9758 /* The symbol tables are set up in read_type_unit_scope. */
9759 process_die (cu->dies, cu);
9760
9761 /* For now fudge the Go package. */
9762 if (cu->language == language_go)
9763 fixup_go_packaging (cu);
9764
5f48f8f3 9765 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9766 should be complete, and it should now be safe to compute all of the
9767 physnames. */
9768 compute_delayed_physnames (cu);
f4dc4d17 9769
c9317f21
TT
9770 if (cu->language == language_rust)
9771 rust_union_quirks (cu);
9772
f4dc4d17
DE
9773 /* TUs share symbol tables.
9774 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9775 of it with end_expandable_symtab. Otherwise, complete the addition of
9776 this TU's symbols to the existing symtab. */
8adb8487 9777 type_unit_group_unshareable *tug_unshare =
976ca316 9778 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9779 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9780 {
c24bdb02
KS
9781 buildsym_compunit *builder = cu->get_builder ();
9782 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9783 tug_unshare->compunit_symtab = cust;
f4dc4d17 9784
43f3e411 9785 if (cust != NULL)
f4dc4d17
DE
9786 {
9787 /* Set symtab language to language from DW_AT_language. If the
9788 compilation is from a C file generated by language preprocessors,
9789 do not set the language if it was already deduced by
9790 start_subfile. */
43f3e411
DE
9791 if (!(cu->language == language_c
9792 && COMPUNIT_FILETABS (cust)->language != language_c))
9793 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9794 }
9795 }
9796 else
9797 {
c24bdb02 9798 cu->get_builder ()->augment_type_symtab ();
8adb8487 9799 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9800 }
9801
976ca316 9802 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9803
9804 /* Not needed any more. */
c24bdb02 9805 cu->reset_builder ();
c906108c
SS
9806}
9807
95554aad
TT
9808/* Process an imported unit DIE. */
9809
9810static void
9811process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9812{
9813 struct attribute *attr;
9814
f4dc4d17
DE
9815 /* For now we don't handle imported units in type units. */
9816 if (cu->per_cu->is_debug_types)
9817 {
9818 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9819 " supported in type units [in module %s]"),
5e22e966 9820 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9821 }
9822
95554aad
TT
9823 attr = dwarf2_attr (die, DW_AT_import, cu);
9824 if (attr != NULL)
9825 {
0826b30a 9826 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9827 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9828 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9829 dwarf2_per_cu_data *per_cu
ab432490 9830 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9831
58990295
TV
9832 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9833 into another compilation unit, at root level. Regard this as a hint,
9834 and ignore it. */
9835 if (die->parent && die->parent->parent == NULL
9836 && per_cu->unit_type == DW_UT_compile
9837 && per_cu->lang == language_cplus)
9838 return;
9839
69d751e3 9840 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9841 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
9842 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9843 false, cu->language);
95554aad 9844
ae640021 9845 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9846 }
9847}
9848
4c8aa72d
PA
9849/* RAII object that represents a process_die scope: i.e.,
9850 starts/finishes processing a DIE. */
9851class process_die_scope
adde2bff 9852{
4c8aa72d
PA
9853public:
9854 process_die_scope (die_info *die, dwarf2_cu *cu)
9855 : m_die (die), m_cu (cu)
9856 {
9857 /* We should only be processing DIEs not already in process. */
9858 gdb_assert (!m_die->in_process);
9859 m_die->in_process = true;
9860 }
8c3cb9fa 9861
4c8aa72d
PA
9862 ~process_die_scope ()
9863 {
9864 m_die->in_process = false;
9865
9866 /* If we're done processing the DIE for the CU that owns the line
9867 header, we don't need the line header anymore. */
9868 if (m_cu->line_header_die_owner == m_die)
9869 {
9870 delete m_cu->line_header;
9871 m_cu->line_header = NULL;
9872 m_cu->line_header_die_owner = NULL;
9873 }
9874 }
9875
9876private:
9877 die_info *m_die;
9878 dwarf2_cu *m_cu;
9879};
adde2bff 9880
c906108c
SS
9881/* Process a die and its children. */
9882
9883static void
e7c27a73 9884process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9885{
4c8aa72d 9886 process_die_scope scope (die, cu);
adde2bff 9887
c906108c
SS
9888 switch (die->tag)
9889 {
9890 case DW_TAG_padding:
9891 break;
9892 case DW_TAG_compile_unit:
95554aad 9893 case DW_TAG_partial_unit:
e7c27a73 9894 read_file_scope (die, cu);
c906108c 9895 break;
348e048f
DE
9896 case DW_TAG_type_unit:
9897 read_type_unit_scope (die, cu);
9898 break;
c906108c 9899 case DW_TAG_subprogram:
0a4b0913
AB
9900 /* Nested subprograms in Fortran get a prefix. */
9901 if (cu->language == language_fortran
9902 && die->parent != NULL
9903 && die->parent->tag == DW_TAG_subprogram)
9904 cu->processing_has_namespace_info = true;
9905 /* Fall through. */
c906108c 9906 case DW_TAG_inlined_subroutine:
edb3359d 9907 read_func_scope (die, cu);
c906108c
SS
9908 break;
9909 case DW_TAG_lexical_block:
14898363
L
9910 case DW_TAG_try_block:
9911 case DW_TAG_catch_block:
e7c27a73 9912 read_lexical_block_scope (die, cu);
c906108c 9913 break;
216f72a1 9914 case DW_TAG_call_site:
96408a79
SA
9915 case DW_TAG_GNU_call_site:
9916 read_call_site_scope (die, cu);
9917 break;
c906108c 9918 case DW_TAG_class_type:
680b30c7 9919 case DW_TAG_interface_type:
c906108c
SS
9920 case DW_TAG_structure_type:
9921 case DW_TAG_union_type:
134d01f1 9922 process_structure_scope (die, cu);
c906108c
SS
9923 break;
9924 case DW_TAG_enumeration_type:
134d01f1 9925 process_enumeration_scope (die, cu);
c906108c 9926 break;
134d01f1 9927
f792889a
DJ
9928 /* These dies have a type, but processing them does not create
9929 a symbol or recurse to process the children. Therefore we can
9930 read them on-demand through read_type_die. */
c906108c 9931 case DW_TAG_subroutine_type:
72019c9c 9932 case DW_TAG_set_type:
c906108c 9933 case DW_TAG_pointer_type:
c906108c 9934 case DW_TAG_ptr_to_member_type:
c906108c 9935 case DW_TAG_reference_type:
4297a3f0 9936 case DW_TAG_rvalue_reference_type:
c906108c 9937 case DW_TAG_string_type:
c906108c 9938 break;
134d01f1 9939
d8f62e84
TT
9940 case DW_TAG_array_type:
9941 /* We only need to handle this case for Ada -- in other
9942 languages, it's normal for the compiler to emit a typedef
9943 instead. */
9944 if (cu->language != language_ada)
9945 break;
9946 /* FALLTHROUGH */
c906108c 9947 case DW_TAG_base_type:
a02abb62 9948 case DW_TAG_subrange_type:
cb249c71 9949 case DW_TAG_typedef:
134d01f1 9950 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 9951 DW_AT_name. */
f792889a 9952 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9953 break;
c906108c 9954 case DW_TAG_common_block:
e7c27a73 9955 read_common_block (die, cu);
c906108c
SS
9956 break;
9957 case DW_TAG_common_inclusion:
9958 break;
d9fa45fe 9959 case DW_TAG_namespace:
9068261f 9960 cu->processing_has_namespace_info = true;
e7c27a73 9961 read_namespace (die, cu);
d9fa45fe 9962 break;
5d7cb8df 9963 case DW_TAG_module:
9068261f 9964 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9965 read_module (die, cu);
9966 break;
d9fa45fe 9967 case DW_TAG_imported_declaration:
9068261f 9968 cu->processing_has_namespace_info = true;
74921315
KS
9969 if (read_namespace_alias (die, cu))
9970 break;
86a73007
TT
9971 /* The declaration is not a global namespace alias. */
9972 /* Fall through. */
d9fa45fe 9973 case DW_TAG_imported_module:
9068261f 9974 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9975 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9976 || cu->language != language_fortran))
b98664d3 9977 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9978 dwarf_tag_name (die->tag));
9979 read_import_statement (die, cu);
d9fa45fe 9980 break;
95554aad
TT
9981
9982 case DW_TAG_imported_unit:
9983 process_imported_unit_die (die, cu);
9984 break;
9985
71a3c369
TT
9986 case DW_TAG_variable:
9987 read_variable (die, cu);
9988 break;
9989
c906108c 9990 default:
e7c27a73 9991 new_symbol (die, NULL, cu);
c906108c
SS
9992 break;
9993 }
9994}
ca69b9e6
DE
9995\f
9996/* DWARF name computation. */
c906108c 9997
94af9270
KS
9998/* A helper function for dwarf2_compute_name which determines whether DIE
9999 needs to have the name of the scope prepended to the name listed in the
10000 die. */
10001
10002static int
10003die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10004{
1c809c68
TT
10005 struct attribute *attr;
10006
94af9270
KS
10007 switch (die->tag)
10008 {
10009 case DW_TAG_namespace:
10010 case DW_TAG_typedef:
10011 case DW_TAG_class_type:
10012 case DW_TAG_interface_type:
10013 case DW_TAG_structure_type:
10014 case DW_TAG_union_type:
10015 case DW_TAG_enumeration_type:
10016 case DW_TAG_enumerator:
10017 case DW_TAG_subprogram:
08a76f8a 10018 case DW_TAG_inlined_subroutine:
94af9270 10019 case DW_TAG_member:
74921315 10020 case DW_TAG_imported_declaration:
94af9270
KS
10021 return 1;
10022
10023 case DW_TAG_variable:
c2b0a229 10024 case DW_TAG_constant:
94af9270
KS
10025 /* We only need to prefix "globally" visible variables. These include
10026 any variable marked with DW_AT_external or any variable that
10027 lives in a namespace. [Variables in anonymous namespaces
10028 require prefixing, but they are not DW_AT_external.] */
10029
10030 if (dwarf2_attr (die, DW_AT_specification, cu))
10031 {
10032 struct dwarf2_cu *spec_cu = cu;
9a619af0 10033
94af9270
KS
10034 return die_needs_namespace (die_specification (die, &spec_cu),
10035 spec_cu);
10036 }
10037
1c809c68 10038 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10039 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10040 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10041 return 0;
10042 /* A variable in a lexical block of some kind does not need a
10043 namespace, even though in C++ such variables may be external
10044 and have a mangled name. */
10045 if (die->parent->tag == DW_TAG_lexical_block
10046 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10047 || die->parent->tag == DW_TAG_catch_block
10048 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10049 return 0;
10050 return 1;
94af9270
KS
10051
10052 default:
10053 return 0;
10054 }
10055}
10056
73b9be8b
KS
10057/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10058 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10059 defined for the given DIE. */
10060
10061static struct attribute *
10062dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10063{
10064 struct attribute *attr;
10065
10066 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10067 if (attr == NULL)
10068 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10069
10070 return attr;
10071}
10072
10073/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10074 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10075 defined for the given DIE. */
10076
10077static const char *
10078dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10079{
10080 const char *linkage_name;
10081
10082 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10083 if (linkage_name == NULL)
10084 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10085
787de330
TT
10086 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10087 See https://github.com/rust-lang/rust/issues/32925. */
10088 if (cu->language == language_rust && linkage_name != NULL
10089 && strchr (linkage_name, '{') != NULL)
10090 linkage_name = NULL;
10091
73b9be8b
KS
10092 return linkage_name;
10093}
10094
94af9270 10095/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10096 compute the physname for the object, which include a method's:
9c37b5ae 10097 - formal parameters (C++),
a766d390 10098 - receiver type (Go),
a766d390
DE
10099
10100 The term "physname" is a bit confusing.
10101 For C++, for example, it is the demangled name.
10102 For Go, for example, it's the mangled name.
94af9270 10103
af6b7be1
JB
10104 For Ada, return the DIE's linkage name rather than the fully qualified
10105 name. PHYSNAME is ignored..
10106
5989a64e 10107 The result is allocated on the objfile->per_bfd's obstack and
45940949 10108 canonicalized. */
94af9270
KS
10109
10110static const char *
15d034d0
TT
10111dwarf2_compute_name (const char *name,
10112 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10113 int physname)
10114{
5e22e966 10115 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10116
94af9270
KS
10117 if (name == NULL)
10118 name = dwarf2_name (die, cu);
10119
2ee7123e
DE
10120 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10121 but otherwise compute it by typename_concat inside GDB.
10122 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10123 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10124 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10125 will set the demangled name to the result of dwarf2_full_name, and it is
10126 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10127 if (cu->language == language_ada
10128 || (cu->language == language_fortran && physname))
10129 {
10130 /* For Ada unit, we prefer the linkage name over the name, as
10131 the former contains the exported name, which the user expects
10132 to be able to reference. Ideally, we want the user to be able
10133 to reference this entity using either natural or linkage name,
10134 but we haven't started looking at this enhancement yet. */
73b9be8b 10135 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10136
2ee7123e
DE
10137 if (linkage_name != NULL)
10138 return linkage_name;
f55ee35c
JK
10139 }
10140
94af9270
KS
10141 /* These are the only languages we know how to qualify names in. */
10142 if (name != NULL
9c37b5ae 10143 && (cu->language == language_cplus
c44af4eb
TT
10144 || cu->language == language_fortran || cu->language == language_d
10145 || cu->language == language_rust))
94af9270
KS
10146 {
10147 if (die_needs_namespace (die, cu))
10148 {
0d5cff50 10149 const char *prefix;
34a68019 10150 const char *canonical_name = NULL;
94af9270 10151
d7e74731
PA
10152 string_file buf;
10153
94af9270 10154 prefix = determine_prefix (die, cu);
94af9270
KS
10155 if (*prefix != '\0')
10156 {
43816ebc
TT
10157 gdb::unique_xmalloc_ptr<char> prefixed_name
10158 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10159
43816ebc 10160 buf.puts (prefixed_name.get ());
94af9270
KS
10161 }
10162 else
d7e74731 10163 buf.puts (name);
94af9270 10164
98bfdba5
PA
10165 /* Template parameters may be specified in the DIE's DW_AT_name, or
10166 as children with DW_TAG_template_type_param or
10167 DW_TAG_value_type_param. If the latter, add them to the name
10168 here. If the name already has template parameters, then
10169 skip this step; some versions of GCC emit both, and
10170 it is more efficient to use the pre-computed name.
10171
10172 Something to keep in mind about this process: it is very
10173 unlikely, or in some cases downright impossible, to produce
10174 something that will match the mangled name of a function.
10175 If the definition of the function has the same debug info,
10176 we should be able to match up with it anyway. But fallbacks
10177 using the minimal symbol, for instance to find a method
10178 implemented in a stripped copy of libstdc++, will not work.
10179 If we do not have debug info for the definition, we will have to
10180 match them up some other way.
10181
10182 When we do name matching there is a related problem with function
10183 templates; two instantiated function templates are allowed to
10184 differ only by their return types, which we do not add here. */
10185
10186 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10187 {
10188 struct attribute *attr;
10189 struct die_info *child;
10190 int first = 1;
2c75ccb2 10191 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10192
10193 die->building_fullname = 1;
10194
10195 for (child = die->child; child != NULL; child = child->sibling)
10196 {
10197 struct type *type;
12df843f 10198 LONGEST value;
d521ce57 10199 const gdb_byte *bytes;
98bfdba5
PA
10200 struct dwarf2_locexpr_baton *baton;
10201 struct value *v;
10202
10203 if (child->tag != DW_TAG_template_type_param
10204 && child->tag != DW_TAG_template_value_param)
10205 continue;
10206
10207 if (first)
10208 {
d7e74731 10209 buf.puts ("<");
98bfdba5
PA
10210 first = 0;
10211 }
10212 else
d7e74731 10213 buf.puts (", ");
98bfdba5
PA
10214
10215 attr = dwarf2_attr (child, DW_AT_type, cu);
10216 if (attr == NULL)
10217 {
b98664d3 10218 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10219 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10220 continue;
10221 }
10222 type = die_type (child, cu);
10223
10224 if (child->tag == DW_TAG_template_type_param)
10225 {
2c75ccb2
AB
10226 cplus_lang->print_type (type, "", &buf, -1, 0,
10227 &type_print_raw_options);
98bfdba5
PA
10228 continue;
10229 }
10230
10231 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10232 if (attr == NULL)
10233 {
b98664d3 10234 complaint (_("template parameter missing "
3e43a32a 10235 "DW_AT_const_value"));
d7e74731 10236 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10237 continue;
10238 }
10239
10240 dwarf2_const_value_attr (attr, type, name,
10241 &cu->comp_unit_obstack, cu,
10242 &value, &bytes, &baton);
10243
20ce4123 10244 if (type->has_no_signedness ())
98bfdba5
PA
10245 /* GDB prints characters as NUMBER 'CHAR'. If that's
10246 changed, this can use value_print instead. */
2c75ccb2 10247 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10248 else
10249 {
10250 struct value_print_options opts;
10251
10252 if (baton != NULL)
10253 v = dwarf2_evaluate_loc_desc (type, NULL,
10254 baton->data,
10255 baton->size,
9f47c707
SM
10256 baton->per_cu,
10257 baton->per_objfile);
98bfdba5
PA
10258 else if (bytes != NULL)
10259 {
10260 v = allocate_value (type);
10261 memcpy (value_contents_writeable (v), bytes,
10262 TYPE_LENGTH (type));
10263 }
10264 else
10265 v = value_from_longest (type, value);
10266
3e43a32a
MS
10267 /* Specify decimal so that we do not depend on
10268 the radix. */
98bfdba5
PA
10269 get_formatted_print_options (&opts, 'd');
10270 opts.raw = 1;
d7e74731 10271 value_print (v, &buf, &opts);
98bfdba5 10272 release_value (v);
98bfdba5
PA
10273 }
10274 }
10275
10276 die->building_fullname = 0;
10277
10278 if (!first)
10279 {
10280 /* Close the argument list, with a space if necessary
10281 (nested templates). */
d7e74731
PA
10282 if (!buf.empty () && buf.string ().back () == '>')
10283 buf.puts (" >");
98bfdba5 10284 else
d7e74731 10285 buf.puts (">");
98bfdba5
PA
10286 }
10287 }
10288
9c37b5ae 10289 /* For C++ methods, append formal parameter type
94af9270 10290 information, if PHYSNAME. */
6e70227d 10291
94af9270 10292 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10293 && cu->language == language_cplus)
94af9270
KS
10294 {
10295 struct type *type = read_type_die (die, cu);
10296
d7e74731 10297 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10298 &type_print_raw_options);
94af9270 10299
9c37b5ae 10300 if (cu->language == language_cplus)
94af9270 10301 {
60430eff
DJ
10302 /* Assume that an artificial first parameter is
10303 "this", but do not crash if it is not. RealView
10304 marks unnamed (and thus unused) parameters as
10305 artificial; there is no way to differentiate
10306 the two cases. */
1f704f76 10307 if (type->num_fields () > 0
94af9270 10308 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10309 && type->field (0).type ()->code () == TYPE_CODE_PTR
10310 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10311 buf.puts (" const");
94af9270
KS
10312 }
10313 }
10314
d7e74731 10315 const std::string &intermediate_name = buf.string ();
94af9270
KS
10316
10317 if (cu->language == language_cplus)
34a68019 10318 canonical_name
322a8516 10319 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10320 objfile);
34a68019
TT
10321
10322 /* If we only computed INTERMEDIATE_NAME, or if
10323 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10324 intern it. */
322a8516 10325 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10326 name = objfile->intern (intermediate_name);
34a68019
TT
10327 else
10328 name = canonical_name;
94af9270
KS
10329 }
10330 }
10331
10332 return name;
10333}
10334
0114d602
DJ
10335/* Return the fully qualified name of DIE, based on its DW_AT_name.
10336 If scope qualifiers are appropriate they will be added. The result
34a68019 10337 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10338 not have a name. NAME may either be from a previous call to
10339 dwarf2_name or NULL.
10340
9c37b5ae 10341 The output string will be canonicalized (if C++). */
0114d602
DJ
10342
10343static const char *
15d034d0 10344dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10345{
94af9270
KS
10346 return dwarf2_compute_name (name, die, cu, 0);
10347}
0114d602 10348
94af9270
KS
10349/* Construct a physname for the given DIE in CU. NAME may either be
10350 from a previous call to dwarf2_name or NULL. The result will be
10351 allocated on the objfile_objstack or NULL if the DIE does not have a
10352 name.
0114d602 10353
9c37b5ae 10354 The output string will be canonicalized (if C++). */
0114d602 10355
94af9270 10356static const char *
15d034d0 10357dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10358{
5e22e966 10359 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10360 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10361 int need_copy = 1;
10362
10363 /* In this case dwarf2_compute_name is just a shortcut not building anything
10364 on its own. */
10365 if (!die_needs_namespace (die, cu))
10366 return dwarf2_compute_name (name, die, cu, 1);
10367
906bb4c5
TT
10368 if (cu->language != language_rust)
10369 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10370
10371 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10372 has computed. */
791afaa2 10373 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10374 if (mangled != NULL)
900e11f9 10375 {
900e11f9 10376
d3355e4d 10377 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10378 {
10379 /* Do nothing (do not demangle the symbol name). */
10380 }
a766d390
DE
10381 else
10382 {
0eb876f5
JB
10383 /* Use DMGL_RET_DROP for C++ template functions to suppress
10384 their return type. It is easier for GDB users to search
10385 for such functions as `name(params)' than `long name(params)'.
10386 In such case the minimal symbol names do not match the full
10387 symbol names but for template functions there is never a need
10388 to look up their definition from their declaration so
10389 the only disadvantage remains the minimal symbol variant
10390 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10391 demangled.reset (gdb_demangle (mangled,
10392 (DMGL_PARAMS | DMGL_ANSI
10393 | DMGL_RET_DROP)));
a766d390 10394 }
900e11f9 10395 if (demangled)
791afaa2 10396 canon = demangled.get ();
900e11f9
JK
10397 else
10398 {
10399 canon = mangled;
10400 need_copy = 0;
10401 }
10402 }
10403
10404 if (canon == NULL || check_physname)
10405 {
10406 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10407
10408 if (canon != NULL && strcmp (physname, canon) != 0)
10409 {
10410 /* It may not mean a bug in GDB. The compiler could also
10411 compute DW_AT_linkage_name incorrectly. But in such case
10412 GDB would need to be bug-to-bug compatible. */
10413
b98664d3 10414 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10415 "(from linkage <%s>) - DIE at %s [in module %s]"),
10416 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10417 objfile_name (objfile));
900e11f9
JK
10418
10419 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10420 is available here - over computed PHYSNAME. It is safer
10421 against both buggy GDB and buggy compilers. */
10422
10423 retval = canon;
10424 }
10425 else
10426 {
10427 retval = physname;
10428 need_copy = 0;
10429 }
10430 }
10431 else
10432 retval = canon;
10433
10434 if (need_copy)
be1e3d3e 10435 retval = objfile->intern (retval);
900e11f9 10436
900e11f9 10437 return retval;
0114d602
DJ
10438}
10439
74921315
KS
10440/* Inspect DIE in CU for a namespace alias. If one exists, record
10441 a new symbol for it.
10442
10443 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10444
10445static int
10446read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10447{
10448 struct attribute *attr;
10449
10450 /* If the die does not have a name, this is not a namespace
10451 alias. */
10452 attr = dwarf2_attr (die, DW_AT_name, cu);
10453 if (attr != NULL)
10454 {
10455 int num;
10456 struct die_info *d = die;
10457 struct dwarf2_cu *imported_cu = cu;
10458
10459 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10460 keep inspecting DIEs until we hit the underlying import. */
10461#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10462 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10463 {
10464 attr = dwarf2_attr (d, DW_AT_import, cu);
10465 if (attr == NULL)
10466 break;
10467
10468 d = follow_die_ref (d, attr, &imported_cu);
10469 if (d->tag != DW_TAG_imported_declaration)
10470 break;
10471 }
10472
10473 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10474 {
b98664d3 10475 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10476 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10477 return 0;
10478 }
10479
10480 if (attr != NULL)
10481 {
10482 struct type *type;
0826b30a 10483 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10484
aa66c379 10485 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10486 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10487 {
10488 /* This declaration is a global namespace alias. Add
10489 a symbol for it whose type is the aliased namespace. */
10490 new_symbol (die, type, cu);
10491 return 1;
10492 }
10493 }
10494 }
10495
10496 return 0;
10497}
10498
22cee43f 10499/* Return the using directives repository (global or local?) to use in the
804d2729 10500 current context for CU.
22cee43f
PMR
10501
10502 For Ada, imported declarations can materialize renamings, which *may* be
10503 global. However it is impossible (for now?) in DWARF to distinguish
10504 "external" imported declarations and "static" ones. As all imported
10505 declarations seem to be static in all other languages, make them all CU-wide
10506 global only in Ada. */
10507
10508static struct using_direct **
804d2729 10509using_directives (struct dwarf2_cu *cu)
22cee43f 10510{
c24bdb02
KS
10511 if (cu->language == language_ada
10512 && cu->get_builder ()->outermost_context_p ())
10513 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10514 else
c24bdb02 10515 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10516}
10517
27aa8d6a
SW
10518/* Read the import statement specified by the given die and record it. */
10519
10520static void
10521read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10522{
5e22e966 10523 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10524 struct attribute *import_attr;
32019081 10525 struct die_info *imported_die, *child_die;
de4affc9 10526 struct dwarf2_cu *imported_cu;
27aa8d6a 10527 const char *imported_name;
794684b6 10528 const char *imported_name_prefix;
13387711
SW
10529 const char *canonical_name;
10530 const char *import_alias;
10531 const char *imported_declaration = NULL;
794684b6 10532 const char *import_prefix;
eb1e02fd 10533 std::vector<const char *> excludes;
13387711 10534
27aa8d6a
SW
10535 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10536 if (import_attr == NULL)
10537 {
b98664d3 10538 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10539 dwarf_tag_name (die->tag));
10540 return;
10541 }
10542
de4affc9
CC
10543 imported_cu = cu;
10544 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10545 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10546 if (imported_name == NULL)
10547 {
10548 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10549
dda83cd7
SM
10550 The import in the following code:
10551 namespace A
10552 {
10553 typedef int B;
10554 }
10555
10556 int main ()
10557 {
10558 using A::B;
10559 B b;
10560 return b;
10561 }
10562
10563 ...
10564 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10565 <52> DW_AT_decl_file : 1
10566 <53> DW_AT_decl_line : 6
10567 <54> DW_AT_import : <0x75>
10568 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10569 <59> DW_AT_name : B
10570 <5b> DW_AT_decl_file : 1
10571 <5c> DW_AT_decl_line : 2
10572 <5d> DW_AT_type : <0x6e>
10573 ...
10574 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10575 <76> DW_AT_byte_size : 4
10576 <77> DW_AT_encoding : 5 (signed)
10577
10578 imports the wrong die ( 0x75 instead of 0x58 ).
10579 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10580 return;
10581 }
10582
82856980
SW
10583 /* Figure out the local name after import. */
10584 import_alias = dwarf2_name (die, cu);
27aa8d6a 10585
794684b6
SW
10586 /* Figure out where the statement is being imported to. */
10587 import_prefix = determine_prefix (die, cu);
10588
10589 /* Figure out what the scope of the imported die is and prepend it
10590 to the name of the imported die. */
de4affc9 10591 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10592
f55ee35c
JK
10593 if (imported_die->tag != DW_TAG_namespace
10594 && imported_die->tag != DW_TAG_module)
794684b6 10595 {
13387711
SW
10596 imported_declaration = imported_name;
10597 canonical_name = imported_name_prefix;
794684b6 10598 }
13387711 10599 else if (strlen (imported_name_prefix) > 0)
12aaed36 10600 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10601 imported_name_prefix,
10602 (cu->language == language_d ? "." : "::"),
10603 imported_name, (char *) NULL);
13387711
SW
10604 else
10605 canonical_name = imported_name;
794684b6 10606
32019081
JK
10607 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10608 for (child_die = die->child; child_die && child_die->tag;
436c571c 10609 child_die = child_die->sibling)
32019081
JK
10610 {
10611 /* DWARF-4: A Fortran use statement with a “rename list” may be
10612 represented by an imported module entry with an import attribute
10613 referring to the module and owned entries corresponding to those
10614 entities that are renamed as part of being imported. */
10615
10616 if (child_die->tag != DW_TAG_imported_declaration)
10617 {
b98664d3 10618 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10619 "- DIE at %s [in module %s]"),
10620 sect_offset_str (child_die->sect_off),
10621 objfile_name (objfile));
32019081
JK
10622 continue;
10623 }
10624
10625 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10626 if (import_attr == NULL)
10627 {
b98664d3 10628 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10629 dwarf_tag_name (child_die->tag));
10630 continue;
10631 }
10632
10633 imported_cu = cu;
10634 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10635 &imported_cu);
10636 imported_name = dwarf2_name (imported_die, imported_cu);
10637 if (imported_name == NULL)
10638 {
b98664d3 10639 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10640 "imported name - DIE at %s [in module %s]"),
10641 sect_offset_str (child_die->sect_off),
10642 objfile_name (objfile));
32019081
JK
10643 continue;
10644 }
10645
eb1e02fd 10646 excludes.push_back (imported_name);
32019081
JK
10647
10648 process_die (child_die, cu);
10649 }
10650
804d2729 10651 add_using_directive (using_directives (cu),
22cee43f
PMR
10652 import_prefix,
10653 canonical_name,
10654 import_alias,
10655 imported_declaration,
10656 excludes,
10657 0,
10658 &objfile->objfile_obstack);
27aa8d6a
SW
10659}
10660
5230b05a
WT
10661/* ICC<14 does not output the required DW_AT_declaration on incomplete
10662 types, but gives them a size of zero. Starting with version 14,
10663 ICC is compatible with GCC. */
10664
9068261f 10665static bool
5230b05a
WT
10666producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10667{
10668 if (!cu->checked_producer)
10669 check_producer (cu);
10670
10671 return cu->producer_is_icc_lt_14;
10672}
10673
eb77c9df
AB
10674/* ICC generates a DW_AT_type for C void functions. This was observed on
10675 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10676 which says that void functions should not have a DW_AT_type. */
10677
10678static bool
10679producer_is_icc (struct dwarf2_cu *cu)
10680{
10681 if (!cu->checked_producer)
10682 check_producer (cu);
10683
10684 return cu->producer_is_icc;
10685}
10686
1b80a9fa
JK
10687/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10688 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10689 this, it was first present in GCC release 4.3.0. */
10690
9068261f 10691static bool
1b80a9fa
JK
10692producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10693{
10694 if (!cu->checked_producer)
10695 check_producer (cu);
10696
10697 return cu->producer_is_gcc_lt_4_3;
10698}
10699
d721ba37
PA
10700static file_and_directory
10701find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10702{
d721ba37
PA
10703 file_and_directory res;
10704
9291a0cd
TT
10705 /* Find the filename. Do not use dwarf2_name here, since the filename
10706 is not a source language identifier. */
d721ba37
PA
10707 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10708 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10709
d721ba37
PA
10710 if (res.comp_dir == NULL
10711 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10712 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10713 {
d721ba37
PA
10714 res.comp_dir_storage = ldirname (res.name);
10715 if (!res.comp_dir_storage.empty ())
10716 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10717 }
d721ba37 10718 if (res.comp_dir != NULL)
9291a0cd
TT
10719 {
10720 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10721 directory, get rid of it. */
d721ba37 10722 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10723
d721ba37
PA
10724 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10725 res.comp_dir = cp + 1;
9291a0cd
TT
10726 }
10727
d721ba37
PA
10728 if (res.name == NULL)
10729 res.name = "<unknown>";
10730
10731 return res;
9291a0cd
TT
10732}
10733
f4dc4d17
DE
10734/* Handle DW_AT_stmt_list for a compilation unit.
10735 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10736 COMP_DIR is the compilation directory. LOWPC is passed to
10737 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10738
10739static void
10740handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10741 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10742{
976ca316 10743 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10744 struct attribute *attr;
527f3840
JK
10745 struct line_header line_header_local;
10746 hashval_t line_header_local_hash;
527f3840
JK
10747 void **slot;
10748 int decode_mapping;
2ab95328 10749
f4dc4d17
DE
10750 gdb_assert (! cu->per_cu->is_debug_types);
10751
2ab95328 10752 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 10753 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
10754 return;
10755
d4df075e 10756 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
10757
10758 /* The line header hash table is only created if needed (it exists to
10759 prevent redundant reading of the line table for partial_units).
10760 If we're given a partial_unit, we'll need it. If we're given a
10761 compile_unit, then use the line header hash table if it's already
10762 created, but don't create one just yet. */
10763
976ca316 10764 if (per_objfile->line_header_hash == NULL
527f3840 10765 && die->tag == DW_TAG_partial_unit)
2ab95328 10766 {
976ca316 10767 per_objfile->line_header_hash
d15acc42
TT
10768 .reset (htab_create_alloc (127, line_header_hash_voidp,
10769 line_header_eq_voidp,
10770 free_line_header_voidp,
10771 xcalloc, xfree));
527f3840 10772 }
2ab95328 10773
9c541725 10774 line_header_local.sect_off = line_offset;
527f3840
JK
10775 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10776 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10777 if (per_objfile->line_header_hash != NULL)
527f3840 10778 {
976ca316 10779 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10780 &line_header_local,
10781 line_header_local_hash, NO_INSERT);
10782
10783 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10784 is not present in *SLOT (since if there is something in *SLOT then
10785 it will be for a partial_unit). */
10786 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10787 {
527f3840 10788 gdb_assert (*slot != NULL);
9a3c8263 10789 cu->line_header = (struct line_header *) *slot;
527f3840 10790 return;
dee91e82 10791 }
2ab95328 10792 }
527f3840
JK
10793
10794 /* dwarf_decode_line_header does not yet provide sufficient information.
10795 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10796 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10797 if (lh == NULL)
527f3840 10798 return;
4c8aa72d
PA
10799
10800 cu->line_header = lh.release ();
10801 cu->line_header_die_owner = die;
527f3840 10802
976ca316 10803 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10804 slot = NULL;
10805 else
10806 {
976ca316 10807 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10808 &line_header_local,
10809 line_header_local_hash, INSERT);
10810 gdb_assert (slot != NULL);
10811 }
10812 if (slot != NULL && *slot == NULL)
10813 {
10814 /* This newly decoded line number information unit will be owned
10815 by line_header_hash hash table. */
10816 *slot = cu->line_header;
4c8aa72d 10817 cu->line_header_die_owner = NULL;
527f3840
JK
10818 }
10819 else
10820 {
10821 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 10822 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
10823 line_header for this CU - it may happen at most once for each line
10824 number information unit. And if we're not using line_header_hash
10825 then this is what we want as well. */
10826 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10827 }
10828 decode_mapping = (die->tag != DW_TAG_partial_unit);
10829 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10830 decode_mapping);
fff8551c 10831
2ab95328
TT
10832}
10833
95554aad 10834/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10835
c906108c 10836static void
e7c27a73 10837read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10838{
976ca316
SM
10839 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10840 struct objfile *objfile = per_objfile->objfile;
08feed99 10841 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10842 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10843 CORE_ADDR highpc = ((CORE_ADDR) 0);
10844 struct attribute *attr;
c906108c 10845 struct die_info *child_die;
e142c38c 10846 CORE_ADDR baseaddr;
6e70227d 10847
380618d6 10848 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10849 baseaddr = objfile->text_section_offset ();
c906108c 10850
fae299cd 10851 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10852
10853 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10854 from finish_block. */
2acceee2 10855 if (lowpc == ((CORE_ADDR) -1))
c906108c 10856 lowpc = highpc;
3e29f34a 10857 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10858
d721ba37 10859 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10860
f4b8a18d
KW
10861 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10862 standardised yet. As a workaround for the language detection we fall
10863 back to the DW_AT_producer string. */
10864 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10865 cu->language = language_opencl;
10866
3019eac3
DE
10867 /* Similar hack for Go. */
10868 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10869 set_cu_language (DW_LANG_Go, cu);
10870
c24bdb02 10871 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10872
10873 /* Decode line number information if present. We do this before
10874 processing child DIEs, so that the line header table is available
10875 for DW_AT_decl_file. */
d721ba37 10876 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10877
10878 /* Process all dies in compilation unit. */
10879 if (die->child != NULL)
10880 {
10881 child_die = die->child;
10882 while (child_die && child_die->tag)
10883 {
10884 process_die (child_die, cu);
436c571c 10885 child_die = child_die->sibling;
3019eac3
DE
10886 }
10887 }
10888
10889 /* Decode macro information, if present. Dwarf 2 macro information
10890 refers to information in the line number info statement program
10891 header, so we can only read it if we've read the header
10892 successfully. */
0af92d60
JK
10893 attr = dwarf2_attr (die, DW_AT_macros, cu);
10894 if (attr == NULL)
10895 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 10896 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
10897 {
10898 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10899 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10900
529908cb 10901 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
10902 }
10903 else
10904 {
10905 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 10906 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 10907 {
529908cb 10908 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 10909
43f3e411 10910 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10911 }
10912 }
3019eac3
DE
10913}
10914
c24bdb02
KS
10915void
10916dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10917{
f4dc4d17
DE
10918 struct type_unit_group *tu_group;
10919 int first_time;
3019eac3 10920 struct attribute *attr;
9c541725 10921 unsigned int i;
0186c6a7 10922 struct signatured_type *sig_type;
3019eac3 10923
f4dc4d17 10924 gdb_assert (per_cu->is_debug_types);
0186c6a7 10925 sig_type = (struct signatured_type *) per_cu;
3019eac3 10926
c24bdb02 10927 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10928
f4dc4d17 10929 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10930 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10931 if (sig_type->type_unit_group == NULL)
c24bdb02 10932 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10933 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10934
10935 /* If we've already processed this stmt_list there's no real need to
10936 do it again, we could fake it and just recreate the part we need
10937 (file name,index -> symtab mapping). If data shows this optimization
10938 is useful we can do it then. */
8adb8487
TT
10939 type_unit_group_unshareable *tug_unshare
10940 = per_objfile->get_type_unit_group_unshareable (tu_group);
10941 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
10942
10943 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10944 debug info. */
fff8551c 10945 line_header_up lh;
d4df075e 10946 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 10947 {
d4df075e 10948 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 10949 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10950 }
10951 if (lh == NULL)
10952 {
10953 if (first_time)
c24bdb02 10954 start_symtab ("", NULL, 0);
f4dc4d17
DE
10955 else
10956 {
8adb8487 10957 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 10958 gdb_assert (m_builder == nullptr);
8adb8487 10959 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10960 m_builder.reset (new struct buildsym_compunit
10961 (COMPUNIT_OBJFILE (cust), "",
10962 COMPUNIT_DIRNAME (cust),
10963 compunit_language (cust),
10964 0, cust));
770479f2 10965 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10966 }
f4dc4d17 10967 return;
3019eac3
DE
10968 }
10969
c24bdb02
KS
10970 line_header = lh.release ();
10971 line_header_die_owner = die;
3019eac3 10972
f4dc4d17
DE
10973 if (first_time)
10974 {
c24bdb02 10975 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10976
1fd60fc0
DE
10977 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10978 still initializing it, and our caller (a few levels up)
10979 process_full_type_unit still needs to know if this is the first
10980 time. */
10981
8adb8487 10982 tug_unshare->symtabs
4ac93832
TT
10983 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10984 struct symtab *, line_header->file_names_size ());
3019eac3 10985
7ba99d21
AT
10986 auto &file_names = line_header->file_names ();
10987 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10988 {
7ba99d21 10989 file_entry &fe = file_names[i];
c24bdb02
KS
10990 dwarf2_start_subfile (this, fe.name,
10991 fe.include_dir (line_header));
10992 buildsym_compunit *b = get_builder ();
10993 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10994 {
4c8aa72d
PA
10995 /* NOTE: start_subfile will recognize when it's been
10996 passed a file it has already seen. So we can't
10997 assume there's a simple mapping from
10998 cu->line_header->file_names to subfiles, plus
10999 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11000 b->get_current_subfile ()->symtab
11001 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11002 }
11003
c24bdb02 11004 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11005 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11006 }
11007 }
11008 else
3019eac3 11009 {
c24bdb02 11010 gdb_assert (m_builder == nullptr);
8adb8487 11011 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11012 m_builder.reset (new struct buildsym_compunit
11013 (COMPUNIT_OBJFILE (cust), "",
11014 COMPUNIT_DIRNAME (cust),
11015 compunit_language (cust),
11016 0, cust));
770479f2 11017 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11018
7ba99d21
AT
11019 auto &file_names = line_header->file_names ();
11020 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11021 {
7ba99d21 11022 file_entry &fe = file_names[i];
8adb8487 11023 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11024 }
3019eac3
DE
11025 }
11026
f4dc4d17
DE
11027 /* The main symtab is allocated last. Type units don't have DW_AT_name
11028 so they don't have a "real" (so to speak) symtab anyway.
11029 There is later code that will assign the main symtab to all symbols
11030 that don't have one. We need to handle the case of a symbol with a
11031 missing symtab (DW_AT_decl_file) anyway. */
11032}
3019eac3 11033
f4dc4d17
DE
11034/* Process DW_TAG_type_unit.
11035 For TUs we want to skip the first top level sibling if it's not the
11036 actual type being defined by this TU. In this case the first top
11037 level sibling is there to provide context only. */
3019eac3 11038
f4dc4d17
DE
11039static void
11040read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11041{
11042 struct die_info *child_die;
3019eac3 11043
f4dc4d17
DE
11044 prepare_one_comp_unit (cu, die, language_minimal);
11045
11046 /* Initialize (or reinitialize) the machinery for building symtabs.
11047 We do this before processing child DIEs, so that the line header table
11048 is available for DW_AT_decl_file. */
c24bdb02 11049 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11050
11051 if (die->child != NULL)
11052 {
11053 child_die = die->child;
11054 while (child_die && child_die->tag)
11055 {
11056 process_die (child_die, cu);
436c571c 11057 child_die = child_die->sibling;
f4dc4d17
DE
11058 }
11059 }
3019eac3
DE
11060}
11061\f
80626a55
DE
11062/* DWO/DWP files.
11063
11064 http://gcc.gnu.org/wiki/DebugFission
11065 http://gcc.gnu.org/wiki/DebugFissionDWP
11066
11067 To simplify handling of both DWO files ("object" files with the DWARF info)
11068 and DWP files (a file with the DWOs packaged up into one file), we treat
11069 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11070
11071static hashval_t
11072hash_dwo_file (const void *item)
11073{
9a3c8263 11074 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11075 hashval_t hash;
3019eac3 11076
a2ce51a0
DE
11077 hash = htab_hash_string (dwo_file->dwo_name);
11078 if (dwo_file->comp_dir != NULL)
11079 hash += htab_hash_string (dwo_file->comp_dir);
11080 return hash;
3019eac3
DE
11081}
11082
11083static int
11084eq_dwo_file (const void *item_lhs, const void *item_rhs)
11085{
9a3c8263
SM
11086 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11087 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11088
a2ce51a0
DE
11089 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11090 return 0;
11091 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11092 return lhs->comp_dir == rhs->comp_dir;
11093 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11094}
11095
11096/* Allocate a hash table for DWO files. */
11097
51ac9db5 11098static htab_up
298e9637 11099allocate_dwo_file_hash_table ()
3019eac3 11100{
51ac9db5
SM
11101 auto delete_dwo_file = [] (void *item)
11102 {
11103 struct dwo_file *dwo_file = (struct dwo_file *) item;
11104
11105 delete dwo_file;
11106 };
11107
bc68fb19
TT
11108 return htab_up (htab_create_alloc (41,
11109 hash_dwo_file,
11110 eq_dwo_file,
11111 delete_dwo_file,
11112 xcalloc, xfree));
3019eac3
DE
11113}
11114
80626a55
DE
11115/* Lookup DWO file DWO_NAME. */
11116
11117static void **
976ca316 11118lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11119 const char *dwo_name,
11120 const char *comp_dir)
80626a55
DE
11121{
11122 struct dwo_file find_entry;
11123 void **slot;
11124
976ca316
SM
11125 if (per_objfile->per_bfd->dwo_files == NULL)
11126 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11127
0ac5b59e
DE
11128 find_entry.dwo_name = dwo_name;
11129 find_entry.comp_dir = comp_dir;
976ca316 11130 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11131 INSERT);
80626a55
DE
11132
11133 return slot;
11134}
11135
3019eac3
DE
11136static hashval_t
11137hash_dwo_unit (const void *item)
11138{
9a3c8263 11139 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11140
11141 /* This drops the top 32 bits of the id, but is ok for a hash. */
11142 return dwo_unit->signature;
11143}
11144
11145static int
11146eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11147{
9a3c8263
SM
11148 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11149 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11150
11151 /* The signature is assumed to be unique within the DWO file.
11152 So while object file CU dwo_id's always have the value zero,
11153 that's OK, assuming each object file DWO file has only one CU,
11154 and that's the rule for now. */
11155 return lhs->signature == rhs->signature;
11156}
11157
11158/* Allocate a hash table for DWO CUs,TUs.
11159 There is one of these tables for each of CUs,TUs for each DWO file. */
11160
b0b6a987 11161static htab_up
298e9637 11162allocate_dwo_unit_table ()
3019eac3
DE
11163{
11164 /* Start out with a pretty small number.
11165 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11166 return htab_up (htab_create_alloc (3,
11167 hash_dwo_unit,
11168 eq_dwo_unit,
11169 NULL, xcalloc, xfree));
3019eac3
DE
11170}
11171
19c3d4c9 11172/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11173
11174static void
19c3d4c9
DE
11175create_dwo_cu_reader (const struct die_reader_specs *reader,
11176 const gdb_byte *info_ptr,
11177 struct die_info *comp_unit_die,
c0ab21c2
TT
11178 struct dwo_file *dwo_file,
11179 struct dwo_unit *dwo_unit)
3019eac3
DE
11180{
11181 struct dwarf2_cu *cu = reader->cu;
9c541725 11182 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11183 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11184
a084a2a6
AT
11185 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11186 if (!signature.has_value ())
3019eac3 11187 {
b98664d3 11188 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11189 " its dwo_id [in module %s]"),
9d8780f0 11190 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11191 return;
11192 }
11193
3019eac3 11194 dwo_unit->dwo_file = dwo_file;
a084a2a6 11195 dwo_unit->signature = *signature;
8a0459fd 11196 dwo_unit->section = section;
9c541725 11197 dwo_unit->sect_off = sect_off;
3019eac3
DE
11198 dwo_unit->length = cu->per_cu->length;
11199
6f738b01
SM
11200 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11201 sect_offset_str (sect_off),
11202 hex_string (dwo_unit->signature));
3019eac3
DE
11203}
11204
33c5cd75 11205/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11206 Note: This function processes DWO files only, not DWP files. */
3019eac3 11207
33c5cd75 11208static void
976ca316 11209create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11210 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11211 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11212{
976ca316
SM
11213 struct objfile *objfile = per_objfile->objfile;
11214 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11215 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11216
96b79293 11217 section.read (objfile);
33c5cd75 11218 info_ptr = section.buffer;
3019eac3
DE
11219
11220 if (info_ptr == NULL)
33c5cd75 11221 return;
3019eac3 11222
6f738b01
SM
11223 dwarf_read_debug_printf ("Reading %s for %s:",
11224 section.get_name (),
11225 section.get_file_name ());
3019eac3 11226
33c5cd75 11227 end_ptr = info_ptr + section.size;
3019eac3
DE
11228 while (info_ptr < end_ptr)
11229 {
11230 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11231 struct dwo_unit read_unit {};
33c5cd75
DB
11232 struct dwo_unit *dwo_unit;
11233 void **slot;
11234 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11235
11236 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11237 per_cu.per_bfd = per_bfd;
3019eac3 11238 per_cu.is_debug_types = 0;
33c5cd75
DB
11239 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11240 per_cu.section = &section;
11241
976ca316 11242 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11243 if (!reader.dummy_p)
11244 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11245 &dwo_file, &read_unit);
33c5cd75
DB
11246 info_ptr += per_cu.length;
11247
11248 // If the unit could not be parsed, skip it.
c0ab21c2 11249 if (read_unit.dwo_file == NULL)
33c5cd75 11250 continue;
3019eac3 11251
33c5cd75 11252 if (cus_htab == NULL)
298e9637 11253 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11254
1859c670 11255 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11256 struct dwo_unit);
c0ab21c2 11257 *dwo_unit = read_unit;
b0b6a987 11258 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11259 gdb_assert (slot != NULL);
11260 if (*slot != NULL)
19c3d4c9 11261 {
33c5cd75
DB
11262 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11263 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11264
b98664d3 11265 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11266 " the entry at offset %s, signature %s"),
11267 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11268 hex_string (dwo_unit->signature));
19c3d4c9 11269 }
33c5cd75 11270 *slot = (void *)dwo_unit;
3019eac3 11271 }
3019eac3
DE
11272}
11273
80626a55
DE
11274/* DWP file .debug_{cu,tu}_index section format:
11275 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11276 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11277
11278 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11279 officially standard DWP format was published with DWARF v5 and is called
11280 Version 5. There are no versions 3 or 4.
80626a55 11281
d2415c6c
DE
11282 DWP Version 1:
11283
80626a55
DE
11284 Both index sections have the same format, and serve to map a 64-bit
11285 signature to a set of section numbers. Each section begins with a header,
11286 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11287 indexes, and a pool of 32-bit section numbers. The index sections will be
11288 aligned at 8-byte boundaries in the file.
11289
d2415c6c
DE
11290 The index section header consists of:
11291
11292 V, 32 bit version number
11293 -, 32 bits unused
11294 N, 32 bit number of compilation units or type units in the index
11295 M, 32 bit number of slots in the hash table
80626a55 11296
d2415c6c 11297 Numbers are recorded using the byte order of the application binary.
80626a55 11298
d2415c6c
DE
11299 The hash table begins at offset 16 in the section, and consists of an array
11300 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11301 order of the application binary). Unused slots in the hash table are 0.
11302 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11303
d2415c6c
DE
11304 The parallel table begins immediately after the hash table
11305 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11306 array of 32-bit indexes (using the byte order of the application binary),
11307 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11308 table contains a 32-bit index into the pool of section numbers. For unused
11309 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11310
73869dc2
DE
11311 The pool of section numbers begins immediately following the hash table
11312 (at offset 16 + 12 * M from the beginning of the section). The pool of
11313 section numbers consists of an array of 32-bit words (using the byte order
11314 of the application binary). Each item in the array is indexed starting
11315 from 0. The hash table entry provides the index of the first section
11316 number in the set. Additional section numbers in the set follow, and the
11317 set is terminated by a 0 entry (section number 0 is not used in ELF).
11318
11319 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11320 section must be the first entry in the set, and the .debug_abbrev.dwo must
11321 be the second entry. Other members of the set may follow in any order.
11322
11323 ---
11324
d2854d8d 11325 DWP Versions 2 and 5:
73869dc2 11326
d2854d8d 11327 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11328 and the entries in the index tables are now offsets into these sections.
11329 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11330 section.
11331
11332 Index Section Contents:
11333 Header
11334 Hash Table of Signatures dwp_hash_table.hash_table
11335 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11336 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11337 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11338
11339 The index section header consists of:
11340
11341 V, 32 bit version number
11342 L, 32 bit number of columns in the table of section offsets
11343 N, 32 bit number of compilation units or type units in the index
11344 M, 32 bit number of slots in the hash table
11345
11346 Numbers are recorded using the byte order of the application binary.
11347
11348 The hash table has the same format as version 1.
11349 The parallel table of indices has the same format as version 1,
11350 except that the entries are origin-1 indices into the table of sections
11351 offsets and the table of section sizes.
11352
11353 The table of offsets begins immediately following the parallel table
11354 (at offset 16 + 12 * M from the beginning of the section). The table is
11355 a two-dimensional array of 32-bit words (using the byte order of the
11356 application binary), with L columns and N+1 rows, in row-major order.
11357 Each row in the array is indexed starting from 0. The first row provides
11358 a key to the remaining rows: each column in this row provides an identifier
11359 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11360 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11361
11362 DW_SECT_INFO 1 .debug_info.dwo
11363 DW_SECT_TYPES 2 .debug_types.dwo
11364 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11365 DW_SECT_LINE 4 .debug_line.dwo
11366 DW_SECT_LOC 5 .debug_loc.dwo
11367 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11368 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11369 DW_SECT_MACRO 8 .debug_macro.dwo
11370
d2854d8d
CT
11371 The section identifiers for Version 5 are:
11372
11373 DW_SECT_INFO_V5 1 .debug_info.dwo
11374 DW_SECT_RESERVED_V5 2 --
11375 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11376 DW_SECT_LINE_V5 4 .debug_line.dwo
11377 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11378 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11379 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11380 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11381
73869dc2
DE
11382 The offsets provided by the CU and TU index sections are the base offsets
11383 for the contributions made by each CU or TU to the corresponding section
11384 in the package file. Each CU and TU header contains an abbrev_offset
11385 field, used to find the abbreviations table for that CU or TU within the
11386 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11387 be interpreted as relative to the base offset given in the index section.
11388 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11389 should be interpreted as relative to the base offset for .debug_line.dwo,
11390 and offsets into other debug sections obtained from DWARF attributes should
11391 also be interpreted as relative to the corresponding base offset.
11392
11393 The table of sizes begins immediately following the table of offsets.
11394 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11395 with L columns and N rows, in row-major order. Each row in the array is
11396 indexed starting from 1 (row 0 is shared by the two tables).
11397
11398 ---
11399
11400 Hash table lookup is handled the same in version 1 and 2:
11401
11402 We assume that N and M will not exceed 2^32 - 1.
11403 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11404
d2415c6c
DE
11405 Given a 64-bit compilation unit signature or a type signature S, an entry
11406 in the hash table is located as follows:
80626a55 11407
d2415c6c
DE
11408 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11409 the low-order k bits all set to 1.
80626a55 11410
d2415c6c 11411 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11412
d2415c6c
DE
11413 3) If the hash table entry at index H matches the signature, use that
11414 entry. If the hash table entry at index H is unused (all zeroes),
11415 terminate the search: the signature is not present in the table.
80626a55 11416
d2415c6c 11417 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11418
d2415c6c 11419 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11420 to stop at an unused slot or find the match. */
80626a55
DE
11421
11422/* Create a hash table to map DWO IDs to their CU/TU entry in
11423 .debug_{info,types}.dwo in DWP_FILE.
11424 Returns NULL if there isn't one.
11425 Note: This function processes DWP files only, not DWO files. */
11426
11427static struct dwp_hash_table *
976ca316 11428create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11429 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11430{
976ca316 11431 struct objfile *objfile = per_objfile->objfile;
400174b1 11432 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11433 const gdb_byte *index_ptr, *index_end;
80626a55 11434 struct dwarf2_section_info *index;
73869dc2 11435 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11436 struct dwp_hash_table *htab;
11437
11438 if (is_debug_types)
11439 index = &dwp_file->sections.tu_index;
11440 else
11441 index = &dwp_file->sections.cu_index;
11442
96b79293 11443 if (index->empty ())
80626a55 11444 return NULL;
96b79293 11445 index->read (objfile);
80626a55
DE
11446
11447 index_ptr = index->buffer;
11448 index_end = index_ptr + index->size;
11449
d2854d8d
CT
11450 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11451 For now it's safe to just read 4 bytes (particularly as it's difficult to
11452 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11453 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11454 index_ptr += 4;
d2854d8d 11455 if (version == 2 || version == 5)
73869dc2
DE
11456 nr_columns = read_4_bytes (dbfd, index_ptr);
11457 else
11458 nr_columns = 0;
11459 index_ptr += 4;
80626a55
DE
11460 nr_units = read_4_bytes (dbfd, index_ptr);
11461 index_ptr += 4;
11462 nr_slots = read_4_bytes (dbfd, index_ptr);
11463 index_ptr += 4;
11464
d2854d8d 11465 if (version != 1 && version != 2 && version != 5)
80626a55 11466 {
21aa081e 11467 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11468 " [in module %s]"),
21aa081e 11469 pulongest (version), dwp_file->name);
80626a55
DE
11470 }
11471 if (nr_slots != (nr_slots & -nr_slots))
11472 {
21aa081e 11473 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11474 " is not power of 2 [in module %s]"),
21aa081e 11475 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11476 }
11477
976ca316 11478 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11479 htab->version = version;
11480 htab->nr_columns = nr_columns;
80626a55
DE
11481 htab->nr_units = nr_units;
11482 htab->nr_slots = nr_slots;
11483 htab->hash_table = index_ptr;
11484 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11485
11486 /* Exit early if the table is empty. */
11487 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11488 || (version == 2 && nr_columns == 0)
11489 || (version == 5 && nr_columns == 0))
73869dc2
DE
11490 {
11491 /* All must be zero. */
11492 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11493 || (version == 2 && nr_columns != 0)
11494 || (version == 5 && nr_columns != 0))
73869dc2 11495 {
b98664d3 11496 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11497 " all zero [in modules %s]"),
11498 dwp_file->name);
11499 }
11500 return htab;
11501 }
11502
11503 if (version == 1)
11504 {
11505 htab->section_pool.v1.indices =
11506 htab->unit_table + sizeof (uint32_t) * nr_slots;
11507 /* It's harder to decide whether the section is too small in v1.
11508 V1 is deprecated anyway so we punt. */
11509 }
d2854d8d 11510 else if (version == 2)
73869dc2
DE
11511 {
11512 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11513 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11514 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11515 /* Reverse map for error checking. */
11516 int ids_seen[DW_SECT_MAX + 1];
11517 int i;
11518
11519 if (nr_columns < 2)
11520 {
11521 error (_("Dwarf Error: bad DWP hash table, too few columns"
11522 " in section table [in module %s]"),
11523 dwp_file->name);
11524 }
11525 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11526 {
11527 error (_("Dwarf Error: bad DWP hash table, too many columns"
11528 " in section table [in module %s]"),
11529 dwp_file->name);
11530 }
04fd5eed
GB
11531 memset (ids, 255, sizeof_ids);
11532 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11533 for (i = 0; i < nr_columns; ++i)
11534 {
11535 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11536
11537 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11538 {
11539 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11540 " in section table [in module %s]"),
11541 id, dwp_file->name);
11542 }
11543 if (ids_seen[id] != -1)
11544 {
11545 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11546 " id %d in section table [in module %s]"),
11547 id, dwp_file->name);
11548 }
11549 ids_seen[id] = i;
11550 ids[i] = id;
11551 }
11552 /* Must have exactly one info or types section. */
11553 if (((ids_seen[DW_SECT_INFO] != -1)
11554 + (ids_seen[DW_SECT_TYPES] != -1))
11555 != 1)
11556 {
11557 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11558 " DWO info/types section [in module %s]"),
11559 dwp_file->name);
11560 }
11561 /* Must have an abbrev section. */
11562 if (ids_seen[DW_SECT_ABBREV] == -1)
11563 {
11564 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11565 " section [in module %s]"),
11566 dwp_file->name);
11567 }
11568 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11569 htab->section_pool.v2.sizes =
11570 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11571 * nr_units * nr_columns);
11572 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11573 * nr_units * nr_columns))
11574 > index_end)
11575 {
11576 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11577 " [in module %s]"),
11578 dwp_file->name);
11579 }
11580 }
d2854d8d
CT
11581 else /* version == 5 */
11582 {
11583 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11584 int *ids = htab->section_pool.v5.section_ids;
11585 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11586 /* Reverse map for error checking. */
11587 int ids_seen[DW_SECT_MAX_V5 + 1];
11588
11589 if (nr_columns < 2)
11590 {
11591 error (_("Dwarf Error: bad DWP hash table, too few columns"
11592 " in section table [in module %s]"),
11593 dwp_file->name);
11594 }
11595 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11596 {
11597 error (_("Dwarf Error: bad DWP hash table, too many columns"
11598 " in section table [in module %s]"),
11599 dwp_file->name);
11600 }
11601 memset (ids, 255, sizeof_ids);
11602 memset (ids_seen, 255, sizeof (ids_seen));
11603 for (int i = 0; i < nr_columns; ++i)
11604 {
11605 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11606
11607 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11608 {
11609 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11610 " in section table [in module %s]"),
11611 id, dwp_file->name);
11612 }
11613 if (ids_seen[id] != -1)
11614 {
11615 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11616 " id %d in section table [in module %s]"),
11617 id, dwp_file->name);
11618 }
11619 ids_seen[id] = i;
11620 ids[i] = id;
11621 }
11622 /* Must have seen an info section. */
11623 if (ids_seen[DW_SECT_INFO_V5] == -1)
11624 {
11625 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11626 " DWO info/types section [in module %s]"),
11627 dwp_file->name);
11628 }
11629 /* Must have an abbrev section. */
11630 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11631 {
11632 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11633 " section [in module %s]"),
11634 dwp_file->name);
11635 }
11636 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11637 htab->section_pool.v5.sizes
11638 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11639 * nr_units * nr_columns);
11640 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11641 * nr_units * nr_columns))
11642 > index_end)
11643 {
11644 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11645 " [in module %s]"),
11646 dwp_file->name);
11647 }
11648 }
80626a55
DE
11649
11650 return htab;
11651}
11652
11653/* Update SECTIONS with the data from SECTP.
11654
5bb6e9dd
TT
11655 This function is like the other "locate" section routines, but in
11656 this context the sections to read comes from the DWP V1 hash table,
11657 not the full ELF section table.
80626a55
DE
11658
11659 The result is non-zero for success, or zero if an error was found. */
11660
11661static int
73869dc2
DE
11662locate_v1_virtual_dwo_sections (asection *sectp,
11663 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11664{
11665 const struct dwop_section_names *names = &dwop_section_names;
11666
fbedd546 11667 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11668 {
11669 /* There can be only one. */
049412e3 11670 if (sections->abbrev.s.section != NULL)
80626a55 11671 return 0;
049412e3 11672 sections->abbrev.s.section = sectp;
fd361982 11673 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11674 }
fbedd546
TT
11675 else if (names->info_dwo.matches (sectp->name)
11676 || names->types_dwo.matches (sectp->name))
80626a55
DE
11677 {
11678 /* There can be only one. */
049412e3 11679 if (sections->info_or_types.s.section != NULL)
80626a55 11680 return 0;
049412e3 11681 sections->info_or_types.s.section = sectp;
fd361982 11682 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11683 }
fbedd546 11684 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
11685 {
11686 /* There can be only one. */
049412e3 11687 if (sections->line.s.section != NULL)
80626a55 11688 return 0;
049412e3 11689 sections->line.s.section = sectp;
fd361982 11690 sections->line.size = bfd_section_size (sectp);
80626a55 11691 }
fbedd546 11692 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
11693 {
11694 /* There can be only one. */
049412e3 11695 if (sections->loc.s.section != NULL)
80626a55 11696 return 0;
049412e3 11697 sections->loc.s.section = sectp;
fd361982 11698 sections->loc.size = bfd_section_size (sectp);
80626a55 11699 }
fbedd546 11700 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
11701 {
11702 /* There can be only one. */
049412e3 11703 if (sections->macinfo.s.section != NULL)
80626a55 11704 return 0;
049412e3 11705 sections->macinfo.s.section = sectp;
fd361982 11706 sections->macinfo.size = bfd_section_size (sectp);
80626a55 11707 }
fbedd546 11708 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
11709 {
11710 /* There can be only one. */
049412e3 11711 if (sections->macro.s.section != NULL)
80626a55 11712 return 0;
049412e3 11713 sections->macro.s.section = sectp;
fd361982 11714 sections->macro.size = bfd_section_size (sectp);
80626a55 11715 }
fbedd546 11716 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
11717 {
11718 /* There can be only one. */
049412e3 11719 if (sections->str_offsets.s.section != NULL)
80626a55 11720 return 0;
049412e3 11721 sections->str_offsets.s.section = sectp;
fd361982 11722 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11723 }
11724 else
11725 {
11726 /* No other kind of section is valid. */
11727 return 0;
11728 }
11729
11730 return 1;
11731}
11732
73869dc2
DE
11733/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11734 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11735 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11736 This is for DWP version 1 files. */
80626a55
DE
11737
11738static struct dwo_unit *
976ca316 11739create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11740 struct dwp_file *dwp_file,
73869dc2
DE
11741 uint32_t unit_index,
11742 const char *comp_dir,
11743 ULONGEST signature, int is_debug_types)
80626a55 11744{
73869dc2
DE
11745 const struct dwp_hash_table *dwp_htab =
11746 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11747 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11748 const char *kind = is_debug_types ? "TU" : "CU";
11749 struct dwo_file *dwo_file;
11750 struct dwo_unit *dwo_unit;
73869dc2 11751 struct virtual_v1_dwo_sections sections;
80626a55 11752 void **dwo_file_slot;
80626a55
DE
11753 int i;
11754
73869dc2
DE
11755 gdb_assert (dwp_file->version == 1);
11756
6f738b01
SM
11757 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11758 kind, pulongest (unit_index), hex_string (signature),
11759 dwp_file->name);
80626a55 11760
19ac8c2e 11761 /* Fetch the sections of this DWO unit.
80626a55
DE
11762 Put a limit on the number of sections we look for so that bad data
11763 doesn't cause us to loop forever. */
11764
73869dc2 11765#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11766 (1 /* .debug_info or .debug_types */ \
11767 + 1 /* .debug_abbrev */ \
11768 + 1 /* .debug_line */ \
11769 + 1 /* .debug_loc */ \
11770 + 1 /* .debug_str_offsets */ \
19ac8c2e 11771 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11772 + 1 /* trailing zero */)
11773
11774 memset (&sections, 0, sizeof (sections));
80626a55 11775
73869dc2 11776 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11777 {
11778 asection *sectp;
11779 uint32_t section_nr =
11780 read_4_bytes (dbfd,
73869dc2
DE
11781 dwp_htab->section_pool.v1.indices
11782 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11783
11784 if (section_nr == 0)
11785 break;
11786 if (section_nr >= dwp_file->num_sections)
11787 {
11788 error (_("Dwarf Error: bad DWP hash table, section number too large"
11789 " [in module %s]"),
11790 dwp_file->name);
11791 }
11792
11793 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11794 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11795 {
11796 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11797 " [in module %s]"),
11798 dwp_file->name);
11799 }
11800 }
11801
11802 if (i < 2
96b79293
TT
11803 || sections.info_or_types.empty ()
11804 || sections.abbrev.empty ())
80626a55
DE
11805 {
11806 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11807 " [in module %s]"),
11808 dwp_file->name);
11809 }
73869dc2 11810 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11811 {
11812 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11813 " [in module %s]"),
11814 dwp_file->name);
11815 }
11816
11817 /* It's easier for the rest of the code if we fake a struct dwo_file and
11818 have dwo_unit "live" in that. At least for now.
11819
11820 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11821 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11822 file, we can combine them back into a virtual DWO file to save space
11823 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11824 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11825
791afaa2
TT
11826 std::string virtual_dwo_name =
11827 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11828 sections.abbrev.get_id (),
11829 sections.line.get_id (),
11830 sections.loc.get_id (),
11831 sections.str_offsets.get_id ());
80626a55 11832 /* Can we use an existing virtual DWO file? */
976ca316 11833 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11834 comp_dir);
80626a55
DE
11835 /* Create one if necessary. */
11836 if (*dwo_file_slot == NULL)
11837 {
6f738b01
SM
11838 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11839 virtual_dwo_name.c_str ());
11840
51ac9db5 11841 dwo_file = new struct dwo_file;
976ca316 11842 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11843 dwo_file->comp_dir = comp_dir;
80626a55
DE
11844 dwo_file->sections.abbrev = sections.abbrev;
11845 dwo_file->sections.line = sections.line;
11846 dwo_file->sections.loc = sections.loc;
11847 dwo_file->sections.macinfo = sections.macinfo;
11848 dwo_file->sections.macro = sections.macro;
11849 dwo_file->sections.str_offsets = sections.str_offsets;
11850 /* The "str" section is global to the entire DWP file. */
11851 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11852 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11853 there's no need to record it in dwo_file.
11854 Also, we can't simply record type sections in dwo_file because
11855 we record a pointer into the vector in dwo_unit. As we collect more
11856 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11857 for it, invalidating all copies of pointers into the previous
11858 contents. */
80626a55
DE
11859 *dwo_file_slot = dwo_file;
11860 }
11861 else
11862 {
6f738b01
SM
11863 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11864 virtual_dwo_name.c_str ());
11865
9a3c8263 11866 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11867 }
80626a55 11868
976ca316 11869 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11870 dwo_unit->dwo_file = dwo_file;
11871 dwo_unit->signature = signature;
8d749320 11872 dwo_unit->section =
976ca316 11873 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11874 *dwo_unit->section = sections.info_or_types;
57d63ce2 11875 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11876
11877 return dwo_unit;
11878}
11879
d2854d8d
CT
11880/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11881 simplify them. Given a pointer to the containing section SECTION, and
11882 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11883 virtual section of just that piece. */
73869dc2
DE
11884
11885static struct dwarf2_section_info
d2854d8d
CT
11886create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11887 struct dwarf2_section_info *section,
11888 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
11889{
11890 struct dwarf2_section_info result;
11891 asection *sectp;
11892
11893 gdb_assert (section != NULL);
11894 gdb_assert (!section->is_virtual);
11895
11896 memset (&result, 0, sizeof (result));
11897 result.s.containing_section = section;
dc4ccb6f 11898 result.is_virtual = true;
73869dc2
DE
11899
11900 if (size == 0)
11901 return result;
11902
96b79293 11903 sectp = section->get_bfd_section ();
73869dc2
DE
11904
11905 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11906 bounds of the real section. This is a pretty-rare event, so just
11907 flag an error (easier) instead of a warning and trying to cope. */
11908 if (sectp == NULL
fd361982 11909 || offset + size > bfd_section_size (sectp))
73869dc2 11910 {
d2854d8d 11911 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 11912 " in section %s [in module %s]"),
fd361982 11913 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 11914 objfile_name (per_objfile->objfile));
73869dc2
DE
11915 }
11916
11917 result.virtual_offset = offset;
11918 result.size = size;
11919 return result;
11920}
11921
11922/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11923 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11924 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11925 This is for DWP version 2 files. */
11926
11927static struct dwo_unit *
976ca316 11928create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 11929 struct dwp_file *dwp_file,
73869dc2
DE
11930 uint32_t unit_index,
11931 const char *comp_dir,
11932 ULONGEST signature, int is_debug_types)
11933{
73869dc2
DE
11934 const struct dwp_hash_table *dwp_htab =
11935 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11936 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11937 const char *kind = is_debug_types ? "TU" : "CU";
11938 struct dwo_file *dwo_file;
11939 struct dwo_unit *dwo_unit;
d2854d8d 11940 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 11941 void **dwo_file_slot;
73869dc2
DE
11942 int i;
11943
11944 gdb_assert (dwp_file->version == 2);
11945
6f738b01
SM
11946 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11947 kind, pulongest (unit_index), hex_string (signature),
11948 dwp_file->name);
73869dc2
DE
11949
11950 /* Fetch the section offsets of this DWO unit. */
11951
11952 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11953
11954 for (i = 0; i < dwp_htab->nr_columns; ++i)
11955 {
11956 uint32_t offset = read_4_bytes (dbfd,
11957 dwp_htab->section_pool.v2.offsets
11958 + (((unit_index - 1) * dwp_htab->nr_columns
11959 + i)
11960 * sizeof (uint32_t)));
11961 uint32_t size = read_4_bytes (dbfd,
11962 dwp_htab->section_pool.v2.sizes
11963 + (((unit_index - 1) * dwp_htab->nr_columns
11964 + i)
11965 * sizeof (uint32_t)));
11966
11967 switch (dwp_htab->section_pool.v2.section_ids[i])
11968 {
11969 case DW_SECT_INFO:
11970 case DW_SECT_TYPES:
11971 sections.info_or_types_offset = offset;
11972 sections.info_or_types_size = size;
11973 break;
11974 case DW_SECT_ABBREV:
11975 sections.abbrev_offset = offset;
11976 sections.abbrev_size = size;
11977 break;
11978 case DW_SECT_LINE:
11979 sections.line_offset = offset;
11980 sections.line_size = size;
11981 break;
11982 case DW_SECT_LOC:
11983 sections.loc_offset = offset;
11984 sections.loc_size = size;
11985 break;
11986 case DW_SECT_STR_OFFSETS:
11987 sections.str_offsets_offset = offset;
11988 sections.str_offsets_size = size;
11989 break;
11990 case DW_SECT_MACINFO:
11991 sections.macinfo_offset = offset;
11992 sections.macinfo_size = size;
11993 break;
11994 case DW_SECT_MACRO:
11995 sections.macro_offset = offset;
11996 sections.macro_size = size;
11997 break;
11998 }
11999 }
12000
12001 /* It's easier for the rest of the code if we fake a struct dwo_file and
12002 have dwo_unit "live" in that. At least for now.
12003
12004 The DWP file can be made up of a random collection of CUs and TUs.
12005 However, for each CU + set of TUs that came from the same original DWO
12006 file, we can combine them back into a virtual DWO file to save space
12007 (fewer struct dwo_file objects to allocate). Remember that for really
12008 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12009
791afaa2
TT
12010 std::string virtual_dwo_name =
12011 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12012 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12013 (long) (sections.line_size ? sections.line_offset : 0),
12014 (long) (sections.loc_size ? sections.loc_offset : 0),
12015 (long) (sections.str_offsets_size
12016 ? sections.str_offsets_offset : 0));
73869dc2 12017 /* Can we use an existing virtual DWO file? */
976ca316 12018 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12019 comp_dir);
73869dc2
DE
12020 /* Create one if necessary. */
12021 if (*dwo_file_slot == NULL)
12022 {
6f738b01
SM
12023 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12024 virtual_dwo_name.c_str ());
12025
51ac9db5 12026 dwo_file = new struct dwo_file;
976ca316 12027 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12028 dwo_file->comp_dir = comp_dir;
12029 dwo_file->sections.abbrev =
d2854d8d
CT
12030 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12031 sections.abbrev_offset,
12032 sections.abbrev_size);
73869dc2 12033 dwo_file->sections.line =
d2854d8d
CT
12034 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12035 sections.line_offset,
12036 sections.line_size);
73869dc2 12037 dwo_file->sections.loc =
d2854d8d
CT
12038 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12039 sections.loc_offset, sections.loc_size);
73869dc2 12040 dwo_file->sections.macinfo =
d2854d8d
CT
12041 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12042 sections.macinfo_offset,
12043 sections.macinfo_size);
73869dc2 12044 dwo_file->sections.macro =
d2854d8d
CT
12045 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12046 sections.macro_offset,
12047 sections.macro_size);
73869dc2 12048 dwo_file->sections.str_offsets =
d2854d8d
CT
12049 create_dwp_v2_or_v5_section (per_objfile,
12050 &dwp_file->sections.str_offsets,
12051 sections.str_offsets_offset,
12052 sections.str_offsets_size);
73869dc2
DE
12053 /* The "str" section is global to the entire DWP file. */
12054 dwo_file->sections.str = dwp_file->sections.str;
12055 /* The info or types section is assigned below to dwo_unit,
12056 there's no need to record it in dwo_file.
12057 Also, we can't simply record type sections in dwo_file because
12058 we record a pointer into the vector in dwo_unit. As we collect more
12059 types we'll grow the vector and eventually have to reallocate space
12060 for it, invalidating all copies of pointers into the previous
12061 contents. */
12062 *dwo_file_slot = dwo_file;
12063 }
12064 else
12065 {
6f738b01
SM
12066 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12067 virtual_dwo_name.c_str ());
12068
9a3c8263 12069 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12070 }
73869dc2 12071
976ca316 12072 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12073 dwo_unit->dwo_file = dwo_file;
12074 dwo_unit->signature = signature;
8d749320 12075 dwo_unit->section =
976ca316 12076 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12077 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12078 (per_objfile,
d2854d8d
CT
12079 is_debug_types
12080 ? &dwp_file->sections.types
12081 : &dwp_file->sections.info,
12082 sections.info_or_types_offset,
12083 sections.info_or_types_size);
12084 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12085
12086 return dwo_unit;
12087}
12088
12089/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12090 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12091 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12092 This is for DWP version 5 files. */
12093
12094static struct dwo_unit *
12095create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12096 struct dwp_file *dwp_file,
12097 uint32_t unit_index,
12098 const char *comp_dir,
12099 ULONGEST signature, int is_debug_types)
12100{
12101 const struct dwp_hash_table *dwp_htab
12102 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12103 bfd *dbfd = dwp_file->dbfd.get ();
12104 const char *kind = is_debug_types ? "TU" : "CU";
12105 struct dwo_file *dwo_file;
12106 struct dwo_unit *dwo_unit;
12107 struct virtual_v2_or_v5_dwo_sections sections {};
12108 void **dwo_file_slot;
12109
12110 gdb_assert (dwp_file->version == 5);
12111
6f738b01
SM
12112 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12113 kind, pulongest (unit_index), hex_string (signature),
12114 dwp_file->name);
d2854d8d
CT
12115
12116 /* Fetch the section offsets of this DWO unit. */
12117
12118 /* memset (&sections, 0, sizeof (sections)); */
12119
12120 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12121 {
12122 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12123 dwp_htab->section_pool.v5.offsets
12124 + (((unit_index - 1)
12125 * dwp_htab->nr_columns
12126 + i)
12127 * sizeof (uint32_t)));
d2854d8d 12128 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12129 dwp_htab->section_pool.v5.sizes
12130 + (((unit_index - 1) * dwp_htab->nr_columns
12131 + i)
12132 * sizeof (uint32_t)));
d2854d8d
CT
12133
12134 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12135 {
12136 case DW_SECT_ABBREV_V5:
12137 sections.abbrev_offset = offset;
12138 sections.abbrev_size = size;
12139 break;
12140 case DW_SECT_INFO_V5:
12141 sections.info_or_types_offset = offset;
12142 sections.info_or_types_size = size;
12143 break;
12144 case DW_SECT_LINE_V5:
12145 sections.line_offset = offset;
12146 sections.line_size = size;
12147 break;
12148 case DW_SECT_LOCLISTS_V5:
12149 sections.loclists_offset = offset;
12150 sections.loclists_size = size;
12151 break;
12152 case DW_SECT_MACRO_V5:
12153 sections.macro_offset = offset;
12154 sections.macro_size = size;
12155 break;
12156 case DW_SECT_RNGLISTS_V5:
12157 sections.rnglists_offset = offset;
12158 sections.rnglists_size = size;
12159 break;
12160 case DW_SECT_STR_OFFSETS_V5:
12161 sections.str_offsets_offset = offset;
12162 sections.str_offsets_size = size;
12163 break;
12164 case DW_SECT_RESERVED_V5:
12165 default:
12166 break;
12167 }
d2854d8d
CT
12168 }
12169
12170 /* It's easier for the rest of the code if we fake a struct dwo_file and
12171 have dwo_unit "live" in that. At least for now.
12172
12173 The DWP file can be made up of a random collection of CUs and TUs.
12174 However, for each CU + set of TUs that came from the same original DWO
12175 file, we can combine them back into a virtual DWO file to save space
12176 (fewer struct dwo_file objects to allocate). Remember that for really
12177 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12178
12179 std::string virtual_dwo_name =
12180 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12181 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12182 (long) (sections.line_size ? sections.line_offset : 0),
12183 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12184 (long) (sections.str_offsets_size
12185 ? sections.str_offsets_offset : 0),
12186 (long) (sections.macro_size ? sections.macro_offset : 0),
12187 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12188 /* Can we use an existing virtual DWO file? */
12189 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12190 virtual_dwo_name.c_str (),
12191 comp_dir);
d2854d8d
CT
12192 /* Create one if necessary. */
12193 if (*dwo_file_slot == NULL)
12194 {
6f738b01
SM
12195 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12196 virtual_dwo_name.c_str ());
12197
d2854d8d
CT
12198 dwo_file = new struct dwo_file;
12199 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12200 dwo_file->comp_dir = comp_dir;
12201 dwo_file->sections.abbrev =
dda83cd7
SM
12202 create_dwp_v2_or_v5_section (per_objfile,
12203 &dwp_file->sections.abbrev,
12204 sections.abbrev_offset,
12205 sections.abbrev_size);
d2854d8d 12206 dwo_file->sections.line =
dda83cd7
SM
12207 create_dwp_v2_or_v5_section (per_objfile,
12208 &dwp_file->sections.line,
12209 sections.line_offset, sections.line_size);
d2854d8d 12210 dwo_file->sections.macro =
dda83cd7
SM
12211 create_dwp_v2_or_v5_section (per_objfile,
12212 &dwp_file->sections.macro,
12213 sections.macro_offset,
12214 sections.macro_size);
d2854d8d 12215 dwo_file->sections.loclists =
dda83cd7
SM
12216 create_dwp_v2_or_v5_section (per_objfile,
12217 &dwp_file->sections.loclists,
12218 sections.loclists_offset,
12219 sections.loclists_size);
d2854d8d 12220 dwo_file->sections.rnglists =
dda83cd7
SM
12221 create_dwp_v2_or_v5_section (per_objfile,
12222 &dwp_file->sections.rnglists,
12223 sections.rnglists_offset,
12224 sections.rnglists_size);
d2854d8d 12225 dwo_file->sections.str_offsets =
dda83cd7
SM
12226 create_dwp_v2_or_v5_section (per_objfile,
12227 &dwp_file->sections.str_offsets,
12228 sections.str_offsets_offset,
12229 sections.str_offsets_size);
d2854d8d
CT
12230 /* The "str" section is global to the entire DWP file. */
12231 dwo_file->sections.str = dwp_file->sections.str;
12232 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12233 there's no need to record it in dwo_file.
12234 Also, we can't simply record type sections in dwo_file because
12235 we record a pointer into the vector in dwo_unit. As we collect more
12236 types we'll grow the vector and eventually have to reallocate space
12237 for it, invalidating all copies of pointers into the previous
12238 contents. */
d2854d8d
CT
12239 *dwo_file_slot = dwo_file;
12240 }
12241 else
12242 {
6f738b01
SM
12243 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12244 virtual_dwo_name.c_str ());
12245
d2854d8d
CT
12246 dwo_file = (struct dwo_file *) *dwo_file_slot;
12247 }
12248
12249 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12250 dwo_unit->dwo_file = dwo_file;
12251 dwo_unit->signature = signature;
12252 dwo_unit->section
12253 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12254 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12255 &dwp_file->sections.info,
12256 sections.info_or_types_offset,
12257 sections.info_or_types_size);
73869dc2
DE
12258 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12259
12260 return dwo_unit;
12261}
12262
57d63ce2
DE
12263/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12264 Returns NULL if the signature isn't found. */
80626a55
DE
12265
12266static struct dwo_unit *
976ca316 12267lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12268 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12269 ULONGEST signature, int is_debug_types)
80626a55 12270{
57d63ce2
DE
12271 const struct dwp_hash_table *dwp_htab =
12272 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12273 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12274 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12275 uint32_t hash = signature & mask;
12276 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12277 unsigned int i;
12278 void **slot;
870f88f7 12279 struct dwo_unit find_dwo_cu;
80626a55
DE
12280
12281 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12282 find_dwo_cu.signature = signature;
19ac8c2e 12283 slot = htab_find_slot (is_debug_types
48b490f2
TT
12284 ? dwp_file->loaded_tus.get ()
12285 : dwp_file->loaded_cus.get (),
19ac8c2e 12286 &find_dwo_cu, INSERT);
80626a55
DE
12287
12288 if (*slot != NULL)
9a3c8263 12289 return (struct dwo_unit *) *slot;
80626a55
DE
12290
12291 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12292 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12293 {
12294 ULONGEST signature_in_table;
12295
12296 signature_in_table =
57d63ce2 12297 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12298 if (signature_in_table == signature)
12299 {
57d63ce2
DE
12300 uint32_t unit_index =
12301 read_4_bytes (dbfd,
12302 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12303
73869dc2
DE
12304 if (dwp_file->version == 1)
12305 {
976ca316
SM
12306 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12307 unit_index, comp_dir,
12308 signature, is_debug_types);
73869dc2 12309 }
d2854d8d 12310 else if (dwp_file->version == 2)
73869dc2 12311 {
976ca316
SM
12312 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12313 unit_index, comp_dir,
12314 signature, is_debug_types);
73869dc2 12315 }
d2854d8d
CT
12316 else /* version == 5 */
12317 {
12318 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12319 unit_index, comp_dir,
12320 signature, is_debug_types);
12321 }
9a3c8263 12322 return (struct dwo_unit *) *slot;
80626a55
DE
12323 }
12324 if (signature_in_table == 0)
12325 return NULL;
12326 hash = (hash + hash2) & mask;
12327 }
12328
12329 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12330 " [in module %s]"),
12331 dwp_file->name);
12332}
12333
ab5088bf 12334/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12335 Open the file specified by FILE_NAME and hand it off to BFD for
12336 preliminary analysis. Return a newly initialized bfd *, which
12337 includes a canonicalized copy of FILE_NAME.
80626a55 12338 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12339 SEARCH_CWD is true if the current directory is to be searched.
12340 It will be searched before debug-file-directory.
13aaf454
DE
12341 If successful, the file is added to the bfd include table of the
12342 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12343 If unable to find/open the file, return NULL.
3019eac3
DE
12344 NOTE: This function is derived from symfile_bfd_open. */
12345
192b62ce 12346static gdb_bfd_ref_ptr
976ca316 12347try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12348 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12349{
24b9144d 12350 int desc;
9c02c129
DE
12351 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12352 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12353 to debug_file_directory. */
e0cc99a6 12354 const char *search_path;
9c02c129
DE
12355 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12356
e0cc99a6 12357 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12358 if (search_cwd)
12359 {
12360 if (*debug_file_directory != '\0')
e0cc99a6
TT
12361 {
12362 search_path_holder.reset (concat (".", dirname_separator_string,
12363 debug_file_directory,
12364 (char *) NULL));
12365 search_path = search_path_holder.get ();
12366 }
6ac97d4c 12367 else
e0cc99a6 12368 search_path = ".";
6ac97d4c 12369 }
9c02c129 12370 else
e0cc99a6 12371 search_path = debug_file_directory;
3019eac3 12372
56d467f4
CT
12373 /* Add the path for the executable binary to the list of search paths. */
12374 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12375 search_path_holder.reset (concat (objfile_dir.c_str (),
12376 dirname_separator_string,
12377 search_path, nullptr));
12378 search_path = search_path_holder.get ();
12379
24b9144d 12380 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12381 if (is_dwp)
12382 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12383
12384 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12385 desc = openp (search_path, flags, file_name,
3019eac3
DE
12386 O_RDONLY | O_BINARY, &absolute_name);
12387 if (desc < 0)
12388 return NULL;
12389
e0cc99a6
TT
12390 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12391 gnutarget, desc));
9c02c129
DE
12392 if (sym_bfd == NULL)
12393 return NULL;
192b62ce 12394 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12395
192b62ce
TT
12396 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12397 return NULL;
3019eac3 12398
13aaf454
DE
12399 /* Success. Record the bfd as having been included by the objfile's bfd.
12400 This is important because things like demangled_names_hash lives in the
12401 objfile's per_bfd space and may have references to things like symbol
12402 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12403 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12404
3019eac3
DE
12405 return sym_bfd;
12406}
12407
ab5088bf 12408/* Try to open DWO file FILE_NAME.
3019eac3
DE
12409 COMP_DIR is the DW_AT_comp_dir attribute.
12410 The result is the bfd handle of the file.
12411 If there is a problem finding or opening the file, return NULL.
12412 Upon success, the canonicalized path of the file is stored in the bfd,
12413 same as symfile_bfd_open. */
12414
192b62ce 12415static gdb_bfd_ref_ptr
976ca316 12416open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12417 const char *file_name, const char *comp_dir)
3019eac3 12418{
80626a55 12419 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12420 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12421 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12422
12423 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12424
12425 if (comp_dir != NULL)
12426 {
43816ebc
TT
12427 gdb::unique_xmalloc_ptr<char> path_to_try
12428 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12429
12430 /* NOTE: If comp_dir is a relative path, this will also try the
12431 search path, which seems useful. */
976ca316 12432 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12433 0 /*is_dwp*/,
192b62ce 12434 1 /*search_cwd*/));
3019eac3
DE
12435 if (abfd != NULL)
12436 return abfd;
12437 }
12438
12439 /* That didn't work, try debug-file-directory, which, despite its name,
12440 is a list of paths. */
12441
12442 if (*debug_file_directory == '\0')
12443 return NULL;
12444
976ca316 12445 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12446 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12447}
12448
80626a55
DE
12449/* This function is mapped across the sections and remembers the offset and
12450 size of each of the DWO debugging sections we are interested in. */
12451
12452static void
5bb6e9dd
TT
12453dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12454 dwo_sections *dwo_sections)
80626a55 12455{
80626a55
DE
12456 const struct dwop_section_names *names = &dwop_section_names;
12457
fbedd546 12458 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12459 {
049412e3 12460 dwo_sections->abbrev.s.section = sectp;
fd361982 12461 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12462 }
fbedd546 12463 else if (names->info_dwo.matches (sectp->name))
80626a55 12464 {
049412e3 12465 dwo_sections->info.s.section = sectp;
fd361982 12466 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12467 }
fbedd546 12468 else if (names->line_dwo.matches (sectp->name))
80626a55 12469 {
049412e3 12470 dwo_sections->line.s.section = sectp;
fd361982 12471 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12472 }
fbedd546 12473 else if (names->loc_dwo.matches (sectp->name))
80626a55 12474 {
049412e3 12475 dwo_sections->loc.s.section = sectp;
fd361982 12476 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12477 }
fbedd546 12478 else if (names->loclists_dwo.matches (sectp->name))
41144253 12479 {
12480 dwo_sections->loclists.s.section = sectp;
12481 dwo_sections->loclists.size = bfd_section_size (sectp);
12482 }
fbedd546 12483 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12484 {
049412e3 12485 dwo_sections->macinfo.s.section = sectp;
fd361982 12486 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12487 }
fbedd546 12488 else if (names->macro_dwo.matches (sectp->name))
80626a55 12489 {
049412e3 12490 dwo_sections->macro.s.section = sectp;
fd361982 12491 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12492 }
fbedd546 12493 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12494 {
12495 dwo_sections->rnglists.s.section = sectp;
12496 dwo_sections->rnglists.size = bfd_section_size (sectp);
12497 }
fbedd546 12498 else if (names->str_dwo.matches (sectp->name))
80626a55 12499 {
049412e3 12500 dwo_sections->str.s.section = sectp;
fd361982 12501 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12502 }
fbedd546 12503 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12504 {
049412e3 12505 dwo_sections->str_offsets.s.section = sectp;
fd361982 12506 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12507 }
fbedd546 12508 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12509 {
12510 struct dwarf2_section_info type_section;
12511
12512 memset (&type_section, 0, sizeof (type_section));
049412e3 12513 type_section.s.section = sectp;
fd361982 12514 type_section.size = bfd_section_size (sectp);
fd5866f6 12515 dwo_sections->types.push_back (type_section);
80626a55
DE
12516 }
12517}
12518
ab5088bf 12519/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12520 by PER_CU. This is for the non-DWP case.
80626a55 12521 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12522
12523static struct dwo_file *
4ab09049
SM
12524open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12525 const char *comp_dir)
3019eac3 12526{
976ca316 12527 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12528
976ca316 12529 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12530 if (dbfd == NULL)
12531 {
6f738b01
SM
12532 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12533
80626a55
DE
12534 return NULL;
12535 }
263db9a1 12536
51ac9db5 12537 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12538 dwo_file->dwo_name = dwo_name;
12539 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12540 dwo_file->dbfd = std::move (dbfd);
3019eac3 12541
5bb6e9dd
TT
12542 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12543 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12544 &dwo_file->sections);
3019eac3 12545
976ca316
SM
12546 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12547 dwo_file->cus);
3019eac3 12548
d2854d8d
CT
12549 if (cu->per_cu->dwarf_version < 5)
12550 {
12551 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12552 dwo_file->sections.types, dwo_file->tus);
12553 }
12554 else
12555 {
12556 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12557 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12558 rcuh_kind::COMPILE);
d2854d8d 12559 }
3019eac3 12560
6f738b01 12561 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12562
263db9a1 12563 return dwo_file.release ();
3019eac3
DE
12564}
12565
80626a55 12566/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12567 size of each of the DWP debugging sections common to version 1 and 2 that
12568 we are interested in. */
3019eac3 12569
80626a55 12570static void
73869dc2 12571dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12572 dwp_file *dwp_file)
3019eac3 12573{
80626a55
DE
12574 const struct dwop_section_names *names = &dwop_section_names;
12575 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12576
80626a55 12577 /* Record the ELF section number for later lookup: this is what the
73869dc2 12578 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12579 gdb_assert (elf_section_nr < dwp_file->num_sections);
12580 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12581
80626a55 12582 /* Look for specific sections that we need. */
fbedd546 12583 if (names->str_dwo.matches (sectp->name))
80626a55 12584 {
049412e3 12585 dwp_file->sections.str.s.section = sectp;
fd361982 12586 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12587 }
fbedd546 12588 else if (names->cu_index.matches (sectp->name))
80626a55 12589 {
049412e3 12590 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12591 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12592 }
fbedd546 12593 else if (names->tu_index.matches (sectp->name))
80626a55 12594 {
049412e3 12595 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12596 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12597 }
12598}
3019eac3 12599
73869dc2
DE
12600/* This function is mapped across the sections and remembers the offset and
12601 size of each of the DWP version 2 debugging sections that we are interested
12602 in. This is split into a separate function because we don't know if we
d2854d8d 12603 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12604
12605static void
12606dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12607{
9a3c8263 12608 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12609 const struct dwop_section_names *names = &dwop_section_names;
12610 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12611
12612 /* Record the ELF section number for later lookup: this is what the
12613 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12614 gdb_assert (elf_section_nr < dwp_file->num_sections);
12615 dwp_file->elf_sections[elf_section_nr] = sectp;
12616
12617 /* Look for specific sections that we need. */
fbedd546 12618 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12619 {
049412e3 12620 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12621 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12622 }
fbedd546 12623 else if (names->info_dwo.matches (sectp->name))
73869dc2 12624 {
049412e3 12625 dwp_file->sections.info.s.section = sectp;
fd361982 12626 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12627 }
fbedd546 12628 else if (names->line_dwo.matches (sectp->name))
73869dc2 12629 {
049412e3 12630 dwp_file->sections.line.s.section = sectp;
fd361982 12631 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12632 }
fbedd546 12633 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12634 {
049412e3 12635 dwp_file->sections.loc.s.section = sectp;
fd361982 12636 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12637 }
fbedd546 12638 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12639 {
049412e3 12640 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12641 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12642 }
fbedd546 12643 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12644 {
049412e3 12645 dwp_file->sections.macro.s.section = sectp;
fd361982 12646 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12647 }
fbedd546 12648 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12649 {
049412e3 12650 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12651 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12652 }
fbedd546 12653 else if (names->types_dwo.matches (sectp->name))
73869dc2 12654 {
049412e3 12655 dwp_file->sections.types.s.section = sectp;
fd361982 12656 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12657 }
12658}
12659
d2854d8d
CT
12660/* This function is mapped across the sections and remembers the offset and
12661 size of each of the DWP version 5 debugging sections that we are interested
12662 in. This is split into a separate function because we don't know if we
12663 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12664
12665static void
12666dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12667{
12668 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12669 const struct dwop_section_names *names = &dwop_section_names;
12670 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12671
12672 /* Record the ELF section number for later lookup: this is what the
12673 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12674 gdb_assert (elf_section_nr < dwp_file->num_sections);
12675 dwp_file->elf_sections[elf_section_nr] = sectp;
12676
12677 /* Look for specific sections that we need. */
fbedd546 12678 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12679 {
12680 dwp_file->sections.abbrev.s.section = sectp;
12681 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12682 }
fbedd546 12683 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12684 {
12685 dwp_file->sections.info.s.section = sectp;
12686 dwp_file->sections.info.size = bfd_section_size (sectp);
12687 }
fbedd546 12688 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12689 {
12690 dwp_file->sections.line.s.section = sectp;
12691 dwp_file->sections.line.size = bfd_section_size (sectp);
12692 }
fbedd546 12693 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
12694 {
12695 dwp_file->sections.loclists.s.section = sectp;
12696 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12697 }
fbedd546 12698 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
12699 {
12700 dwp_file->sections.macro.s.section = sectp;
12701 dwp_file->sections.macro.size = bfd_section_size (sectp);
12702 }
fbedd546 12703 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
12704 {
12705 dwp_file->sections.rnglists.s.section = sectp;
12706 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12707 }
fbedd546 12708 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
12709 {
12710 dwp_file->sections.str_offsets.s.section = sectp;
12711 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12712 }
12713}
12714
80626a55 12715/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12716
80626a55
DE
12717static hashval_t
12718hash_dwp_loaded_cutus (const void *item)
12719{
9a3c8263 12720 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12721
80626a55
DE
12722 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12723 return dwo_unit->signature;
3019eac3
DE
12724}
12725
80626a55 12726/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12727
80626a55
DE
12728static int
12729eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12730{
9a3c8263
SM
12731 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12732 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12733
80626a55
DE
12734 return dua->signature == dub->signature;
12735}
3019eac3 12736
80626a55 12737/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12738
48b490f2 12739static htab_up
298e9637 12740allocate_dwp_loaded_cutus_table ()
80626a55 12741{
48b490f2
TT
12742 return htab_up (htab_create_alloc (3,
12743 hash_dwp_loaded_cutus,
12744 eq_dwp_loaded_cutus,
12745 NULL, xcalloc, xfree));
80626a55 12746}
3019eac3 12747
ab5088bf
DE
12748/* Try to open DWP file FILE_NAME.
12749 The result is the bfd handle of the file.
12750 If there is a problem finding or opening the file, return NULL.
12751 Upon success, the canonicalized path of the file is stored in the bfd,
12752 same as symfile_bfd_open. */
12753
192b62ce 12754static gdb_bfd_ref_ptr
976ca316 12755open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12756{
976ca316 12757 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12758 1 /*is_dwp*/,
192b62ce 12759 1 /*search_cwd*/));
6ac97d4c
DE
12760 if (abfd != NULL)
12761 return abfd;
12762
12763 /* Work around upstream bug 15652.
12764 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12765 [Whether that's a "bug" is debatable, but it is getting in our way.]
12766 We have no real idea where the dwp file is, because gdb's realpath-ing
12767 of the executable's path may have discarded the needed info.
12768 [IWBN if the dwp file name was recorded in the executable, akin to
12769 .gnu_debuglink, but that doesn't exist yet.]
12770 Strip the directory from FILE_NAME and search again. */
12771 if (*debug_file_directory != '\0')
12772 {
12773 /* Don't implicitly search the current directory here.
12774 If the user wants to search "." to handle this case,
12775 it must be added to debug-file-directory. */
976ca316
SM
12776 return try_open_dwop_file (per_objfile, lbasename (file_name),
12777 1 /*is_dwp*/,
6ac97d4c
DE
12778 0 /*search_cwd*/);
12779 }
12780
12781 return NULL;
ab5088bf
DE
12782}
12783
80626a55
DE
12784/* Initialize the use of the DWP file for the current objfile.
12785 By convention the name of the DWP file is ${objfile}.dwp.
12786 The result is NULL if it can't be found. */
a766d390 12787
400174b1 12788static std::unique_ptr<struct dwp_file>
976ca316 12789open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12790{
976ca316 12791 struct objfile *objfile = per_objfile->objfile;
80626a55 12792
82bf32bc
JK
12793 /* Try to find first .dwp for the binary file before any symbolic links
12794 resolving. */
6c447423
DE
12795
12796 /* If the objfile is a debug file, find the name of the real binary
12797 file and get the name of dwp file from there. */
d721ba37 12798 std::string dwp_name;
6c447423
DE
12799 if (objfile->separate_debug_objfile_backlink != NULL)
12800 {
12801 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12802 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12803
d721ba37 12804 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12805 }
12806 else
d721ba37
PA
12807 dwp_name = objfile->original_name;
12808
12809 dwp_name += ".dwp";
80626a55 12810
976ca316 12811 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12812 if (dbfd == NULL
12813 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12814 {
12815 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12816 dwp_name = objfile_name (objfile);
12817 dwp_name += ".dwp";
976ca316 12818 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12819 }
12820
80626a55
DE
12821 if (dbfd == NULL)
12822 {
6f738b01
SM
12823 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12824
400174b1 12825 return std::unique_ptr<dwp_file> ();
3019eac3 12826 }
400174b1
TT
12827
12828 const char *name = bfd_get_filename (dbfd.get ());
12829 std::unique_ptr<struct dwp_file> dwp_file
12830 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12831
0a0f4c01 12832 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12833 dwp_file->elf_sections =
976ca316 12834 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12835 dwp_file->num_sections, asection *);
12836
5bb6e9dd
TT
12837 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12838 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12839 dwp_file.get ());
80626a55 12840
976ca316 12841 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12842
976ca316 12843 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12844
73869dc2 12845 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12846 if (dwp_file->cus && dwp_file->tus
12847 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12848 {
12849 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12850 pretty bizarre. We use pulongest here because that's the established
4d65956b 12851 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12852 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12853 " TU version %s [in DWP file %s]"),
12854 pulongest (dwp_file->cus->version),
d721ba37 12855 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12856 }
08302ed2
DE
12857
12858 if (dwp_file->cus)
12859 dwp_file->version = dwp_file->cus->version;
12860 else if (dwp_file->tus)
12861 dwp_file->version = dwp_file->tus->version;
12862 else
12863 dwp_file->version = 2;
73869dc2 12864
5bb6e9dd
TT
12865 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12866 {
12867 if (dwp_file->version == 2)
12868 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12869 dwp_file.get ());
12870 else
12871 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12872 dwp_file.get ());
12873 }
73869dc2 12874
298e9637
SM
12875 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12876 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12877
6f738b01
SM
12878 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12879 dwarf_read_debug_printf (" %s CUs, %s TUs",
12880 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12881 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12882
12883 return dwp_file;
3019eac3 12884}
c906108c 12885
ab5088bf
DE
12886/* Wrapper around open_and_init_dwp_file, only open it once. */
12887
12888static struct dwp_file *
976ca316 12889get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12890{
976ca316 12891 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12892 {
976ca316
SM
12893 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12894 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12895 }
976ca316 12896 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12897}
12898
80626a55
DE
12899/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12900 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12901 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12902 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12903 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12904
12905 This is called, for example, when wanting to read a variable with a
12906 complex location. Therefore we don't want to do file i/o for every call.
12907 Therefore we don't want to look for a DWO file on every call.
12908 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12909 then we check if we've already seen DWO_NAME, and only THEN do we check
12910 for a DWO file.
12911
1c658ad5 12912 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12913 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12914
3019eac3 12915static struct dwo_unit *
4ab09049 12916lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12917 ULONGEST signature, int is_debug_types)
3019eac3 12918{
976ca316
SM
12919 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12920 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12921 const char *kind = is_debug_types ? "TU" : "CU";
12922 void **dwo_file_slot;
3019eac3 12923 struct dwo_file *dwo_file;
80626a55 12924 struct dwp_file *dwp_file;
cb1df416 12925
6a506a2d
DE
12926 /* First see if there's a DWP file.
12927 If we have a DWP file but didn't find the DWO inside it, don't
12928 look for the original DWO file. It makes gdb behave differently
12929 depending on whether one is debugging in the build tree. */
cf2c3c16 12930
976ca316 12931 dwp_file = get_dwp_file (per_objfile);
80626a55 12932 if (dwp_file != NULL)
cf2c3c16 12933 {
80626a55
DE
12934 const struct dwp_hash_table *dwp_htab =
12935 is_debug_types ? dwp_file->tus : dwp_file->cus;
12936
12937 if (dwp_htab != NULL)
12938 {
12939 struct dwo_unit *dwo_cutu =
976ca316
SM
12940 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12941 is_debug_types);
80626a55
DE
12942
12943 if (dwo_cutu != NULL)
12944 {
6f738b01
SM
12945 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12946 kind, hex_string (signature),
12947 host_address_to_string (dwo_cutu));
12948
80626a55
DE
12949 return dwo_cutu;
12950 }
12951 }
12952 }
6a506a2d 12953 else
80626a55 12954 {
6a506a2d 12955 /* No DWP file, look for the DWO file. */
80626a55 12956
976ca316 12957 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12958 if (*dwo_file_slot == NULL)
80626a55 12959 {
6a506a2d 12960 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12961 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12962 }
6a506a2d 12963 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12964 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12965
6a506a2d 12966 if (dwo_file != NULL)
19c3d4c9 12967 {
6a506a2d
DE
12968 struct dwo_unit *dwo_cutu = NULL;
12969
12970 if (is_debug_types && dwo_file->tus)
12971 {
12972 struct dwo_unit find_dwo_cutu;
12973
12974 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12975 find_dwo_cutu.signature = signature;
9a3c8263 12976 dwo_cutu
b0b6a987
TT
12977 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12978 &find_dwo_cutu);
6a506a2d 12979 }
33c5cd75 12980 else if (!is_debug_types && dwo_file->cus)
80626a55 12981 {
33c5cd75
DB
12982 struct dwo_unit find_dwo_cutu;
12983
12984 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12985 find_dwo_cutu.signature = signature;
b0b6a987 12986 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12987 &find_dwo_cutu);
6a506a2d
DE
12988 }
12989
12990 if (dwo_cutu != NULL)
12991 {
6f738b01
SM
12992 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12993 kind, dwo_name, hex_string (signature),
12994 host_address_to_string (dwo_cutu));
12995
6a506a2d 12996 return dwo_cutu;
80626a55
DE
12997 }
12998 }
2e276125 12999 }
9cdd5dbd 13000
80626a55
DE
13001 /* We didn't find it. This could mean a dwo_id mismatch, or
13002 someone deleted the DWO/DWP file, or the search path isn't set up
13003 correctly to find the file. */
13004
6f738b01
SM
13005 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13006 kind, dwo_name, hex_string (signature));
3019eac3 13007
6656a72d
DE
13008 /* This is a warning and not a complaint because it can be caused by
13009 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13010 {
13011 /* Print the name of the DWP file if we looked there, helps the user
13012 better diagnose the problem. */
791afaa2 13013 std::string dwp_text;
43942612
DE
13014
13015 if (dwp_file != NULL)
791afaa2
TT
13016 dwp_text = string_printf (" [in DWP file %s]",
13017 lbasename (dwp_file->name));
43942612 13018
9d8780f0 13019 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13020 " [in module %s]"),
4ab09049
SM
13021 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13022 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13023 }
3019eac3 13024 return NULL;
5fb290d7
DJ
13025}
13026
80626a55
DE
13027/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13028 See lookup_dwo_cutu_unit for details. */
13029
13030static struct dwo_unit *
4ab09049 13031lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13032 ULONGEST signature)
13033{
4ab09049
SM
13034 gdb_assert (!cu->per_cu->is_debug_types);
13035
13036 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13037}
13038
13039/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13040 See lookup_dwo_cutu_unit for details. */
13041
13042static struct dwo_unit *
4ab09049 13043lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13044{
4ab09049
SM
13045 gdb_assert (cu->per_cu->is_debug_types);
13046
13047 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13048
13049 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13050}
13051
89e63ee4
DE
13052/* Traversal function for queue_and_load_all_dwo_tus. */
13053
13054static int
13055queue_and_load_dwo_tu (void **slot, void *info)
13056{
13057 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13058 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13059 ULONGEST signature = dwo_unit->signature;
d460f660 13060 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13061
13062 if (sig_type != NULL)
13063 {
13064 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13065
13066 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13067 a real dependency of PER_CU on SIG_TYPE. That is detected later
13068 while processing PER_CU. */
120ce1b5 13069 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13070 load_full_type_unit (sig_cu, cu->per_objfile);
13071 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13072 }
13073
13074 return 1;
13075}
13076
1b555f17 13077/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13078 The DWO may have the only definition of the type, though it may not be
13079 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13080 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13081
13082static void
1b555f17 13083queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13084{
13085 struct dwo_unit *dwo_unit;
13086 struct dwo_file *dwo_file;
13087
1b555f17
SM
13088 gdb_assert (cu != nullptr);
13089 gdb_assert (!cu->per_cu->is_debug_types);
13090 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13091
1b555f17 13092 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13093 gdb_assert (dwo_unit != NULL);
13094
13095 dwo_file = dwo_unit->dwo_file;
13096 if (dwo_file->tus != NULL)
1b555f17 13097 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13098}
13099
3019eac3 13100/* Read in various DIEs. */
348e048f 13101
d389af10 13102/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13103 Inherit only the children of the DW_AT_abstract_origin DIE not being
13104 already referenced by DW_AT_abstract_origin from the children of the
13105 current DIE. */
d389af10
JK
13106
13107static void
13108inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13109{
13110 struct die_info *child_die;
791afaa2 13111 sect_offset *offsetp;
d389af10
JK
13112 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13113 struct die_info *origin_die;
13114 /* Iterator of the ORIGIN_DIE children. */
13115 struct die_info *origin_child_die;
d389af10 13116 struct attribute *attr;
cd02d79d
PA
13117 struct dwarf2_cu *origin_cu;
13118 struct pending **origin_previous_list_in_scope;
d389af10
JK
13119
13120 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13121 if (!attr)
13122 return;
13123
cd02d79d
PA
13124 /* Note that following die references may follow to a die in a
13125 different cu. */
13126
13127 origin_cu = cu;
13128 origin_die = follow_die_ref (die, attr, &origin_cu);
13129
13130 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13131 symbols in. */
13132 origin_previous_list_in_scope = origin_cu->list_in_scope;
13133 origin_cu->list_in_scope = cu->list_in_scope;
13134
edb3359d
DJ
13135 if (die->tag != origin_die->tag
13136 && !(die->tag == DW_TAG_inlined_subroutine
13137 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13138 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13139 sect_offset_str (die->sect_off),
13140 sect_offset_str (origin_die->sect_off));
d389af10 13141
34dc0f95
TBA
13142 /* Find if the concrete and abstract trees are structurally the
13143 same. This is a shallow traversal and it is not bullet-proof;
13144 the compiler can trick the debugger into believing that the trees
13145 are isomorphic, whereas they actually are not. However, the
13146 likelyhood of this happening is pretty low, and a full-fledged
13147 check would be an overkill. */
13148 bool are_isomorphic = true;
13149 die_info *concrete_child = die->child;
13150 die_info *abstract_child = origin_die->child;
13151 while (concrete_child != nullptr || abstract_child != nullptr)
13152 {
13153 if (concrete_child == nullptr
13154 || abstract_child == nullptr
13155 || concrete_child->tag != abstract_child->tag)
13156 {
13157 are_isomorphic = false;
13158 break;
13159 }
13160
13161 concrete_child = concrete_child->sibling;
13162 abstract_child = abstract_child->sibling;
13163 }
13164
13165 /* Walk the origin's children in parallel to the concrete children.
13166 This helps match an origin child in case the debug info misses
13167 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13168 origin tree may not have the same tree structure as the concrete
13169 DIE, though. */
13170 die_info *corresponding_abstract_child
13171 = are_isomorphic ? origin_die->child : nullptr;
13172
791afaa2 13173 std::vector<sect_offset> offsets;
d389af10 13174
3ea89b92
PMR
13175 for (child_die = die->child;
13176 child_die && child_die->tag;
436c571c 13177 child_die = child_die->sibling)
3ea89b92
PMR
13178 {
13179 struct die_info *child_origin_die;
13180 struct dwarf2_cu *child_origin_cu;
13181
13182 /* We are trying to process concrete instance entries:
216f72a1 13183 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13184 it's not relevant to our analysis here. i.e. detecting DIEs that are
13185 present in the abstract instance but not referenced in the concrete
13186 one. */
216f72a1 13187 if (child_die->tag == DW_TAG_call_site
dda83cd7 13188 || child_die->tag == DW_TAG_GNU_call_site)
34dc0f95
TBA
13189 {
13190 if (are_isomorphic)
13191 corresponding_abstract_child
13192 = corresponding_abstract_child->sibling;
13193 continue;
13194 }
3ea89b92 13195
c38f313d
DJ
13196 /* For each CHILD_DIE, find the corresponding child of
13197 ORIGIN_DIE. If there is more than one layer of
13198 DW_AT_abstract_origin, follow them all; there shouldn't be,
13199 but GCC versions at least through 4.4 generate this (GCC PR
13200 40573). */
3ea89b92
PMR
13201 child_origin_die = child_die;
13202 child_origin_cu = cu;
c38f313d
DJ
13203 while (1)
13204 {
cd02d79d
PA
13205 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13206 child_origin_cu);
c38f313d
DJ
13207 if (attr == NULL)
13208 break;
cd02d79d
PA
13209 child_origin_die = follow_die_ref (child_origin_die, attr,
13210 &child_origin_cu);
c38f313d
DJ
13211 }
13212
34dc0f95
TBA
13213 /* If missing DW_AT_abstract_origin, try the corresponding child
13214 of the origin. Clang emits such lexical scopes. */
13215 if (child_origin_die == child_die
13216 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13217 && are_isomorphic
13218 && child_die->tag == DW_TAG_lexical_block)
13219 child_origin_die = corresponding_abstract_child;
13220
d389af10
JK
13221 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13222 counterpart may exist. */
c38f313d 13223 if (child_origin_die != child_die)
d389af10 13224 {
edb3359d
DJ
13225 if (child_die->tag != child_origin_die->tag
13226 && !(child_die->tag == DW_TAG_inlined_subroutine
13227 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13228 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13229 "different tags"),
9d8780f0
SM
13230 sect_offset_str (child_die->sect_off),
13231 sect_offset_str (child_origin_die->sect_off));
c38f313d 13232 if (child_origin_die->parent != origin_die)
b98664d3 13233 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13234 "different parents"),
9d8780f0
SM
13235 sect_offset_str (child_die->sect_off),
13236 sect_offset_str (child_origin_die->sect_off));
c38f313d 13237 else
791afaa2 13238 offsets.push_back (child_origin_die->sect_off);
d389af10 13239 }
34dc0f95
TBA
13240
13241 if (are_isomorphic)
13242 corresponding_abstract_child = corresponding_abstract_child->sibling;
d389af10 13243 }
791afaa2
TT
13244 std::sort (offsets.begin (), offsets.end ());
13245 sect_offset *offsets_end = offsets.data () + offsets.size ();
13246 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13247 if (offsetp[-1] == *offsetp)
b98664d3 13248 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13249 "to DIE %s as their abstract origin"),
13250 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13251
791afaa2 13252 offsetp = offsets.data ();
d389af10
JK
13253 origin_child_die = origin_die->child;
13254 while (origin_child_die && origin_child_die->tag)
13255 {
13256 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13257 while (offsetp < offsets_end
9c541725 13258 && *offsetp < origin_child_die->sect_off)
d389af10 13259 offsetp++;
b64f50a1 13260 if (offsetp >= offsets_end
9c541725 13261 || *offsetp > origin_child_die->sect_off)
d389af10 13262 {
adde2bff
DE
13263 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13264 Check whether we're already processing ORIGIN_CHILD_DIE.
13265 This can happen with mutually referenced abstract_origins.
13266 PR 16581. */
13267 if (!origin_child_die->in_process)
13268 process_die (origin_child_die, origin_cu);
d389af10 13269 }
436c571c 13270 origin_child_die = origin_child_die->sibling;
d389af10 13271 }
cd02d79d 13272 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13273
13274 if (cu != origin_cu)
13275 compute_delayed_physnames (origin_cu);
d389af10
JK
13276}
13277
c906108c 13278static void
e7c27a73 13279read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13280{
5e22e966 13281 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13282 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13283 struct context_stack *newobj;
c906108c
SS
13284 CORE_ADDR lowpc;
13285 CORE_ADDR highpc;
13286 struct die_info *child_die;
edb3359d 13287 struct attribute *attr, *call_line, *call_file;
15d034d0 13288 const char *name;
e142c38c 13289 CORE_ADDR baseaddr;
801e3a5b 13290 struct block *block;
edb3359d 13291 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13292 std::vector<struct symbol *> template_args;
34eaf542 13293 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13294
13295 if (inlined_func)
13296 {
13297 /* If we do not have call site information, we can't show the
13298 caller of this inlined function. That's too confusing, so
13299 only use the scope for local variables. */
13300 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13301 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13302 if (call_line == NULL || call_file == NULL)
13303 {
13304 read_lexical_block_scope (die, cu);
13305 return;
13306 }
13307 }
c906108c 13308
b3b3bada 13309 baseaddr = objfile->text_section_offset ();
e142c38c 13310
94af9270 13311 name = dwarf2_name (die, cu);
c906108c 13312
e8d05480
JB
13313 /* Ignore functions with missing or empty names. These are actually
13314 illegal according to the DWARF standard. */
13315 if (name == NULL)
13316 {
b98664d3 13317 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13318 sect_offset_str (die->sect_off));
e8d05480
JB
13319 return;
13320 }
13321
13322 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13323 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13324 <= PC_BOUNDS_INVALID)
e8d05480 13325 {
ae4d0c03 13326 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13327 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13328 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13329 "for subprogram DIE at %s"),
13330 sect_offset_str (die->sect_off));
e8d05480
JB
13331 return;
13332 }
c906108c 13333
3e29f34a
MR
13334 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13335 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13336
34eaf542
TT
13337 /* If we have any template arguments, then we must allocate a
13338 different sort of symbol. */
436c571c 13339 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13340 {
13341 if (child_die->tag == DW_TAG_template_type_param
13342 || child_die->tag == DW_TAG_template_value_param)
13343 {
8c14c3a3 13344 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13345 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13346 break;
13347 }
13348 }
13349
702cf3f5 13350 gdb_assert (cu->get_builder () != nullptr);
c24bdb02 13351 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13352 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13353 (struct symbol *) templ_func);
4c2df51b 13354
81873cc8 13355 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13356 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13357 cu->language);
13358
4cecd739
DJ
13359 /* If there is a location expression for DW_AT_frame_base, record
13360 it. */
e142c38c 13361 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13362 if (attr != nullptr)
fe978cb0 13363 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13364
63e43d3a
PMR
13365 /* If there is a location for the static link, record it. */
13366 newobj->static_link = NULL;
13367 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13368 if (attr != nullptr)
63e43d3a 13369 {
224c3ddb
SM
13370 newobj->static_link
13371 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13372 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13373 cu->addr_type ());
63e43d3a
PMR
13374 }
13375
c24bdb02 13376 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13377
639d11d3 13378 if (die->child != NULL)
c906108c 13379 {
639d11d3 13380 child_die = die->child;
c906108c
SS
13381 while (child_die && child_die->tag)
13382 {
34eaf542
TT
13383 if (child_die->tag == DW_TAG_template_type_param
13384 || child_die->tag == DW_TAG_template_value_param)
13385 {
13386 struct symbol *arg = new_symbol (child_die, NULL, cu);
13387
f1078f66 13388 if (arg != NULL)
2f4732b0 13389 template_args.push_back (arg);
34eaf542
TT
13390 }
13391 else
13392 process_die (child_die, cu);
436c571c 13393 child_die = child_die->sibling;
c906108c
SS
13394 }
13395 }
13396
d389af10
JK
13397 inherit_abstract_dies (die, cu);
13398
4a811a97
UW
13399 /* If we have a DW_AT_specification, we might need to import using
13400 directives from the context of the specification DIE. See the
13401 comment in determine_prefix. */
13402 if (cu->language == language_cplus
13403 && dwarf2_attr (die, DW_AT_specification, cu))
13404 {
13405 struct dwarf2_cu *spec_cu = cu;
13406 struct die_info *spec_die = die_specification (die, &spec_cu);
13407
13408 while (spec_die)
13409 {
13410 child_die = spec_die->child;
13411 while (child_die && child_die->tag)
13412 {
13413 if (child_die->tag == DW_TAG_imported_module)
13414 process_die (child_die, spec_cu);
436c571c 13415 child_die = child_die->sibling;
4a811a97
UW
13416 }
13417
13418 /* In some cases, GCC generates specification DIEs that
13419 themselves contain DW_AT_specification attributes. */
13420 spec_die = die_specification (spec_die, &spec_cu);
13421 }
13422 }
13423
c24bdb02 13424 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13425 /* Make a block for the local symbols within. */
c24bdb02 13426 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13427 cstk.static_link, lowpc, highpc);
801e3a5b 13428
df8a16a1 13429 /* For C++, set the block's scope. */
45280282
IB
13430 if ((cu->language == language_cplus
13431 || cu->language == language_fortran
c44af4eb
TT
13432 || cu->language == language_d
13433 || cu->language == language_rust)
4d4ec4e5 13434 && cu->processing_has_namespace_info)
195a3f6c
TT
13435 block_set_scope (block, determine_prefix (die, cu),
13436 &objfile->objfile_obstack);
df8a16a1 13437
801e3a5b
JB
13438 /* If we have address ranges, record them. */
13439 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13440
a60f3166 13441 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13442
34eaf542 13443 /* Attach template arguments to function. */
2f4732b0 13444 if (!template_args.empty ())
34eaf542
TT
13445 {
13446 gdb_assert (templ_func != NULL);
13447
2f4732b0 13448 templ_func->n_template_arguments = template_args.size ();
34eaf542 13449 templ_func->template_arguments
dda83cd7 13450 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13451 templ_func->n_template_arguments);
34eaf542 13452 memcpy (templ_func->template_arguments,
2f4732b0 13453 template_args.data (),
34eaf542 13454 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13455
13456 /* Make sure that the symtab is set on the new symbols. Even
13457 though they don't appear in this symtab directly, other parts
13458 of gdb assume that symbols do, and this is reasonably
13459 true. */
8634679f 13460 for (symbol *sym : template_args)
3e1d3d8c 13461 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13462 }
13463
208d8187
JB
13464 /* In C++, we can have functions nested inside functions (e.g., when
13465 a function declares a class that has methods). This means that
13466 when we finish processing a function scope, we may need to go
13467 back to building a containing block's symbol lists. */
c24bdb02
KS
13468 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13469 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13470
921e78cf
JB
13471 /* If we've finished processing a top-level function, subsequent
13472 symbols go in the file symbol list. */
c24bdb02
KS
13473 if (cu->get_builder ()->outermost_context_p ())
13474 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13475}
13476
13477/* Process all the DIES contained within a lexical block scope. Start
13478 a new scope, process the dies, and then close the scope. */
13479
13480static void
e7c27a73 13481read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13482{
5e22e966 13483 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13484 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13485 CORE_ADDR lowpc, highpc;
13486 struct die_info *child_die;
e142c38c
DJ
13487 CORE_ADDR baseaddr;
13488
b3b3bada 13489 baseaddr = objfile->text_section_offset ();
c906108c
SS
13490
13491 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13492 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13493 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13494 be nasty. Might be easier to properly extend generic blocks to
af34e669 13495 describe ranges. */
e385593e
JK
13496 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13497 {
13498 case PC_BOUNDS_NOT_PRESENT:
13499 /* DW_TAG_lexical_block has no attributes, process its children as if
13500 there was no wrapping by that DW_TAG_lexical_block.
13501 GCC does no longer produces such DWARF since GCC r224161. */
13502 for (child_die = die->child;
13503 child_die != NULL && child_die->tag;
436c571c 13504 child_die = child_die->sibling)
4f7bc5ed
TT
13505 {
13506 /* We might already be processing this DIE. This can happen
13507 in an unusual circumstance -- where a subroutine A
13508 appears lexically in another subroutine B, but A actually
13509 inlines B. The recursion is broken here, rather than in
13510 inherit_abstract_dies, because it seems better to simply
13511 drop concrete children here. */
13512 if (!child_die->in_process)
13513 process_die (child_die, cu);
13514 }
e385593e
JK
13515 return;
13516 case PC_BOUNDS_INVALID:
13517 return;
13518 }
3e29f34a
MR
13519 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13520 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13521
c24bdb02 13522 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13523 if (die->child != NULL)
c906108c 13524 {
639d11d3 13525 child_die = die->child;
c906108c
SS
13526 while (child_die && child_die->tag)
13527 {
e7c27a73 13528 process_die (child_die, cu);
436c571c 13529 child_die = child_die->sibling;
c906108c
SS
13530 }
13531 }
3ea89b92 13532 inherit_abstract_dies (die, cu);
c24bdb02 13533 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13534
c24bdb02
KS
13535 if (*cu->get_builder ()->get_local_symbols () != NULL
13536 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13537 {
801e3a5b 13538 struct block *block
dda83cd7 13539 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13540 cstk.start_addr, highpc);
801e3a5b
JB
13541
13542 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13543 do here, means that recording a parent's ranges entails
13544 walking across all its children's ranges as they appear in
13545 the address map, which is quadratic behavior.
13546
13547 It would be nicer to record the parent's ranges before
13548 traversing its children, simply overriding whatever you find
13549 there. But since we don't even decide whether to create a
13550 block until after we've traversed its children, that's hard
13551 to do. */
801e3a5b 13552 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13553 }
c24bdb02
KS
13554 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13555 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13556}
13557
216f72a1 13558/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13559
13560static void
13561read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13562{
5e22e966 13563 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13564 struct objfile *objfile = per_objfile->objfile;
08feed99 13565 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13566 CORE_ADDR pc, baseaddr;
13567 struct attribute *attr;
13568 struct call_site *call_site, call_site_local;
13569 void **slot;
13570 int nparams;
13571 struct die_info *child_die;
13572
b3b3bada 13573 baseaddr = objfile->text_section_offset ();
96408a79 13574
216f72a1
JK
13575 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13576 if (attr == NULL)
13577 {
13578 /* This was a pre-DWARF-5 GNU extension alias
13579 for DW_AT_call_return_pc. */
13580 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13581 }
96408a79
SA
13582 if (!attr)
13583 {
b98664d3 13584 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13585 "DIE %s [in module %s]"),
13586 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13587 return;
13588 }
95f982e5 13589 pc = attr->as_address () + baseaddr;
3e29f34a 13590 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13591
13592 if (cu->call_site_htab == NULL)
13593 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13594 NULL, &objfile->objfile_obstack,
13595 hashtab_obstack_allocate, NULL);
13596 call_site_local.pc = pc;
13597 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13598 if (*slot != NULL)
13599 {
b98664d3 13600 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13601 "DIE %s [in module %s]"),
13602 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13603 objfile_name (objfile));
96408a79
SA
13604 return;
13605 }
13606
13607 /* Count parameters at the caller. */
13608
13609 nparams = 0;
13610 for (child_die = die->child; child_die && child_die->tag;
436c571c 13611 child_die = child_die->sibling)
96408a79 13612 {
216f72a1 13613 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13614 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13615 {
b98664d3 13616 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13617 "DW_TAG_call_site child DIE %s [in module %s]"),
13618 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13619 objfile_name (objfile));
96408a79
SA
13620 continue;
13621 }
13622
13623 nparams++;
13624 }
13625
224c3ddb
SM
13626 call_site
13627 = ((struct call_site *)
13628 obstack_alloc (&objfile->objfile_obstack,
13629 sizeof (*call_site)
13630 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13631 *slot = call_site;
13632 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13633 call_site->pc = pc;
13634
216f72a1
JK
13635 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13636 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13637 {
13638 struct die_info *func_die;
13639
13640 /* Skip also over DW_TAG_inlined_subroutine. */
13641 for (func_die = die->parent;
13642 func_die && func_die->tag != DW_TAG_subprogram
13643 && func_die->tag != DW_TAG_subroutine_type;
13644 func_die = func_die->parent);
13645
216f72a1
JK
13646 /* DW_AT_call_all_calls is a superset
13647 of DW_AT_call_all_tail_calls. */
96408a79 13648 if (func_die
dda83cd7
SM
13649 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13650 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13651 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13652 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13653 {
13654 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13655 not complete. But keep CALL_SITE for look ups via call_site_htab,
13656 both the initial caller containing the real return address PC and
13657 the final callee containing the current PC of a chain of tail
13658 calls do not need to have the tail call list complete. But any
13659 function candidate for a virtual tail call frame searched via
13660 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13661 determined unambiguously. */
13662 }
13663 else
13664 {
13665 struct type *func_type = NULL;
13666
13667 if (func_die)
13668 func_type = get_die_type (func_die, cu);
13669 if (func_type != NULL)
13670 {
78134374 13671 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13672
13673 /* Enlist this call site to the function. */
13674 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13675 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13676 }
13677 else
b98664d3 13678 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13679 "DIE %s [in module %s]"),
13680 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13681 }
13682 }
13683
216f72a1
JK
13684 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13685 if (attr == NULL)
13686 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13687 if (attr == NULL)
13688 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13689 if (attr == NULL)
216f72a1
JK
13690 {
13691 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13692 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13693 }
96408a79 13694 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13695 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13696 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13697 else if (attr->form_is_block ())
96408a79
SA
13698 {
13699 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13700 struct dwarf_block *block = attr->as_block ();
96408a79 13701
8d749320 13702 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13703 dlbaton->data = block->data;
13704 dlbaton->size = block->size;
a50264ba 13705 dlbaton->per_objfile = per_objfile;
96408a79
SA
13706 dlbaton->per_cu = cu->per_cu;
13707
13708 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13709 }
cd6c91b4 13710 else if (attr->form_is_ref ())
96408a79 13711 {
96408a79
SA
13712 struct dwarf2_cu *target_cu = cu;
13713 struct die_info *target_die;
13714
ac9ec31b 13715 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13716 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13717 if (die_is_declaration (target_die, target_cu))
13718 {
7d45c7c3 13719 const char *target_physname;
9112db09
JK
13720
13721 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13722 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13723 if (target_physname == NULL)
9112db09 13724 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13725 if (target_physname == NULL)
b98664d3 13726 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13727 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13728 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13729 else
7d455152 13730 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13731 }
13732 else
13733 {
13734 CORE_ADDR lowpc;
13735
13736 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13737 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13738 <= PC_BOUNDS_INVALID)
b98664d3 13739 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13740 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13741 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13742 else
3e29f34a
MR
13743 {
13744 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13745 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13746 }
96408a79
SA
13747 }
13748 }
13749 else
b98664d3 13750 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13751 "block nor reference, for DIE %s [in module %s]"),
13752 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13753
13754 call_site->per_cu = cu->per_cu;
9f47c707 13755 call_site->per_objfile = per_objfile;
96408a79
SA
13756
13757 for (child_die = die->child;
13758 child_die && child_die->tag;
436c571c 13759 child_die = child_die->sibling)
96408a79 13760 {
96408a79 13761 struct call_site_parameter *parameter;
1788b2d3 13762 struct attribute *loc, *origin;
96408a79 13763
216f72a1 13764 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13765 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13766 {
13767 /* Already printed the complaint above. */
13768 continue;
13769 }
13770
13771 gdb_assert (call_site->parameter_count < nparams);
13772 parameter = &call_site->parameter[call_site->parameter_count];
13773
1788b2d3
JK
13774 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13775 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13776 register is contained in DW_AT_call_value. */
96408a79 13777
24c5c679 13778 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13779 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13780 if (origin == NULL)
13781 {
13782 /* This was a pre-DWARF-5 GNU extension alias
13783 for DW_AT_call_parameter. */
13784 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13785 }
cd6c91b4 13786 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13787 {
1788b2d3 13788 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13789
0826b30a 13790 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13791 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13792 {
13793 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13794 binding can be done only inside one CU. Such referenced DIE
13795 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13796 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13797 "DW_TAG_call_site child DIE %s [in module %s]"),
13798 sect_offset_str (child_die->sect_off),
9c541725 13799 objfile_name (objfile));
d76b7dbc
JK
13800 continue;
13801 }
9c541725
PA
13802 parameter->u.param_cu_off
13803 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13804 }
4fc6c0d5 13805 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13806 {
b98664d3 13807 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13808 "DW_TAG_call_site child DIE %s [in module %s]"),
13809 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13810 continue;
13811 }
24c5c679 13812 else
96408a79 13813 {
9d2246fc
TT
13814 struct dwarf_block *block = loc->as_block ();
13815
24c5c679 13816 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 13817 (block->data, &block->data[block->size]);
24c5c679
JK
13818 if (parameter->u.dwarf_reg != -1)
13819 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
13820 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13821 &block->data[block->size],
24c5c679
JK
13822 &parameter->u.fb_offset))
13823 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13824 else
13825 {
b98664d3 13826 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13827 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13828 "DW_TAG_call_site child DIE %s "
24c5c679 13829 "[in module %s]"),
9d8780f0 13830 sect_offset_str (child_die->sect_off),
9c541725 13831 objfile_name (objfile));
24c5c679
JK
13832 continue;
13833 }
96408a79
SA
13834 }
13835
216f72a1
JK
13836 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13837 if (attr == NULL)
13838 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13839 if (attr == NULL || !attr->form_is_block ())
96408a79 13840 {
b98664d3 13841 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13842 "DW_TAG_call_site child DIE %s [in module %s]"),
13843 sect_offset_str (child_die->sect_off),
9c541725 13844 objfile_name (objfile));
96408a79
SA
13845 continue;
13846 }
9d2246fc
TT
13847
13848 struct dwarf_block *block = attr->as_block ();
13849 parameter->value = block->data;
13850 parameter->value_size = block->size;
96408a79
SA
13851
13852 /* Parameters are not pre-cleared by memset above. */
13853 parameter->data_value = NULL;
13854 parameter->data_value_size = 0;
13855 call_site->parameter_count++;
13856
216f72a1
JK
13857 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13858 if (attr == NULL)
13859 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13860 if (attr != nullptr)
96408a79 13861 {
4fc6c0d5 13862 if (!attr->form_is_block ())
b98664d3 13863 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13864 "DW_TAG_call_site child DIE %s [in module %s]"),
13865 sect_offset_str (child_die->sect_off),
9c541725 13866 objfile_name (objfile));
96408a79
SA
13867 else
13868 {
9d2246fc
TT
13869 block = attr->as_block ();
13870 parameter->data_value = block->data;
13871 parameter->data_value_size = block->size;
96408a79
SA
13872 }
13873 }
13874 }
13875}
13876
71a3c369
TT
13877/* Helper function for read_variable. If DIE represents a virtual
13878 table, then return the type of the concrete object that is
13879 associated with the virtual table. Otherwise, return NULL. */
13880
13881static struct type *
13882rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13883{
13884 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13885 if (attr == NULL)
13886 return NULL;
13887
13888 /* Find the type DIE. */
13889 struct die_info *type_die = NULL;
13890 struct dwarf2_cu *type_cu = cu;
13891
cd6c91b4 13892 if (attr->form_is_ref ())
71a3c369
TT
13893 type_die = follow_die_ref (die, attr, &type_cu);
13894 if (type_die == NULL)
13895 return NULL;
13896
13897 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13898 return NULL;
13899 return die_containing_type (type_die, type_cu);
13900}
13901
13902/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13903
13904static void
13905read_variable (struct die_info *die, struct dwarf2_cu *cu)
13906{
13907 struct rust_vtable_symbol *storage = NULL;
13908
13909 if (cu->language == language_rust)
13910 {
13911 struct type *containing_type = rust_containing_type (die, cu);
13912
13913 if (containing_type != NULL)
13914 {
5e22e966 13915 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13916
8c14c3a3 13917 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13918 storage->concrete_type = containing_type;
cf724bc9 13919 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13920 }
13921 }
13922
e4a62c65
TV
13923 struct symbol *res = new_symbol (die, NULL, cu, storage);
13924 struct attribute *abstract_origin
13925 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13926 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13927 if (res == NULL && loc && abstract_origin)
13928 {
13929 /* We have a variable without a name, but with a location and an abstract
13930 origin. This may be a concrete instance of an abstract variable
13931 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13932 later. */
13933 struct dwarf2_cu *origin_cu = cu;
13934 struct die_info *origin_die
13935 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13936 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13937 per_objfile->per_bfd->abstract_to_concrete
13938 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13939 }
71a3c369
TT
13940}
13941
43988095
JK
13942/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13943 reading .debug_rnglists.
13944 Callback's type should be:
13945 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13946 Return true if the attributes are present and valid, otherwise,
13947 return false. */
13948
13949template <typename Callback>
13950static bool
13951dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 13952 dwarf_tag tag, Callback &&callback)
43988095 13953{
976ca316
SM
13954 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13955 struct objfile *objfile = per_objfile->objfile;
43988095 13956 bfd *obfd = objfile->obfd;
43988095 13957 /* Base address selection entry. */
2b24b6e4 13958 gdb::optional<CORE_ADDR> base;
43988095 13959 const gdb_byte *buffer;
43988095
JK
13960 CORE_ADDR baseaddr;
13961 bool overflow = false;
d0ce17d8
CT
13962 ULONGEST addr_index;
13963 struct dwarf2_section_info *rnglists_section;
43988095 13964
43988095 13965 base = cu->base_address;
d0ce17d8
CT
13966 rnglists_section = cu_debug_rnglists_section (cu, tag);
13967 rnglists_section->read (objfile);
43988095 13968
d0ce17d8 13969 if (offset >= rnglists_section->size)
43988095 13970 {
b98664d3 13971 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13972 offset);
13973 return false;
13974 }
d0ce17d8 13975 buffer = rnglists_section->buffer + offset;
43988095 13976
b3b3bada 13977 baseaddr = objfile->text_section_offset ();
43988095
JK
13978
13979 while (1)
13980 {
7814882a
JK
13981 /* Initialize it due to a false compiler warning. */
13982 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
13983 const gdb_byte *buf_end = (rnglists_section->buffer
13984 + rnglists_section->size);
43988095
JK
13985 unsigned int bytes_read;
13986
13987 if (buffer == buf_end)
13988 {
13989 overflow = true;
13990 break;
13991 }
13992 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13993 switch (rlet)
13994 {
13995 case DW_RLE_end_of_list:
13996 break;
13997 case DW_RLE_base_address:
13998 if (buffer + cu->header.addr_size > buf_end)
13999 {
14000 overflow = true;
14001 break;
14002 }
c8a7a66f 14003 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14004 buffer += bytes_read;
14005 break;
dda83cd7
SM
14006 case DW_RLE_base_addressx:
14007 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14008 buffer += bytes_read;
14009 base = read_addr_index (cu, addr_index);
14010 break;
43988095
JK
14011 case DW_RLE_start_length:
14012 if (buffer + cu->header.addr_size > buf_end)
14013 {
14014 overflow = true;
14015 break;
14016 }
c8a7a66f
TT
14017 range_beginning = cu->header.read_address (obfd, buffer,
14018 &bytes_read);
43988095
JK
14019 buffer += bytes_read;
14020 range_end = (range_beginning
14021 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14022 buffer += bytes_read;
14023 if (buffer > buf_end)
14024 {
14025 overflow = true;
14026 break;
14027 }
14028 break;
d0ce17d8 14029 case DW_RLE_startx_length:
dda83cd7
SM
14030 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14031 buffer += bytes_read;
14032 range_beginning = read_addr_index (cu, addr_index);
14033 if (buffer > buf_end)
14034 {
14035 overflow = true;
14036 break;
14037 }
14038 range_end = (range_beginning
14039 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14040 buffer += bytes_read;
14041 break;
43988095
JK
14042 case DW_RLE_offset_pair:
14043 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14044 buffer += bytes_read;
14045 if (buffer > buf_end)
14046 {
14047 overflow = true;
14048 break;
14049 }
14050 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14051 buffer += bytes_read;
14052 if (buffer > buf_end)
14053 {
14054 overflow = true;
14055 break;
14056 }
14057 break;
14058 case DW_RLE_start_end:
14059 if (buffer + 2 * cu->header.addr_size > buf_end)
14060 {
14061 overflow = true;
14062 break;
14063 }
c8a7a66f
TT
14064 range_beginning = cu->header.read_address (obfd, buffer,
14065 &bytes_read);
43988095 14066 buffer += bytes_read;
c8a7a66f 14067 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14068 buffer += bytes_read;
14069 break;
d0ce17d8 14070 case DW_RLE_startx_endx:
dda83cd7
SM
14071 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14072 buffer += bytes_read;
14073 range_beginning = read_addr_index (cu, addr_index);
14074 if (buffer > buf_end)
14075 {
14076 overflow = true;
14077 break;
14078 }
14079 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14080 buffer += bytes_read;
14081 range_end = read_addr_index (cu, addr_index);
14082 break;
43988095 14083 default:
b98664d3 14084 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14085 return false;
14086 }
14087 if (rlet == DW_RLE_end_of_list || overflow)
14088 break;
14089 if (rlet == DW_RLE_base_address)
14090 continue;
14091
43988095
JK
14092 if (range_beginning > range_end)
14093 {
14094 /* Inverted range entries are invalid. */
b98664d3 14095 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14096 return false;
14097 }
14098
14099 /* Empty range entries have no effect. */
14100 if (range_beginning == range_end)
14101 continue;
14102
d0ce17d8
CT
14103 /* Only DW_RLE_offset_pair needs the base address added. */
14104 if (rlet == DW_RLE_offset_pair)
14105 {
14106 if (!base.has_value ())
14107 {
14108 /* We have no valid base address for the DW_RLE_offset_pair. */
14109 complaint (_("Invalid .debug_rnglists data (no base address for "
14110 "DW_RLE_offset_pair)"));
14111 return false;
14112 }
14113
14114 range_beginning += *base;
14115 range_end += *base;
14116 }
43988095
JK
14117
14118 /* A not-uncommon case of bad debug info.
14119 Don't pollute the addrmap with bad data. */
14120 if (range_beginning + baseaddr == 0
976ca316 14121 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14122 {
b98664d3 14123 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14124 " [in module %s]"), objfile_name (objfile));
14125 continue;
14126 }
14127
14128 callback (range_beginning, range_end);
14129 }
14130
14131 if (overflow)
14132 {
b98664d3 14133 complaint (_("Offset %d is not terminated "
43988095
JK
14134 "for DW_AT_ranges attribute"),
14135 offset);
14136 return false;
14137 }
14138
14139 return true;
14140}
14141
14142/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14143 Callback's type should be:
14144 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14145 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14146
43988095 14147template <typename Callback>
43039443 14148static int
d0ce17d8 14149dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14150 Callback &&callback)
43039443 14151{
5e22e966
SM
14152 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14153 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14154 struct comp_unit_head *cu_header = &cu->header;
14155 bfd *obfd = objfile->obfd;
14156 unsigned int addr_size = cu_header->addr_size;
14157 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14158 /* Base address selection entry. */
2b24b6e4 14159 gdb::optional<CORE_ADDR> base;
43039443 14160 unsigned int dummy;
d521ce57 14161 const gdb_byte *buffer;
ff013f42 14162 CORE_ADDR baseaddr;
43039443 14163
43988095 14164 if (cu_header->version >= 5)
d0ce17d8 14165 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14166
d00adf39 14167 base = cu->base_address;
43039443 14168
5e22e966
SM
14169 per_objfile->per_bfd->ranges.read (objfile);
14170 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14171 {
b98664d3 14172 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14173 offset);
14174 return 0;
14175 }
5e22e966 14176 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14177
b3b3bada 14178 baseaddr = objfile->text_section_offset ();
ff013f42 14179
43039443
JK
14180 while (1)
14181 {
14182 CORE_ADDR range_beginning, range_end;
14183
c8a7a66f 14184 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14185 buffer += addr_size;
c8a7a66f 14186 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14187 buffer += addr_size;
14188 offset += 2 * addr_size;
14189
14190 /* An end of list marker is a pair of zero addresses. */
14191 if (range_beginning == 0 && range_end == 0)
14192 /* Found the end of list entry. */
14193 break;
14194
14195 /* Each base address selection entry is a pair of 2 values.
14196 The first is the largest possible address, the second is
14197 the base address. Check for a base address here. */
14198 if ((range_beginning & mask) == mask)
14199 {
28d2bfb9
AB
14200 /* If we found the largest possible address, then we already
14201 have the base address in range_end. */
14202 base = range_end;
43039443
JK
14203 continue;
14204 }
14205
2b24b6e4 14206 if (!base.has_value ())
43039443
JK
14207 {
14208 /* We have no valid base address for the ranges
14209 data. */
b98664d3 14210 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14211 return 0;
14212 }
14213
9277c30c
UW
14214 if (range_beginning > range_end)
14215 {
14216 /* Inverted range entries are invalid. */
b98664d3 14217 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14218 return 0;
14219 }
14220
14221 /* Empty range entries have no effect. */
14222 if (range_beginning == range_end)
14223 continue;
14224
2b24b6e4
TT
14225 range_beginning += *base;
14226 range_end += *base;
43039443 14227
01093045
DE
14228 /* A not-uncommon case of bad debug info.
14229 Don't pollute the addrmap with bad data. */
14230 if (range_beginning + baseaddr == 0
5e22e966 14231 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14232 {
b98664d3 14233 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14234 " [in module %s]"), objfile_name (objfile));
01093045
DE
14235 continue;
14236 }
14237
5f46c5a5
JK
14238 callback (range_beginning, range_end);
14239 }
14240
14241 return 1;
14242}
14243
14244/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14245 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 14246 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
14247
14248static int
14249dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14250 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14251 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14252{
5e22e966 14253 struct objfile *objfile = cu->per_objfile->objfile;
84685904 14254 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 14255 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14256 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14257 int low_set = 0;
14258 CORE_ADDR low = 0;
14259 CORE_ADDR high = 0;
14260 int retval;
14261
d0ce17d8 14262 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14263 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14264 {
9277c30c 14265 if (ranges_pst != NULL)
3e29f34a
MR
14266 {
14267 CORE_ADDR lowpc;
14268 CORE_ADDR highpc;
14269
79748972
TT
14270 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14271 range_beginning + baseaddr)
14272 - baseaddr);
14273 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14274 range_end + baseaddr)
14275 - baseaddr);
84685904 14276 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 14277 lowpc, highpc - 1, ranges_pst);
3e29f34a 14278 }
ff013f42 14279
43039443
JK
14280 /* FIXME: This is recording everything as a low-high
14281 segment of consecutive addresses. We should have a
14282 data structure for discontiguous block ranges
14283 instead. */
14284 if (! low_set)
14285 {
14286 low = range_beginning;
14287 high = range_end;
14288 low_set = 1;
14289 }
14290 else
14291 {
14292 if (range_beginning < low)
14293 low = range_beginning;
14294 if (range_end > high)
14295 high = range_end;
14296 }
5f46c5a5
JK
14297 });
14298 if (!retval)
14299 return 0;
43039443
JK
14300
14301 if (! low_set)
14302 /* If the first entry is an end-of-list marker, the range
14303 describes an empty scope, i.e. no instructions. */
14304 return 0;
14305
14306 if (low_return)
14307 *low_return = low;
14308 if (high_return)
14309 *high_return = high;
14310 return 1;
14311}
14312
3a2b436a
JK
14313/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14314 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14315 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14316
3a2b436a 14317static enum pc_bounds_kind
af34e669 14318dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14319 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14320 dwarf2_psymtab *pst)
c906108c 14321{
976ca316 14322 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14323 struct attribute *attr;
91da1414 14324 struct attribute *attr_high;
af34e669
DJ
14325 CORE_ADDR low = 0;
14326 CORE_ADDR high = 0;
e385593e 14327 enum pc_bounds_kind ret;
c906108c 14328
91da1414
MW
14329 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14330 if (attr_high)
af34e669 14331 {
e142c38c 14332 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14333 if (attr != nullptr)
dda83cd7 14334 {
95f982e5
TT
14335 low = attr->as_address ();
14336 high = attr_high->as_address ();
cd6c91b4 14337 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14338 high += low;
91da1414 14339 }
af34e669
DJ
14340 else
14341 /* Found high w/o low attribute. */
e385593e 14342 return PC_BOUNDS_INVALID;
af34e669
DJ
14343
14344 /* Found consecutive range of addresses. */
3a2b436a 14345 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14346 }
c906108c 14347 else
af34e669 14348 {
e142c38c 14349 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14350 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14351 {
2b0c7f41
SM
14352 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14353 on DWARF version). */
14354 ULONGEST ranges_offset = attr->as_unsigned ();
14355
14356 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14357 this value. */
14358 if (die->tag != DW_TAG_compile_unit)
14359 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14360
af34e669 14361 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14362 .debug_ranges section. */
d0ce17d8
CT
14363 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14364 die->tag))
e385593e 14365 return PC_BOUNDS_INVALID;
43039443 14366 /* Found discontinuous range of addresses. */
3a2b436a 14367 ret = PC_BOUNDS_RANGES;
af34e669 14368 }
e385593e
JK
14369 else
14370 return PC_BOUNDS_NOT_PRESENT;
af34e669 14371 }
c906108c 14372
48fbe735 14373 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14374 if (high <= low)
e385593e 14375 return PC_BOUNDS_INVALID;
c906108c
SS
14376
14377 /* When using the GNU linker, .gnu.linkonce. sections are used to
14378 eliminate duplicate copies of functions and vtables and such.
14379 The linker will arbitrarily choose one and discard the others.
14380 The AT_*_pc values for such functions refer to local labels in
14381 these sections. If the section from that file was discarded, the
14382 labels are not in the output, so the relocs get a value of 0.
14383 If this is a discarded function, mark the pc bounds as invalid,
14384 so that GDB will ignore it. */
976ca316 14385 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14386 return PC_BOUNDS_INVALID;
c906108c
SS
14387
14388 *lowpc = low;
96408a79
SA
14389 if (highpc)
14390 *highpc = high;
af34e669 14391 return ret;
c906108c
SS
14392}
14393
b084d499
JB
14394/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14395 its low and high PC addresses. Do nothing if these addresses could not
14396 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14397 and HIGHPC to the high address if greater than HIGHPC. */
14398
14399static void
14400dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14401 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14402 struct dwarf2_cu *cu)
b084d499
JB
14403{
14404 CORE_ADDR low, high;
14405 struct die_info *child = die->child;
14406
e385593e 14407 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14408 {
325fac50
PA
14409 *lowpc = std::min (*lowpc, low);
14410 *highpc = std::max (*highpc, high);
b084d499
JB
14411 }
14412
14413 /* If the language does not allow nested subprograms (either inside
14414 subprograms or lexical blocks), we're done. */
14415 if (cu->language != language_ada)
14416 return;
6e70227d 14417
b084d499
JB
14418 /* Check all the children of the given DIE. If it contains nested
14419 subprograms, then check their pc bounds. Likewise, we need to
14420 check lexical blocks as well, as they may also contain subprogram
14421 definitions. */
14422 while (child && child->tag)
14423 {
14424 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14425 || child->tag == DW_TAG_lexical_block)
14426 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14427 child = child->sibling;
b084d499
JB
14428 }
14429}
14430
fae299cd
DC
14431/* Get the low and high pc's represented by the scope DIE, and store
14432 them in *LOWPC and *HIGHPC. If the correct values can't be
14433 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14434
14435static void
14436get_scope_pc_bounds (struct die_info *die,
14437 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14438 struct dwarf2_cu *cu)
14439{
14440 CORE_ADDR best_low = (CORE_ADDR) -1;
14441 CORE_ADDR best_high = (CORE_ADDR) 0;
14442 CORE_ADDR current_low, current_high;
14443
3a2b436a 14444 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14445 >= PC_BOUNDS_RANGES)
fae299cd
DC
14446 {
14447 best_low = current_low;
14448 best_high = current_high;
14449 }
14450 else
14451 {
14452 struct die_info *child = die->child;
14453
14454 while (child && child->tag)
14455 {
14456 switch (child->tag) {
14457 case DW_TAG_subprogram:
dda83cd7 14458 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14459 break;
14460 case DW_TAG_namespace:
f55ee35c 14461 case DW_TAG_module:
fae299cd
DC
14462 /* FIXME: carlton/2004-01-16: Should we do this for
14463 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14464 that current GCC's always emit the DIEs corresponding
14465 to definitions of methods of classes as children of a
14466 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14467 the DIEs giving the declarations, which could be
14468 anywhere). But I don't see any reason why the
14469 standards says that they have to be there. */
14470 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14471
14472 if (current_low != ((CORE_ADDR) -1))
14473 {
325fac50
PA
14474 best_low = std::min (best_low, current_low);
14475 best_high = std::max (best_high, current_high);
fae299cd
DC
14476 }
14477 break;
14478 default:
0963b4bd 14479 /* Ignore. */
fae299cd
DC
14480 break;
14481 }
14482
436c571c 14483 child = child->sibling;
fae299cd
DC
14484 }
14485 }
14486
14487 *lowpc = best_low;
14488 *highpc = best_high;
14489}
14490
801e3a5b
JB
14491/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14492 in DIE. */
380bca97 14493
801e3a5b
JB
14494static void
14495dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14496 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14497{
5e22e966 14498 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14499 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14500 struct attribute *attr;
91da1414 14501 struct attribute *attr_high;
801e3a5b 14502
91da1414
MW
14503 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14504 if (attr_high)
801e3a5b 14505 {
801e3a5b 14506 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14507 if (attr != nullptr)
dda83cd7 14508 {
95f982e5
TT
14509 CORE_ADDR low = attr->as_address ();
14510 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14511
cd6c91b4 14512 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14513 high += low;
9a619af0 14514
3e29f34a
MR
14515 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14516 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14517 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14518 }
801e3a5b
JB
14519 }
14520
14521 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14522 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14523 {
2b0c7f41
SM
14524 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14525 on DWARF version). */
14526 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14527
2b0c7f41
SM
14528 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14529 this value. */
14530 if (die->tag != DW_TAG_compile_unit)
14531 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14532
2d5f09ec 14533 std::vector<blockrange> blockvec;
2b0c7f41 14534 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14535 [&] (CORE_ADDR start, CORE_ADDR end)
14536 {
58fdfd2c
JK
14537 start += baseaddr;
14538 end += baseaddr;
5f46c5a5
JK
14539 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14540 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14541 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14542 blockvec.emplace_back (start, end);
5f46c5a5 14543 });
2d5f09ec
KB
14544
14545 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14546 }
14547}
14548
685b1105
JK
14549/* Check whether the producer field indicates either of GCC < 4.6, or the
14550 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14551
685b1105
JK
14552static void
14553check_producer (struct dwarf2_cu *cu)
60d5a603 14554{
38360086 14555 int major, minor;
60d5a603
JK
14556
14557 if (cu->producer == NULL)
14558 {
14559 /* For unknown compilers expect their behavior is DWARF version
14560 compliant.
14561
14562 GCC started to support .debug_types sections by -gdwarf-4 since
14563 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14564 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14565 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14566 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14567 }
b1ffba5a 14568 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14569 {
38360086
MW
14570 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14571 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14572 }
5230b05a 14573 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14574 {
14575 cu->producer_is_icc = true;
14576 cu->producer_is_icc_lt_14 = major < 14;
14577 }
c258c396
JD
14578 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14579 cu->producer_is_codewarrior = true;
685b1105
JK
14580 else
14581 {
14582 /* For other non-GCC compilers, expect their behavior is DWARF version
14583 compliant. */
60d5a603
JK
14584 }
14585
9068261f 14586 cu->checked_producer = true;
685b1105 14587}
ba919b58 14588
685b1105
JK
14589/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14590 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14591 during 4.6.0 experimental. */
14592
9068261f 14593static bool
685b1105
JK
14594producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14595{
14596 if (!cu->checked_producer)
14597 check_producer (cu);
14598
14599 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14600}
14601
c258c396
JD
14602
14603/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14604 with incorrect is_stmt attributes. */
14605
14606static bool
14607producer_is_codewarrior (struct dwarf2_cu *cu)
14608{
14609 if (!cu->checked_producer)
14610 check_producer (cu);
14611
14612 return cu->producer_is_codewarrior;
14613}
14614
bf23a268
TT
14615/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14616 If that attribute is not available, return the appropriate
14617 default. */
60d5a603
JK
14618
14619static enum dwarf_access_attribute
bf23a268 14620dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14621{
bf23a268
TT
14622 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14623 if (attr != nullptr)
14624 {
14625 LONGEST value = attr->constant_value (-1);
14626 if (value == DW_ACCESS_public
14627 || value == DW_ACCESS_protected
14628 || value == DW_ACCESS_private)
14629 return (dwarf_access_attribute) value;
14630 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14631 plongest (value));
14632 }
14633
60d5a603
JK
14634 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14635 {
14636 /* The default DWARF 2 accessibility for members is public, the default
14637 accessibility for inheritance is private. */
14638
14639 if (die->tag != DW_TAG_inheritance)
14640 return DW_ACCESS_public;
14641 else
14642 return DW_ACCESS_private;
14643 }
14644 else
14645 {
14646 /* DWARF 3+ defines the default accessibility a different way. The same
14647 rules apply now for DW_TAG_inheritance as for the members and it only
14648 depends on the container kind. */
14649
14650 if (die->parent->tag == DW_TAG_class_type)
14651 return DW_ACCESS_private;
14652 else
14653 return DW_ACCESS_public;
14654 }
14655}
14656
74ac6d43
TT
14657/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14658 offset. If the attribute was not found return 0, otherwise return
14659 1. If it was found but could not properly be handled, set *OFFSET
14660 to 0. */
14661
14662static int
14663handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14664 LONGEST *offset)
14665{
14666 struct attribute *attr;
14667
14668 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14669 if (attr != NULL)
14670 {
14671 *offset = 0;
14672
14673 /* Note that we do not check for a section offset first here.
14674 This is because DW_AT_data_member_location is new in DWARF 4,
14675 so if we see it, we can assume that a constant form is really
14676 a constant and not a section offset. */
cd6c91b4 14677 if (attr->form_is_constant ())
0826b30a 14678 *offset = attr->constant_value (0);
cd6c91b4 14679 else if (attr->form_is_section_offset ())
74ac6d43 14680 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14681 else if (attr->form_is_block ())
9d2246fc 14682 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14683 else
14684 dwarf2_complex_location_expr_complaint ();
14685
14686 return 1;
14687 }
14688
14689 return 0;
14690}
14691
7d79de9a
TT
14692/* Look for DW_AT_data_member_location and store the results in FIELD. */
14693
14694static void
14695handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14696 struct field *field)
14697{
14698 struct attribute *attr;
14699
14700 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14701 if (attr != NULL)
14702 {
14703 if (attr->form_is_constant ())
14704 {
14705 LONGEST offset = attr->constant_value (0);
14706 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14707 }
14708 else if (attr->form_is_section_offset ())
14709 dwarf2_complex_location_expr_complaint ();
14710 else if (attr->form_is_block ())
14711 {
14712 bool handled;
9d2246fc 14713 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14714 if (handled)
14715 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14716 else
14717 {
5e22e966
SM
14718 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14719 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14720 struct dwarf2_locexpr_baton *dlbaton
14721 = XOBNEW (&objfile->objfile_obstack,
14722 struct dwarf2_locexpr_baton);
9d2246fc
TT
14723 dlbaton->data = attr->as_block ()->data;
14724 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14725 /* When using this baton, we want to compute the address
14726 of the field, not the value. This is why
14727 is_reference is set to false here. */
14728 dlbaton->is_reference = false;
5e22e966 14729 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14730 dlbaton->per_cu = cu->per_cu;
14731
14732 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14733 }
14734 }
14735 else
14736 dwarf2_complex_location_expr_complaint ();
14737 }
14738}
14739
c906108c
SS
14740/* Add an aggregate field to the field list. */
14741
14742static void
107d2387 14743dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14744 struct dwarf2_cu *cu)
6e70227d 14745{
5e22e966 14746 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14747 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14748 struct nextfield *new_field;
14749 struct attribute *attr;
14750 struct field *fp;
15d034d0 14751 const char *fieldname = "";
c906108c 14752
7d0ccb61
DJ
14753 if (die->tag == DW_TAG_inheritance)
14754 {
be2daae6
TT
14755 fip->baseclasses.emplace_back ();
14756 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14757 }
14758 else
14759 {
be2daae6
TT
14760 fip->fields.emplace_back ();
14761 new_field = &fip->fields.back ();
7d0ccb61 14762 }
be2daae6 14763
9c6a1327
TT
14764 new_field->offset = die->sect_off;
14765
bf23a268 14766 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 14767 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 14768 fip->non_public_fields = true;
60d5a603 14769
e142c38c 14770 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14771 if (attr != nullptr)
23dca5c3 14772 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
14773 else
14774 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14775
14776 fp = &new_field->field;
a9a9bd0f 14777
e142c38c 14778 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14779 {
a9a9bd0f 14780 /* Data member other than a C++ static data member. */
6e70227d 14781
c906108c 14782 /* Get type of field. */
5d14b6e5 14783 fp->set_type (die_type (die, cu));
c906108c 14784
d6a843b5 14785 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14786
c906108c 14787 /* Get bit size of field (zero if none). */
e142c38c 14788 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14789 if (attr != nullptr)
c906108c 14790 {
529908cb 14791 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
14792 }
14793 else
14794 {
14795 FIELD_BITSIZE (*fp) = 0;
14796 }
14797
14798 /* Get bit offset of field. */
7d79de9a 14799 handle_data_member_location (die, cu, fp);
e142c38c 14800 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 14801 if (attr != nullptr && attr->form_is_constant ())
c906108c 14802 {
d5a22e77 14803 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14804 {
14805 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
14806 additional bit offset from the MSB of the containing
14807 anonymous object to the MSB of the field. We don't
14808 have to do anything special since we don't need to
14809 know the size of the anonymous object. */
529908cb 14810 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 14811 + attr->constant_value (0)));
c906108c
SS
14812 }
14813 else
14814 {
14815 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
14816 MSB of the anonymous object, subtract off the number of
14817 bits from the MSB of the field to the MSB of the
14818 object, and then subtract off the number of bits of
14819 the field itself. The result is the bit offset of
14820 the LSB of the field. */
c906108c 14821 int anonymous_size;
7c184d33 14822 int bit_offset = attr->constant_value (0);
c906108c 14823
e142c38c 14824 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 14825 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
14826 {
14827 /* The size of the anonymous object containing
14828 the bit field is explicit, so use the
14829 indicated size (in bytes). */
7c184d33 14830 anonymous_size = attr->constant_value (0);
c906108c
SS
14831 }
14832 else
14833 {
14834 /* The size of the anonymous object containing
14835 the bit field must be inferred from the type
14836 attribute of the data member containing the
14837 bit field. */
5d14b6e5 14838 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14839 }
f41f5e61
PA
14840 SET_FIELD_BITPOS (*fp,
14841 (FIELD_BITPOS (*fp)
14842 + anonymous_size * bits_per_byte
14843 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14844 }
14845 }
da5b30da
AA
14846 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14847 if (attr != NULL)
14848 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14849 + attr->constant_value (0)));
c906108c
SS
14850
14851 /* Get name of field. */
39cbfefa
DJ
14852 fieldname = dwarf2_name (die, cu);
14853 if (fieldname == NULL)
14854 fieldname = "";
d8151005
DJ
14855
14856 /* The name is already allocated along with this objfile, so we don't
14857 need to duplicate it for the type. */
14858 fp->name = fieldname;
c906108c
SS
14859
14860 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 14861 pointer or virtual base class pointer) to private. */
e142c38c 14862 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14863 {
d48cc9dd 14864 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 14865 new_field->accessibility = DW_ACCESS_private;
264fc0e2 14866 fip->non_public_fields = true;
c906108c
SS
14867 }
14868 }
a9a9bd0f 14869 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14870 {
a9a9bd0f
DC
14871 /* C++ static member. */
14872
14873 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14874 is a declaration, but all versions of G++ as of this writing
14875 (so through at least 3.2.1) incorrectly generate
14876 DW_TAG_variable tags. */
6e70227d 14877
ff355380 14878 const char *physname;
c906108c 14879
a9a9bd0f 14880 /* Get name of field. */
39cbfefa
DJ
14881 fieldname = dwarf2_name (die, cu);
14882 if (fieldname == NULL)
c906108c
SS
14883 return;
14884
254e6b9e 14885 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14886 if (attr
14887 /* Only create a symbol if this is an external value.
14888 new_symbol checks this and puts the value in the global symbol
14889 table, which we want. If it is not external, new_symbol
14890 will try to put the value in cu->list_in_scope which is wrong. */
14891 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14892 {
14893 /* A static const member, not much different than an enum as far as
14894 we're concerned, except that we can support more types. */
14895 new_symbol (die, NULL, cu);
14896 }
14897
2df3850c 14898 /* Get physical name. */
ff355380 14899 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14900
d8151005
DJ
14901 /* The name is already allocated along with this objfile, so we don't
14902 need to duplicate it for the type. */
14903 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14904 fp->set_type (die_type (die, cu));
d8151005 14905 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14906 }
14907 else if (die->tag == DW_TAG_inheritance)
14908 {
74ac6d43 14909 /* C++ base class field. */
7d79de9a 14910 handle_data_member_location (die, cu, fp);
c906108c 14911 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14912 fp->set_type (die_type (die, cu));
14913 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14914 }
2ddeaf8a
TT
14915 else
14916 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14917}
14918
883fd55a
KS
14919/* Can the type given by DIE define another type? */
14920
14921static bool
14922type_can_define_types (const struct die_info *die)
14923{
14924 switch (die->tag)
14925 {
14926 case DW_TAG_typedef:
14927 case DW_TAG_class_type:
14928 case DW_TAG_structure_type:
14929 case DW_TAG_union_type:
14930 case DW_TAG_enumeration_type:
14931 return true;
14932
14933 default:
14934 return false;
14935 }
14936}
14937
14938/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14939
14940static void
883fd55a
KS
14941dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14942 struct dwarf2_cu *cu)
6e70227d 14943{
be2daae6
TT
14944 struct decl_field fp;
14945 memset (&fp, 0, sizeof (fp));
98751a41 14946
883fd55a 14947 gdb_assert (type_can_define_types (die));
98751a41 14948
883fd55a 14949 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14950 fp.name = dwarf2_name (die, cu);
14951 fp.type = read_type_die (die, cu);
98751a41 14952
c191a687 14953 /* Save accessibility. */
bf23a268 14954 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
14955 switch (accessibility)
14956 {
14957 case DW_ACCESS_public:
14958 /* The assumed value if neither private nor protected. */
14959 break;
14960 case DW_ACCESS_private:
be2daae6 14961 fp.is_private = 1;
c191a687
KS
14962 break;
14963 case DW_ACCESS_protected:
be2daae6 14964 fp.is_protected = 1;
c191a687 14965 break;
c191a687
KS
14966 }
14967
883fd55a 14968 if (die->tag == DW_TAG_typedef)
be2daae6 14969 fip->typedef_field_list.push_back (fp);
883fd55a 14970 else
be2daae6 14971 fip->nested_types_list.push_back (fp);
98751a41
JK
14972}
14973
9c6a1327
TT
14974/* A convenience typedef that's used when finding the discriminant
14975 field for a variant part. */
1b95cdb7
SM
14976typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14977 offset_map_type;
9c6a1327
TT
14978
14979/* Compute the discriminant range for a given variant. OBSTACK is
14980 where the results will be stored. VARIANT is the variant to
14981 process. IS_UNSIGNED indicates whether the discriminant is signed
14982 or unsigned. */
14983
14984static const gdb::array_view<discriminant_range>
14985convert_variant_range (struct obstack *obstack, const variant_field &variant,
14986 bool is_unsigned)
14987{
14988 std::vector<discriminant_range> ranges;
14989
14990 if (variant.default_branch)
14991 return {};
14992
14993 if (variant.discr_list_data == nullptr)
14994 {
14995 discriminant_range r
14996 = {variant.discriminant_value, variant.discriminant_value};
14997 ranges.push_back (r);
14998 }
14999 else
15000 {
15001 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15002 variant.discr_list_data->size);
15003 while (!data.empty ())
15004 {
15005 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15006 {
15007 complaint (_("invalid discriminant marker: %d"), data[0]);
15008 break;
15009 }
15010 bool is_range = data[0] == DW_DSC_range;
15011 data = data.slice (1);
15012
15013 ULONGEST low, high;
15014 unsigned int bytes_read;
15015
15016 if (data.empty ())
15017 {
15018 complaint (_("DW_AT_discr_list missing low value"));
15019 break;
15020 }
15021 if (is_unsigned)
15022 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15023 else
15024 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15025 &bytes_read);
15026 data = data.slice (bytes_read);
15027
15028 if (is_range)
15029 {
15030 if (data.empty ())
15031 {
15032 complaint (_("DW_AT_discr_list missing high value"));
15033 break;
15034 }
15035 if (is_unsigned)
15036 high = read_unsigned_leb128 (nullptr, data.data (),
15037 &bytes_read);
15038 else
15039 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15040 &bytes_read);
15041 data = data.slice (bytes_read);
15042 }
15043 else
15044 high = low;
15045
15046 ranges.push_back ({ low, high });
15047 }
15048 }
15049
15050 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15051 ranges.size ());
15052 std::copy (ranges.begin (), ranges.end (), result);
15053 return gdb::array_view<discriminant_range> (result, ranges.size ());
15054}
15055
15056static const gdb::array_view<variant_part> create_variant_parts
15057 (struct obstack *obstack,
15058 const offset_map_type &offset_map,
15059 struct field_info *fi,
15060 const std::vector<variant_part_builder> &variant_parts);
15061
15062/* Fill in a "struct variant" for a given variant field. RESULT is
15063 the variant to fill in. OBSTACK is where any needed allocations
15064 will be done. OFFSET_MAP holds the mapping from section offsets to
15065 fields for the type. FI describes the fields of the type we're
15066 processing. FIELD is the variant field we're converting. */
15067
15068static void
15069create_one_variant (variant &result, struct obstack *obstack,
15070 const offset_map_type &offset_map,
15071 struct field_info *fi, const variant_field &field)
15072{
15073 result.discriminants = convert_variant_range (obstack, field, false);
15074 result.first_field = field.first_field + fi->baseclasses.size ();
15075 result.last_field = field.last_field + fi->baseclasses.size ();
15076 result.parts = create_variant_parts (obstack, offset_map, fi,
15077 field.variant_parts);
15078}
15079
15080/* Fill in a "struct variant_part" for a given variant part. RESULT
15081 is the variant part to fill in. OBSTACK is where any needed
15082 allocations will be done. OFFSET_MAP holds the mapping from
15083 section offsets to fields for the type. FI describes the fields of
15084 the type we're processing. BUILDER is the variant part to be
15085 converted. */
15086
15087static void
15088create_one_variant_part (variant_part &result,
15089 struct obstack *obstack,
15090 const offset_map_type &offset_map,
15091 struct field_info *fi,
15092 const variant_part_builder &builder)
15093{
15094 auto iter = offset_map.find (builder.discriminant_offset);
15095 if (iter == offset_map.end ())
15096 {
15097 result.discriminant_index = -1;
15098 /* Doesn't matter. */
15099 result.is_unsigned = false;
15100 }
15101 else
15102 {
15103 result.discriminant_index = iter->second;
15104 result.is_unsigned
c6d940a9 15105 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15106 }
15107
15108 size_t n = builder.variants.size ();
15109 variant *output = new (obstack) variant[n];
15110 for (size_t i = 0; i < n; ++i)
15111 create_one_variant (output[i], obstack, offset_map, fi,
15112 builder.variants[i]);
15113
15114 result.variants = gdb::array_view<variant> (output, n);
15115}
15116
15117/* Create a vector of variant parts that can be attached to a type.
15118 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15119 holds the mapping from section offsets to fields for the type. FI
15120 describes the fields of the type we're processing. VARIANT_PARTS
15121 is the vector to convert. */
15122
15123static const gdb::array_view<variant_part>
15124create_variant_parts (struct obstack *obstack,
15125 const offset_map_type &offset_map,
15126 struct field_info *fi,
15127 const std::vector<variant_part_builder> &variant_parts)
15128{
15129 if (variant_parts.empty ())
15130 return {};
15131
15132 size_t n = variant_parts.size ();
15133 variant_part *result = new (obstack) variant_part[n];
15134 for (size_t i = 0; i < n; ++i)
15135 create_one_variant_part (result[i], obstack, offset_map, fi,
15136 variant_parts[i]);
15137
15138 return gdb::array_view<variant_part> (result, n);
15139}
15140
15141/* Compute the variant part vector for FIP, attaching it to TYPE when
15142 done. */
15143
15144static void
15145add_variant_property (struct field_info *fip, struct type *type,
15146 struct dwarf2_cu *cu)
15147{
15148 /* Map section offsets of fields to their field index. Note the
15149 field index here does not take the number of baseclasses into
15150 account. */
15151 offset_map_type offset_map;
15152 for (int i = 0; i < fip->fields.size (); ++i)
15153 offset_map[fip->fields[i].offset] = i;
15154
5e22e966 15155 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15156 gdb::array_view<variant_part> parts
15157 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15158 fip->variant_parts);
15159
15160 struct dynamic_prop prop;
8c2e4e06
SM
15161 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15162 obstack_copy (&objfile->objfile_obstack, &parts,
15163 sizeof (parts)));
9c6a1327 15164
5c54719c 15165 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15166}
15167
c906108c
SS
15168/* Create the vector of fields, and attach it to the type. */
15169
15170static void
fba45db2 15171dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15172 struct dwarf2_cu *cu)
c906108c 15173{
317f7127 15174 int nfields = fip->nfields ();
c906108c
SS
15175
15176 /* Record the field count, allocate space for the array of fields,
15177 and create blank accessibility bitfields if necessary. */
5e33d5f4 15178 type->set_num_fields (nfields);
3cabb6b0
SM
15179 type->set_fields
15180 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15181
b4ba55a1 15182 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15183 {
15184 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15185
15186 TYPE_FIELD_PRIVATE_BITS (type) =
15187 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15188 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15189
15190 TYPE_FIELD_PROTECTED_BITS (type) =
15191 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15192 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15193
774b6a14
TT
15194 TYPE_FIELD_IGNORE_BITS (type) =
15195 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15196 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15197 }
15198
15199 /* If the type has baseclasses, allocate and clear a bit vector for
15200 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15201 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15202 {
be2daae6 15203 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15204 unsigned char *pointer;
c906108c
SS
15205
15206 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15207 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15208 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15209 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15210 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15211 }
15212
9c6a1327
TT
15213 if (!fip->variant_parts.empty ())
15214 add_variant_property (fip, type, cu);
2ddeaf8a 15215
be2daae6
TT
15216 /* Copy the saved-up fields into the field vector. */
15217 for (int i = 0; i < nfields; ++i)
c906108c 15218 {
be2daae6
TT
15219 struct nextfield &field
15220 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15221 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15222
ceacbf6e 15223 type->field (i) = field.field;
be2daae6 15224 switch (field.accessibility)
c906108c 15225 {
c5aa993b 15226 case DW_ACCESS_private:
b4ba55a1 15227 if (cu->language != language_ada)
be2daae6 15228 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15229 break;
c906108c 15230
c5aa993b 15231 case DW_ACCESS_protected:
b4ba55a1 15232 if (cu->language != language_ada)
be2daae6 15233 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15234 break;
c906108c 15235
c5aa993b
JM
15236 case DW_ACCESS_public:
15237 break;
c906108c 15238
c5aa993b
JM
15239 default:
15240 /* Unknown accessibility. Complain and treat it as public. */
15241 {
b98664d3 15242 complaint (_("unsupported accessibility %d"),
be2daae6 15243 field.accessibility);
c5aa993b
JM
15244 }
15245 break;
c906108c 15246 }
be2daae6 15247 if (i < fip->baseclasses.size ())
c906108c 15248 {
be2daae6 15249 switch (field.virtuality)
c906108c 15250 {
c5aa993b
JM
15251 case DW_VIRTUALITY_virtual:
15252 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15253 if (cu->language == language_ada)
a73c6dcd 15254 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15255 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15256 break;
c906108c
SS
15257 }
15258 }
c906108c
SS
15259 }
15260}
15261
7d27a96d
TT
15262/* Return true if this member function is a constructor, false
15263 otherwise. */
15264
15265static int
15266dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15267{
15268 const char *fieldname;
fe978cb0 15269 const char *type_name;
7d27a96d
TT
15270 int len;
15271
15272 if (die->parent == NULL)
15273 return 0;
15274
15275 if (die->parent->tag != DW_TAG_structure_type
15276 && die->parent->tag != DW_TAG_union_type
15277 && die->parent->tag != DW_TAG_class_type)
15278 return 0;
15279
15280 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15281 type_name = dwarf2_name (die->parent, cu);
15282 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15283 return 0;
15284
15285 len = strlen (fieldname);
fe978cb0
PA
15286 return (strncmp (fieldname, type_name, len) == 0
15287 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15288}
15289
c906108c
SS
15290/* Add a member function to the proper fieldlist. */
15291
15292static void
107d2387 15293dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15294 struct type *type, struct dwarf2_cu *cu)
c906108c 15295{
5e22e966 15296 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15297 struct attribute *attr;
c906108c 15298 int i;
be2daae6 15299 struct fnfieldlist *flp = nullptr;
c906108c 15300 struct fn_field *fnp;
15d034d0 15301 const char *fieldname;
f792889a 15302 struct type *this_type;
c906108c 15303
b4ba55a1 15304 if (cu->language == language_ada)
a73c6dcd 15305 error (_("unexpected member function in Ada type"));
b4ba55a1 15306
2df3850c 15307 /* Get name of member function. */
39cbfefa
DJ
15308 fieldname = dwarf2_name (die, cu);
15309 if (fieldname == NULL)
2df3850c 15310 return;
c906108c 15311
c906108c 15312 /* Look up member function name in fieldlist. */
be2daae6 15313 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15314 {
27bfe10e 15315 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15316 {
15317 flp = &fip->fnfieldlists[i];
15318 break;
15319 }
c906108c
SS
15320 }
15321
be2daae6
TT
15322 /* Create a new fnfieldlist if necessary. */
15323 if (flp == nullptr)
c906108c 15324 {
be2daae6
TT
15325 fip->fnfieldlists.emplace_back ();
15326 flp = &fip->fnfieldlists.back ();
c906108c 15327 flp->name = fieldname;
be2daae6 15328 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15329 }
15330
be2daae6
TT
15331 /* Create a new member function field and add it to the vector of
15332 fnfieldlists. */
15333 flp->fnfields.emplace_back ();
15334 fnp = &flp->fnfields.back ();
3da10d80
KS
15335
15336 /* Delay processing of the physname until later. */
9c37b5ae 15337 if (cu->language == language_cplus)
be2daae6
TT
15338 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15339 die, cu);
3da10d80
KS
15340 else
15341 {
1d06ead6 15342 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15343 fnp->physname = physname ? physname : "";
15344 }
15345
c906108c 15346 fnp->type = alloc_type (objfile);
f792889a 15347 this_type = read_type_die (die, cu);
78134374 15348 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15349 {
1f704f76 15350 int nparams = this_type->num_fields ();
c906108c 15351
f792889a 15352 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15353 of the method itself (TYPE_CODE_METHOD). */
15354 smash_to_method_type (fnp->type, type,
f792889a 15355 TYPE_TARGET_TYPE (this_type),
80fc5e77 15356 this_type->fields (),
1f704f76 15357 this_type->num_fields (),
a409645d 15358 this_type->has_varargs ());
c906108c
SS
15359
15360 /* Handle static member functions.
dda83cd7
SM
15361 Dwarf2 has no clean way to discern C++ static and non-static
15362 member functions. G++ helps GDB by marking the first
15363 parameter for non-static member functions (which is the this
15364 pointer) as artificial. We obtain this information from
15365 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15366 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15367 fnp->voffset = VOFFSET_STATIC;
15368 }
15369 else
b98664d3 15370 complaint (_("member function type missing for '%s'"),
3da10d80 15371 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15372
15373 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15374 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15375 fnp->fcontext = die_containing_type (die, cu);
c906108c 15376
3e43a32a
MS
15377 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15378 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15379
15380 /* Get accessibility. */
bf23a268 15381 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15382 switch (accessibility)
c906108c 15383 {
60d5a603
JK
15384 case DW_ACCESS_private:
15385 fnp->is_private = 1;
15386 break;
15387 case DW_ACCESS_protected:
15388 fnp->is_protected = 1;
15389 break;
c906108c
SS
15390 }
15391
b02dede2 15392 /* Check for artificial methods. */
e142c38c 15393 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15394 if (attr && attr->as_boolean ())
b02dede2
DJ
15395 fnp->is_artificial = 1;
15396
e35000a7
TBA
15397 /* Check for defaulted methods. */
15398 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15399 if (attr != nullptr)
15400 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15401
15402 /* Check for deleted methods. */
15403 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15404 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15405 fnp->is_deleted = 1;
15406
7d27a96d
TT
15407 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15408
0d564a31 15409 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15410 function. For older versions of GCC, this is an offset in the
15411 appropriate virtual table, as specified by DW_AT_containing_type.
15412 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15413 to the object address. */
15414
e142c38c 15415 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15416 if (attr != nullptr)
8e19ed76 15417 {
9d2246fc 15418 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15419 {
9d2246fc
TT
15420 struct dwarf_block *block = attr->as_block ();
15421
15422 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15423 {
15424 /* Old-style GCC. */
9d2246fc 15425 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15426 }
9d2246fc
TT
15427 else if (block->data[0] == DW_OP_deref
15428 || (block->size > 1
15429 && block->data[0] == DW_OP_deref_size
15430 && block->data[1] == cu->header.addr_size))
aec5aa8b 15431 {
9d2246fc 15432 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15433 if ((fnp->voffset % cu->header.addr_size) != 0)
15434 dwarf2_complex_location_expr_complaint ();
15435 else
15436 fnp->voffset /= cu->header.addr_size;
15437 fnp->voffset += 2;
15438 }
15439 else
15440 dwarf2_complex_location_expr_complaint ();
15441
15442 if (!fnp->fcontext)
7e993ebf
KS
15443 {
15444 /* If there is no `this' field and no DW_AT_containing_type,
15445 we cannot actually find a base class context for the
15446 vtable! */
1f704f76 15447 if (this_type->num_fields () == 0
7e993ebf
KS
15448 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15449 {
b98664d3 15450 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15451 "function \"%s\" (offset %s)"),
15452 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15453 }
15454 else
15455 {
15456 fnp->fcontext
940da03e 15457 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15458 }
15459 }
aec5aa8b 15460 }
cd6c91b4 15461 else if (attr->form_is_section_offset ())
dda83cd7 15462 {
4d3c2250 15463 dwarf2_complex_location_expr_complaint ();
dda83cd7 15464 }
8e19ed76 15465 else
dda83cd7 15466 {
4d3c2250
KB
15467 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15468 fieldname);
dda83cd7 15469 }
0d564a31 15470 }
d48cc9dd
DJ
15471 else
15472 {
15473 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15474 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15475 {
15476 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15477 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15478 "but the vtable offset is not specified"),
9d8780f0 15479 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15480 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15481 TYPE_CPLUS_DYNAMIC (type) = 1;
15482 }
15483 }
c906108c
SS
15484}
15485
15486/* Create the vector of member function fields, and attach it to the type. */
15487
15488static void
fba45db2 15489dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15490 struct dwarf2_cu *cu)
c906108c 15491{
b4ba55a1 15492 if (cu->language == language_ada)
a73c6dcd 15493 error (_("unexpected member functions in Ada type"));
b4ba55a1 15494
c906108c
SS
15495 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15496 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15497 TYPE_ALLOC (type,
15498 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15499
be2daae6 15500 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15501 {
be2daae6 15502 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15503 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15504
be2daae6
TT
15505 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15506 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15507 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15508 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15509
15510 for (int k = 0; k < nf.fnfields.size (); ++k)
15511 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15512 }
15513
be2daae6 15514 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15515}
15516
1168df01
JB
15517/* Returns non-zero if NAME is the name of a vtable member in CU's
15518 language, zero otherwise. */
15519static int
15520is_vtable_name (const char *name, struct dwarf2_cu *cu)
15521{
15522 static const char vptr[] = "_vptr";
15523
9c37b5ae
TT
15524 /* Look for the C++ form of the vtable. */
15525 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15526 return 1;
15527
15528 return 0;
15529}
15530
c0dd20ea 15531/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15532 functions, with the ABI-specified layout. If TYPE describes
15533 such a structure, smash it into a member function type.
61049d3b
DJ
15534
15535 GCC shouldn't do this; it should just output pointer to member DIEs.
15536 This is GCC PR debug/28767. */
c0dd20ea 15537
0b92b5bb
TT
15538static void
15539quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15540{
09e2d7c7 15541 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15542
15543 /* Check for a structure with no name and two children. */
1f704f76 15544 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15545 return;
c0dd20ea
DJ
15546
15547 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15548 if (TYPE_FIELD_NAME (type, 0) == NULL
15549 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15550 || TYPE_FIELD_NAME (type, 1) == NULL
15551 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15552 return;
c0dd20ea
DJ
15553
15554 /* Find the type of the method. */
940da03e 15555 pfn_type = type->field (0).type ();
c0dd20ea 15556 if (pfn_type == NULL
78134374
SM
15557 || pfn_type->code () != TYPE_CODE_PTR
15558 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15559 return;
c0dd20ea
DJ
15560
15561 /* Look for the "this" argument. */
15562 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15563 if (pfn_type->num_fields () == 0
940da03e
SM
15564 /* || pfn_type->field (0).type () == NULL */
15565 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15566 return;
c0dd20ea 15567
940da03e 15568 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15569 new_type = alloc_type (objfile);
09e2d7c7 15570 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15571 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15572 pfn_type->has_varargs ());
0b92b5bb 15573 smash_to_methodptr_type (type, new_type);
c0dd20ea 15574}
1168df01 15575
e26624c6
TT
15576/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15577 requires rewriting, then copy it and return the updated copy.
15578 Otherwise return nullptr. */
15579
15580static struct type *
15581rewrite_array_type (struct type *type)
15582{
15583 if (type->code () != TYPE_CODE_ARRAY)
15584 return nullptr;
15585
15586 struct type *index_type = type->index_type ();
15587 range_bounds *current_bounds = index_type->bounds ();
15588
15589 /* Handle multi-dimensional arrays. */
15590 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15591 if (new_target == nullptr)
15592 {
15593 /* Maybe we don't need to rewrite this array. */
15594 if (current_bounds->low.kind () == PROP_CONST
15595 && current_bounds->high.kind () == PROP_CONST)
15596 return nullptr;
15597 }
15598
15599 /* Either the target type was rewritten, or the bounds have to be
15600 updated. Either way we want to copy the type and update
15601 everything. */
15602 struct type *copy = copy_type (type);
15603 int nfields = copy->num_fields ();
15604 field *new_fields
15605 = ((struct field *) TYPE_ZALLOC (copy,
15606 nfields * sizeof (struct field)));
15607 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15608 copy->set_fields (new_fields);
15609 if (new_target != nullptr)
15610 TYPE_TARGET_TYPE (copy) = new_target;
15611
15612 struct type *index_copy = copy_type (index_type);
15613 range_bounds *bounds
15614 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15615 sizeof (range_bounds));
15616 *bounds = *current_bounds;
15617 bounds->low.set_const_val (1);
15618 bounds->high.set_const_val (0);
15619 index_copy->set_bounds (bounds);
15620 copy->set_index_type (index_copy);
15621
15622 return copy;
15623}
15624
57567375
TT
15625/* While some versions of GCC will generate complicated DWARF for an
15626 array (see quirk_ada_thick_pointer), more recent versions were
15627 modified to emit an explicit thick pointer structure. However, in
15628 this case, the array still has DWARF expressions for its ranges,
15629 and these must be ignored. */
15630
15631static void
15632quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15633 struct type *type)
15634{
15635 gdb_assert (cu->language == language_ada);
15636
15637 /* Check for a structure with two children. */
15638 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15639 return;
15640
15641 /* Check for P_ARRAY and P_BOUNDS members. */
15642 if (TYPE_FIELD_NAME (type, 0) == NULL
15643 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15644 || TYPE_FIELD_NAME (type, 1) == NULL
15645 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15646 return;
15647
15648 /* Make sure we're looking at a pointer to an array. */
15649 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15650 return;
57567375 15651
e26624c6
TT
15652 /* The Ada code already knows how to handle these types, so all that
15653 we need to do is turn the bounds into static bounds. However, we
15654 don't want to rewrite existing array or index types in-place,
15655 because those may be referenced in other contexts where this
15656 rewriting is undesirable. */
15657 struct type *new_ary_type
15658 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15659 if (new_ary_type != nullptr)
15660 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15661}
15662
2b4424c3
TT
15663/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15664 appropriate error checking and issuing complaints if there is a
15665 problem. */
15666
15667static ULONGEST
15668get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15669{
15670 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15671
15672 if (attr == nullptr)
15673 return 0;
15674
cd6c91b4 15675 if (!attr->form_is_constant ())
2b4424c3 15676 {
b98664d3 15677 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15678 " - DIE at %s [in module %s]"),
15679 sect_offset_str (die->sect_off),
5e22e966 15680 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15681 return 0;
15682 }
15683
529908cb
TT
15684 LONGEST val = attr->constant_value (0);
15685 if (val < 0)
2b4424c3 15686 {
529908cb
TT
15687 complaint (_("DW_AT_alignment value must not be negative"
15688 " - DIE at %s [in module %s]"),
15689 sect_offset_str (die->sect_off),
15690 objfile_name (cu->per_objfile->objfile));
15691 return 0;
2b4424c3 15692 }
529908cb 15693 ULONGEST align = val;
2b4424c3
TT
15694
15695 if (align == 0)
15696 {
b98664d3 15697 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15698 " - DIE at %s [in module %s]"),
15699 sect_offset_str (die->sect_off),
5e22e966 15700 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15701 return 0;
15702 }
15703 if ((align & (align - 1)) != 0)
15704 {
b98664d3 15705 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15706 " - DIE at %s [in module %s]"),
15707 sect_offset_str (die->sect_off),
5e22e966 15708 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15709 return 0;
15710 }
15711
15712 return align;
15713}
15714
15715/* If the DIE has a DW_AT_alignment attribute, use its value to set
15716 the alignment for TYPE. */
15717
15718static void
15719maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15720 struct type *type)
15721{
15722 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15723 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15724 " - DIE at %s [in module %s]"),
15725 sect_offset_str (die->sect_off),
5e22e966 15726 objfile_name (cu->per_objfile->objfile));
2b4424c3 15727}
685b1105 15728
e35000a7
TBA
15729/* Check if the given VALUE is a valid enum dwarf_calling_convention
15730 constant for a type, according to DWARF5 spec, Table 5.5. */
15731
15732static bool
15733is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15734{
15735 switch (value)
15736 {
15737 case DW_CC_normal:
15738 case DW_CC_pass_by_reference:
15739 case DW_CC_pass_by_value:
15740 return true;
15741
15742 default:
15743 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15744 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15745 return false;
15746 }
15747}
15748
d0922fcf
TBA
15749/* Check if the given VALUE is a valid enum dwarf_calling_convention
15750 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15751 also according to GNU-specific values (see include/dwarf2.h). */
15752
15753static bool
15754is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15755{
15756 switch (value)
15757 {
15758 case DW_CC_normal:
15759 case DW_CC_program:
15760 case DW_CC_nocall:
15761 return true;
15762
15763 case DW_CC_GNU_renesas_sh:
15764 case DW_CC_GNU_borland_fastcall_i386:
15765 case DW_CC_GDB_IBM_OpenCL:
15766 return true;
15767
15768 default:
15769 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15770 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15771 return false;
15772 }
15773}
15774
c906108c 15775/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15776 (definition) to create a type for the structure or union. Fill in
15777 the type's name and general properties; the members will not be
83655187
DE
15778 processed until process_structure_scope. A symbol table entry for
15779 the type will also not be done until process_structure_scope (assuming
15780 the type has a name).
c906108c 15781
c767944b
DJ
15782 NOTE: we need to call these functions regardless of whether or not the
15783 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15784 structure or union. This gets the type entered into our set of
83655187 15785 user defined types. */
c906108c 15786
f792889a 15787static struct type *
134d01f1 15788read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15789{
5e22e966 15790 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15791 struct type *type;
15792 struct attribute *attr;
15d034d0 15793 const char *name;
c906108c 15794
348e048f
DE
15795 /* If the definition of this type lives in .debug_types, read that type.
15796 Don't follow DW_AT_specification though, that will take us back up
15797 the chain and we want to go down. */
052c8bb8 15798 attr = die->attr (DW_AT_signature);
435d3d88 15799 if (attr != nullptr)
348e048f 15800 {
ac9ec31b 15801 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15802
ac9ec31b 15803 /* The type's CU may not be the same as CU.
02142a6c 15804 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15805 return set_die_type (die, type, cu);
15806 }
15807
c0dd20ea 15808 type = alloc_type (objfile);
c906108c 15809 INIT_CPLUS_SPECIFIC (type);
93311388 15810
39cbfefa
DJ
15811 name = dwarf2_name (die, cu);
15812 if (name != NULL)
c906108c 15813 {
987504bb 15814 if (cu->language == language_cplus
c44af4eb
TT
15815 || cu->language == language_d
15816 || cu->language == language_rust)
63d06c5c 15817 {
15d034d0 15818 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15819
15820 /* dwarf2_full_name might have already finished building the DIE's
15821 type. If so, there is no need to continue. */
15822 if (get_die_type (die, cu) != NULL)
15823 return get_die_type (die, cu);
15824
d0e39ea2 15825 type->set_name (full_name);
63d06c5c
DC
15826 }
15827 else
15828 {
d8151005
DJ
15829 /* The name is already allocated along with this objfile, so
15830 we don't need to duplicate it for the type. */
d0e39ea2 15831 type->set_name (name);
63d06c5c 15832 }
c906108c
SS
15833 }
15834
15835 if (die->tag == DW_TAG_structure_type)
15836 {
67607e24 15837 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15838 }
15839 else if (die->tag == DW_TAG_union_type)
15840 {
67607e24 15841 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15842 }
15843 else
15844 {
67607e24 15845 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15846 }
15847
0cc2414c 15848 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
aa70e35c 15849 type->set_is_declared_class (true);
0cc2414c 15850
e35000a7
TBA
15851 /* Store the calling convention in the type if it's available in
15852 the die. Otherwise the calling convention remains set to
15853 the default value DW_CC_normal. */
15854 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15855 if (attr != nullptr
529908cb 15856 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
15857 {
15858 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15859 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 15860 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
15861 }
15862
e142c38c 15863 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15864 if (attr != nullptr)
c906108c 15865 {
cd6c91b4 15866 if (attr->form_is_constant ())
dda83cd7 15867 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
15868 else
15869 {
f8e89861 15870 struct dynamic_prop prop;
293e7e51 15871 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15872 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 15873 TYPE_LENGTH (type) = 0;
155bfbd3 15874 }
c906108c
SS
15875 }
15876 else
15877 {
15878 TYPE_LENGTH (type) = 0;
15879 }
15880
2b4424c3
TT
15881 maybe_set_alignment (cu, die, type);
15882
5230b05a 15883 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15884 {
5230b05a
WT
15885 /* ICC<14 does not output the required DW_AT_declaration on
15886 incomplete types, but gives them a size of zero. */
b4b73759 15887 type->set_is_stub (true);
685b1105
JK
15888 }
15889 else
9baccff6 15890 type->set_stub_is_supported (true);
685b1105 15891
dc718098 15892 if (die_is_declaration (die, cu))
b4b73759 15893 type->set_is_stub (true);
a6c727b2
DJ
15894 else if (attr == NULL && die->child == NULL
15895 && producer_is_realview (cu->producer))
15896 /* RealView does not output the required DW_AT_declaration
15897 on incomplete types. */
b4b73759 15898 type->set_is_stub (true);
dc718098 15899
c906108c
SS
15900 /* We need to add the type field to the die immediately so we don't
15901 infinitely recurse when dealing with pointers to the structure
0963b4bd 15902 type within the structure itself. */
1c379e20 15903 set_die_type (die, type, cu);
c906108c 15904
7e314c57
JK
15905 /* set_die_type should be already done. */
15906 set_descriptive_type (type, die, cu);
15907
c767944b
DJ
15908 return type;
15909}
15910
9c6a1327
TT
15911static void handle_struct_member_die
15912 (struct die_info *child_die,
15913 struct type *type,
15914 struct field_info *fi,
15915 std::vector<struct symbol *> *template_args,
15916 struct dwarf2_cu *cu);
15917
15918/* A helper for handle_struct_member_die that handles
15919 DW_TAG_variant_part. */
15920
15921static void
15922handle_variant_part (struct die_info *die, struct type *type,
15923 struct field_info *fi,
15924 std::vector<struct symbol *> *template_args,
15925 struct dwarf2_cu *cu)
15926{
15927 variant_part_builder *new_part;
15928 if (fi->current_variant_part == nullptr)
15929 {
15930 fi->variant_parts.emplace_back ();
15931 new_part = &fi->variant_parts.back ();
15932 }
15933 else if (!fi->current_variant_part->processing_variant)
15934 {
15935 complaint (_("nested DW_TAG_variant_part seen "
15936 "- DIE at %s [in module %s]"),
15937 sect_offset_str (die->sect_off),
5e22e966 15938 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15939 return;
15940 }
15941 else
15942 {
15943 variant_field &current = fi->current_variant_part->variants.back ();
15944 current.variant_parts.emplace_back ();
15945 new_part = &current.variant_parts.back ();
15946 }
15947
15948 /* When we recurse, we want callees to add to this new variant
15949 part. */
15950 scoped_restore save_current_variant_part
15951 = make_scoped_restore (&fi->current_variant_part, new_part);
15952
15953 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15954 if (discr == NULL)
15955 {
15956 /* It's a univariant form, an extension we support. */
15957 }
15958 else if (discr->form_is_ref ())
15959 {
15960 struct dwarf2_cu *target_cu = cu;
15961 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15962
15963 new_part->discriminant_offset = target_die->sect_off;
15964 }
15965 else
15966 {
15967 complaint (_("DW_AT_discr does not have DIE reference form"
15968 " - DIE at %s [in module %s]"),
15969 sect_offset_str (die->sect_off),
5e22e966 15970 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15971 }
15972
15973 for (die_info *child_die = die->child;
15974 child_die != NULL;
15975 child_die = child_die->sibling)
15976 handle_struct_member_die (child_die, type, fi, template_args, cu);
15977}
15978
15979/* A helper for handle_struct_member_die that handles
15980 DW_TAG_variant. */
15981
15982static void
15983handle_variant (struct die_info *die, struct type *type,
15984 struct field_info *fi,
15985 std::vector<struct symbol *> *template_args,
15986 struct dwarf2_cu *cu)
15987{
15988 if (fi->current_variant_part == nullptr)
15989 {
15990 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15991 "- DIE at %s [in module %s]"),
15992 sect_offset_str (die->sect_off),
5e22e966 15993 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15994 return;
15995 }
15996 if (fi->current_variant_part->processing_variant)
15997 {
15998 complaint (_("nested DW_TAG_variant seen "
15999 "- DIE at %s [in module %s]"),
16000 sect_offset_str (die->sect_off),
5e22e966 16001 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16002 return;
16003 }
16004
16005 scoped_restore save_processing_variant
16006 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16007 true);
16008
16009 fi->current_variant_part->variants.emplace_back ();
16010 variant_field &variant = fi->current_variant_part->variants.back ();
16011 variant.first_field = fi->fields.size ();
16012
16013 /* In a variant we want to get the discriminant and also add a
16014 field for our sole member child. */
16015 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16016 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16017 {
16018 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16019 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16020 variant.default_branch = true;
16021 else
9d2246fc 16022 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16023 }
16024 else
cae21f8e 16025 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16026
16027 for (die_info *variant_child = die->child;
16028 variant_child != NULL;
16029 variant_child = variant_child->sibling)
16030 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16031
16032 variant.last_field = fi->fields.size ();
16033}
16034
2ddeaf8a
TT
16035/* A helper for process_structure_scope that handles a single member
16036 DIE. */
16037
16038static void
16039handle_struct_member_die (struct die_info *child_die, struct type *type,
16040 struct field_info *fi,
16041 std::vector<struct symbol *> *template_args,
16042 struct dwarf2_cu *cu)
16043{
16044 if (child_die->tag == DW_TAG_member
9c6a1327 16045 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16046 {
16047 /* NOTE: carlton/2002-11-05: A C++ static data member
16048 should be a DW_TAG_member that is a declaration, but
16049 all versions of G++ as of this writing (so through at
16050 least 3.2.1) incorrectly generate DW_TAG_variable
16051 tags for them instead. */
16052 dwarf2_add_field (fi, child_die, cu);
16053 }
16054 else if (child_die->tag == DW_TAG_subprogram)
16055 {
16056 /* Rust doesn't have member functions in the C++ sense.
16057 However, it does emit ordinary functions as children
16058 of a struct DIE. */
16059 if (cu->language == language_rust)
16060 read_func_scope (child_die, cu);
16061 else
16062 {
16063 /* C++ member function. */
16064 dwarf2_add_member_fn (fi, child_die, type, cu);
16065 }
16066 }
16067 else if (child_die->tag == DW_TAG_inheritance)
16068 {
16069 /* C++ base class field. */
16070 dwarf2_add_field (fi, child_die, cu);
16071 }
16072 else if (type_can_define_types (child_die))
16073 dwarf2_add_type_defn (fi, child_die, cu);
16074 else if (child_die->tag == DW_TAG_template_type_param
16075 || child_die->tag == DW_TAG_template_value_param)
16076 {
16077 struct symbol *arg = new_symbol (child_die, NULL, cu);
16078
16079 if (arg != NULL)
16080 template_args->push_back (arg);
16081 }
9c6a1327
TT
16082 else if (child_die->tag == DW_TAG_variant_part)
16083 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16084 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16085 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16086}
16087
c767944b
DJ
16088/* Finish creating a structure or union type, including filling in
16089 its members and creating a symbol for it. */
16090
16091static void
16092process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16093{
5e22e966 16094 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16095 struct die_info *child_die;
c767944b
DJ
16096 struct type *type;
16097
16098 type = get_die_type (die, cu);
16099 if (type == NULL)
16100 type = read_structure_type (die, cu);
16101
3e1d3d8c 16102 bool has_template_parameters = false;
e142c38c 16103 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16104 {
16105 struct field_info fi;
2f4732b0 16106 std::vector<struct symbol *> template_args;
c906108c 16107
639d11d3 16108 child_die = die->child;
c906108c
SS
16109
16110 while (child_die && child_die->tag)
16111 {
2ddeaf8a 16112 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16113 child_die = child_die->sibling;
c906108c
SS
16114 }
16115
34eaf542 16116 /* Attach template arguments to type. */
2f4732b0 16117 if (!template_args.empty ())
34eaf542 16118 {
3e1d3d8c 16119 has_template_parameters = true;
34eaf542 16120 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16121 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16122 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16123 = XOBNEWVEC (&objfile->objfile_obstack,
16124 struct symbol *,
16125 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16126 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16127 template_args.data (),
34eaf542
TT
16128 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16129 * sizeof (struct symbol *)));
34eaf542
TT
16130 }
16131
c906108c 16132 /* Attach fields and member functions to the type. */
317f7127 16133 if (fi.nfields () > 0)
e7c27a73 16134 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16135 if (!fi.fnfieldlists.empty ())
c906108c 16136 {
e7c27a73 16137 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16138
c5aa993b 16139 /* Get the type which refers to the base class (possibly this
c906108c 16140 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16141 class from the DW_AT_containing_type attribute. This use of
16142 DW_AT_containing_type is a GNU extension. */
c906108c 16143
e142c38c 16144 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16145 {
e7c27a73 16146 struct type *t = die_containing_type (die, cu);
c906108c 16147
ae6ae975 16148 set_type_vptr_basetype (type, t);
c906108c
SS
16149 if (type == t)
16150 {
c906108c
SS
16151 int i;
16152
16153 /* Our own class provides vtbl ptr. */
1f704f76 16154 for (i = t->num_fields () - 1;
c906108c
SS
16155 i >= TYPE_N_BASECLASSES (t);
16156 --i)
16157 {
0d5cff50 16158 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16159
dda83cd7 16160 if (is_vtable_name (fieldname, cu))
c906108c 16161 {
ae6ae975 16162 set_type_vptr_fieldno (type, i);
c906108c
SS
16163 break;
16164 }
16165 }
16166
16167 /* Complain if virtual function table field not found. */
16168 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16169 complaint (_("virtual function table pointer "
3e43a32a 16170 "not found when defining class '%s'"),
7d93a1e0 16171 type->name () ? type->name () : "");
c906108c
SS
16172 }
16173 else
16174 {
ae6ae975 16175 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16176 }
16177 }
f6235d4c 16178 else if (cu->producer
61012eef 16179 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16180 {
16181 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16182 of the containing type, but the vtable pointer is
16183 always named __vfp. */
f6235d4c
EZ
16184
16185 int i;
16186
1f704f76 16187 for (i = type->num_fields () - 1;
f6235d4c
EZ
16188 i >= TYPE_N_BASECLASSES (type);
16189 --i)
16190 {
16191 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16192 {
ae6ae975
DE
16193 set_type_vptr_fieldno (type, i);
16194 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16195 break;
16196 }
16197 }
16198 }
c906108c 16199 }
98751a41
JK
16200
16201 /* Copy fi.typedef_field_list linked list elements content into the
16202 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16203 if (!fi.typedef_field_list.empty ())
98751a41 16204 {
be2daae6 16205 int count = fi.typedef_field_list.size ();
98751a41 16206
a0d7a4ff 16207 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16208 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16209 = ((struct decl_field *)
be2daae6
TT
16210 TYPE_ALLOC (type,
16211 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16212 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16213
be2daae6
TT
16214 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16215 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16216 }
c767944b 16217
883fd55a
KS
16218 /* Copy fi.nested_types_list linked list elements content into the
16219 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16220 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16221 {
be2daae6 16222 int count = fi.nested_types_list.size ();
883fd55a
KS
16223
16224 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16225 TYPE_NESTED_TYPES_ARRAY (type)
16226 = ((struct decl_field *)
be2daae6
TT
16227 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16228 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16229
be2daae6
TT
16230 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16231 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16232 }
c906108c 16233 }
63d06c5c 16234
bb5ed363 16235 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16236 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16237 cu->rust_unions.push_back (type);
57567375
TT
16238 else if (cu->language == language_ada)
16239 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16240
90aeadfc
DC
16241 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16242 snapshots) has been known to create a die giving a declaration
16243 for a class that has, as a child, a die giving a definition for a
16244 nested class. So we have to process our children even if the
16245 current die is a declaration. Normally, of course, a declaration
16246 won't have any children at all. */
134d01f1 16247
ca040673
DE
16248 child_die = die->child;
16249
90aeadfc
DC
16250 while (child_die != NULL && child_die->tag)
16251 {
16252 if (child_die->tag == DW_TAG_member
16253 || child_die->tag == DW_TAG_variable
34eaf542
TT
16254 || child_die->tag == DW_TAG_inheritance
16255 || child_die->tag == DW_TAG_template_value_param
16256 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16257 {
90aeadfc 16258 /* Do nothing. */
134d01f1 16259 }
90aeadfc
DC
16260 else
16261 process_die (child_die, cu);
134d01f1 16262
436c571c 16263 child_die = child_die->sibling;
134d01f1
DJ
16264 }
16265
fa4028e9
JB
16266 /* Do not consider external references. According to the DWARF standard,
16267 these DIEs are identified by the fact that they have no byte_size
16268 attribute, and a declaration attribute. */
16269 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16270 || !die_is_declaration (die, cu)
16271 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16272 {
16273 struct symbol *sym = new_symbol (die, type, cu);
16274
16275 if (has_template_parameters)
16276 {
a776957c
TT
16277 struct symtab *symtab;
16278 if (sym != nullptr)
16279 symtab = symbol_symtab (sym);
16280 else if (cu->line_header != nullptr)
16281 {
16282 /* Any related symtab will do. */
16283 symtab
7ba99d21 16284 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16285 }
16286 else
16287 {
16288 symtab = nullptr;
16289 complaint (_("could not find suitable "
16290 "symtab for template parameter"
16291 " - DIE at %s [in module %s]"),
16292 sect_offset_str (die->sect_off),
16293 objfile_name (objfile));
16294 }
16295
16296 if (symtab != nullptr)
16297 {
16298 /* Make sure that the symtab is set on the new symbols.
16299 Even though they don't appear in this symtab directly,
16300 other parts of gdb assume that symbols do, and this is
16301 reasonably true. */
16302 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16303 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16304 }
3e1d3d8c
TT
16305 }
16306 }
134d01f1
DJ
16307}
16308
ed6acedd
TT
16309/* Assuming DIE is an enumeration type, and TYPE is its associated
16310 type, update TYPE using some information only available in DIE's
16311 children. In particular, the fields are computed. */
55426c9d
JB
16312
16313static void
16314update_enumeration_type_from_children (struct die_info *die,
16315 struct type *type,
16316 struct dwarf2_cu *cu)
16317{
60f7655a 16318 struct die_info *child_die;
55426c9d
JB
16319 int unsigned_enum = 1;
16320 int flag_enum = 1;
55426c9d 16321
8268c778 16322 auto_obstack obstack;
ed6acedd 16323 std::vector<struct field> fields;
55426c9d 16324
60f7655a
DE
16325 for (child_die = die->child;
16326 child_die != NULL && child_die->tag;
436c571c 16327 child_die = child_die->sibling)
55426c9d
JB
16328 {
16329 struct attribute *attr;
16330 LONGEST value;
16331 const gdb_byte *bytes;
16332 struct dwarf2_locexpr_baton *baton;
16333 const char *name;
60f7655a 16334
55426c9d
JB
16335 if (child_die->tag != DW_TAG_enumerator)
16336 continue;
16337
16338 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16339 if (attr == NULL)
16340 continue;
16341
16342 name = dwarf2_name (child_die, cu);
16343 if (name == NULL)
16344 name = "<anonymous enumerator>";
16345
16346 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16347 &value, &bytes, &baton);
16348 if (value < 0)
16349 {
16350 unsigned_enum = 0;
16351 flag_enum = 0;
16352 }
55426c9d 16353 else
edd45eb0
SM
16354 {
16355 if (count_one_bits_ll (value) >= 2)
16356 flag_enum = 0;
edd45eb0 16357 }
55426c9d 16358
ed6acedd
TT
16359 fields.emplace_back ();
16360 struct field &field = fields.back ();
16361 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16362 SET_FIELD_ENUMVAL (field, value);
16363 }
16364
16365 if (!fields.empty ())
16366 {
5e33d5f4 16367 type->set_num_fields (fields.size ());
3cabb6b0
SM
16368 type->set_fields
16369 ((struct field *)
16370 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16371 memcpy (type->fields (), fields.data (),
ed6acedd 16372 sizeof (struct field) * fields.size ());
55426c9d
JB
16373 }
16374
16375 if (unsigned_enum)
653223d3
SM
16376 type->set_is_unsigned (true);
16377
55426c9d 16378 if (flag_enum)
9902b327 16379 type->set_is_flag_enum (true);
55426c9d
JB
16380}
16381
134d01f1
DJ
16382/* Given a DW_AT_enumeration_type die, set its type. We do not
16383 complete the type's fields yet, or create any symbols. */
c906108c 16384
f792889a 16385static struct type *
134d01f1 16386read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16387{
5e22e966 16388 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16389 struct type *type;
c906108c 16390 struct attribute *attr;
0114d602 16391 const char *name;
134d01f1 16392
348e048f
DE
16393 /* If the definition of this type lives in .debug_types, read that type.
16394 Don't follow DW_AT_specification though, that will take us back up
16395 the chain and we want to go down. */
052c8bb8 16396 attr = die->attr (DW_AT_signature);
435d3d88 16397 if (attr != nullptr)
348e048f 16398 {
ac9ec31b 16399 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16400
ac9ec31b 16401 /* The type's CU may not be the same as CU.
02142a6c 16402 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16403 return set_die_type (die, type, cu);
16404 }
16405
c906108c
SS
16406 type = alloc_type (objfile);
16407
67607e24 16408 type->set_code (TYPE_CODE_ENUM);
94af9270 16409 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16410 if (name != NULL)
d0e39ea2 16411 type->set_name (name);
c906108c 16412
0626fc76
TT
16413 attr = dwarf2_attr (die, DW_AT_type, cu);
16414 if (attr != NULL)
16415 {
16416 struct type *underlying_type = die_type (die, cu);
16417
16418 TYPE_TARGET_TYPE (type) = underlying_type;
16419 }
16420
e142c38c 16421 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16422 if (attr != nullptr)
c906108c 16423 {
529908cb 16424 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16425 }
16426 else
16427 {
16428 TYPE_LENGTH (type) = 0;
16429 }
16430
2b4424c3
TT
16431 maybe_set_alignment (cu, die, type);
16432
137033e9
JB
16433 /* The enumeration DIE can be incomplete. In Ada, any type can be
16434 declared as private in the package spec, and then defined only
16435 inside the package body. Such types are known as Taft Amendment
16436 Types. When another package uses such a type, an incomplete DIE
16437 may be generated by the compiler. */
02eb380e 16438 if (die_is_declaration (die, cu))
b4b73759 16439 type->set_is_stub (true);
02eb380e 16440
0626fc76
TT
16441 /* If this type has an underlying type that is not a stub, then we
16442 may use its attributes. We always use the "unsigned" attribute
16443 in this situation, because ordinarily we guess whether the type
16444 is unsigned -- but the guess can be wrong and the underlying type
16445 can tell us the reality. However, we defer to a local size
16446 attribute if one exists, because this lets the compiler override
16447 the underlying type if needed. */
e46d3488 16448 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16449 {
9e7c9a03
HD
16450 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16451 underlying_type = check_typedef (underlying_type);
653223d3
SM
16452
16453 type->set_is_unsigned (underlying_type->is_unsigned ());
16454
0626fc76 16455 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16456 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16457
2b4424c3 16458 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16459 && TYPE_RAW_ALIGN (underlying_type) != 0)
16460 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16461 }
16462
aa70e35c 16463 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
3d567982 16464
ed6acedd
TT
16465 set_die_type (die, type, cu);
16466
16467 /* Finish the creation of this type by using the enum's children.
16468 Note that, as usual, this must come after set_die_type to avoid
16469 infinite recursion when trying to compute the names of the
16470 enumerators. */
16471 update_enumeration_type_from_children (die, type, cu);
16472
16473 return type;
134d01f1
DJ
16474}
16475
16476/* Given a pointer to a die which begins an enumeration, process all
16477 the dies that define the members of the enumeration, and create the
16478 symbol for the enumeration type.
16479
16480 NOTE: We reverse the order of the element list. */
16481
16482static void
16483process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16484{
f792889a 16485 struct type *this_type;
134d01f1 16486
f792889a
DJ
16487 this_type = get_die_type (die, cu);
16488 if (this_type == NULL)
16489 this_type = read_enumeration_type (die, cu);
9dc481d3 16490
639d11d3 16491 if (die->child != NULL)
c906108c 16492 {
9dc481d3 16493 struct die_info *child_die;
15d034d0 16494 const char *name;
9dc481d3 16495
639d11d3 16496 child_die = die->child;
c906108c
SS
16497 while (child_die && child_die->tag)
16498 {
16499 if (child_die->tag != DW_TAG_enumerator)
16500 {
e7c27a73 16501 process_die (child_die, cu);
c906108c
SS
16502 }
16503 else
16504 {
39cbfefa
DJ
16505 name = dwarf2_name (child_die, cu);
16506 if (name)
ed6acedd 16507 new_symbol (child_die, this_type, cu);
c906108c
SS
16508 }
16509
436c571c 16510 child_die = child_die->sibling;
c906108c 16511 }
c906108c 16512 }
134d01f1 16513
6c83ed52
TT
16514 /* If we are reading an enum from a .debug_types unit, and the enum
16515 is a declaration, and the enum is not the signatured type in the
16516 unit, then we do not want to add a symbol for it. Adding a
16517 symbol would in some cases obscure the true definition of the
16518 enum, giving users an incomplete type when the definition is
16519 actually available. Note that we do not want to do this for all
16520 enums which are just declarations, because C++0x allows forward
16521 enum declarations. */
3019eac3 16522 if (cu->per_cu->is_debug_types
6c83ed52
TT
16523 && die_is_declaration (die, cu))
16524 {
52dc124a 16525 struct signatured_type *sig_type;
6c83ed52 16526
c0f78cd4 16527 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16528 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16529 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16530 return;
16531 }
16532
f792889a 16533 new_symbol (die, this_type, cu);
c906108c
SS
16534}
16535
57567375
TT
16536/* Helper function for quirk_ada_thick_pointer that examines a bounds
16537 expression for an index type and finds the corresponding field
16538 offset in the hidden "P_BOUNDS" structure. Returns true on success
16539 and updates *FIELD, false if it fails to recognize an
16540 expression. */
16541
16542static bool
16543recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16544 int *bounds_offset, struct field *field,
16545 struct dwarf2_cu *cu)
16546{
16547 struct attribute *attr = dwarf2_attr (die, name, cu);
16548 if (attr == nullptr || !attr->form_is_block ())
16549 return false;
16550
16551 const struct dwarf_block *block = attr->as_block ();
16552 const gdb_byte *start = block->data;
16553 const gdb_byte *end = block->data + block->size;
16554
16555 /* The expression to recognize generally looks like:
16556
16557 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16558 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16559
16560 However, the second "plus_uconst" may be missing:
16561
16562 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16563 DW_OP_deref_size: 4)
16564
16565 This happens when the field is at the start of the structure.
16566
16567 Also, the final deref may not be sized:
16568
16569 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16570 DW_OP_deref)
16571
16572 This happens when the size of the index type happens to be the
16573 same as the architecture's word size. This can occur with or
16574 without the second plus_uconst. */
16575
16576 if (end - start < 2)
16577 return false;
16578 if (*start++ != DW_OP_push_object_address)
16579 return false;
16580 if (*start++ != DW_OP_plus_uconst)
16581 return false;
16582
16583 uint64_t this_bound_off;
16584 start = gdb_read_uleb128 (start, end, &this_bound_off);
16585 if (start == nullptr || (int) this_bound_off != this_bound_off)
16586 return false;
16587 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16588 is consistent among all bounds. */
16589 if (*bounds_offset == -1)
16590 *bounds_offset = this_bound_off;
16591 else if (*bounds_offset != this_bound_off)
16592 return false;
16593
16594 if (start == end || *start++ != DW_OP_deref)
16595 return false;
16596
16597 int offset = 0;
16598 if (start ==end)
16599 return false;
16600 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16601 {
16602 /* This means an offset of 0. */
16603 }
16604 else if (*start++ != DW_OP_plus_uconst)
16605 return false;
16606 else
16607 {
16608 /* The size is the parameter to DW_OP_plus_uconst. */
16609 uint64_t val;
16610 start = gdb_read_uleb128 (start, end, &val);
16611 if (start == nullptr)
16612 return false;
16613 if ((int) val != val)
16614 return false;
16615 offset = val;
16616 }
16617
16618 if (start == end)
16619 return false;
16620
16621 uint64_t size;
16622 if (*start == DW_OP_deref_size)
16623 {
16624 start = gdb_read_uleb128 (start + 1, end, &size);
16625 if (start == nullptr)
16626 return false;
16627 }
16628 else if (*start == DW_OP_deref)
16629 {
16630 size = cu->header.addr_size;
16631 ++start;
16632 }
16633 else
16634 return false;
16635
16636 SET_FIELD_BITPOS (*field, 8 * offset);
16637 if (size != TYPE_LENGTH (field->type ()))
16638 FIELD_BITSIZE (*field) = 8 * size;
16639
16640 return true;
16641}
16642
16643/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16644 some kinds of Ada arrays:
16645
16646 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16647 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16648 <11e0> DW_AT_data_location: 2 byte block: 97 6
16649 (DW_OP_push_object_address; DW_OP_deref)
16650 <11e3> DW_AT_type : <0x1173>
16651 <11e7> DW_AT_sibling : <0x1201>
16652 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16653 <11ec> DW_AT_type : <0x1206>
16654 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16655 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16656 DW_OP_deref_size: 4)
16657 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16658 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16659 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16660
16661 This actually represents a "thick pointer", which is a structure
16662 with two elements: one that is a pointer to the array data, and one
16663 that is a pointer to another structure; this second structure holds
16664 the array bounds.
16665
16666 This returns a new type on success, or nullptr if this didn't
16667 recognize the type. */
16668
16669static struct type *
16670quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16671 struct type *type)
16672{
16673 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16674 /* So far we've only seen this with block form. */
16675 if (attr == nullptr || !attr->form_is_block ())
16676 return nullptr;
16677
16678 /* Note that this will fail if the structure layout is changed by
16679 the compiler. However, we have no good way to recognize some
16680 other layout, because we don't know what expression the compiler
16681 might choose to emit should this happen. */
16682 struct dwarf_block *blk = attr->as_block ();
16683 if (blk->size != 2
16684 || blk->data[0] != DW_OP_push_object_address
16685 || blk->data[1] != DW_OP_deref)
16686 return nullptr;
16687
16688 int bounds_offset = -1;
16689 int max_align = -1;
16690 std::vector<struct field> range_fields;
16691 for (struct die_info *child_die = die->child;
16692 child_die;
16693 child_die = child_die->sibling)
16694 {
16695 if (child_die->tag == DW_TAG_subrange_type)
16696 {
16697 struct type *underlying = read_subrange_index_type (child_die, cu);
16698
16699 int this_align = type_align (underlying);
16700 if (this_align > max_align)
16701 max_align = this_align;
16702
16703 range_fields.emplace_back ();
16704 range_fields.emplace_back ();
16705
16706 struct field &lower = range_fields[range_fields.size () - 2];
16707 struct field &upper = range_fields[range_fields.size () - 1];
16708
16709 lower.set_type (underlying);
16710 FIELD_ARTIFICIAL (lower) = 1;
16711
16712 upper.set_type (underlying);
16713 FIELD_ARTIFICIAL (upper) = 1;
16714
16715 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16716 &bounds_offset, &lower, cu)
16717 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16718 &bounds_offset, &upper, cu))
16719 return nullptr;
16720 }
16721 }
16722
16723 /* This shouldn't really happen, but double-check that we found
16724 where the bounds are stored. */
16725 if (bounds_offset == -1)
16726 return nullptr;
16727
16728 struct objfile *objfile = cu->per_objfile->objfile;
16729 for (int i = 0; i < range_fields.size (); i += 2)
16730 {
16731 char name[20];
16732
16733 /* Set the name of each field in the bounds. */
16734 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16735 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16736 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16737 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16738 }
16739
16740 struct type *bounds = alloc_type (objfile);
16741 bounds->set_code (TYPE_CODE_STRUCT);
16742
16743 bounds->set_num_fields (range_fields.size ());
16744 bounds->set_fields
16745 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16746 * sizeof (struct field))));
16747 memcpy (bounds->fields (), range_fields.data (),
16748 bounds->num_fields () * sizeof (struct field));
16749
16750 int last_fieldno = range_fields.size () - 1;
16751 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16752 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16753 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16754
16755 /* Rewrite the existing array type in place. Specifically, we
16756 remove any dynamic properties we might have read, and we replace
16757 the index types. */
16758 struct type *iter = type;
16759 for (int i = 0; i < range_fields.size (); i += 2)
16760 {
16761 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16762 iter->main_type->dyn_prop_list = nullptr;
16763 iter->set_index_type
16764 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16765 iter = TYPE_TARGET_TYPE (iter);
16766 }
16767
16768 struct type *result = alloc_type (objfile);
16769 result->set_code (TYPE_CODE_STRUCT);
16770
16771 result->set_num_fields (2);
16772 result->set_fields
16773 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16774 * sizeof (struct field))));
16775
16776 /* The names are chosen to coincide with what the compiler does with
16777 -fgnat-encodings=all, which the Ada code in gdb already
16778 understands. */
16779 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16780 result->field (0).set_type (lookup_pointer_type (type));
16781
16782 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16783 result->field (1).set_type (lookup_pointer_type (bounds));
16784 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16785
16786 result->set_name (type->name ());
16787 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16788 + TYPE_LENGTH (result->field (1).type ()));
16789
16790 return result;
16791}
16792
c906108c
SS
16793/* Extract all information from a DW_TAG_array_type DIE and put it in
16794 the DIE's type field. For now, this only handles one dimensional
16795 arrays. */
16796
f792889a 16797static struct type *
e7c27a73 16798read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16799{
5e22e966 16800 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16801 struct die_info *child_die;
7e314c57 16802 struct type *type;
c906108c 16803 struct type *element_type, *range_type, *index_type;
c906108c 16804 struct attribute *attr;
15d034d0 16805 const char *name;
a405673c 16806 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16807 unsigned int bit_stride = 0;
c906108c 16808
e7c27a73 16809 element_type = die_type (die, cu);
c906108c 16810
7e314c57
JK
16811 /* The die_type call above may have already set the type for this DIE. */
16812 type = get_die_type (die, cu);
16813 if (type)
16814 return type;
16815
dc53a7ad
JB
16816 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16817 if (attr != NULL)
a405673c
JB
16818 {
16819 int stride_ok;
293e7e51 16820 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16821
16822 byte_stride_prop
16823 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16824 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16825 prop_type);
a405673c
JB
16826 if (!stride_ok)
16827 {
b98664d3 16828 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16829 " - DIE at %s [in module %s]"),
16830 sect_offset_str (die->sect_off),
5e22e966 16831 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16832 /* Ignore this attribute. We will likely not be able to print
16833 arrays of this type correctly, but there is little we can do
16834 to help if we cannot read the attribute's value. */
16835 byte_stride_prop = NULL;
16836 }
16837 }
dc53a7ad
JB
16838
16839 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16840 if (attr != NULL)
529908cb 16841 bit_stride = attr->constant_value (0);
dc53a7ad 16842
c906108c
SS
16843 /* Irix 6.2 native cc creates array types without children for
16844 arrays with unspecified length. */
639d11d3 16845 if (die->child == NULL)
c906108c 16846 {
46bf5051 16847 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16848 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16849 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16850 byte_stride_prop, bit_stride);
f792889a 16851 return set_die_type (die, type, cu);
c906108c
SS
16852 }
16853
791afaa2 16854 std::vector<struct type *> range_types;
639d11d3 16855 child_die = die->child;
c906108c
SS
16856 while (child_die && child_die->tag)
16857 {
16858 if (child_die->tag == DW_TAG_subrange_type)
16859 {
f792889a 16860 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16861
dda83cd7
SM
16862 if (child_type != NULL)
16863 {
0963b4bd 16864 /* The range type was succesfully read. Save it for the
dda83cd7 16865 array type creation. */
791afaa2 16866 range_types.push_back (child_type);
dda83cd7 16867 }
c906108c 16868 }
436c571c 16869 child_die = child_die->sibling;
c906108c
SS
16870 }
16871
cf2b2075
TV
16872 if (range_types.empty ())
16873 {
16874 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16875 sect_offset_str (die->sect_off),
16876 objfile_name (cu->per_objfile->objfile));
16877 return NULL;
16878 }
16879
c906108c
SS
16880 /* Dwarf2 dimensions are output from left to right, create the
16881 necessary array types in backwards order. */
7ca2d3a3 16882
c906108c 16883 type = element_type;
7ca2d3a3
DL
16884
16885 if (read_array_order (die, cu) == DW_ORD_col_major)
16886 {
16887 int i = 0;
9a619af0 16888
791afaa2 16889 while (i < range_types.size ())
10f6a3ad
TT
16890 {
16891 type = create_array_type_with_stride (NULL, type, range_types[i++],
16892 byte_stride_prop, bit_stride);
16893 bit_stride = 0;
16894 byte_stride_prop = nullptr;
16895 }
7ca2d3a3
DL
16896 }
16897 else
16898 {
791afaa2 16899 size_t ndim = range_types.size ();
7ca2d3a3 16900 while (ndim-- > 0)
10f6a3ad
TT
16901 {
16902 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16903 byte_stride_prop, bit_stride);
16904 bit_stride = 0;
16905 byte_stride_prop = nullptr;
16906 }
7ca2d3a3 16907 }
c906108c 16908
cf2b2075
TV
16909 gdb_assert (type != element_type);
16910
f5f8a009
EZ
16911 /* Understand Dwarf2 support for vector types (like they occur on
16912 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16913 array type. This is not part of the Dwarf2/3 standard yet, but a
16914 custom vendor extension. The main difference between a regular
16915 array and the vector variant is that vectors are passed by value
16916 to functions. */
e142c38c 16917 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16918 if (attr != nullptr)
ea37ba09 16919 make_vector_type (type);
f5f8a009 16920
dbc98a8b
KW
16921 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16922 implementation may choose to implement triple vectors using this
16923 attribute. */
16924 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 16925 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 16926 {
529908cb
TT
16927 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16928 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 16929 else
b98664d3 16930 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16931 "than the total size of elements"));
dbc98a8b
KW
16932 }
16933
39cbfefa
DJ
16934 name = dwarf2_name (die, cu);
16935 if (name)
d0e39ea2 16936 type->set_name (name);
6e70227d 16937
2b4424c3
TT
16938 maybe_set_alignment (cu, die, type);
16939
57567375
TT
16940 struct type *replacement_type = nullptr;
16941 if (cu->language == language_ada)
16942 {
16943 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16944 if (replacement_type != nullptr)
16945 type = replacement_type;
16946 }
16947
0963b4bd 16948 /* Install the type in the die. */
57567375 16949 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
16950
16951 /* set_die_type should be already done. */
b4ba55a1
JB
16952 set_descriptive_type (type, die, cu);
16953
7e314c57 16954 return type;
c906108c
SS
16955}
16956
7ca2d3a3 16957static enum dwarf_array_dim_ordering
6e70227d 16958read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16959{
16960 struct attribute *attr;
16961
16962 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16963
435d3d88 16964 if (attr != nullptr)
1bc397c5
TT
16965 {
16966 LONGEST val = attr->constant_value (-1);
16967 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16968 return (enum dwarf_array_dim_ordering) val;
16969 }
7ca2d3a3 16970
0963b4bd
MS
16971 /* GNU F77 is a special case, as at 08/2004 array type info is the
16972 opposite order to the dwarf2 specification, but data is still
16973 laid out as per normal fortran.
7ca2d3a3 16974
0963b4bd
MS
16975 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16976 version checking. */
7ca2d3a3 16977
905e0470
PM
16978 if (cu->language == language_fortran
16979 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16980 {
16981 return DW_ORD_row_major;
16982 }
16983
3a3440fb 16984 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
16985 {
16986 case array_column_major:
16987 return DW_ORD_col_major;
16988 case array_row_major:
16989 default:
16990 return DW_ORD_row_major;
16991 };
16992}
16993
72019c9c 16994/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16995 the DIE's type field. */
72019c9c 16996
f792889a 16997static struct type *
72019c9c
GM
16998read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16999{
7e314c57
JK
17000 struct type *domain_type, *set_type;
17001 struct attribute *attr;
f792889a 17002
7e314c57
JK
17003 domain_type = die_type (die, cu);
17004
17005 /* The die_type call above may have already set the type for this DIE. */
17006 set_type = get_die_type (die, cu);
17007 if (set_type)
17008 return set_type;
17009
17010 set_type = create_set_type (NULL, domain_type);
17011
17012 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17013 if (attr != nullptr && attr->form_is_unsigned ())
17014 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17015
2b4424c3
TT
17016 maybe_set_alignment (cu, die, set_type);
17017
f792889a 17018 return set_die_type (die, set_type, cu);
72019c9c 17019}
7ca2d3a3 17020
0971de02
TT
17021/* A helper for read_common_block that creates a locexpr baton.
17022 SYM is the symbol which we are marking as computed.
17023 COMMON_DIE is the DIE for the common block.
17024 COMMON_LOC is the location expression attribute for the common
17025 block itself.
17026 MEMBER_LOC is the location expression attribute for the particular
17027 member of the common block that we are processing.
17028 CU is the CU from which the above come. */
17029
17030static void
17031mark_common_block_symbol_computed (struct symbol *sym,
17032 struct die_info *common_die,
17033 struct attribute *common_loc,
17034 struct attribute *member_loc,
17035 struct dwarf2_cu *cu)
17036{
5e22e966 17037 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17038 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17039 struct dwarf2_locexpr_baton *baton;
17040 gdb_byte *ptr;
17041 unsigned int cu_off;
08feed99 17042 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17043 LONGEST offset = 0;
17044
17045 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17046 gdb_assert (common_loc->form_is_block ());
17047 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17048 || member_loc->form_is_constant ());
0971de02 17049
8d749320 17050 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17051 baton->per_objfile = per_objfile;
0971de02
TT
17052 baton->per_cu = cu->per_cu;
17053 gdb_assert (baton->per_cu);
17054
17055 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17056
cd6c91b4 17057 if (member_loc->form_is_constant ())
0971de02 17058 {
0826b30a 17059 offset = member_loc->constant_value (0);
0971de02
TT
17060 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17061 }
17062 else
9d2246fc 17063 baton->size += member_loc->as_block ()->size;
0971de02 17064
224c3ddb 17065 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17066 baton->data = ptr;
17067
17068 *ptr++ = DW_OP_call4;
9c541725 17069 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17070 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17071 ptr += 4;
17072
cd6c91b4 17073 if (member_loc->form_is_constant ())
0971de02
TT
17074 {
17075 *ptr++ = DW_OP_addr;
17076 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17077 ptr += cu->header.addr_size;
17078 }
17079 else
17080 {
17081 /* We have to copy the data here, because DW_OP_call4 will only
17082 use a DW_AT_location attribute. */
9d2246fc
TT
17083 struct dwarf_block *block = member_loc->as_block ();
17084 memcpy (ptr, block->data, block->size);
17085 ptr += block->size;
0971de02
TT
17086 }
17087
17088 *ptr++ = DW_OP_plus;
17089 gdb_assert (ptr - baton->data == baton->size);
17090
0971de02 17091 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17092 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17093}
17094
4357ac6c
TT
17095/* Create appropriate locally-scoped variables for all the
17096 DW_TAG_common_block entries. Also create a struct common_block
17097 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17098 is used to separate the common blocks name namespace from regular
4357ac6c 17099 variable names. */
c906108c
SS
17100
17101static void
e7c27a73 17102read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17103{
0971de02
TT
17104 struct attribute *attr;
17105
17106 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17107 if (attr != nullptr)
0971de02
TT
17108 {
17109 /* Support the .debug_loc offsets. */
4fc6c0d5 17110 if (attr->form_is_block ())
dda83cd7 17111 {
0971de02 17112 /* Ok. */
dda83cd7 17113 }
cd6c91b4 17114 else if (attr->form_is_section_offset ())
dda83cd7 17115 {
0971de02
TT
17116 dwarf2_complex_location_expr_complaint ();
17117 attr = NULL;
dda83cd7 17118 }
0971de02 17119 else
dda83cd7 17120 {
0971de02
TT
17121 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17122 "common block member");
17123 attr = NULL;
dda83cd7 17124 }
0971de02
TT
17125 }
17126
639d11d3 17127 if (die->child != NULL)
c906108c 17128 {
5e22e966 17129 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17130 struct die_info *child_die;
17131 size_t n_entries = 0, size;
17132 struct common_block *common_block;
17133 struct symbol *sym;
74ac6d43 17134
4357ac6c
TT
17135 for (child_die = die->child;
17136 child_die && child_die->tag;
436c571c 17137 child_die = child_die->sibling)
4357ac6c
TT
17138 ++n_entries;
17139
17140 size = (sizeof (struct common_block)
17141 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17142 common_block
17143 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17144 size);
4357ac6c
TT
17145 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17146 common_block->n_entries = 0;
17147
17148 for (child_die = die->child;
17149 child_die && child_die->tag;
436c571c 17150 child_die = child_die->sibling)
4357ac6c
TT
17151 {
17152 /* Create the symbol in the DW_TAG_common_block block in the current
17153 symbol scope. */
e7c27a73 17154 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17155 if (sym != NULL)
17156 {
17157 struct attribute *member_loc;
17158
17159 common_block->contents[common_block->n_entries++] = sym;
17160
17161 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17162 cu);
17163 if (member_loc)
17164 {
17165 /* GDB has handled this for a long time, but it is
17166 not specified by DWARF. It seems to have been
17167 emitted by gfortran at least as recently as:
17168 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17169 complaint (_("Variable in common block has "
0971de02 17170 "DW_AT_data_member_location "
9d8780f0
SM
17171 "- DIE at %s [in module %s]"),
17172 sect_offset_str (child_die->sect_off),
518817b3 17173 objfile_name (objfile));
0971de02 17174
cd6c91b4 17175 if (member_loc->form_is_section_offset ())
0971de02 17176 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17177 else if (member_loc->form_is_constant ()
4fc6c0d5 17178 || member_loc->form_is_block ())
0971de02 17179 {
435d3d88 17180 if (attr != nullptr)
0971de02
TT
17181 mark_common_block_symbol_computed (sym, die, attr,
17182 member_loc, cu);
17183 }
17184 else
17185 dwarf2_complex_location_expr_complaint ();
17186 }
17187 }
c906108c 17188 }
4357ac6c
TT
17189
17190 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17191 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17192 }
17193}
17194
0114d602 17195/* Create a type for a C++ namespace. */
d9fa45fe 17196
0114d602
DJ
17197static struct type *
17198read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17199{
5e22e966 17200 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17201 const char *previous_prefix, *name;
9219021c 17202 int is_anonymous;
0114d602
DJ
17203 struct type *type;
17204
17205 /* For extensions, reuse the type of the original namespace. */
17206 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17207 {
17208 struct die_info *ext_die;
17209 struct dwarf2_cu *ext_cu = cu;
9a619af0 17210
0114d602
DJ
17211 ext_die = dwarf2_extension (die, &ext_cu);
17212 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17213
17214 /* EXT_CU may not be the same as CU.
02142a6c 17215 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17216 return set_die_type (die, type, cu);
17217 }
9219021c 17218
e142c38c 17219 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17220
17221 /* Now build the name of the current namespace. */
17222
0114d602
DJ
17223 previous_prefix = determine_prefix (die, cu);
17224 if (previous_prefix[0] != '\0')
17225 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17226 previous_prefix, name, 0, cu);
0114d602
DJ
17227
17228 /* Create the type. */
19f392bc 17229 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17230
60531b24 17231 return set_die_type (die, type, cu);
0114d602
DJ
17232}
17233
22cee43f 17234/* Read a namespace scope. */
0114d602
DJ
17235
17236static void
17237read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17238{
5e22e966 17239 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17240 int is_anonymous;
9219021c 17241
5c4e30ca
DC
17242 /* Add a symbol associated to this if we haven't seen the namespace
17243 before. Also, add a using directive if it's an anonymous
17244 namespace. */
9219021c 17245
f2f0e013 17246 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17247 {
17248 struct type *type;
17249
0114d602 17250 type = read_type_die (die, cu);
e7c27a73 17251 new_symbol (die, type, cu);
5c4e30ca 17252
e8e80198 17253 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17254 if (is_anonymous)
0114d602
DJ
17255 {
17256 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17257
eb1e02fd 17258 std::vector<const char *> excludes;
804d2729 17259 add_using_directive (using_directives (cu),
7d93a1e0 17260 previous_prefix, type->name (), NULL,
eb1e02fd 17261 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17262 }
5c4e30ca 17263 }
9219021c 17264
639d11d3 17265 if (die->child != NULL)
d9fa45fe 17266 {
639d11d3 17267 struct die_info *child_die = die->child;
6e70227d 17268
d9fa45fe
DC
17269 while (child_die && child_die->tag)
17270 {
e7c27a73 17271 process_die (child_die, cu);
436c571c 17272 child_die = child_die->sibling;
d9fa45fe
DC
17273 }
17274 }
38d518c9
EZ
17275}
17276
f55ee35c
JK
17277/* Read a Fortran module as type. This DIE can be only a declaration used for
17278 imported module. Still we need that type as local Fortran "use ... only"
17279 declaration imports depend on the created type in determine_prefix. */
17280
17281static struct type *
17282read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17283{
5e22e966 17284 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17285 const char *module_name;
f55ee35c
JK
17286 struct type *type;
17287
17288 module_name = dwarf2_name (die, cu);
19f392bc 17289 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17290
f55ee35c
JK
17291 return set_die_type (die, type, cu);
17292}
17293
5d7cb8df
JK
17294/* Read a Fortran module. */
17295
17296static void
17297read_module (struct die_info *die, struct dwarf2_cu *cu)
17298{
17299 struct die_info *child_die = die->child;
530e8392
KB
17300 struct type *type;
17301
17302 type = read_type_die (die, cu);
17303 new_symbol (die, type, cu);
5d7cb8df 17304
5d7cb8df
JK
17305 while (child_die && child_die->tag)
17306 {
17307 process_die (child_die, cu);
436c571c 17308 child_die = child_die->sibling;
5d7cb8df
JK
17309 }
17310}
17311
38d518c9
EZ
17312/* Return the name of the namespace represented by DIE. Set
17313 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17314 namespace. */
17315
17316static const char *
e142c38c 17317namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17318{
17319 struct die_info *current_die;
17320 const char *name = NULL;
17321
17322 /* Loop through the extensions until we find a name. */
17323
17324 for (current_die = die;
17325 current_die != NULL;
f2f0e013 17326 current_die = dwarf2_extension (die, &cu))
38d518c9 17327 {
96553a0c
DE
17328 /* We don't use dwarf2_name here so that we can detect the absence
17329 of a name -> anonymous namespace. */
7d45c7c3 17330 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17331
38d518c9
EZ
17332 if (name != NULL)
17333 break;
17334 }
17335
17336 /* Is it an anonymous namespace? */
17337
17338 *is_anonymous = (name == NULL);
17339 if (*is_anonymous)
2b1dbab0 17340 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17341
17342 return name;
d9fa45fe
DC
17343}
17344
c906108c
SS
17345/* Extract all information from a DW_TAG_pointer_type DIE and add to
17346 the user defined type vector. */
17347
f792889a 17348static struct type *
e7c27a73 17349read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17350{
5e22e966 17351 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17352 struct comp_unit_head *cu_header = &cu->header;
c906108c 17353 struct type *type;
8b2dbe47
KB
17354 struct attribute *attr_byte_size;
17355 struct attribute *attr_address_class;
17356 int byte_size, addr_class;
7e314c57
JK
17357 struct type *target_type;
17358
17359 target_type = die_type (die, cu);
c906108c 17360
7e314c57
JK
17361 /* The die_type call above may have already set the type for this DIE. */
17362 type = get_die_type (die, cu);
17363 if (type)
17364 return type;
17365
17366 type = lookup_pointer_type (target_type);
8b2dbe47 17367
e142c38c 17368 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17369 if (attr_byte_size)
529908cb 17370 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17371 else
8b2dbe47
KB
17372 byte_size = cu_header->addr_size;
17373
e142c38c 17374 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17375 if (attr_address_class)
529908cb 17376 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17377 else
17378 addr_class = DW_ADDR_none;
17379
2b4424c3
TT
17380 ULONGEST alignment = get_alignment (cu, die);
17381
17382 /* If the pointer size, alignment, or address class is different
17383 than the default, create a type variant marked as such and set
17384 the length accordingly. */
17385 if (TYPE_LENGTH (type) != byte_size
17386 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17387 && alignment != TYPE_RAW_ALIGN (type))
17388 || addr_class != DW_ADDR_none)
c906108c 17389 {
5e2b427d 17390 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17391 {
314ad88d
PA
17392 type_instance_flags type_flags
17393 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17394 addr_class);
876cecd0
TT
17395 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17396 == 0);
8b2dbe47
KB
17397 type = make_type_with_address_space (type, type_flags);
17398 }
17399 else if (TYPE_LENGTH (type) != byte_size)
17400 {
b98664d3 17401 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17402 }
2b4424c3
TT
17403 else if (TYPE_RAW_ALIGN (type) != alignment)
17404 {
b98664d3 17405 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17406 " - DIE at %s [in module %s]"),
17407 sect_offset_str (die->sect_off),
5e22e966 17408 objfile_name (cu->per_objfile->objfile));
2b4424c3 17409 }
6e70227d 17410 else
9a619af0
MS
17411 {
17412 /* Should we also complain about unhandled address classes? */
17413 }
c906108c 17414 }
8b2dbe47
KB
17415
17416 TYPE_LENGTH (type) = byte_size;
2b4424c3 17417 set_type_align (type, alignment);
f792889a 17418 return set_die_type (die, type, cu);
c906108c
SS
17419}
17420
17421/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17422 the user defined type vector. */
17423
f792889a 17424static struct type *
e7c27a73 17425read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17426{
17427 struct type *type;
17428 struct type *to_type;
17429 struct type *domain;
17430
e7c27a73
DJ
17431 to_type = die_type (die, cu);
17432 domain = die_containing_type (die, cu);
0d5de010 17433
7e314c57
JK
17434 /* The calls above may have already set the type for this DIE. */
17435 type = get_die_type (die, cu);
17436 if (type)
17437 return type;
17438
78134374 17439 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17440 type = lookup_methodptr_type (to_type);
78134374 17441 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17442 {
5e22e966 17443 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17444
17445 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17446 to_type->fields (), to_type->num_fields (),
a409645d 17447 to_type->has_varargs ());
7078baeb
TT
17448 type = lookup_methodptr_type (new_type);
17449 }
0d5de010
DJ
17450 else
17451 type = lookup_memberptr_type (to_type, domain);
c906108c 17452
f792889a 17453 return set_die_type (die, type, cu);
c906108c
SS
17454}
17455
4297a3f0 17456/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17457 the user defined type vector. */
17458
f792889a 17459static struct type *
4297a3f0 17460read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17461 enum type_code refcode)
c906108c 17462{
e7c27a73 17463 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17464 struct type *type, *target_type;
c906108c
SS
17465 struct attribute *attr;
17466
4297a3f0
AV
17467 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17468
7e314c57
JK
17469 target_type = die_type (die, cu);
17470
17471 /* The die_type call above may have already set the type for this DIE. */
17472 type = get_die_type (die, cu);
17473 if (type)
17474 return type;
17475
4297a3f0 17476 type = lookup_reference_type (target_type, refcode);
e142c38c 17477 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17478 if (attr != nullptr)
c906108c 17479 {
529908cb 17480 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17481 }
17482 else
17483 {
107d2387 17484 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17485 }
2b4424c3 17486 maybe_set_alignment (cu, die, type);
f792889a 17487 return set_die_type (die, type, cu);
c906108c
SS
17488}
17489
cf363f18
MW
17490/* Add the given cv-qualifiers to the element type of the array. GCC
17491 outputs DWARF type qualifiers that apply to an array, not the
17492 element type. But GDB relies on the array element type to carry
17493 the cv-qualifiers. This mimics section 6.7.3 of the C99
17494 specification. */
17495
17496static struct type *
17497add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17498 struct type *base_type, int cnst, int voltl)
17499{
17500 struct type *el_type, *inner_array;
17501
17502 base_type = copy_type (base_type);
17503 inner_array = base_type;
17504
78134374 17505 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17506 {
17507 TYPE_TARGET_TYPE (inner_array) =
17508 copy_type (TYPE_TARGET_TYPE (inner_array));
17509 inner_array = TYPE_TARGET_TYPE (inner_array);
17510 }
17511
17512 el_type = TYPE_TARGET_TYPE (inner_array);
17513 cnst |= TYPE_CONST (el_type);
17514 voltl |= TYPE_VOLATILE (el_type);
17515 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17516
17517 return set_die_type (die, base_type, cu);
17518}
17519
f792889a 17520static struct type *
e7c27a73 17521read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17522{
f792889a 17523 struct type *base_type, *cv_type;
c906108c 17524
e7c27a73 17525 base_type = die_type (die, cu);
7e314c57
JK
17526
17527 /* The die_type call above may have already set the type for this DIE. */
17528 cv_type = get_die_type (die, cu);
17529 if (cv_type)
17530 return cv_type;
17531
2f608a3a
KW
17532 /* In case the const qualifier is applied to an array type, the element type
17533 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17534 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17535 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17536
f792889a
DJ
17537 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17538 return set_die_type (die, cv_type, cu);
c906108c
SS
17539}
17540
f792889a 17541static struct type *
e7c27a73 17542read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17543{
f792889a 17544 struct type *base_type, *cv_type;
c906108c 17545
e7c27a73 17546 base_type = die_type (die, cu);
7e314c57
JK
17547
17548 /* The die_type call above may have already set the type for this DIE. */
17549 cv_type = get_die_type (die, cu);
17550 if (cv_type)
17551 return cv_type;
17552
cf363f18
MW
17553 /* In case the volatile qualifier is applied to an array type, the
17554 element type is so qualified, not the array type (section 6.7.3
17555 of C99). */
78134374 17556 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17557 return add_array_cv_type (die, cu, base_type, 0, 1);
17558
f792889a
DJ
17559 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17560 return set_die_type (die, cv_type, cu);
c906108c
SS
17561}
17562
06d66ee9
TT
17563/* Handle DW_TAG_restrict_type. */
17564
17565static struct type *
17566read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17567{
17568 struct type *base_type, *cv_type;
17569
17570 base_type = die_type (die, cu);
17571
17572 /* The die_type call above may have already set the type for this DIE. */
17573 cv_type = get_die_type (die, cu);
17574 if (cv_type)
17575 return cv_type;
17576
17577 cv_type = make_restrict_type (base_type);
17578 return set_die_type (die, cv_type, cu);
17579}
17580
a2c2acaf
MW
17581/* Handle DW_TAG_atomic_type. */
17582
17583static struct type *
17584read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17585{
17586 struct type *base_type, *cv_type;
17587
17588 base_type = die_type (die, cu);
17589
17590 /* The die_type call above may have already set the type for this DIE. */
17591 cv_type = get_die_type (die, cu);
17592 if (cv_type)
17593 return cv_type;
17594
17595 cv_type = make_atomic_type (base_type);
17596 return set_die_type (die, cv_type, cu);
17597}
17598
c906108c
SS
17599/* Extract all information from a DW_TAG_string_type DIE and add to
17600 the user defined type vector. It isn't really a user defined type,
17601 but it behaves like one, with other DIE's using an AT_user_def_type
17602 attribute to reference it. */
17603
f792889a 17604static struct type *
e7c27a73 17605read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17606{
5e22e966 17607 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17608 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17609 struct type *type, *range_type, *index_type, *char_type;
17610 struct attribute *attr;
216a7e6b
AB
17611 struct dynamic_prop prop;
17612 bool length_is_constant = true;
17613 LONGEST length;
17614
17615 /* There are a couple of places where bit sizes might be made use of
17616 when parsing a DW_TAG_string_type, however, no producer that we know
17617 of make use of these. Handling bit sizes that are a multiple of the
17618 byte size is easy enough, but what about other bit sizes? Lets deal
17619 with that problem when we have to. Warn about these attributes being
17620 unsupported, then parse the type and ignore them like we always
17621 have. */
17622 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17623 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17624 {
17625 static bool warning_printed = false;
17626 if (!warning_printed)
17627 {
17628 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17629 "currently supported on DW_TAG_string_type."));
17630 warning_printed = true;
17631 }
17632 }
c906108c 17633
e142c38c 17634 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17635 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17636 {
17637 /* The string length describes the location at which the length of
17638 the string can be found. The size of the length field can be
17639 specified with one of the attributes below. */
17640 struct type *prop_type;
17641 struct attribute *len
17642 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17643 if (len == nullptr)
17644 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17645 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17646 {
17647 /* Pass 0 as the default as we know this attribute is constant
17648 and the default value will not be returned. */
0826b30a 17649 LONGEST sz = len->constant_value (0);
293e7e51 17650 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17651 }
17652 else
17653 {
17654 /* If the size is not specified then we assume it is the size of
17655 an address on this target. */
293e7e51 17656 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17657 }
17658
17659 /* Convert the attribute into a dynamic property. */
17660 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17661 length = 1;
17662 else
17663 length_is_constant = false;
17664 }
17665 else if (attr != nullptr)
17666 {
17667 /* This DW_AT_string_length just contains the length with no
17668 indirection. There's no need to create a dynamic property in this
17669 case. Pass 0 for the default value as we know it will not be
17670 returned in this case. */
0826b30a 17671 length = attr->constant_value (0);
216a7e6b
AB
17672 }
17673 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17674 {
216a7e6b 17675 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17676 length = attr->constant_value (1);
c906108c
SS
17677 }
17678 else
17679 {
216a7e6b
AB
17680 /* Use 1 as a fallback length if we have nothing else. */
17681 length = 1;
c906108c 17682 }
6ccb9162 17683
46bf5051 17684 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17685 if (length_is_constant)
17686 range_type = create_static_range_type (NULL, index_type, 1, length);
17687 else
17688 {
17689 struct dynamic_prop low_bound;
17690
8c2e4e06 17691 low_bound.set_const_val (1);
216a7e6b
AB
17692 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17693 }
3b7538c0
UW
17694 char_type = language_string_char_type (cu->language_defn, gdbarch);
17695 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17696
f792889a 17697 return set_die_type (die, type, cu);
c906108c
SS
17698}
17699
4d804846
JB
17700/* Assuming that DIE corresponds to a function, returns nonzero
17701 if the function is prototyped. */
17702
17703static int
17704prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17705{
17706 struct attribute *attr;
17707
17708 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17709 if (attr && attr->as_boolean ())
4d804846
JB
17710 return 1;
17711
17712 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17713 is only meaningful for C, but the concept also extends to other
4d804846
JB
17714 languages that allow unprototyped functions (Eg: Objective C).
17715 For all other languages, assume that functions are always
17716 prototyped. */
17717 if (cu->language != language_c
17718 && cu->language != language_objc
17719 && cu->language != language_opencl)
17720 return 1;
17721
17722 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17723 prototyped and unprototyped functions; default to prototyped,
17724 since that is more common in modern code (and RealView warns
17725 about unprototyped functions). */
17726 if (producer_is_realview (cu->producer))
17727 return 1;
17728
17729 return 0;
17730}
17731
c906108c
SS
17732/* Handle DIES due to C code like:
17733
17734 struct foo
c5aa993b
JM
17735 {
17736 int (*funcp)(int a, long l);
17737 int b;
17738 };
c906108c 17739
0963b4bd 17740 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17741
f792889a 17742static struct type *
e7c27a73 17743read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17744{
5e22e966 17745 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17746 struct type *type; /* Type that this function returns. */
17747 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17748 struct attribute *attr;
17749
e7c27a73 17750 type = die_type (die, cu);
7e314c57
JK
17751
17752 /* The die_type call above may have already set the type for this DIE. */
17753 ftype = get_die_type (die, cu);
17754 if (ftype)
17755 return ftype;
17756
0c8b41f1 17757 ftype = lookup_function_type (type);
c906108c 17758
4d804846 17759 if (prototyped_function_p (die, cu))
27e69b7a 17760 ftype->set_is_prototyped (true);
c906108c 17761
c055b101
CV
17762 /* Store the calling convention in the type if it's available in
17763 the subroutine die. Otherwise set the calling convention to
17764 the default value DW_CC_normal. */
17765 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17766 if (attr != nullptr
529908cb 17767 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17768 TYPE_CALLING_CONVENTION (ftype)
529908cb 17769 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17770 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17771 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17772 else
17773 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17774
743649fd
MW
17775 /* Record whether the function returns normally to its caller or not
17776 if the DWARF producer set that information. */
17777 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17778 if (attr && attr->as_boolean ())
743649fd
MW
17779 TYPE_NO_RETURN (ftype) = 1;
17780
76c10ea2
GM
17781 /* We need to add the subroutine type to the die immediately so
17782 we don't infinitely recurse when dealing with parameters
0963b4bd 17783 declared as the same subroutine type. */
76c10ea2 17784 set_die_type (die, ftype, cu);
6e70227d 17785
639d11d3 17786 if (die->child != NULL)
c906108c 17787 {
bb5ed363 17788 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17789 struct die_info *child_die;
8072405b 17790 int nparams, iparams;
c906108c
SS
17791
17792 /* Count the number of parameters.
dda83cd7
SM
17793 FIXME: GDB currently ignores vararg functions, but knows about
17794 vararg member functions. */
8072405b 17795 nparams = 0;
639d11d3 17796 child_die = die->child;
c906108c
SS
17797 while (child_die && child_die->tag)
17798 {
17799 if (child_die->tag == DW_TAG_formal_parameter)
17800 nparams++;
17801 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17802 ftype->set_has_varargs (true);
17803
436c571c 17804 child_die = child_die->sibling;
c906108c
SS
17805 }
17806
17807 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17808 ftype->set_num_fields (nparams);
3cabb6b0
SM
17809 ftype->set_fields
17810 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17811
8072405b
JK
17812 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17813 even if we error out during the parameters reading below. */
17814 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17815 ftype->field (iparams).set_type (void_type);
8072405b
JK
17816
17817 iparams = 0;
639d11d3 17818 child_die = die->child;
c906108c
SS
17819 while (child_die && child_die->tag)
17820 {
17821 if (child_die->tag == DW_TAG_formal_parameter)
17822 {
3ce3b1ba
PA
17823 struct type *arg_type;
17824
17825 /* DWARF version 2 has no clean way to discern C++
17826 static and non-static member functions. G++ helps
17827 GDB by marking the first parameter for non-static
17828 member functions (which is the this pointer) as
17829 artificial. We pass this information to
17830 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17831
17832 DWARF version 3 added DW_AT_object_pointer, which GCC
17833 4.5 does not yet generate. */
e142c38c 17834 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17835 if (attr != nullptr)
c45bc3f8 17836 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 17837 else
9c37b5ae 17838 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17839 arg_type = die_type (child_die, cu);
17840
17841 /* RealView does not mark THIS as const, which the testsuite
17842 expects. GCC marks THIS as const in method definitions,
17843 but not in the class specifications (GCC PR 43053). */
17844 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17845 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17846 {
17847 int is_this = 0;
17848 struct dwarf2_cu *arg_cu = cu;
17849 const char *name = dwarf2_name (child_die, cu);
17850
17851 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17852 if (attr != nullptr)
3ce3b1ba
PA
17853 {
17854 /* If the compiler emits this, use it. */
17855 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17856 is_this = 1;
17857 }
17858 else if (name && strcmp (name, "this") == 0)
17859 /* Function definitions will have the argument names. */
17860 is_this = 1;
17861 else if (name == NULL && iparams == 0)
17862 /* Declarations may not have the names, so like
17863 elsewhere in GDB, assume an artificial first
17864 argument is "this". */
17865 is_this = 1;
17866
17867 if (is_this)
17868 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17869 arg_type, 0);
17870 }
17871
5d14b6e5 17872 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17873 iparams++;
17874 }
436c571c 17875 child_die = child_die->sibling;
c906108c
SS
17876 }
17877 }
17878
76c10ea2 17879 return ftype;
c906108c
SS
17880}
17881
f792889a 17882static struct type *
e7c27a73 17883read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17884{
5e22e966 17885 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17886 const char *name = NULL;
3c8e0968 17887 struct type *this_type, *target_type;
c906108c 17888
94af9270 17889 name = dwarf2_full_name (NULL, die, cu);
19f392bc 17890 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 17891 this_type->set_target_is_stub (true);
f792889a 17892 set_die_type (die, this_type, cu);
3c8e0968
DE
17893 target_type = die_type (die, cu);
17894 if (target_type != this_type)
17895 TYPE_TARGET_TYPE (this_type) = target_type;
17896 else
17897 {
17898 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17899 spec and cause infinite loops in GDB. */
b98664d3 17900 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17901 "- DIE at %s [in module %s]"),
17902 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17903 TYPE_TARGET_TYPE (this_type) = NULL;
17904 }
e4003a34
TV
17905 if (name == NULL)
17906 {
17907 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17908 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17909 Handle these by just returning the target type, rather than
17910 constructing an anonymous typedef type and trying to handle this
17911 elsewhere. */
17912 set_die_type (die, target_type, cu);
17913 return target_type;
17914 }
f792889a 17915 return this_type;
c906108c
SS
17916}
17917
a625a8c9
TT
17918/* Helper for get_dwarf2_rational_constant that computes the value of
17919 a given gmp_mpz given an attribute. */
17920
17921static void
17922get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17923{
17924 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17925 location expression that pushes an implicit value. */
17926 if (attr->form == DW_FORM_exprloc)
17927 {
17928 dwarf_block *blk = attr->as_block ();
17929 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17930 {
17931 uint64_t len;
17932 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17933 blk->data + blk->size,
17934 &len);
17935 if (ptr - blk->data + len <= blk->size)
17936 {
17937 mpz_import (value->val, len,
17938 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17939 1, 0, 0, ptr);
17940 return;
17941 }
17942 }
17943
17944 /* On failure set it to 1. */
17945 *value = gdb_mpz (1);
17946 }
17947 else if (attr->form_is_block ())
17948 {
17949 dwarf_block *blk = attr->as_block ();
17950 mpz_import (value->val, blk->size,
17951 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17952 1, 0, 0, blk->data);
17953 }
17954 else
17955 *value = gdb_mpz (attr->constant_value (1));
17956}
17957
09584414
JB
17958/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17959 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17960
17961 If the numerator and/or numerator attribute is missing,
17962 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17963 untouched. */
17964
17965static void
17966get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 17967 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
17968{
17969 struct attribute *num_attr, *denom_attr;
17970
17971 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17972 if (num_attr == nullptr)
17973 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17974 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17975
17976 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17977 if (denom_attr == nullptr)
17978 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17979 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17980
17981 if (num_attr == nullptr || denom_attr == nullptr)
17982 return;
17983
a625a8c9
TT
17984 get_mpz (cu, numerator, num_attr);
17985 get_mpz (cu, denominator, denom_attr);
09584414
JB
17986}
17987
17988/* Same as get_dwarf2_rational_constant, but extracting an unsigned
17989 rational constant, rather than a signed one.
17990
17991 If the rational constant has a negative value, a complaint
17992 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17993
17994static void
17995get_dwarf2_unsigned_rational_constant (struct die_info *die,
17996 struct dwarf2_cu *cu,
5cde1d82
TT
17997 gdb_mpz *numerator,
17998 gdb_mpz *denominator)
09584414 17999{
5cde1d82
TT
18000 gdb_mpz num (1);
18001 gdb_mpz denom (1);
09584414
JB
18002
18003 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 18004 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 18005 {
5cde1d82
TT
18006 mpz_neg (num.val, num.val);
18007 mpz_neg (denom.val, denom.val);
09584414 18008 }
5cde1d82 18009 else if (mpz_sgn (num.val) == -1)
09584414
JB
18010 {
18011 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18012 " in DIE at %s"),
18013 sect_offset_str (die->sect_off));
18014 return;
18015 }
5cde1d82 18016 else if (mpz_sgn (denom.val) == -1)
09584414
JB
18017 {
18018 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18019 " in DIE at %s"),
18020 sect_offset_str (die->sect_off));
18021 return;
18022 }
18023
5cde1d82
TT
18024 *numerator = std::move (num);
18025 *denominator = std::move (denom);
09584414
JB
18026}
18027
bbcdf9ab
TT
18028/* Assuming that ENCODING is a string whose contents starting at the
18029 K'th character is "_nn" where "nn" is a decimal number, scan that
18030 number and set RESULT to the value. K is updated to point to the
18031 character immediately following the number.
18032
18033 If the string does not conform to the format described above, false
18034 is returned, and K may or may not be changed. */
18035
18036static bool
18037ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18038{
18039 /* The next character should be an underscore ('_') followed
18040 by a digit. */
18041 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18042 return false;
18043
18044 /* Skip the underscore. */
18045 k++;
18046 int start = k;
18047
18048 /* Determine the number of digits for our number. */
18049 while (isdigit (encoding[k]))
18050 k++;
18051 if (k == start)
18052 return false;
18053
18054 std::string copy (&encoding[start], k - start);
18055 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18056 return false;
18057
18058 return true;
18059}
18060
18061/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18062 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18063 DENOM, update OFFSET, and return true on success. Return false on
18064 failure. */
18065
18066static bool
18067ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18068 gdb_mpz *num, gdb_mpz *denom)
18069{
18070 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18071 return false;
18072 return ada_get_gnat_encoded_number (encoding, offset, denom);
18073}
18074
09584414 18075/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
18076 of the corresponding TYPE by setting its type-specific data. CU is
18077 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18078 encodings. It is nullptr if the GNAT encoding should be
18079 ignored. */
09584414
JB
18080
18081static void
bbcdf9ab
TT
18082finish_fixed_point_type (struct type *type, const char *suffix,
18083 struct die_info *die, struct dwarf2_cu *cu)
09584414 18084{
09584414
JB
18085 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18086 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18087
bbcdf9ab
TT
18088 /* If GNAT encodings are preferred, don't examine the
18089 attributes. */
18090 struct attribute *attr = nullptr;
18091 if (suffix == nullptr)
18092 {
18093 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18094 if (attr == nullptr)
18095 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18096 if (attr == nullptr)
18097 attr = dwarf2_attr (die, DW_AT_small, cu);
18098 }
09584414 18099
5cde1d82
TT
18100 /* Numerator and denominator of our fixed-point type's scaling factor.
18101 The default is a scaling factor of 1, which we use as a fallback
18102 when we are not able to decode it (problem with the debugging info,
18103 unsupported forms, bug in GDB, etc...). Using that as the default
18104 allows us to at least print the unscaled value, which might still
18105 be useful to a user. */
18106 gdb_mpz scale_num (1);
18107 gdb_mpz scale_denom (1);
18108
09584414
JB
18109 if (attr == nullptr)
18110 {
bbcdf9ab
TT
18111 int offset = 0;
18112 if (suffix != nullptr
18113 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18114 &scale_denom)
18115 /* The number might be encoded as _nn_dd_nn_dd, where the
18116 second ratio is the 'small value. In this situation, we
18117 want the second value. */
18118 && (suffix[offset] != '_'
18119 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18120 &scale_denom)))
18121 {
18122 /* Found it. */
18123 }
18124 else
18125 {
18126 /* Scaling factor not found. Assume a scaling factor of 1,
18127 and hope for the best. At least the user will be able to
18128 see the encoded value. */
18129 scale_num = 1;
18130 scale_denom = 1;
18131 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18132 sect_offset_str (die->sect_off));
18133 }
09584414
JB
18134 }
18135 else if (attr->name == DW_AT_binary_scale)
18136 {
18137 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18138 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18139
5cde1d82 18140 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18141 }
18142 else if (attr->name == DW_AT_decimal_scale)
18143 {
18144 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18145 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18146
5cde1d82 18147 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18148 }
18149 else if (attr->name == DW_AT_small)
18150 {
18151 struct die_info *scale_die;
18152 struct dwarf2_cu *scale_cu = cu;
18153
18154 scale_die = follow_die_ref (die, attr, &scale_cu);
18155 if (scale_die->tag == DW_TAG_constant)
18156 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18157 &scale_num, &scale_denom);
18158 else
18159 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18160 " (DIE at %s)"),
18161 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18162 }
18163 else
18164 {
18165 complaint (_("unsupported scale attribute %s for fixed-point type"
18166 " (DIE at %s)"),
18167 dwarf_attr_name (attr->name),
18168 sect_offset_str (die->sect_off));
18169 }
18170
2a12c336 18171 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18172 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18173 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18174 mpq_canonicalize (scaling_factor.val);
18175}
18176
bbcdf9ab
TT
18177/* The gnat-encoding suffix for fixed point. */
18178
18179#define GNAT_FIXED_POINT_SUFFIX "___XF_"
18180
18181/* If NAME encodes an Ada fixed-point type, return a pointer to the
18182 "XF" suffix of the name. The text after this is what encodes the
18183 'small and 'delta information. Otherwise, return nullptr. */
18184
18185static const char *
18186gnat_encoded_fixed_point_type_info (const char *name)
18187{
18188 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18189}
18190
9b790ce7
UW
18191/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18192 (which may be different from NAME) to the architecture back-end to allow
18193 it to guess the correct format if necessary. */
18194
18195static struct type *
18196dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18197 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18198{
08feed99 18199 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18200 const struct floatformat **format;
18201 struct type *type;
18202
18203 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18204 if (format)
103a685e 18205 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18206 else
77b7c781 18207 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18208
18209 return type;
18210}
18211
eb77c9df
AB
18212/* Allocate an integer type of size BITS and name NAME. */
18213
18214static struct type *
18215dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18216 int bits, int unsigned_p, const char *name)
18217{
18218 struct type *type;
18219
18220 /* Versions of Intel's C Compiler generate an integer type called "void"
18221 instead of using DW_TAG_unspecified_type. This has been seen on
18222 at least versions 14, 17, and 18. */
35ee2dc2
AB
18223 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18224 && strcmp (name, "void") == 0)
eb77c9df
AB
18225 type = objfile_type (objfile)->builtin_void;
18226 else
18227 type = init_integer_type (objfile, bits, unsigned_p, name);
18228
18229 return type;
18230}
18231
09584414
JB
18232/* Return true if DIE has a DW_AT_small attribute whose value is
18233 a constant rational, where both the numerator and denominator
18234 are equal to zero.
18235
18236 CU is the DIE's Compilation Unit. */
18237
18238static bool
18239has_zero_over_zero_small_attribute (struct die_info *die,
18240 struct dwarf2_cu *cu)
18241{
18242 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18243 if (attr == nullptr)
18244 return false;
18245
18246 struct dwarf2_cu *scale_cu = cu;
18247 struct die_info *scale_die
18248 = follow_die_ref (die, attr, &scale_cu);
18249
18250 if (scale_die->tag != DW_TAG_constant)
18251 return false;
18252
5cde1d82 18253 gdb_mpz num (1), denom (1);
09584414 18254 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18255 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18256}
18257
8bdc1658
AB
18258/* Initialise and return a floating point type of size BITS suitable for
18259 use as a component of a complex number. The NAME_HINT is passed through
18260 when initialising the floating point type and is the name of the complex
18261 type.
18262
18263 As DWARF doesn't currently provide an explicit name for the components
18264 of a complex number, but it can be helpful to have these components
18265 named, we try to select a suitable name based on the size of the
18266 component. */
18267static struct type *
18268dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18269 struct objfile *objfile,
103a685e
TT
18270 int bits, const char *name_hint,
18271 enum bfd_endian byte_order)
8bdc1658 18272{
08feed99 18273 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18274 struct type *tt = nullptr;
18275
35add35e
AB
18276 /* Try to find a suitable floating point builtin type of size BITS.
18277 We're going to use the name of this type as the name for the complex
18278 target type that we are about to create. */
1db455a7 18279 switch (cu->language)
8bdc1658 18280 {
1db455a7
AB
18281 case language_fortran:
18282 switch (bits)
18283 {
18284 case 32:
18285 tt = builtin_f_type (gdbarch)->builtin_real;
18286 break;
18287 case 64:
18288 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18289 break;
18290 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18291 case 128:
18292 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18293 break;
18294 }
8bdc1658 18295 break;
1db455a7
AB
18296 default:
18297 switch (bits)
18298 {
18299 case 32:
18300 tt = builtin_type (gdbarch)->builtin_float;
18301 break;
18302 case 64:
18303 tt = builtin_type (gdbarch)->builtin_double;
18304 break;
18305 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18306 case 128:
18307 tt = builtin_type (gdbarch)->builtin_long_double;
18308 break;
18309 }
8bdc1658
AB
18310 break;
18311 }
18312
35add35e
AB
18313 /* If the type we found doesn't match the size we were looking for, then
18314 pretend we didn't find a type at all, the complex target type we
18315 create will then be nameless. */
a12e5744 18316 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18317 tt = nullptr;
18318
7d93a1e0 18319 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18320 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18321}
18322
c906108c
SS
18323/* Find a representation of a given base type and install
18324 it in the TYPE field of the die. */
18325
f792889a 18326static struct type *
e7c27a73 18327read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18328{
5e22e966 18329 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18330 struct type *type;
18331 struct attribute *attr;
19f392bc 18332 int encoding = 0, bits = 0;
15d034d0 18333 const char *name;
34877895 18334 gdbarch *arch;
c906108c 18335
e142c38c 18336 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18337 if (attr != nullptr && attr->form_is_constant ())
18338 encoding = attr->constant_value (0);
e142c38c 18339 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18340 if (attr != nullptr)
529908cb 18341 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18342 name = dwarf2_name (die, cu);
6ccb9162 18343 if (!name)
34877895 18344 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18345
08feed99 18346 arch = objfile->arch ();
103a685e
TT
18347 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18348
34877895 18349 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18350 if (attr != nullptr && attr->form_is_constant ())
103a685e 18351 {
529908cb 18352 int endianity = attr->constant_value (0);
103a685e
TT
18353
18354 switch (endianity)
18355 {
18356 case DW_END_big:
18357 byte_order = BFD_ENDIAN_BIG;
18358 break;
18359 case DW_END_little:
18360 byte_order = BFD_ENDIAN_LITTLE;
18361 break;
18362 default:
18363 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18364 break;
18365 }
18366 }
6ccb9162 18367
09584414
JB
18368 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18369 && cu->language == language_ada
18370 && has_zero_over_zero_small_attribute (die, cu))
18371 {
18372 /* brobecker/2018-02-24: This is a fixed point type for which
18373 the scaling factor is represented as fraction whose value
18374 does not make sense (zero divided by zero), so we should
18375 normally never see these. However, there is a small category
18376 of fixed point types for which GNAT is unable to provide
18377 the scaling factor via the standard DWARF mechanisms, and
18378 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18379 This is likely what this DIE is about. */
09584414
JB
18380 encoding = (encoding == DW_ATE_signed_fixed
18381 ? DW_ATE_signed
18382 : DW_ATE_unsigned);
18383 }
18384
bbcdf9ab
TT
18385 /* With GNAT encodings, fixed-point information will be encoded in
18386 the type name. Note that this can also occur with the above
18387 zero-over-zero case, which is why this is a separate "if" rather
18388 than an "else if". */
18389 const char *gnat_encoding_suffix = nullptr;
18390 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18391 && cu->language == language_ada
18392 && name != nullptr)
18393 {
18394 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18395 if (gnat_encoding_suffix != nullptr)
18396 {
18397 gdb_assert (startswith (gnat_encoding_suffix,
18398 GNAT_FIXED_POINT_SUFFIX));
18399 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18400 name, gnat_encoding_suffix - name);
18401 /* Use -1 here so that SUFFIX points at the "_" after the
18402 "XF". */
18403 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18404
18405 encoding = (encoding == DW_ATE_signed
18406 ? DW_ATE_signed_fixed
18407 : DW_ATE_unsigned_fixed);
18408 }
18409 }
18410
6ccb9162 18411 switch (encoding)
c906108c 18412 {
6ccb9162
UW
18413 case DW_ATE_address:
18414 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18415 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18416 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18417 break;
18418 case DW_ATE_boolean:
19f392bc 18419 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18420 break;
18421 case DW_ATE_complex_float:
103a685e
TT
18422 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18423 byte_order);
78134374 18424 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18425 {
18426 if (name == nullptr)
18427 {
18428 struct obstack *obstack
5e22e966 18429 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18430 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18431 nullptr);
18432 }
18433 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18434 }
18435 else
18436 type = init_complex_type (name, type);
6ccb9162
UW
18437 break;
18438 case DW_ATE_decimal_float:
19f392bc 18439 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18440 break;
18441 case DW_ATE_float:
103a685e 18442 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18443 break;
18444 case DW_ATE_signed:
eb77c9df 18445 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18446 break;
18447 case DW_ATE_unsigned:
3b2b8fea
TT
18448 if (cu->language == language_fortran
18449 && name
61012eef 18450 && startswith (name, "character("))
19f392bc
UW
18451 type = init_character_type (objfile, bits, 1, name);
18452 else
eb77c9df 18453 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18454 break;
18455 case DW_ATE_signed_char:
6e70227d 18456 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18457 || cu->language == language_pascal
18458 || cu->language == language_fortran)
19f392bc
UW
18459 type = init_character_type (objfile, bits, 0, name);
18460 else
eb77c9df 18461 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18462 break;
18463 case DW_ATE_unsigned_char:
868a0084 18464 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18465 || cu->language == language_pascal
c44af4eb
TT
18466 || cu->language == language_fortran
18467 || cu->language == language_rust)
19f392bc
UW
18468 type = init_character_type (objfile, bits, 1, name);
18469 else
eb77c9df 18470 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18471 break;
75079b2b 18472 case DW_ATE_UTF:
53e710ac 18473 {
53e710ac
PA
18474 if (bits == 16)
18475 type = builtin_type (arch)->builtin_char16;
18476 else if (bits == 32)
18477 type = builtin_type (arch)->builtin_char32;
18478 else
18479 {
b98664d3 18480 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18481 bits);
eb77c9df 18482 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18483 }
18484 return set_die_type (die, type, cu);
18485 }
75079b2b 18486 break;
09584414
JB
18487 case DW_ATE_signed_fixed:
18488 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18489 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18490 break;
18491 case DW_ATE_unsigned_fixed:
18492 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18493 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18494 break;
75079b2b 18495
6ccb9162 18496 default:
b98664d3 18497 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18498 dwarf_type_encoding_name (encoding));
77b7c781 18499 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18500 break;
c906108c 18501 }
6ccb9162 18502
0114d602 18503 if (name && strcmp (name, "char") == 0)
15152a54 18504 type->set_has_no_signedness (true);
0114d602 18505
2b4424c3
TT
18506 maybe_set_alignment (cu, die, type);
18507
db558e34 18508 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18509
20a5fcbd
TT
18510 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18511 {
18512 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18513 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18514 {
529908cb 18515 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18516 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18517 /* Only use the attributes if they make sense together. */
18518 if (attr == nullptr
529908cb
TT
18519 || (attr->as_unsigned () + real_bit_size
18520 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18521 {
18522 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18523 = real_bit_size;
18524 if (attr != nullptr)
18525 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18526 = attr->as_unsigned ();
20a5fcbd
TT
18527 }
18528 }
18529 }
18530
f792889a 18531 return set_die_type (die, type, cu);
c906108c
SS
18532}
18533
80180f79
SA
18534/* Parse dwarf attribute if it's a block, reference or constant and put the
18535 resulting value of the attribute into struct bound_prop.
18536 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18537
18538static int
18539attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18540 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18541 struct type *default_type)
80180f79
SA
18542{
18543 struct dwarf2_property_baton *baton;
5e22e966 18544 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18545 struct objfile *objfile = per_objfile->objfile;
18546 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18547
9a49df9d
AB
18548 gdb_assert (default_type != NULL);
18549
80180f79
SA
18550 if (attr == NULL || prop == NULL)
18551 return 0;
18552
4fc6c0d5 18553 if (attr->form_is_block ())
80180f79 18554 {
8d749320 18555 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18556 baton->property_type = default_type;
80180f79 18557 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18558 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18559
18560 struct dwarf_block *block = attr->as_block ();
18561 baton->locexpr.size = block->size;
18562 baton->locexpr.data = block->data;
216a7e6b
AB
18563 switch (attr->name)
18564 {
18565 case DW_AT_string_length:
18566 baton->locexpr.is_reference = true;
18567 break;
18568 default:
18569 baton->locexpr.is_reference = false;
18570 break;
18571 }
8c2e4e06
SM
18572
18573 prop->set_locexpr (baton);
18574 gdb_assert (prop->baton () != NULL);
80180f79 18575 }
cd6c91b4 18576 else if (attr->form_is_ref ())
80180f79
SA
18577 {
18578 struct dwarf2_cu *target_cu = cu;
18579 struct die_info *target_die;
18580 struct attribute *target_attr;
18581
18582 target_die = follow_die_ref (die, attr, &target_cu);
18583 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18584 if (target_attr == NULL)
18585 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18586 target_cu);
80180f79
SA
18587 if (target_attr == NULL)
18588 return 0;
18589
df25ebbd 18590 switch (target_attr->name)
80180f79 18591 {
df25ebbd 18592 case DW_AT_location:
cd6c91b4 18593 if (target_attr->form_is_section_offset ())
df25ebbd 18594 {
8d749320 18595 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18596 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18597 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18598 prop->set_loclist (baton);
18599 gdb_assert (prop->baton () != NULL);
df25ebbd 18600 }
4fc6c0d5 18601 else if (target_attr->form_is_block ())
df25ebbd 18602 {
8d749320 18603 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18604 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18605 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18606 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18607 struct dwarf_block *block = target_attr->as_block ();
18608 baton->locexpr.size = block->size;
18609 baton->locexpr.data = block->data;
9a49df9d 18610 baton->locexpr.is_reference = true;
8c2e4e06
SM
18611 prop->set_locexpr (baton);
18612 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18613 }
18614 else
18615 {
18616 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18617 "dynamic property");
18618 return 0;
18619 }
18620 break;
18621 case DW_AT_data_member_location:
18622 {
18623 LONGEST offset;
18624
18625 if (!handle_data_member_location (target_die, target_cu,
18626 &offset))
18627 return 0;
18628
8d749320 18629 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18630 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18631 target_cu);
df25ebbd
JB
18632 baton->offset_info.offset = offset;
18633 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18634 prop->set_addr_offset (baton);
df25ebbd
JB
18635 break;
18636 }
80180f79
SA
18637 }
18638 }
cd6c91b4 18639 else if (attr->form_is_constant ())
8c2e4e06 18640 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18641 else
18642 {
18643 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18644 dwarf2_name (die, cu));
18645 return 0;
18646 }
18647
18648 return 1;
18649}
18650
09ba997f 18651/* See read.h. */
9a49df9d 18652
09ba997f 18653struct type *
293e7e51 18654dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18655{
9a49df9d
AB
18656 struct type *int_type;
18657
18658 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18659#define TRY_TYPE(F) \
18660 int_type = (unsigned_p \
18661 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18662 : objfile_type (objfile)->builtin_ ## F); \
18663 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18664 return int_type
18665
18666 TRY_TYPE (char);
18667 TRY_TYPE (short);
18668 TRY_TYPE (int);
18669 TRY_TYPE (long);
18670 TRY_TYPE (long_long);
18671
18672#undef TRY_TYPE
18673
18674 gdb_assert_not_reached ("unable to find suitable integer type");
18675}
18676
09ba997f 18677/* See read.h. */
11a8b164 18678
09ba997f 18679struct type *
293e7e51 18680dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18681{
293e7e51
SM
18682 int addr_size = this->per_cu->addr_size ();
18683 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18684}
18685
b86352cf
AB
18686/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18687 present (which is valid) then compute the default type based on the
18688 compilation units address size. */
18689
18690static struct type *
18691read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18692{
18693 struct type *index_type = die_type (die, cu);
18694
18695 /* Dwarf-2 specifications explicitly allows to create subrange types
18696 without specifying a base type.
18697 In that case, the base type must be set to the type of
18698 the lower bound, upper bound or count, in that order, if any of these
18699 three attributes references an object that has a type.
18700 If no base type is found, the Dwarf-2 specifications say that
18701 a signed integer type of size equal to the size of an address should
18702 be used.
18703 For the following C code: `extern char gdb_int [];'
18704 GCC produces an empty range DIE.
18705 FIXME: muller/2010-05-28: Possible references to object for low bound,
18706 high bound or count are not yet handled by this code. */
78134374 18707 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18708 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18709
18710 return index_type;
18711}
18712
a02abb62
JB
18713/* Read the given DW_AT_subrange DIE. */
18714
f792889a 18715static struct type *
a02abb62
JB
18716read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18717{
4c9ad8c2 18718 struct type *base_type, *orig_base_type;
a02abb62
JB
18719 struct type *range_type;
18720 struct attribute *attr;
729efb13 18721 struct dynamic_prop low, high;
4fae6e18 18722 int low_default_is_valid;
c451ebe5 18723 int high_bound_is_count = 0;
15d034d0 18724 const char *name;
d359392f 18725 ULONGEST negative_mask;
e77813c8 18726
b86352cf
AB
18727 orig_base_type = read_subrange_index_type (die, cu);
18728
4c9ad8c2
TT
18729 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18730 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18731 creating the range type, but we use the result of check_typedef
18732 when examining properties of the type. */
18733 base_type = check_typedef (orig_base_type);
a02abb62 18734
7e314c57
JK
18735 /* The die_type call above may have already set the type for this DIE. */
18736 range_type = get_die_type (die, cu);
18737 if (range_type)
18738 return range_type;
18739
8c2e4e06 18740 high.set_const_val (0);
729efb13 18741
4fae6e18
JK
18742 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18743 omitting DW_AT_lower_bound. */
18744 switch (cu->language)
6e70227d 18745 {
4fae6e18
JK
18746 case language_c:
18747 case language_cplus:
8c2e4e06 18748 low.set_const_val (0);
4fae6e18
JK
18749 low_default_is_valid = 1;
18750 break;
18751 case language_fortran:
8c2e4e06 18752 low.set_const_val (1);
4fae6e18
JK
18753 low_default_is_valid = 1;
18754 break;
18755 case language_d:
4fae6e18 18756 case language_objc:
c44af4eb 18757 case language_rust:
8c2e4e06 18758 low.set_const_val (0);
4fae6e18
JK
18759 low_default_is_valid = (cu->header.version >= 4);
18760 break;
18761 case language_ada:
18762 case language_m2:
18763 case language_pascal:
8c2e4e06 18764 low.set_const_val (1);
4fae6e18
JK
18765 low_default_is_valid = (cu->header.version >= 4);
18766 break;
18767 default:
8c2e4e06 18768 low.set_const_val (0);
4fae6e18
JK
18769 low_default_is_valid = 0;
18770 break;
a02abb62
JB
18771 }
18772
e142c38c 18773 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18774 if (attr != nullptr)
9a49df9d 18775 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18776 else if (!low_default_is_valid)
b98664d3 18777 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18778 "- DIE at %s [in module %s]"),
18779 sect_offset_str (die->sect_off),
5e22e966 18780 objfile_name (cu->per_objfile->objfile));
a02abb62 18781
506f5c41
TV
18782 struct attribute *attr_ub, *attr_count;
18783 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18784 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18785 {
506f5c41 18786 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18787 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18788 {
c451ebe5 18789 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18790 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18791 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18792 else
18793 high_bound_is_count = 1;
c2ff108b 18794 }
506f5c41
TV
18795 else
18796 {
18797 if (attr_ub != NULL)
18798 complaint (_("Unresolved DW_AT_upper_bound "
18799 "- DIE at %s [in module %s]"),
18800 sect_offset_str (die->sect_off),
5e22e966 18801 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18802 if (attr_count != NULL)
18803 complaint (_("Unresolved DW_AT_count "
18804 "- DIE at %s [in module %s]"),
18805 sect_offset_str (die->sect_off),
5e22e966 18806 objfile_name (cu->per_objfile->objfile));
506f5c41 18807 }
e77813c8 18808 }
a02abb62 18809
4e962e74
TT
18810 LONGEST bias = 0;
18811 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18812 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18813 bias = bias_attr->constant_value (0);
4e962e74 18814
dbb9c2b1
JB
18815 /* Normally, the DWARF producers are expected to use a signed
18816 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18817 But this is unfortunately not always the case, as witnessed
18818 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18819 is used instead. To work around that ambiguity, we treat
18820 the bounds as signed, and thus sign-extend their values, when
18821 the base type is signed. */
6e70227d 18822 negative_mask =
d359392f 18823 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18824 if (low.kind () == PROP_CONST
c6d940a9 18825 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18826 low.set_const_val (low.const_val () | negative_mask);
18827 if (high.kind () == PROP_CONST
c6d940a9 18828 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18829 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18830
5bbd8269
AB
18831 /* Check for bit and byte strides. */
18832 struct dynamic_prop byte_stride_prop;
18833 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18834 if (attr_byte_stride != nullptr)
18835 {
293e7e51 18836 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18837 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18838 prop_type);
18839 }
18840
18841 struct dynamic_prop bit_stride_prop;
18842 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18843 if (attr_bit_stride != nullptr)
18844 {
18845 /* It only makes sense to have either a bit or byte stride. */
18846 if (attr_byte_stride != nullptr)
18847 {
18848 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18849 "- DIE at %s [in module %s]"),
18850 sect_offset_str (die->sect_off),
5e22e966 18851 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18852 attr_bit_stride = nullptr;
18853 }
18854 else
18855 {
293e7e51 18856 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18857 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18858 prop_type);
18859 }
18860 }
18861
18862 if (attr_byte_stride != nullptr
18863 || attr_bit_stride != nullptr)
18864 {
18865 bool byte_stride_p = (attr_byte_stride != nullptr);
18866 struct dynamic_prop *stride
18867 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18868
18869 range_type
18870 = create_range_type_with_stride (NULL, orig_base_type, &low,
18871 &high, bias, stride, byte_stride_p);
18872 }
18873 else
18874 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18875
c451ebe5 18876 if (high_bound_is_count)
599088e3 18877 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18878
c2ff108b
JK
18879 /* Ada expects an empty array on no boundary attributes. */
18880 if (attr == NULL && cu->language != language_ada)
8c2e4e06 18881 range_type->bounds ()->high.set_undefined ();
c2ff108b 18882
39cbfefa
DJ
18883 name = dwarf2_name (die, cu);
18884 if (name)
d0e39ea2 18885 range_type->set_name (name);
6e70227d 18886
e142c38c 18887 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18888 if (attr != nullptr)
529908cb 18889 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 18890
2b4424c3
TT
18891 maybe_set_alignment (cu, die, range_type);
18892
7e314c57
JK
18893 set_die_type (die, range_type, cu);
18894
18895 /* set_die_type should be already done. */
b4ba55a1
JB
18896 set_descriptive_type (range_type, die, cu);
18897
7e314c57 18898 return range_type;
a02abb62 18899}
6e70227d 18900
f792889a 18901static struct type *
81a17f79
JB
18902read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18903{
18904 struct type *type;
81a17f79 18905
5e22e966 18906 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18907 type->set_name (dwarf2_name (die, cu));
81a17f79 18908
74a2f8ff 18909 /* In Ada, an unspecified type is typically used when the description
85102364 18910 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18911 such a type, we treat it as a stub, and try to resolve it later on,
18912 when needed. */
18913 if (cu->language == language_ada)
b4b73759 18914 type->set_is_stub (true);
74a2f8ff 18915
f792889a 18916 return set_die_type (die, type, cu);
81a17f79 18917}
a02abb62 18918
639d11d3
DC
18919/* Read a single die and all its descendents. Set the die's sibling
18920 field to NULL; set other fields in the die correctly, and set all
18921 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18922 location of the info_ptr after reading all of those dies. PARENT
18923 is the parent of the die in question. */
18924
18925static struct die_info *
dee91e82 18926read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18927 const gdb_byte *info_ptr,
18928 const gdb_byte **new_info_ptr,
dee91e82 18929 struct die_info *parent)
639d11d3
DC
18930{
18931 struct die_info *die;
d521ce57 18932 const gdb_byte *cur_ptr;
639d11d3 18933
3e225074 18934 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18935 if (die == NULL)
18936 {
18937 *new_info_ptr = cur_ptr;
18938 return NULL;
18939 }
93311388 18940 store_in_ref_table (die, reader->cu);
639d11d3 18941
3e225074 18942 if (die->has_children)
bf6af496 18943 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18944 else
18945 {
18946 die->child = NULL;
18947 *new_info_ptr = cur_ptr;
18948 }
18949
18950 die->sibling = NULL;
18951 die->parent = parent;
18952 return die;
18953}
18954
18955/* Read a die, all of its descendents, and all of its siblings; set
18956 all of the fields of all of the dies correctly. Arguments are as
18957 in read_die_and_children. */
18958
18959static struct die_info *
bf6af496 18960read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18961 const gdb_byte *info_ptr,
18962 const gdb_byte **new_info_ptr,
bf6af496 18963 struct die_info *parent)
639d11d3
DC
18964{
18965 struct die_info *first_die, *last_sibling;
d521ce57 18966 const gdb_byte *cur_ptr;
639d11d3 18967
c906108c 18968 cur_ptr = info_ptr;
639d11d3
DC
18969 first_die = last_sibling = NULL;
18970
18971 while (1)
c906108c 18972 {
639d11d3 18973 struct die_info *die
dee91e82 18974 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18975
1d325ec1 18976 if (die == NULL)
c906108c 18977 {
639d11d3
DC
18978 *new_info_ptr = cur_ptr;
18979 return first_die;
c906108c 18980 }
1d325ec1
DJ
18981
18982 if (!first_die)
18983 first_die = die;
c906108c 18984 else
1d325ec1
DJ
18985 last_sibling->sibling = die;
18986
18987 last_sibling = die;
c906108c 18988 }
c906108c
SS
18989}
18990
bf6af496
DE
18991/* Read a die, all of its descendents, and all of its siblings; set
18992 all of the fields of all of the dies correctly. Arguments are as
18993 in read_die_and_children.
18994 This the main entry point for reading a DIE and all its children. */
18995
18996static struct die_info *
18997read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18998 const gdb_byte *info_ptr,
18999 const gdb_byte **new_info_ptr,
bf6af496
DE
19000 struct die_info *parent)
19001{
19002 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19003 new_info_ptr, parent);
19004
b4f54984 19005 if (dwarf_die_debug)
bf6af496
DE
19006 {
19007 fprintf_unfiltered (gdb_stdlog,
19008 "Read die from %s@0x%x of %s:\n",
96b79293 19009 reader->die_section->get_name (),
bf6af496
DE
19010 (unsigned) (info_ptr - reader->die_section->buffer),
19011 bfd_get_filename (reader->abfd));
b4f54984 19012 dump_die (die, dwarf_die_debug);
bf6af496
DE
19013 }
19014
19015 return die;
19016}
19017
3019eac3
DE
19018/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19019 attributes.
19020 The caller is responsible for filling in the extra attributes
19021 and updating (*DIEP)->num_attrs.
19022 Set DIEP to point to a newly allocated die with its information,
3e225074 19023 except for its child, sibling, and parent fields. */
93311388 19024
d521ce57 19025static const gdb_byte *
3019eac3 19026read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19027 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19028 int num_extra_attrs)
93311388 19029{
b64f50a1 19030 unsigned int abbrev_number, bytes_read, i;
7c32eebb 19031 const struct abbrev_info *abbrev;
93311388
DE
19032 struct die_info *die;
19033 struct dwarf2_cu *cu = reader->cu;
19034 bfd *abfd = reader->abfd;
19035
9c541725 19036 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19037 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19038 info_ptr += bytes_read;
19039 if (!abbrev_number)
19040 {
19041 *diep = NULL;
93311388
DE
19042 return info_ptr;
19043 }
19044
685af9cd 19045 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19046 if (!abbrev)
348e048f
DE
19047 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19048 abbrev_number,
19049 bfd_get_filename (abfd));
19050
3019eac3 19051 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19052 die->sect_off = sect_off;
93311388
DE
19053 die->tag = abbrev->tag;
19054 die->abbrev = abbrev_number;
3e225074 19055 die->has_children = abbrev->has_children;
93311388 19056
3019eac3
DE
19057 /* Make the result usable.
19058 The caller needs to update num_attrs after adding the extra
19059 attributes. */
93311388
DE
19060 die->num_attrs = abbrev->num_attrs;
19061
7a5f294d 19062 bool any_need_reprocess = false;
93311388 19063 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19064 {
7a5f294d
TT
19065 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19066 info_ptr);
19067 if (die->attrs[i].requires_reprocessing_p ())
19068 any_need_reprocess = true;
18a8505e
AT
19069 }
19070
052c8bb8 19071 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19072 if (attr != nullptr && attr->form_is_unsigned ())
19073 cu->str_offsets_base = attr->as_unsigned ();
93311388 19074
41144253 19075 attr = die->attr (DW_AT_loclists_base);
19076 if (attr != nullptr)
529908cb 19077 cu->loclist_base = attr->as_unsigned ();
41144253 19078
a39fdb41 19079 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19080 if (maybe_addr_base.has_value ())
19081 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19082
19083 attr = die->attr (DW_AT_rnglists_base);
19084 if (attr != nullptr)
2b0c7f41 19085 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 19086
7a5f294d
TT
19087 if (any_need_reprocess)
19088 {
19089 for (i = 0; i < abbrev->num_attrs; ++i)
19090 {
19091 if (die->attrs[i].requires_reprocessing_p ())
19092 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19093 }
19094 }
93311388 19095 *diep = die;
93311388
DE
19096 return info_ptr;
19097}
19098
3019eac3
DE
19099/* Read a die and all its attributes.
19100 Set DIEP to point to a newly allocated die with its information,
3e225074 19101 except for its child, sibling, and parent fields. */
3019eac3 19102
d521ce57 19103static const gdb_byte *
3019eac3 19104read_full_die (const struct die_reader_specs *reader,
3e225074 19105 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19106{
d521ce57 19107 const gdb_byte *result;
bf6af496 19108
3e225074 19109 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19110
b4f54984 19111 if (dwarf_die_debug)
bf6af496
DE
19112 {
19113 fprintf_unfiltered (gdb_stdlog,
19114 "Read die from %s@0x%x of %s:\n",
96b79293 19115 reader->die_section->get_name (),
bf6af496
DE
19116 (unsigned) (info_ptr - reader->die_section->buffer),
19117 bfd_get_filename (reader->abfd));
b4f54984 19118 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19119 }
19120
19121 return result;
3019eac3 19122}
433df2d4 19123\f
c906108c 19124
72bf9492
DJ
19125/* Returns nonzero if TAG represents a type that we might generate a partial
19126 symbol for. */
19127
19128static int
d8f62e84 19129is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19130{
19131 switch (tag)
19132 {
19133#if 0
19134 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19135 that we don't at present. Note that normally this does not
19136 matter, mainly because C compilers don't give names to these
19137 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19138 case DW_TAG_file_type:
19139 case DW_TAG_ptr_to_member_type:
19140 case DW_TAG_set_type:
19141 case DW_TAG_string_type:
19142 case DW_TAG_subroutine_type:
19143#endif
d8f62e84
TT
19144
19145 /* GNAT may emit an array with a name, but no typedef, so we
19146 need to make a symbol in this case. */
19147 case DW_TAG_array_type:
19148 return lang == language_ada;
19149
72bf9492
DJ
19150 case DW_TAG_base_type:
19151 case DW_TAG_class_type:
680b30c7 19152 case DW_TAG_interface_type:
72bf9492
DJ
19153 case DW_TAG_enumeration_type:
19154 case DW_TAG_structure_type:
19155 case DW_TAG_subrange_type:
19156 case DW_TAG_typedef:
19157 case DW_TAG_union_type:
19158 return 1;
19159 default:
19160 return 0;
19161 }
19162}
19163
19164/* Load all DIEs that are interesting for partial symbols into memory. */
19165
19166static struct partial_die_info *
dee91e82 19167load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19168 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19169{
dee91e82 19170 struct dwarf2_cu *cu = reader->cu;
5e22e966 19171 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19172 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19173 unsigned int bytes_read;
5afb4e99 19174 unsigned int load_all = 0;
72bf9492
DJ
19175 int nesting_level = 1;
19176
19177 parent_die = NULL;
19178 last_die = NULL;
19179
7adf1e79
DE
19180 gdb_assert (cu->per_cu != NULL);
19181 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19182 load_all = 1;
19183
72bf9492
DJ
19184 cu->partial_dies
19185 = htab_create_alloc_ex (cu->header.length / 12,
19186 partial_die_hash,
19187 partial_die_eq,
19188 NULL,
19189 &cu->comp_unit_obstack,
19190 hashtab_obstack_allocate,
19191 dummy_obstack_deallocate);
19192
72bf9492
DJ
19193 while (1)
19194 {
7c32eebb
TT
19195 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19196 &bytes_read);
72bf9492
DJ
19197
19198 /* A NULL abbrev means the end of a series of children. */
19199 if (abbrev == NULL)
19200 {
19201 if (--nesting_level == 0)
cd9983dd
YQ
19202 return first_die;
19203
72bf9492
DJ
19204 info_ptr += bytes_read;
19205 last_die = parent_die;
19206 parent_die = parent_die->die_parent;
19207 continue;
19208 }
19209
98bfdba5
PA
19210 /* Check for template arguments. We never save these; if
19211 they're seen, we just mark the parent, and go on our way. */
19212 if (parent_die != NULL
19213 && cu->language == language_cplus
19214 && (abbrev->tag == DW_TAG_template_type_param
19215 || abbrev->tag == DW_TAG_template_value_param))
19216 {
19217 parent_die->has_template_arguments = 1;
19218
19219 if (!load_all)
19220 {
19221 /* We don't need a partial DIE for the template argument. */
dee91e82 19222 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19223 continue;
19224 }
19225 }
19226
0d99eb77 19227 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19228 Skip their other children. */
19229 if (!load_all
19230 && cu->language == language_cplus
19231 && parent_die != NULL
f9b5d5ea
TV
19232 && parent_die->tag == DW_TAG_subprogram
19233 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19234 {
dee91e82 19235 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19236 continue;
19237 }
19238
5afb4e99
DJ
19239 /* Check whether this DIE is interesting enough to save. Normally
19240 we would not be interested in members here, but there may be
19241 later variables referencing them via DW_AT_specification (for
19242 static members). */
19243 if (!load_all
d8f62e84 19244 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19245 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19246 && abbrev->tag != DW_TAG_enumerator
19247 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19248 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19249 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19250 && abbrev->tag != DW_TAG_variable
5afb4e99 19251 && abbrev->tag != DW_TAG_namespace
f55ee35c 19252 && abbrev->tag != DW_TAG_module
95554aad 19253 && abbrev->tag != DW_TAG_member
74921315
KS
19254 && abbrev->tag != DW_TAG_imported_unit
19255 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19256 {
19257 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19258 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19259 continue;
19260 }
19261
6f06d47b
YQ
19262 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19263 abbrev);
cd9983dd 19264
48fbe735 19265 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19266
19267 /* This two-pass algorithm for processing partial symbols has a
19268 high cost in cache pressure. Thus, handle some simple cases
19269 here which cover the majority of C partial symbols. DIEs
19270 which neither have specification tags in them, nor could have
19271 specification tags elsewhere pointing at them, can simply be
19272 processed and discarded.
19273
19274 This segment is also optional; scan_partial_symbols and
19275 add_partial_symbol will handle these DIEs if we chain
19276 them in normally. When compilers which do not emit large
19277 quantities of duplicate debug information are more common,
19278 this code can probably be removed. */
19279
19280 /* Any complete simple types at the top level (pretty much all
19281 of them, for a language without namespaces), can be processed
19282 directly. */
19283 if (parent_die == NULL
cd9983dd
YQ
19284 && pdi.has_specification == 0
19285 && pdi.is_declaration == 0
19286 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19287 || pdi.tag == DW_TAG_base_type
d8f62e84 19288 || pdi.tag == DW_TAG_array_type
cd9983dd 19289 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19290 {
7d00ffec 19291 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19292 add_partial_symbol (&pdi, cu);
19293
cd9983dd 19294 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19295 continue;
19296 }
19297
d8228535
JK
19298 /* The exception for DW_TAG_typedef with has_children above is
19299 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19300 type_name_or_error will error on such types later.
d8228535
JK
19301
19302 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19303 it could not find the child DIEs referenced later, this is checked
19304 above. In correct DWARF DW_TAG_typedef should have no children. */
19305
cd9983dd 19306 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19307 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19308 "- DIE at %s [in module %s]"),
cd9983dd 19309 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19310
72bf9492
DJ
19311 /* If we're at the second level, and we're an enumerator, and
19312 our parent has no specification (meaning possibly lives in a
19313 namespace elsewhere), then we can add the partial symbol now
19314 instead of queueing it. */
cd9983dd 19315 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19316 && parent_die != NULL
19317 && parent_die->die_parent == NULL
19318 && parent_die->tag == DW_TAG_enumeration_type
19319 && parent_die->has_specification == 0)
19320 {
7d00ffec 19321 if (pdi.raw_name == NULL)
b98664d3 19322 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19323 else if (building_psymtab)
f0fbb768 19324 add_partial_symbol (&pdi, cu);
72bf9492 19325
cd9983dd 19326 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19327 continue;
19328 }
19329
cd9983dd 19330 struct partial_die_info *part_die
6f06d47b 19331 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19332
72bf9492
DJ
19333 /* We'll save this DIE so link it in. */
19334 part_die->die_parent = parent_die;
19335 part_die->die_sibling = NULL;
19336 part_die->die_child = NULL;
19337
19338 if (last_die && last_die == parent_die)
19339 last_die->die_child = part_die;
19340 else if (last_die)
19341 last_die->die_sibling = part_die;
19342
19343 last_die = part_die;
19344
19345 if (first_die == NULL)
19346 first_die = part_die;
19347
19348 /* Maybe add the DIE to the hash table. Not all DIEs that we
19349 find interesting need to be in the hash table, because we
19350 also have the parent/sibling/child chains; only those that we
19351 might refer to by offset later during partial symbol reading.
19352
19353 For now this means things that might have be the target of a
19354 DW_AT_specification, DW_AT_abstract_origin, or
19355 DW_AT_extension. DW_AT_extension will refer only to
19356 namespaces; DW_AT_abstract_origin refers to functions (and
19357 many things under the function DIE, but we do not recurse
19358 into function DIEs during partial symbol reading) and
19359 possibly variables as well; DW_AT_specification refers to
19360 declarations. Declarations ought to have the DW_AT_declaration
19361 flag. It happens that GCC forgets to put it in sometimes, but
19362 only for functions, not for types.
19363
19364 Adding more things than necessary to the hash table is harmless
19365 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19366 wasted time in find_partial_die, when we reread the compilation
19367 unit with load_all_dies set. */
72bf9492 19368
5afb4e99 19369 if (load_all
72929c62 19370 || abbrev->tag == DW_TAG_constant
5afb4e99 19371 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19372 || abbrev->tag == DW_TAG_variable
19373 || abbrev->tag == DW_TAG_namespace
19374 || part_die->is_declaration)
19375 {
19376 void **slot;
19377
19378 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19379 to_underlying (part_die->sect_off),
19380 INSERT);
72bf9492
DJ
19381 *slot = part_die;
19382 }
19383
72bf9492 19384 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19385 we have no reason to follow the children of structures; for other
98bfdba5
PA
19386 languages we have to, so that we can get at method physnames
19387 to infer fully qualified class names, for DW_AT_specification,
19388 and for C++ template arguments. For C++, we also look one level
19389 inside functions to find template arguments (if the name of the
19390 function does not already contain the template arguments).
bc30ff58 19391
0a4b0913
AB
19392 For Ada and Fortran, we need to scan the children of subprograms
19393 and lexical blocks as well because these languages allow the
19394 definition of nested entities that could be interesting for the
19395 debugger, such as nested subprograms for instance. */
72bf9492 19396 if (last_die->has_children
5afb4e99
DJ
19397 && (load_all
19398 || last_die->tag == DW_TAG_namespace
f55ee35c 19399 || last_die->tag == DW_TAG_module
72bf9492 19400 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19401 || (cu->language == language_cplus
19402 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19403 && (last_die->raw_name == NULL
19404 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19405 || (cu->language != language_c
19406 && (last_die->tag == DW_TAG_class_type
680b30c7 19407 || last_die->tag == DW_TAG_interface_type
72bf9492 19408 || last_die->tag == DW_TAG_structure_type
bc30ff58 19409 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19410 || ((cu->language == language_ada
19411 || cu->language == language_fortran)
bc30ff58
JB
19412 && (last_die->tag == DW_TAG_subprogram
19413 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19414 {
19415 nesting_level++;
19416 parent_die = last_die;
19417 continue;
19418 }
19419
19420 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19421 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19422
19423 /* Back to the top, do it again. */
19424 }
19425}
19426
6f06d47b 19427partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19428 const struct abbrev_info *abbrev)
6f06d47b
YQ
19429 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19430{
19431}
19432
7d00ffec
TT
19433/* See class definition. */
19434
19435const char *
19436partial_die_info::name (dwarf2_cu *cu)
19437{
19438 if (!canonical_name && raw_name != nullptr)
19439 {
19440 struct objfile *objfile = cu->per_objfile->objfile;
19441 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19442 canonical_name = 1;
19443 }
19444
19445 return raw_name;
19446}
19447
35cc7ed7
YQ
19448/* Read a minimal amount of information into the minimal die structure.
19449 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19450
48fbe735
YQ
19451const gdb_byte *
19452partial_die_info::read (const struct die_reader_specs *reader,
19453 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19454{
dee91e82 19455 struct dwarf2_cu *cu = reader->cu;
976ca316 19456 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19457 unsigned int i;
c5aa993b 19458 int has_low_pc_attr = 0;
c906108c 19459 int has_high_pc_attr = 0;
91da1414 19460 int high_pc_relative = 0;
c906108c 19461
fd0a254f 19462 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19463 {
e7da7f8f 19464 attribute attr;
7a5f294d 19465 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19466 /* String and address offsets that need to do the reprocessing have
dda83cd7 19467 already been read at this point, so there is no need to wait until
18a8505e 19468 the loop terminates to do the reprocessing. */
7a5f294d 19469 if (attr.requires_reprocessing_p ())
d0ce17d8 19470 read_attribute_reprocess (reader, &attr, tag);
c906108c 19471 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19472 partial symbol table. */
c906108c
SS
19473 switch (attr.name)
19474 {
19475 case DW_AT_name:
48fbe735 19476 switch (tag)
71c25dea
TT
19477 {
19478 case DW_TAG_compile_unit:
95554aad 19479 case DW_TAG_partial_unit:
348e048f 19480 case DW_TAG_type_unit:
71c25dea
TT
19481 /* Compilation units have a DW_AT_name that is a filename, not
19482 a source language identifier. */
19483 case DW_TAG_enumeration_type:
19484 case DW_TAG_enumerator:
19485 /* These tags always have simple identifiers already; no need
19486 to canonicalize them. */
7d00ffec 19487 canonical_name = 1;
2c830f54 19488 raw_name = attr.as_string ();
71c25dea
TT
19489 break;
19490 default:
7d00ffec 19491 canonical_name = 0;
2c830f54 19492 raw_name = attr.as_string ();
71c25dea
TT
19493 break;
19494 }
c906108c 19495 break;
31ef98ae 19496 case DW_AT_linkage_name:
c906108c 19497 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19498 /* Note that both forms of linkage name might appear. We
19499 assume they will be the same, and we only store the last
19500 one we see. */
95f982e5 19501 linkage_name = attr.as_string ();
c906108c
SS
19502 break;
19503 case DW_AT_low_pc:
19504 has_low_pc_attr = 1;
95f982e5 19505 lowpc = attr.as_address ();
c906108c
SS
19506 break;
19507 case DW_AT_high_pc:
19508 has_high_pc_attr = 1;
95f982e5 19509 highpc = attr.as_address ();
cd6c91b4 19510 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19511 high_pc_relative = 1;
c906108c
SS
19512 break;
19513 case DW_AT_location:
dda83cd7
SM
19514 /* Support the .debug_loc offsets. */
19515 if (attr.form_is_block ())
19516 {
9d2246fc 19517 d.locdesc = attr.as_block ();
dda83cd7
SM
19518 }
19519 else if (attr.form_is_section_offset ())
19520 {
4d3c2250 19521 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19522 }
19523 else
19524 {
4d3c2250
KB
19525 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19526 "partial symbol information");
dda83cd7 19527 }
c906108c 19528 break;
c906108c 19529 case DW_AT_external:
c45bc3f8 19530 is_external = attr.as_boolean ();
c906108c
SS
19531 break;
19532 case DW_AT_declaration:
c45bc3f8 19533 is_declaration = attr.as_boolean ();
c906108c
SS
19534 break;
19535 case DW_AT_type:
48fbe735 19536 has_type = 1;
c906108c
SS
19537 break;
19538 case DW_AT_abstract_origin:
19539 case DW_AT_specification:
72bf9492 19540 case DW_AT_extension:
48fbe735 19541 has_specification = 1;
0826b30a 19542 spec_offset = attr.get_ref_die_offset ();
48fbe735 19543 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19544 || cu->per_cu->is_dwz);
c906108c
SS
19545 break;
19546 case DW_AT_sibling:
19547 /* Ignore absolute siblings, they might point outside of
19548 the current compile unit. */
19549 if (attr.form == DW_FORM_ref_addr)
b98664d3 19550 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19551 else
b9502d3f 19552 {
48fbe735 19553 const gdb_byte *buffer = reader->buffer;
0826b30a 19554 sect_offset off = attr.get_ref_die_offset ();
9c541725 19555 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19556
19557 if (sibling_ptr < info_ptr)
b98664d3 19558 complaint (_("DW_AT_sibling points backwards"));
22869d73 19559 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19560 reader->die_section->overflow_complaint ();
b9502d3f 19561 else
48fbe735 19562 sibling = sibling_ptr;
b9502d3f 19563 }
c906108c 19564 break;
dda83cd7
SM
19565 case DW_AT_byte_size:
19566 has_byte_size = 1;
19567 break;
19568 case DW_AT_const_value:
19569 has_const_value = 1;
19570 break;
68511cec
CES
19571 case DW_AT_calling_convention:
19572 /* DWARF doesn't provide a way to identify a program's source-level
19573 entry point. DW_AT_calling_convention attributes are only meant
19574 to describe functions' calling conventions.
19575
19576 However, because it's a necessary piece of information in
0c1b455e
TT
19577 Fortran, and before DWARF 4 DW_CC_program was the only
19578 piece of debugging information whose definition refers to
19579 a 'main program' at all, several compilers marked Fortran
19580 main programs with DW_CC_program --- even when those
19581 functions use the standard calling conventions.
19582
19583 Although DWARF now specifies a way to provide this
19584 information, we support this practice for backward
19585 compatibility. */
529908cb 19586 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19587 && cu->language == language_fortran)
48fbe735 19588 main_subprogram = 1;
68511cec 19589 break;
481860b3 19590 case DW_AT_inline:
529908cb
TT
19591 {
19592 LONGEST value = attr.constant_value (-1);
19593 if (value == DW_INL_inlined
19594 || value == DW_INL_declared_inlined)
19595 may_be_inlined = 1;
19596 }
481860b3 19597 break;
95554aad
TT
19598
19599 case DW_AT_import:
48fbe735 19600 if (tag == DW_TAG_imported_unit)
36586728 19601 {
0826b30a 19602 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19603 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19604 || cu->per_cu->is_dwz);
19605 }
95554aad
TT
19606 break;
19607
0c1b455e 19608 case DW_AT_main_subprogram:
c45bc3f8 19609 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19610 break;
19611
05caa1d2
TT
19612 case DW_AT_ranges:
19613 {
2b0c7f41
SM
19614 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19615 on DWARF version). */
19616 ULONGEST ranges_offset = attr.as_unsigned ();
19617
19618 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19619 this value. */
19620 if (tag != DW_TAG_compile_unit)
19621 ranges_offset += cu->gnu_ranges_base;
19622
05caa1d2 19623 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19624 nullptr, tag))
05caa1d2
TT
19625 has_pc_info = 1;
19626 }
19627 break;
19628
c906108c
SS
19629 default:
19630 break;
19631 }
19632 }
19633
10d06d82
TT
19634 /* For Ada, if both the name and the linkage name appear, we prefer
19635 the latter. This lets "catch exception" work better, regardless
19636 of the order in which the name and linkage name were emitted.
19637 Really, though, this is just a workaround for the fact that gdb
19638 doesn't store both the name and the linkage name. */
19639 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19640 raw_name = linkage_name;
10d06d82 19641
91da1414 19642 if (high_pc_relative)
48fbe735 19643 highpc += lowpc;
91da1414 19644
9373cf26
JK
19645 if (has_low_pc_attr && has_high_pc_attr)
19646 {
19647 /* When using the GNU linker, .gnu.linkonce. sections are used to
19648 eliminate duplicate copies of functions and vtables and such.
19649 The linker will arbitrarily choose one and discard the others.
19650 The AT_*_pc values for such functions refer to local labels in
19651 these sections. If the section from that file was discarded, the
19652 labels are not in the output, so the relocs get a value of 0.
19653 If this is a discarded function, mark the pc bounds as invalid,
19654 so that GDB will ignore it. */
976ca316 19655 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19656 {
976ca316 19657 struct objfile *objfile = per_objfile->objfile;
08feed99 19658 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19659
b98664d3 19660 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19661 "for DIE at %s [in module %s]"),
48fbe735
YQ
19662 paddress (gdbarch, lowpc),
19663 sect_offset_str (sect_off),
9d8780f0 19664 objfile_name (objfile));
9373cf26
JK
19665 }
19666 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19667 else if (lowpc >= highpc)
9373cf26 19668 {
976ca316 19669 struct objfile *objfile = per_objfile->objfile;
08feed99 19670 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19671
b98664d3 19672 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19673 "for DIE at %s [in module %s]"),
48fbe735
YQ
19674 paddress (gdbarch, lowpc),
19675 paddress (gdbarch, highpc),
19676 sect_offset_str (sect_off),
9c541725 19677 objfile_name (objfile));
9373cf26
JK
19678 }
19679 else
48fbe735 19680 has_pc_info = 1;
9373cf26 19681 }
85cbf3d3 19682
c906108c
SS
19683 return info_ptr;
19684}
19685
72bf9492
DJ
19686/* Find a cached partial DIE at OFFSET in CU. */
19687
d590ff25
YQ
19688struct partial_die_info *
19689dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19690{
19691 struct partial_die_info *lookup_die = NULL;
6f06d47b 19692 struct partial_die_info part_die (sect_off);
72bf9492 19693
9a3c8263 19694 lookup_die = ((struct partial_die_info *)
d590ff25 19695 htab_find_with_hash (partial_dies, &part_die,
9c541725 19696 to_underlying (sect_off)));
72bf9492 19697
72bf9492
DJ
19698 return lookup_die;
19699}
19700
348e048f
DE
19701/* Find a partial DIE at OFFSET, which may or may not be in CU,
19702 except in the case of .debug_types DIEs which do not reference
19703 outside their CU (they do however referencing other types via
55f1336d 19704 DW_FORM_ref_sig8). */
72bf9492 19705
122cf0f2 19706static const struct cu_partial_die_info
9c541725 19707find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19708{
976ca316
SM
19709 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19710 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19711 struct partial_die_info *pd = NULL;
72bf9492 19712
36586728 19713 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19714 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19715 {
d590ff25 19716 pd = cu->find_partial_die (sect_off);
5afb4e99 19717 if (pd != NULL)
fb816e8b 19718 return { cu, pd };
0d99eb77
DE
19719 /* We missed recording what we needed.
19720 Load all dies and try again. */
5afb4e99 19721 }
0d99eb77
DE
19722 else
19723 {
19724 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19725 if (cu->per_cu->is_debug_types)
0d99eb77 19726 {
9d8780f0
SM
19727 error (_("Dwarf Error: Type Unit at offset %s contains"
19728 " external reference to offset %s [in module %s].\n"),
19729 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19730 bfd_get_filename (objfile->obfd));
19731 }
7188ed02
SM
19732 dwarf2_per_cu_data *per_cu
19733 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19734 per_objfile);
72bf9492 19735
976ca316 19736 cu = per_objfile->get_cu (per_cu);
7188ed02 19737 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19738 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19739
976ca316 19740 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19741
19742 cu->last_used = 0;
19743 pd = cu->find_partial_die (sect_off);
0d99eb77 19744 }
5afb4e99 19745
dee91e82
DE
19746 /* If we didn't find it, and not all dies have been loaded,
19747 load them all and try again. */
19748
7188ed02 19749 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19750 {
7188ed02 19751 cu->per_cu->load_all_dies = 1;
fd820528
DE
19752
19753 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19754 THIS_CU->cu may already be in use. So we can't just free it and
19755 replace its DIEs with the ones we read in. Instead, we leave those
19756 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19757 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19758 set. */
976ca316 19759 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19760
7188ed02 19761 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19762 }
19763
19764 if (pd == NULL)
521894aa 19765 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19766 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19767 return { cu, pd };
72bf9492
DJ
19768}
19769
abc72ce4
DE
19770/* See if we can figure out if the class lives in a namespace. We do
19771 this by looking for a member function; its demangled name will
19772 contain namespace info, if there is any. */
19773
19774static void
19775guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19776 struct dwarf2_cu *cu)
19777{
19778 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19779 what template types look like, because the demangler
19780 frequently doesn't give the same name as the debug info. We
19781 could fix this by only using the demangled name to get the
19782 prefix (but see comment in read_structure_type). */
19783
19784 struct partial_die_info *real_pdi;
19785 struct partial_die_info *child_pdi;
19786
19787 /* If this DIE (this DIE's specification, if any) has a parent, then
19788 we should not do this. We'll prepend the parent's fully qualified
19789 name when we create the partial symbol. */
19790
19791 real_pdi = struct_pdi;
19792 while (real_pdi->has_specification)
fb816e8b 19793 {
122cf0f2
AB
19794 auto res = find_partial_die (real_pdi->spec_offset,
19795 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19796 real_pdi = res.pdi;
19797 cu = res.cu;
19798 }
abc72ce4
DE
19799
19800 if (real_pdi->die_parent != NULL)
19801 return;
19802
19803 for (child_pdi = struct_pdi->die_child;
19804 child_pdi != NULL;
19805 child_pdi = child_pdi->die_sibling)
19806 {
19807 if (child_pdi->tag == DW_TAG_subprogram
19808 && child_pdi->linkage_name != NULL)
19809 {
43816ebc 19810 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19811 (cu->language_defn->class_name_from_physname
19812 (child_pdi->linkage_name));
abc72ce4
DE
19813 if (actual_class_name != NULL)
19814 {
5e22e966 19815 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19816 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19817 struct_pdi->canonical_name = 1;
abc72ce4
DE
19818 }
19819 break;
19820 }
19821 }
19822}
19823
25c11aca
TV
19824/* Return true if a DIE with TAG may have the DW_AT_const_value
19825 attribute. */
19826
19827static bool
19828can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19829{
19830 switch (tag)
19831 {
19832 case DW_TAG_constant:
19833 case DW_TAG_enumerator:
19834 case DW_TAG_formal_parameter:
19835 case DW_TAG_template_value_param:
19836 case DW_TAG_variable:
19837 return true;
19838 }
19839
19840 return false;
19841}
19842
52356b79
YQ
19843void
19844partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19845{
abc72ce4
DE
19846 /* Once we've fixed up a die, there's no point in doing so again.
19847 This also avoids a memory leak if we were to call
19848 guess_partial_die_structure_name multiple times. */
52356b79 19849 if (fixup_called)
abc72ce4
DE
19850 return;
19851
72bf9492
DJ
19852 /* If we found a reference attribute and the DIE has no name, try
19853 to find a name in the referred to DIE. */
19854
7d00ffec 19855 if (raw_name == NULL && has_specification)
72bf9492
DJ
19856 {
19857 struct partial_die_info *spec_die;
72bf9492 19858
122cf0f2 19859 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19860 spec_die = res.pdi;
19861 cu = res.cu;
72bf9492 19862
52356b79 19863 spec_die->fixup (cu);
72bf9492 19864
7d00ffec 19865 if (spec_die->raw_name)
72bf9492 19866 {
7d00ffec
TT
19867 raw_name = spec_die->raw_name;
19868 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19869
19870 /* Copy DW_AT_external attribute if it is set. */
19871 if (spec_die->is_external)
52356b79 19872 is_external = spec_die->is_external;
72bf9492
DJ
19873 }
19874 }
19875
25c11aca
TV
19876 if (!has_const_value && has_specification
19877 && can_have_DW_AT_const_value_p (tag))
19878 {
19879 struct partial_die_info *spec_die;
19880
19881 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19882 spec_die = res.pdi;
19883 cu = res.cu;
19884
19885 spec_die->fixup (cu);
19886
19887 if (spec_die->has_const_value)
19888 {
19889 /* Copy DW_AT_const_value attribute if it is set. */
19890 has_const_value = spec_die->has_const_value;
19891 }
19892 }
19893
72bf9492 19894 /* Set default names for some unnamed DIEs. */
72bf9492 19895
7d00ffec
TT
19896 if (raw_name == NULL && tag == DW_TAG_namespace)
19897 {
19898 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19899 canonical_name = 1;
19900 }
72bf9492 19901
abc72ce4
DE
19902 /* If there is no parent die to provide a namespace, and there are
19903 children, see if we can determine the namespace from their linkage
122d1940 19904 name. */
abc72ce4 19905 if (cu->language == language_cplus
5e22e966 19906 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19907 && die_parent == NULL
19908 && has_children
19909 && (tag == DW_TAG_class_type
19910 || tag == DW_TAG_structure_type
19911 || tag == DW_TAG_union_type))
19912 guess_partial_die_structure_name (this, cu);
abc72ce4 19913
53832f31
TT
19914 /* GCC might emit a nameless struct or union that has a linkage
19915 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19916 if (raw_name == NULL
52356b79
YQ
19917 && (tag == DW_TAG_class_type
19918 || tag == DW_TAG_interface_type
19919 || tag == DW_TAG_structure_type
19920 || tag == DW_TAG_union_type)
19921 && linkage_name != NULL)
53832f31 19922 {
43816ebc
TT
19923 gdb::unique_xmalloc_ptr<char> demangled
19924 (gdb_demangle (linkage_name, DMGL_TYPES));
19925 if (demangled != nullptr)
53832f31 19926 {
96408a79
SA
19927 const char *base;
19928
19929 /* Strip any leading namespaces/classes, keep only the base name.
19930 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19931 base = strrchr (demangled.get (), ':');
19932 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19933 base++;
19934 else
43816ebc 19935 base = demangled.get ();
96408a79 19936
5e22e966 19937 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19938 raw_name = objfile->intern (base);
19939 canonical_name = 1;
53832f31
TT
19940 }
19941 }
19942
52356b79 19943 fixup_called = 1;
72bf9492
DJ
19944}
19945
d0ce17d8 19946/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
19947 contents from the given SECTION in the HEADER.
19948
19949 HEADER_OFFSET is the offset of the header in the section. */
41144253 19950static void
d0ce17d8 19951read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
19952 struct dwarf2_section_info *section,
19953 sect_offset header_offset)
41144253 19954{
19955 unsigned int bytes_read;
19956 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
19957 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19958
41144253 19959 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19960 info_ptr += bytes_read;
a1c40103 19961
41144253 19962 header->version = read_2_bytes (abfd, info_ptr);
19963 info_ptr += 2;
a1c40103 19964
41144253 19965 header->addr_size = read_1_byte (abfd, info_ptr);
19966 info_ptr += 1;
a1c40103 19967
41144253 19968 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19969 info_ptr += 1;
a1c40103 19970
41144253 19971 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19972}
19973
19974/* Return the DW_AT_loclists_base value for the CU. */
19975static ULONGEST
19976lookup_loclist_base (struct dwarf2_cu *cu)
19977{
19978 /* For the .dwo unit, the loclist_base points to the first offset following
19979 the header. The header consists of the following entities-
19980 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19981 bit format)
19982 2. version (2 bytes)
19983 3. address size (1 byte)
19984 4. segment selector size (1 byte)
19985 5. offset entry count (4 bytes)
19986 These sizes are derived as per the DWARFv5 standard. */
19987 if (cu->dwo_unit != nullptr)
19988 {
19989 if (cu->header.initial_length_size == 4)
19990 return LOCLIST_HEADER_SIZE32;
19991 return LOCLIST_HEADER_SIZE64;
19992 }
19993 return cu->loclist_base;
19994}
19995
19996/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19997 array of offsets in the .debug_loclists section. */
e57933dc
SM
19998
19999static sect_offset
41144253 20000read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20001{
976ca316
SM
20002 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20003 struct objfile *objfile = per_objfile->objfile;
41144253 20004 bfd *abfd = objfile->obfd;
a1c40103
SM
20005 ULONGEST loclist_header_size =
20006 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20007 : LOCLIST_HEADER_SIZE64);
41144253 20008 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
20009
20010 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20011 ULONGEST start_offset =
20012 loclist_base + loclist_index * cu->header.offset_size;
20013
a1c40103 20014 /* Get loclists section. */
41144253 20015 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20016
a1c40103 20017 /* Read the loclists section content. */
41144253 20018 section->read (objfile);
20019 if (section->buffer == NULL)
a0c1eeba
SM
20020 error (_("DW_FORM_loclistx used without .debug_loclists "
20021 "section [in module %s]"), objfile_name (objfile));
20022
a1c40103
SM
20023 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20024 so if loclist_base is smaller than the header size, we have a problem. */
20025 if (loclist_base < loclist_header_size)
20026 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20027 objfile_name (objfile));
20028
20029 /* Read the header of the loclists contribution. */
d0ce17d8 20030 struct loclists_rnglists_header header;
a1c40103
SM
20031 read_loclists_rnglists_header (&header, section,
20032 (sect_offset) (loclist_base - loclist_header_size));
20033
20034 /* Verify the loclist index is valid. */
41144253 20035 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
20036 error (_("DW_FORM_loclistx pointing outside of "
20037 ".debug_loclists offset array [in module %s]"),
20038 objfile_name (objfile));
20039
05787bad
SM
20040 /* Validate that reading won't go beyond the end of the section. */
20041 if (start_offset + cu->header.offset_size > section->size)
20042 error (_("Reading DW_FORM_loclistx index beyond end of"
20043 ".debug_loclists section [in module %s]"),
20044 objfile_name (objfile));
20045
20046 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 20047
20048 if (cu->header.offset_size == 4)
e57933dc 20049 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 20050 else
e57933dc 20051 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 20052}
20053
d0ce17d8
CT
20054/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20055 array of offsets in the .debug_rnglists section. */
e57933dc
SM
20056
20057static sect_offset
d0ce17d8
CT
20058read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20059 dwarf_tag tag)
20060{
20061 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20062 struct objfile *objfile = dwarf2_per_objfile->objfile;
20063 bfd *abfd = objfile->obfd;
20064 ULONGEST rnglist_header_size =
20065 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20066 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
20067
20068 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20069 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20070 doesn't apply. */
d0ce17d8 20071 ULONGEST rnglist_base =
2b0c7f41 20072 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
20073
20074 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
20075 ULONGEST start_offset =
20076 rnglist_base + rnglist_index * cu->header.offset_size;
20077
20078 /* Get rnglists section. */
20079 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20080
20081 /* Read the rnglists section content. */
20082 section->read (objfile);
20083 if (section->buffer == nullptr)
20084 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20085 "[in module %s]"),
20086 objfile_name (objfile));
20087
a1c40103
SM
20088 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20089 so if rnglist_base is smaller than the header size, we have a problem. */
20090 if (rnglist_base < rnglist_header_size)
20091 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20092 objfile_name (objfile));
20093
20094 /* Read the header of the rnglists contribution. */
d0ce17d8 20095 struct loclists_rnglists_header header;
a1c40103
SM
20096 read_loclists_rnglists_header (&header, section,
20097 (sect_offset) (rnglist_base - rnglist_header_size));
20098
20099 /* Verify the rnglist index is valid. */
d0ce17d8
CT
20100 if (rnglist_index >= header.offset_entry_count)
20101 error (_("DW_FORM_rnglistx index pointing outside of "
20102 ".debug_rnglists offset array [in module %s]"),
20103 objfile_name (objfile));
20104
d0ce17d8 20105 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20106 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20107 error (_("Reading DW_FORM_rnglistx index beyond end of"
20108 ".debug_rnglists section [in module %s]"),
20109 objfile_name (objfile));
20110
20111 const gdb_byte *info_ptr = section->buffer + start_offset;
20112
20113 if (cu->header.offset_size == 4)
e57933dc 20114 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 20115 else
e57933dc 20116 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
20117}
20118
18a8505e
AT
20119/* Process the attributes that had to be skipped in the first round. These
20120 attributes are the ones that need str_offsets_base or addr_base attributes.
20121 They could not have been processed in the first round, because at the time
20122 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20123static void
20124read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20125 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20126{
20127 struct dwarf2_cu *cu = reader->cu;
20128 switch (attr->form)
20129 {
20130 case DW_FORM_addrx:
20131 case DW_FORM_GNU_addr_index:
36d378cf
TT
20132 attr->set_address (read_addr_index (cu,
20133 attr->as_unsigned_reprocess ()));
dda83cd7 20134 break;
41144253 20135 case DW_FORM_loclistx:
b1829e1b 20136 {
e57933dc 20137 sect_offset loclists_sect_off
b1829e1b
SM
20138 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20139
e57933dc 20140 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
20141 }
20142 break;
d0ce17d8 20143 case DW_FORM_rnglistx:
b1829e1b 20144 {
e57933dc 20145 sect_offset rnglists_sect_off
b1829e1b
SM
20146 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20147
e57933dc 20148 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 20149 }
dda83cd7 20150 break;
18a8505e
AT
20151 case DW_FORM_strx:
20152 case DW_FORM_strx1:
20153 case DW_FORM_strx2:
20154 case DW_FORM_strx3:
20155 case DW_FORM_strx4:
20156 case DW_FORM_GNU_str_index:
20157 {
fe56917a 20158 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20159 gdb_assert (!attr->canonical_string_p ());
18a8505e 20160 if (reader->dwo_file != NULL)
c6481205
TT
20161 attr->set_string_noncanonical (read_dwo_str_index (reader,
20162 str_index));
18a8505e 20163 else
c6481205
TT
20164 attr->set_string_noncanonical (read_stub_str_index (cu,
20165 str_index));
18a8505e
AT
20166 break;
20167 }
20168 default:
20169 gdb_assert_not_reached (_("Unexpected DWARF form."));
20170 }
20171}
20172
a8329558 20173/* Read an attribute value described by an attribute form. */
c906108c 20174
d521ce57 20175static const gdb_byte *
dee91e82
DE
20176read_attribute_value (const struct die_reader_specs *reader,
20177 struct attribute *attr, unsigned form,
7a5f294d 20178 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20179{
dee91e82 20180 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20181 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20182 struct objfile *objfile = per_objfile->objfile;
dee91e82 20183 bfd *abfd = reader->abfd;
e7c27a73 20184 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20185 unsigned int bytes_read;
20186 struct dwarf_block *blk;
20187
aead7601 20188 attr->form = (enum dwarf_form) form;
a8329558 20189 switch (form)
c906108c 20190 {
c906108c 20191 case DW_FORM_ref_addr:
7c290a04
TT
20192 if (cu_header->version == 2)
20193 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 20194 &bytes_read));
ae411497 20195 else
7c290a04 20196 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20197 &bytes_read));
ae411497
TT
20198 info_ptr += bytes_read;
20199 break;
36586728 20200 case DW_FORM_GNU_ref_alt:
7c290a04 20201 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20202 &bytes_read));
36586728
TT
20203 info_ptr += bytes_read;
20204 break;
ae411497 20205 case DW_FORM_addr:
08feed99
TT
20206 {
20207 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 20208 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
20209 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20210 attr->set_address (addr);
08feed99
TT
20211 info_ptr += bytes_read;
20212 }
c906108c
SS
20213 break;
20214 case DW_FORM_block2:
7b5a2f43 20215 blk = dwarf_alloc_block (cu);
c906108c
SS
20216 blk->size = read_2_bytes (abfd, info_ptr);
20217 info_ptr += 2;
20218 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20219 info_ptr += blk->size;
9d2246fc 20220 attr->set_block (blk);
c906108c
SS
20221 break;
20222 case DW_FORM_block4:
7b5a2f43 20223 blk = dwarf_alloc_block (cu);
c906108c
SS
20224 blk->size = read_4_bytes (abfd, info_ptr);
20225 info_ptr += 4;
20226 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20227 info_ptr += blk->size;
9d2246fc 20228 attr->set_block (blk);
c906108c
SS
20229 break;
20230 case DW_FORM_data2:
414ad644 20231 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20232 info_ptr += 2;
20233 break;
20234 case DW_FORM_data4:
414ad644 20235 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20236 info_ptr += 4;
20237 break;
20238 case DW_FORM_data8:
414ad644 20239 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20240 info_ptr += 8;
20241 break;
0224619f
JK
20242 case DW_FORM_data16:
20243 blk = dwarf_alloc_block (cu);
20244 blk->size = 16;
20245 blk->data = read_n_bytes (abfd, info_ptr, 16);
20246 info_ptr += 16;
9d2246fc 20247 attr->set_block (blk);
0224619f 20248 break;
2dc7f7b3 20249 case DW_FORM_sec_offset:
7c290a04 20250 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20251 &bytes_read));
2dc7f7b3
TT
20252 info_ptr += bytes_read;
20253 break;
41144253 20254 case DW_FORM_loclistx:
20255 {
7a5f294d
TT
20256 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20257 &bytes_read));
20258 info_ptr += bytes_read;
41144253 20259 }
20260 break;
c906108c 20261 case DW_FORM_string:
c6481205
TT
20262 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20263 &bytes_read));
c906108c
SS
20264 info_ptr += bytes_read;
20265 break;
4bdf3d34 20266 case DW_FORM_strp:
36586728
TT
20267 if (!cu->per_cu->is_dwz)
20268 {
c6481205
TT
20269 attr->set_string_noncanonical
20270 (read_indirect_string (per_objfile,
20271 abfd, info_ptr, cu_header,
20272 &bytes_read));
36586728
TT
20273 info_ptr += bytes_read;
20274 break;
20275 }
20276 /* FALLTHROUGH */
43988095
JK
20277 case DW_FORM_line_strp:
20278 if (!cu->per_cu->is_dwz)
20279 {
c6481205
TT
20280 attr->set_string_noncanonical
20281 (per_objfile->read_line_string (info_ptr, cu_header,
20282 &bytes_read));
43988095
JK
20283 info_ptr += bytes_read;
20284 break;
20285 }
20286 /* FALLTHROUGH */
36586728
TT
20287 case DW_FORM_GNU_strp_alt:
20288 {
a7308ce0 20289 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
20290 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20291 &bytes_read);
36586728 20292
c6481205
TT
20293 attr->set_string_noncanonical
20294 (dwz->read_string (objfile, str_offset));
36586728
TT
20295 info_ptr += bytes_read;
20296 }
4bdf3d34 20297 break;
2dc7f7b3 20298 case DW_FORM_exprloc:
c906108c 20299 case DW_FORM_block:
7b5a2f43 20300 blk = dwarf_alloc_block (cu);
c906108c
SS
20301 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20302 info_ptr += bytes_read;
20303 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20304 info_ptr += blk->size;
9d2246fc 20305 attr->set_block (blk);
c906108c
SS
20306 break;
20307 case DW_FORM_block1:
7b5a2f43 20308 blk = dwarf_alloc_block (cu);
c906108c
SS
20309 blk->size = read_1_byte (abfd, info_ptr);
20310 info_ptr += 1;
20311 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20312 info_ptr += blk->size;
9d2246fc 20313 attr->set_block (blk);
c906108c
SS
20314 break;
20315 case DW_FORM_data1:
c906108c 20316 case DW_FORM_flag:
414ad644 20317 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20318 info_ptr += 1;
20319 break;
2dc7f7b3 20320 case DW_FORM_flag_present:
414ad644 20321 attr->set_unsigned (1);
2dc7f7b3 20322 break;
c906108c 20323 case DW_FORM_sdata:
1bc397c5 20324 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20325 info_ptr += bytes_read;
20326 break;
18a8505e 20327 case DW_FORM_rnglistx:
7a5f294d
TT
20328 {
20329 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20330 &bytes_read));
20331 info_ptr += bytes_read;
20332 }
20333 break;
d0ce17d8 20334 case DW_FORM_udata:
414ad644 20335 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20336 info_ptr += bytes_read;
20337 break;
20338 case DW_FORM_ref1:
7c290a04 20339 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20340 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20341 info_ptr += 1;
20342 break;
20343 case DW_FORM_ref2:
7c290a04 20344 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20345 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20346 info_ptr += 2;
20347 break;
20348 case DW_FORM_ref4:
7c290a04 20349 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20350 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20351 info_ptr += 4;
20352 break;
613e1657 20353 case DW_FORM_ref8:
7c290a04 20354 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20355 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20356 info_ptr += 8;
20357 break;
55f1336d 20358 case DW_FORM_ref_sig8:
630ed6b9 20359 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20360 info_ptr += 8;
20361 break;
c906108c 20362 case DW_FORM_ref_udata:
7c290a04 20363 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20364 + read_unsigned_leb128 (abfd, info_ptr,
20365 &bytes_read)));
c906108c
SS
20366 info_ptr += bytes_read;
20367 break;
c906108c 20368 case DW_FORM_indirect:
a8329558
KW
20369 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20370 info_ptr += bytes_read;
43988095
JK
20371 if (form == DW_FORM_implicit_const)
20372 {
20373 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20374 info_ptr += bytes_read;
20375 }
20376 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20377 info_ptr);
43988095
JK
20378 break;
20379 case DW_FORM_implicit_const:
1bc397c5 20380 attr->set_signed (implicit_const);
a8329558 20381 break;
336d760d 20382 case DW_FORM_addrx:
3019eac3 20383 case DW_FORM_GNU_addr_index:
fe56917a
TT
20384 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20385 &bytes_read));
3019eac3
DE
20386 info_ptr += bytes_read;
20387 break;
cf532bd1 20388 case DW_FORM_strx:
15f18d14
AT
20389 case DW_FORM_strx1:
20390 case DW_FORM_strx2:
20391 case DW_FORM_strx3:
20392 case DW_FORM_strx4:
3019eac3 20393 case DW_FORM_GNU_str_index:
3019eac3 20394 {
15f18d14
AT
20395 ULONGEST str_index;
20396 if (form == DW_FORM_strx1)
20397 {
20398 str_index = read_1_byte (abfd, info_ptr);
20399 info_ptr += 1;
20400 }
20401 else if (form == DW_FORM_strx2)
20402 {
20403 str_index = read_2_bytes (abfd, info_ptr);
20404 info_ptr += 2;
20405 }
20406 else if (form == DW_FORM_strx3)
20407 {
20408 str_index = read_3_bytes (abfd, info_ptr);
20409 info_ptr += 3;
20410 }
20411 else if (form == DW_FORM_strx4)
20412 {
20413 str_index = read_4_bytes (abfd, info_ptr);
20414 info_ptr += 4;
20415 }
20416 else
20417 {
20418 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20419 info_ptr += bytes_read;
20420 }
fe56917a 20421 attr->set_unsigned_reprocess (str_index);
7a5f294d 20422 }
3019eac3 20423 break;
c906108c 20424 default:
8a3fe4f8 20425 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20426 dwarf_form_name (form),
20427 bfd_get_filename (abfd));
c906108c 20428 }
28e94949 20429
36586728 20430 /* Super hack. */
cd6c91b4 20431 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20432 attr->form = DW_FORM_GNU_ref_alt;
20433
28e94949
JB
20434 /* We have seen instances where the compiler tried to emit a byte
20435 size attribute of -1 which ended up being encoded as an unsigned
20436 0xffffffff. Although 0xffffffff is technically a valid size value,
20437 an object of this size seems pretty unlikely so we can relatively
20438 safely treat these cases as if the size attribute was invalid and
20439 treat them as zero by default. */
20440 if (attr->name == DW_AT_byte_size
20441 && form == DW_FORM_data4
529908cb 20442 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20443 {
20444 complaint
dda83cd7
SM
20445 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20446 hex_string (attr->as_unsigned ()));
414ad644 20447 attr->set_unsigned (0);
01c66ae6 20448 }
28e94949 20449
c906108c
SS
20450 return info_ptr;
20451}
20452
a8329558
KW
20453/* Read an attribute described by an abbreviated attribute. */
20454
d521ce57 20455static const gdb_byte *
dee91e82 20456read_attribute (const struct die_reader_specs *reader,
4444f407 20457 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20458 const gdb_byte *info_ptr)
a8329558
KW
20459{
20460 attr->name = abbrev->name;
c6481205 20461 attr->string_is_canonical = 0;
fe56917a 20462 attr->requires_reprocessing = 0;
43988095 20463 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20464 abbrev->implicit_const, info_ptr);
a8329558
KW
20465}
20466
43988095
JK
20467/* Return pointer to string at .debug_str offset STR_OFFSET. */
20468
20469static const char *
976ca316 20470read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20471 LONGEST str_offset)
43988095 20472{
976ca316
SM
20473 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20474 str_offset, "DW_FORM_strp");
c906108c
SS
20475}
20476
43988095
JK
20477/* Return pointer to string at .debug_str offset as read from BUF.
20478 BUF is assumed to be in a compilation unit described by CU_HEADER.
20479 Return *BYTES_READ_PTR count of bytes read from BUF. */
20480
d521ce57 20481static const char *
976ca316 20482read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20483 const gdb_byte *buf,
cf2c3c16
TT
20484 const struct comp_unit_head *cu_header,
20485 unsigned int *bytes_read_ptr)
20486{
8266302d 20487 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20488
976ca316 20489 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20490}
20491
86c0bb4c 20492/* See read.h. */
43988095 20493
86c0bb4c
TT
20494const char *
20495dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20496 const struct comp_unit_head *cu_header,
20497 unsigned int *bytes_read_ptr)
43988095 20498{
86c0bb4c 20499 bfd *abfd = objfile->obfd;
8266302d 20500 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20501
5989a64e 20502 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20503}
20504
3019eac3 20505/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20506 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20507 ADDR_SIZE is the size of addresses from the CU header. */
20508
20509static CORE_ADDR
976ca316
SM
20510read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20511 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20512{
976ca316 20513 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20514 bfd *abfd = objfile->obfd;
20515 const gdb_byte *info_ptr;
18a8505e 20516 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20517
976ca316
SM
20518 per_objfile->per_bfd->addr.read (objfile);
20519 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20520 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20521 objfile_name (objfile));
18a8505e 20522 if (addr_base_or_zero + addr_index * addr_size
976ca316 20523 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20524 error (_("DW_FORM_addr_index pointing outside of "
20525 ".debug_addr section [in module %s]"),
4262abfb 20526 objfile_name (objfile));
976ca316
SM
20527 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20528 + addr_index * addr_size);
3019eac3
DE
20529 if (addr_size == 4)
20530 return bfd_get_32 (abfd, info_ptr);
20531 else
20532 return bfd_get_64 (abfd, info_ptr);
20533}
20534
20535/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20536
20537static CORE_ADDR
20538read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20539{
5e22e966 20540 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20541 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20542}
20543
20544/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20545
20546static CORE_ADDR
d521ce57 20547read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20548 unsigned int *bytes_read)
20549{
5e22e966 20550 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20551 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20552
20553 return read_addr_index (cu, addr_index);
20554}
20555
450a1bfc 20556/* See read.h. */
3019eac3
DE
20557
20558CORE_ADDR
82ca3f51 20559dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20560 dwarf2_per_objfile *per_objfile,
82ca3f51 20561 unsigned int addr_index)
3019eac3 20562{
976ca316 20563 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20564 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20565 int addr_size;
20566
3019eac3
DE
20567 /* We need addr_base and addr_size.
20568 If we don't have PER_CU->cu, we have to get it.
20569 Nasty, but the alternative is storing the needed info in PER_CU,
20570 which at this point doesn't seem justified: it's not clear how frequently
20571 it would get used and it would increase the size of every PER_CU.
20572 Entry points like dwarf2_per_cu_addr_size do a similar thing
20573 so we're not in uncharted territory here.
20574 Alas we need to be a bit more complicated as addr_base is contained
20575 in the DIE.
20576
20577 We don't need to read the entire CU(/TU).
20578 We just need the header and top level die.
a1b64ce1 20579
3019eac3 20580 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20581 For now we skip this optimization. */
3019eac3
DE
20582
20583 if (cu != NULL)
20584 {
20585 addr_base = cu->addr_base;
20586 addr_size = cu->header.addr_size;
20587 }
20588 else
20589 {
976ca316 20590 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20591 addr_base = reader.cu->addr_base;
20592 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20593 }
20594
976ca316 20595 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20596}
20597
18a8505e
AT
20598/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20599 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20600 DWO file. */
3019eac3 20601
d521ce57 20602static const char *
18a8505e
AT
20603read_str_index (struct dwarf2_cu *cu,
20604 struct dwarf2_section_info *str_section,
20605 struct dwarf2_section_info *str_offsets_section,
20606 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20607{
976ca316
SM
20608 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20609 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20610 const char *objf_name = objfile_name (objfile);
3019eac3 20611 bfd *abfd = objfile->obfd;
d521ce57 20612 const gdb_byte *info_ptr;
3019eac3 20613 ULONGEST str_offset;
cf532bd1 20614 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20615
96b79293
TT
20616 str_section->read (objfile);
20617 str_offsets_section->read (objfile);
73869dc2 20618 if (str_section->buffer == NULL)
18a8505e 20619 error (_("%s used without %s section"
9d8780f0 20620 " in CU at offset %s [in module %s]"),
96b79293 20621 form_name, str_section->get_name (),
dda83cd7 20622 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20623 if (str_offsets_section->buffer == NULL)
18a8505e 20624 error (_("%s used without %s section"
9d8780f0 20625 " in CU at offset %s [in module %s]"),
96b79293 20626 form_name, str_section->get_name (),
dda83cd7 20627 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20628 info_ptr = (str_offsets_section->buffer
18a8505e 20629 + str_offsets_base
3019eac3
DE
20630 + str_index * cu->header.offset_size);
20631 if (cu->header.offset_size == 4)
20632 str_offset = bfd_get_32 (abfd, info_ptr);
20633 else
20634 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20635 if (str_offset >= str_section->size)
57d63ce2 20636 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20637 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20638 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20639 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20640}
20641
18a8505e
AT
20642/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20643
20644static const char *
20645read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20646{
20647 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20648 ? reader->cu->header.addr_size : 0;
20649 return read_str_index (reader->cu,
20650 &reader->dwo_file->sections.str,
20651 &reader->dwo_file->sections.str_offsets,
20652 str_offsets_base, str_index);
20653}
20654
20655/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20656
20657static const char *
20658read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20659{
5e22e966 20660 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20661 const char *objf_name = objfile_name (objfile);
20662 static const char form_name[] = "DW_FORM_GNU_str_index";
20663 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20664
20665 if (!cu->str_offsets_base.has_value ())
20666 error (_("%s used in Fission stub without %s"
20667 " in CU at offset 0x%lx [in module %s]"),
20668 form_name, str_offsets_attr_name,
20669 (long) cu->header.offset_size, objf_name);
20670
20671 return read_str_index (cu,
5e22e966
SM
20672 &cu->per_objfile->per_bfd->str,
20673 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20674 *cu->str_offsets_base, str_index);
20675}
20676
3019eac3
DE
20677/* Return the length of an LEB128 number in BUF. */
20678
20679static int
20680leb128_size (const gdb_byte *buf)
20681{
20682 const gdb_byte *begin = buf;
20683 gdb_byte byte;
20684
20685 while (1)
20686 {
20687 byte = *buf++;
20688 if ((byte & 128) == 0)
20689 return buf - begin;
20690 }
20691}
20692
c906108c 20693static void
e142c38c 20694set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20695{
20696 switch (lang)
20697 {
20698 case DW_LANG_C89:
76bee0cc 20699 case DW_LANG_C99:
0cfd832f 20700 case DW_LANG_C11:
c906108c 20701 case DW_LANG_C:
d1be3247 20702 case DW_LANG_UPC:
e142c38c 20703 cu->language = language_c;
c906108c 20704 break;
9c37b5ae 20705 case DW_LANG_Java:
c906108c 20706 case DW_LANG_C_plus_plus:
0cfd832f
MW
20707 case DW_LANG_C_plus_plus_11:
20708 case DW_LANG_C_plus_plus_14:
e142c38c 20709 cu->language = language_cplus;
c906108c 20710 break;
6aecb9c2
JB
20711 case DW_LANG_D:
20712 cu->language = language_d;
20713 break;
c906108c
SS
20714 case DW_LANG_Fortran77:
20715 case DW_LANG_Fortran90:
b21b22e0 20716 case DW_LANG_Fortran95:
f7de9aab
MW
20717 case DW_LANG_Fortran03:
20718 case DW_LANG_Fortran08:
e142c38c 20719 cu->language = language_fortran;
c906108c 20720 break;
a766d390
DE
20721 case DW_LANG_Go:
20722 cu->language = language_go;
20723 break;
c906108c 20724 case DW_LANG_Mips_Assembler:
e142c38c 20725 cu->language = language_asm;
c906108c
SS
20726 break;
20727 case DW_LANG_Ada83:
8aaf0b47 20728 case DW_LANG_Ada95:
bc5f45f8
JB
20729 cu->language = language_ada;
20730 break;
72019c9c
GM
20731 case DW_LANG_Modula2:
20732 cu->language = language_m2;
20733 break;
fe8e67fd
PM
20734 case DW_LANG_Pascal83:
20735 cu->language = language_pascal;
20736 break;
22566fbd
DJ
20737 case DW_LANG_ObjC:
20738 cu->language = language_objc;
20739 break;
c44af4eb
TT
20740 case DW_LANG_Rust:
20741 case DW_LANG_Rust_old:
20742 cu->language = language_rust;
20743 break;
c906108c
SS
20744 case DW_LANG_Cobol74:
20745 case DW_LANG_Cobol85:
c906108c 20746 default:
e142c38c 20747 cu->language = language_minimal;
c906108c
SS
20748 break;
20749 }
e142c38c 20750 cu->language_defn = language_def (cu->language);
c906108c
SS
20751}
20752
20753/* Return the named attribute or NULL if not there. */
20754
20755static struct attribute *
e142c38c 20756dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20757{
a48e046c 20758 for (;;)
c906108c 20759 {
a48e046c
TT
20760 unsigned int i;
20761 struct attribute *spec = NULL;
20762
20763 for (i = 0; i < die->num_attrs; ++i)
20764 {
20765 if (die->attrs[i].name == name)
20766 return &die->attrs[i];
20767 if (die->attrs[i].name == DW_AT_specification
20768 || die->attrs[i].name == DW_AT_abstract_origin)
20769 spec = &die->attrs[i];
20770 }
20771
20772 if (!spec)
20773 break;
c906108c 20774
f2f0e013 20775 die = follow_die_ref (die, spec, &cu);
f2f0e013 20776 }
c5aa993b 20777
c906108c
SS
20778 return NULL;
20779}
20780
7d45c7c3
KB
20781/* Return the string associated with a string-typed attribute, or NULL if it
20782 is either not found or is of an incorrect type. */
20783
20784static const char *
20785dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20786{
20787 struct attribute *attr;
20788 const char *str = NULL;
20789
20790 attr = dwarf2_attr (die, name, cu);
20791
20792 if (attr != NULL)
20793 {
95f982e5 20794 str = attr->as_string ();
e61108c9 20795 if (str == nullptr)
dda83cd7 20796 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20797 "DIE at %s in module %s"),
20798 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20799 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20800 }
20801
20802 return str;
20803}
20804
a084a2a6 20805/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20806 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20807static const char *
20808dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20809{
20810 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20811 if (dwo_name == nullptr)
20812 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20813 return dwo_name;
20814}
20815
05cf31d1
JB
20816/* Return non-zero iff the attribute NAME is defined for the given DIE,
20817 and holds a non-zero value. This function should only be used for
2dc7f7b3 20818 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20819
20820static int
20821dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20822{
20823 struct attribute *attr = dwarf2_attr (die, name, cu);
20824
c45bc3f8 20825 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20826}
20827
3ca72b44 20828static int
e142c38c 20829die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20830{
05cf31d1
JB
20831 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20832 which value is non-zero. However, we have to be careful with
20833 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20834 (via dwarf2_flag_true_p) follows this attribute. So we may
20835 end up accidently finding a declaration attribute that belongs
20836 to a different DIE referenced by the specification attribute,
20837 even though the given DIE does not have a declaration attribute. */
20838 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20839 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20840}
20841
63d06c5c 20842/* Return the die giving the specification for DIE, if there is
f2f0e013 20843 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20844 containing the return value on output. If there is no
20845 specification, but there is an abstract origin, that is
20846 returned. */
63d06c5c
DC
20847
20848static struct die_info *
f2f0e013 20849die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20850{
f2f0e013
DJ
20851 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20852 *spec_cu);
63d06c5c 20853
edb3359d
DJ
20854 if (spec_attr == NULL)
20855 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20856
63d06c5c
DC
20857 if (spec_attr == NULL)
20858 return NULL;
20859 else
f2f0e013 20860 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20861}
c906108c 20862
527f3840
JK
20863/* Stub for free_line_header to match void * callback types. */
20864
20865static void
20866free_line_header_voidp (void *arg)
20867{
9a3c8263 20868 struct line_header *lh = (struct line_header *) arg;
527f3840 20869
fff8551c 20870 delete lh;
527f3840
JK
20871}
20872
83769d0b 20873/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20874
20875static struct dwarf2_section_info *
20876get_debug_line_section (struct dwarf2_cu *cu)
20877{
20878 struct dwarf2_section_info *section;
976ca316 20879 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20880
20881 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20882 DWO file. */
20883 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20884 section = &cu->dwo_unit->dwo_file->sections.line;
20885 else if (cu->per_cu->is_dwz)
20886 {
a7308ce0 20887 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
20888
20889 section = &dwz->line;
20890 }
20891 else
976ca316 20892 section = &per_objfile->per_bfd->line;
36586728
TT
20893
20894 return section;
20895}
20896
debd256d 20897/* Read the statement program header starting at OFFSET in
3019eac3 20898 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20899 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20900 Returns NULL if there is a problem reading the header, e.g., if it
20901 has a version we don't understand.
debd256d
JB
20902
20903 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20904 the returned object point into the dwarf line section buffer,
20905 and must not be freed. */
ae2de4f8 20906
fff8551c 20907static line_header_up
9c541725 20908dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20909{
3019eac3 20910 struct dwarf2_section_info *section;
976ca316 20911 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20912
36586728 20913 section = get_debug_line_section (cu);
976ca316 20914 section->read (per_objfile->objfile);
3019eac3 20915 if (section->buffer == NULL)
debd256d 20916 {
3019eac3 20917 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20918 complaint (_("missing .debug_line.dwo section"));
3019eac3 20919 else
b98664d3 20920 complaint (_("missing .debug_line section"));
debd256d
JB
20921 return 0;
20922 }
20923
0df7ad3a 20924 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20925 per_objfile, section, &cu->header);
debd256d 20926}
c906108c 20927
c6da4cef 20928/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20929 Return the file name of the psymtab for the given file_entry.
c6da4cef 20930 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20931 If space for the result is malloc'd, *NAME_HOLDER will be set.
20932 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20933
d521ce57 20934static const char *
7ba99d21 20935psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20936 const dwarf2_psymtab *pst,
c89b44cd
TT
20937 const char *comp_dir,
20938 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20939{
d521ce57
TT
20940 const char *include_name = fe.name;
20941 const char *include_name_to_compare = include_name;
72b9f47f 20942 const char *pst_filename;
c6da4cef
DE
20943 int file_is_pst;
20944
8c43009f 20945 const char *dir_name = fe.include_dir (lh);
c6da4cef 20946
c89b44cd 20947 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20948 if (!IS_ABSOLUTE_PATH (include_name)
20949 && (dir_name != NULL || comp_dir != NULL))
20950 {
20951 /* Avoid creating a duplicate psymtab for PST.
20952 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20953 Before we do the comparison, however, we need to account
20954 for DIR_NAME and COMP_DIR.
20955 First prepend dir_name (if non-NULL). If we still don't
20956 have an absolute path prepend comp_dir (if non-NULL).
20957 However, the directory we record in the include-file's
20958 psymtab does not contain COMP_DIR (to match the
20959 corresponding symtab(s)).
20960
20961 Example:
20962
20963 bash$ cd /tmp
20964 bash$ gcc -g ./hello.c
20965 include_name = "hello.c"
20966 dir_name = "."
20967 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20968 DW_AT_name = "./hello.c"
20969
20970 */
c6da4cef
DE
20971
20972 if (dir_name != NULL)
20973 {
c89b44cd
TT
20974 name_holder->reset (concat (dir_name, SLASH_STRING,
20975 include_name, (char *) NULL));
20976 include_name = name_holder->get ();
c6da4cef 20977 include_name_to_compare = include_name;
c6da4cef
DE
20978 }
20979 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20980 {
c89b44cd
TT
20981 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20982 include_name, (char *) NULL));
20983 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20984 }
20985 }
20986
20987 pst_filename = pst->filename;
c89b44cd 20988 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20989 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20990 {
c89b44cd
TT
20991 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20992 pst_filename, (char *) NULL));
20993 pst_filename = copied_name.get ();
c6da4cef
DE
20994 }
20995
1e3fad37 20996 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20997
c6da4cef
DE
20998 if (file_is_pst)
20999 return NULL;
21000 return include_name;
21001}
21002
d9b3de22
DE
21003/* State machine to track the state of the line number program. */
21004
6f77053d 21005class lnp_state_machine
d9b3de22 21006{
6f77053d
PA
21007public:
21008 /* Initialize a machine state for the start of a line number
21009 program. */
804d2729
TT
21010 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21011 bool record_lines_p);
6f77053d 21012
8c43009f
PA
21013 file_entry *current_file ()
21014 {
21015 /* lh->file_names is 0-based, but the file name numbers in the
21016 statement program are 1-based. */
6f77053d
PA
21017 return m_line_header->file_name_at (m_file);
21018 }
21019
21020 /* Record the line in the state machine. END_SEQUENCE is true if
21021 we're processing the end of a sequence. */
21022 void record_line (bool end_sequence);
21023
a8caed5d 21024 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21025 nop-out rest of the lines in this sequence. */
6f77053d
PA
21026 void check_line_address (struct dwarf2_cu *cu,
21027 const gdb_byte *line_ptr,
7ab6656f 21028 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21029
21030 void handle_set_discriminator (unsigned int discriminator)
21031 {
21032 m_discriminator = discriminator;
21033 m_line_has_non_zero_discriminator |= discriminator != 0;
21034 }
21035
21036 /* Handle DW_LNE_set_address. */
21037 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21038 {
21039 m_op_index = 0;
21040 address += baseaddr;
21041 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21042 }
21043
21044 /* Handle DW_LNS_advance_pc. */
21045 void handle_advance_pc (CORE_ADDR adjust);
21046
21047 /* Handle a special opcode. */
21048 void handle_special_opcode (unsigned char op_code);
21049
21050 /* Handle DW_LNS_advance_line. */
21051 void handle_advance_line (int line_delta)
21052 {
21053 advance_line (line_delta);
21054 }
21055
21056 /* Handle DW_LNS_set_file. */
21057 void handle_set_file (file_name_index file);
21058
21059 /* Handle DW_LNS_negate_stmt. */
21060 void handle_negate_stmt ()
21061 {
21062 m_is_stmt = !m_is_stmt;
21063 }
21064
21065 /* Handle DW_LNS_const_add_pc. */
21066 void handle_const_add_pc ();
21067
21068 /* Handle DW_LNS_fixed_advance_pc. */
21069 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21070 {
21071 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21072 m_op_index = 0;
21073 }
21074
21075 /* Handle DW_LNS_copy. */
21076 void handle_copy ()
21077 {
21078 record_line (false);
21079 m_discriminator = 0;
21080 }
21081
21082 /* Handle DW_LNE_end_sequence. */
21083 void handle_end_sequence ()
21084 {
804d2729 21085 m_currently_recording_lines = true;
6f77053d
PA
21086 }
21087
21088private:
21089 /* Advance the line by LINE_DELTA. */
21090 void advance_line (int line_delta)
21091 {
21092 m_line += line_delta;
21093
21094 if (line_delta != 0)
21095 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21096 }
21097
804d2729
TT
21098 struct dwarf2_cu *m_cu;
21099
6f77053d
PA
21100 gdbarch *m_gdbarch;
21101
21102 /* True if we're recording lines.
21103 Otherwise we're building partial symtabs and are just interested in
21104 finding include files mentioned by the line number program. */
21105 bool m_record_lines_p;
21106
8c43009f 21107 /* The line number header. */
6f77053d 21108 line_header *m_line_header;
8c43009f 21109
6f77053d
PA
21110 /* These are part of the standard DWARF line number state machine,
21111 and initialized according to the DWARF spec. */
d9b3de22 21112
6f77053d 21113 unsigned char m_op_index = 0;
7ba99d21
AT
21114 /* The line table index of the current file. */
21115 file_name_index m_file = 1;
6f77053d
PA
21116 unsigned int m_line = 1;
21117
21118 /* These are initialized in the constructor. */
21119
21120 CORE_ADDR m_address;
21121 bool m_is_stmt;
21122 unsigned int m_discriminator;
d9b3de22
DE
21123
21124 /* Additional bits of state we need to track. */
21125
21126 /* The last file that we called dwarf2_start_subfile for.
21127 This is only used for TLLs. */
6f77053d 21128 unsigned int m_last_file = 0;
d9b3de22 21129 /* The last file a line number was recorded for. */
6f77053d 21130 struct subfile *m_last_subfile = NULL;
d9b3de22 21131
1313c56e
AB
21132 /* The address of the last line entry. */
21133 CORE_ADDR m_last_address;
21134
21135 /* Set to true when a previous line at the same address (using
21136 m_last_address) had m_is_stmt true. This is reset to false when a
21137 line entry at a new address (m_address different to m_last_address) is
21138 processed. */
21139 bool m_stmt_at_address = false;
21140
804d2729
TT
21141 /* When true, record the lines we decode. */
21142 bool m_currently_recording_lines = false;
d9b3de22
DE
21143
21144 /* The last line number that was recorded, used to coalesce
21145 consecutive entries for the same line. This can happen, for
21146 example, when discriminators are present. PR 17276. */
6f77053d
PA
21147 unsigned int m_last_line = 0;
21148 bool m_line_has_non_zero_discriminator = false;
8c43009f 21149};
d9b3de22 21150
6f77053d
PA
21151void
21152lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21153{
21154 CORE_ADDR addr_adj = (((m_op_index + adjust)
21155 / m_line_header->maximum_ops_per_instruction)
21156 * m_line_header->minimum_instruction_length);
21157 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21158 m_op_index = ((m_op_index + adjust)
21159 % m_line_header->maximum_ops_per_instruction);
21160}
d9b3de22 21161
6f77053d
PA
21162void
21163lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21164{
6f77053d 21165 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21166 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21167 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21168 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21169 / m_line_header->maximum_ops_per_instruction)
21170 * m_line_header->minimum_instruction_length);
21171 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21172 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21173 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21174
258bf0ee 21175 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21176 advance_line (line_delta);
21177 record_line (false);
21178 m_discriminator = 0;
21179}
d9b3de22 21180
6f77053d
PA
21181void
21182lnp_state_machine::handle_set_file (file_name_index file)
21183{
21184 m_file = file;
21185
21186 const file_entry *fe = current_file ();
21187 if (fe == NULL)
21188 dwarf2_debug_line_missing_file_complaint ();
21189 else if (m_record_lines_p)
21190 {
21191 const char *dir = fe->include_dir (m_line_header);
21192
c24bdb02 21193 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21194 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21195 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21196 }
21197}
21198
21199void
21200lnp_state_machine::handle_const_add_pc ()
21201{
21202 CORE_ADDR adjust
21203 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21204
21205 CORE_ADDR addr_adj
21206 = (((m_op_index + adjust)
21207 / m_line_header->maximum_ops_per_instruction)
21208 * m_line_header->minimum_instruction_length);
21209
21210 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21211 m_op_index = ((m_op_index + adjust)
21212 % m_line_header->maximum_ops_per_instruction);
21213}
d9b3de22 21214
a05a36a5
DE
21215/* Return non-zero if we should add LINE to the line number table.
21216 LINE is the line to add, LAST_LINE is the last line that was added,
21217 LAST_SUBFILE is the subfile for LAST_LINE.
21218 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21219 had a non-zero discriminator.
21220
21221 We have to be careful in the presence of discriminators.
21222 E.g., for this line:
21223
21224 for (i = 0; i < 100000; i++);
21225
21226 clang can emit four line number entries for that one line,
21227 each with a different discriminator.
21228 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21229
21230 However, we want gdb to coalesce all four entries into one.
21231 Otherwise the user could stepi into the middle of the line and
21232 gdb would get confused about whether the pc really was in the
21233 middle of the line.
21234
21235 Things are further complicated by the fact that two consecutive
21236 line number entries for the same line is a heuristic used by gcc
21237 to denote the end of the prologue. So we can't just discard duplicate
21238 entries, we have to be selective about it. The heuristic we use is
21239 that we only collapse consecutive entries for the same line if at least
21240 one of those entries has a non-zero discriminator. PR 17276.
21241
21242 Note: Addresses in the line number state machine can never go backwards
21243 within one sequence, thus this coalescing is ok. */
21244
21245static int
804d2729
TT
21246dwarf_record_line_p (struct dwarf2_cu *cu,
21247 unsigned int line, unsigned int last_line,
a05a36a5
DE
21248 int line_has_non_zero_discriminator,
21249 struct subfile *last_subfile)
21250{
c24bdb02 21251 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21252 return 1;
21253 if (line != last_line)
21254 return 1;
21255 /* Same line for the same file that we've seen already.
21256 As a last check, for pr 17276, only record the line if the line
21257 has never had a non-zero discriminator. */
21258 if (!line_has_non_zero_discriminator)
21259 return 1;
21260 return 0;
21261}
21262
804d2729
TT
21263/* Use the CU's builder to record line number LINE beginning at
21264 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21265
21266static void
d9b3de22 21267dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21268 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21269 struct dwarf2_cu *cu)
252a6764
DE
21270{
21271 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21272
27e0867f
DE
21273 if (dwarf_line_debug)
21274 {
21275 fprintf_unfiltered (gdb_stdlog,
21276 "Recording line %u, file %s, address %s\n",
21277 line, lbasename (subfile->name),
21278 paddress (gdbarch, address));
21279 }
21280
804d2729 21281 if (cu != nullptr)
8c95582d 21282 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21283}
21284
21285/* Subroutine of dwarf_decode_lines_1 to simplify it.
21286 Mark the end of a set of line number records.
d9b3de22 21287 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21288 If SUBFILE is NULL the request is ignored. */
21289
21290static void
21291dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21292 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21293{
27e0867f
DE
21294 if (subfile == NULL)
21295 return;
21296
21297 if (dwarf_line_debug)
21298 {
21299 fprintf_unfiltered (gdb_stdlog,
21300 "Finishing current line, file %s, address %s\n",
21301 lbasename (subfile->name),
21302 paddress (gdbarch, address));
21303 }
21304
8c95582d 21305 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21306}
21307
6f77053d
PA
21308void
21309lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21310{
d9b3de22
DE
21311 if (dwarf_line_debug)
21312 {
21313 fprintf_unfiltered (gdb_stdlog,
21314 "Processing actual line %u: file %u,"
94a72be7 21315 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21316 m_line, m_file,
6f77053d 21317 paddress (m_gdbarch, m_address),
94a72be7
AB
21318 m_is_stmt, m_discriminator,
21319 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21320 }
21321
6f77053d 21322 file_entry *fe = current_file ();
8c43009f
PA
21323
21324 if (fe == NULL)
d9b3de22
DE
21325 dwarf2_debug_line_missing_file_complaint ();
21326 /* For now we ignore lines not starting on an instruction boundary.
21327 But not when processing end_sequence for compatibility with the
21328 previous version of the code. */
6f77053d 21329 else if (m_op_index == 0 || end_sequence)
d9b3de22 21330 {
8c43009f 21331 fe->included_p = 1;
8c95582d 21332 if (m_record_lines_p)
d9b3de22 21333 {
1313c56e
AB
21334 /* When we switch files we insert an end maker in the first file,
21335 switch to the second file and add a new line entry. The
21336 problem is that the end marker inserted in the first file will
21337 discard any previous line entries at the same address. If the
21338 line entries in the first file are marked as is-stmt, while
21339 the new line in the second file is non-stmt, then this means
21340 the end marker will discard is-stmt lines so we can have a
21341 non-stmt line. This means that there are less addresses at
21342 which the user can insert a breakpoint.
21343
21344 To improve this we track the last address in m_last_address,
21345 and whether we have seen an is-stmt at this address. Then
21346 when switching files, if we have seen a stmt at the current
21347 address, and we are switching to create a non-stmt line, then
21348 discard the new line. */
21349 bool file_changed
21350 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21351 bool ignore_this_line
dda83cd7
SM
21352 = ((file_changed && !end_sequence && m_last_address == m_address
21353 && !m_is_stmt && m_stmt_at_address)
21354 || (!end_sequence && m_line == 0));
1313c56e
AB
21355
21356 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21357 {
804d2729
TT
21358 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21359 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21360 }
21361
1313c56e 21362 if (!end_sequence && !ignore_this_line)
d9b3de22 21363 {
8c95582d
AB
21364 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21365
804d2729 21366 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21367 m_line_has_non_zero_discriminator,
21368 m_last_subfile))
d9b3de22 21369 {
c24bdb02 21370 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21371 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21372 builder->get_current_subfile (),
8c95582d 21373 m_line, m_address, is_stmt,
804d2729 21374 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21375 }
c24bdb02 21376 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21377 m_last_line = m_line;
d9b3de22
DE
21378 }
21379 }
21380 }
1313c56e
AB
21381
21382 /* Track whether we have seen any m_is_stmt true at m_address in case we
21383 have multiple line table entries all at m_address. */
21384 if (m_last_address != m_address)
21385 {
21386 m_stmt_at_address = false;
21387 m_last_address = m_address;
21388 }
21389 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21390}
21391
804d2729
TT
21392lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21393 line_header *lh, bool record_lines_p)
d9b3de22 21394{
804d2729 21395 m_cu = cu;
6f77053d
PA
21396 m_gdbarch = arch;
21397 m_record_lines_p = record_lines_p;
21398 m_line_header = lh;
d9b3de22 21399
804d2729 21400 m_currently_recording_lines = true;
d9b3de22 21401
d9b3de22
DE
21402 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21403 was a line entry for it so that the backend has a chance to adjust it
21404 and also record it in case it needs it. This is currently used by MIPS
21405 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21406 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21407 m_is_stmt = lh->default_is_stmt;
21408 m_discriminator = 0;
1313c56e
AB
21409
21410 m_last_address = m_address;
21411 m_stmt_at_address = false;
252a6764
DE
21412}
21413
6f77053d
PA
21414void
21415lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21416 const gdb_byte *line_ptr,
7ab6656f 21417 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21418{
a8caed5d
FS
21419 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21420 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21421 located at 0x0. In this case, additionally check that if
21422 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21423
a8caed5d
FS
21424 if ((address == 0 && address < unrelocated_lowpc)
21425 || address == (CORE_ADDR) -1)
924c2928
DE
21426 {
21427 /* This line table is for a function which has been
21428 GCd by the linker. Ignore it. PR gdb/12528 */
21429
5e22e966 21430 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21431 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21432
b98664d3 21433 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21434 line_offset, objfile_name (objfile));
804d2729
TT
21435 m_currently_recording_lines = false;
21436 /* Note: m_currently_recording_lines is left as false until we see
21437 DW_LNE_end_sequence. */
924c2928
DE
21438 }
21439}
21440
f3f5162e 21441/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21442 Process the line number information in LH.
21443 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21444 program in order to set included_p for every referenced header. */
debd256d 21445
c906108c 21446static void
43f3e411
DE
21447dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21448 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21449{
d521ce57
TT
21450 const gdb_byte *line_ptr, *extended_end;
21451 const gdb_byte *line_end;
a8c50c1f 21452 unsigned int bytes_read, extended_len;
699ca60a 21453 unsigned char op_code, extended_op;
e142c38c 21454 CORE_ADDR baseaddr;
5e22e966 21455 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21456 bfd *abfd = objfile->obfd;
08feed99 21457 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21458 /* True if we're recording line info (as opposed to building partial
21459 symtabs and just interested in finding include files mentioned by
21460 the line number program). */
21461 bool record_lines_p = !decode_for_pst_p;
e142c38c 21462
b3b3bada 21463 baseaddr = objfile->text_section_offset ();
c906108c 21464
debd256d
JB
21465 line_ptr = lh->statement_program_start;
21466 line_end = lh->statement_program_end;
c906108c
SS
21467
21468 /* Read the statement sequences until there's nothing left. */
21469 while (line_ptr < line_end)
21470 {
6f77053d
PA
21471 /* The DWARF line number program state machine. Reset the state
21472 machine at the start of each sequence. */
804d2729 21473 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21474 bool end_sequence = false;
d9b3de22 21475
8c43009f 21476 if (record_lines_p)
c906108c 21477 {
8c43009f
PA
21478 /* Start a subfile for the current file of the state
21479 machine. */
21480 const file_entry *fe = state_machine.current_file ();
21481
21482 if (fe != NULL)
804d2729 21483 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21484 }
21485
a738430d 21486 /* Decode the table. */
d9b3de22 21487 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21488 {
21489 op_code = read_1_byte (abfd, line_ptr);
21490 line_ptr += 1;
9aa1fe7e 21491
debd256d 21492 if (op_code >= lh->opcode_base)
6e70227d 21493 {
8e07a239 21494 /* Special opcode. */
6f77053d 21495 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21496 }
21497 else switch (op_code)
c906108c
SS
21498 {
21499 case DW_LNS_extended_op:
3e43a32a
MS
21500 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21501 &bytes_read);
473b7be6 21502 line_ptr += bytes_read;
a8c50c1f 21503 extended_end = line_ptr + extended_len;
c906108c
SS
21504 extended_op = read_1_byte (abfd, line_ptr);
21505 line_ptr += 1;
8f34b746
TV
21506 if (DW_LNE_lo_user <= extended_op
21507 && extended_op <= DW_LNE_hi_user)
21508 {
21509 /* Vendor extension, ignore. */
21510 line_ptr = extended_end;
21511 break;
21512 }
c906108c
SS
21513 switch (extended_op)
21514 {
21515 case DW_LNE_end_sequence:
6f77053d
PA
21516 state_machine.handle_end_sequence ();
21517 end_sequence = true;
c906108c
SS
21518 break;
21519 case DW_LNE_set_address:
d9b3de22
DE
21520 {
21521 CORE_ADDR address
c8a7a66f 21522 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21523 line_ptr += bytes_read;
6f77053d
PA
21524
21525 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21526 lowpc - baseaddr, address);
6f77053d 21527 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21528 }
c906108c
SS
21529 break;
21530 case DW_LNE_define_file:
dda83cd7
SM
21531 {
21532 const char *cur_file;
ecfb656c
PA
21533 unsigned int mod_time, length;
21534 dir_index dindex;
6e70227d 21535
dda83cd7 21536 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21537 &bytes_read);
dda83cd7
SM
21538 line_ptr += bytes_read;
21539 dindex = (dir_index)
21540 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21541 line_ptr += bytes_read;
21542 mod_time =
21543 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21544 line_ptr += bytes_read;
21545 length =
21546 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21547 line_ptr += bytes_read;
21548 lh->add_file_name (cur_file, dindex, mod_time, length);
21549 }
c906108c 21550 break;
d0c6ba3d 21551 case DW_LNE_set_discriminator:
6f77053d
PA
21552 {
21553 /* The discriminator is not interesting to the
21554 debugger; just ignore it. We still need to
21555 check its value though:
21556 if there are consecutive entries for the same
21557 (non-prologue) line we want to coalesce them.
21558 PR 17276. */
21559 unsigned int discr
21560 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21561 line_ptr += bytes_read;
21562
21563 state_machine.handle_set_discriminator (discr);
21564 }
d0c6ba3d 21565 break;
c906108c 21566 default:
b98664d3 21567 complaint (_("mangled .debug_line section"));
debd256d 21568 return;
c906108c 21569 }
a8c50c1f
DJ
21570 /* Make sure that we parsed the extended op correctly. If e.g.
21571 we expected a different address size than the producer used,
21572 we may have read the wrong number of bytes. */
21573 if (line_ptr != extended_end)
21574 {
b98664d3 21575 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21576 return;
21577 }
c906108c
SS
21578 break;
21579 case DW_LNS_copy:
6f77053d 21580 state_machine.handle_copy ();
c906108c
SS
21581 break;
21582 case DW_LNS_advance_pc:
2dc7f7b3
TT
21583 {
21584 CORE_ADDR adjust
21585 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21586 line_ptr += bytes_read;
6f77053d
PA
21587
21588 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21589 }
c906108c
SS
21590 break;
21591 case DW_LNS_advance_line:
a05a36a5
DE
21592 {
21593 int line_delta
21594 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21595 line_ptr += bytes_read;
6f77053d
PA
21596
21597 state_machine.handle_advance_line (line_delta);
a05a36a5 21598 }
c906108c
SS
21599 break;
21600 case DW_LNS_set_file:
d9b3de22 21601 {
6f77053d 21602 file_name_index file
ecfb656c
PA
21603 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21604 &bytes_read);
d9b3de22 21605 line_ptr += bytes_read;
8c43009f 21606
6f77053d 21607 state_machine.handle_set_file (file);
d9b3de22 21608 }
c906108c
SS
21609 break;
21610 case DW_LNS_set_column:
0ad93d4f 21611 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21612 line_ptr += bytes_read;
21613 break;
21614 case DW_LNS_negate_stmt:
6f77053d 21615 state_machine.handle_negate_stmt ();
c906108c
SS
21616 break;
21617 case DW_LNS_set_basic_block:
c906108c 21618 break;
c2c6d25f
JM
21619 /* Add to the address register of the state machine the
21620 address increment value corresponding to special opcode
a738430d
MK
21621 255. I.e., this value is scaled by the minimum
21622 instruction length since special opcode 255 would have
b021a221 21623 scaled the increment. */
c906108c 21624 case DW_LNS_const_add_pc:
6f77053d 21625 state_machine.handle_const_add_pc ();
c906108c
SS
21626 break;
21627 case DW_LNS_fixed_advance_pc:
3e29f34a 21628 {
6f77053d 21629 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21630 line_ptr += 2;
6f77053d
PA
21631
21632 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21633 }
c906108c 21634 break;
9aa1fe7e 21635 default:
a738430d
MK
21636 {
21637 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21638 int i;
a738430d 21639
debd256d 21640 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21641 {
21642 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21643 line_ptr += bytes_read;
21644 }
21645 }
c906108c
SS
21646 }
21647 }
d9b3de22
DE
21648
21649 if (!end_sequence)
21650 dwarf2_debug_line_missing_end_sequence_complaint ();
21651
21652 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21653 in which case we still finish recording the last line). */
6f77053d 21654 state_machine.record_line (true);
c906108c 21655 }
f3f5162e
DE
21656}
21657
21658/* Decode the Line Number Program (LNP) for the given line_header
21659 structure and CU. The actual information extracted and the type
21660 of structures created from the LNP depends on the value of PST.
21661
21662 1. If PST is NULL, then this procedure uses the data from the program
21663 to create all necessary symbol tables, and their linetables.
21664
21665 2. If PST is not NULL, this procedure reads the program to determine
21666 the list of files included by the unit represented by PST, and
21667 builds all the associated partial symbol tables.
21668
21669 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21670 It is used for relative paths in the line table.
21671 NOTE: When processing partial symtabs (pst != NULL),
21672 comp_dir == pst->dirname.
21673
21674 NOTE: It is important that psymtabs have the same file name (via strcmp)
21675 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21676 symtab we don't use it in the name of the psymtabs we create.
21677 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21678 A good testcase for this is mb-inline.exp.
21679
527f3840
JK
21680 LOWPC is the lowest address in CU (or 0 if not known).
21681
21682 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21683 for its PC<->lines mapping information. Otherwise only the filename
21684 table is read in. */
f3f5162e
DE
21685
21686static void
21687dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21688 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21689 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21690{
5e22e966 21691 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21692 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21693
527f3840
JK
21694 if (decode_mapping)
21695 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21696
21697 if (decode_for_pst_p)
21698 {
aaa75496 21699 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21700 create the psymtab of each included file. */
7ba99d21 21701 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21702 if (file_entry.included_p == 1)
21703 {
c89b44cd 21704 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21705 const char *include_name =
7ba99d21
AT
21706 psymtab_include_file_name (lh, file_entry, pst,
21707 comp_dir, &name_holder);
c6da4cef 21708 if (include_name != NULL)
7b249e47
TT
21709 dwarf2_create_include_psymtab
21710 (cu->per_objfile->per_bfd, include_name, pst,
21711 cu->per_objfile->per_bfd->partial_symtabs.get (),
0072c873 21712 objfile->per_bfd);
dda83cd7 21713 }
aaa75496 21714 }
cb1df416
DJ
21715 else
21716 {
21717 /* Make sure a symtab is created for every file, even files
21718 which contain only variables (i.e. no code with associated
21719 line numbers). */
c24bdb02
KS
21720 buildsym_compunit *builder = cu->get_builder ();
21721 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21722
7ba99d21 21723 for (auto &fe : lh->file_names ())
cb1df416 21724 {
804d2729 21725 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21726 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21727 {
c24bdb02 21728 builder->get_current_subfile ()->symtab
804d2729 21729 = allocate_symtab (cust,
c24bdb02 21730 builder->get_current_subfile ()->name);
43f3e411 21731 }
c24bdb02 21732 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21733 }
21734 }
c906108c
SS
21735}
21736
21737/* Start a subfile for DWARF. FILENAME is the name of the file and
21738 DIRNAME the name of the source directory which contains FILENAME
4d663531 21739 or NULL if not known.
c906108c
SS
21740 This routine tries to keep line numbers from identical absolute and
21741 relative file names in a common subfile.
21742
21743 Using the `list' example from the GDB testsuite, which resides in
21744 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21745 of /srcdir/list0.c yields the following debugging information for list0.c:
21746
c5aa993b 21747 DW_AT_name: /srcdir/list0.c
4d663531 21748 DW_AT_comp_dir: /compdir
357e46e7 21749 files.files[0].name: list0.h
c5aa993b 21750 files.files[0].dir: /srcdir
357e46e7 21751 files.files[1].name: list0.c
c5aa993b 21752 files.files[1].dir: /srcdir
c906108c
SS
21753
21754 The line number information for list0.c has to end up in a single
4f1520fb
FR
21755 subfile, so that `break /srcdir/list0.c:1' works as expected.
21756 start_subfile will ensure that this happens provided that we pass the
21757 concatenation of files.files[1].dir and files.files[1].name as the
21758 subfile's name. */
c906108c
SS
21759
21760static void
804d2729
TT
21761dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21762 const char *dirname)
c906108c 21763{
43816ebc 21764 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21765
4d663531 21766 /* In order not to lose the line information directory,
4f1520fb
FR
21767 we concatenate it to the filename when it makes sense.
21768 Note that the Dwarf3 standard says (speaking of filenames in line
21769 information): ``The directory index is ignored for file names
21770 that represent full path names''. Thus ignoring dirname in the
21771 `else' branch below isn't an issue. */
c906108c 21772
d5166ae1 21773 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21774 {
43816ebc
TT
21775 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21776 filename = copy.get ();
d521ce57 21777 }
c906108c 21778
c24bdb02 21779 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21780}
21781
804d2729
TT
21782/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21783 buildsym_compunit constructor. */
f4dc4d17 21784
c24bdb02
KS
21785struct compunit_symtab *
21786dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21787 CORE_ADDR low_pc)
f4dc4d17 21788{
c24bdb02 21789 gdb_assert (m_builder == nullptr);
43f3e411 21790
c24bdb02 21791 m_builder.reset (new struct buildsym_compunit
f6e649dd 21792 (this->per_objfile->objfile,
c24bdb02 21793 name, comp_dir, language, low_pc));
93b8bea4 21794
c24bdb02 21795 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21796
c24bdb02
KS
21797 get_builder ()->record_debugformat ("DWARF 2");
21798 get_builder ()->record_producer (producer);
f4dc4d17 21799
c24bdb02 21800 processing_has_namespace_info = false;
43f3e411 21801
c24bdb02 21802 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21803}
21804
4c2df51b
DJ
21805static void
21806var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21807 struct dwarf2_cu *cu)
4c2df51b 21808{
5e22e966 21809 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21810 struct comp_unit_head *cu_header = &cu->header;
21811
4c2df51b
DJ
21812 /* NOTE drow/2003-01-30: There used to be a comment and some special
21813 code here to turn a symbol with DW_AT_external and a
21814 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21815 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21816 with some versions of binutils) where shared libraries could have
21817 relocations against symbols in their debug information - the
21818 minimal symbol would have the right address, but the debug info
21819 would not. It's no longer necessary, because we will explicitly
21820 apply relocations when we read in the debug information now. */
21821
21822 /* A DW_AT_location attribute with no contents indicates that a
21823 variable has been optimized away. */
9d2246fc 21824 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21825 {
f1e6e072 21826 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21827 return;
21828 }
21829
21830 /* Handle one degenerate form of location expression specially, to
21831 preserve GDB's previous behavior when section offsets are
336d760d
AT
21832 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21833 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21834
9d2246fc
TT
21835 if (attr->form_is_block ())
21836 {
21837 struct dwarf_block *block = attr->as_block ();
21838
21839 if ((block->data[0] == DW_OP_addr
21840 && block->size == 1 + cu_header->addr_size)
21841 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21842 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21843 && (block->size
21844 == 1 + leb128_size (&block->data[1]))))
21845 {
21846 unsigned int dummy;
21847
21848 if (block->data[0] == DW_OP_addr)
21849 SET_SYMBOL_VALUE_ADDRESS
21850 (sym, cu->header.read_address (objfile->obfd,
21851 block->data + 1,
38583298 21852 &dummy));
9d2246fc
TT
21853 else
21854 SET_SYMBOL_VALUE_ADDRESS
21855 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21856 &dummy));
21857 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21858 fixup_symbol_section (sym, objfile);
21859 SET_SYMBOL_VALUE_ADDRESS
21860 (sym,
21861 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 21862 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
21863 return;
21864 }
4c2df51b
DJ
21865 }
21866
21867 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21868 expression evaluator, and use LOC_COMPUTED only when necessary
21869 (i.e. when the value of a register or memory location is
21870 referenced, or a thread-local block, etc.). Then again, it might
21871 not be worthwhile. I'm assuming that it isn't unless performance
21872 or memory numbers show me otherwise. */
21873
f1e6e072 21874 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21875
f1e6e072 21876 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21877 cu->has_loclist = true;
4c2df51b
DJ
21878}
21879
c906108c
SS
21880/* Given a pointer to a DWARF information entry, figure out if we need
21881 to make a symbol table entry for it, and if so, create a new entry
21882 and return a pointer to it.
21883 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21884 used the passed type.
21885 If SPACE is not NULL, use it to hold the new symbol. If it is
21886 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21887
21888static struct symbol *
5e2db402
TT
21889new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21890 struct symbol *space)
c906108c 21891{
976ca316
SM
21892 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21893 struct objfile *objfile = per_objfile->objfile;
08feed99 21894 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21895 struct symbol *sym = NULL;
15d034d0 21896 const char *name;
c906108c
SS
21897 struct attribute *attr = NULL;
21898 struct attribute *attr2 = NULL;
e142c38c 21899 CORE_ADDR baseaddr;
e37fd15a
SW
21900 struct pending **list_to_add = NULL;
21901
edb3359d 21902 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21903
b3b3bada 21904 baseaddr = objfile->text_section_offset ();
c906108c 21905
94af9270 21906 name = dwarf2_name (die, cu);
c906108c
SS
21907 if (name)
21908 {
34eaf542 21909 int suppress_add = 0;
94af9270 21910
34eaf542
TT
21911 if (space)
21912 sym = space;
21913 else
8c14c3a3 21914 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21915 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21916
21917 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21918 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
21919 /* Fortran does not have mangling standard and the mangling does differ
21920 between gfortran, iFort etc. */
bcfe6157
TT
21921 const char *physname
21922 = (cu->language == language_fortran
21923 ? dwarf2_full_name (name, die, cu)
21924 : dwarf2_physname (name, die, cu));
21925 const char *linkagename = dw2_linkage_name (die, cu);
21926
21927 if (linkagename == nullptr || cu->language == language_ada)
21928 sym->set_linkage_name (physname);
21929 else
21930 {
21931 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21932 sym->set_linkage_name (linkagename);
21933 }
f55ee35c 21934
c906108c 21935 /* Default assumptions.
dda83cd7 21936 Use the passed type or decode it from the die. */
176620f1 21937 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21938 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21939 if (type != NULL)
21940 SYMBOL_TYPE (sym) = type;
21941 else
e7c27a73 21942 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21943 attr = dwarf2_attr (die,
21944 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21945 cu);
435d3d88 21946 if (attr != nullptr)
529908cb 21947 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 21948
edb3359d
DJ
21949 attr = dwarf2_attr (die,
21950 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21951 cu);
aa659cfa 21952 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 21953 {
529908cb 21954 file_name_index file_index
aa659cfa 21955 = (file_name_index) attr->as_nonnegative ();
8c43009f 21956 struct file_entry *fe;
9a619af0 21957
ecfb656c
PA
21958 if (cu->line_header != NULL)
21959 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21960 else
21961 fe = NULL;
21962
21963 if (fe == NULL)
b98664d3 21964 complaint (_("file index out of range"));
8c43009f
PA
21965 else
21966 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21967 }
21968
c906108c
SS
21969 switch (die->tag)
21970 {
21971 case DW_TAG_label:
e142c38c 21972 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21973 if (attr != nullptr)
3e29f34a
MR
21974 {
21975 CORE_ADDR addr;
21976
95f982e5 21977 addr = attr->as_address ();
3e29f34a 21978 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21979 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 21980 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 21981 }
8f5c6526
TV
21982 else
21983 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
21984 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21985 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 21986 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21987 break;
21988 case DW_TAG_subprogram:
21989 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21990 finish_block. */
f1e6e072 21991 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21992 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 21993 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
21994 || cu->language == language_ada
21995 || cu->language == language_fortran)
c906108c 21996 {
dda83cd7
SM
21997 /* Subprograms marked external are stored as a global symbol.
21998 Ada and Fortran subprograms, whether marked external or
21999 not, are always stored as a global symbol, because we want
22000 to be able to access them globally. For instance, we want
22001 to be able to break on a nested subprogram without having
22002 to specify the context. */
c24bdb02 22003 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
22004 }
22005 else
22006 {
e37fd15a 22007 list_to_add = cu->list_in_scope;
c906108c
SS
22008 }
22009 break;
edb3359d
DJ
22010 case DW_TAG_inlined_subroutine:
22011 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22012 finish_block. */
f1e6e072 22013 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22014 SYMBOL_INLINED (sym) = 1;
481860b3 22015 list_to_add = cu->list_in_scope;
edb3359d 22016 break;
34eaf542
TT
22017 case DW_TAG_template_value_param:
22018 suppress_add = 1;
22019 /* Fall through. */
72929c62 22020 case DW_TAG_constant:
c906108c 22021 case DW_TAG_variable:
254e6b9e 22022 case DW_TAG_member:
0963b4bd
MS
22023 /* Compilation with minimal debug info may result in
22024 variables with missing type entries. Change the
22025 misleading `void' type to something sensible. */
78134374 22026 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22027 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22028
e142c38c 22029 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22030 /* In the case of DW_TAG_member, we should only be called for
22031 static const members. */
22032 if (die->tag == DW_TAG_member)
22033 {
3863f96c
DE
22034 /* dwarf2_add_field uses die_is_declaration,
22035 so we do the same. */
254e6b9e
DE
22036 gdb_assert (die_is_declaration (die, cu));
22037 gdb_assert (attr);
22038 }
435d3d88 22039 if (attr != nullptr)
c906108c 22040 {
e7c27a73 22041 dwarf2_const_value (attr, sym, cu);
e142c38c 22042 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22043 if (!suppress_add)
34eaf542 22044 {
c45bc3f8 22045 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22046 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22047 else
e37fd15a 22048 list_to_add = cu->list_in_scope;
34eaf542 22049 }
c906108c
SS
22050 break;
22051 }
e142c38c 22052 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22053 if (attr != nullptr)
c906108c 22054 {
e7c27a73 22055 var_decode_location (attr, sym, cu);
e142c38c 22056 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22057
22058 /* Fortran explicitly imports any global symbols to the local
22059 scope by DW_TAG_common_block. */
22060 if (cu->language == language_fortran && die->parent
22061 && die->parent->tag == DW_TAG_common_block)
22062 attr2 = NULL;
22063
caac4577
JG
22064 if (SYMBOL_CLASS (sym) == LOC_STATIC
22065 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22066 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22067 {
22068 /* When a static variable is eliminated by the linker,
22069 the corresponding debug information is not stripped
22070 out, but the variable address is set to null;
22071 do not add such variables into symbol table. */
22072 }
c45bc3f8 22073 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22074 {
4b610737
TT
22075 if (SYMBOL_CLASS (sym) == LOC_STATIC
22076 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22077 && per_objfile->per_bfd->can_copy)
4b610737
TT
22078 {
22079 /* A global static variable might be subject to
22080 copy relocation. We first check for a local
22081 minsym, though, because maybe the symbol was
22082 marked hidden, in which case this would not
22083 apply. */
22084 bound_minimal_symbol found
22085 = (lookup_minimal_symbol_linkage
987012b8 22086 (sym->linkage_name (), objfile));
4b610737
TT
22087 if (found.minsym != nullptr)
22088 sym->maybe_copied = 1;
22089 }
f55ee35c 22090
1c809c68
TT
22091 /* A variable with DW_AT_external is never static,
22092 but it may be block-scoped. */
804d2729 22093 list_to_add
c24bdb02
KS
22094 = ((cu->list_in_scope
22095 == cu->get_builder ()->get_file_symbols ())
22096 ? cu->get_builder ()->get_global_symbols ()
804d2729 22097 : cu->list_in_scope);
1c809c68 22098 }
c906108c 22099 else
e37fd15a 22100 list_to_add = cu->list_in_scope;
c906108c
SS
22101 }
22102 else
22103 {
22104 /* We do not know the address of this symbol.
dda83cd7
SM
22105 If it is an external symbol and we have type information
22106 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22107 The address of the variable will then be determined from
22108 the minimal symbol table whenever the variable is
22109 referenced. */
e142c38c 22110 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22111
22112 /* Fortran explicitly imports any global symbols to the local
22113 scope by DW_TAG_common_block. */
22114 if (cu->language == language_fortran && die->parent
22115 && die->parent->tag == DW_TAG_common_block)
22116 {
22117 /* SYMBOL_CLASS doesn't matter here because
22118 read_common_block is going to reset it. */
22119 if (!suppress_add)
22120 list_to_add = cu->list_in_scope;
22121 }
c45bc3f8 22122 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22123 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22124 {
0fe7935b
DJ
22125 /* A variable with DW_AT_external is never static, but it
22126 may be block-scoped. */
804d2729 22127 list_to_add
c24bdb02
KS
22128 = ((cu->list_in_scope
22129 == cu->get_builder ()->get_file_symbols ())
22130 ? cu->get_builder ()->get_global_symbols ()
804d2729 22131 : cu->list_in_scope);
0fe7935b 22132
f1e6e072 22133 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22134 }
442ddf59
JK
22135 else if (!die_is_declaration (die, cu))
22136 {
22137 /* Use the default LOC_OPTIMIZED_OUT class. */
22138 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22139 if (!suppress_add)
22140 list_to_add = cu->list_in_scope;
442ddf59 22141 }
c906108c
SS
22142 }
22143 break;
22144 case DW_TAG_formal_parameter:
a60f3166
TT
22145 {
22146 /* If we are inside a function, mark this as an argument. If
22147 not, we might be looking at an argument to an inlined function
22148 when we do not have enough information to show inlined frames;
22149 pretend it's a local variable in that case so that the user can
22150 still see it. */
804d2729 22151 struct context_stack *curr
c24bdb02 22152 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22153 if (curr != nullptr && curr->name != nullptr)
22154 SYMBOL_IS_ARGUMENT (sym) = 1;
22155 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22156 if (attr != nullptr)
a60f3166
TT
22157 {
22158 var_decode_location (attr, sym, cu);
22159 }
22160 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22161 if (attr != nullptr)
a60f3166
TT
22162 {
22163 dwarf2_const_value (attr, sym, cu);
22164 }
f346a30d 22165
a60f3166
TT
22166 list_to_add = cu->list_in_scope;
22167 }
c906108c
SS
22168 break;
22169 case DW_TAG_unspecified_parameters:
22170 /* From varargs functions; gdb doesn't seem to have any
22171 interest in this information, so just ignore it for now.
22172 (FIXME?) */
22173 break;
34eaf542
TT
22174 case DW_TAG_template_type_param:
22175 suppress_add = 1;
22176 /* Fall through. */
c906108c 22177 case DW_TAG_class_type:
680b30c7 22178 case DW_TAG_interface_type:
c906108c
SS
22179 case DW_TAG_structure_type:
22180 case DW_TAG_union_type:
72019c9c 22181 case DW_TAG_set_type:
c906108c 22182 case DW_TAG_enumeration_type:
f1e6e072 22183 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22184 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22185
63d06c5c 22186 {
9c37b5ae 22187 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22188 really ever be static objects: otherwise, if you try
22189 to, say, break of a class's method and you're in a file
22190 which doesn't mention that class, it won't work unless
22191 the check for all static symbols in lookup_symbol_aux
22192 saves you. See the OtherFileClass tests in
22193 gdb.c++/namespace.exp. */
22194
e37fd15a 22195 if (!suppress_add)
34eaf542 22196 {
c24bdb02 22197 buildsym_compunit *builder = cu->get_builder ();
804d2729 22198 list_to_add
c24bdb02 22199 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22200 && cu->language == language_cplus
c24bdb02 22201 ? builder->get_global_symbols ()
804d2729 22202 : cu->list_in_scope);
63d06c5c 22203
64382290 22204 /* The semantics of C++ state that "struct foo {
9c37b5ae 22205 ... }" also defines a typedef for "foo". */
64382290 22206 if (cu->language == language_cplus
45280282 22207 || cu->language == language_ada
c44af4eb
TT
22208 || cu->language == language_d
22209 || cu->language == language_rust)
64382290
TT
22210 {
22211 /* The symbol's name is already allocated along
22212 with this objfile, so we don't need to
22213 duplicate it for the type. */
7d93a1e0 22214 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22215 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22216 }
63d06c5c
DC
22217 }
22218 }
c906108c
SS
22219 break;
22220 case DW_TAG_typedef:
f1e6e072 22221 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22222 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22223 list_to_add = cu->list_in_scope;
63d06c5c 22224 break;
d8f62e84 22225 case DW_TAG_array_type:
c906108c 22226 case DW_TAG_base_type:
dda83cd7 22227 case DW_TAG_subrange_type:
f1e6e072 22228 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22229 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22230 list_to_add = cu->list_in_scope;
c906108c
SS
22231 break;
22232 case DW_TAG_enumerator:
e142c38c 22233 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22234 if (attr != nullptr)
c906108c 22235 {
e7c27a73 22236 dwarf2_const_value (attr, sym, cu);
c906108c 22237 }
63d06c5c
DC
22238 {
22239 /* NOTE: carlton/2003-11-10: See comment above in the
22240 DW_TAG_class_type, etc. block. */
22241
804d2729 22242 list_to_add
c24bdb02 22243 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22244 && cu->language == language_cplus
c24bdb02 22245 ? cu->get_builder ()->get_global_symbols ()
804d2729 22246 : cu->list_in_scope);
63d06c5c 22247 }
c906108c 22248 break;
74921315 22249 case DW_TAG_imported_declaration:
5c4e30ca 22250 case DW_TAG_namespace:
f1e6e072 22251 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22252 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22253 break;
530e8392
KB
22254 case DW_TAG_module:
22255 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22256 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22257 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22258 break;
4357ac6c 22259 case DW_TAG_common_block:
f1e6e072 22260 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22261 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22262 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22263 break;
c906108c
SS
22264 default:
22265 /* Not a tag we recognize. Hopefully we aren't processing
22266 trash data, but since we must specifically ignore things
22267 we don't recognize, there is nothing else we should do at
0963b4bd 22268 this point. */
b98664d3 22269 complaint (_("unsupported tag: '%s'"),
4d3c2250 22270 dwarf_tag_name (die->tag));
c906108c
SS
22271 break;
22272 }
df8a16a1 22273
e37fd15a
SW
22274 if (suppress_add)
22275 {
22276 sym->hash_next = objfile->template_symbols;
22277 objfile->template_symbols = sym;
22278 list_to_add = NULL;
22279 }
22280
22281 if (list_to_add != NULL)
d3cb6808 22282 add_symbol_to_list (sym, list_to_add);
e37fd15a 22283
df8a16a1
DJ
22284 /* For the benefit of old versions of GCC, check for anonymous
22285 namespaces based on the demangled name. */
4d4ec4e5 22286 if (!cu->processing_has_namespace_info
94af9270 22287 && cu->language == language_cplus)
c24bdb02 22288 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22289 }
22290 return (sym);
22291}
22292
98bfdba5
PA
22293/* Given an attr with a DW_FORM_dataN value in host byte order,
22294 zero-extend it as appropriate for the symbol's type. The DWARF
22295 standard (v4) is not entirely clear about the meaning of using
22296 DW_FORM_dataN for a constant with a signed type, where the type is
22297 wider than the data. The conclusion of a discussion on the DWARF
22298 list was that this is unspecified. We choose to always zero-extend
22299 because that is the interpretation long in use by GCC. */
c906108c 22300
98bfdba5 22301static gdb_byte *
ff39bb5e 22302dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22303 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22304{
5e22e966 22305 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22306 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22307 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22308 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22309
22310 if (bits < sizeof (*value) * 8)
22311 {
22312 l &= ((LONGEST) 1 << bits) - 1;
22313 *value = l;
22314 }
22315 else if (bits == sizeof (*value) * 8)
22316 *value = l;
22317 else
22318 {
224c3ddb 22319 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22320 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22321 return bytes;
22322 }
22323
22324 return NULL;
22325}
22326
22327/* Read a constant value from an attribute. Either set *VALUE, or if
22328 the value does not fit in *VALUE, set *BYTES - either already
22329 allocated on the objfile obstack, or newly allocated on OBSTACK,
22330 or, set *BATON, if we translated the constant to a location
22331 expression. */
22332
22333static void
ff39bb5e 22334dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22335 const char *name, struct obstack *obstack,
22336 struct dwarf2_cu *cu,
d521ce57 22337 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22338 struct dwarf2_locexpr_baton **baton)
22339{
5e22e966 22340 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22341 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22342 struct comp_unit_head *cu_header = &cu->header;
c906108c 22343 struct dwarf_block *blk;
98bfdba5
PA
22344 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22345 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22346
22347 *value = 0;
22348 *bytes = NULL;
22349 *baton = NULL;
c906108c
SS
22350
22351 switch (attr->form)
22352 {
22353 case DW_FORM_addr:
336d760d 22354 case DW_FORM_addrx:
3019eac3 22355 case DW_FORM_GNU_addr_index:
ac56253d 22356 {
ac56253d
TT
22357 gdb_byte *data;
22358
98bfdba5
PA
22359 if (TYPE_LENGTH (type) != cu_header->addr_size)
22360 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22361 cu_header->addr_size,
98bfdba5 22362 TYPE_LENGTH (type));
ac56253d
TT
22363 /* Symbols of this form are reasonably rare, so we just
22364 piggyback on the existing location code rather than writing
22365 a new implementation of symbol_computed_ops. */
8d749320 22366 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22367 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22368 (*baton)->per_cu = cu->per_cu;
22369 gdb_assert ((*baton)->per_cu);
ac56253d 22370
98bfdba5 22371 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22372 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22373 (*baton)->data = data;
ac56253d
TT
22374
22375 data[0] = DW_OP_addr;
22376 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22377 byte_order, attr->as_address ());
ac56253d 22378 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22379 }
c906108c 22380 break;
4ac36638 22381 case DW_FORM_string:
93b5768b 22382 case DW_FORM_strp:
cf532bd1 22383 case DW_FORM_strx:
3019eac3 22384 case DW_FORM_GNU_str_index:
36586728 22385 case DW_FORM_GNU_strp_alt:
c6481205 22386 /* The string is already allocated on the objfile obstack, point
98bfdba5 22387 directly to it. */
2c830f54 22388 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22389 break;
c906108c
SS
22390 case DW_FORM_block1:
22391 case DW_FORM_block2:
22392 case DW_FORM_block4:
22393 case DW_FORM_block:
2dc7f7b3 22394 case DW_FORM_exprloc:
0224619f 22395 case DW_FORM_data16:
9d2246fc 22396 blk = attr->as_block ();
98bfdba5
PA
22397 if (TYPE_LENGTH (type) != blk->size)
22398 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22399 TYPE_LENGTH (type));
22400 *bytes = blk->data;
c906108c 22401 break;
2df3850c
JM
22402
22403 /* The DW_AT_const_value attributes are supposed to carry the
22404 symbol's value "represented as it would be on the target
22405 architecture." By the time we get here, it's already been
22406 converted to host endianness, so we just need to sign- or
22407 zero-extend it as appropriate. */
22408 case DW_FORM_data1:
3aef2284 22409 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22410 break;
c906108c 22411 case DW_FORM_data2:
3aef2284 22412 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22413 break;
c906108c 22414 case DW_FORM_data4:
3aef2284 22415 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22416 break;
c906108c 22417 case DW_FORM_data8:
3aef2284 22418 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22419 break;
22420
c906108c 22421 case DW_FORM_sdata:
663c44ac 22422 case DW_FORM_implicit_const:
1bc397c5 22423 *value = attr->as_signed ();
2df3850c
JM
22424 break;
22425
c906108c 22426 case DW_FORM_udata:
529908cb 22427 *value = attr->as_unsigned ();
c906108c 22428 break;
2df3850c 22429
c906108c 22430 default:
b98664d3 22431 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22432 dwarf_form_name (attr->form));
98bfdba5 22433 *value = 0;
c906108c
SS
22434 break;
22435 }
22436}
22437
2df3850c 22438
98bfdba5
PA
22439/* Copy constant value from an attribute to a symbol. */
22440
2df3850c 22441static void
ff39bb5e 22442dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22443 struct dwarf2_cu *cu)
2df3850c 22444{
5e22e966 22445 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22446 LONGEST value;
d521ce57 22447 const gdb_byte *bytes;
98bfdba5 22448 struct dwarf2_locexpr_baton *baton;
2df3850c 22449
98bfdba5 22450 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22451 sym->print_name (),
98bfdba5
PA
22452 &objfile->objfile_obstack, cu,
22453 &value, &bytes, &baton);
2df3850c 22454
98bfdba5
PA
22455 if (baton != NULL)
22456 {
98bfdba5 22457 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22458 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22459 }
22460 else if (bytes != NULL)
22461 {
22462 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22463 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22464 }
22465 else
22466 {
22467 SYMBOL_VALUE (sym) = value;
f1e6e072 22468 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22469 }
2df3850c
JM
22470}
22471
c906108c
SS
22472/* Return the type of the die in question using its DW_AT_type attribute. */
22473
22474static struct type *
e7c27a73 22475die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22476{
c906108c 22477 struct attribute *type_attr;
c906108c 22478
e142c38c 22479 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22480 if (!type_attr)
22481 {
5e22e966 22482 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22483 /* A missing DW_AT_type represents a void type. */
518817b3 22484 return objfile_type (objfile)->builtin_void;
c906108c 22485 }
348e048f 22486
673bfd45 22487 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22488}
22489
b4ba55a1
JB
22490/* True iff CU's producer generates GNAT Ada auxiliary information
22491 that allows to find parallel types through that information instead
22492 of having to do expensive parallel lookups by type name. */
22493
22494static int
22495need_gnat_info (struct dwarf2_cu *cu)
22496{
de4cb04a
JB
22497 /* Assume that the Ada compiler was GNAT, which always produces
22498 the auxiliary information. */
22499 return (cu->language == language_ada);
b4ba55a1
JB
22500}
22501
b4ba55a1
JB
22502/* Return the auxiliary type of the die in question using its
22503 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22504 attribute is not present. */
22505
22506static struct type *
22507die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22508{
b4ba55a1 22509 struct attribute *type_attr;
b4ba55a1
JB
22510
22511 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22512 if (!type_attr)
22513 return NULL;
22514
673bfd45 22515 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22516}
22517
22518/* If DIE has a descriptive_type attribute, then set the TYPE's
22519 descriptive type accordingly. */
22520
22521static void
22522set_descriptive_type (struct type *type, struct die_info *die,
22523 struct dwarf2_cu *cu)
22524{
22525 struct type *descriptive_type = die_descriptive_type (die, cu);
22526
22527 if (descriptive_type)
22528 {
22529 ALLOCATE_GNAT_AUX_TYPE (type);
22530 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22531 }
22532}
22533
c906108c
SS
22534/* Return the containing type of the die in question using its
22535 DW_AT_containing_type attribute. */
22536
22537static struct type *
e7c27a73 22538die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22539{
c906108c 22540 struct attribute *type_attr;
5e22e966 22541 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22542
e142c38c 22543 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22544 if (!type_attr)
22545 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22546 "[in module %s]"), objfile_name (objfile));
33ac96f0 22547
673bfd45 22548 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22549}
22550
ac9ec31b
DE
22551/* Return an error marker type to use for the ill formed type in DIE/CU. */
22552
22553static struct type *
22554build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22555{
976ca316
SM
22556 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22557 struct objfile *objfile = per_objfile->objfile;
528e1572 22558 char *saved;
ac9ec31b 22559
528e1572
SM
22560 std::string message
22561 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22562 objfile_name (objfile),
22563 sect_offset_str (cu->header.sect_off),
22564 sect_offset_str (die->sect_off));
efba19b0 22565 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22566
19f392bc 22567 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22568}
22569
673bfd45 22570/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22571 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22572 DW_AT_containing_type.
673bfd45
DE
22573 If there is no type substitute an error marker. */
22574
c906108c 22575static struct type *
ff39bb5e 22576lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22577 struct dwarf2_cu *cu)
c906108c 22578{
976ca316
SM
22579 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22580 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22581 struct type *this_type;
22582
ac9ec31b
DE
22583 gdb_assert (attr->name == DW_AT_type
22584 || attr->name == DW_AT_GNAT_descriptive_type
22585 || attr->name == DW_AT_containing_type);
22586
673bfd45
DE
22587 /* First see if we have it cached. */
22588
36586728
TT
22589 if (attr->form == DW_FORM_GNU_ref_alt)
22590 {
22591 struct dwarf2_per_cu_data *per_cu;
0826b30a 22592 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22593
976ca316
SM
22594 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22595 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22596 }
cd6c91b4 22597 else if (attr->form_is_ref ())
673bfd45 22598 {
0826b30a 22599 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22600
976ca316 22601 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22602 }
55f1336d 22603 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22604 {
630ed6b9 22605 ULONGEST signature = attr->as_signature ();
673bfd45 22606
ac9ec31b 22607 return get_signatured_type (die, signature, cu);
673bfd45
DE
22608 }
22609 else
22610 {
b98664d3 22611 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22612 " at %s [in module %s]"),
22613 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22614 objfile_name (objfile));
ac9ec31b 22615 return build_error_marker_type (cu, die);
673bfd45
DE
22616 }
22617
22618 /* If not cached we need to read it in. */
22619
22620 if (this_type == NULL)
22621 {
ac9ec31b 22622 struct die_info *type_die = NULL;
673bfd45
DE
22623 struct dwarf2_cu *type_cu = cu;
22624
cd6c91b4 22625 if (attr->form_is_ref ())
ac9ec31b
DE
22626 type_die = follow_die_ref (die, attr, &type_cu);
22627 if (type_die == NULL)
22628 return build_error_marker_type (cu, die);
22629 /* If we find the type now, it's probably because the type came
3019eac3
DE
22630 from an inter-CU reference and the type's CU got expanded before
22631 ours. */
ac9ec31b 22632 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22633 }
22634
22635 /* If we still don't have a type use an error marker. */
22636
22637 if (this_type == NULL)
ac9ec31b 22638 return build_error_marker_type (cu, die);
673bfd45 22639
f792889a 22640 return this_type;
c906108c
SS
22641}
22642
673bfd45
DE
22643/* Return the type in DIE, CU.
22644 Returns NULL for invalid types.
22645
02142a6c 22646 This first does a lookup in die_type_hash,
673bfd45
DE
22647 and only reads the die in if necessary.
22648
22649 NOTE: This can be called when reading in partial or full symbols. */
22650
f792889a 22651static struct type *
e7c27a73 22652read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22653{
f792889a
DJ
22654 struct type *this_type;
22655
22656 this_type = get_die_type (die, cu);
22657 if (this_type)
22658 return this_type;
22659
673bfd45
DE
22660 return read_type_die_1 (die, cu);
22661}
22662
22663/* Read the type in DIE, CU.
22664 Returns NULL for invalid types. */
22665
22666static struct type *
22667read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22668{
22669 struct type *this_type = NULL;
22670
c906108c
SS
22671 switch (die->tag)
22672 {
22673 case DW_TAG_class_type:
680b30c7 22674 case DW_TAG_interface_type:
c906108c
SS
22675 case DW_TAG_structure_type:
22676 case DW_TAG_union_type:
f792889a 22677 this_type = read_structure_type (die, cu);
c906108c
SS
22678 break;
22679 case DW_TAG_enumeration_type:
f792889a 22680 this_type = read_enumeration_type (die, cu);
c906108c
SS
22681 break;
22682 case DW_TAG_subprogram:
22683 case DW_TAG_subroutine_type:
edb3359d 22684 case DW_TAG_inlined_subroutine:
f792889a 22685 this_type = read_subroutine_type (die, cu);
c906108c
SS
22686 break;
22687 case DW_TAG_array_type:
f792889a 22688 this_type = read_array_type (die, cu);
c906108c 22689 break;
72019c9c 22690 case DW_TAG_set_type:
f792889a 22691 this_type = read_set_type (die, cu);
72019c9c 22692 break;
c906108c 22693 case DW_TAG_pointer_type:
f792889a 22694 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22695 break;
22696 case DW_TAG_ptr_to_member_type:
f792889a 22697 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22698 break;
22699 case DW_TAG_reference_type:
4297a3f0
AV
22700 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22701 break;
22702 case DW_TAG_rvalue_reference_type:
22703 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22704 break;
22705 case DW_TAG_const_type:
f792889a 22706 this_type = read_tag_const_type (die, cu);
c906108c
SS
22707 break;
22708 case DW_TAG_volatile_type:
f792889a 22709 this_type = read_tag_volatile_type (die, cu);
c906108c 22710 break;
06d66ee9
TT
22711 case DW_TAG_restrict_type:
22712 this_type = read_tag_restrict_type (die, cu);
22713 break;
c906108c 22714 case DW_TAG_string_type:
f792889a 22715 this_type = read_tag_string_type (die, cu);
c906108c
SS
22716 break;
22717 case DW_TAG_typedef:
f792889a 22718 this_type = read_typedef (die, cu);
c906108c 22719 break;
a02abb62 22720 case DW_TAG_subrange_type:
f792889a 22721 this_type = read_subrange_type (die, cu);
a02abb62 22722 break;
c906108c 22723 case DW_TAG_base_type:
f792889a 22724 this_type = read_base_type (die, cu);
c906108c 22725 break;
81a17f79 22726 case DW_TAG_unspecified_type:
f792889a 22727 this_type = read_unspecified_type (die, cu);
81a17f79 22728 break;
0114d602
DJ
22729 case DW_TAG_namespace:
22730 this_type = read_namespace_type (die, cu);
22731 break;
f55ee35c
JK
22732 case DW_TAG_module:
22733 this_type = read_module_type (die, cu);
22734 break;
a2c2acaf
MW
22735 case DW_TAG_atomic_type:
22736 this_type = read_tag_atomic_type (die, cu);
22737 break;
c906108c 22738 default:
b98664d3 22739 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22740 dwarf_tag_name (die->tag));
c906108c
SS
22741 break;
22742 }
63d06c5c 22743
f792889a 22744 return this_type;
63d06c5c
DC
22745}
22746
abc72ce4
DE
22747/* See if we can figure out if the class lives in a namespace. We do
22748 this by looking for a member function; its demangled name will
22749 contain namespace info, if there is any.
22750 Return the computed name or NULL.
22751 Space for the result is allocated on the objfile's obstack.
22752 This is the full-die version of guess_partial_die_structure_name.
22753 In this case we know DIE has no useful parent. */
22754
43816ebc 22755static const char *
abc72ce4
DE
22756guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22757{
22758 struct die_info *spec_die;
22759 struct dwarf2_cu *spec_cu;
22760 struct die_info *child;
5e22e966 22761 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22762
22763 spec_cu = cu;
22764 spec_die = die_specification (die, &spec_cu);
22765 if (spec_die != NULL)
22766 {
22767 die = spec_die;
22768 cu = spec_cu;
22769 }
22770
22771 for (child = die->child;
22772 child != NULL;
22773 child = child->sibling)
22774 {
22775 if (child->tag == DW_TAG_subprogram)
22776 {
73b9be8b 22777 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22778
7d45c7c3 22779 if (linkage_name != NULL)
abc72ce4 22780 {
43816ebc 22781 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22782 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22783 const char *name = NULL;
abc72ce4
DE
22784
22785 if (actual_name != NULL)
22786 {
15d034d0 22787 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22788
22789 if (die_name != NULL
43816ebc 22790 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22791 {
22792 /* Strip off the class name from the full name.
22793 We want the prefix. */
22794 int die_name_len = strlen (die_name);
43816ebc
TT
22795 int actual_name_len = strlen (actual_name.get ());
22796 const char *ptr = actual_name.get ();
abc72ce4
DE
22797
22798 /* Test for '::' as a sanity check. */
22799 if (actual_name_len > die_name_len + 2
43816ebc 22800 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22801 name = obstack_strndup (
e3b94546 22802 &objfile->per_bfd->storage_obstack,
43816ebc 22803 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22804 }
22805 }
abc72ce4
DE
22806 return name;
22807 }
22808 }
22809 }
22810
22811 return NULL;
22812}
22813
96408a79
SA
22814/* GCC might emit a nameless typedef that has a linkage name. Determine the
22815 prefix part in such case. See
22816 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22817
a121b7c1 22818static const char *
96408a79
SA
22819anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22820{
22821 struct attribute *attr;
e6a959d6 22822 const char *base;
96408a79
SA
22823
22824 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22825 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22826 return NULL;
22827
7d45c7c3 22828 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22829 return NULL;
22830
73b9be8b 22831 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22832 const char *attr_name = attr->as_string ();
22833 if (attr == NULL || attr_name == NULL)
96408a79
SA
22834 return NULL;
22835
22836 /* dwarf2_name had to be already called. */
3b64bf15 22837 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22838
22839 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22840 base = strrchr (attr_name, ':');
22841 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22842 return "";
22843
5e22e966 22844 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22845 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22846 attr_name,
22847 &base[-1] - attr_name);
96408a79
SA
22848}
22849
fdde2d81 22850/* Return the name of the namespace/class that DIE is defined within,
0114d602 22851 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22852
0114d602
DJ
22853 For example, if we're within the method foo() in the following
22854 code:
22855
22856 namespace N {
22857 class C {
22858 void foo () {
22859 }
22860 };
22861 }
22862
22863 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22864
0d5cff50 22865static const char *
e142c38c 22866determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22867{
976ca316 22868 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22869 struct die_info *parent, *spec_die;
22870 struct dwarf2_cu *spec_cu;
22871 struct type *parent_type;
a121b7c1 22872 const char *retval;
63d06c5c 22873
9c37b5ae 22874 if (cu->language != language_cplus
c44af4eb
TT
22875 && cu->language != language_fortran && cu->language != language_d
22876 && cu->language != language_rust)
0114d602
DJ
22877 return "";
22878
96408a79
SA
22879 retval = anonymous_struct_prefix (die, cu);
22880 if (retval)
22881 return retval;
22882
0114d602
DJ
22883 /* We have to be careful in the presence of DW_AT_specification.
22884 For example, with GCC 3.4, given the code
22885
22886 namespace N {
22887 void foo() {
22888 // Definition of N::foo.
22889 }
22890 }
22891
22892 then we'll have a tree of DIEs like this:
22893
22894 1: DW_TAG_compile_unit
22895 2: DW_TAG_namespace // N
22896 3: DW_TAG_subprogram // declaration of N::foo
22897 4: DW_TAG_subprogram // definition of N::foo
22898 DW_AT_specification // refers to die #3
22899
22900 Thus, when processing die #4, we have to pretend that we're in
22901 the context of its DW_AT_specification, namely the contex of die
22902 #3. */
22903 spec_cu = cu;
22904 spec_die = die_specification (die, &spec_cu);
22905 if (spec_die == NULL)
22906 parent = die->parent;
22907 else
63d06c5c 22908 {
0114d602
DJ
22909 parent = spec_die->parent;
22910 cu = spec_cu;
63d06c5c 22911 }
0114d602
DJ
22912
22913 if (parent == NULL)
22914 return "";
98bfdba5
PA
22915 else if (parent->building_fullname)
22916 {
22917 const char *name;
22918 const char *parent_name;
22919
22920 /* It has been seen on RealView 2.2 built binaries,
22921 DW_TAG_template_type_param types actually _defined_ as
22922 children of the parent class:
22923
22924 enum E {};
22925 template class <class Enum> Class{};
22926 Class<enum E> class_e;
22927
dda83cd7
SM
22928 1: DW_TAG_class_type (Class)
22929 2: DW_TAG_enumeration_type (E)
22930 3: DW_TAG_enumerator (enum1:0)
22931 3: DW_TAG_enumerator (enum2:1)
22932 ...
22933 2: DW_TAG_template_type_param
22934 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
22935
22936 Besides being broken debug info, it can put GDB into an
22937 infinite loop. Consider:
22938
22939 When we're building the full name for Class<E>, we'll start
22940 at Class, and go look over its template type parameters,
22941 finding E. We'll then try to build the full name of E, and
22942 reach here. We're now trying to build the full name of E,
22943 and look over the parent DIE for containing scope. In the
22944 broken case, if we followed the parent DIE of E, we'd again
22945 find Class, and once again go look at its template type
22946 arguments, etc., etc. Simply don't consider such parent die
22947 as source-level parent of this die (it can't be, the language
22948 doesn't allow it), and break the loop here. */
22949 name = dwarf2_name (die, cu);
22950 parent_name = dwarf2_name (parent, cu);
b98664d3 22951 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22952 name ? name : "<unknown>",
22953 parent_name ? parent_name : "<unknown>");
22954 return "";
22955 }
63d06c5c 22956 else
0114d602
DJ
22957 switch (parent->tag)
22958 {
63d06c5c 22959 case DW_TAG_namespace:
0114d602 22960 parent_type = read_type_die (parent, cu);
acebe513
UW
22961 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22962 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22963 Work around this problem here. */
22964 if (cu->language == language_cplus
7d93a1e0 22965 && strcmp (parent_type->name (), "::") == 0)
acebe513 22966 return "";
0114d602 22967 /* We give a name to even anonymous namespaces. */
7d93a1e0 22968 return parent_type->name ();
63d06c5c 22969 case DW_TAG_class_type:
680b30c7 22970 case DW_TAG_interface_type:
63d06c5c 22971 case DW_TAG_structure_type:
0114d602 22972 case DW_TAG_union_type:
f55ee35c 22973 case DW_TAG_module:
0114d602 22974 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
22975 if (parent_type->name () != NULL)
22976 return parent_type->name ();
0114d602
DJ
22977 else
22978 /* An anonymous structure is only allowed non-static data
22979 members; no typedefs, no member functions, et cetera.
22980 So it does not need a prefix. */
22981 return "";
abc72ce4 22982 case DW_TAG_compile_unit:
95554aad 22983 case DW_TAG_partial_unit:
abc72ce4
DE
22984 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22985 if (cu->language == language_cplus
976ca316 22986 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
22987 && die->child != NULL
22988 && (die->tag == DW_TAG_class_type
22989 || die->tag == DW_TAG_structure_type
22990 || die->tag == DW_TAG_union_type))
22991 {
43816ebc 22992 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22993 if (name != NULL)
22994 return name;
22995 }
22996 return "";
0a4b0913
AB
22997 case DW_TAG_subprogram:
22998 /* Nested subroutines in Fortran get a prefix with the name
22999 of the parent's subroutine. */
23000 if (cu->language == language_fortran)
23001 {
23002 if ((die->tag == DW_TAG_subprogram)
23003 && (dwarf2_name (parent, cu) != NULL))
23004 return dwarf2_name (parent, cu);
23005 }
23006 return determine_prefix (parent, cu);
3d567982
TT
23007 case DW_TAG_enumeration_type:
23008 parent_type = read_type_die (parent, cu);
3bc440a2 23009 if (parent_type->is_declared_class ())
3d567982 23010 {
7d93a1e0
SM
23011 if (parent_type->name () != NULL)
23012 return parent_type->name ();
3d567982
TT
23013 return "";
23014 }
23015 /* Fall through. */
63d06c5c 23016 default:
8176b9b8 23017 return determine_prefix (parent, cu);
63d06c5c 23018 }
63d06c5c
DC
23019}
23020
3e43a32a
MS
23021/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23022 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23023 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23024 an obconcat, otherwise allocate storage for the result. The CU argument is
23025 used to determine the language and hence, the appropriate separator. */
987504bb 23026
f55ee35c 23027#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23028
23029static char *
f55ee35c 23030typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23031 int physname, struct dwarf2_cu *cu)
63d06c5c 23032{
f55ee35c 23033 const char *lead = "";
5c315b68 23034 const char *sep;
63d06c5c 23035
3e43a32a
MS
23036 if (suffix == NULL || suffix[0] == '\0'
23037 || prefix == NULL || prefix[0] == '\0')
987504bb 23038 sep = "";
45280282
IB
23039 else if (cu->language == language_d)
23040 {
23041 /* For D, the 'main' function could be defined in any module, but it
23042 should never be prefixed. */
23043 if (strcmp (suffix, "D main") == 0)
23044 {
23045 prefix = "";
23046 sep = "";
23047 }
23048 else
23049 sep = ".";
23050 }
f55ee35c
JK
23051 else if (cu->language == language_fortran && physname)
23052 {
23053 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23054 DW_AT_MIPS_linkage_name is preferred and used instead. */
23055
23056 lead = "__";
23057 sep = "_MOD_";
23058 }
987504bb
JJ
23059 else
23060 sep = "::";
63d06c5c 23061
6dd47d34
DE
23062 if (prefix == NULL)
23063 prefix = "";
23064 if (suffix == NULL)
23065 suffix = "";
23066
987504bb
JJ
23067 if (obs == NULL)
23068 {
3e43a32a 23069 char *retval
224c3ddb
SM
23070 = ((char *)
23071 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23072
f55ee35c
JK
23073 strcpy (retval, lead);
23074 strcat (retval, prefix);
6dd47d34
DE
23075 strcat (retval, sep);
23076 strcat (retval, suffix);
63d06c5c
DC
23077 return retval;
23078 }
987504bb
JJ
23079 else
23080 {
23081 /* We have an obstack. */
f55ee35c 23082 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23083 }
63d06c5c
DC
23084}
23085
71c25dea
TT
23086/* Get name of a die, return NULL if not found. */
23087
15d034d0
TT
23088static const char *
23089dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23090 struct objfile *objfile)
71c25dea
TT
23091{
23092 if (name && cu->language == language_cplus)
23093 {
596dc4ad
TT
23094 gdb::unique_xmalloc_ptr<char> canon_name
23095 = cp_canonicalize_string (name);
71c25dea 23096
596dc4ad
TT
23097 if (canon_name != nullptr)
23098 name = objfile->intern (canon_name.get ());
71c25dea
TT
23099 }
23100
23101 return name;
c906108c
SS
23102}
23103
96553a0c
DE
23104/* Get name of a die, return NULL if not found.
23105 Anonymous namespaces are converted to their magic string. */
9219021c 23106
15d034d0 23107static const char *
e142c38c 23108dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23109{
23110 struct attribute *attr;
5e22e966 23111 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23112
e142c38c 23113 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23114 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23115 if (attr_name == nullptr
96553a0c 23116 && die->tag != DW_TAG_namespace
53832f31
TT
23117 && die->tag != DW_TAG_class_type
23118 && die->tag != DW_TAG_interface_type
23119 && die->tag != DW_TAG_structure_type
23120 && die->tag != DW_TAG_union_type)
71c25dea
TT
23121 return NULL;
23122
23123 switch (die->tag)
23124 {
23125 case DW_TAG_compile_unit:
95554aad 23126 case DW_TAG_partial_unit:
71c25dea
TT
23127 /* Compilation units have a DW_AT_name that is a filename, not
23128 a source language identifier. */
23129 case DW_TAG_enumeration_type:
23130 case DW_TAG_enumerator:
23131 /* These tags always have simple identifiers already; no need
23132 to canonicalize them. */
2c830f54 23133 return attr_name;
907af001 23134
96553a0c 23135 case DW_TAG_namespace:
2c830f54
TT
23136 if (attr_name != nullptr)
23137 return attr_name;
96553a0c
DE
23138 return CP_ANONYMOUS_NAMESPACE_STR;
23139
907af001
UW
23140 case DW_TAG_class_type:
23141 case DW_TAG_interface_type:
23142 case DW_TAG_structure_type:
23143 case DW_TAG_union_type:
23144 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23145 structures or unions. These were of the form "._%d" in GCC 4.1,
23146 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23147 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23148 if (attr_name != nullptr
23149 && (startswith (attr_name, "._")
23150 || startswith (attr_name, "<anonymous")))
907af001 23151 return NULL;
53832f31
TT
23152
23153 /* GCC might emit a nameless typedef that has a linkage name. See
23154 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23155 if (!attr || attr_name == NULL)
53832f31 23156 {
73b9be8b 23157 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23158 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23159 if (attr == NULL || attr_name == NULL)
53832f31
TT
23160 return NULL;
23161
2c830f54 23162 /* Avoid demangling attr_name the second time on a second
df5c6c50 23163 call for the same DIE. */
3b64bf15 23164 if (!attr->canonical_string_p ())
53832f31 23165 {
43816ebc 23166 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23167 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23168 if (demangled == nullptr)
23169 return nullptr;
43816ebc 23170
c6481205 23171 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23172 attr_name = attr->as_string ();
53832f31 23173 }
67430cd0 23174
2c830f54
TT
23175 /* Strip any leading namespaces/classes, keep only the
23176 base name. DW_AT_name for named DIEs does not
23177 contain the prefixes. */
23178 const char *base = strrchr (attr_name, ':');
23179 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23180 return &base[1];
23181 else
2c830f54 23182 return attr_name;
53832f31 23183 }
907af001
UW
23184 break;
23185
71c25dea 23186 default:
907af001
UW
23187 break;
23188 }
23189
3b64bf15 23190 if (!attr->canonical_string_p ())
c6481205
TT
23191 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23192 objfile));
2c830f54 23193 return attr->as_string ();
9219021c
DC
23194}
23195
23196/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23197 is none. *EXT_CU is the CU containing DIE on input, and the CU
23198 containing the return value on output. */
9219021c
DC
23199
23200static struct die_info *
f2f0e013 23201dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23202{
23203 struct attribute *attr;
9219021c 23204
f2f0e013 23205 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23206 if (attr == NULL)
23207 return NULL;
23208
f2f0e013 23209 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23210}
23211
f9aca02d 23212static void
d97bc12b 23213dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23214{
23215 unsigned int i;
23216
d97bc12b 23217 print_spaces (indent, f);
9d8780f0 23218 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23219 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23220 sect_offset_str (die->sect_off));
d97bc12b
DE
23221
23222 if (die->parent != NULL)
23223 {
23224 print_spaces (indent, f);
9d8780f0
SM
23225 fprintf_unfiltered (f, " parent at offset: %s\n",
23226 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23227 }
23228
23229 print_spaces (indent, f);
23230 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23231 dwarf_bool_name (die->child != NULL));
c906108c 23232
d97bc12b
DE
23233 print_spaces (indent, f);
23234 fprintf_unfiltered (f, " attributes:\n");
23235
c906108c
SS
23236 for (i = 0; i < die->num_attrs; ++i)
23237 {
d97bc12b
DE
23238 print_spaces (indent, f);
23239 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23240 dwarf_attr_name (die->attrs[i].name),
23241 dwarf_form_name (die->attrs[i].form));
d97bc12b 23242
c906108c
SS
23243 switch (die->attrs[i].form)
23244 {
c906108c 23245 case DW_FORM_addr:
336d760d 23246 case DW_FORM_addrx:
3019eac3 23247 case DW_FORM_GNU_addr_index:
d97bc12b 23248 fprintf_unfiltered (f, "address: ");
36d378cf 23249 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23250 break;
23251 case DW_FORM_block2:
23252 case DW_FORM_block4:
23253 case DW_FORM_block:
23254 case DW_FORM_block1:
56eb65bd 23255 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23256 pulongest (die->attrs[i].as_block ()->size));
c906108c 23257 break;
2dc7f7b3 23258 case DW_FORM_exprloc:
56eb65bd 23259 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23260 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23261 break;
0224619f
JK
23262 case DW_FORM_data16:
23263 fprintf_unfiltered (f, "constant of 16 bytes");
23264 break;
4568ecf9
DE
23265 case DW_FORM_ref_addr:
23266 fprintf_unfiltered (f, "ref address: ");
529908cb 23267 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23268 break;
36586728
TT
23269 case DW_FORM_GNU_ref_alt:
23270 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23271 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23272 break;
10b3939b
DJ
23273 case DW_FORM_ref1:
23274 case DW_FORM_ref2:
23275 case DW_FORM_ref4:
4568ecf9
DE
23276 case DW_FORM_ref8:
23277 case DW_FORM_ref_udata:
d97bc12b 23278 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23279 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23280 break;
c906108c
SS
23281 case DW_FORM_data1:
23282 case DW_FORM_data2:
23283 case DW_FORM_data4:
ce5d95e1 23284 case DW_FORM_data8:
c906108c 23285 case DW_FORM_udata:
43bbcdc2 23286 fprintf_unfiltered (f, "constant: %s",
529908cb 23287 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23288 break;
2dc7f7b3
TT
23289 case DW_FORM_sec_offset:
23290 fprintf_unfiltered (f, "section offset: %s",
529908cb 23291 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23292 break;
55f1336d 23293 case DW_FORM_ref_sig8:
ac9ec31b 23294 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23295 hex_string (die->attrs[i].as_signature ()));
348e048f 23296 break;
c906108c 23297 case DW_FORM_string:
4bdf3d34 23298 case DW_FORM_strp:
43988095 23299 case DW_FORM_line_strp:
cf532bd1 23300 case DW_FORM_strx:
3019eac3 23301 case DW_FORM_GNU_str_index:
36586728 23302 case DW_FORM_GNU_strp_alt:
8285870a 23303 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23304 die->attrs[i].as_string ()
23305 ? die->attrs[i].as_string () : "",
23306 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23307 break;
23308 case DW_FORM_flag:
c45bc3f8 23309 if (die->attrs[i].as_boolean ())
d97bc12b 23310 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23311 else
d97bc12b 23312 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23313 break;
2dc7f7b3
TT
23314 case DW_FORM_flag_present:
23315 fprintf_unfiltered (f, "flag: TRUE");
23316 break;
a8329558 23317 case DW_FORM_indirect:
0963b4bd
MS
23318 /* The reader will have reduced the indirect form to
23319 the "base form" so this form should not occur. */
5f48f8f3 23320 fprintf_unfiltered (f,
3e43a32a 23321 "unexpected attribute form: DW_FORM_indirect");
a8329558 23322 break;
1bc397c5 23323 case DW_FORM_sdata:
663c44ac
JK
23324 case DW_FORM_implicit_const:
23325 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23326 plongest (die->attrs[i].as_signed ()));
663c44ac 23327 break;
c906108c 23328 default:
d97bc12b 23329 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23330 die->attrs[i].form);
d97bc12b 23331 break;
c906108c 23332 }
d97bc12b 23333 fprintf_unfiltered (f, "\n");
c906108c
SS
23334 }
23335}
23336
f9aca02d 23337static void
d97bc12b 23338dump_die_for_error (struct die_info *die)
c906108c 23339{
d97bc12b
DE
23340 dump_die_shallow (gdb_stderr, 0, die);
23341}
23342
23343static void
23344dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23345{
23346 int indent = level * 4;
23347
23348 gdb_assert (die != NULL);
23349
23350 if (level >= max_level)
23351 return;
23352
23353 dump_die_shallow (f, indent, die);
23354
23355 if (die->child != NULL)
c906108c 23356 {
d97bc12b
DE
23357 print_spaces (indent, f);
23358 fprintf_unfiltered (f, " Children:");
23359 if (level + 1 < max_level)
23360 {
23361 fprintf_unfiltered (f, "\n");
23362 dump_die_1 (f, level + 1, max_level, die->child);
23363 }
23364 else
23365 {
3e43a32a
MS
23366 fprintf_unfiltered (f,
23367 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23368 }
23369 }
23370
23371 if (die->sibling != NULL && level > 0)
23372 {
23373 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23374 }
23375}
23376
d97bc12b
DE
23377/* This is called from the pdie macro in gdbinit.in.
23378 It's not static so gcc will keep a copy callable from gdb. */
23379
23380void
23381dump_die (struct die_info *die, int max_level)
23382{
23383 dump_die_1 (gdb_stdlog, 0, max_level, die);
23384}
23385
f9aca02d 23386static void
51545339 23387store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23388{
51545339 23389 void **slot;
c906108c 23390
9c541725
PA
23391 slot = htab_find_slot_with_hash (cu->die_hash, die,
23392 to_underlying (die->sect_off),
b64f50a1 23393 INSERT);
51545339
DJ
23394
23395 *slot = die;
c906108c
SS
23396}
23397
348e048f
DE
23398/* Follow reference or signature attribute ATTR of SRC_DIE.
23399 On entry *REF_CU is the CU of SRC_DIE.
23400 On exit *REF_CU is the CU of the result. */
23401
23402static struct die_info *
ff39bb5e 23403follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23404 struct dwarf2_cu **ref_cu)
23405{
23406 struct die_info *die;
23407
cd6c91b4 23408 if (attr->form_is_ref ())
348e048f 23409 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23410 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23411 die = follow_die_sig (src_die, attr, ref_cu);
23412 else
23413 {
23414 dump_die_for_error (src_die);
23415 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23416 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23417 }
23418
23419 return die;
03dd20cc
DJ
23420}
23421
5c631832 23422/* Follow reference OFFSET.
673bfd45
DE
23423 On entry *REF_CU is the CU of the source die referencing OFFSET.
23424 On exit *REF_CU is the CU of the result.
23425 Returns NULL if OFFSET is invalid. */
f504f079 23426
f9aca02d 23427static struct die_info *
9c541725 23428follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23429 struct dwarf2_cu **ref_cu)
c906108c 23430{
10b3939b 23431 struct die_info temp_die;
f2f0e013 23432 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23433 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23434
348e048f
DE
23435 gdb_assert (cu->per_cu != NULL);
23436
98bfdba5
PA
23437 target_cu = cu;
23438
17e593e9
SM
23439 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23440 "source CU contains target offset: %d",
23441 sect_offset_str (cu->per_cu->sect_off),
23442 sect_offset_str (sect_off),
23443 cu->header.offset_in_cu_p (sect_off));
23444
3019eac3 23445 if (cu->per_cu->is_debug_types)
348e048f
DE
23446 {
23447 /* .debug_types CUs cannot reference anything outside their CU.
23448 If they need to, they have to reference a signatured type via
55f1336d 23449 DW_FORM_ref_sig8. */
4057dfde 23450 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23451 return NULL;
348e048f 23452 }
36586728 23453 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23454 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23455 {
23456 struct dwarf2_per_cu_data *per_cu;
9a619af0 23457
9c541725 23458 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23459 per_objfile);
03dd20cc 23460
17e593e9
SM
23461 dwarf_read_debug_printf_v ("target CU offset: %s, "
23462 "target CU DIEs loaded: %d",
23463 sect_offset_str (per_cu->sect_off),
23464 per_objfile->get_cu (per_cu) != nullptr);
23465
616c069a
SM
23466 /* If necessary, add it to the queue and load its DIEs.
23467
23468 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23469 it doesn't mean they are currently loaded. Since we require them
23470 to be loaded, we must check for ourselves. */
23471 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23472 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23473 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23474 false, cu->language);
03dd20cc 23475
976ca316 23476 target_cu = per_objfile->get_cu (per_cu);
616c069a 23477 gdb_assert (target_cu != nullptr);
10b3939b 23478 }
98bfdba5
PA
23479 else if (cu->dies == NULL)
23480 {
23481 /* We're loading full DIEs during partial symbol reading. */
976ca316 23482 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23483 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23484 language_minimal);
98bfdba5 23485 }
c906108c 23486
f2f0e013 23487 *ref_cu = target_cu;
9c541725 23488 temp_die.sect_off = sect_off;
c24bdb02
KS
23489
23490 if (target_cu != cu)
23491 target_cu->ancestor = cu;
23492
9a3c8263 23493 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23494 &temp_die,
23495 to_underlying (sect_off));
5c631832 23496}
10b3939b 23497
5c631832
JK
23498/* Follow reference attribute ATTR of SRC_DIE.
23499 On entry *REF_CU is the CU of SRC_DIE.
23500 On exit *REF_CU is the CU of the result. */
23501
23502static struct die_info *
ff39bb5e 23503follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23504 struct dwarf2_cu **ref_cu)
23505{
0826b30a 23506 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23507 struct dwarf2_cu *cu = *ref_cu;
23508 struct die_info *die;
23509
9c541725 23510 die = follow_die_offset (sect_off,
36586728
TT
23511 (attr->form == DW_FORM_GNU_ref_alt
23512 || cu->per_cu->is_dwz),
23513 ref_cu);
5c631832 23514 if (!die)
9d8780f0
SM
23515 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23516 "at %s [in module %s]"),
23517 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23518 objfile_name (cu->per_objfile->objfile));
348e048f 23519
5c631832
JK
23520 return die;
23521}
23522
d4c9a4f8 23523/* See read.h. */
5c631832
JK
23524
23525struct dwarf2_locexpr_baton
9c541725 23526dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23527 dwarf2_per_cu_data *per_cu,
976ca316 23528 dwarf2_per_objfile *per_objfile,
041d9819
SM
23529 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23530 bool resolve_abstract_p)
5c631832 23531{
5c631832
JK
23532 struct die_info *die;
23533 struct attribute *attr;
23534 struct dwarf2_locexpr_baton retval;
976ca316 23535 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23536
976ca316 23537 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23538 if (cu == nullptr)
976ca316 23539 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23540
23541 if (cu == nullptr)
cc12ce38
DE
23542 {
23543 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23544 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23545 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23546 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23547 }
918dd910 23548
9c541725 23549 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23550 if (!die)
9d8780f0
SM
23551 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23552 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23553
23554 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23555 if (!attr && resolve_abstract_p
976ca316
SM
23556 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23557 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23558 {
041d9819 23559 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23560 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23561 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23562
3360b6e7 23563 for (const auto &cand_off
976ca316 23564 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23565 {
3360b6e7
TV
23566 struct dwarf2_cu *cand_cu = cu;
23567 struct die_info *cand
23568 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23569 if (!cand
23570 || !cand->parent
e4a62c65
TV
23571 || cand->parent->tag != DW_TAG_subprogram)
23572 continue;
23573
23574 CORE_ADDR pc_low, pc_high;
23575 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23576 if (pc_low == ((CORE_ADDR) -1))
23577 continue;
23578 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23579 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23580 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23581 continue;
23582
23583 die = cand;
23584 attr = dwarf2_attr (die, DW_AT_location, cu);
23585 break;
23586 }
23587 }
23588
5c631832
JK
23589 if (!attr)
23590 {
e103e986
JK
23591 /* DWARF: "If there is no such attribute, then there is no effect.".
23592 DATA is ignored if SIZE is 0. */
5c631832 23593
e103e986 23594 retval.data = NULL;
5c631832
JK
23595 retval.size = 0;
23596 }
cd6c91b4 23597 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23598 {
23599 struct dwarf2_loclist_baton loclist_baton;
041d9819 23600 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23601 size_t size;
23602
23603 fill_in_loclist_baton (cu, &loclist_baton, attr);
23604
23605 retval.data = dwarf2_find_location_expression (&loclist_baton,
23606 &size, pc);
23607 retval.size = size;
23608 }
5c631832
JK
23609 else
23610 {
4fc6c0d5 23611 if (!attr->form_is_block ())
9d8780f0 23612 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23613 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23614 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23615
9d2246fc
TT
23616 struct dwarf_block *block = attr->as_block ();
23617 retval.data = block->data;
23618 retval.size = block->size;
5c631832 23619 }
976ca316 23620 retval.per_objfile = per_objfile;
5c631832 23621 retval.per_cu = cu->per_cu;
918dd910 23622
976ca316 23623 per_objfile->age_comp_units ();
918dd910 23624
5c631832 23625 return retval;
348e048f
DE
23626}
23627
d4c9a4f8 23628/* See read.h. */
8b9737bf
TT
23629
23630struct dwarf2_locexpr_baton
23631dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23632 dwarf2_per_cu_data *per_cu,
14095eb3 23633 dwarf2_per_objfile *per_objfile,
041d9819 23634 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23635{
9c541725 23636 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23637
14095eb3 23638 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23639 get_frame_pc);
8b9737bf
TT
23640}
23641
b6807d98
TT
23642/* Write a constant of a given type as target-ordered bytes into
23643 OBSTACK. */
23644
23645static const gdb_byte *
23646write_constant_as_bytes (struct obstack *obstack,
23647 enum bfd_endian byte_order,
23648 struct type *type,
23649 ULONGEST value,
23650 LONGEST *len)
23651{
23652 gdb_byte *result;
23653
23654 *len = TYPE_LENGTH (type);
224c3ddb 23655 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23656 store_unsigned_integer (result, *len, byte_order, value);
23657
23658 return result;
23659}
23660
d4c9a4f8 23661/* See read.h. */
b6807d98
TT
23662
23663const gdb_byte *
9c541725 23664dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23665 dwarf2_per_cu_data *per_cu,
14095eb3 23666 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23667 obstack *obstack,
b6807d98
TT
23668 LONGEST *len)
23669{
b6807d98
TT
23670 struct die_info *die;
23671 struct attribute *attr;
23672 const gdb_byte *result = NULL;
23673 struct type *type;
23674 LONGEST value;
23675 enum bfd_endian byte_order;
14095eb3 23676 struct objfile *objfile = per_objfile->objfile;
b6807d98 23677
7188ed02 23678 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23679 if (cu == nullptr)
23680 cu = load_cu (per_cu, per_objfile, false);
23681
23682 if (cu == nullptr)
cc12ce38
DE
23683 {
23684 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23685 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23686 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23687 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23688 }
b6807d98 23689
9c541725 23690 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23691 if (!die)
9d8780f0
SM
23692 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23693 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23694
23695 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23696 if (attr == NULL)
23697 return NULL;
23698
e3b94546 23699 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23700 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23701
23702 switch (attr->form)
23703 {
23704 case DW_FORM_addr:
336d760d 23705 case DW_FORM_addrx:
b6807d98
TT
23706 case DW_FORM_GNU_addr_index:
23707 {
23708 gdb_byte *tem;
23709
23710 *len = cu->header.addr_size;
224c3ddb 23711 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23712 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23713 result = tem;
23714 }
23715 break;
23716 case DW_FORM_string:
23717 case DW_FORM_strp:
cf532bd1 23718 case DW_FORM_strx:
b6807d98
TT
23719 case DW_FORM_GNU_str_index:
23720 case DW_FORM_GNU_strp_alt:
c6481205 23721 /* The string is already allocated on the objfile obstack, point
b6807d98 23722 directly to it. */
2c830f54
TT
23723 {
23724 const char *attr_name = attr->as_string ();
23725 result = (const gdb_byte *) attr_name;
23726 *len = strlen (attr_name);
23727 }
b6807d98
TT
23728 break;
23729 case DW_FORM_block1:
23730 case DW_FORM_block2:
23731 case DW_FORM_block4:
23732 case DW_FORM_block:
23733 case DW_FORM_exprloc:
0224619f 23734 case DW_FORM_data16:
9d2246fc
TT
23735 {
23736 struct dwarf_block *block = attr->as_block ();
23737 result = block->data;
23738 *len = block->size;
23739 }
b6807d98
TT
23740 break;
23741
23742 /* The DW_AT_const_value attributes are supposed to carry the
23743 symbol's value "represented as it would be on the target
23744 architecture." By the time we get here, it's already been
23745 converted to host endianness, so we just need to sign- or
23746 zero-extend it as appropriate. */
23747 case DW_FORM_data1:
23748 type = die_type (die, cu);
23749 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23750 if (result == NULL)
23751 result = write_constant_as_bytes (obstack, byte_order,
23752 type, value, len);
23753 break;
23754 case DW_FORM_data2:
23755 type = die_type (die, cu);
23756 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23757 if (result == NULL)
23758 result = write_constant_as_bytes (obstack, byte_order,
23759 type, value, len);
23760 break;
23761 case DW_FORM_data4:
23762 type = die_type (die, cu);
23763 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23764 if (result == NULL)
23765 result = write_constant_as_bytes (obstack, byte_order,
23766 type, value, len);
23767 break;
23768 case DW_FORM_data8:
23769 type = die_type (die, cu);
23770 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23771 if (result == NULL)
23772 result = write_constant_as_bytes (obstack, byte_order,
23773 type, value, len);
23774 break;
23775
23776 case DW_FORM_sdata:
663c44ac 23777 case DW_FORM_implicit_const:
b6807d98
TT
23778 type = die_type (die, cu);
23779 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23780 type, attr->as_signed (), len);
b6807d98
TT
23781 break;
23782
23783 case DW_FORM_udata:
23784 type = die_type (die, cu);
23785 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23786 type, attr->as_unsigned (), len);
b6807d98
TT
23787 break;
23788
23789 default:
b98664d3 23790 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23791 dwarf_form_name (attr->form));
23792 break;
23793 }
23794
23795 return result;
23796}
23797
d4c9a4f8 23798/* See read.h. */
7942e96e
AA
23799
23800struct type *
9c541725 23801dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23802 dwarf2_per_cu_data *per_cu,
23803 dwarf2_per_objfile *per_objfile)
7942e96e 23804{
7942e96e
AA
23805 struct die_info *die;
23806
7188ed02 23807 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23808 if (cu == nullptr)
23809 cu = load_cu (per_cu, per_objfile, false);
23810
23811 if (cu == nullptr)
23812 return nullptr;
7942e96e 23813
9c541725 23814 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23815 if (!die)
23816 return NULL;
23817
23818 return die_type (die, cu);
23819}
23820
8cb5117c 23821/* See read.h. */
8a9b8146
TT
23822
23823struct type *
b64f50a1 23824dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23825 dwarf2_per_cu_data *per_cu,
23826 dwarf2_per_objfile *per_objfile)
8a9b8146 23827{
9c541725 23828 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23829 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23830}
23831
ac9ec31b 23832/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23833 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23834 On exit *REF_CU is the CU of the result.
23835 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23836
23837static struct die_info *
ac9ec31b
DE
23838follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23839 struct dwarf2_cu **ref_cu)
348e048f 23840{
348e048f 23841 struct die_info temp_die;
c24bdb02 23842 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 23843 struct die_info *die;
976ca316 23844 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23845
348e048f 23846
ac9ec31b
DE
23847 /* While it might be nice to assert sig_type->type == NULL here,
23848 we can get here for DW_AT_imported_declaration where we need
23849 the DIE not the type. */
348e048f 23850
616c069a 23851 /* If necessary, add it to the queue and load its DIEs.
348e048f 23852
616c069a
SM
23853 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23854 it doesn't mean they are currently loaded. Since we require them
23855 to be loaded, we must check for ourselves. */
976ca316 23856 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
616c069a
SM
23857 language_minimal)
23858 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
976ca316 23859 read_signatured_type (sig_type, per_objfile);
348e048f 23860
976ca316 23861 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 23862 gdb_assert (sig_cu != NULL);
9c541725
PA
23863 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23864 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23865 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23866 to_underlying (temp_die.sect_off));
348e048f
DE
23867 if (die)
23868 {
796a7ff8
DE
23869 /* For .gdb_index version 7 keep track of included TUs.
23870 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23871 if (per_objfile->per_bfd->index_table != NULL
23872 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23873 {
ae640021 23874 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23875 }
23876
348e048f 23877 *ref_cu = sig_cu;
c24bdb02
KS
23878 if (sig_cu != cu)
23879 sig_cu->ancestor = cu;
23880
348e048f
DE
23881 return die;
23882 }
23883
ac9ec31b
DE
23884 return NULL;
23885}
23886
23887/* Follow signatured type referenced by ATTR in SRC_DIE.
23888 On entry *REF_CU is the CU of SRC_DIE.
23889 On exit *REF_CU is the CU of the result.
23890 The result is the DIE of the type.
23891 If the referenced type cannot be found an error is thrown. */
23892
23893static struct die_info *
ff39bb5e 23894follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23895 struct dwarf2_cu **ref_cu)
23896{
630ed6b9 23897 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23898 struct signatured_type *sig_type;
23899 struct die_info *die;
23900
23901 gdb_assert (attr->form == DW_FORM_ref_sig8);
23902
a2ce51a0 23903 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23904 /* sig_type will be NULL if the signatured type is missing from
23905 the debug info. */
23906 if (sig_type == NULL)
23907 {
23908 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
23909 " from DIE at %s [in module %s]"),
23910 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23911 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23912 }
23913
23914 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23915 if (die == NULL)
23916 {
23917 dump_die_for_error (src_die);
23918 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23919 " from DIE at %s [in module %s]"),
23920 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23921 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23922 }
23923
23924 return die;
23925}
23926
23927/* Get the type specified by SIGNATURE referenced in DIE/CU,
23928 reading in and processing the type unit if necessary. */
23929
23930static struct type *
23931get_signatured_type (struct die_info *die, ULONGEST signature,
23932 struct dwarf2_cu *cu)
23933{
976ca316 23934 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
23935 struct signatured_type *sig_type;
23936 struct dwarf2_cu *type_cu;
23937 struct die_info *type_die;
23938 struct type *type;
23939
a2ce51a0 23940 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23941 /* sig_type will be NULL if the signatured type is missing from
23942 the debug info. */
23943 if (sig_type == NULL)
23944 {
b98664d3 23945 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23946 " from DIE at %s [in module %s]"),
23947 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23948 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23949 return build_error_marker_type (cu, die);
23950 }
23951
23952 /* If we already know the type we're done. */
976ca316 23953 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
23954 if (type != nullptr)
23955 return type;
ac9ec31b
DE
23956
23957 type_cu = cu;
23958 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23959 if (type_die != NULL)
23960 {
23961 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23962 is created. This is important, for example, because for c++ classes
23963 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23964 type = read_type_die (type_die, type_cu);
23965 if (type == NULL)
23966 {
b98664d3 23967 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23968 " referenced from DIE at %s [in module %s]"),
23969 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23970 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23971 type = build_error_marker_type (cu, die);
23972 }
23973 }
23974 else
23975 {
b98664d3 23976 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23977 " from DIE at %s [in module %s]"),
23978 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23979 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23980 type = build_error_marker_type (cu, die);
23981 }
e286671b 23982
976ca316 23983 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
23984
23985 return type;
23986}
23987
23988/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23989 reading in and processing the type unit if necessary. */
23990
23991static struct type *
ff39bb5e 23992get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23993 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23994{
23995 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23996 if (attr->form_is_ref ())
ac9ec31b
DE
23997 {
23998 struct dwarf2_cu *type_cu = cu;
23999 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24000
24001 return read_type_die (type_die, type_cu);
24002 }
24003 else if (attr->form == DW_FORM_ref_sig8)
24004 {
630ed6b9 24005 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
24006 }
24007 else
24008 {
976ca316 24009 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24010
b98664d3 24011 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24012 " at %s [in module %s]"),
24013 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 24014 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24015 return build_error_marker_type (cu, die);
24016 }
348e048f
DE
24017}
24018
e5fe5e75 24019/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24020
24021static void
ab432490
SM
24022load_full_type_unit (dwarf2_per_cu_data *per_cu,
24023 dwarf2_per_objfile *per_objfile)
348e048f 24024{
52dc124a 24025 struct signatured_type *sig_type;
348e048f 24026
f4dc4d17 24027 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24028 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24029
6721b2ec
DE
24030 /* We have the per_cu, but we need the signatured_type.
24031 Fortunately this is an easy translation. */
24032 gdb_assert (per_cu->is_debug_types);
24033 sig_type = (struct signatured_type *) per_cu;
348e048f 24034
7188ed02 24035 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24036
ab432490 24037 read_signatured_type (sig_type, per_objfile);
348e048f 24038
7188ed02 24039 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24040}
24041
3019eac3
DE
24042/* Read in a signatured type and build its CU and DIEs.
24043 If the type is a stub for the real type in a DWO file,
24044 read in the real type from the DWO file as well. */
dee91e82
DE
24045
24046static void
ab432490
SM
24047read_signatured_type (signatured_type *sig_type,
24048 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24049{
24050 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24051
3019eac3 24052 gdb_assert (per_cu->is_debug_types);
7188ed02 24053 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24054
2e671100 24055 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24056
24057 if (!reader.dummy_p)
24058 {
24059 struct dwarf2_cu *cu = reader.cu;
24060 const gdb_byte *info_ptr = reader.info_ptr;
24061
24062 gdb_assert (cu->die_hash == NULL);
24063 cu->die_hash =
24064 htab_create_alloc_ex (cu->header.length / 12,
24065 die_hash,
24066 die_eq,
24067 NULL,
24068 &cu->comp_unit_obstack,
24069 hashtab_obstack_allocate,
24070 dummy_obstack_deallocate);
24071
3e225074 24072 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24073 reader.comp_unit_die->child
24074 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24075 reader.comp_unit_die);
24076 cu->dies = reader.comp_unit_die;
24077 /* comp_unit_die is not stored in die_hash, no need. */
24078
24079 /* We try not to read any attributes in this function, because
24080 not all CUs needed for references have been loaded yet, and
24081 symbol table processing isn't initialized. But we have to
24082 set the CU language, or we won't be able to build types
24083 correctly. Similarly, if we do not read the producer, we can
24084 not apply producer-specific interpretation. */
24085 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24086
24087 reader.keep ();
c0ab21c2
TT
24088 }
24089
7ee85ab1 24090 sig_type->per_cu.tu_read = 1;
c906108c
SS
24091}
24092
c906108c
SS
24093/* Decode simple location descriptions.
24094 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24095 the location and return the value. If COMPUTED is non-null, it is
24096 set to true to indicate that decoding was successful, and false
24097 otherwise. If COMPUTED is null, then this function may emit a
24098 complaint. */
c906108c
SS
24099
24100static CORE_ADDR
7d79de9a 24101decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24102{
5e22e966 24103 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24104 size_t i;
24105 size_t size = blk->size;
d521ce57 24106 const gdb_byte *data = blk->data;
21ae7a4d
JK
24107 CORE_ADDR stack[64];
24108 int stacki;
24109 unsigned int bytes_read, unsnd;
24110 gdb_byte op;
c906108c 24111
7d79de9a
TT
24112 if (computed != nullptr)
24113 *computed = false;
24114
21ae7a4d
JK
24115 i = 0;
24116 stacki = 0;
24117 stack[stacki] = 0;
24118 stack[++stacki] = 0;
24119
24120 while (i < size)
24121 {
24122 op = data[i++];
24123 switch (op)
24124 {
24125 case DW_OP_lit0:
24126 case DW_OP_lit1:
24127 case DW_OP_lit2:
24128 case DW_OP_lit3:
24129 case DW_OP_lit4:
24130 case DW_OP_lit5:
24131 case DW_OP_lit6:
24132 case DW_OP_lit7:
24133 case DW_OP_lit8:
24134 case DW_OP_lit9:
24135 case DW_OP_lit10:
24136 case DW_OP_lit11:
24137 case DW_OP_lit12:
24138 case DW_OP_lit13:
24139 case DW_OP_lit14:
24140 case DW_OP_lit15:
24141 case DW_OP_lit16:
24142 case DW_OP_lit17:
24143 case DW_OP_lit18:
24144 case DW_OP_lit19:
24145 case DW_OP_lit20:
24146 case DW_OP_lit21:
24147 case DW_OP_lit22:
24148 case DW_OP_lit23:
24149 case DW_OP_lit24:
24150 case DW_OP_lit25:
24151 case DW_OP_lit26:
24152 case DW_OP_lit27:
24153 case DW_OP_lit28:
24154 case DW_OP_lit29:
24155 case DW_OP_lit30:
24156 case DW_OP_lit31:
24157 stack[++stacki] = op - DW_OP_lit0;
24158 break;
f1bea926 24159
21ae7a4d
JK
24160 case DW_OP_reg0:
24161 case DW_OP_reg1:
24162 case DW_OP_reg2:
24163 case DW_OP_reg3:
24164 case DW_OP_reg4:
24165 case DW_OP_reg5:
24166 case DW_OP_reg6:
24167 case DW_OP_reg7:
24168 case DW_OP_reg8:
24169 case DW_OP_reg9:
24170 case DW_OP_reg10:
24171 case DW_OP_reg11:
24172 case DW_OP_reg12:
24173 case DW_OP_reg13:
24174 case DW_OP_reg14:
24175 case DW_OP_reg15:
24176 case DW_OP_reg16:
24177 case DW_OP_reg17:
24178 case DW_OP_reg18:
24179 case DW_OP_reg19:
24180 case DW_OP_reg20:
24181 case DW_OP_reg21:
24182 case DW_OP_reg22:
24183 case DW_OP_reg23:
24184 case DW_OP_reg24:
24185 case DW_OP_reg25:
24186 case DW_OP_reg26:
24187 case DW_OP_reg27:
24188 case DW_OP_reg28:
24189 case DW_OP_reg29:
24190 case DW_OP_reg30:
24191 case DW_OP_reg31:
24192 stack[++stacki] = op - DW_OP_reg0;
24193 if (i < size)
7d79de9a
TT
24194 {
24195 if (computed == nullptr)
24196 dwarf2_complex_location_expr_complaint ();
24197 else
24198 return 0;
24199 }
21ae7a4d 24200 break;
c906108c 24201
21ae7a4d
JK
24202 case DW_OP_regx:
24203 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24204 i += bytes_read;
24205 stack[++stacki] = unsnd;
24206 if (i < size)
7d79de9a
TT
24207 {
24208 if (computed == nullptr)
24209 dwarf2_complex_location_expr_complaint ();
24210 else
24211 return 0;
24212 }
21ae7a4d 24213 break;
c906108c 24214
21ae7a4d 24215 case DW_OP_addr:
c8a7a66f
TT
24216 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24217 &bytes_read);
21ae7a4d
JK
24218 i += bytes_read;
24219 break;
d53d4ac5 24220
21ae7a4d
JK
24221 case DW_OP_const1u:
24222 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24223 i += 1;
24224 break;
24225
24226 case DW_OP_const1s:
24227 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24228 i += 1;
24229 break;
24230
24231 case DW_OP_const2u:
24232 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24233 i += 2;
24234 break;
24235
24236 case DW_OP_const2s:
24237 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24238 i += 2;
24239 break;
d53d4ac5 24240
21ae7a4d
JK
24241 case DW_OP_const4u:
24242 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24243 i += 4;
24244 break;
24245
24246 case DW_OP_const4s:
24247 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24248 i += 4;
24249 break;
24250
585861ea
JK
24251 case DW_OP_const8u:
24252 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24253 i += 8;
24254 break;
24255
21ae7a4d
JK
24256 case DW_OP_constu:
24257 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24258 &bytes_read);
24259 i += bytes_read;
24260 break;
24261
24262 case DW_OP_consts:
24263 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24264 i += bytes_read;
24265 break;
24266
24267 case DW_OP_dup:
24268 stack[stacki + 1] = stack[stacki];
24269 stacki++;
24270 break;
24271
24272 case DW_OP_plus:
24273 stack[stacki - 1] += stack[stacki];
24274 stacki--;
24275 break;
24276
24277 case DW_OP_plus_uconst:
24278 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24279 &bytes_read);
24280 i += bytes_read;
24281 break;
24282
24283 case DW_OP_minus:
24284 stack[stacki - 1] -= stack[stacki];
24285 stacki--;
24286 break;
24287
24288 case DW_OP_deref:
24289 /* If we're not the last op, then we definitely can't encode
24290 this using GDB's address_class enum. This is valid for partial
24291 global symbols, although the variable's address will be bogus
24292 in the psymtab. */
24293 if (i < size)
7d79de9a
TT
24294 {
24295 if (computed == nullptr)
24296 dwarf2_complex_location_expr_complaint ();
24297 else
24298 return 0;
24299 }
21ae7a4d
JK
24300 break;
24301
dda83cd7 24302 case DW_OP_GNU_push_tls_address:
4aa4e28b 24303 case DW_OP_form_tls_address:
21ae7a4d
JK
24304 /* The top of the stack has the offset from the beginning
24305 of the thread control block at which the variable is located. */
24306 /* Nothing should follow this operator, so the top of stack would
24307 be returned. */
24308 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24309 address will be bogus in the psymtab. Make it always at least
24310 non-zero to not look as a variable garbage collected by linker
24311 which have DW_OP_addr 0. */
21ae7a4d 24312 if (i < size)
7d79de9a
TT
24313 {
24314 if (computed == nullptr)
24315 dwarf2_complex_location_expr_complaint ();
24316 else
24317 return 0;
24318 }
585861ea 24319 stack[stacki]++;
dda83cd7 24320 break;
21ae7a4d
JK
24321
24322 case DW_OP_GNU_uninit:
7d79de9a
TT
24323 if (computed != nullptr)
24324 return 0;
21ae7a4d
JK
24325 break;
24326
336d760d 24327 case DW_OP_addrx:
3019eac3 24328 case DW_OP_GNU_addr_index:
49f6c839 24329 case DW_OP_GNU_const_index:
3019eac3
DE
24330 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24331 &bytes_read);
24332 i += bytes_read;
24333 break;
24334
21ae7a4d 24335 default:
7d79de9a
TT
24336 if (computed == nullptr)
24337 {
24338 const char *name = get_DW_OP_name (op);
21ae7a4d 24339
7d79de9a
TT
24340 if (name)
24341 complaint (_("unsupported stack op: '%s'"),
24342 name);
24343 else
24344 complaint (_("unsupported stack op: '%02x'"),
24345 op);
24346 }
21ae7a4d
JK
24347
24348 return (stack[stacki]);
d53d4ac5 24349 }
3c6e0cb3 24350
21ae7a4d 24351 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24352 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24353 if (stacki >= ARRAY_SIZE (stack) - 1)
24354 {
7d79de9a
TT
24355 if (computed == nullptr)
24356 complaint (_("location description stack overflow"));
21ae7a4d
JK
24357 return 0;
24358 }
24359
24360 if (stacki <= 0)
24361 {
7d79de9a
TT
24362 if (computed == nullptr)
24363 complaint (_("location description stack underflow"));
21ae7a4d
JK
24364 return 0;
24365 }
24366 }
7d79de9a
TT
24367
24368 if (computed != nullptr)
24369 *computed = true;
21ae7a4d 24370 return (stack[stacki]);
c906108c
SS
24371}
24372
24373/* memory allocation interface */
24374
c906108c 24375static struct dwarf_block *
7b5a2f43 24376dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24377{
8d749320 24378 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24379}
24380
c906108c 24381static struct die_info *
b60c80d6 24382dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24383{
24384 struct die_info *die;
b60c80d6
DJ
24385 size_t size = sizeof (struct die_info);
24386
24387 if (num_attrs > 1)
24388 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24389
b60c80d6 24390 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24391 memset (die, 0, sizeof (struct die_info));
24392 return (die);
24393}
2e276125
JB
24394
24395\f
a036ba48 24396
c90ec28a 24397/* Macro support. */
cf2c3c16 24398
9eac9650
TT
24399/* An overload of dwarf_decode_macros that finds the correct section
24400 and ensures it is read in before calling the other overload. */
24401
24402static void
24403dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24404 int section_is_gnu)
24405{
976ca316
SM
24406 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24407 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24408 const struct line_header *lh = cu->line_header;
9eac9650
TT
24409 unsigned int offset_size = cu->header.offset_size;
24410 struct dwarf2_section_info *section;
24411 const char *section_name;
24412
24413 if (cu->dwo_unit != nullptr)
24414 {
24415 if (section_is_gnu)
24416 {
24417 section = &cu->dwo_unit->dwo_file->sections.macro;
24418 section_name = ".debug_macro.dwo";
24419 }
24420 else
24421 {
24422 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24423 section_name = ".debug_macinfo.dwo";
24424 }
24425 }
24426 else
24427 {
24428 if (section_is_gnu)
24429 {
976ca316 24430 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24431 section_name = ".debug_macro";
24432 }
24433 else
24434 {
976ca316 24435 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24436 section_name = ".debug_macinfo";
24437 }
24438 }
24439
24440 section->read (objfile);
24441 if (section->buffer == nullptr)
24442 {
24443 complaint (_("missing %s section"), section_name);
24444 return;
24445 }
24446
24447 buildsym_compunit *builder = cu->get_builder ();
24448
048fde1e 24449 struct dwarf2_section_info *str_offsets_section;
24450 struct dwarf2_section_info *str_section;
24451 ULONGEST str_offsets_base;
24452
24453 if (cu->dwo_unit != nullptr)
24454 {
24455 str_offsets_section = &cu->dwo_unit->dwo_file
24456 ->sections.str_offsets;
24457 str_section = &cu->dwo_unit->dwo_file->sections.str;
24458 str_offsets_base = cu->header.addr_size;
24459 }
24460 else
24461 {
24462 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24463 str_section = &per_objfile->per_bfd->str;
24464 str_offsets_base = *cu->str_offsets_base;
24465 }
24466
976ca316 24467 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24468 offset_size, offset, str_section, str_offsets_section,
24469 str_offsets_base, section_is_gnu);
9eac9650
TT
24470}
24471
3019eac3
DE
24472/* Return the .debug_loc section to use for CU.
24473 For DWO files use .debug_loc.dwo. */
24474
24475static struct dwarf2_section_info *
24476cu_debug_loc_section (struct dwarf2_cu *cu)
24477{
976ca316 24478 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24479
3019eac3 24480 if (cu->dwo_unit)
43988095
JK
24481 {
24482 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24483
43988095
JK
24484 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24485 }
976ca316
SM
24486 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24487 : &per_objfile->per_bfd->loc);
3019eac3
DE
24488}
24489
d0ce17d8
CT
24490/* Return the .debug_rnglists section to use for CU. */
24491static struct dwarf2_section_info *
24492cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24493{
24494 if (cu->header.version < 5)
24495 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24496 cu->header.version);
24497 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24498
24499 /* Make sure we read the .debug_rnglists section from the file that
24500 contains the DW_AT_ranges attribute we are reading. Normally that
24501 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24502 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24503 program. */
24504 if (cu->dwo_unit != nullptr
24505 && tag != DW_TAG_compile_unit
24506 && tag != DW_TAG_skeleton_unit)
24507 {
24508 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24509
24510 if (sections->rnglists.size > 0)
24511 return &sections->rnglists;
24512 else
24513 error (_(".debug_rnglists section is missing from .dwo file."));
24514 }
24515 return &dwarf2_per_objfile->per_bfd->rnglists;
24516}
24517
8cf6f0b1
TT
24518/* A helper function that fills in a dwarf2_loclist_baton. */
24519
24520static void
24521fill_in_loclist_baton (struct dwarf2_cu *cu,
24522 struct dwarf2_loclist_baton *baton,
ff39bb5e 24523 const struct attribute *attr)
8cf6f0b1 24524{
976ca316 24525 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24526 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24527
976ca316 24528 section->read (per_objfile->objfile);
8cf6f0b1 24529
976ca316 24530 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24531 baton->per_cu = cu->per_cu;
24532 gdb_assert (baton->per_cu);
24533 /* We don't know how long the location list is, but make sure we
24534 don't run off the edge of the section. */
d4df075e
TT
24535 baton->size = section->size - attr->as_unsigned ();
24536 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24537 if (cu->base_address.has_value ())
24538 baton->base_address = *cu->base_address;
24539 else
24540 baton->base_address = 0;
f664829e 24541 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24542}
24543
4c2df51b 24544static void
ff39bb5e 24545dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24546 struct dwarf2_cu *cu, int is_block)
4c2df51b 24547{
976ca316
SM
24548 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24549 struct objfile *objfile = per_objfile->objfile;
3019eac3 24550 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24551
cd6c91b4 24552 if (attr->form_is_section_offset ()
3019eac3 24553 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24554 the section. If so, fall through to the complaint in the
24555 other branch. */
d4df075e 24556 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24557 {
0d53c4c4 24558 struct dwarf2_loclist_baton *baton;
4c2df51b 24559
8d749320 24560 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24561
8cf6f0b1 24562 fill_in_loclist_baton (cu, baton, attr);
be391dca 24563
2b24b6e4 24564 if (!cu->base_address.has_value ())
b98664d3 24565 complaint (_("Location list used without "
3e43a32a 24566 "specifying the CU base address."));
4c2df51b 24567
f1e6e072
TT
24568 SYMBOL_ACLASS_INDEX (sym) = (is_block
24569 ? dwarf2_loclist_block_index
24570 : dwarf2_loclist_index);
0d53c4c4
DJ
24571 SYMBOL_LOCATION_BATON (sym) = baton;
24572 }
24573 else
24574 {
24575 struct dwarf2_locexpr_baton *baton;
24576
8d749320 24577 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24578 baton->per_objfile = per_objfile;
ae0d2f24
UW
24579 baton->per_cu = cu->per_cu;
24580 gdb_assert (baton->per_cu);
0d53c4c4 24581
4fc6c0d5 24582 if (attr->form_is_block ())
0d53c4c4
DJ
24583 {
24584 /* Note that we're just copying the block's data pointer
24585 here, not the actual data. We're still pointing into the
6502dd73
DJ
24586 info_buffer for SYM's objfile; right now we never release
24587 that buffer, but when we do clean up properly this may
24588 need to change. */
9d2246fc
TT
24589 struct dwarf_block *block = attr->as_block ();
24590 baton->size = block->size;
24591 baton->data = block->data;
0d53c4c4
DJ
24592 }
24593 else
24594 {
24595 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24596 sym->natural_name ());
0d53c4c4 24597 baton->size = 0;
0d53c4c4 24598 }
6e70227d 24599
f1e6e072
TT
24600 SYMBOL_ACLASS_INDEX (sym) = (is_block
24601 ? dwarf2_locexpr_block_index
24602 : dwarf2_locexpr_index);
0d53c4c4
DJ
24603 SYMBOL_LOCATION_BATON (sym) = baton;
24604 }
4c2df51b 24605}
6502dd73 24606
2e6a9f79 24607/* See read.h. */
96408a79 24608
2e6a9f79
SM
24609const comp_unit_head *
24610dwarf2_per_cu_data::get_header () const
96408a79 24611{
2e6a9f79
SM
24612 if (!m_header_read_in)
24613 {
24614 const gdb_byte *info_ptr
24615 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24616
2e6a9f79 24617 memset (&m_header, 0, sizeof (m_header));
96408a79 24618
2e6a9f79
SM
24619 read_comp_unit_head (&m_header, info_ptr, this->section,
24620 rcuh_kind::COMPILE);
a9f172c6
TT
24621
24622 m_header_read_in = true;
2e6a9f79 24623 }
96408a79 24624
2e6a9f79 24625 return &m_header;
96408a79
SA
24626}
24627
09ba997f 24628/* See read.h. */
ae0d2f24 24629
98714339 24630int
09ba997f 24631dwarf2_per_cu_data::addr_size () const
ae0d2f24 24632{
2e6a9f79 24633 return this->get_header ()->addr_size;
ae0d2f24
UW
24634}
24635
09ba997f 24636/* See read.h. */
9eae7c52
TT
24637
24638int
09ba997f 24639dwarf2_per_cu_data::offset_size () const
9eae7c52 24640{
2e6a9f79 24641 return this->get_header ()->offset_size;
96408a79
SA
24642}
24643
09ba997f 24644/* See read.h. */
96408a79
SA
24645
24646int
09ba997f 24647dwarf2_per_cu_data::ref_addr_size () const
96408a79 24648{
2e6a9f79 24649 const comp_unit_head *header = this->get_header ();
96408a79 24650
2e6a9f79
SM
24651 if (header->version == 2)
24652 return header->addr_size;
96408a79 24653 else
2e6a9f79 24654 return header->offset_size;
181cebd4
JK
24655}
24656
09ba997f 24657/* See read.h. */
9aa1f1e3 24658
09ba997f 24659struct type *
293e7e51 24660dwarf2_cu::addr_type () const
9a49df9d 24661{
293e7e51 24662 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24663 struct type *void_type = objfile_type (objfile)->builtin_void;
24664 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24665 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24666
24667 if (TYPE_LENGTH (addr_type) == addr_size)
24668 return addr_type;
24669
c6d940a9 24670 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24671 return addr_type;
24672}
24673
22b6cd70
TT
24674/* A helper function for dwarf2_find_containing_comp_unit that returns
24675 the index of the result, and that searches a vector. It will
24676 return a result even if the offset in question does not actually
24677 occur in any CU. This is separate so that it can be unit
24678 tested. */
ae038cb0 24679
22b6cd70
TT
24680static int
24681dwarf2_find_containing_comp_unit
24682 (sect_offset sect_off,
24683 unsigned int offset_in_dwz,
24684 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24685{
ae038cb0
DJ
24686 int low, high;
24687
ae038cb0 24688 low = 0;
22b6cd70 24689 high = all_comp_units.size () - 1;
ae038cb0
DJ
24690 while (high > low)
24691 {
36586728 24692 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24693 int mid = low + (high - low) / 2;
9a619af0 24694
22b6cd70 24695 mid_cu = all_comp_units[mid];
36586728 24696 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24697 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24698 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24699 high = mid;
24700 else
24701 low = mid + 1;
24702 }
24703 gdb_assert (low == high);
22b6cd70
TT
24704 return low;
24705}
24706
24707/* Locate the .debug_info compilation unit from CU's objfile which contains
24708 the DIE at OFFSET. Raises an error on failure. */
24709
24710static struct dwarf2_per_cu_data *
24711dwarf2_find_containing_comp_unit (sect_offset sect_off,
24712 unsigned int offset_in_dwz,
976ca316 24713 dwarf2_per_objfile *per_objfile)
22b6cd70 24714{
976ca316
SM
24715 int low = dwarf2_find_containing_comp_unit
24716 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24717 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24718
45b8ae0c 24719 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24720 {
36586728 24721 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24722 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24723 "offset %s [in module %s]"),
24724 sect_offset_str (sect_off),
976ca316 24725 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24726
976ca316 24727 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24728 <= sect_off);
976ca316 24729 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
24730 }
24731 else
24732 {
976ca316 24733 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24734 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24735 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24736 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24737 return this_cu;
24738 }
24739}
24740
22b6cd70
TT
24741#if GDB_SELF_TEST
24742
24743namespace selftests {
24744namespace find_containing_comp_unit {
24745
24746static void
24747run_test ()
24748{
24749 struct dwarf2_per_cu_data one {};
24750 struct dwarf2_per_cu_data two {};
24751 struct dwarf2_per_cu_data three {};
24752 struct dwarf2_per_cu_data four {};
24753
24754 one.length = 5;
24755 two.sect_off = sect_offset (one.length);
24756 two.length = 7;
24757
24758 three.length = 5;
24759 three.is_dwz = 1;
24760 four.sect_off = sect_offset (three.length);
24761 four.length = 7;
24762 four.is_dwz = 1;
24763
24764 std::vector<dwarf2_per_cu_data *> units;
24765 units.push_back (&one);
24766 units.push_back (&two);
24767 units.push_back (&three);
24768 units.push_back (&four);
24769
24770 int result;
24771
24772 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24773 SELF_CHECK (units[result] == &one);
24774 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24775 SELF_CHECK (units[result] == &one);
24776 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24777 SELF_CHECK (units[result] == &two);
24778
24779 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24780 SELF_CHECK (units[result] == &three);
24781 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24782 SELF_CHECK (units[result] == &three);
24783 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24784 SELF_CHECK (units[result] == &four);
24785}
24786
24787}
24788}
24789
24790#endif /* GDB_SELF_TEST */
24791
9e021579 24792/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 24793
9e021579
SM
24794dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24795 dwarf2_per_objfile *per_objfile)
24796 : per_cu (per_cu),
24797 per_objfile (per_objfile),
9068261f
AB
24798 mark (false),
24799 has_loclist (false),
24800 checked_producer (false),
24801 producer_is_gxx_lt_4_6 (false),
24802 producer_is_gcc_lt_4_3 (false),
eb77c9df 24803 producer_is_icc (false),
9068261f 24804 producer_is_icc_lt_14 (false),
c258c396 24805 producer_is_codewarrior (false),
9068261f 24806 processing_has_namespace_info (false)
93311388 24807{
9816fde3
JK
24808}
24809
24810/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24811
24812static void
95554aad
TT
24813prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24814 enum language pretend_language)
9816fde3
JK
24815{
24816 struct attribute *attr;
24817
24818 /* Set the language we're debugging. */
24819 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24820 if (attr != nullptr)
529908cb 24821 set_cu_language (attr->constant_value (0), cu);
9816fde3 24822 else
9cded63f 24823 {
95554aad 24824 cu->language = pretend_language;
9cded63f
TT
24825 cu->language_defn = language_def (cu->language);
24826 }
dee91e82 24827
7d45c7c3 24828 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24829}
24830
7188ed02 24831/* See read.h. */
ae038cb0 24832
7188ed02
SM
24833dwarf2_cu *
24834dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24835{
7188ed02
SM
24836 auto it = m_dwarf2_cus.find (per_cu);
24837 if (it == m_dwarf2_cus.end ())
24838 return nullptr;
ae038cb0 24839
7188ed02
SM
24840 return it->second;
24841}
24842
24843/* See read.h. */
24844
24845void
24846dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24847{
24848 gdb_assert (this->get_cu (per_cu) == nullptr);
24849
24850 m_dwarf2_cus[per_cu] = cu;
24851}
24852
24853/* See read.h. */
24854
24855void
24856dwarf2_per_objfile::age_comp_units ()
24857{
17e593e9
SM
24858 dwarf_read_debug_printf_v ("running");
24859
08ac5771
SM
24860 /* This is not expected to be called in the middle of CU expansion. There is
24861 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24862 loaded in memory. Calling age_comp_units while the queue is in use could
24863 make us free the DIEs for a CU that is in the queue and therefore break
24864 that invariant. */
24865 gdb_assert (!this->per_bfd->queue.has_value ());
24866
7188ed02
SM
24867 /* Start by clearing all marks. */
24868 for (auto pair : m_dwarf2_cus)
24869 pair.second->mark = false;
24870
24871 /* Traverse all CUs, mark them and their dependencies if used recently
24872 enough. */
24873 for (auto pair : m_dwarf2_cus)
ae038cb0 24874 {
7188ed02
SM
24875 dwarf2_cu *cu = pair.second;
24876
24877 cu->last_used++;
24878 if (cu->last_used <= dwarf_max_cache_age)
24879 dwarf2_mark (cu);
ae038cb0
DJ
24880 }
24881
7188ed02
SM
24882 /* Delete all CUs still not marked. */
24883 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24884 {
7188ed02 24885 dwarf2_cu *cu = it->second;
ae038cb0 24886
7188ed02 24887 if (!cu->mark)
ae038cb0 24888 {
17e593e9
SM
24889 dwarf_read_debug_printf_v ("deleting old CU %s",
24890 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
24891 delete cu;
24892 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24893 }
24894 else
7188ed02 24895 it++;
ae038cb0
DJ
24896 }
24897}
24898
7188ed02 24899/* See read.h. */
ae038cb0 24900
7188ed02
SM
24901void
24902dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24903{
7188ed02
SM
24904 auto it = m_dwarf2_cus.find (per_cu);
24905 if (it == m_dwarf2_cus.end ())
24906 return;
ae038cb0 24907
7188ed02 24908 delete it->second;
ae038cb0 24909
7188ed02
SM
24910 m_dwarf2_cus.erase (it);
24911}
ae038cb0 24912
7188ed02
SM
24913dwarf2_per_objfile::~dwarf2_per_objfile ()
24914{
24915 remove_all_cus ();
ae038cb0
DJ
24916}
24917
dee91e82
DE
24918/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24919 We store these in a hash table separate from the DIEs, and preserve them
24920 when the DIEs are flushed out of cache.
24921
24922 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24923 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24924 or the type may come from a DWO file. Furthermore, while it's more logical
24925 to use per_cu->section+offset, with Fission the section with the data is in
24926 the DWO file but we don't know that section at the point we need it.
24927 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24928 because we can enter the lookup routine, get_die_type_at_offset, from
24929 outside this file, and thus won't necessarily have PER_CU->cu.
24930 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24931
dee91e82 24932struct dwarf2_per_cu_offset_and_type
1c379e20 24933{
dee91e82 24934 const struct dwarf2_per_cu_data *per_cu;
9c541725 24935 sect_offset sect_off;
1c379e20
DJ
24936 struct type *type;
24937};
24938
dee91e82 24939/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24940
24941static hashval_t
dee91e82 24942per_cu_offset_and_type_hash (const void *item)
1c379e20 24943{
9a3c8263
SM
24944 const struct dwarf2_per_cu_offset_and_type *ofs
24945 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24946
9c541725 24947 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24948}
24949
dee91e82 24950/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24951
24952static int
dee91e82 24953per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24954{
9a3c8263
SM
24955 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24956 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24957 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24958 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24959
dee91e82 24960 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24961 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24962}
24963
24964/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24965 table if necessary. For convenience, return TYPE.
24966
24967 The DIEs reading must have careful ordering to:
85102364 24968 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24969 reading current DIE.
24970 * Not trying to dereference contents of still incompletely read in types
24971 while reading in other DIEs.
24972 * Enable referencing still incompletely read in types just by a pointer to
24973 the type without accessing its fields.
24974
24975 Therefore caller should follow these rules:
24976 * Try to fetch any prerequisite types we may need to build this DIE type
24977 before building the type and calling set_die_type.
e71ec853 24978 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24979 possible before fetching more types to complete the current type.
24980 * Make the type as complete as possible before fetching more types. */
1c379e20 24981
f792889a 24982static struct type *
57567375
TT
24983set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24984 bool skip_data_location)
1c379e20 24985{
976ca316 24986 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 24987 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 24988 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
24989 struct attribute *attr;
24990 struct dynamic_prop prop;
1c379e20 24991
b4ba55a1
JB
24992 /* For Ada types, make sure that the gnat-specific data is always
24993 initialized (if not already set). There are a few types where
24994 we should not be doing so, because the type-specific area is
24995 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24996 where the type-specific area is used to store the floatformat).
24997 But this is not a problem, because the gnat-specific information
24998 is actually not needed for these types. */
24999 if (need_gnat_info (cu)
78134374
SM
25000 && type->code () != TYPE_CODE_FUNC
25001 && type->code () != TYPE_CODE_FLT
25002 && type->code () != TYPE_CODE_METHODPTR
25003 && type->code () != TYPE_CODE_MEMBERPTR
25004 && type->code () != TYPE_CODE_METHOD
09584414 25005 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
25006 && !HAVE_GNAT_AUX_INFO (type))
25007 INIT_GNAT_SPECIFIC (type);
25008
3f2f83dd
KB
25009 /* Read DW_AT_allocated and set in type. */
25010 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 25011 if (attr != NULL)
3f2f83dd 25012 {
293e7e51 25013 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25014 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25015 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 25016 }
3f2f83dd
KB
25017
25018 /* Read DW_AT_associated and set in type. */
25019 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 25020 if (attr != NULL)
3f2f83dd 25021 {
293e7e51 25022 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25023 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25024 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 25025 }
3f2f83dd 25026
3cdcd0ce 25027 /* Read DW_AT_data_location and set in type. */
57567375
TT
25028 if (!skip_data_location)
25029 {
25030 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25031 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25032 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25033 }
3cdcd0ce 25034
976ca316
SM
25035 if (per_objfile->die_type_hash == NULL)
25036 per_objfile->die_type_hash
0335378b
TT
25037 = htab_up (htab_create_alloc (127,
25038 per_cu_offset_and_type_hash,
25039 per_cu_offset_and_type_eq,
25040 NULL, xcalloc, xfree));
1c379e20 25041
dee91e82 25042 ofs.per_cu = cu->per_cu;
9c541725 25043 ofs.sect_off = die->sect_off;
1c379e20 25044 ofs.type = type;
dee91e82 25045 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25046 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25047 if (*slot)
b98664d3 25048 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25049 sect_offset_str (die->sect_off));
8d749320
SM
25050 *slot = XOBNEW (&objfile->objfile_obstack,
25051 struct dwarf2_per_cu_offset_and_type);
1c379e20 25052 **slot = ofs;
f792889a 25053 return type;
1c379e20
DJ
25054}
25055
9c541725 25056/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25057 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25058
25059static struct type *
9c541725 25060get_die_type_at_offset (sect_offset sect_off,
aa66c379 25061 dwarf2_per_cu_data *per_cu,
976ca316 25062 dwarf2_per_objfile *per_objfile)
1c379e20 25063{
dee91e82 25064 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25065
976ca316 25066 if (per_objfile->die_type_hash == NULL)
f792889a 25067 return NULL;
1c379e20 25068
dee91e82 25069 ofs.per_cu = per_cu;
9c541725 25070 ofs.sect_off = sect_off;
9a3c8263 25071 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25072 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25073 if (slot)
25074 return slot->type;
25075 else
25076 return NULL;
25077}
25078
02142a6c 25079/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25080 or return NULL if DIE does not have a saved type. */
25081
25082static struct type *
25083get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25084{
aa66c379 25085 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25086}
25087
10b3939b
DJ
25088/* Add a dependence relationship from CU to REF_PER_CU. */
25089
25090static void
25091dwarf2_add_dependence (struct dwarf2_cu *cu,
25092 struct dwarf2_per_cu_data *ref_per_cu)
25093{
25094 void **slot;
25095
25096 if (cu->dependencies == NULL)
25097 cu->dependencies
25098 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25099 NULL, &cu->comp_unit_obstack,
25100 hashtab_obstack_allocate,
25101 dummy_obstack_deallocate);
25102
25103 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25104 if (*slot == NULL)
25105 *slot = ref_per_cu;
25106}
1c379e20 25107
f504f079
DE
25108/* Subroutine of dwarf2_mark to pass to htab_traverse.
25109 Set the mark field in every compilation unit in the
7188ed02
SM
25110 cache that we must keep because we are keeping CU.
25111
25112 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25113
10b3939b
DJ
25114static int
25115dwarf2_mark_helper (void **slot, void *data)
25116{
7188ed02
SM
25117 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25118 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25119 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25120
25121 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25122 reading of the chain. As such dependencies remain valid it is not much
25123 useful to track and undo them during QUIT cleanups. */
7188ed02 25124 if (cu == nullptr)
d07ed419
JK
25125 return 1;
25126
7188ed02 25127 if (cu->mark)
10b3939b 25128 return 1;
10b3939b 25129
7188ed02
SM
25130 cu->mark = true;
25131
25132 if (cu->dependencies != nullptr)
25133 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25134
25135 return 1;
25136}
25137
f504f079
DE
25138/* Set the mark field in CU and in every other compilation unit in the
25139 cache that we must keep because we are keeping CU. */
25140
ae038cb0
DJ
25141static void
25142dwarf2_mark (struct dwarf2_cu *cu)
25143{
25144 if (cu->mark)
25145 return;
7188ed02 25146
9068261f 25147 cu->mark = true;
ae038cb0 25148
7188ed02
SM
25149 if (cu->dependencies != nullptr)
25150 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25151}
25152
72bf9492
DJ
25153/* Trivial hash function for partial_die_info: the hash value of a DIE
25154 is its offset in .debug_info for this objfile. */
25155
25156static hashval_t
25157partial_die_hash (const void *item)
25158{
9a3c8263
SM
25159 const struct partial_die_info *part_die
25160 = (const struct partial_die_info *) item;
9a619af0 25161
9c541725 25162 return to_underlying (part_die->sect_off);
72bf9492
DJ
25163}
25164
25165/* Trivial comparison function for partial_die_info structures: two DIEs
25166 are equal if they have the same offset. */
25167
25168static int
25169partial_die_eq (const void *item_lhs, const void *item_rhs)
25170{
9a3c8263
SM
25171 const struct partial_die_info *part_die_lhs
25172 = (const struct partial_die_info *) item_lhs;
25173 const struct partial_die_info *part_die_rhs
25174 = (const struct partial_die_info *) item_rhs;
9a619af0 25175
9c541725 25176 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25177}
25178
3c3bb058
AB
25179struct cmd_list_element *set_dwarf_cmdlist;
25180struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25181
9291a0cd 25182static void
cd4fb1b2
SM
25183show_check_physname (struct ui_file *file, int from_tty,
25184 struct cmd_list_element *c, const char *value)
9291a0cd 25185{
cd4fb1b2
SM
25186 fprintf_filtered (file,
25187 _("Whether to check \"physname\" is %s.\n"),
25188 value);
9291a0cd
TT
25189}
25190
6c265988 25191void _initialize_dwarf2_read ();
cd4fb1b2 25192void
6c265988 25193_initialize_dwarf2_read ()
9291a0cd 25194{
0743fc83 25195 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25196Set DWARF specific variables.\n\
590042fc 25197Configure DWARF variables such as the cache size."),
0743fc83
TT
25198 &set_dwarf_cmdlist, "maintenance set dwarf ",
25199 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25200
0743fc83 25201 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25202Show DWARF specific variables.\n\
25203Show DWARF variables such as the cache size."),
0743fc83
TT
25204 &show_dwarf_cmdlist, "maintenance show dwarf ",
25205 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25206
cd4fb1b2
SM
25207 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25208 &dwarf_max_cache_age, _("\
25209Set the upper bound on the age of cached DWARF compilation units."), _("\
25210Show the upper bound on the age of cached DWARF compilation units."), _("\
25211A higher limit means that cached compilation units will be stored\n\
25212in memory longer, and more total memory will be used. Zero disables\n\
25213caching, which can slow down startup."),
25214 NULL,
25215 show_dwarf_max_cache_age,
25216 &set_dwarf_cmdlist,
25217 &show_dwarf_cmdlist);
156942c7 25218
cd4fb1b2
SM
25219 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25220Set debugging of the DWARF reader."), _("\
25221Show debugging of the DWARF reader."), _("\
25222When enabled (non-zero), debugging messages are printed during DWARF\n\
25223reading and symtab expansion. A value of 1 (one) provides basic\n\
25224information. A value greater than 1 provides more verbose information."),
25225 NULL,
25226 NULL,
25227 &setdebuglist, &showdebuglist);
9291a0cd 25228
cd4fb1b2
SM
25229 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25230Set debugging of the DWARF DIE reader."), _("\
25231Show debugging of the DWARF DIE reader."), _("\
25232When enabled (non-zero), DIEs are dumped after they are read in.\n\
25233The value is the maximum depth to print."),
25234 NULL,
25235 NULL,
25236 &setdebuglist, &showdebuglist);
9291a0cd 25237
cd4fb1b2
SM
25238 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25239Set debugging of the dwarf line reader."), _("\
25240Show debugging of the dwarf line reader."), _("\
25241When enabled (non-zero), line number entries are dumped as they are read in.\n\
25242A value of 1 (one) provides basic information.\n\
25243A value greater than 1 provides more verbose information."),
25244 NULL,
25245 NULL,
25246 &setdebuglist, &showdebuglist);
437afbb8 25247
cd4fb1b2
SM
25248 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25249Set cross-checking of \"physname\" code against demangler."), _("\
25250Show cross-checking of \"physname\" code against demangler."), _("\
25251When enabled, GDB's internal \"physname\" code is checked against\n\
25252the demangler."),
25253 NULL, show_check_physname,
25254 &setdebuglist, &showdebuglist);
900e11f9 25255
e615022a
DE
25256 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25257 no_class, &use_deprecated_index_sections, _("\
25258Set whether to use deprecated gdb_index sections."), _("\
25259Show whether to use deprecated gdb_index sections."), _("\
25260When enabled, deprecated .gdb_index sections are used anyway.\n\
25261Normally they are ignored either because of a missing feature or\n\
25262performance issue.\n\
25263Warning: This option must be enabled before gdb reads the file."),
25264 NULL,
25265 NULL,
25266 &setlist, &showlist);
25267
f1e6e072
TT
25268 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25269 &dwarf2_locexpr_funcs);
25270 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25271 &dwarf2_loclist_funcs);
25272
25273 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25274 &dwarf2_block_frame_base_locexpr_funcs);
25275 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25276 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25277
25278#if GDB_SELF_TEST
25279 selftests::register_test ("dw2_expand_symtabs_matching",
25280 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25281 selftests::register_test ("dwarf2_find_containing_comp_unit",
25282 selftests::find_containing_comp_unit::run_test);
c62446b1 25283#endif
6502dd73 25284}
This page took 8.287 seconds and 4 git commands to generate.