Remove quick_symbol_functions::expand_symtabs_for_function
[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
2244 void expand_symtabs_with_fullname (struct objfile *objfile,
2245 const char *fullname) override;
2246
2247 struct compunit_symtab *find_pc_sect_compunit_symtab
2248 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2249 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2250
2251 struct compunit_symtab *find_compunit_symtab_by_address
2252 (struct objfile *objfile, CORE_ADDR address) override
2253 {
2254 return nullptr;
2255 }
2256
2257 void map_symbol_filenames (struct objfile *objfile,
f4655dee
TT
2258 gdb::function_view<symbol_filename_ftype> fun,
2259 bool need_fullname) override;
39298a5d
TT
2260};
2261
2262struct dwarf2_gdb_index : public dwarf2_base_index_functions
2263{
39298a5d
TT
2264 void dump (struct objfile *objfile) override;
2265
39298a5d
TT
2266 void map_matching_symbols
2267 (struct objfile *,
2268 const lookup_name_info &lookup_name,
2269 domain_enum domain,
2270 int global,
2271 gdb::function_view<symbol_found_callback_ftype> callback,
2272 symbol_compare_ftype *ordered_compare) override;
2273
df35e626 2274 bool expand_symtabs_matching
39298a5d
TT
2275 (struct objfile *objfile,
2276 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2277 const lookup_name_info *lookup_name,
2278 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2279 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2280 block_search_flags search_flags,
3bfa51a7 2281 domain_enum domain,
39298a5d
TT
2282 enum search_domain kind) override;
2283};
2284
2285struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2286{
39298a5d
TT
2287 void dump (struct objfile *objfile) override;
2288
39298a5d
TT
2289 void map_matching_symbols
2290 (struct objfile *,
2291 const lookup_name_info &lookup_name,
2292 domain_enum domain,
2293 int global,
2294 gdb::function_view<symbol_found_callback_ftype> callback,
2295 symbol_compare_ftype *ordered_compare) override;
2296
df35e626 2297 bool expand_symtabs_matching
39298a5d
TT
2298 (struct objfile *objfile,
2299 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2300 const lookup_name_info *lookup_name,
2301 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2302 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2303 block_search_flags search_flags,
3bfa51a7 2304 domain_enum domain,
39298a5d
TT
2305 enum search_domain kind) override;
2306};
2307
edc02ceb 2308static quick_symbol_functions_up
39298a5d
TT
2309make_dwarf_gdb_index ()
2310{
2311 return quick_symbol_functions_up (new dwarf2_gdb_index);
2312}
2313
edc02ceb 2314static quick_symbol_functions_up
39298a5d
TT
2315make_dwarf_debug_names ()
2316{
2317 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2318}
2319
094b34ac
DE
2320/* Utility hash function for a stmt_list_hash. */
2321
2322static hashval_t
2323hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2324{
2325 hashval_t v = 0;
2326
2327 if (stmt_list_hash->dwo_unit != NULL)
2328 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2329 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2330 return v;
2331}
2332
2333/* Utility equality function for a stmt_list_hash. */
2334
2335static int
2336eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2337 const struct stmt_list_hash *rhs)
2338{
2339 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2340 return 0;
2341 if (lhs->dwo_unit != NULL
2342 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2343 return 0;
2344
9c541725 2345 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2346}
2347
7b9f3c50
DE
2348/* Hash function for a quick_file_names. */
2349
2350static hashval_t
2351hash_file_name_entry (const void *e)
2352{
9a3c8263
SM
2353 const struct quick_file_names *file_data
2354 = (const struct quick_file_names *) e;
7b9f3c50 2355
094b34ac 2356 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2357}
2358
2359/* Equality function for a quick_file_names. */
2360
2361static int
2362eq_file_name_entry (const void *a, const void *b)
2363{
9a3c8263
SM
2364 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2365 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2366
094b34ac 2367 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2368}
2369
2370/* Delete function for a quick_file_names. */
2371
2372static void
2373delete_file_name_entry (void *e)
2374{
9a3c8263 2375 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2376 int i;
2377
2378 for (i = 0; i < file_data->num_file_names; ++i)
2379 {
2380 xfree ((void*) file_data->file_names[i]);
2381 if (file_data->real_names)
2382 xfree ((void*) file_data->real_names[i]);
2383 }
2384
45940949
TT
2385 /* The space for the struct itself lives on the obstack, so we don't
2386 free it here. */
7b9f3c50
DE
2387}
2388
2389/* Create a quick_file_names hash table. */
2390
5895093f 2391static htab_up
7b9f3c50
DE
2392create_quick_file_names_table (unsigned int nr_initial_entries)
2393{
5895093f
TT
2394 return htab_up (htab_create_alloc (nr_initial_entries,
2395 hash_file_name_entry, eq_file_name_entry,
2396 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2397}
9291a0cd 2398
ab432490
SM
2399/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2400 function is unrelated to symtabs, symtab would have to be created afterwards.
2401 You should call age_cached_comp_units after processing the CU. */
918dd910 2402
1b555f17 2403static dwarf2_cu *
ab432490
SM
2404load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2405 bool skip_partial)
918dd910 2406{
3019eac3 2407 if (per_cu->is_debug_types)
ab432490 2408 load_full_type_unit (per_cu, per_objfile);
918dd910 2409 else
4a636814
SM
2410 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2411 skip_partial, language_minimal);
918dd910 2412
7188ed02
SM
2413 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2414 if (cu == nullptr)
1b555f17 2415 return nullptr; /* Dummy CU. */
2dc860c0 2416
7188ed02 2417 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2418
7188ed02 2419 return cu;
918dd910
JK
2420}
2421
1350c3b4 2422/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2423
9291a0cd 2424static void
97a1449a 2425dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2426 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2427{
f4dc4d17
DE
2428 /* Skip type_unit_groups, reading the type units they contain
2429 is handled elsewhere. */
197400e8 2430 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2431 return;
2432
08ac5771
SM
2433 {
2434 /* The destructor of dwarf2_queue_guard frees any entries left on
2435 the queue. After this point we're guaranteed to leave this function
2436 with the dwarf queue empty. */
2437 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2438
08ac5771
SM
2439 if (!per_objfile->symtab_set_p (per_cu))
2440 {
2441 queue_comp_unit (per_cu, per_objfile, language_minimal);
2442 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2443
2444 /* If we just loaded a CU from a DWO, and we're working with an index
2445 that may badly handle TUs, load all the TUs in that DWO as well.
2446 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2447 if (!per_cu->is_debug_types
2448 && cu != NULL
2449 && cu->dwo_unit != NULL
2450 && per_objfile->per_bfd->index_table != NULL
2451 && per_objfile->per_bfd->index_table->version <= 7
2452 /* DWP files aren't supported yet. */
2453 && get_dwp_file (per_objfile) == NULL)
2454 queue_and_load_all_dwo_tus (cu);
2455 }
9291a0cd 2456
08ac5771
SM
2457 process_queue (per_objfile);
2458 }
9291a0cd
TT
2459
2460 /* Age the cache, releasing compilation units that have not
2461 been used recently. */
976ca316 2462 per_objfile->age_comp_units ();
9291a0cd
TT
2463}
2464
97a1449a
SM
2465/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2466 the per-objfile for which this symtab is instantiated.
2467
2468 Returns the resulting symbol table. */
2fdf6df6 2469
43f3e411 2470static struct compunit_symtab *
97a1449a 2471dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2472 dwarf2_per_objfile *per_objfile,
97a1449a 2473 bool skip_partial)
9291a0cd 2474{
976ca316 2475 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2476
976ca316 2477 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2478 {
976ca316 2479 free_cached_comp_units freer (per_objfile);
c83dd867 2480 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2481 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2482 process_cu_includes (per_objfile);
9291a0cd 2483 }
f194fefb 2484
976ca316 2485 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2486}
2487
ff4c9fec 2488/* See declaration. */
f4dc4d17 2489
ff4c9fec 2490dwarf2_per_cu_data *
5989a64e 2491dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2492{
b76e467d 2493 if (index >= this->all_comp_units.size ())
ff4c9fec 2494 {
b76e467d 2495 index -= this->all_comp_units.size ();
b2bdb8cf 2496 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2497 return &this->all_type_units[index]->per_cu;
2498 }
f4dc4d17 2499
ff4c9fec
SM
2500 return this->all_comp_units[index];
2501}
f4dc4d17 2502
ff4c9fec 2503/* See declaration. */
2fdf6df6 2504
ff4c9fec 2505dwarf2_per_cu_data *
5989a64e 2506dwarf2_per_bfd::get_cu (int index)
1fd400ff 2507{
b76e467d 2508 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2509
ff4c9fec 2510 return this->all_comp_units[index];
f4dc4d17
DE
2511}
2512
ff4c9fec 2513/* See declaration. */
f4dc4d17 2514
ff4c9fec 2515signatured_type *
5989a64e 2516dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2517{
b2bdb8cf 2518 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2519
ff4c9fec 2520 return this->all_type_units[index];
1fd400ff
TT
2521}
2522
d3473f0c
TT
2523/* See read.h. */
2524
2525dwarf2_per_cu_data *
5989a64e 2526dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2527{
2528 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2529 result->per_bfd = this;
d3473f0c
TT
2530 result->index = m_num_psymtabs++;
2531 return result;
2532}
2533
2534/* See read.h. */
2535
2536signatured_type *
5989a64e 2537dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2538{
2539 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2540 result->per_cu.per_bfd = this;
d3473f0c
TT
2541 result->per_cu.index = m_num_psymtabs++;
2542 return result;
2543}
2544
168c9250 2545/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2546 obstack, and constructed with the specified field values. */
4b514bc8
JK
2547
2548static dwarf2_per_cu_data *
168c9250
SM
2549create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2550 struct dwarf2_section_info *section,
2551 int is_dwz,
2552 sect_offset sect_off, ULONGEST length)
4b514bc8 2553{
168c9250 2554 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2555 the_cu->sect_off = sect_off;
2556 the_cu->length = length;
4b514bc8 2557 the_cu->section = section;
168c9250 2558 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2559 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2560 the_cu->is_dwz = is_dwz;
2561 return the_cu;
2562}
2563
2ec9a5e0
TT
2564/* A helper for create_cus_from_index that handles a given list of
2565 CUs. */
2fdf6df6 2566
74a0d9f6 2567static void
168c9250 2568create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2569 const gdb_byte *cu_list, offset_type n_elements,
2570 struct dwarf2_section_info *section,
b76e467d 2571 int is_dwz)
9291a0cd 2572{
12359b5e 2573 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2574 {
74a0d9f6 2575 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2576
2577 sect_offset sect_off
2578 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2579 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2580 cu_list += 2 * 8;
2581
b76e467d 2582 dwarf2_per_cu_data *per_cu
168c9250
SM
2583 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2584 length);
2585 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2586 }
9291a0cd
TT
2587}
2588
2ec9a5e0 2589/* Read the CU list from the mapped index, and use it to create all
168c9250 2590 the CU objects for PER_BFD. */
2ec9a5e0 2591
74a0d9f6 2592static void
168c9250 2593create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2594 const gdb_byte *cu_list, offset_type cu_list_elements,
2595 const gdb_byte *dwz_list, offset_type dwz_elements)
2596{
168c9250
SM
2597 gdb_assert (per_bfd->all_comp_units.empty ());
2598 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2599
168c9250
SM
2600 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2601 &per_bfd->info, 0);
2ec9a5e0
TT
2602
2603 if (dwz_elements == 0)
74a0d9f6 2604 return;
2ec9a5e0 2605
168c9250
SM
2606 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2607 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2608 &dwz->info, 1);
2ec9a5e0
TT
2609}
2610
1fd400ff 2611/* Create the signatured type hash table from the index. */
673bfd45 2612
74a0d9f6 2613static void
12359b5e 2614create_signatured_type_table_from_index
168c9250
SM
2615 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2616 const gdb_byte *bytes, offset_type elements)
1fd400ff 2617{
168c9250
SM
2618 gdb_assert (per_bfd->all_type_units.empty ());
2619 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2620
298e9637 2621 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2622
12359b5e 2623 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2624 {
52dc124a 2625 struct signatured_type *sig_type;
9c541725 2626 ULONGEST signature;
1fd400ff 2627 void **slot;
9c541725 2628 cu_offset type_offset_in_tu;
1fd400ff 2629
74a0d9f6 2630 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2631 sect_offset sect_off
2632 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2633 type_offset_in_tu
2634 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2635 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2636 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2637 bytes += 3 * 8;
2638
168c9250 2639 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2640 sig_type->signature = signature;
9c541725 2641 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2642 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2643 sig_type->per_cu.section = section;
9c541725 2644 sig_type->per_cu.sect_off = sect_off;
52dc124a 2645 sig_type->per_cu.v.quick
168c9250 2646 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2647 struct dwarf2_per_cu_quick_data);
2648
b0b6a987 2649 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2650 *slot = sig_type;
1fd400ff 2651
168c9250 2652 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2653 }
2654
168c9250 2655 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2656}
2657
927aa2e7
JK
2658/* Create the signatured type hash table from .debug_names. */
2659
2660static void
2661create_signatured_type_table_from_debug_names
976ca316 2662 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2663 const mapped_debug_names &map,
2664 struct dwarf2_section_info *section,
2665 struct dwarf2_section_info *abbrev_section)
2666{
976ca316 2667 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2668
96b79293
TT
2669 section->read (objfile);
2670 abbrev_section->read (objfile);
927aa2e7 2671
976ca316
SM
2672 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2673 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2674
298e9637 2675 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2676
2677 for (uint32_t i = 0; i < map.tu_count; ++i)
2678 {
2679 struct signatured_type *sig_type;
927aa2e7 2680 void **slot;
927aa2e7
JK
2681
2682 sect_offset sect_off
2683 = (sect_offset) (extract_unsigned_integer
2684 (map.tu_table_reordered + i * map.offset_size,
2685 map.offset_size,
2686 map.dwarf5_byte_order));
2687
2688 comp_unit_head cu_header;
976ca316 2689 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2690 abbrev_section,
927aa2e7
JK
2691 section->buffer + to_underlying (sect_off),
2692 rcuh_kind::TYPE);
2693
976ca316 2694 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2695 sig_type->signature = cu_header.signature;
2696 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2697 sig_type->per_cu.is_debug_types = 1;
2698 sig_type->per_cu.section = section;
2699 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2700 sig_type->per_cu.v.quick
976ca316 2701 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2702 struct dwarf2_per_cu_quick_data);
2703
b0b6a987 2704 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2705 *slot = sig_type;
2706
976ca316 2707 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2708 }
2709
976ca316 2710 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2711}
2712
9291a0cd 2713/* Read the address map data from the mapped index, and use it to
efd7398e 2714 populate the psymtabs_addrmap. */
2fdf6df6 2715
9291a0cd 2716static void
976ca316 2717create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2718 struct mapped_index *index)
9291a0cd 2719{
976ca316 2720 struct objfile *objfile = per_objfile->objfile;
efd7398e 2721 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2722 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2723 const gdb_byte *iter, *end;
9291a0cd 2724 struct addrmap *mutable_map;
9291a0cd
TT
2725 CORE_ADDR baseaddr;
2726
8268c778
PA
2727 auto_obstack temp_obstack;
2728
9291a0cd
TT
2729 mutable_map = addrmap_create_mutable (&temp_obstack);
2730
f00a2de2
PA
2731 iter = index->address_table.data ();
2732 end = iter + index->address_table.size ();
9291a0cd 2733
b3b3bada 2734 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2735
2736 while (iter < end)
2737 {
2738 ULONGEST hi, lo, cu_index;
2739 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2740 iter += 8;
2741 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2742 iter += 8;
2743 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2744 iter += 4;
f652bce2 2745
24a55014 2746 if (lo > hi)
f652bce2 2747 {
b98664d3 2748 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2749 hex_string (lo), hex_string (hi));
24a55014 2750 continue;
f652bce2 2751 }
24a55014 2752
efd7398e 2753 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2754 {
b98664d3 2755 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2756 (unsigned) cu_index);
24a55014 2757 continue;
f652bce2 2758 }
24a55014 2759
79748972
TT
2760 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2761 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2762 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2763 per_bfd->get_cu (cu_index));
9291a0cd
TT
2764 }
2765
efd7398e
TT
2766 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2767 &per_bfd->obstack);
9291a0cd
TT
2768}
2769
927aa2e7 2770/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2771 populate the psymtabs_addrmap. */
927aa2e7
JK
2772
2773static void
976ca316 2774create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2775 struct dwarf2_section_info *section)
2776{
976ca316 2777 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2778 bfd *abfd = objfile->obfd;
08feed99 2779 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2780 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2781 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2782
2783 auto_obstack temp_obstack;
2784 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2785
2786 std::unordered_map<sect_offset,
2787 dwarf2_per_cu_data *,
2788 gdb::hash_enum<sect_offset>>
2789 debug_info_offset_to_per_cu;
bab287cd 2790 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
927aa2e7 2791 {
927aa2e7
JK
2792 const auto insertpair
2793 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2794 if (!insertpair.second)
2795 {
2796 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2797 "debug_info_offset %s, ignoring .debug_aranges."),
2798 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2799 return;
2800 }
2801 }
2802
96b79293 2803 section->read (objfile);
927aa2e7
JK
2804
2805 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2806
2807 const gdb_byte *addr = section->buffer;
2808
2809 while (addr < section->buffer + section->size)
2810 {
2811 const gdb_byte *const entry_addr = addr;
2812 unsigned int bytes_read;
2813
2814 const LONGEST entry_length = read_initial_length (abfd, addr,
2815 &bytes_read);
2816 addr += bytes_read;
2817
2818 const gdb_byte *const entry_end = addr + entry_length;
2819 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2820 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2821 if (addr + entry_length > section->buffer + section->size)
2822 {
47e3f474 2823 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2824 "length %s exceeds section length %s, "
927aa2e7 2825 "ignoring .debug_aranges."),
47e3f474
TV
2826 objfile_name (objfile),
2827 plongest (entry_addr - section->buffer),
927aa2e7
JK
2828 plongest (bytes_read + entry_length),
2829 pulongest (section->size));
2830 return;
2831 }
2832
2833 /* The version number. */
2834 const uint16_t version = read_2_bytes (abfd, addr);
2835 addr += 2;
2836 if (version != 2)
2837 {
47e3f474 2838 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2839 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2840 objfile_name (objfile),
2841 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2842 return;
2843 }
2844
2845 const uint64_t debug_info_offset
2846 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2847 addr += offset_size;
2848 const auto per_cu_it
2849 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2850 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2851 {
47e3f474 2852 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2853 "debug_info_offset %s does not exists, "
2854 "ignoring .debug_aranges."),
47e3f474
TV
2855 objfile_name (objfile),
2856 plongest (entry_addr - section->buffer),
927aa2e7
JK
2857 pulongest (debug_info_offset));
2858 return;
2859 }
2860 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2861
2862 const uint8_t address_size = *addr++;
2863 if (address_size < 1 || address_size > 8)
2864 {
47e3f474 2865 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2866 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2867 objfile_name (objfile),
2868 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2869 return;
2870 }
2871
2872 const uint8_t segment_selector_size = *addr++;
2873 if (segment_selector_size != 0)
2874 {
47e3f474 2875 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2876 "segment_selector_size %u is not supported, "
2877 "ignoring .debug_aranges."),
47e3f474
TV
2878 objfile_name (objfile),
2879 plongest (entry_addr - section->buffer),
927aa2e7
JK
2880 segment_selector_size);
2881 return;
2882 }
2883
2884 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2885 size. It is undocumented by the DWARF standard but GCC does
2886 use it. */
927aa2e7
JK
2887 for (size_t padding = ((-(addr - section->buffer))
2888 & (2 * address_size - 1));
dda83cd7 2889 padding > 0; padding--)
927aa2e7
JK
2890 if (*addr++ != 0)
2891 {
47e3f474 2892 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2893 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2894 objfile_name (objfile),
2895 plongest (entry_addr - section->buffer));
927aa2e7
JK
2896 return;
2897 }
2898
2899 for (;;)
2900 {
2901 if (addr + 2 * address_size > entry_end)
2902 {
47e3f474 2903 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2904 "address list is not properly terminated, "
2905 "ignoring .debug_aranges."),
47e3f474
TV
2906 objfile_name (objfile),
2907 plongest (entry_addr - section->buffer));
927aa2e7
JK
2908 return;
2909 }
2910 ULONGEST start = extract_unsigned_integer (addr, address_size,
2911 dwarf5_byte_order);
2912 addr += address_size;
2913 ULONGEST length = extract_unsigned_integer (addr, address_size,
2914 dwarf5_byte_order);
2915 addr += address_size;
2916 if (start == 0 && length == 0)
2917 break;
efd7398e 2918 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2919 {
2920 /* Symbol was eliminated due to a COMDAT group. */
2921 continue;
2922 }
2923 ULONGEST end = start + length;
79748972
TT
2924 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2925 - baseaddr);
2926 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2927 - baseaddr);
927aa2e7
JK
2928 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2929 }
2930 }
2931
efd7398e
TT
2932 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2933 &per_bfd->obstack);
927aa2e7
JK
2934}
2935
4485a1c1
SM
2936/* A helper function that reads the .gdb_index from BUFFER and fills
2937 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2938 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2939 ok to use deprecated sections.
2940
2941 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2942 out parameters that are filled in with information about the CU and
2943 TU lists in the section.
2944
4485a1c1 2945 Returns true if all went well, false otherwise. */
2fdf6df6 2946
d33bc52e 2947static bool
3810f182 2948read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2949 bool deprecated_ok,
2950 gdb::array_view<const gdb_byte> buffer,
2951 struct mapped_index *map,
2952 const gdb_byte **cu_list,
2953 offset_type *cu_list_elements,
2954 const gdb_byte **types_list,
2955 offset_type *types_list_elements)
2956{
2957 const gdb_byte *addr = &buffer[0];
82430852 2958
9291a0cd 2959 /* Version check. */
4485a1c1 2960 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2961 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2962 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2963 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2964 indices. */
831adc1f 2965 if (version < 4)
481860b3
GB
2966 {
2967 static int warning_printed = 0;
2968 if (!warning_printed)
2969 {
2970 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2971 filename);
481860b3
GB
2972 warning_printed = 1;
2973 }
2974 return 0;
2975 }
2976 /* Index version 4 uses a different hash function than index version
2977 5 and later.
2978
2979 Versions earlier than 6 did not emit psymbols for inlined
2980 functions. Using these files will cause GDB not to be able to
2981 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2982 indices unless the user has done
2983 "set use-deprecated-index-sections on". */
2ec9a5e0 2984 if (version < 6 && !deprecated_ok)
481860b3
GB
2985 {
2986 static int warning_printed = 0;
2987 if (!warning_printed)
2988 {
e615022a
DE
2989 warning (_("\
2990Skipping deprecated .gdb_index section in %s.\n\
2991Do \"set use-deprecated-index-sections on\" before the file is read\n\
2992to use the section anyway."),
2ec9a5e0 2993 filename);
481860b3
GB
2994 warning_printed = 1;
2995 }
2996 return 0;
2997 }
796a7ff8 2998 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2999 of the TU (for symbols coming from TUs),
3000 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3001 Plus gold-generated indices can have duplicate entries for global symbols,
3002 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3003 These are just performance bugs, and we can't distinguish gdb-generated
3004 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3005
481860b3 3006 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3007 longer backward compatible. */
796a7ff8 3008 if (version > 8)
594e8718 3009 return 0;
9291a0cd 3010
559a7a62 3011 map->version = version;
9291a0cd 3012
4485a1c1 3013 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3014
4485a1c1 3015 int i = 0;
2ec9a5e0
TT
3016 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3017 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3018 / 8);
1fd400ff
TT
3019 ++i;
3020
2ec9a5e0
TT
3021 *types_list = addr + MAYBE_SWAP (metadata[i]);
3022 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3023 - MAYBE_SWAP (metadata[i]))
3024 / 8);
987d643c 3025 ++i;
1fd400ff 3026
f00a2de2
PA
3027 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3028 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3029 map->address_table
3030 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3031 ++i;
3032
f00a2de2
PA
3033 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3034 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3035 map->symbol_table
3036 = gdb::array_view<mapped_index::symbol_table_slot>
3037 ((mapped_index::symbol_table_slot *) symbol_table,
3038 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3039
f00a2de2 3040 ++i;
f9d83a0b 3041 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3042
2ec9a5e0
TT
3043 return 1;
3044}
3045
4485a1c1
SM
3046/* Callback types for dwarf2_read_gdb_index. */
3047
3048typedef gdb::function_view
5989a64e 3049 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3050 get_gdb_index_contents_ftype;
3051typedef gdb::function_view
3052 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3053 get_gdb_index_contents_dwz_ftype;
3054
927aa2e7 3055/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3056 elements of all the CUs and return 1. Otherwise, return 0. */
3057
3058static int
4485a1c1 3059dwarf2_read_gdb_index
976ca316 3060 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3061 get_gdb_index_contents_ftype get_gdb_index_contents,
3062 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3063{
2ec9a5e0
TT
3064 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3065 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3066 struct dwz_file *dwz;
976ca316 3067 struct objfile *objfile = per_objfile->objfile;
f8c41851 3068 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3069
4485a1c1 3070 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3071 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3072
3073 if (main_index_contents.empty ())
3074 return 0;
3075
3063847f 3076 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3077 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3078 use_deprecated_index_sections,
3079 main_index_contents, map.get (), &cu_list,
3080 &cu_list_elements, &types_list,
3081 &types_list_elements))
2ec9a5e0
TT
3082 return 0;
3083
0fefef59 3084 /* Don't use the index if it's empty. */
3063847f 3085 if (map->symbol_table.empty ())
0fefef59
DE
3086 return 0;
3087
2ec9a5e0
TT
3088 /* If there is a .dwz file, read it so we can get its CU list as
3089 well. */
f8c41851 3090 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3091 if (dwz != NULL)
2ec9a5e0 3092 {
2ec9a5e0
TT
3093 struct mapped_index dwz_map;
3094 const gdb_byte *dwz_types_ignore;
3095 offset_type dwz_types_elements_ignore;
3096
4485a1c1
SM
3097 gdb::array_view<const gdb_byte> dwz_index_content
3098 = get_gdb_index_contents_dwz (objfile, dwz);
3099
3100 if (dwz_index_content.empty ())
3101 return 0;
3102
3810f182 3103 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3104 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3105 &dwz_list, &dwz_list_elements,
3106 &dwz_types_ignore,
3107 &dwz_types_elements_ignore))
2ec9a5e0
TT
3108 {
3109 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3110 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3111 return 0;
3112 }
3113 }
3114
f8c41851
SM
3115 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3116 dwz_list_elements);
1fd400ff 3117
8b70b953
TT
3118 if (types_list_elements)
3119 {
8b70b953
TT
3120 /* We can only handle a single .debug_types when we have an
3121 index. */
f8c41851 3122 if (per_bfd->types.size () != 1)
8b70b953
TT
3123 return 0;
3124
f8c41851 3125 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3126
f8c41851 3127 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3128 types_list_elements);
8b70b953 3129 }
9291a0cd 3130
976ca316 3131 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3132
f8c41851
SM
3133 per_bfd->index_table = std::move (map);
3134 per_bfd->using_index = 1;
3135 per_bfd->quick_file_names_table =
3136 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3137
9291a0cd
TT
3138 return 1;
3139}
3140
dee91e82 3141/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3142
dee91e82
DE
3143static void
3144dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 3145 struct die_info *comp_unit_die)
9291a0cd 3146{
dee91e82 3147 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3148 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3149 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3150 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3151 struct attribute *attr;
7b9f3c50
DE
3152 void **slot;
3153 struct quick_file_names *qfn;
9291a0cd 3154
0186c6a7
DE
3155 gdb_assert (! this_cu->is_debug_types);
3156
07261596
TT
3157 /* Our callers never want to match partial units -- instead they
3158 will match the enclosing full CU. */
3159 if (comp_unit_die->tag == DW_TAG_partial_unit)
3160 {
3161 this_cu->v.quick->no_file_data = 1;
3162 return;
3163 }
3164
0186c6a7 3165 lh_cu = this_cu;
7b9f3c50 3166 slot = NULL;
dee91e82 3167
fff8551c 3168 line_header_up lh;
9c541725 3169 sect_offset line_offset {};
fff8551c 3170
dee91e82 3171 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3172 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3173 {
7b9f3c50
DE
3174 struct quick_file_names find_entry;
3175
d4df075e 3176 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3177
3178 /* We may have already read in this line header (TU line header sharing).
3179 If we have we're done. */
094b34ac 3180 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3181 find_entry.hash.line_sect_off = line_offset;
976ca316 3182 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3183 &find_entry, INSERT);
3184 if (*slot != NULL)
3185 {
9a3c8263 3186 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3187 return;
7b9f3c50
DE
3188 }
3189
3019eac3 3190 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3191 }
3192 if (lh == NULL)
3193 {
094b34ac 3194 lh_cu->v.quick->no_file_data = 1;
dee91e82 3195 return;
9291a0cd
TT
3196 }
3197
976ca316 3198 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3199 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3200 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3201 gdb_assert (slot != NULL);
3202 *slot = qfn;
9291a0cd 3203
d721ba37 3204 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3205
aa391654
TT
3206 int offset = 0;
3207 if (strcmp (fnd.name, "<unknown>") != 0)
3208 ++offset;
3209
7ba99d21 3210 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3211 qfn->file_names =
976ca316 3212 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3213 qfn->num_file_names);
aa391654
TT
3214 if (offset != 0)
3215 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3216 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3217 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3218 fnd.comp_dir).release ();
7b9f3c50 3219 qfn->real_names = NULL;
9291a0cd 3220
094b34ac 3221 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3222}
3223
3224/* A helper for the "quick" functions which attempts to read the line
3225 table for THIS_CU. */
3226
3227static struct quick_file_names *
ab432490
SM
3228dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3229 dwarf2_per_objfile *per_objfile)
dee91e82 3230{
0186c6a7
DE
3231 /* This should never be called for TUs. */
3232 gdb_assert (! this_cu->is_debug_types);
3233 /* Nor type unit groups. */
197400e8 3234 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3235
dee91e82
DE
3236 if (this_cu->v.quick->file_names != NULL)
3237 return this_cu->v.quick->file_names;
3238 /* If we know there is no line data, no point in looking again. */
3239 if (this_cu->v.quick->no_file_data)
3240 return NULL;
3241
ab432490 3242 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3243 if (!reader.dummy_p)
eb5dd737 3244 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
3245
3246 if (this_cu->v.quick->no_file_data)
3247 return NULL;
3248 return this_cu->v.quick->file_names;
9291a0cd
TT
3249}
3250
3251/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3252 real path for a given file name from the line table. */
2fdf6df6 3253
9291a0cd 3254static const char *
976ca316 3255dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3256 struct quick_file_names *qfn, int index)
9291a0cd 3257{
7b9f3c50 3258 if (qfn->real_names == NULL)
976ca316 3259 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3260 qfn->num_file_names, const char *);
9291a0cd 3261
7b9f3c50 3262 if (qfn->real_names[index] == NULL)
14278e1f 3263 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3264
7b9f3c50 3265 return qfn->real_names[index];
9291a0cd
TT
3266}
3267
39298a5d
TT
3268struct symtab *
3269dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3270{
976ca316
SM
3271 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3272 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3273 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3274
43f3e411
DE
3275 if (cust == NULL)
3276 return NULL;
ed2dc618 3277
43f3e411 3278 return compunit_primary_filetab (cust);
9291a0cd
TT
3279}
3280
7b9f3c50
DE
3281/* Traversal function for dw2_forget_cached_source_info. */
3282
3283static int
3284dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3285{
7b9f3c50 3286 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3287
7b9f3c50 3288 if (file_data->real_names)
9291a0cd 3289 {
7b9f3c50 3290 int i;
9291a0cd 3291
7b9f3c50 3292 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3293 {
7b9f3c50
DE
3294 xfree ((void*) file_data->real_names[i]);
3295 file_data->real_names[i] = NULL;
9291a0cd
TT
3296 }
3297 }
7b9f3c50
DE
3298
3299 return 1;
3300}
3301
39298a5d
TT
3302void
3303dwarf2_base_index_functions::forget_cached_source_info
3304 (struct objfile *objfile)
7b9f3c50 3305{
976ca316 3306 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3307
976ca316 3308 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3309 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3310}
3311
da51c347
DE
3312/* Struct used to manage iterating over all CUs looking for a symbol. */
3313
3314struct dw2_symtab_iterator
9291a0cd 3315{
ed2dc618 3316 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3317 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3318 /* If set, only look for symbols that match that block. Valid values are
3319 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3320 gdb::optional<block_enum> block_index;
da51c347
DE
3321 /* The kind of symbol we're looking for. */
3322 domain_enum domain;
3323 /* The list of CUs from the index entry of the symbol,
3324 or NULL if not found. */
3325 offset_type *vec;
3326 /* The next element in VEC to look at. */
3327 int next;
3328 /* The number of elements in VEC, or zero if there is no match. */
3329 int length;
8943b874
DE
3330 /* Have we seen a global version of the symbol?
3331 If so we can ignore all further global instances.
3332 This is to work around gold/15646, inefficient gold-generated
3333 indices. */
3334 int global_seen;
da51c347 3335};
9291a0cd 3336
7089bd88 3337/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2fdf6df6 3338
9291a0cd 3339static void
7089bd88
TT
3340dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3341 dwarf2_per_objfile *per_objfile,
3342 gdb::optional<block_enum> block_index,
3343 domain_enum domain, offset_type namei)
da51c347 3344{
976ca316 3345 iter->per_objfile = per_objfile;
da51c347
DE
3346 iter->block_index = block_index;
3347 iter->domain = domain;
3348 iter->next = 0;
8943b874 3349 iter->global_seen = 0;
e5f3ece2
TV
3350 iter->vec = NULL;
3351 iter->length = 0;
e5f3ece2
TV
3352
3353 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3354 /* index is NULL if OBJF_READNOW. */
3355 if (index == NULL)
3356 return;
3357
3358 gdb_assert (!index->symbol_name_slot_invalid (namei));
3359 const auto &bucket = index->symbol_table[namei];
3360
3361 iter->vec = (offset_type *) (index->constant_pool
3362 + MAYBE_SWAP (bucket.vec));
3363 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3364}
3365
3366/* Return the next matching CU or NULL if there are no more. */
3367
3368static struct dwarf2_per_cu_data *
3369dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3370{
976ca316 3371 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3372
da51c347
DE
3373 for ( ; iter->next < iter->length; ++iter->next)
3374 {
3375 offset_type cu_index_and_attrs =
3376 MAYBE_SWAP (iter->vec[iter->next + 1]);
3377 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3378 gdb_index_symbol_kind symbol_kind =
3379 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3380 /* Only check the symbol attributes if they're present.
3381 Indices prior to version 7 don't record them,
3382 and indices >= 7 may elide them for certain symbols
3383 (gold does this). */
3384 int attrs_valid =
976ca316 3385 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3386 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3387
3190f0c6 3388 /* Don't crash on bad data. */
976ca316
SM
3389 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3390 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3391 {
b98664d3 3392 complaint (_(".gdb_index entry has bad CU index"
976ca316 3393 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3394 continue;
3395 }
3396
976ca316 3397 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3398
da51c347 3399 /* Skip if already read in. */
976ca316 3400 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3401 continue;
3402
8943b874
DE
3403 /* Check static vs global. */
3404 if (attrs_valid)
3405 {
2b79f376
SM
3406 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3407
3408 if (iter->block_index.has_value ())
3409 {
3410 bool want_static = *iter->block_index == STATIC_BLOCK;
3411
3412 if (is_static != want_static)
3413 continue;
3414 }
3415
8943b874 3416 /* Work around gold/15646. */
f030440d
TV
3417 if (!is_static
3418 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3419 {
3420 if (iter->global_seen)
3421 continue;
3422
3423 iter->global_seen = 1;
3424 }
8943b874 3425 }
da51c347
DE
3426
3427 /* Only check the symbol's kind if it has one. */
3428 if (attrs_valid)
3429 {
3430 switch (iter->domain)
3431 {
3432 case VAR_DOMAIN:
3433 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3434 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3435 /* Some types are also in VAR_DOMAIN. */
3436 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3437 continue;
3438 break;
3439 case STRUCT_DOMAIN:
3440 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3441 continue;
3442 break;
3443 case LABEL_DOMAIN:
3444 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3445 continue;
3446 break;
59c35742
AB
3447 case MODULE_DOMAIN:
3448 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3449 continue;
3450 break;
da51c347
DE
3451 default:
3452 break;
3453 }
3454 }
3455
3456 ++iter->next;
3457 return per_cu;
3458 }
3459
3460 return NULL;
3461}
3462
39298a5d 3463void
4829711b
TT
3464dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3465 bool print_bcache)
9291a0cd 3466{
4829711b
TT
3467 if (print_bcache)
3468 return;
3469
976ca316
SM
3470 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3471 int total = (per_objfile->per_bfd->all_comp_units.size ()
3472 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3473 int count = 0;
9291a0cd 3474
ed2dc618 3475 for (int i = 0; i < total; ++i)
9291a0cd 3476 {
976ca316 3477 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3478
976ca316 3479 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3480 ++count;
3481 }
e4a48d9d 3482 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3483 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3484}
3485
779bd270
DE
3486/* This dumps minimal information about the index.
3487 It is called via "mt print objfiles".
3488 One use is to verify .gdb_index has been loaded by the
3489 gdb.dwarf2/gdb-index.exp testcase. */
3490
39298a5d
TT
3491void
3492dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3493{
976ca316 3494 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3495
976ca316 3496 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3497 printf_filtered (".gdb_index:");
976ca316 3498 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3499 {
3500 printf_filtered (" version %d\n",
976ca316 3501 per_objfile->per_bfd->index_table->version);
779bd270
DE
3502 }
3503 else
3504 printf_filtered (" faked for \"readnow\"\n");
3505 printf_filtered ("\n");
9291a0cd
TT
3506}
3507
39298a5d
TT
3508void
3509dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3510{
976ca316
SM
3511 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3512 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3513 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3514
ed2dc618 3515 for (int i = 0; i < total_units; ++i)
9291a0cd 3516 {
976ca316 3517 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3518
58f0c718
TT
3519 /* We don't want to directly expand a partial CU, because if we
3520 read it with the wrong language, then assertion failures can
3521 be triggered later on. See PR symtab/23010. So, tell
3522 dw2_instantiate_symtab to skip partial CUs -- any important
3523 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3524 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3525 }
3526}
3527
39298a5d
TT
3528void
3529dwarf2_base_index_functions::expand_symtabs_with_fullname
3530 (struct objfile *objfile, const char *fullname)
9291a0cd 3531{
976ca316 3532 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3533
3534 /* We don't need to consider type units here.
3535 This is only called for examining code, e.g. expand_line_sal.
3536 There can be an order of magnitude (or more) more type units
3537 than comp units, and we avoid them if we can. */
3538
976ca316 3539 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3540 {
3d7bb9d9 3541 /* We only need to look at symtabs not already expanded. */
976ca316 3542 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3543 continue;
3544
976ca316 3545 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3546 if (file_data == NULL)
9291a0cd
TT
3547 continue;
3548
b76e467d 3549 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3550 {
652a8996
JK
3551 const char *this_fullname = file_data->file_names[j];
3552
3553 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3554 {
976ca316 3555 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3556 break;
3557 }
3558 }
3559 }
3560}
3561
df35e626 3562static bool
9a0bacfb
TV
3563dw2_expand_symtabs_matching_symbol
3564 (mapped_index_base &index,
3565 const lookup_name_info &lookup_name_in,
3566 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3567 gdb::function_view<bool (offset_type)> match_callback,
3568 dwarf2_per_objfile *per_objfile);
9a0bacfb 3569
df35e626 3570static bool
9a0bacfb 3571dw2_expand_symtabs_matching_one
97a1449a
SM
3572 (dwarf2_per_cu_data *per_cu,
3573 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3574 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3575 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3576
af82f89d
TT
3577void
3578dwarf2_gdb_index::map_matching_symbols
199b4314 3579 (struct objfile *objfile,
b054970d 3580 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3581 int global,
3582 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3583 symbol_compare_ftype *ordered_compare)
9291a0cd 3584{
1aa98955 3585 /* Used for Ada. */
976ca316 3586 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3587
9a0bacfb
TV
3588 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3589
976ca316 3590 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3591 {
976ca316 3592 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3593
9a0bacfb
TV
3594 const char *match_name = name.ada ().lookup_name ().c_str ();
3595 auto matcher = [&] (const char *symname)
3596 {
3597 if (ordered_compare == nullptr)
3598 return true;
3599 return ordered_compare (symname, match_name) == 0;
3600 };
3601
b64f703b 3602 dw2_expand_symtabs_matching_symbol (index, name, matcher,
9a0bacfb
TV
3603 [&] (offset_type namei)
3604 {
3605 struct dw2_symtab_iterator iter;
3606 struct dwarf2_per_cu_data *per_cu;
3607
976ca316 3608 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3609 namei);
9a0bacfb 3610 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3611 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3612 nullptr);
9a0bacfb 3613 return true;
976ca316 3614 }, per_objfile);
9a0bacfb
TV
3615 }
3616 else
3617 {
3618 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3619 proceed assuming all symtabs have been read in. */
3620 }
1aa98955
TV
3621
3622 for (compunit_symtab *cust : objfile->compunits ())
3623 {
3624 const struct block *block;
3625
3626 if (cust == NULL)
3627 continue;
3628 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3629 if (!iterate_over_symbols_terminated (block, name,
3630 domain, callback))
3631 return;
3632 }
9291a0cd
TT
3633}
3634
e1ef7d7a
PA
3635/* Starting from a search name, return the string that finds the upper
3636 bound of all strings that start with SEARCH_NAME in a sorted name
3637 list. Returns the empty string to indicate that the upper bound is
3638 the end of the list. */
3639
3640static std::string
3641make_sort_after_prefix_name (const char *search_name)
3642{
3643 /* When looking to complete "func", we find the upper bound of all
3644 symbols that start with "func" by looking for where we'd insert
3645 the closest string that would follow "func" in lexicographical
3646 order. Usually, that's "func"-with-last-character-incremented,
3647 i.e. "fund". Mind non-ASCII characters, though. Usually those
3648 will be UTF-8 multi-byte sequences, but we can't be certain.
3649 Especially mind the 0xff character, which is a valid character in
3650 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3651 rule out compilers allowing it in identifiers. Note that
3652 conveniently, strcmp/strcasecmp are specified to compare
3653 characters interpreted as unsigned char. So what we do is treat
3654 the whole string as a base 256 number composed of a sequence of
3655 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3656 to 0, and carries 1 to the following more-significant position.
3657 If the very first character in SEARCH_NAME ends up incremented
3658 and carries/overflows, then the upper bound is the end of the
3659 list. The string after the empty string is also the empty
3660 string.
3661
3662 Some examples of this operation:
3663
3664 SEARCH_NAME => "+1" RESULT
3665
3666 "abc" => "abd"
3667 "ab\xff" => "ac"
3668 "\xff" "a" "\xff" => "\xff" "b"
3669 "\xff" => ""
3670 "\xff\xff" => ""
3671 "" => ""
3672
3673 Then, with these symbols for example:
3674
3675 func
3676 func1
3677 fund
3678
3679 completing "func" looks for symbols between "func" and
3680 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3681 which finds "func" and "func1", but not "fund".
3682
3683 And with:
3684
3685 funcÿ (Latin1 'ÿ' [0xff])
3686 funcÿ1
3687 fund
3688
3689 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3690 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3691
3692 And with:
3693
3694 ÿÿ (Latin1 'ÿ' [0xff])
3695 ÿÿ1
3696
3697 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3698 the end of the list.
3699 */
3700 std::string after = search_name;
3701 while (!after.empty () && (unsigned char) after.back () == 0xff)
3702 after.pop_back ();
3703 if (!after.empty ())
3704 after.back () = (unsigned char) after.back () + 1;
3705 return after;
3706}
3707
5c58de74 3708/* See declaration. */
61d96d7e 3709
5c58de74
PA
3710std::pair<std::vector<name_component>::const_iterator,
3711 std::vector<name_component>::const_iterator>
44ed8f3e 3712mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3713 (const lookup_name_info &lookup_name_without_params, language lang,
3714 dwarf2_per_objfile *per_objfile) const
3f563c84 3715{
5c58de74
PA
3716 auto *name_cmp
3717 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3718
3b00ef10 3719 const char *lang_name
e0802d59 3720 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3721
3f563c84
PA
3722 /* Comparison function object for lower_bound that matches against a
3723 given symbol name. */
3724 auto lookup_compare_lower = [&] (const name_component &elem,
3725 const char *name)
3726 {
fcf23d5b 3727 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3728 const char *elem_name = elem_qualified + elem.name_offset;
3729 return name_cmp (elem_name, name) < 0;
3730 };
3731
3732 /* Comparison function object for upper_bound that matches against a
3733 given symbol name. */
3734 auto lookup_compare_upper = [&] (const char *name,
3735 const name_component &elem)
3736 {
fcf23d5b 3737 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3738 const char *elem_name = elem_qualified + elem.name_offset;
3739 return name_cmp (name, elem_name) < 0;
3740 };
3741
5c58de74
PA
3742 auto begin = this->name_components.begin ();
3743 auto end = this->name_components.end ();
3f563c84
PA
3744
3745 /* Find the lower bound. */
3746 auto lower = [&] ()
3747 {
3b00ef10 3748 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3749 return begin;
3750 else
3b00ef10 3751 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3752 } ();
3753
3754 /* Find the upper bound. */
3755 auto upper = [&] ()
3756 {
5c58de74 3757 if (lookup_name_without_params.completion_mode ())
3f563c84 3758 {
e1ef7d7a
PA
3759 /* In completion mode, we want UPPER to point past all
3760 symbols names that have the same prefix. I.e., with
3761 these symbols, and completing "func":
3762
3763 function << lower bound
3764 function1
3765 other_function << upper bound
3766
3767 We find the upper bound by looking for the insertion
3768 point of "func"-with-last-character-incremented,
3769 i.e. "fund". */
3b00ef10 3770 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3771 if (after.empty ())
3f563c84 3772 return end;
e6b2f5ef
PA
3773 return std::lower_bound (lower, end, after.c_str (),
3774 lookup_compare_lower);
3f563c84
PA
3775 }
3776 else
3b00ef10 3777 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3778 } ();
3779
5c58de74
PA
3780 return {lower, upper};
3781}
3782
3783/* See declaration. */
3784
3785void
fcf23d5b 3786mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3787{
3788 if (!this->name_components.empty ())
3789 return;
3790
3791 this->name_components_casing = case_sensitivity;
3792 auto *name_cmp
3793 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3794
3795 /* The code below only knows how to break apart components of C++
3796 symbol names (and other languages that use '::' as
3b00ef10 3797 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3798 auto count = this->symbol_name_count ();
3799 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3800 {
44ed8f3e 3801 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3802 continue;
3803
fcf23d5b 3804 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3805
3806 /* Add each name component to the name component table. */
3807 unsigned int previous_len = 0;
3b00ef10
TT
3808
3809 if (strstr (name, "::") != nullptr)
3810 {
3811 for (unsigned int current_len = cp_find_first_component (name);
3812 name[current_len] != '\0';
3813 current_len += cp_find_first_component (name + current_len))
3814 {
3815 gdb_assert (name[current_len] == ':');
3816 this->name_components.push_back ({previous_len, idx});
3817 /* Skip the '::'. */
3818 current_len += 2;
3819 previous_len = current_len;
3820 }
3821 }
3822 else
5c58de74 3823 {
3b00ef10
TT
3824 /* Handle the Ada encoded (aka mangled) form here. */
3825 for (const char *iter = strstr (name, "__");
3826 iter != nullptr;
3827 iter = strstr (iter, "__"))
3828 {
3829 this->name_components.push_back ({previous_len, idx});
3830 iter += 2;
3831 previous_len = iter - name;
3832 }
5c58de74 3833 }
3b00ef10 3834
5c58de74
PA
3835 this->name_components.push_back ({previous_len, idx});
3836 }
3837
3838 /* Sort name_components elements by name. */
3839 auto name_comp_compare = [&] (const name_component &left,
3840 const name_component &right)
3841 {
fcf23d5b
SM
3842 const char *left_qualified
3843 = this->symbol_name_at (left.idx, per_objfile);
3844 const char *right_qualified
3845 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
3846
3847 const char *left_name = left_qualified + left.name_offset;
3848 const char *right_name = right_qualified + right.name_offset;
3849
3850 return name_cmp (left_name, right_name) < 0;
3851 };
3852
3853 std::sort (this->name_components.begin (),
3854 this->name_components.end (),
3855 name_comp_compare);
3856}
3857
3858/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3859 mapped_index_base instead of the containing objfile. This is split
3860 to a separate function in order to be able to unit test the
3861 name_components matching using a mock mapped_index_base. For each
5c58de74 3862 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3863 symbol's index in the mapped_index_base symbol table. */
5c58de74 3864
df35e626 3865static bool
5c58de74 3866dw2_expand_symtabs_matching_symbol
44ed8f3e 3867 (mapped_index_base &index,
5c58de74
PA
3868 const lookup_name_info &lookup_name_in,
3869 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3870 gdb::function_view<bool (offset_type)> match_callback,
3871 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3872{
3873 lookup_name_info lookup_name_without_params
3874 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3875
3876 /* Build the symbol name component sorted vector, if we haven't
3877 yet. */
fcf23d5b 3878 index.build_name_components (per_objfile);
5c58de74 3879
3f563c84
PA
3880 /* The same symbol may appear more than once in the range though.
3881 E.g., if we're looking for symbols that complete "w", and we have
3882 a symbol named "w1::w2", we'll find the two name components for
3883 that same symbol in the range. To be sure we only call the
3884 callback once per symbol, we first collect the symbol name
3885 indexes that matched in a temporary vector and ignore
3886 duplicates. */
3887 std::vector<offset_type> matches;
3f563c84 3888
3b00ef10
TT
3889 struct name_and_matcher
3890 {
3891 symbol_name_matcher_ftype *matcher;
ecc6c606 3892 const char *name;
3b00ef10
TT
3893
3894 bool operator== (const name_and_matcher &other) const
3f563c84 3895 {
ecc6c606 3896 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3897 }
3898 };
3899
3900 /* A vector holding all the different symbol name matchers, for all
3901 languages. */
3902 std::vector<name_and_matcher> matchers;
3903
3904 for (int i = 0; i < nr_languages; i++)
3905 {
3906 enum language lang_e = (enum language) i;
3907
3908 const language_defn *lang = language_def (lang_e);
3909 symbol_name_matcher_ftype *name_matcher
c9debfb9 3910 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 3911
3b00ef10 3912 name_and_matcher key {
dda83cd7 3913 name_matcher,
3b00ef10
TT
3914 lookup_name_without_params.language_lookup_name (lang_e)
3915 };
3916
3917 /* Don't insert the same comparison routine more than once.
3918 Note that we do this linear walk. This is not a problem in
3919 practice because the number of supported languages is
3920 low. */
3921 if (std::find (matchers.begin (), matchers.end (), key)
3922 != matchers.end ())
9291a0cd 3923 continue;
3b00ef10
TT
3924 matchers.push_back (std::move (key));
3925
3926 auto bounds
3927 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 3928 lang_e, per_objfile);
3b00ef10
TT
3929
3930 /* Now for each symbol name in range, check to see if we have a name
3931 match, and if so, call the MATCH_CALLBACK callback. */
3932
3933 for (; bounds.first != bounds.second; ++bounds.first)
3934 {
fcf23d5b
SM
3935 const char *qualified
3936 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
3937
3938 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3939 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3940 continue;
9291a0cd 3941
3b00ef10
TT
3942 matches.push_back (bounds.first->idx);
3943 }
3f563c84
PA
3944 }
3945
3946 std::sort (matches.begin (), matches.end ());
3947
3948 /* Finally call the callback, once per match. */
3949 ULONGEST prev = -1;
df35e626 3950 bool result = true;
3f563c84
PA
3951 for (offset_type idx : matches)
3952 {
3953 if (prev != idx)
3954 {
3b00ef10 3955 if (!match_callback (idx))
df35e626
TT
3956 {
3957 result = false;
3958 break;
3959 }
3f563c84
PA
3960 prev = idx;
3961 }
3962 }
3963
3964 /* Above we use a type wider than idx's for 'prev', since 0 and
3965 (offset_type)-1 are both possible values. */
3966 static_assert (sizeof (prev) > sizeof (offset_type), "");
df35e626
TT
3967
3968 return result;
3f563c84
PA
3969}
3970
c62446b1
PA
3971#if GDB_SELF_TEST
3972
3973namespace selftests { namespace dw2_expand_symtabs_matching {
3974
a3c5fafd
PA
3975/* A mock .gdb_index/.debug_names-like name index table, enough to
3976 exercise dw2_expand_symtabs_matching_symbol, which works with the
3977 mapped_index_base interface. Builds an index from the symbol list
3978 passed as parameter to the constructor. */
3979class mock_mapped_index : public mapped_index_base
c62446b1
PA
3980{
3981public:
a3c5fafd
PA
3982 mock_mapped_index (gdb::array_view<const char *> symbols)
3983 : m_symbol_table (symbols)
c62446b1
PA
3984 {}
3985
a3c5fafd 3986 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3987
a3c5fafd 3988 /* Return the number of names in the symbol table. */
632e107b 3989 size_t symbol_name_count () const override
c62446b1 3990 {
a3c5fafd 3991 return m_symbol_table.size ();
c62446b1
PA
3992 }
3993
a3c5fafd 3994 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
3995 const char *symbol_name_at
3996 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
3997 {
3998 return m_symbol_table[idx];
3999 }
c62446b1 4000
a3c5fafd
PA
4001private:
4002 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4003};
4004
4005/* Convenience function that converts a NULL pointer to a "<null>"
4006 string, to pass to print routines. */
4007
4008static const char *
4009string_or_null (const char *str)
4010{
4011 return str != NULL ? str : "<null>";
4012}
4013
4014/* Check if a lookup_name_info built from
4015 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4016 index. EXPECTED_LIST is the list of expected matches, in expected
4017 matching order. If no match expected, then an empty list is
4018 specified. Returns true on success. On failure prints a warning
4019 indicating the file:line that failed, and returns false. */
4020
4021static bool
4022check_match (const char *file, int line,
4023 mock_mapped_index &mock_index,
4024 const char *name, symbol_name_match_type match_type,
4025 bool completion_mode,
fcf23d5b
SM
4026 std::initializer_list<const char *> expected_list,
4027 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4028{
4029 lookup_name_info lookup_name (name, match_type, completion_mode);
4030
4031 bool matched = true;
4032
4033 auto mismatch = [&] (const char *expected_str,
4034 const char *got)
4035 {
4036 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4037 "expected=\"%s\", got=\"%s\"\n"),
4038 file, line,
4039 (match_type == symbol_name_match_type::FULL
4040 ? "FULL" : "WILD"),
4041 name, string_or_null (expected_str), string_or_null (got));
4042 matched = false;
4043 };
4044
4045 auto expected_it = expected_list.begin ();
4046 auto expected_end = expected_list.end ();
4047
a3c5fafd 4048 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
b64f703b 4049 nullptr,
c62446b1
PA
4050 [&] (offset_type idx)
4051 {
fcf23d5b 4052 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4053 const char *expected_str
4054 = expected_it == expected_end ? NULL : *expected_it++;
4055
4056 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4057 mismatch (expected_str, matched_name);
3b00ef10 4058 return true;
fcf23d5b 4059 }, per_objfile);
c62446b1
PA
4060
4061 const char *expected_str
4062 = expected_it == expected_end ? NULL : *expected_it++;
4063 if (expected_str != NULL)
4064 mismatch (expected_str, NULL);
4065
4066 return matched;
4067}
4068
4069/* The symbols added to the mock mapped_index for testing (in
4070 canonical form). */
4071static const char *test_symbols[] = {
4072 "function",
4073 "std::bar",
4074 "std::zfunction",
4075 "std::zfunction2",
4076 "w1::w2",
4077 "ns::foo<char*>",
4078 "ns::foo<int>",
4079 "ns::foo<long>",
a20714ff
PA
4080 "ns2::tmpl<int>::foo2",
4081 "(anonymous namespace)::A::B::C",
c62446b1 4082
e1ef7d7a
PA
4083 /* These are used to check that the increment-last-char in the
4084 matching algorithm for completion doesn't match "t1_fund" when
4085 completing "t1_func". */
4086 "t1_func",
4087 "t1_func1",
4088 "t1_fund",
4089 "t1_fund1",
4090
4091 /* A UTF-8 name with multi-byte sequences to make sure that
4092 cp-name-parser understands this as a single identifier ("função"
4093 is "function" in PT). */
4094 u8"u8função",
4095
4096 /* \377 (0xff) is Latin1 'ÿ'. */
4097 "yfunc\377",
4098
4099 /* \377 (0xff) is Latin1 'ÿ'. */
4100 "\377",
4101 "\377\377123",
4102
c62446b1
PA
4103 /* A name with all sorts of complications. Starts with "z" to make
4104 it easier for the completion tests below. */
4105#define Z_SYM_NAME \
4106 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4107 "::tuple<(anonymous namespace)::ui*, " \
4108 "std::default_delete<(anonymous namespace)::ui>, void>"
4109
4110 Z_SYM_NAME
4111};
4112
a3c5fafd
PA
4113/* Returns true if the mapped_index_base::find_name_component_bounds
4114 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4115 in completion mode. */
5c58de74
PA
4116
4117static bool
a3c5fafd 4118check_find_bounds_finds (mapped_index_base &index,
5c58de74 4119 const char *search_name,
fcf23d5b
SM
4120 gdb::array_view<const char *> expected_syms,
4121 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4122{
4123 lookup_name_info lookup_name (search_name,
4124 symbol_name_match_type::FULL, true);
4125
3b00ef10 4126 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4127 language_cplus,
4128 per_objfile);
5c58de74
PA
4129
4130 size_t distance = std::distance (bounds.first, bounds.second);
4131 if (distance != expected_syms.size ())
4132 return false;
4133
4134 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4135 {
4136 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4137 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4138 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4139 return false;
4140 }
4141
4142 return true;
4143}
4144
4145/* Test the lower-level mapped_index::find_name_component_bounds
4146 method. */
4147
c62446b1 4148static void
5c58de74
PA
4149test_mapped_index_find_name_component_bounds ()
4150{
4151 mock_mapped_index mock_index (test_symbols);
4152
fcf23d5b 4153 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4154
4155 /* Test the lower-level mapped_index::find_name_component_bounds
4156 method in completion mode. */
4157 {
4158 static const char *expected_syms[] = {
4159 "t1_func",
4160 "t1_func1",
5c58de74
PA
4161 };
4162
fcf23d5b
SM
4163 SELF_CHECK (check_find_bounds_finds
4164 (mock_index, "t1_func", expected_syms,
4165 NULL /* per_objfile */));
5c58de74
PA
4166 }
4167
4168 /* Check that the increment-last-char in the name matching algorithm
4169 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4170 {
4171 static const char *expected_syms1[] = {
4172 "\377",
4173 "\377\377123",
4174 };
fcf23d5b
SM
4175 SELF_CHECK (check_find_bounds_finds
4176 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4177
4178 static const char *expected_syms2[] = {
4179 "\377\377123",
4180 };
fcf23d5b
SM
4181 SELF_CHECK (check_find_bounds_finds
4182 (mock_index, "\377\377", expected_syms2,
4183 NULL /* per_objfile */));
5c58de74
PA
4184 }
4185}
4186
4187/* Test dw2_expand_symtabs_matching_symbol. */
4188
4189static void
4190test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4191{
4192 mock_mapped_index mock_index (test_symbols);
4193
4194 /* We let all tests run until the end even if some fails, for debug
4195 convenience. */
4196 bool any_mismatch = false;
4197
4198 /* Create the expected symbols list (an initializer_list). Needed
4199 because lists have commas, and we need to pass them to CHECK,
4200 which is a macro. */
4201#define EXPECT(...) { __VA_ARGS__ }
4202
4203 /* Wrapper for check_match that passes down the current
4204 __FILE__/__LINE__. */
4205#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4206 any_mismatch |= !check_match (__FILE__, __LINE__, \
4207 mock_index, \
4208 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4209 EXPECTED_LIST, NULL)
c62446b1
PA
4210
4211 /* Identity checks. */
4212 for (const char *sym : test_symbols)
4213 {
4214 /* Should be able to match all existing symbols. */
4215 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4216 EXPECT (sym));
4217
4218 /* Should be able to match all existing symbols with
4219 parameters. */
4220 std::string with_params = std::string (sym) + "(int)";
4221 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4222 EXPECT (sym));
4223
4224 /* Should be able to match all existing symbols with
4225 parameters and qualifiers. */
4226 with_params = std::string (sym) + " ( int ) const";
4227 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4228 EXPECT (sym));
4229
4230 /* This should really find sym, but cp-name-parser.y doesn't
4231 know about lvalue/rvalue qualifiers yet. */
4232 with_params = std::string (sym) + " ( int ) &&";
4233 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4234 {});
4235 }
4236
e1ef7d7a
PA
4237 /* Check that the name matching algorithm for completion doesn't get
4238 confused with Latin1 'ÿ' / 0xff. */
4239 {
4240 static const char str[] = "\377";
4241 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4242 EXPECT ("\377", "\377\377123"));
4243 }
4244
4245 /* Check that the increment-last-char in the matching algorithm for
4246 completion doesn't match "t1_fund" when completing "t1_func". */
4247 {
4248 static const char str[] = "t1_func";
4249 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4250 EXPECT ("t1_func", "t1_func1"));
4251 }
4252
c62446b1
PA
4253 /* Check that completion mode works at each prefix of the expected
4254 symbol name. */
4255 {
4256 static const char str[] = "function(int)";
4257 size_t len = strlen (str);
4258 std::string lookup;
4259
4260 for (size_t i = 1; i < len; i++)
4261 {
4262 lookup.assign (str, i);
4263 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4264 EXPECT ("function"));
4265 }
4266 }
4267
4268 /* While "w" is a prefix of both components, the match function
4269 should still only be called once. */
4270 {
4271 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4272 EXPECT ("w1::w2"));
a20714ff
PA
4273 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4274 EXPECT ("w1::w2"));
c62446b1
PA
4275 }
4276
4277 /* Same, with a "complicated" symbol. */
4278 {
4279 static const char str[] = Z_SYM_NAME;
4280 size_t len = strlen (str);
4281 std::string lookup;
4282
4283 for (size_t i = 1; i < len; i++)
4284 {
4285 lookup.assign (str, i);
4286 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4287 EXPECT (Z_SYM_NAME));
4288 }
4289 }
4290
4291 /* In FULL mode, an incomplete symbol doesn't match. */
4292 {
4293 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4294 {});
4295 }
4296
4297 /* A complete symbol with parameters matches any overload, since the
4298 index has no overload info. */
4299 {
4300 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4301 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4302 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4303 EXPECT ("std::zfunction", "std::zfunction2"));
4304 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4305 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4306 }
4307
4308 /* Check that whitespace is ignored appropriately. A symbol with a
4309 template argument list. */
4310 {
4311 static const char expected[] = "ns::foo<int>";
4312 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4313 EXPECT (expected));
a20714ff
PA
4314 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4315 EXPECT (expected));
c62446b1
PA
4316 }
4317
4318 /* Check that whitespace is ignored appropriately. A symbol with a
4319 template argument list that includes a pointer. */
4320 {
4321 static const char expected[] = "ns::foo<char*>";
4322 /* Try both completion and non-completion modes. */
4323 static const bool completion_mode[2] = {false, true};
4324 for (size_t i = 0; i < 2; i++)
4325 {
4326 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4327 completion_mode[i], EXPECT (expected));
a20714ff
PA
4328 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4329 completion_mode[i], EXPECT (expected));
c62446b1
PA
4330
4331 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4332 completion_mode[i], EXPECT (expected));
a20714ff
PA
4333 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4334 completion_mode[i], EXPECT (expected));
c62446b1
PA
4335 }
4336 }
4337
4338 {
4339 /* Check method qualifiers are ignored. */
4340 static const char expected[] = "ns::foo<char*>";
4341 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4342 symbol_name_match_type::FULL, true, EXPECT (expected));
4343 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4344 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4345 CHECK_MATCH ("foo < char * > ( int ) const",
4346 symbol_name_match_type::WILD, true, EXPECT (expected));
4347 CHECK_MATCH ("foo < char * > ( int ) &&",
4348 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4349 }
4350
4351 /* Test lookup names that don't match anything. */
4352 {
a20714ff
PA
4353 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4354 {});
4355
c62446b1
PA
4356 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4357 {});
4358 }
4359
a20714ff
PA
4360 /* Some wild matching tests, exercising "(anonymous namespace)",
4361 which should not be confused with a parameter list. */
4362 {
4363 static const char *syms[] = {
4364 "A::B::C",
4365 "B::C",
4366 "C",
4367 "A :: B :: C ( int )",
4368 "B :: C ( int )",
4369 "C ( int )",
4370 };
4371
4372 for (const char *s : syms)
4373 {
4374 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4375 EXPECT ("(anonymous namespace)::A::B::C"));
4376 }
4377 }
4378
4379 {
4380 static const char expected[] = "ns2::tmpl<int>::foo2";
4381 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4382 EXPECT (expected));
4383 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4384 EXPECT (expected));
4385 }
4386
c62446b1
PA
4387 SELF_CHECK (!any_mismatch);
4388
4389#undef EXPECT
4390#undef CHECK_MATCH
4391}
4392
5c58de74
PA
4393static void
4394run_test ()
4395{
4396 test_mapped_index_find_name_component_bounds ();
4397 test_dw2_expand_symtabs_matching_symbol ();
4398}
4399
c62446b1
PA
4400}} // namespace selftests::dw2_expand_symtabs_matching
4401
4402#endif /* GDB_SELF_TEST */
4403
4b514bc8
JK
4404/* If FILE_MATCHER is NULL or if PER_CU has
4405 dwarf2_per_cu_quick_data::MARK set (see
4406 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4407 EXPANSION_NOTIFY on it. */
4408
df35e626 4409static bool
4b514bc8 4410dw2_expand_symtabs_matching_one
97a1449a
SM
4411 (dwarf2_per_cu_data *per_cu,
4412 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4413 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4414 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4415{
4416 if (file_matcher == NULL || per_cu->v.quick->mark)
4417 {
af758d11 4418 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4419
97a1449a
SM
4420 compunit_symtab *symtab
4421 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4422 gdb_assert (symtab != nullptr);
4b514bc8 4423
af758d11 4424 if (expansion_notify != NULL && symtab_was_null)
df35e626 4425 return expansion_notify (symtab);
4b514bc8 4426 }
df35e626 4427 return true;
4b514bc8
JK
4428}
4429
3f563c84
PA
4430/* Helper for dw2_expand_matching symtabs. Called on each symbol
4431 matched, to expand corresponding CUs that were marked. IDX is the
4432 index of the symbol name that matched. */
4433
df35e626 4434static bool
3f563c84 4435dw2_expand_marked_cus
976ca316 4436 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4437 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4438 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4439 block_search_flags search_flags,
3f563c84
PA
4440 search_domain kind)
4441{
3f563c84
PA
4442 offset_type *vec, vec_len, vec_idx;
4443 bool global_seen = false;
976ca316 4444 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4445
61920122 4446 vec = (offset_type *) (index.constant_pool
f00a2de2 4447 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4448 vec_len = MAYBE_SWAP (vec[0]);
4449 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4450 {
61920122
PA
4451 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4452 /* This value is only valid for index versions >= 7. */
4453 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4454 gdb_index_symbol_kind symbol_kind =
4455 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4456 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4457 /* Only check the symbol attributes if they're present.
4458 Indices prior to version 7 don't record them,
4459 and indices >= 7 may elide them for certain symbols
4460 (gold does this). */
4461 int attrs_valid =
4462 (index.version >= 7
4463 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4464
4465 /* Work around gold/15646. */
f030440d
TV
4466 if (attrs_valid
4467 && !is_static
4468 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4469 {
f030440d 4470 if (global_seen)
61920122 4471 continue;
f030440d
TV
4472
4473 global_seen = true;
61920122 4474 }
3190f0c6 4475
61920122
PA
4476 /* Only check the symbol's kind if it has one. */
4477 if (attrs_valid)
4478 {
03a8ea51
TT
4479 if (is_static)
4480 {
4481 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4482 continue;
4483 }
4484 else
4485 {
4486 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4487 continue;
4488 }
4489
61920122 4490 switch (kind)
8943b874 4491 {
61920122
PA
4492 case VARIABLES_DOMAIN:
4493 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4494 continue;
4495 break;
4496 case FUNCTIONS_DOMAIN:
4497 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4498 continue;
61920122
PA
4499 break;
4500 case TYPES_DOMAIN:
4501 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4502 continue;
4503 break;
59c35742
AB
4504 case MODULES_DOMAIN:
4505 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4506 continue;
4507 break;
61920122
PA
4508 default:
4509 break;
8943b874 4510 }
61920122 4511 }
8943b874 4512
61920122 4513 /* Don't crash on bad data. */
976ca316
SM
4514 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4515 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4516 {
b98664d3 4517 complaint (_(".gdb_index entry has bad CU index"
976ca316 4518 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4519 continue;
4520 }
4521
976ca316 4522 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
df35e626
TT
4523 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4524 expansion_notify))
4525 return false;
61920122 4526 }
df35e626
TT
4527
4528 return true;
61920122
PA
4529}
4530
4b514bc8
JK
4531/* If FILE_MATCHER is non-NULL, set all the
4532 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4533 that match FILE_MATCHER. */
4534
61920122 4535static void
4b514bc8 4536dw_expand_symtabs_matching_file_matcher
976ca316 4537 (dwarf2_per_objfile *per_objfile,
ed2dc618 4538 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4539{
4b514bc8 4540 if (file_matcher == NULL)
61920122
PA
4541 return;
4542
4b514bc8
JK
4543 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4544 htab_eq_pointer,
4545 NULL, xcalloc, xfree));
4546 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4547 htab_eq_pointer,
4548 NULL, xcalloc, xfree));
61920122 4549
4b514bc8
JK
4550 /* The rule is CUs specify all the files, including those used by
4551 any TU, so there's no need to scan TUs here. */
61920122 4552
976ca316 4553 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4554 {
927aa2e7
JK
4555 QUIT;
4556
4557 per_cu->v.quick->mark = 0;
4558
4559 /* We only need to look at symtabs not already expanded. */
976ca316 4560 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4561 continue;
4562
976ca316 4563 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4564 if (file_data == NULL)
4565 continue;
4566
4567 if (htab_find (visited_not_found.get (), file_data) != NULL)
4568 continue;
4569 else if (htab_find (visited_found.get (), file_data) != NULL)
4570 {
4571 per_cu->v.quick->mark = 1;
4572 continue;
4573 }
4574
b76e467d 4575 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4576 {
4577 const char *this_real_name;
4578
4579 if (file_matcher (file_data->file_names[j], false))
4580 {
4581 per_cu->v.quick->mark = 1;
4582 break;
4583 }
4584
4585 /* Before we invoke realpath, which can get expensive when many
4586 files are involved, do a quick comparison of the basenames. */
4587 if (!basenames_may_differ
4588 && !file_matcher (lbasename (file_data->file_names[j]),
4589 true))
4590 continue;
4591
976ca316 4592 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4593 if (file_matcher (this_real_name, false))
4594 {
4595 per_cu->v.quick->mark = 1;
4596 break;
4597 }
4598 }
4599
b76e467d
SM
4600 void **slot = htab_find_slot (per_cu->v.quick->mark
4601 ? visited_found.get ()
4602 : visited_not_found.get (),
4603 file_data, INSERT);
927aa2e7
JK
4604 *slot = file_data;
4605 }
4606}
4607
df35e626 4608bool
af82f89d
TT
4609dwarf2_gdb_index::expand_symtabs_matching
4610 (struct objfile *objfile,
4611 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4612 const lookup_name_info *lookup_name,
4613 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4614 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4615 block_search_flags search_flags,
3bfa51a7 4616 domain_enum domain,
af82f89d 4617 enum search_domain kind)
927aa2e7 4618{
976ca316 4619 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4620
4621 /* index_table is NULL if OBJF_READNOW. */
976ca316 4622 if (!per_objfile->per_bfd->index_table)
df35e626 4623 return true;
927aa2e7 4624
976ca316 4625 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4626
c1a66c06
TV
4627 if (symbol_matcher == NULL && lookup_name == NULL)
4628 {
976ca316 4629 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4630 {
4631 QUIT;
4632
df35e626
TT
4633 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4634 file_matcher,
4635 expansion_notify))
4636 return false;
c1a66c06 4637 }
df35e626 4638 return true;
c1a66c06
TV
4639 }
4640
976ca316 4641 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4642
df35e626
TT
4643 bool result
4644 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4645 symbol_matcher,
4646 [&] (offset_type idx)
927aa2e7 4647 {
df35e626 4648 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
03a8ea51 4649 expansion_notify, search_flags, kind))
df35e626 4650 return false;
3b00ef10 4651 return true;
976ca316 4652 }, per_objfile);
df35e626
TT
4653
4654 return result;
927aa2e7
JK
4655}
4656
4657/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4658 symtab. */
4659
4660static struct compunit_symtab *
4661recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4662 CORE_ADDR pc)
4663{
4664 int i;
4665
4666 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4667 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4668 return cust;
4669
4670 if (cust->includes == NULL)
4671 return NULL;
4672
4673 for (i = 0; cust->includes[i]; ++i)
4674 {
4675 struct compunit_symtab *s = cust->includes[i];
4676
4677 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4678 if (s != NULL)
4679 return s;
4680 }
4681
4682 return NULL;
4683}
4684
39298a5d
TT
4685struct compunit_symtab *
4686dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4687 (struct objfile *objfile,
4688 struct bound_minimal_symbol msymbol,
4689 CORE_ADDR pc,
4690 struct obj_section *section,
4691 int warn_if_readin)
927aa2e7
JK
4692{
4693 struct dwarf2_per_cu_data *data;
4694 struct compunit_symtab *result;
4695
efd7398e
TT
4696 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4697 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4698 return NULL;
4699
b3b3bada 4700 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4701 data = ((struct dwarf2_per_cu_data *)
4702 addrmap_find (per_objfile->per_bfd->index_addrmap,
4703 pc - baseaddr));
927aa2e7
JK
4704 if (!data)
4705 return NULL;
4706
af758d11 4707 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4708 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4709 paddress (objfile->arch (), pc));
927aa2e7 4710
97a1449a
SM
4711 result = recursively_find_pc_sect_compunit_symtab
4712 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4713
927aa2e7
JK
4714 gdb_assert (result != NULL);
4715 return result;
4716}
4717
39298a5d 4718void
f4655dee
TT
4719dwarf2_base_index_functions::map_symbol_filenames
4720 (struct objfile *objfile,
4721 gdb::function_view<symbol_filename_ftype> fun,
4722 bool need_fullname)
927aa2e7 4723{
976ca316 4724 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4725
976ca316 4726 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4727 {
976ca316 4728 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4729
4730 htab_up visited (htab_create_alloc (10,
4731 htab_hash_pointer, htab_eq_pointer,
4732 NULL, xcalloc, xfree));
4733
4734 /* The rule is CUs specify all the files, including those used
4735 by any TU, so there's no need to scan TUs here. We can
4736 ignore file names coming from already-expanded CUs. */
4737
976ca316 4738 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4739 {
976ca316 4740 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4741 {
4742 void **slot = htab_find_slot (visited.get (),
4743 per_cu->v.quick->file_names,
4744 INSERT);
4745
4746 *slot = per_cu->v.quick->file_names;
4747 }
4748 }
4749
976ca316 4750 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4751 {
927aa2e7 4752 /* We only need to look at symtabs not already expanded. */
976ca316 4753 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4754 continue;
4755
ab432490 4756 quick_file_names *file_data
976ca316 4757 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4758 if (file_data == NULL)
4759 continue;
4760
b76e467d 4761 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4762 if (*slot)
4763 {
4764 /* Already visited. */
4765 continue;
4766 }
4767 *slot = file_data;
4768
4769 for (int j = 0; j < file_data->num_file_names; ++j)
4770 {
4771 const char *filename = file_data->file_names[j];
976ca316 4772 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4773 }
4774 }
4775 }
4776
976ca316 4777 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4778 {
4779 gdb::unique_xmalloc_ptr<char> this_real_name;
4780
4781 if (need_fullname)
4782 this_real_name = gdb_realpath (filename);
f4655dee 4783 fun (filename, this_real_name.get ());
927aa2e7
JK
4784 });
4785}
4786
39298a5d
TT
4787bool
4788dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 4789{
fae2120b 4790 return true;
927aa2e7
JK
4791}
4792
927aa2e7
JK
4793/* DWARF-5 debug_names reader. */
4794
4795/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4796static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4797
4798/* A helper function that reads the .debug_names section in SECTION
4799 and fills in MAP. FILENAME is the name of the file containing the
4800 section; it is used for error reporting.
4801
4802 Returns true if all went well, false otherwise. */
4803
4804static bool
4805read_debug_names_from_section (struct objfile *objfile,
4806 const char *filename,
4807 struct dwarf2_section_info *section,
4808 mapped_debug_names &map)
4809{
96b79293 4810 if (section->empty ())
927aa2e7
JK
4811 return false;
4812
4813 /* Older elfutils strip versions could keep the section in the main
4814 executable while splitting it for the separate debug info file. */
96b79293 4815 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4816 return false;
4817
96b79293 4818 section->read (objfile);
927aa2e7 4819
08feed99 4820 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4821
4822 const gdb_byte *addr = section->buffer;
4823
96b79293 4824 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4825
4826 unsigned int bytes_read;
4827 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4828 addr += bytes_read;
4829
4830 map.dwarf5_is_dwarf64 = bytes_read != 4;
4831 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4832 if (bytes_read + length != section->size)
4833 {
4834 /* There may be multiple per-CU indices. */
4835 warning (_("Section .debug_names in %s length %s does not match "
4836 "section length %s, ignoring .debug_names."),
4837 filename, plongest (bytes_read + length),
4838 pulongest (section->size));
4839 return false;
4840 }
4841
4842 /* The version number. */
4843 uint16_t version = read_2_bytes (abfd, addr);
4844 addr += 2;
4845 if (version != 5)
4846 {
4847 warning (_("Section .debug_names in %s has unsupported version %d, "
4848 "ignoring .debug_names."),
4849 filename, version);
4850 return false;
4851 }
4852
4853 /* Padding. */
4854 uint16_t padding = read_2_bytes (abfd, addr);
4855 addr += 2;
4856 if (padding != 0)
4857 {
4858 warning (_("Section .debug_names in %s has unsupported padding %d, "
4859 "ignoring .debug_names."),
4860 filename, padding);
4861 return false;
4862 }
4863
4864 /* comp_unit_count - The number of CUs in the CU list. */
4865 map.cu_count = read_4_bytes (abfd, addr);
4866 addr += 4;
4867
4868 /* local_type_unit_count - The number of TUs in the local TU
4869 list. */
4870 map.tu_count = read_4_bytes (abfd, addr);
4871 addr += 4;
4872
4873 /* foreign_type_unit_count - The number of TUs in the foreign TU
4874 list. */
4875 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4876 addr += 4;
4877 if (foreign_tu_count != 0)
4878 {
4879 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4880 "ignoring .debug_names."),
4881 filename, static_cast<unsigned long> (foreign_tu_count));
4882 return false;
4883 }
4884
4885 /* bucket_count - The number of hash buckets in the hash lookup
4886 table. */
4887 map.bucket_count = read_4_bytes (abfd, addr);
4888 addr += 4;
4889
4890 /* name_count - The number of unique names in the index. */
4891 map.name_count = read_4_bytes (abfd, addr);
4892 addr += 4;
4893
4894 /* abbrev_table_size - The size in bytes of the abbreviations
4895 table. */
4896 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4897 addr += 4;
4898
4899 /* augmentation_string_size - The size in bytes of the augmentation
4900 string. This value is rounded up to a multiple of 4. */
4901 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4902 addr += 4;
4903 map.augmentation_is_gdb = ((augmentation_string_size
4904 == sizeof (dwarf5_augmentation))
4905 && memcmp (addr, dwarf5_augmentation,
4906 sizeof (dwarf5_augmentation)) == 0);
4907 augmentation_string_size += (-augmentation_string_size) & 3;
4908 addr += augmentation_string_size;
4909
4910 /* List of CUs */
4911 map.cu_table_reordered = addr;
4912 addr += map.cu_count * map.offset_size;
4913
4914 /* List of Local TUs */
4915 map.tu_table_reordered = addr;
4916 addr += map.tu_count * map.offset_size;
4917
4918 /* Hash Lookup Table */
4919 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4920 addr += map.bucket_count * 4;
4921 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4922 addr += map.name_count * 4;
4923
4924 /* Name Table */
4925 map.name_table_string_offs_reordered = addr;
4926 addr += map.name_count * map.offset_size;
4927 map.name_table_entry_offs_reordered = addr;
4928 addr += map.name_count * map.offset_size;
4929
4930 const gdb_byte *abbrev_table_start = addr;
4931 for (;;)
4932 {
927aa2e7
JK
4933 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4934 addr += bytes_read;
4935 if (index_num == 0)
4936 break;
4937
4938 const auto insertpair
4939 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4940 if (!insertpair.second)
4941 {
4942 warning (_("Section .debug_names in %s has duplicate index %s, "
4943 "ignoring .debug_names."),
4944 filename, pulongest (index_num));
4945 return false;
4946 }
4947 mapped_debug_names::index_val &indexval = insertpair.first->second;
4948 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4949 addr += bytes_read;
4950
4951 for (;;)
4952 {
4953 mapped_debug_names::index_val::attr attr;
4954 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4955 addr += bytes_read;
4956 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4957 addr += bytes_read;
4958 if (attr.form == DW_FORM_implicit_const)
4959 {
4960 attr.implicit_const = read_signed_leb128 (abfd, addr,
4961 &bytes_read);
4962 addr += bytes_read;
4963 }
4964 if (attr.dw_idx == 0 && attr.form == 0)
4965 break;
4966 indexval.attr_vec.push_back (std::move (attr));
4967 }
4968 }
4969 if (addr != abbrev_table_start + abbrev_table_size)
4970 {
4971 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4972 "of size %s vs. written as %u, ignoring .debug_names."),
4973 filename, plongest (addr - abbrev_table_start),
4974 abbrev_table_size);
927aa2e7
JK
4975 return false;
4976 }
4977 map.entry_pool = addr;
4978
4979 return true;
4980}
4981
4982/* A helper for create_cus_from_debug_names that handles the MAP's CU
4983 list. */
4984
4985static void
168c9250 4986create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4987 const mapped_debug_names &map,
4988 dwarf2_section_info &section,
b76e467d 4989 bool is_dwz)
927aa2e7 4990{
3ee6bb11
TV
4991 if (!map.augmentation_is_gdb)
4992 {
fc9a13fb
TV
4993 for (uint32_t i = 0; i < map.cu_count; ++i)
4994 {
4995 sect_offset sect_off
4996 = (sect_offset) (extract_unsigned_integer
4997 (map.cu_table_reordered + i * map.offset_size,
4998 map.offset_size,
4999 map.dwarf5_byte_order));
5000 /* We don't know the length of the CU, because the CU list in a
5001 .debug_names index can be incomplete, so we can't use the start
5002 of the next CU as end of this CU. We create the CUs here with
5003 length 0, and in cutu_reader::cutu_reader we'll fill in the
5004 actual length. */
5005 dwarf2_per_cu_data *per_cu
5006 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5007 sect_off, 0);
5008 per_bfd->all_comp_units.push_back (per_cu);
5009 }
d3b54e63 5010 return;
3ee6bb11
TV
5011 }
5012
927aa2e7
JK
5013 sect_offset sect_off_prev;
5014 for (uint32_t i = 0; i <= map.cu_count; ++i)
5015 {
5016 sect_offset sect_off_next;
5017 if (i < map.cu_count)
5018 {
5019 sect_off_next
5020 = (sect_offset) (extract_unsigned_integer
5021 (map.cu_table_reordered + i * map.offset_size,
5022 map.offset_size,
5023 map.dwarf5_byte_order));
5024 }
5025 else
5026 sect_off_next = (sect_offset) section.size;
5027 if (i >= 1)
5028 {
5029 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5030 dwarf2_per_cu_data *per_cu
168c9250 5031 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5032 sect_off_prev, length);
168c9250 5033 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5034 }
5035 sect_off_prev = sect_off_next;
5036 }
5037}
5038
5039/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5040 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5041
5042static void
168c9250 5043create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5044 const mapped_debug_names &map,
5045 const mapped_debug_names &dwz_map)
5046{
168c9250
SM
5047 gdb_assert (per_bfd->all_comp_units.empty ());
5048 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5049
168c9250 5050 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5051 false /* is_dwz */);
927aa2e7
JK
5052
5053 if (dwz_map.cu_count == 0)
5054 return;
5055
168c9250
SM
5056 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5057 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5058 true /* is_dwz */);
927aa2e7
JK
5059}
5060
5061/* Read .debug_names. If everything went ok, initialize the "quick"
5062 elements of all the CUs and return true. Otherwise, return false. */
5063
5064static bool
976ca316 5065dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5066{
fcf23d5b
SM
5067 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5068 mapped_debug_names dwz_map;
976ca316 5069 struct objfile *objfile = per_objfile->objfile;
f8c41851 5070 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5071
5072 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
bab287cd 5073 &per_bfd->debug_names, *map))
927aa2e7
JK
5074 return false;
5075
5076 /* Don't use the index if it's empty. */
22ca247e 5077 if (map->name_count == 0)
927aa2e7
JK
5078 return false;
5079
5080 /* If there is a .dwz file, read it so we can get its CU list as
5081 well. */
f8c41851 5082 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5083 if (dwz != NULL)
5084 {
5085 if (!read_debug_names_from_section (objfile,
00f93c44 5086 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5087 &dwz->debug_names, dwz_map))
5088 {
5089 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5090 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5091 return false;
5092 }
5093 }
5094
f8c41851 5095 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5096
22ca247e 5097 if (map->tu_count != 0)
927aa2e7
JK
5098 {
5099 /* We can only handle a single .debug_types when we have an
5100 index. */
f8c41851 5101 if (per_bfd->types.size () != 1)
927aa2e7
JK
5102 return false;
5103
f8c41851 5104 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5105
5106 create_signatured_type_table_from_debug_names
f8c41851 5107 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5108 }
5109
f8c41851 5110 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5111
f8c41851
SM
5112 per_bfd->debug_names_table = std::move (map);
5113 per_bfd->using_index = 1;
5114 per_bfd->quick_file_names_table =
bab287cd 5115 create_quick_file_names_table (per_bfd->all_comp_units.size ());
927aa2e7
JK
5116
5117 return true;
5118}
5119
927aa2e7
JK
5120/* Type used to manage iterating over all CUs looking for a symbol for
5121 .debug_names. */
5122
5123class dw2_debug_names_iterator
5124{
5125public:
927aa2e7 5126 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5127 block_search_flags block_index,
2b79f376 5128 domain_enum domain,
fcf23d5b 5129 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5130 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5131 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5132 m_per_objfile (per_objfile)
927aa2e7
JK
5133 {}
5134
5135 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5136 search_domain search, uint32_t namei,
3bfa51a7
TT
5137 dwarf2_per_objfile *per_objfile,
5138 domain_enum domain = UNDEF_DOMAIN)
927aa2e7 5139 : m_map (map),
3bfa51a7 5140 m_domain (domain),
927aa2e7 5141 m_search (search),
fcf23d5b
SM
5142 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5143 m_per_objfile (per_objfile)
927aa2e7
JK
5144 {}
5145
3b00ef10 5146 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5147 block_search_flags block_index, domain_enum domain,
fcf23d5b 5148 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5149 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5150 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5151 m_per_objfile (per_objfile)
3b00ef10
TT
5152 {}
5153
927aa2e7
JK
5154 /* Return the next matching CU or NULL if there are no more. */
5155 dwarf2_per_cu_data *next ();
5156
5157private:
5158 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5159 const char *name,
5160 dwarf2_per_objfile *per_objfile);
927aa2e7 5161 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5162 uint32_t namei,
5163 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5164
5165 /* The internalized form of .debug_names. */
5166 const mapped_debug_names &m_map;
5167
03a8ea51
TT
5168 /* Restrict the search to these blocks. */
5169 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
5170 | SEARCH_STATIC_BLOCK);
927aa2e7
JK
5171
5172 /* The kind of symbol we're looking for. */
5173 const domain_enum m_domain = UNDEF_DOMAIN;
5174 const search_domain m_search = ALL_DOMAIN;
5175
5176 /* The list of CUs from the index entry of the symbol, or NULL if
5177 not found. */
5178 const gdb_byte *m_addr;
fcf23d5b
SM
5179
5180 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5181};
5182
5183const char *
fcf23d5b 5184mapped_debug_names::namei_to_name
976ca316 5185 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5186{
5187 const ULONGEST namei_string_offs
5188 = extract_unsigned_integer ((name_table_string_offs_reordered
5189 + namei * offset_size),
5190 offset_size,
5191 dwarf5_byte_order);
976ca316 5192 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5193}
5194
5195/* Find a slot in .debug_names for the object named NAME. If NAME is
5196 found, return pointer to its pool data. If NAME cannot be found,
5197 return NULL. */
5198
5199const gdb_byte *
5200dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5201 (const mapped_debug_names &map, const char *name,
5202 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5203{
5204 int (*cmp) (const char *, const char *);
5205
54ee4252 5206 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5207 if (current_language->la_language == language_cplus
5208 || current_language->la_language == language_fortran
5209 || current_language->la_language == language_d)
5210 {
5211 /* NAME is already canonical. Drop any qualifiers as
5212 .debug_names does not contain any. */
5213
5214 if (strchr (name, '(') != NULL)
5215 {
54ee4252 5216 without_params = cp_remove_params (name);
927aa2e7 5217 if (without_params != NULL)
54ee4252 5218 name = without_params.get ();
927aa2e7
JK
5219 }
5220 }
5221
5222 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5223
5224 const uint32_t full_hash = dwarf5_djb_hash (name);
5225 uint32_t namei
5226 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5227 (map.bucket_table_reordered
5228 + (full_hash % map.bucket_count)), 4,
5229 map.dwarf5_byte_order);
5230 if (namei == 0)
5231 return NULL;
5232 --namei;
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 for (;;)
5243 {
5244 const uint32_t namei_full_hash
5245 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5246 (map.hash_table_reordered + namei), 4,
5247 map.dwarf5_byte_order);
5248 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5249 return NULL;
5250
5251 if (full_hash == namei_full_hash)
5252 {
fcf23d5b 5253 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5254
5255#if 0 /* An expensive sanity check. */
5256 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5257 {
b98664d3 5258 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5259 "[in module %s]"),
5260 namei, objfile_name (dwarf2_per_objfile->objfile));
5261 return NULL;
5262 }
5263#endif
5264
5265 if (cmp (namei_string, name) == 0)
5266 {
5267 const ULONGEST namei_entry_offs
5268 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5269 + namei * map.offset_size),
5270 map.offset_size, map.dwarf5_byte_order);
5271 return map.entry_pool + namei_entry_offs;
5272 }
5273 }
5274
5275 ++namei;
5276 if (namei >= map.name_count)
5277 return NULL;
5278 }
5279}
5280
5281const gdb_byte *
5282dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5283 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5284{
5285 if (namei >= map.name_count)
5286 {
b98664d3 5287 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5288 "[in module %s]"),
5289 namei, map.name_count,
fcf23d5b 5290 objfile_name (per_objfile->objfile));
927aa2e7
JK
5291 return NULL;
5292 }
5293
5294 const ULONGEST namei_entry_offs
5295 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5296 + namei * map.offset_size),
5297 map.offset_size, map.dwarf5_byte_order);
5298 return map.entry_pool + namei_entry_offs;
5299}
5300
5301/* See dw2_debug_names_iterator. */
5302
5303dwarf2_per_cu_data *
5304dw2_debug_names_iterator::next ()
5305{
5306 if (m_addr == NULL)
5307 return NULL;
5308
fcf23d5b
SM
5309 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5310 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5311 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5312
5313 again:
5314
5315 unsigned int bytes_read;
5316 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5317 m_addr += bytes_read;
5318 if (abbrev == 0)
5319 return NULL;
5320
5321 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5322 if (indexval_it == m_map.abbrev_map.cend ())
5323 {
b98664d3 5324 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5325 "[in module %s]"),
ed2dc618 5326 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5327 return NULL;
5328 }
5329 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5330 enum class symbol_linkage {
5331 unknown,
5332 static_,
5333 extern_,
23c13d42 5334 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5335 dwarf2_per_cu_data *per_cu = NULL;
5336 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5337 {
5338 ULONGEST ull;
5339 switch (attr.form)
5340 {
5341 case DW_FORM_implicit_const:
5342 ull = attr.implicit_const;
5343 break;
5344 case DW_FORM_flag_present:
5345 ull = 1;
5346 break;
5347 case DW_FORM_udata:
5348 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5349 m_addr += bytes_read;
5350 break;
6dc55ce9 5351 case DW_FORM_ref4:
5352 ull = read_4_bytes (abfd, m_addr);
5353 m_addr += 4;
5354 break;
5355 case DW_FORM_ref8:
5356 ull = read_8_bytes (abfd, m_addr);
5357 m_addr += 8;
5358 break;
5359 case DW_FORM_ref_sig8:
5360 ull = read_8_bytes (abfd, m_addr);
5361 m_addr += 8;
5362 break;
927aa2e7 5363 default:
b98664d3 5364 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5365 dwarf_form_name (attr.form),
ed2dc618 5366 objfile_name (objfile));
927aa2e7
JK
5367 return NULL;
5368 }
5369 switch (attr.dw_idx)
5370 {
5371 case DW_IDX_compile_unit:
5372 /* Don't crash on bad data. */
bab287cd 5373 if (ull >= per_bfd->all_comp_units.size ())
927aa2e7 5374 {
b98664d3 5375 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5376 " [in module %s]"),
5377 pulongest (ull),
fcf23d5b 5378 objfile_name (objfile));
927aa2e7
JK
5379 continue;
5380 }
fcf23d5b 5381 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5382 break;
8af5c486
JK
5383 case DW_IDX_type_unit:
5384 /* Don't crash on bad data. */
fcf23d5b 5385 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5386 {
b98664d3 5387 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5388 " [in module %s]"),
5389 pulongest (ull),
fcf23d5b 5390 objfile_name (objfile));
8af5c486
JK
5391 continue;
5392 }
fcf23d5b 5393 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5394 break;
6dc55ce9 5395 case DW_IDX_die_offset:
5396 /* In a per-CU index (as opposed to a per-module index), index
5397 entries without CU attribute implicitly refer to the single CU. */
5398 if (per_cu == NULL)
fcf23d5b 5399 per_cu = per_bfd->get_cu (0);
6dc55ce9 5400 break;
927aa2e7
JK
5401 case DW_IDX_GNU_internal:
5402 if (!m_map.augmentation_is_gdb)
5403 break;
23c13d42 5404 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5405 break;
5406 case DW_IDX_GNU_external:
5407 if (!m_map.augmentation_is_gdb)
5408 break;
23c13d42 5409 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5410 break;
5411 }
5412 }
5413
5414 /* Skip if already read in. */
fcf23d5b 5415 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5416 goto again;
5417
5418 /* Check static vs global. */
03a8ea51 5419 if (symbol_linkage_ != symbol_linkage::unknown)
927aa2e7 5420 {
03a8ea51
TT
5421 if (symbol_linkage_ == symbol_linkage::static_)
5422 {
5423 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5424 goto again;
5425 }
5426 else
5427 {
5428 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5429 goto again;
5430 }
927aa2e7
JK
5431 }
5432
5433 /* Match dw2_symtab_iter_next, symbol_kind
5434 and debug_names::psymbol_tag. */
5435 switch (m_domain)
5436 {
5437 case VAR_DOMAIN:
5438 switch (indexval.dwarf_tag)
5439 {
5440 case DW_TAG_variable:
5441 case DW_TAG_subprogram:
5442 /* Some types are also in VAR_DOMAIN. */
5443 case DW_TAG_typedef:
5444 case DW_TAG_structure_type:
5445 break;
5446 default:
5447 goto again;
5448 }
5449 break;
5450 case STRUCT_DOMAIN:
5451 switch (indexval.dwarf_tag)
5452 {
5453 case DW_TAG_typedef:
5454 case DW_TAG_structure_type:
5455 break;
5456 default:
5457 goto again;
5458 }
5459 break;
5460 case LABEL_DOMAIN:
5461 switch (indexval.dwarf_tag)
5462 {
5463 case 0:
5464 case DW_TAG_variable:
5465 break;
5466 default:
5467 goto again;
5468 }
5469 break;
59c35742
AB
5470 case MODULE_DOMAIN:
5471 switch (indexval.dwarf_tag)
5472 {
5473 case DW_TAG_module:
5474 break;
5475 default:
5476 goto again;
5477 }
5478 break;
927aa2e7
JK
5479 default:
5480 break;
5481 }
5482
5483 /* Match dw2_expand_symtabs_matching, symbol_kind and
5484 debug_names::psymbol_tag. */
5485 switch (m_search)
4b514bc8 5486 {
927aa2e7
JK
5487 case VARIABLES_DOMAIN:
5488 switch (indexval.dwarf_tag)
4b514bc8 5489 {
927aa2e7
JK
5490 case DW_TAG_variable:
5491 break;
5492 default:
5493 goto again;
4b514bc8 5494 }
927aa2e7
JK
5495 break;
5496 case FUNCTIONS_DOMAIN:
5497 switch (indexval.dwarf_tag)
4b514bc8 5498 {
927aa2e7
JK
5499 case DW_TAG_subprogram:
5500 break;
5501 default:
5502 goto again;
4b514bc8 5503 }
927aa2e7
JK
5504 break;
5505 case TYPES_DOMAIN:
5506 switch (indexval.dwarf_tag)
5507 {
5508 case DW_TAG_typedef:
5509 case DW_TAG_structure_type:
5510 break;
5511 default:
5512 goto again;
5513 }
5514 break;
59c35742
AB
5515 case MODULES_DOMAIN:
5516 switch (indexval.dwarf_tag)
5517 {
5518 case DW_TAG_module:
5519 break;
5520 default:
5521 goto again;
5522 }
927aa2e7
JK
5523 default:
5524 break;
4b514bc8 5525 }
927aa2e7
JK
5526
5527 return per_cu;
4b514bc8 5528}
61920122 5529
927aa2e7
JK
5530/* This dumps minimal information about .debug_names. It is called
5531 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5532 uses this to verify that .debug_names has been loaded. */
9291a0cd 5533
39298a5d
TT
5534void
5535dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5536{
976ca316 5537 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5538
976ca316 5539 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5540 printf_filtered (".debug_names:");
976ca316 5541 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5542 printf_filtered (" exists\n");
5543 else
5544 printf_filtered (" faked for \"readnow\"\n");
5545 printf_filtered ("\n");
9291a0cd
TT
5546}
5547
39298a5d
TT
5548void
5549dwarf2_debug_names_index::map_matching_symbols
3b00ef10
TT
5550 (struct objfile *objfile,
5551 const lookup_name_info &name, domain_enum domain,
5552 int global,
5553 gdb::function_view<symbol_found_callback_ftype> callback,
5554 symbol_compare_ftype *ordered_compare)
5555{
976ca316 5556 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5557
5558 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5559 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5560 return;
5561
976ca316 5562 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10 5563 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
03a8ea51
TT
5564 const block_search_flags block_flags
5565 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
3b00ef10
TT
5566
5567 const char *match_name = name.ada ().lookup_name ().c_str ();
5568 auto matcher = [&] (const char *symname)
5569 {
5570 if (ordered_compare == nullptr)
5571 return true;
5572 return ordered_compare (symname, match_name) == 0;
5573 };
5574
b64f703b 5575 dw2_expand_symtabs_matching_symbol (map, name, matcher,
3b00ef10
TT
5576 [&] (offset_type namei)
5577 {
5578 /* The name was matched, now expand corresponding CUs that were
5579 marked. */
03a8ea51 5580 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
976ca316 5581 per_objfile);
3b00ef10
TT
5582
5583 struct dwarf2_per_cu_data *per_cu;
5584 while ((per_cu = iter.next ()) != NULL)
976ca316 5585 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5586 nullptr);
3b00ef10 5587 return true;
976ca316 5588 }, per_objfile);
3b00ef10
TT
5589
5590 /* It's a shame we couldn't do this inside the
5591 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5592 that have already been expanded. Instead, this loop matches what
5593 the psymtab code does. */
976ca316 5594 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5595 {
976ca316 5596 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5597 if (symtab != nullptr)
3b00ef10
TT
5598 {
5599 const struct block *block
af758d11 5600 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5601 if (!iterate_over_symbols_terminated (block, name,
5602 domain, callback))
5603 break;
5604 }
5605 }
5606}
5607
df35e626 5608bool
39298a5d 5609dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5610 (struct objfile *objfile,
5611 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5612 const lookup_name_info *lookup_name,
927aa2e7
JK
5613 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5614 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 5615 block_search_flags search_flags,
3bfa51a7 5616 domain_enum domain,
927aa2e7
JK
5617 enum search_domain kind)
5618{
976ca316 5619 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5620
927aa2e7 5621 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5622 if (!per_objfile->per_bfd->debug_names_table)
df35e626 5623 return true;
9291a0cd 5624
976ca316 5625 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5626
c1a66c06
TV
5627 if (symbol_matcher == NULL && lookup_name == NULL)
5628 {
976ca316 5629 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5630 {
5631 QUIT;
5632
df35e626
TT
5633 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5634 file_matcher,
5635 expansion_notify))
5636 return false;
c1a66c06 5637 }
df35e626 5638 return true;
c1a66c06
TV
5639 }
5640
976ca316 5641 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5642
df35e626
TT
5643 bool result
5644 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5645 symbol_matcher,
5646 [&] (offset_type namei)
927aa2e7 5647 {
927aa2e7
JK
5648 /* The name was matched, now expand corresponding CUs that were
5649 marked. */
3bfa51a7 5650 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
bbf2f4df 5651
927aa2e7
JK
5652 struct dwarf2_per_cu_data *per_cu;
5653 while ((per_cu = iter.next ()) != NULL)
df35e626
TT
5654 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5655 file_matcher,
5656 expansion_notify))
5657 return false;
3b00ef10 5658 return true;
976ca316 5659 }, per_objfile);
df35e626
TT
5660
5661 return result;
9291a0cd
TT
5662}
5663
4485a1c1 5664/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5665 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5666
5667template <typename T>
5668static gdb::array_view<const gdb_byte>
5669get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5670{
5671 dwarf2_section_info *section = &section_owner->gdb_index;
5672
96b79293 5673 if (section->empty ())
4485a1c1
SM
5674 return {};
5675
5676 /* Older elfutils strip versions could keep the section in the main
5677 executable while splitting it for the separate debug info file. */
96b79293 5678 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5679 return {};
5680
96b79293 5681 section->read (obj);
4485a1c1 5682
8bebfcda
PA
5683 /* dwarf2_section_info::size is a bfd_size_type, while
5684 gdb::array_view works with size_t. On 32-bit hosts, with
5685 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5686 is 32-bit. So we need an explicit narrowing conversion here.
5687 This is fine, because it's impossible to allocate or mmap an
5688 array/buffer larger than what size_t can represent. */
5689 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5690}
5691
87d6a7aa
SM
5692/* Lookup the index cache for the contents of the index associated to
5693 DWARF2_OBJ. */
5694
5695static gdb::array_view<const gdb_byte>
5989a64e 5696get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5697{
5698 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5699 if (build_id == nullptr)
5700 return {};
5701
5702 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5703 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5704}
5705
5706/* Same as the above, but for DWZ. */
5707
5708static gdb::array_view<const gdb_byte>
5709get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5710{
5711 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5712 if (build_id == nullptr)
5713 return {};
5714
5715 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5716}
5717
edc02ceb 5718/* See dwarf2/public.h. */
9291a0cd 5719
edc02ceb
TT
5720void
5721dwarf2_initialize_objfile (struct objfile *objfile)
9291a0cd 5722{
976ca316
SM
5723 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5724 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5725
850ed749
SM
5726 dwarf_read_debug_printf ("called");
5727
9291a0cd
TT
5728 /* If we're about to read full symbols, don't bother with the
5729 indices. In this case we also don't care if some other debug
5730 format is making psymtabs, because they are all about to be
5731 expanded anyway. */
5732 if ((objfile->flags & OBJF_READNOW))
5733 {
850ed749
SM
5734 dwarf_read_debug_printf ("readnow requested");
5735
17ee85fc
TT
5736 /* When using READNOW, the using_index flag (set below) indicates that
5737 PER_BFD was already initialized, when we loaded some other objfile. */
5738 if (per_bfd->using_index)
5739 {
850ed749 5740 dwarf_read_debug_printf ("using_index already set");
976ca316 5741 per_objfile->resize_symtabs ();
edc02ceb
TT
5742 objfile->qf.push_front (make_dwarf_gdb_index ());
5743 return;
17ee85fc
TT
5744 }
5745
5746 per_bfd->using_index = 1;
976ca316
SM
5747 create_all_comp_units (per_objfile);
5748 create_all_type_units (per_objfile);
17ee85fc
TT
5749 per_bfd->quick_file_names_table
5750 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5751 per_objfile->resize_symtabs ();
9291a0cd 5752
17ee85fc
TT
5753 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5754 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 5755 {
17ee85fc 5756 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 5757
17ee85fc 5758 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5759 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5760 }
5761
edc02ceb
TT
5762 /* Arrange for gdb to see the "quick" functions. However, these
5763 functions will be no-ops because we will have expanded all
5764 symtabs. */
5765 objfile->qf.push_front (make_dwarf_gdb_index ());
5766 return;
9291a0cd
TT
5767 }
5768
17ee85fc
TT
5769 /* Was a debug names index already read when we processed an objfile sharing
5770 PER_BFD? */
5771 if (per_bfd->debug_names_table != nullptr)
5772 {
850ed749 5773 dwarf_read_debug_printf ("re-using shared debug names table");
976ca316 5774 per_objfile->resize_symtabs ();
edc02ceb
TT
5775 objfile->qf.push_front (make_dwarf_debug_names ());
5776 return;
17ee85fc
TT
5777 }
5778
5779 /* Was a GDB index already read when we processed an objfile sharing
5780 PER_BFD? */
5781 if (per_bfd->index_table != nullptr)
5782 {
850ed749 5783 dwarf_read_debug_printf ("re-using shared index table");
976ca316 5784 per_objfile->resize_symtabs ();
edc02ceb
TT
5785 objfile->qf.push_front (make_dwarf_gdb_index ());
5786 return;
17ee85fc
TT
5787 }
5788
efb763a5
SM
5789 /* There might already be partial symtabs built for this BFD. This happens
5790 when loading the same binary twice with the index-cache enabled. If so,
5791 don't try to read an index. The objfile / per_objfile initialization will
5792 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5793 code path. */
5794 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
5795 {
5796 dwarf_read_debug_printf ("re-using shared partial symtabs");
edc02ceb
TT
5797 objfile->qf.push_front (make_lazy_dwarf_reader ());
5798 return;
850ed749 5799 }
efb763a5 5800
976ca316 5801 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 5802 {
850ed749 5803 dwarf_read_debug_printf ("found debug names");
976ca316 5804 per_objfile->resize_symtabs ();
edc02ceb
TT
5805 objfile->qf.push_front (make_dwarf_debug_names ());
5806 return;
3c0aa29a 5807 }
927aa2e7 5808
976ca316 5809 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5810 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5811 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 5812 {
850ed749 5813 dwarf_read_debug_printf ("found gdb index from file");
976ca316 5814 per_objfile->resize_symtabs ();
edc02ceb
TT
5815 objfile->qf.push_front (make_dwarf_gdb_index ());
5816 return;
3c0aa29a 5817 }
9291a0cd 5818
87d6a7aa 5819 /* ... otherwise, try to find the index in the index cache. */
976ca316 5820 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
5821 get_gdb_index_contents_from_cache,
5822 get_gdb_index_contents_from_cache_dwz))
5823 {
850ed749 5824 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa 5825 global_index_cache.hit ();
976ca316 5826 per_objfile->resize_symtabs ();
edc02ceb
TT
5827 objfile->qf.push_front (make_dwarf_gdb_index ());
5828 return;
87d6a7aa
SM
5829 }
5830
5831 global_index_cache.miss ();
edc02ceb 5832 objfile->qf.push_front (make_lazy_dwarf_reader ());
9291a0cd
TT
5833}
5834
5835\f
5836
dce234bc
PP
5837/* Build a partial symbol table. */
5838
5839void
eb36a3eb 5840dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
dce234bc 5841{
976ca316
SM
5842 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5843 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
5844
5845 if (per_bfd->partial_symtabs != nullptr)
5846 {
5847 /* Partial symbols were already read, so now we can simply
5848 attach them. */
eb36a3eb
TT
5849 if (psf == nullptr)
5850 {
5851 psf = new psymbol_functions (per_bfd->partial_symtabs);
5852 objfile->qf.emplace_front (psf);
5853 }
5854 else
5855 psf->set_partial_symtabs (per_bfd->partial_symtabs);
976ca316 5856 per_objfile->resize_symtabs ();
17ee85fc
TT
5857 return;
5858 }
c9bf0622 5859
eb36a3eb
TT
5860 if (psf == nullptr)
5861 {
5862 psf = new psymbol_functions;
5863 objfile->qf.emplace_front (psf);
5864 }
5865 const std::shared_ptr<psymtab_storage> &partial_symtabs
5866 = psf->get_partial_symtabs ();
5867
51962708
TT
5868 /* Set the local reference to partial symtabs, so that we don't try
5869 to read them again if reading another objfile with the same BFD.
5870 If we can't in fact share, this won't make a difference anyway as
5871 the dwarf2_per_bfd object won't be shared. */
eb36a3eb 5872 per_bfd->partial_symtabs = partial_symtabs;
51962708 5873
a70b8144 5874 try
c9bf0622
TT
5875 {
5876 /* This isn't really ideal: all the data we allocate on the
5877 objfile's obstack is still uselessly kept around. However,
5878 freeing it seems unsafe. */
eb36a3eb 5879 psymtab_discarder psymtabs (partial_symtabs.get ());
976ca316 5880 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 5881 psymtabs.keep ();
87d6a7aa 5882
976ca316 5883 per_objfile->resize_symtabs ();
af758d11 5884
87d6a7aa 5885 /* (maybe) store an index in the cache. */
976ca316 5886 global_index_cache.store (per_objfile);
c9bf0622 5887 }
230d2906 5888 catch (const gdb_exception_error &except)
492d29ea
PA
5889 {
5890 exception_print (gdb_stderr, except);
5891 }
c906108c 5892}
c906108c 5893
3b80fe9b
DE
5894/* Find the base address of the compilation unit for range lists and
5895 location lists. It will normally be specified by DW_AT_low_pc.
5896 In DWARF-3 draft 4, the base address could be overridden by
5897 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5898 compilation units with discontinuous ranges. */
5899
5900static void
5901dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5902{
5903 struct attribute *attr;
5904
2b24b6e4 5905 cu->base_address.reset ();
3b80fe9b
DE
5906
5907 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5908 if (attr != nullptr)
95f982e5 5909 cu->base_address = attr->as_address ();
3b80fe9b
DE
5910 else
5911 {
5912 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5913 if (attr != nullptr)
95f982e5 5914 cu->base_address = attr->as_address ();
3b80fe9b
DE
5915 }
5916}
5917
36586728
TT
5918/* Helper function that returns the proper abbrev section for
5919 THIS_CU. */
5920
5921static struct dwarf2_section_info *
5922get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5923{
5924 struct dwarf2_section_info *abbrev;
c3699833 5925 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
5926
5927 if (this_cu->is_dwz)
a7308ce0 5928 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 5929 else
c3699833 5930 abbrev = &per_bfd->abbrev;
36586728
TT
5931
5932 return abbrev;
5933}
5934
f4dc4d17
DE
5935/* Fetch the abbreviation table offset from a comp or type unit header. */
5936
5937static sect_offset
976ca316 5938read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 5939 struct dwarf2_section_info *section,
9c541725 5940 sect_offset sect_off)
f4dc4d17 5941{
96b79293 5942 bfd *abfd = section->get_bfd_owner ();
d521ce57 5943 const gdb_byte *info_ptr;
ac298888 5944 unsigned int initial_length_size, offset_size;
43988095 5945 uint16_t version;
f4dc4d17 5946
976ca316 5947 section->read (per_objfile->objfile);
9c541725 5948 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5949 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5950 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5951 info_ptr += initial_length_size;
5952
5953 version = read_2_bytes (abfd, info_ptr);
5954 info_ptr += 2;
5955 if (version >= 5)
5956 {
5957 /* Skip unit type and address size. */
5958 info_ptr += 2;
5959 }
5960
24aa364d 5961 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5962}
5963
b83470bf
TT
5964/* A partial symtab that is used only for include files. */
5965struct dwarf2_include_psymtab : public partial_symtab
5966{
7b249e47
TT
5967 dwarf2_include_psymtab (const char *filename,
5968 psymtab_storage *partial_symtabs,
0072c873
SM
5969 objfile_per_bfd_storage *objfile_per_bfd)
5970 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
b83470bf
TT
5971 {
5972 }
5973
5974 void read_symtab (struct objfile *objfile) override
5975 {
194d088f
TV
5976 /* It's an include file, no symbols to read for it.
5977 Everything is in the includer symtab. */
5978
5979 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5980 expansion of the includer psymtab. We use the dependencies[0] field to
5981 model the includer. But if we go the regular route of calling
5982 expand_psymtab here, and having expand_psymtab call expand_dependencies
5983 to expand the includer, we'll only use expand_psymtab on the includer
5984 (making it a non-toplevel psymtab), while if we expand the includer via
5985 another path, we'll use read_symtab (making it a toplevel psymtab).
5986 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5987 psymtab, and trigger read_symtab on the includer here directly. */
5988 includer ()->read_symtab (objfile);
b83470bf
TT
5989 }
5990
5991 void expand_psymtab (struct objfile *objfile) override
5992 {
194d088f
TV
5993 /* This is not called by read_symtab, and should not be called by any
5994 expand_dependencies. */
5995 gdb_assert (false);
b83470bf
TT
5996 }
5997
5717c425 5998 bool readin_p (struct objfile *objfile) const override
b83470bf 5999 {
5717c425 6000 return includer ()->readin_p (objfile);
b83470bf
TT
6001 }
6002
5717c425 6003 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6004 {
6005 return nullptr;
6006 }
6007
6008private:
194d088f
TV
6009 partial_symtab *includer () const
6010 {
6011 /* An include psymtab has exactly one dependency: the psymtab that
6012 includes it. */
6013 gdb_assert (this->number_of_dependencies == 1);
6014 return this->dependencies[0];
6015 }
b83470bf
TT
6016};
6017
aaa75496
JB
6018/* Allocate a new partial symtab for file named NAME and mark this new
6019 partial symtab as being an include of PST. */
6020
6021static void
84685904 6022dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
7b249e47
TT
6023 const char *name,
6024 dwarf2_psymtab *pst,
6025 psymtab_storage *partial_symtabs,
0072c873 6026 objfile_per_bfd_storage *objfile_per_bfd)
aaa75496 6027{
7b249e47 6028 dwarf2_include_psymtab *subpst
0072c873 6029 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
aaa75496 6030
fbd9ab74 6031 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6032 subpst->dirname = pst->dirname;
fbd9ab74 6033
84685904 6034 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6035 subpst->dependencies[0] = pst;
6036 subpst->number_of_dependencies = 1;
aaa75496
JB
6037}
6038
6039/* Read the Line Number Program data and extract the list of files
6040 included by the source file represented by PST. Build an include
d85a05f0 6041 partial symtab for each of these included files. */
aaa75496
JB
6042
6043static void
6044dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6045 struct die_info *die,
891813be 6046 dwarf2_psymtab *pst)
aaa75496 6047{
fff8551c 6048 line_header_up lh;
d85a05f0 6049 struct attribute *attr;
aaa75496 6050
d85a05f0 6051 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6052 if (attr != nullptr && attr->form_is_unsigned ())
6053 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6054 if (lh == NULL)
6055 return; /* No linetable, so no includes. */
6056
79748972
TT
6057 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6058 that we pass in the raw text_low here; that is ok because we're
6059 only decoding the line table to make include partial symtabs, and
6060 so the addresses aren't really used. */
4ae976d1 6061 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6062 pst->raw_text_low (), 1);
aaa75496
JB
6063}
6064
348e048f 6065static hashval_t
52dc124a 6066hash_signatured_type (const void *item)
348e048f 6067{
9a3c8263
SM
6068 const struct signatured_type *sig_type
6069 = (const struct signatured_type *) item;
9a619af0 6070
348e048f 6071 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6072 return sig_type->signature;
348e048f
DE
6073}
6074
6075static int
52dc124a 6076eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6077{
9a3c8263
SM
6078 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6079 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6080
348e048f
DE
6081 return lhs->signature == rhs->signature;
6082}
6083
1fd400ff
TT
6084/* Allocate a hash table for signatured types. */
6085
b0b6a987 6086static htab_up
298e9637 6087allocate_signatured_type_table ()
1fd400ff 6088{
b0b6a987
TT
6089 return htab_up (htab_create_alloc (41,
6090 hash_signatured_type,
6091 eq_signatured_type,
6092 NULL, xcalloc, xfree));
1fd400ff
TT
6093}
6094
d467dd73 6095/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6096
6097static int
d467dd73 6098add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6099{
9a3c8263 6100 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6101 std::vector<signatured_type *> *all_type_units
6102 = (std::vector<signatured_type *> *) datum;
1fd400ff 6103
b2bdb8cf 6104 all_type_units->push_back (sigt);
1fd400ff
TT
6105
6106 return 1;
6107}
6108
78d4d2c5 6109/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6110 and fill them into TYPES_HTAB. It will process only type units,
6111 therefore DW_UT_type. */
c88ee1f0 6112
78d4d2c5 6113static void
976ca316 6114create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6115 struct dwo_file *dwo_file,
b0b6a987 6116 dwarf2_section_info *section, htab_up &types_htab,
43988095 6117 rcuh_kind section_kind)
348e048f 6118{
976ca316 6119 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6120 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6121 bfd *abfd;
6122 const gdb_byte *info_ptr, *end_ptr;
348e048f 6123
4bdcc0c1
DE
6124 abbrev_section = (dwo_file != NULL
6125 ? &dwo_file->sections.abbrev
976ca316 6126 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6127
4800761a 6128 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
6129 section->get_name (),
6130 abbrev_section->get_file_name ());
09406207 6131
96b79293 6132 section->read (objfile);
78d4d2c5 6133 info_ptr = section->buffer;
348e048f 6134
78d4d2c5
JK
6135 if (info_ptr == NULL)
6136 return;
348e048f 6137
78d4d2c5
JK
6138 /* We can't set abfd until now because the section may be empty or
6139 not present, in which case the bfd is unknown. */
96b79293 6140 abfd = section->get_bfd_owner ();
348e048f 6141
c0ab21c2
TT
6142 /* We don't use cutu_reader here because we don't need to read
6143 any dies: the signature is in the header. */
3019eac3 6144
78d4d2c5
JK
6145 end_ptr = info_ptr + section->size;
6146 while (info_ptr < end_ptr)
6147 {
78d4d2c5
JK
6148 struct signatured_type *sig_type;
6149 struct dwo_unit *dwo_tu;
6150 void **slot;
6151 const gdb_byte *ptr = info_ptr;
6152 struct comp_unit_head header;
6153 unsigned int length;
8b70b953 6154
9c541725 6155 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6156
a49dd8dd
JK
6157 /* Initialize it due to a false compiler warning. */
6158 header.signature = -1;
9c541725 6159 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6160
78d4d2c5
JK
6161 /* We need to read the type's signature in order to build the hash
6162 table, but we don't need anything else just yet. */
348e048f 6163
976ca316 6164 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6165 abbrev_section, ptr, section_kind);
348e048f 6166
4057dfde 6167 length = header.get_length ();
6caca83c 6168
78d4d2c5
JK
6169 /* Skip dummy type units. */
6170 if (ptr >= info_ptr + length
43988095 6171 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6172 || (header.unit_type != DW_UT_type
6173 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6174 {
6175 info_ptr += length;
6176 continue;
6177 }
dee91e82 6178
78d4d2c5
JK
6179 if (types_htab == NULL)
6180 {
6181 if (dwo_file)
298e9637 6182 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6183 else
298e9637 6184 types_htab = allocate_signatured_type_table ();
78d4d2c5 6185 }
8b70b953 6186
78d4d2c5
JK
6187 if (dwo_file)
6188 {
6189 sig_type = NULL;
976ca316 6190 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6191 dwo_tu->dwo_file = dwo_file;
43988095 6192 dwo_tu->signature = header.signature;
9c541725 6193 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6194 dwo_tu->section = section;
9c541725 6195 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6196 dwo_tu->length = length;
6197 }
6198 else
6199 {
6200 /* N.B.: type_offset is not usable if this type uses a DWO file.
6201 The real type_offset is in the DWO file. */
6202 dwo_tu = NULL;
976ca316 6203 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6204 sig_type->signature = header.signature;
9c541725 6205 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6206 sig_type->per_cu.is_debug_types = 1;
6207 sig_type->per_cu.section = section;
9c541725 6208 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6209 sig_type->per_cu.length = length;
6210 }
6211
b0b6a987 6212 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6213 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6214 INSERT);
6215 gdb_assert (slot != NULL);
6216 if (*slot != NULL)
6217 {
9c541725 6218 sect_offset dup_sect_off;
0349ea22 6219
3019eac3
DE
6220 if (dwo_file)
6221 {
78d4d2c5
JK
6222 const struct dwo_unit *dup_tu
6223 = (const struct dwo_unit *) *slot;
6224
9c541725 6225 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6226 }
6227 else
6228 {
78d4d2c5
JK
6229 const struct signatured_type *dup_tu
6230 = (const struct signatured_type *) *slot;
6231
9c541725 6232 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6233 }
8b70b953 6234
b98664d3 6235 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6236 " the entry at offset %s, signature %s"),
6237 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6238 hex_string (header.signature));
78d4d2c5
JK
6239 }
6240 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6241
6f738b01
SM
6242 dwarf_read_debug_printf_v (" offset %s, signature %s",
6243 sect_offset_str (sect_off),
6244 hex_string (header.signature));
3019eac3 6245
78d4d2c5
JK
6246 info_ptr += length;
6247 }
6248}
3019eac3 6249
78d4d2c5
JK
6250/* Create the hash table of all entries in the .debug_types
6251 (or .debug_types.dwo) section(s).
6252 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6253 otherwise it is NULL.
b3c8eb43 6254
78d4d2c5 6255 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6256
78d4d2c5 6257 Note: This function processes DWO files only, not DWP files. */
348e048f 6258
78d4d2c5 6259static void
976ca316 6260create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6261 struct dwo_file *dwo_file,
fd5866f6 6262 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6263 htab_up &types_htab)
78d4d2c5 6264{
fd5866f6 6265 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6266 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6267 rcuh_kind::TYPE);
3019eac3
DE
6268}
6269
6270/* Create the hash table of all entries in the .debug_types section,
6271 and initialize all_type_units.
6272 The result is zero if there is an error (e.g. missing .debug_types section),
6273 otherwise non-zero. */
6274
6275static int
976ca316 6276create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6277{
b0b6a987 6278 htab_up types_htab;
3019eac3 6279
976ca316
SM
6280 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6281 types_htab, rcuh_kind::COMPILE);
6282 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6283 types_htab);
3019eac3
DE
6284 if (types_htab == NULL)
6285 {
976ca316 6286 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6287 return 0;
6288 }
6289
976ca316 6290 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6291
976ca316
SM
6292 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6293 per_objfile->per_bfd->all_type_units.reserve
6294 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6295
976ca316 6296 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6297 add_signatured_type_cu_to_table,
976ca316 6298 &per_objfile->per_bfd->all_type_units);
1fd400ff 6299
348e048f
DE
6300 return 1;
6301}
6302
5989a64e 6303/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6304 If SLOT is non-NULL, it is the entry to use in the hash table.
6305 Otherwise we find one. */
6306
6307static struct signatured_type *
976ca316 6308add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6309{
976ca316
SM
6310 if (per_objfile->per_bfd->all_type_units.size ()
6311 == per_objfile->per_bfd->all_type_units.capacity ())
6312 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6313
976ca316 6314 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6315
976ca316 6316 per_objfile->resize_symtabs ();
af758d11 6317
976ca316 6318 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6319 sig_type->signature = sig;
6320 sig_type->per_cu.is_debug_types = 1;
976ca316 6321 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6322 {
6323 sig_type->per_cu.v.quick =
976ca316 6324 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6325 struct dwarf2_per_cu_quick_data);
6326 }
6327
6328 if (slot == NULL)
6329 {
976ca316 6330 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6331 sig_type, INSERT);
6332 }
6333 gdb_assert (*slot == NULL);
6334 *slot = sig_type;
6335 /* The rest of sig_type must be filled in by the caller. */
6336 return sig_type;
6337}
6338
a2ce51a0
DE
6339/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6340 Fill in SIG_ENTRY with DWO_ENTRY. */
6341
6342static void
976ca316 6343fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6344 struct signatured_type *sig_entry,
6345 struct dwo_unit *dwo_entry)
6346{
976ca316 6347 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6348
7ee85ab1 6349 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6350 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6351 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6352 if (per_bfd->using_index)
6aa5f3a6
DE
6353 {
6354 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6355 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6356 }
6357 else
6358 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6359 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6360 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6361 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6362 gdb_assert (sig_entry->dwo_unit == NULL);
6363
6364 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6365 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6366 sig_entry->per_cu.length = dwo_entry->length;
6367 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6368 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6369 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6370 sig_entry->dwo_unit = dwo_entry;
6371}
6372
6373/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6374 If we haven't read the TU yet, create the signatured_type data structure
6375 for a TU to be read in directly from a DWO file, bypassing the stub.
6376 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6377 using .gdb_index, then when reading a CU we want to stay in the DWO file
6378 containing that CU. Otherwise we could end up reading several other DWO
6379 files (due to comdat folding) to process the transitive closure of all the
6380 mentioned TUs, and that can be slow. The current DWO file will have every
6381 type signature that it needs.
a2ce51a0
DE
6382 We only do this for .gdb_index because in the psymtab case we already have
6383 to read all the DWOs to build the type unit groups. */
6384
6385static struct signatured_type *
6386lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6387{
976ca316 6388 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6389 struct dwo_file *dwo_file;
6390 struct dwo_unit find_dwo_entry, *dwo_entry;
6391 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6392 void **slot;
a2ce51a0 6393
976ca316 6394 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6395
6aa5f3a6
DE
6396 /* If TU skeletons have been removed then we may not have read in any
6397 TUs yet. */
976ca316
SM
6398 if (per_objfile->per_bfd->signatured_types == NULL)
6399 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6400
6401 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6402 Use the global signatured_types array to do our own comdat-folding
6403 of types. If this is the first time we're reading this TU, and
6404 the TU has an entry in .gdb_index, replace the recorded data from
6405 .gdb_index with this TU. */
a2ce51a0 6406
a2ce51a0 6407 find_sig_entry.signature = sig;
976ca316 6408 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6409 &find_sig_entry, INSERT);
9a3c8263 6410 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6411
6412 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6413 read. Don't reassign the global entry to point to this DWO if that's
6414 the case. Also note that if the TU is already being read, it may not
6415 have come from a DWO, the program may be a mix of Fission-compiled
6416 code and non-Fission-compiled code. */
6417
6418 /* Have we already tried to read this TU?
6419 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6420 needn't exist in the global table yet). */
6421 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6422 return sig_entry;
6423
6aa5f3a6
DE
6424 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6425 dwo_unit of the TU itself. */
6426 dwo_file = cu->dwo_unit->dwo_file;
6427
a2ce51a0
DE
6428 /* Ok, this is the first time we're reading this TU. */
6429 if (dwo_file->tus == NULL)
6430 return NULL;
6431 find_dwo_entry.signature = sig;
b0b6a987
TT
6432 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6433 &find_dwo_entry);
a2ce51a0
DE
6434 if (dwo_entry == NULL)
6435 return NULL;
6436
6aa5f3a6
DE
6437 /* If the global table doesn't have an entry for this TU, add one. */
6438 if (sig_entry == NULL)
976ca316 6439 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6440
976ca316 6441 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6442 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6443 return sig_entry;
6444}
6445
a2ce51a0
DE
6446/* Subroutine of lookup_signatured_type.
6447 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6448 then try the DWP file. If the TU stub (skeleton) has been removed then
6449 it won't be in .gdb_index. */
a2ce51a0
DE
6450
6451static struct signatured_type *
6452lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6453{
976ca316
SM
6454 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6455 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6456 struct dwo_unit *dwo_entry;
6457 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6458 void **slot;
a2ce51a0 6459
976ca316 6460 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6461 gdb_assert (dwp_file != NULL);
6462
6aa5f3a6
DE
6463 /* If TU skeletons have been removed then we may not have read in any
6464 TUs yet. */
976ca316
SM
6465 if (per_objfile->per_bfd->signatured_types == NULL)
6466 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6467
6aa5f3a6 6468 find_sig_entry.signature = sig;
976ca316 6469 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6470 &find_sig_entry, INSERT);
9a3c8263 6471 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6472
6473 /* Have we already tried to read this TU?
6474 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6475 needn't exist in the global table yet). */
6476 if (sig_entry != NULL)
6477 return sig_entry;
6478
a2ce51a0
DE
6479 if (dwp_file->tus == NULL)
6480 return NULL;
976ca316
SM
6481 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6482 1 /* is_debug_types */);
a2ce51a0
DE
6483 if (dwo_entry == NULL)
6484 return NULL;
6485
976ca316
SM
6486 sig_entry = add_type_unit (per_objfile, sig, slot);
6487 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6488
a2ce51a0
DE
6489 return sig_entry;
6490}
6491
380bca97 6492/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6493 Returns NULL if signature SIG is not present in the table.
6494 It is up to the caller to complain about this. */
348e048f
DE
6495
6496static struct signatured_type *
a2ce51a0 6497lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6498{
976ca316 6499 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6500
976ca316 6501 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6502 {
6503 /* We're in a DWO/DWP file, and we're using .gdb_index.
6504 These cases require special processing. */
976ca316 6505 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6506 return lookup_dwo_signatured_type (cu, sig);
6507 else
6508 return lookup_dwp_signatured_type (cu, sig);
6509 }
6510 else
6511 {
6512 struct signatured_type find_entry, *entry;
348e048f 6513
976ca316 6514 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6515 return NULL;
6516 find_entry.signature = sig;
9a3c8263 6517 entry = ((struct signatured_type *)
976ca316 6518 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6519 &find_entry));
a2ce51a0
DE
6520 return entry;
6521 }
348e048f 6522}
18a8505e 6523
42e7ad6c 6524/* Low level DIE reading support. */
348e048f 6525
d85a05f0
DJ
6526/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6527
6528static void
6529init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6530 struct dwarf2_cu *cu,
3019eac3 6531 struct dwarf2_section_info *section,
685af9cd
TT
6532 struct dwo_file *dwo_file,
6533 struct abbrev_table *abbrev_table)
d85a05f0 6534{
fceca515 6535 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6536 reader->abfd = section->get_bfd_owner ();
d85a05f0 6537 reader->cu = cu;
3019eac3 6538 reader->dwo_file = dwo_file;
dee91e82
DE
6539 reader->die_section = section;
6540 reader->buffer = section->buffer;
f664829e 6541 reader->buffer_end = section->buffer + section->size;
685af9cd 6542 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6543}
6544
c0ab21c2 6545/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6546 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6547 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6548 already.
6549
6550 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6551 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6552 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6553 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6554 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6555 STUB_COMP_DIR may be non-NULL.
3e225074 6556 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6557 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6558 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6559 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6560 kept around for at least as long as *RESULT_READER.
6561
b0c7bfa9
DE
6562 The result is non-zero if a valid (non-dummy) DIE was found. */
6563
6564static int
4ab09049 6565read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6566 struct dwo_unit *dwo_unit,
b0c7bfa9 6567 struct die_info *stub_comp_unit_die,
a2ce51a0 6568 const char *stub_comp_dir,
b0c7bfa9 6569 struct die_reader_specs *result_reader,
d521ce57 6570 const gdb_byte **result_info_ptr,
b0c7bfa9 6571 struct die_info **result_comp_unit_die,
685af9cd 6572 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6573{
976ca316 6574 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6575 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6576 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6577 bfd *abfd;
d521ce57 6578 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6579 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6580 int i,num_extra_attrs;
6581 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6582 struct die_info *comp_unit_die;
6583
b0aeadb3
DE
6584 /* At most one of these may be provided. */
6585 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6586
b0c7bfa9
DE
6587 /* These attributes aren't processed until later:
6588 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6589 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6590 referenced later. However, these attributes are found in the stub
6591 which we won't have later. In order to not impose this complication
6592 on the rest of the code, we read them here and copy them to the
6593 DWO CU/TU die. */
b0c7bfa9
DE
6594
6595 stmt_list = NULL;
6596 low_pc = NULL;
6597 high_pc = NULL;
6598 ranges = NULL;
6599 comp_dir = NULL;
6600
6601 if (stub_comp_unit_die != NULL)
6602 {
6603 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6604 DWO file. */
4ab09049 6605 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6606 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6607 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6608 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6609 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6610 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6611
a39fdb41 6612 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6613
2b0c7f41
SM
6614 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6615 We need the value before we can process DW_AT_ranges values from the
6616 DWO. */
6617 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6618
6619 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6620 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6621 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6622 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6623 section. */
6624 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6625 }
a2ce51a0
DE
6626 else if (stub_comp_dir != NULL)
6627 {
6628 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6629 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6630 comp_dir->name = DW_AT_comp_dir;
6631 comp_dir->form = DW_FORM_string;
c6481205 6632 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6633 }
b0c7bfa9
DE
6634
6635 /* Set up for reading the DWO CU/TU. */
6636 cu->dwo_unit = dwo_unit;
685af9cd 6637 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6638 section->read (objfile);
6639 abfd = section->get_bfd_owner ();
9c541725
PA
6640 begin_info_ptr = info_ptr = (section->buffer
6641 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6642 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6643
4ab09049 6644 if (per_cu->is_debug_types)
b0c7bfa9 6645 {
4ab09049 6646 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6647
976ca316
SM
6648 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6649 section, dwo_abbrev_section,
43988095 6650 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6651 /* This is not an assert because it can be caused by bad debug info. */
43988095 6652 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6653 {
6654 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6655 " TU at offset %s [in module %s]"),
a2ce51a0 6656 hex_string (sig_type->signature),
43988095 6657 hex_string (cu->header.signature),
9d8780f0 6658 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6659 bfd_get_filename (abfd));
6660 }
9c541725 6661 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6662 /* For DWOs coming from DWP files, we don't know the CU length
6663 nor the type's offset in the TU until now. */
4057dfde 6664 dwo_unit->length = cu->header.get_length ();
9c541725 6665 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6666
6667 /* Establish the type offset that can be used to lookup the type.
6668 For DWO files, we don't know it until now. */
9c541725
PA
6669 sig_type->type_offset_in_section
6670 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6671 }
6672 else
6673 {
976ca316
SM
6674 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6675 section, dwo_abbrev_section,
43988095 6676 info_ptr, rcuh_kind::COMPILE);
9c541725 6677 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6678 /* For DWOs coming from DWP files, we don't know the CU length
6679 until now. */
4057dfde 6680 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6681 }
6682
606decb2 6683 dwo_abbrev_section->read (objfile);
685af9cd 6684 *result_dwo_abbrev_table
606decb2 6685 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6686 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6687 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6688
6689 /* Read in the die, but leave space to copy over the attributes
6690 from the stub. This has the benefit of simplifying the rest of
6691 the code - all the work to maintain the illusion of a single
6692 DW_TAG_{compile,type}_unit DIE is done here. */
6693 num_extra_attrs = ((stmt_list != NULL)
6694 + (low_pc != NULL)
6695 + (high_pc != NULL)
6696 + (ranges != NULL)
6697 + (comp_dir != NULL));
6698 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6699 num_extra_attrs);
b0c7bfa9
DE
6700
6701 /* Copy over the attributes from the stub to the DIE we just read in. */
6702 comp_unit_die = *result_comp_unit_die;
6703 i = comp_unit_die->num_attrs;
6704 if (stmt_list != NULL)
6705 comp_unit_die->attrs[i++] = *stmt_list;
6706 if (low_pc != NULL)
6707 comp_unit_die->attrs[i++] = *low_pc;
6708 if (high_pc != NULL)
6709 comp_unit_die->attrs[i++] = *high_pc;
6710 if (ranges != NULL)
6711 comp_unit_die->attrs[i++] = *ranges;
6712 if (comp_dir != NULL)
6713 comp_unit_die->attrs[i++] = *comp_dir;
6714 comp_unit_die->num_attrs += num_extra_attrs;
6715
b4f54984 6716 if (dwarf_die_debug)
bf6af496
DE
6717 {
6718 fprintf_unfiltered (gdb_stdlog,
6719 "Read die from %s@0x%x of %s:\n",
96b79293 6720 section->get_name (),
bf6af496
DE
6721 (unsigned) (begin_info_ptr - section->buffer),
6722 bfd_get_filename (abfd));
b4f54984 6723 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6724 }
6725
b0c7bfa9
DE
6726 /* Skip dummy compilation units. */
6727 if (info_ptr >= begin_info_ptr + dwo_unit->length
6728 || peek_abbrev_code (abfd, info_ptr) == 0)
6729 return 0;
6730
6731 *result_info_ptr = info_ptr;
6732 return 1;
6733}
6734
a084a2a6
AT
6735/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6736 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6737 signature is part of the header. */
6738static gdb::optional<ULONGEST>
6739lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6740{
6741 if (cu->header.version >= 5)
6742 return cu->header.signature;
6743 struct attribute *attr;
6744 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 6745 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 6746 return gdb::optional<ULONGEST> ();
529908cb 6747 return attr->as_unsigned ();
a084a2a6
AT
6748}
6749
c0ab21c2 6750/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6751 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6752 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6753
6754static struct dwo_unit *
4ab09049 6755lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6756{
4ab09049 6757 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6758 struct dwo_unit *dwo_unit;
c0ab21c2 6759 const char *comp_dir;
b0c7bfa9 6760
a2ce51a0
DE
6761 gdb_assert (cu != NULL);
6762
b0c7bfa9 6763 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6764 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6765 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6766
4ab09049
SM
6767 if (per_cu->is_debug_types)
6768 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6769 else
6770 {
a084a2a6 6771 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6772
a084a2a6 6773 if (!signature.has_value ())
b0c7bfa9
DE
6774 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6775 " [in module %s]"),
4ab09049
SM
6776 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6777
6778 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6779 }
6780
b0c7bfa9
DE
6781 return dwo_unit;
6782}
6783
c0ab21c2 6784/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6785 See it for a description of the parameters.
fcd3b13d 6786 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6787
c0ab21c2 6788void
9e021579
SM
6789cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6790 dwarf2_per_objfile *per_objfile,
2e671100 6791 dwarf2_cu *existing_cu)
a2ce51a0 6792{
a2ce51a0 6793 struct signatured_type *sig_type;
a2ce51a0
DE
6794
6795 /* Verify we can do the following downcast, and that we have the
6796 data we need. */
6797 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6798 sig_type = (struct signatured_type *) this_cu;
6799 gdb_assert (sig_type->dwo_unit != NULL);
6800
2e671100
SM
6801 dwarf2_cu *cu;
6802
6803 if (existing_cu != nullptr)
6aa5f3a6 6804 {
2e671100
SM
6805 cu = existing_cu;
6806 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6807 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6808 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6809 }
6810 else
6811 {
7188ed02 6812 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6813 in per_objfile yet. */
7188ed02 6814 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6815 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6816 cu = m_new_cu.get ();
6aa5f3a6
DE
6817 }
6818
6819 /* A future optimization, if needed, would be to use an existing
6820 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6821 could share abbrev tables. */
a2ce51a0 6822
2e671100 6823 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6824 NULL /* stub_comp_unit_die */,
6825 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6826 this, &info_ptr,
3e225074 6827 &comp_unit_die,
c0ab21c2 6828 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6829 {
6830 /* Dummy die. */
c0ab21c2 6831 dummy_p = true;
a2ce51a0 6832 }
a2ce51a0
DE
6833}
6834
fd820528 6835/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6836 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6837
f4dc4d17
DE
6838 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6839 Otherwise the table specified in the comp unit header is read in and used.
6840 This is an optimization for when we already have the abbrev table.
6841
2e671100
SM
6842 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6843 allocated. */
aaa75496 6844
ab432490 6845cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6846 dwarf2_per_objfile *per_objfile,
c0ab21c2 6847 struct abbrev_table *abbrev_table,
2e671100 6848 dwarf2_cu *existing_cu,
c0ab21c2
TT
6849 bool skip_partial)
6850 : die_reader_specs {},
6751ebae 6851 m_this_cu (this_cu)
c906108c 6852{
976ca316 6853 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6854 struct dwarf2_section_info *section = this_cu->section;
96b79293 6855 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 6856 const gdb_byte *begin_info_ptr;
dee91e82 6857 struct signatured_type *sig_type = NULL;
4bdcc0c1 6858 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6859 /* Non-zero if CU currently points to a DWO file and we need to
6860 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6861 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6862 int rereading_dwo_cu = 0;
c906108c 6863
b4f54984 6864 if (dwarf_die_debug)
9d8780f0 6865 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6866 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6867 sect_offset_str (this_cu->sect_off));
09406207 6868
a2ce51a0
DE
6869 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6870 file (instead of going through the stub), short-circuit all of this. */
6871 if (this_cu->reading_dwo_directly)
6872 {
6873 /* Narrow down the scope of possibilities to have to understand. */
6874 gdb_assert (this_cu->is_debug_types);
6875 gdb_assert (abbrev_table == NULL);
976ca316 6876 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
6877 return;
6878 }
6879
dee91e82 6880 /* This is cheap if the section is already read in. */
96b79293 6881 section->read (objfile);
dee91e82 6882
9c541725 6883 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6884
6885 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 6886
2e671100
SM
6887 dwarf2_cu *cu;
6888
6889 if (existing_cu != nullptr)
dee91e82 6890 {
2e671100 6891 cu = existing_cu;
42e7ad6c
DE
6892 /* If this CU is from a DWO file we need to start over, we need to
6893 refetch the attributes from the skeleton CU.
6894 This could be optimized by retrieving those attributes from when we
6895 were here the first time: the previous comp_unit_die was stored in
6896 comp_unit_obstack. But there's no data yet that we need this
6897 optimization. */
6898 if (cu->dwo_unit != NULL)
6899 rereading_dwo_cu = 1;
dee91e82
DE
6900 }
6901 else
6902 {
7188ed02 6903 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6904 in per_objfile yet. */
976ca316
SM
6905 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6906 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 6907 cu = m_new_cu.get ();
42e7ad6c 6908 }
dee91e82 6909
b0c7bfa9 6910 /* Get the header. */
9c541725 6911 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6912 {
6913 /* We already have the header, there's no need to read it in again. */
9c541725 6914 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6915 }
6916 else
6917 {
3019eac3 6918 if (this_cu->is_debug_types)
dee91e82 6919 {
976ca316
SM
6920 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6921 section, abbrev_section,
6922 info_ptr, rcuh_kind::TYPE);
dee91e82 6923
42e7ad6c
DE
6924 /* Since per_cu is the first member of struct signatured_type,
6925 we can go from a pointer to one to a pointer to the other. */
6926 sig_type = (struct signatured_type *) this_cu;
43988095 6927 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6928 gdb_assert (sig_type->type_offset_in_tu
6929 == cu->header.type_cu_offset_in_tu);
6930 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6931
42e7ad6c
DE
6932 /* LENGTH has not been set yet for type units if we're
6933 using .gdb_index. */
4057dfde 6934 this_cu->length = cu->header.get_length ();
3019eac3
DE
6935
6936 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6937 sig_type->type_offset_in_section =
6938 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6939
6940 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6941 }
6942 else
6943 {
976ca316
SM
6944 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6945 section, abbrev_section,
43988095
JK
6946 info_ptr,
6947 rcuh_kind::COMPILE);
dee91e82 6948
9c541725 6949 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
6950 if (this_cu->length == 0)
6951 this_cu->length = cu->header.get_length ();
6952 else
6953 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6954 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6955 }
6956 }
10b3939b 6957
6caca83c 6958 /* Skip dummy compilation units. */
dee91e82 6959 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6960 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6961 {
6962 dummy_p = true;
6963 return;
6964 }
6caca83c 6965
433df2d4
DE
6966 /* If we don't have them yet, read the abbrevs for this compilation unit.
6967 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6968 done. */
f4dc4d17 6969 if (abbrev_table != NULL)
685af9cd
TT
6970 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6971 else
f4dc4d17 6972 {
606decb2 6973 abbrev_section->read (objfile);
c0ab21c2 6974 m_abbrev_table_holder
606decb2 6975 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 6976 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6977 }
af703f96 6978
dee91e82 6979 /* Read the top level CU/TU die. */
c0ab21c2 6980 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6981 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6982
58f0c718 6983 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6984 {
6985 dummy_p = true;
6986 return;
6987 }
58f0c718 6988
b0c7bfa9 6989 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6990 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6991 table from the DWO file and pass the ownership over to us. It will be
6992 referenced from READER, so we must make sure to free it after we're done
6993 with READER.
6994
b0c7bfa9
DE
6995 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6996 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6997 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6998 if (dwo_name != nullptr)
3019eac3 6999 {
3019eac3 7000 struct dwo_unit *dwo_unit;
b0c7bfa9 7001 struct die_info *dwo_comp_unit_die;
3019eac3 7002
3e225074 7003 if (comp_unit_die->has_children)
6a506a2d 7004 {
b98664d3 7005 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7006 " has children (offset %s) [in module %s]"),
7007 sect_offset_str (this_cu->sect_off),
7008 bfd_get_filename (abfd));
6a506a2d 7009 }
4ab09049 7010 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7011 if (dwo_unit != NULL)
3019eac3 7012 {
4ab09049 7013 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7014 comp_unit_die, NULL,
c0ab21c2 7015 this, &info_ptr,
3e225074 7016 &dwo_comp_unit_die,
c0ab21c2 7017 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7018 {
7019 /* Dummy die. */
c0ab21c2 7020 dummy_p = true;
6a506a2d
DE
7021 return;
7022 }
7023 comp_unit_die = dwo_comp_unit_die;
7024 }
7025 else
7026 {
7027 /* Yikes, we couldn't find the rest of the DIE, we only have
7028 the stub. A complaint has already been logged. There's
7029 not much more we can do except pass on the stub DIE to
7030 die_reader_func. We don't want to throw an error on bad
7031 debug info. */
3019eac3
DE
7032 }
7033 }
c0ab21c2 7034}
3019eac3 7035
6751ebae
TT
7036void
7037cutu_reader::keep ()
c0ab21c2 7038{
b0c7bfa9 7039 /* Done, clean up. */
6751ebae
TT
7040 gdb_assert (!dummy_p);
7041 if (m_new_cu != NULL)
348e048f 7042 {
7188ed02 7043 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 7044 now. */
7188ed02
SM
7045 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7046 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7047 }
dee91e82
DE
7048}
7049
18a8505e
AT
7050/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7051 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7052 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7053
7054 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7055 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7056
7057 We fill in THIS_CU->length.
7058
dee91e82 7059 THIS_CU->cu is always freed when done.
3019eac3 7060 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7061 to care whether it refers to the "main" CU or the DWO CU.
7062
7063 When parent_cu is passed, it is used to provide a default value for
7064 str_offsets_base and addr_base from the parent. */
dee91e82 7065
ab432490 7066cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7067 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7068 struct dwarf2_cu *parent_cu,
7069 struct dwo_file *dwo_file)
7070 : die_reader_specs {},
7071 m_this_cu (this_cu)
dee91e82 7072{
976ca316 7073 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7074 struct dwarf2_section_info *section = this_cu->section;
96b79293 7075 bfd *abfd = section->get_bfd_owner ();
33e80786 7076 struct dwarf2_section_info *abbrev_section;
d521ce57 7077 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7078
b4f54984 7079 if (dwarf_die_debug)
9d8780f0 7080 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7081 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7082 sect_offset_str (this_cu->sect_off));
09406207 7083
976ca316 7084 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7085
33e80786
DE
7086 abbrev_section = (dwo_file != NULL
7087 ? &dwo_file->sections.abbrev
7088 : get_abbrev_section_for_cu (this_cu));
7089
dee91e82 7090 /* This is cheap if the section is already read in. */
96b79293 7091 section->read (objfile);
dee91e82 7092
976ca316 7093 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7094
9c541725 7095 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7096 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7097 section, abbrev_section, info_ptr,
43988095
JK
7098 (this_cu->is_debug_types
7099 ? rcuh_kind::TYPE
7100 : rcuh_kind::COMPILE));
dee91e82 7101
18a8505e
AT
7102 if (parent_cu != nullptr)
7103 {
c0ab21c2
TT
7104 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7105 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7106 }
4057dfde 7107 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7108
7109 /* Skip dummy compilation units. */
7110 if (info_ptr >= begin_info_ptr + this_cu->length
7111 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7112 {
7113 dummy_p = true;
7114 return;
7115 }
72bf9492 7116
606decb2 7117 abbrev_section->read (objfile);
c0ab21c2 7118 m_abbrev_table_holder
606decb2 7119 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7120
c0ab21c2
TT
7121 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7122 m_abbrev_table_holder.get ());
3e225074 7123 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7124}
7125
0018ea6f
DE
7126\f
7127/* Type Unit Groups.
dee91e82 7128
0018ea6f
DE
7129 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7130 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7131 so that all types coming from the same compilation (.o file) are grouped
7132 together. A future step could be to put the types in the same symtab as
7133 the CU the types ultimately came from. */
ff013f42 7134
f4dc4d17
DE
7135static hashval_t
7136hash_type_unit_group (const void *item)
7137{
9a3c8263
SM
7138 const struct type_unit_group *tu_group
7139 = (const struct type_unit_group *) item;
f4dc4d17 7140
094b34ac 7141 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7142}
348e048f
DE
7143
7144static int
f4dc4d17 7145eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7146{
9a3c8263
SM
7147 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7148 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7149
094b34ac 7150 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7151}
348e048f 7152
f4dc4d17
DE
7153/* Allocate a hash table for type unit groups. */
7154
eaa5fa8b 7155static htab_up
298e9637 7156allocate_type_unit_groups_table ()
f4dc4d17 7157{
eaa5fa8b
TT
7158 return htab_up (htab_create_alloc (3,
7159 hash_type_unit_group,
7160 eq_type_unit_group,
7161 NULL, xcalloc, xfree));
f4dc4d17 7162}
dee91e82 7163
f4dc4d17
DE
7164/* Type units that don't have DW_AT_stmt_list are grouped into their own
7165 partial symtabs. We combine several TUs per psymtab to not let the size
7166 of any one psymtab grow too big. */
7167#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7168#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7169
094b34ac 7170/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7171 Create the type_unit_group object used to hold one or more TUs. */
7172
7173static struct type_unit_group *
094b34ac 7174create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7175{
976ca316
SM
7176 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7177 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7178 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7179 struct type_unit_group *tu_group;
f4dc4d17 7180
bab287cd 7181 tu_group = OBSTACK_ZALLOC (&per_bfd->obstack, type_unit_group);
094b34ac 7182 per_cu = &tu_group->per_cu;
1859c670 7183 per_cu->per_bfd = per_bfd;
f4dc4d17 7184
1859c670 7185 if (per_bfd->using_index)
094b34ac 7186 {
1859c670 7187 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7188 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7189 }
7190 else
7191 {
9c541725 7192 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7193 dwarf2_psymtab *pst;
528e1572 7194 std::string name;
094b34ac
DE
7195
7196 /* Give the symtab a useful name for debug purposes. */
7197 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7198 name = string_printf ("<type_units_%d>",
7199 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7200 else
528e1572 7201 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7202
976ca316 7203 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7204 pst->anonymous = true;
094b34ac 7205 }
f4dc4d17 7206
094b34ac 7207 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7208 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7209
7210 return tu_group;
7211}
7212
094b34ac
DE
7213/* Look up the type_unit_group for type unit CU, and create it if necessary.
7214 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7215
7216static struct type_unit_group *
ff39bb5e 7217get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7218{
976ca316
SM
7219 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7220 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7221 struct type_unit_group *tu_group;
7222 void **slot;
7223 unsigned int line_offset;
7224 struct type_unit_group type_unit_group_for_lookup;
7225
976ca316
SM
7226 if (per_objfile->per_bfd->type_unit_groups == NULL)
7227 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7228
7229 /* Do we need to create a new group, or can we use an existing one? */
7230
529908cb 7231 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7232 {
529908cb 7233 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7234 ++tu_stats->nr_symtab_sharers;
7235 }
7236 else
7237 {
7238 /* Ugh, no stmt_list. Rare, but we have to handle it.
7239 We can do various things here like create one group per TU or
7240 spread them over multiple groups to split up the expansion work.
7241 To avoid worst case scenarios (too many groups or too large groups)
7242 we, umm, group them in bunches. */
7243 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7244 | (tu_stats->nr_stmt_less_type_units
7245 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7246 ++tu_stats->nr_stmt_less_type_units;
7247 }
7248
094b34ac 7249 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7250 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7251 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7252 &type_unit_group_for_lookup, INSERT);
7253 if (*slot != NULL)
7254 {
9a3c8263 7255 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7256 gdb_assert (tu_group != NULL);
7257 }
7258 else
7259 {
9c541725 7260 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7261 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7262 *slot = tu_group;
7263 ++tu_stats->nr_symtabs;
7264 }
7265
7266 return tu_group;
7267}
0018ea6f
DE
7268\f
7269/* Partial symbol tables. */
7270
7271/* Create a psymtab named NAME and assign it to PER_CU.
7272
7273 The caller must fill in the following details:
7274 dirname, textlow, texthigh. */
7275
891813be 7276static dwarf2_psymtab *
7aa104c4
SM
7277create_partial_symtab (dwarf2_per_cu_data *per_cu,
7278 dwarf2_per_objfile *per_objfile,
7279 const char *name)
0018ea6f 7280{
0072c873
SM
7281 dwarf2_psymtab *pst
7282 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7283 per_objfile->objfile->per_bfd, per_cu);
0018ea6f 7284
6d94535f 7285 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7286
7287 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7288 per_cu->v.psymtab = pst;
7289
7290 return pst;
7291}
7292
c0ab21c2 7293/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7294
7295static void
7296process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7297 const gdb_byte *info_ptr,
0018ea6f 7298 struct die_info *comp_unit_die,
c0ab21c2 7299 enum language pretend_language)
0018ea6f
DE
7300{
7301 struct dwarf2_cu *cu = reader->cu;
7aa104c4 7302 dwarf2_per_objfile *per_objfile = cu->per_objfile;
84685904 7303 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7aa104c4 7304 struct objfile *objfile = per_objfile->objfile;
08feed99 7305 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7306 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7307 CORE_ADDR baseaddr;
7308 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7309 dwarf2_psymtab *pst;
3a2b436a 7310 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7311 const char *filename;
0018ea6f 7312
0018ea6f
DE
7313 gdb_assert (! per_cu->is_debug_types);
7314
c0ab21c2 7315 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7316
0018ea6f 7317 /* Allocate a new partial symbol table structure. */
2e927613
TV
7318 gdb::unique_xmalloc_ptr<char> debug_filename;
7319 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7320 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7321 if (filename == NULL)
0018ea6f 7322 filename = "";
2e927613
TV
7323 else if (strcmp (filename, artificial) == 0)
7324 {
7325 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7326 sect_offset_str (per_cu->sect_off),
7327 (char *) NULL));
2e927613
TV
7328 filename = debug_filename.get ();
7329 }
0018ea6f 7330
7aa104c4 7331 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7332
7333 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7334 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7335
b3b3bada 7336 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7337
7338 dwarf2_find_base_address (comp_unit_die, cu);
7339
7340 /* Possibly set the default values of LOWPC and HIGHPC from
7341 `DW_AT_ranges'. */
3a2b436a
JK
7342 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7343 &best_highpc, cu, pst);
7344 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7345 {
7346 CORE_ADDR low
7347 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7348 - baseaddr);
7349 CORE_ADDR high
7350 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7351 - baseaddr - 1);
7352 /* Store the contiguous range if it is not empty; it can be
7353 empty for CUs with no code. */
84685904 7354 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 7355 low, high, pst);
79748972 7356 }
0018ea6f
DE
7357
7358 /* Check if comp unit has_children.
7359 If so, read the rest of the partial symbols from this comp unit.
7360 If not, there's no more debug_info for this comp unit. */
3e225074 7361 if (comp_unit_die->has_children)
0018ea6f
DE
7362 {
7363 struct partial_die_info *first_die;
7364 CORE_ADDR lowpc, highpc;
7365
7366 lowpc = ((CORE_ADDR) -1);
7367 highpc = ((CORE_ADDR) 0);
7368
7369 first_die = load_partial_dies (reader, info_ptr, 1);
7370
7371 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7372 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7373
7374 /* If we didn't find a lowpc, set it to highpc to avoid
7375 complaints from `maint check'. */
7376 if (lowpc == ((CORE_ADDR) -1))
7377 lowpc = highpc;
7378
7379 /* If the compilation unit didn't have an explicit address range,
7380 then use the information extracted from its child dies. */
e385593e 7381 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7382 {
7383 best_lowpc = lowpc;
7384 best_highpc = highpc;
7385 }
7386 }
4ae976d1 7387 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7388 best_lowpc + baseaddr)
7389 - baseaddr);
4ae976d1 7390 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7391 best_highpc + baseaddr)
7392 - baseaddr);
0018ea6f 7393
ae7754b2 7394 pst->end ();
0018ea6f 7395
ae640021 7396 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7397 {
7398 int i;
ae640021 7399 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7400
7401 /* Fill in 'dependencies' here; we fill in 'users' in a
7402 post-pass. */
7403 pst->number_of_dependencies = len;
a9342b62 7404 pst->dependencies
84685904 7405 = per_bfd->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7406 for (i = 0; i < len; ++i)
7407 {
7408 pst->dependencies[i]
7409 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7410 }
0018ea6f 7411
ae640021 7412 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7413 }
7414
7415 /* Get the list of files included in the current compilation unit,
7416 and build a psymtab for each of them. */
7417 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7418
6f738b01
SM
7419 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7420 ", %d global, %d static syms",
7421 per_cu->is_debug_types ? "type" : "comp",
7422 sect_offset_str (per_cu->sect_off),
7423 paddress (gdbarch, pst->text_low (objfile)),
7424 paddress (gdbarch, pst->text_high (objfile)),
7425 (int) pst->global_psymbols.size (),
7426 (int) pst->static_psymbols.size ());
0018ea6f
DE
7427}
7428
7429/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7430 Process compilation unit THIS_CU for a psymtab. */
7431
7432static void
ab432490
SM
7433process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7434 dwarf2_per_objfile *per_objfile,
135f5437 7435 bool want_partial_unit,
b93601f3 7436 enum language pretend_language)
0018ea6f
DE
7437{
7438 /* If this compilation unit was already read in, free the
7439 cached copy in order to read it in again. This is
7440 necessary because we skipped some symbols when we first
7441 read in the compilation unit (see load_partial_dies).
7442 This problem could be avoided, but the benefit is unclear. */
7188ed02 7443 per_objfile->remove_cu (this_cu);
0018ea6f 7444
2e671100 7445 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7446
58990295
TV
7447 switch (reader.comp_unit_die->tag)
7448 {
7449 case DW_TAG_compile_unit:
7450 this_cu->unit_type = DW_UT_compile;
7451 break;
7452 case DW_TAG_partial_unit:
7453 this_cu->unit_type = DW_UT_partial;
7454 break;
e77b0004
TV
7455 case DW_TAG_type_unit:
7456 this_cu->unit_type = DW_UT_type;
7457 break;
58990295 7458 default:
702cf3f5
AB
7459 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7460 dwarf_tag_name (reader.comp_unit_die->tag),
7461 sect_offset_str (reader.cu->per_cu->sect_off),
7462 objfile_name (per_objfile->objfile));
58990295
TV
7463 }
7464
c0ab21c2 7465 if (reader.dummy_p)
f1902523 7466 {
c0ab21c2 7467 /* Nothing. */
f1902523 7468 }
c0ab21c2 7469 else if (this_cu->is_debug_types)
3e225074
TT
7470 build_type_psymtabs_reader (&reader, reader.info_ptr,
7471 reader.comp_unit_die);
135f5437
TT
7472 else if (want_partial_unit
7473 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7474 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7475 reader.comp_unit_die,
c0ab21c2 7476 pretend_language);
0018ea6f 7477
7188ed02 7478 this_cu->lang = reader.cu->language;
58990295 7479
0018ea6f 7480 /* Age out any secondary CUs. */
7188ed02 7481 per_objfile->age_comp_units ();
0018ea6f 7482}
f4dc4d17
DE
7483
7484/* Reader function for build_type_psymtabs. */
7485
7486static void
7487build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7488 const gdb_byte *info_ptr,
3e225074 7489 struct die_info *type_unit_die)
f4dc4d17 7490{
976ca316 7491 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7492 struct dwarf2_cu *cu = reader->cu;
7493 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7494 struct signatured_type *sig_type;
f4dc4d17
DE
7495 struct type_unit_group *tu_group;
7496 struct attribute *attr;
7497 struct partial_die_info *first_die;
7498 CORE_ADDR lowpc, highpc;
891813be 7499 dwarf2_psymtab *pst;
f4dc4d17 7500
0186c6a7
DE
7501 gdb_assert (per_cu->is_debug_types);
7502 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7503
3e225074 7504 if (! type_unit_die->has_children)
f4dc4d17
DE
7505 return;
7506
052c8bb8 7507 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7508 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7509
df07e2c7 7510 if (tu_group->tus == nullptr)
a8b3b8e9 7511 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7512 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7513
7514 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7515 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7516 pst->anonymous = true;
f4dc4d17
DE
7517
7518 first_die = load_partial_dies (reader, info_ptr, 1);
7519
7520 lowpc = (CORE_ADDR) -1;
7521 highpc = (CORE_ADDR) 0;
7522 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7523
ae7754b2 7524 pst->end ();
f4dc4d17
DE
7525}
7526
73051182
DE
7527/* Struct used to sort TUs by their abbreviation table offset. */
7528
7529struct tu_abbrev_offset
7530{
b2bdb8cf
SM
7531 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7532 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7533 {}
7534
7535 signatured_type *sig_type;
73051182
DE
7536 sect_offset abbrev_offset;
7537};
7538
484cf504 7539/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7540
484cf504
TT
7541static bool
7542sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7543 const struct tu_abbrev_offset &b)
73051182 7544{
484cf504 7545 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7546}
7547
7548/* Efficiently read all the type units.
7549 This does the bulk of the work for build_type_psymtabs.
7550
7551 The efficiency is because we sort TUs by the abbrev table they use and
7552 only read each abbrev table once. In one program there are 200K TUs
7553 sharing 8K abbrev tables.
7554
7555 The main purpose of this function is to support building the
5989a64e 7556 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7557 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7558 can collapse the search space by grouping them by stmt_list.
7559 The savings can be significant, in the same program from above the 200K TUs
7560 share 8K stmt_list tables.
7561
7562 FUNC is expected to call get_type_unit_group, which will create the
7563 struct type_unit_group if necessary and add it to
5989a64e 7564 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7565
7566static void
976ca316 7567build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7568{
976ca316 7569 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7570 abbrev_table_up abbrev_table;
73051182 7571 sect_offset abbrev_offset;
73051182
DE
7572
7573 /* It's up to the caller to not call us multiple times. */
976ca316 7574 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7575
976ca316 7576 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7577 return;
7578
7579 /* TUs typically share abbrev tables, and there can be way more TUs than
7580 abbrev tables. Sort by abbrev table to reduce the number of times we
7581 read each abbrev table in.
7582 Alternatives are to punt or to maintain a cache of abbrev tables.
7583 This is simpler and efficient enough for now.
7584
7585 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7586 symtab to use). Typically TUs with the same abbrev offset have the same
7587 stmt_list value too so in practice this should work well.
7588
7589 The basic algorithm here is:
7590
7591 sort TUs by abbrev table
7592 for each TU with same abbrev table:
7593 read abbrev table if first user
7594 read TU top level DIE
7595 [IWBN if DWO skeletons had DW_AT_stmt_list]
7596 call FUNC */
7597
6f738b01 7598 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7599
7600 /* Sort in a separate table to maintain the order of all_type_units
7601 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7602 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7603 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7604
976ca316 7605 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7606 sorted_by_abbrev.emplace_back
976ca316 7607 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7608 sig_type->per_cu.sect_off));
73051182 7609
484cf504
TT
7610 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7611 sort_tu_by_abbrev_offset);
73051182 7612
9c541725 7613 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7614
b2bdb8cf 7615 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7616 {
73051182
DE
7617 /* Switch to the next abbrev table if necessary. */
7618 if (abbrev_table == NULL
b2bdb8cf 7619 || tu.abbrev_offset != abbrev_offset)
73051182 7620 {
b2bdb8cf 7621 abbrev_offset = tu.abbrev_offset;
606decb2 7622 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7623 abbrev_table =
606decb2 7624 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7625 ++tu_stats->nr_uniq_abbrev_tables;
7626 }
7627
976ca316 7628 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7629 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7630 if (!reader.dummy_p)
7631 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7632 reader.comp_unit_die);
73051182 7633 }
6aa5f3a6 7634}
73051182 7635
6aa5f3a6
DE
7636/* Print collected type unit statistics. */
7637
7638static void
976ca316 7639print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7640{
976ca316 7641 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7642
6f738b01
SM
7643 dwarf_read_debug_printf ("Type unit statistics:");
7644 dwarf_read_debug_printf (" %zu TUs",
7645 per_objfile->per_bfd->all_type_units.size ());
7646 dwarf_read_debug_printf (" %d uniq abbrev tables",
7647 tu_stats->nr_uniq_abbrev_tables);
7648 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7649 tu_stats->nr_symtabs);
7650 dwarf_read_debug_printf (" %d symtab sharers",
7651 tu_stats->nr_symtab_sharers);
7652 dwarf_read_debug_printf (" %d type units without a stmt_list",
7653 tu_stats->nr_stmt_less_type_units);
7654 dwarf_read_debug_printf (" %d all_type_units reallocs",
7655 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7656}
7657
f4dc4d17
DE
7658/* Traversal function for build_type_psymtabs. */
7659
7660static int
7661build_type_psymtab_dependencies (void **slot, void *info)
7662{
976ca316 7663 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
84685904 7664 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 7665 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7666 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7667 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7668 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7669 int i;
7670
7671 gdb_assert (len > 0);
197400e8 7672 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7673
7674 pst->number_of_dependencies = len;
84685904 7675 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
df07e2c7 7676 for (i = 0; i < len; ++i)
f4dc4d17 7677 {
df07e2c7 7678 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7679 gdb_assert (iter->per_cu.is_debug_types);
7680 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7681 iter->type_unit_group = tu_group;
f4dc4d17
DE
7682 }
7683
df07e2c7
AB
7684 delete tu_group->tus;
7685 tu_group->tus = nullptr;
348e048f
DE
7686
7687 return 1;
7688}
7689
7690/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7691 Build partial symbol tables for the .debug_types comp-units. */
7692
7693static void
976ca316 7694build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7695{
976ca316 7696 if (! create_all_type_units (per_objfile))
348e048f
DE
7697 return;
7698
976ca316 7699 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7700}
f4dc4d17 7701
6aa5f3a6
DE
7702/* Traversal function for process_skeletonless_type_unit.
7703 Read a TU in a DWO file and build partial symbols for it. */
7704
7705static int
7706process_skeletonless_type_unit (void **slot, void *info)
7707{
7708 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7709 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7710 struct signatured_type find_entry, *entry;
7711
7712 /* If this TU doesn't exist in the global table, add it and read it in. */
7713
976ca316
SM
7714 if (per_objfile->per_bfd->signatured_types == NULL)
7715 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7716
7717 find_entry.signature = dwo_unit->signature;
976ca316 7718 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7719 &find_entry, INSERT);
6aa5f3a6
DE
7720 /* If we've already seen this type there's nothing to do. What's happening
7721 is we're doing our own version of comdat-folding here. */
7722 if (*slot != NULL)
7723 return 1;
7724
7725 /* This does the job that create_all_type_units would have done for
7726 this TU. */
976ca316
SM
7727 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7728 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7729 *slot = entry;
7730
7731 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7732 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7733 if (!reader.dummy_p)
7734 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7735 reader.comp_unit_die);
6aa5f3a6
DE
7736
7737 return 1;
7738}
7739
7740/* Traversal function for process_skeletonless_type_units. */
7741
7742static int
7743process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7744{
7745 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7746
7747 if (dwo_file->tus != NULL)
b0b6a987
TT
7748 htab_traverse_noresize (dwo_file->tus.get (),
7749 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7750
7751 return 1;
7752}
7753
7754/* Scan all TUs of DWO files, verifying we've processed them.
7755 This is needed in case a TU was emitted without its skeleton.
7756 Note: This can't be done until we know what all the DWO files are. */
7757
7758static void
976ca316 7759process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7760{
7761 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7762 if (get_dwp_file (per_objfile) == NULL
7763 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7764 {
976ca316 7765 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7766 process_dwo_file_for_skeletonless_type_units,
976ca316 7767 per_objfile);
6aa5f3a6 7768 }
348e048f
DE
7769}
7770
ed2dc618 7771/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7772
7773static void
976ca316 7774set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7775{
976ca316 7776 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7777 {
891813be 7778 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7779
36586728
TT
7780 if (pst == NULL)
7781 continue;
7782
b76e467d 7783 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7784 {
7785 /* Set the 'user' field only if it is not already set. */
7786 if (pst->dependencies[j]->user == NULL)
7787 pst->dependencies[j]->user = pst;
7788 }
7789 }
7790}
7791
93311388
DE
7792/* Build the partial symbol table by doing a quick pass through the
7793 .debug_info and .debug_abbrev sections. */
72bf9492 7794
93311388 7795static void
976ca316 7796dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7797{
976ca316 7798 struct objfile *objfile = per_objfile->objfile;
84685904 7799 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
93311388 7800
6f738b01
SM
7801 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7802 objfile_name (objfile));
45cfd468 7803
76935768 7804 scoped_restore restore_reading_psyms
bab287cd 7805 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
98bfdba5 7806
84685904 7807 per_bfd->info.read (objfile);
91c24f0a 7808
93311388
DE
7809 /* Any cached compilation units will be linked by the per-objfile
7810 read_in_chain. Make sure to free them when we're done. */
976ca316 7811 free_cached_comp_units freer (per_objfile);
72bf9492 7812
976ca316 7813 build_type_psymtabs (per_objfile);
348e048f 7814
976ca316 7815 create_all_comp_units (per_objfile);
c906108c 7816
60606b2c
TT
7817 /* Create a temporary address map on a temporary obstack. We later
7818 copy this to the final obstack. */
8268c778 7819 auto_obstack temp_obstack;
791afaa2
TT
7820
7821 scoped_restore save_psymtabs_addrmap
84685904 7822 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
791afaa2 7823 addrmap_create_mutable (&temp_obstack));
72bf9492 7824
84685904 7825 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
3d5afab3
TV
7826 {
7827 if (per_cu->v.psymtab != NULL)
7828 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7829 continue;
976ca316 7830 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 7831 language_minimal);
3d5afab3 7832 }
ff013f42 7833
6aa5f3a6 7834 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 7835 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
7836
7837 /* Now that all TUs have been processed we can fill in the dependencies. */
84685904 7838 if (per_bfd->type_unit_groups != NULL)
6aa5f3a6 7839 {
84685904 7840 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
976ca316 7841 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
7842 }
7843
6f738b01 7844 if (dwarf_read_debug > 0)
976ca316 7845 print_tu_stats (per_objfile);
6aa5f3a6 7846
976ca316 7847 set_partial_user (per_objfile);
95554aad 7848
84685904
TT
7849 per_bfd->partial_symtabs->psymtabs_addrmap
7850 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7851 per_bfd->partial_symtabs->obstack ());
791afaa2
TT
7852 /* At this point we want to keep the address map. */
7853 save_psymtabs_addrmap.release ();
ff013f42 7854
6f738b01
SM
7855 dwarf_read_debug_printf ("Done building psymtabs of %s",
7856 objfile_name (objfile));
ae038cb0
DJ
7857}
7858
dee91e82
DE
7859/* Load the partial DIEs for a secondary CU into memory.
7860 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7861
dee91e82 7862static void
ab432490 7863load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
7864 dwarf2_per_objfile *per_objfile,
7865 dwarf2_cu *existing_cu)
dee91e82 7866{
2e671100 7867 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
7868
7869 if (!reader.dummy_p)
7870 {
7871 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7872 language_minimal);
7873
7874 /* Check if comp unit has_children.
7875 If so, read the rest of the partial symbols from this comp unit.
7876 If not, there's no more debug_info for this comp unit. */
3e225074 7877 if (reader.comp_unit_die->has_children)
c0ab21c2 7878 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7879
7880 reader.keep ();
c0ab21c2 7881 }
ae038cb0
DJ
7882}
7883
ae038cb0 7884static void
976ca316 7885read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 7886 struct dwarf2_section_info *section,
f1902523 7887 struct dwarf2_section_info *abbrev_section,
b76e467d 7888 unsigned int is_dwz)
ae038cb0 7889{
d521ce57 7890 const gdb_byte *info_ptr;
976ca316 7891 struct objfile *objfile = per_objfile->objfile;
be391dca 7892
6f738b01
SM
7893 dwarf_read_debug_printf ("Reading %s for %s",
7894 section->get_name (),
7895 section->get_file_name ());
bf6af496 7896
96b79293 7897 section->read (objfile);
ae038cb0 7898
36586728 7899 info_ptr = section->buffer;
6e70227d 7900
36586728 7901 while (info_ptr < section->buffer + section->size)
ae038cb0 7902 {
ae038cb0 7903 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7904
9c541725 7905 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7906
f1902523 7907 comp_unit_head cu_header;
976ca316 7908 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
7909 abbrev_section, info_ptr,
7910 rcuh_kind::COMPILE);
ae038cb0
DJ
7911
7912 /* Save the compilation unit for later lookup. */
f1902523 7913 if (cu_header.unit_type != DW_UT_type)
976ca316 7914 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
7915 else
7916 {
976ca316 7917 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
7918 sig_type->signature = cu_header.signature;
7919 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7920 this_cu = &sig_type->per_cu;
7921 }
7922 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7923 this_cu->sect_off = sect_off;
f1902523 7924 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7925 this_cu->is_dwz = is_dwz;
8a0459fd 7926 this_cu->section = section;
ae038cb0 7927
976ca316 7928 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7929
7930 info_ptr = info_ptr + this_cu->length;
7931 }
36586728
TT
7932}
7933
7934/* Create a list of all compilation units in OBJFILE.
7935 This is only done for -readnow and building partial symtabs. */
7936
7937static void
976ca316 7938create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 7939{
976ca316
SM
7940 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
7941 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7942 &per_objfile->per_bfd->abbrev, 0);
36586728 7943
976ca316 7944 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 7945 if (dwz != NULL)
976ca316 7946 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
7947}
7948
5734ee8b 7949/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7950 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7951 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7952 DW_AT_ranges). See the comments of add_partial_subprogram on how
7953 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7954
72bf9492
DJ
7955static void
7956scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7957 CORE_ADDR *highpc, int set_addrmap,
7958 struct dwarf2_cu *cu)
c906108c 7959{
72bf9492 7960 struct partial_die_info *pdi;
c906108c 7961
91c24f0a
DC
7962 /* Now, march along the PDI's, descending into ones which have
7963 interesting children but skipping the children of the other ones,
7964 until we reach the end of the compilation unit. */
c906108c 7965
72bf9492 7966 pdi = first_die;
91c24f0a 7967
72bf9492
DJ
7968 while (pdi != NULL)
7969 {
52356b79 7970 pdi->fixup (cu);
c906108c 7971
f55ee35c 7972 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7973 children, so we need to look at them. Ditto for anonymous
7974 enums. */
933c6fe4 7975
7d00ffec 7976 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7977 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7978 || pdi->tag == DW_TAG_imported_unit
7979 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7980 {
72bf9492 7981 switch (pdi->tag)
c906108c
SS
7982 {
7983 case DW_TAG_subprogram:
b1dc1806 7984 case DW_TAG_inlined_subroutine:
cdc07690 7985 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
7986 if (cu->language == language_cplus)
7987 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7988 set_addrmap, cu);
c906108c 7989 break;
72929c62 7990 case DW_TAG_constant:
c906108c
SS
7991 case DW_TAG_variable:
7992 case DW_TAG_typedef:
91c24f0a 7993 case DW_TAG_union_type:
317d2668
TV
7994 if (!pdi->is_declaration
7995 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 7996 {
72bf9492 7997 add_partial_symbol (pdi, cu);
63d06c5c
DC
7998 }
7999 break;
c906108c 8000 case DW_TAG_class_type:
680b30c7 8001 case DW_TAG_interface_type:
c906108c 8002 case DW_TAG_structure_type:
72bf9492 8003 if (!pdi->is_declaration)
c906108c 8004 {
72bf9492 8005 add_partial_symbol (pdi, cu);
c906108c 8006 }
b7fee5a3
KS
8007 if ((cu->language == language_rust
8008 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8009 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8010 set_addrmap, cu);
c906108c 8011 break;
91c24f0a 8012 case DW_TAG_enumeration_type:
72bf9492
DJ
8013 if (!pdi->is_declaration)
8014 add_partial_enumeration (pdi, cu);
c906108c
SS
8015 break;
8016 case DW_TAG_base_type:
dda83cd7 8017 case DW_TAG_subrange_type:
c906108c 8018 /* File scope base type definitions are added to the partial
dda83cd7 8019 symbol table. */
72bf9492 8020 add_partial_symbol (pdi, cu);
c906108c 8021 break;
d9fa45fe 8022 case DW_TAG_namespace:
cdc07690 8023 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8024 break;
5d7cb8df 8025 case DW_TAG_module:
59c35742
AB
8026 if (!pdi->is_declaration)
8027 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8028 break;
95554aad
TT
8029 case DW_TAG_imported_unit:
8030 {
8031 struct dwarf2_per_cu_data *per_cu;
8032
f4dc4d17
DE
8033 /* For now we don't handle imported units in type units. */
8034 if (cu->per_cu->is_debug_types)
8035 {
8036 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8037 " supported in type units [in module %s]"),
5e22e966 8038 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8039 }
8040
e3b94546 8041 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8042 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8043
8044 /* Go read the partial unit, if needed. */
8045 if (per_cu->v.psymtab == NULL)
ab432490
SM
8046 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8047 cu->language);
95554aad 8048
ae640021 8049 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8050 }
8051 break;
74921315
KS
8052 case DW_TAG_imported_declaration:
8053 add_partial_symbol (pdi, cu);
8054 break;
c906108c
SS
8055 default:
8056 break;
8057 }
8058 }
8059
72bf9492
DJ
8060 /* If the die has a sibling, skip to the sibling. */
8061
8062 pdi = pdi->die_sibling;
8063 }
8064}
8065
8066/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8067
72bf9492 8068 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8069 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8070 Enumerators are an exception; they use the scope of their parent
8071 enumeration type, i.e. the name of the enumeration type is not
8072 prepended to the enumerator.
91c24f0a 8073
72bf9492
DJ
8074 There are two complexities. One is DW_AT_specification; in this
8075 case "parent" means the parent of the target of the specification,
8076 instead of the direct parent of the DIE. The other is compilers
8077 which do not emit DW_TAG_namespace; in this case we try to guess
8078 the fully qualified name of structure types from their members'
8079 linkage names. This must be done using the DIE's children rather
8080 than the children of any DW_AT_specification target. We only need
8081 to do this for structures at the top level, i.e. if the target of
8082 any DW_AT_specification (if any; otherwise the DIE itself) does not
8083 have a parent. */
8084
8085/* Compute the scope prefix associated with PDI's parent, in
8086 compilation unit CU. The result will be allocated on CU's
8087 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8088 field. NULL is returned if no prefix is necessary. */
15d034d0 8089static const char *
72bf9492
DJ
8090partial_die_parent_scope (struct partial_die_info *pdi,
8091 struct dwarf2_cu *cu)
8092{
15d034d0 8093 const char *grandparent_scope;
72bf9492 8094 struct partial_die_info *parent, *real_pdi;
91c24f0a 8095
72bf9492
DJ
8096 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8097 then this means the parent of the specification DIE. */
8098
8099 real_pdi = pdi;
72bf9492 8100 while (real_pdi->has_specification)
fb816e8b 8101 {
122cf0f2
AB
8102 auto res = find_partial_die (real_pdi->spec_offset,
8103 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8104 real_pdi = res.pdi;
8105 cu = res.cu;
8106 }
72bf9492
DJ
8107
8108 parent = real_pdi->die_parent;
8109 if (parent == NULL)
8110 return NULL;
8111
8112 if (parent->scope_set)
8113 return parent->scope;
8114
52356b79 8115 parent->fixup (cu);
72bf9492 8116
10b3939b 8117 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8118
acebe513
UW
8119 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8120 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8121 Work around this problem here. */
8122 if (cu->language == language_cplus
6e70227d 8123 && parent->tag == DW_TAG_namespace
7d00ffec 8124 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8125 && grandparent_scope == NULL)
8126 {
8127 parent->scope = NULL;
8128 parent->scope_set = 1;
8129 return NULL;
8130 }
8131
0a4b0913 8132 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8133 if (pdi->tag == DW_TAG_enumerator)
8134 /* Enumerators should not get the name of the enumeration as a prefix. */
8135 parent->scope = grandparent_scope;
8136 else if (parent->tag == DW_TAG_namespace
f55ee35c 8137 || parent->tag == DW_TAG_module
72bf9492
DJ
8138 || parent->tag == DW_TAG_structure_type
8139 || parent->tag == DW_TAG_class_type
680b30c7 8140 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8141 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8142 || parent->tag == DW_TAG_enumeration_type
8143 || (cu->language == language_fortran
8144 && parent->tag == DW_TAG_subprogram
8145 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8146 {
8147 if (grandparent_scope == NULL)
7d00ffec 8148 parent->scope = parent->name (cu);
72bf9492 8149 else
3e43a32a
MS
8150 parent->scope = typename_concat (&cu->comp_unit_obstack,
8151 grandparent_scope,
7d00ffec 8152 parent->name (cu), 0, cu);
72bf9492 8153 }
72bf9492
DJ
8154 else
8155 {
8156 /* FIXME drow/2004-04-01: What should we be doing with
8157 function-local names? For partial symbols, we should probably be
8158 ignoring them. */
fa9c3fa0
TT
8159 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8160 dwarf_tag_name (parent->tag),
8161 sect_offset_str (pdi->sect_off));
72bf9492 8162 parent->scope = grandparent_scope;
c906108c
SS
8163 }
8164
72bf9492
DJ
8165 parent->scope_set = 1;
8166 return parent->scope;
8167}
8168
8169/* Return the fully scoped name associated with PDI, from compilation unit
8170 CU. The result will be allocated with malloc. */
4568ecf9 8171
43816ebc 8172static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8173partial_die_full_name (struct partial_die_info *pdi,
8174 struct dwarf2_cu *cu)
8175{
15d034d0 8176 const char *parent_scope;
72bf9492 8177
98bfdba5
PA
8178 /* If this is a template instantiation, we can not work out the
8179 template arguments from partial DIEs. So, unfortunately, we have
8180 to go through the full DIEs. At least any work we do building
8181 types here will be reused if full symbols are loaded later. */
8182 if (pdi->has_template_arguments)
8183 {
52356b79 8184 pdi->fixup (cu);
98bfdba5 8185
7d00ffec 8186 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8187 {
8188 struct die_info *die;
8189 struct attribute attr;
8190 struct dwarf2_cu *ref_cu = cu;
8191
b64f50a1 8192 /* DW_FORM_ref_addr is using section offset. */
b4069958 8193 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8194 attr.form = DW_FORM_ref_addr;
9c541725 8195 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8196 die = follow_die_ref (NULL, &attr, &ref_cu);
8197
43816ebc 8198 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8199 }
8200 }
8201
72bf9492
DJ
8202 parent_scope = partial_die_parent_scope (pdi, cu);
8203 if (parent_scope == NULL)
8204 return NULL;
8205 else
43816ebc 8206 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8207 pdi->name (cu),
8208 0, cu));
c906108c
SS
8209}
8210
8211static void
72bf9492 8212add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8213{
976ca316
SM
8214 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8215 struct objfile *objfile = per_objfile->objfile;
08feed99 8216 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8217 CORE_ADDR addr = 0;
15d034d0 8218 const char *actual_name = NULL;
e142c38c
DJ
8219 CORE_ADDR baseaddr;
8220
b3b3bada 8221 baseaddr = objfile->text_section_offset ();
c906108c 8222
43816ebc
TT
8223 gdb::unique_xmalloc_ptr<char> built_actual_name
8224 = partial_die_full_name (pdi, cu);
15d034d0 8225 if (built_actual_name != NULL)
43816ebc 8226 actual_name = built_actual_name.get ();
63d06c5c 8227
72bf9492 8228 if (actual_name == NULL)
7d00ffec 8229 actual_name = pdi->name (cu);
72bf9492 8230
76e288d1
TT
8231 partial_symbol psymbol;
8232 memset (&psymbol, 0, sizeof (psymbol));
8233 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 8234 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
8235
8236 /* The code below indicates that the psymbol should be installed by
8237 setting this. */
8238 gdb::optional<psymbol_placement> where;
8239
c906108c
SS
8240 switch (pdi->tag)
8241 {
b1dc1806 8242 case DW_TAG_inlined_subroutine:
c906108c 8243 case DW_TAG_subprogram:
79748972
TT
8244 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8245 - baseaddr);
0a4b0913
AB
8246 if (pdi->is_external
8247 || cu->language == language_ada
8248 || (cu->language == language_fortran
8249 && pdi->die_parent != NULL
8250 && pdi->die_parent->tag == DW_TAG_subprogram))
8251 {
dda83cd7
SM
8252 /* Normally, only "external" DIEs are part of the global scope.
8253 But in Ada and Fortran, we want to be able to access nested
8254 procedures globally. So all Ada and Fortran subprograms are
8255 stored in the global scope. */
76e288d1 8256 where = psymbol_placement::GLOBAL;
c906108c
SS
8257 }
8258 else
76e288d1
TT
8259 where = psymbol_placement::STATIC;
8260
8261 psymbol.domain = VAR_DOMAIN;
8262 psymbol.aclass = LOC_BLOCK;
a52d653e 8263 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 8264 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8265
8266 if (pdi->main_subprogram && actual_name != NULL)
8267 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8268 break;
72929c62 8269 case DW_TAG_constant:
76e288d1
TT
8270 psymbol.domain = VAR_DOMAIN;
8271 psymbol.aclass = LOC_STATIC;
8272 where = (pdi->is_external
8273 ? psymbol_placement::GLOBAL
8274 : psymbol_placement::STATIC);
72929c62 8275 break;
c906108c 8276 case DW_TAG_variable:
95554aad
TT
8277 if (pdi->d.locdesc)
8278 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8279
95554aad 8280 if (pdi->d.locdesc
caac4577 8281 && addr == 0
976ca316 8282 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8283 {
8284 /* A global or static variable may also have been stripped
8285 out by the linker if unused, in which case its address
8286 will be nullified; do not add such variables into partial
8287 symbol table then. */
8288 }
8289 else if (pdi->is_external)
c906108c
SS
8290 {
8291 /* Global Variable.
8292 Don't enter into the minimal symbol tables as there is
8293 a minimal symbol table entry from the ELF symbols already.
8294 Enter into partial symbol table if it has a location
8295 descriptor or a type.
8296 If the location descriptor is missing, new_symbol will create
8297 a LOC_UNRESOLVED symbol, the address of the variable will then
8298 be determined from the minimal symbol table whenever the variable
8299 is referenced.
8300 The address for the partial symbol table entry is not
8301 used by GDB, but it comes in handy for debugging partial symbol
8302 table building. */
8303
95554aad 8304 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8305 {
8306 psymbol.domain = VAR_DOMAIN;
8307 psymbol.aclass = LOC_STATIC;
a52d653e 8308 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8309 psymbol.ginfo.value.address = addr;
8310 where = psymbol_placement::GLOBAL;
8311 }
c906108c
SS
8312 }
8313 else
8314 {
ff908ebf
AW
8315 int has_loc = pdi->d.locdesc != NULL;
8316
8317 /* Static Variable. Skip symbols whose value we cannot know (those
8318 without location descriptors or constant values). */
8319 if (!has_loc && !pdi->has_const_value)
43816ebc 8320 return;
ff908ebf 8321
76e288d1
TT
8322 psymbol.domain = VAR_DOMAIN;
8323 psymbol.aclass = LOC_STATIC;
a52d653e 8324 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8325 if (has_loc)
8326 psymbol.ginfo.value.address = addr;
8327 where = psymbol_placement::STATIC;
c906108c
SS
8328 }
8329 break;
d8f62e84 8330 case DW_TAG_array_type:
c906108c
SS
8331 case DW_TAG_typedef:
8332 case DW_TAG_base_type:
a02abb62 8333 case DW_TAG_subrange_type:
76e288d1
TT
8334 psymbol.domain = VAR_DOMAIN;
8335 psymbol.aclass = LOC_TYPEDEF;
8336 where = psymbol_placement::STATIC;
c906108c 8337 break;
74921315 8338 case DW_TAG_imported_declaration:
72bf9492 8339 case DW_TAG_namespace:
76e288d1
TT
8340 psymbol.domain = VAR_DOMAIN;
8341 psymbol.aclass = LOC_TYPEDEF;
8342 where = psymbol_placement::GLOBAL;
72bf9492 8343 break;
530e8392 8344 case DW_TAG_module:
a5fd13a9 8345 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8346 available without any name. If so, we skip the module as it
8347 doesn't bring any value. */
a5fd13a9 8348 if (actual_name != nullptr)
76e288d1
TT
8349 {
8350 psymbol.domain = MODULE_DOMAIN;
8351 psymbol.aclass = LOC_TYPEDEF;
8352 where = psymbol_placement::GLOBAL;
8353 }
530e8392 8354 break;
c906108c 8355 case DW_TAG_class_type:
680b30c7 8356 case DW_TAG_interface_type:
c906108c
SS
8357 case DW_TAG_structure_type:
8358 case DW_TAG_union_type:
8359 case DW_TAG_enumeration_type:
fa4028e9 8360 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8361 about "Structure, Union, and Class Type Entries": "An incomplete
8362 structure, union or class type is represented by a structure,
8363 union or class entry that does not have a byte size attribute
8364 and that has a DW_AT_declaration attribute." */
fa4028e9 8365 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8366 return;
fa4028e9 8367
63d06c5c
DC
8368 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8369 static vs. global. */
76e288d1
TT
8370 psymbol.domain = STRUCT_DOMAIN;
8371 psymbol.aclass = LOC_TYPEDEF;
8372 where = (cu->language == language_cplus
8373 ? psymbol_placement::GLOBAL
8374 : psymbol_placement::STATIC);
c906108c
SS
8375 break;
8376 case DW_TAG_enumerator:
76e288d1
TT
8377 psymbol.domain = VAR_DOMAIN;
8378 psymbol.aclass = LOC_CONST;
8379 where = (cu->language == language_cplus
8380 ? psymbol_placement::GLOBAL
8381 : psymbol_placement::STATIC);
c906108c
SS
8382 break;
8383 default:
8384 break;
8385 }
76e288d1
TT
8386
8387 if (where.has_value ())
8388 {
f049a313
TT
8389 if (built_actual_name != nullptr)
8390 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8391 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8392 psymbol.ginfo.set_linkage_name (actual_name);
8393 else
8394 {
8395 psymbol.ginfo.set_demangled_name (actual_name,
8396 &objfile->objfile_obstack);
8397 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8398 }
7b249e47
TT
8399 cu->per_cu->v.psymtab->add_psymbol
8400 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8401 objfile);
76e288d1 8402 }
c906108c
SS
8403}
8404
5c4e30ca
DC
8405/* Read a partial die corresponding to a namespace; also, add a symbol
8406 corresponding to that namespace to the symbol table. NAMESPACE is
8407 the name of the enclosing namespace. */
91c24f0a 8408
72bf9492
DJ
8409static void
8410add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8411 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8412 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8413{
72bf9492 8414 /* Add a symbol for the namespace. */
e7c27a73 8415
72bf9492 8416 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8417
8418 /* Now scan partial symbols in that namespace. */
8419
91c24f0a 8420 if (pdi->has_children)
cdc07690 8421 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8422}
8423
5d7cb8df
JK
8424/* Read a partial die corresponding to a Fortran module. */
8425
8426static void
8427add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8428 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8429{
530e8392
KB
8430 /* Add a symbol for the namespace. */
8431
8432 add_partial_symbol (pdi, cu);
8433
f55ee35c 8434 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8435
8436 if (pdi->has_children)
cdc07690 8437 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8438}
8439
b1dc1806
XR
8440/* Read a partial die corresponding to a subprogram or an inlined
8441 subprogram and create a partial symbol for that subprogram.
8442 When the CU language allows it, this routine also defines a partial
8443 symbol for each nested subprogram that this subprogram contains.
8444 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8445 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8446
cdc07690
YQ
8447 PDI may also be a lexical block, in which case we simply search
8448 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8449 Again, this is only performed when the CU language allows this
8450 type of definitions. */
8451
8452static void
8453add_partial_subprogram (struct partial_die_info *pdi,
8454 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8455 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8456{
b1dc1806 8457 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8458 {
8459 if (pdi->has_pc_info)
dda83cd7
SM
8460 {
8461 if (pdi->lowpc < *lowpc)
8462 *lowpc = pdi->lowpc;
8463 if (pdi->highpc > *highpc)
8464 *highpc = pdi->highpc;
cdc07690 8465 if (set_addrmap)
5734ee8b 8466 {
5e22e966 8467 struct objfile *objfile = cu->per_objfile->objfile;
84685904 8468 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 8469 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8470 CORE_ADDR baseaddr;
b926417a
TT
8471 CORE_ADDR this_highpc;
8472 CORE_ADDR this_lowpc;
5734ee8b 8473
b3b3bada 8474 baseaddr = objfile->text_section_offset ();
b926417a
TT
8475 this_lowpc
8476 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8477 pdi->lowpc + baseaddr)
8478 - baseaddr);
8479 this_highpc
8480 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8481 pdi->highpc + baseaddr)
8482 - baseaddr);
84685904 8483 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
b926417a 8484 this_lowpc, this_highpc - 1,
9291a0cd 8485 cu->per_cu->v.psymtab);
5734ee8b 8486 }
dda83cd7 8487 }
481860b3
GB
8488
8489 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8490 {
dda83cd7 8491 if (!pdi->is_declaration)
e8d05480
JB
8492 /* Ignore subprogram DIEs that do not have a name, they are
8493 illegal. Do not emit a complaint at this point, we will
8494 do so when we convert this psymtab into a symtab. */
7d00ffec 8495 if (pdi->name (cu))
e8d05480 8496 add_partial_symbol (pdi, cu);
dda83cd7 8497 }
bc30ff58 8498 }
6e70227d 8499
bc30ff58
JB
8500 if (! pdi->has_children)
8501 return;
8502
0a4b0913 8503 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8504 {
8505 pdi = pdi->die_child;
8506 while (pdi != NULL)
8507 {
52356b79 8508 pdi->fixup (cu);
bc30ff58 8509 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8510 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8511 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8512 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8513 pdi = pdi->die_sibling;
8514 }
8515 }
8516}
8517
91c24f0a
DC
8518/* Read a partial die corresponding to an enumeration type. */
8519
72bf9492
DJ
8520static void
8521add_partial_enumeration (struct partial_die_info *enum_pdi,
8522 struct dwarf2_cu *cu)
91c24f0a 8523{
72bf9492 8524 struct partial_die_info *pdi;
91c24f0a 8525
7d00ffec 8526 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8527 add_partial_symbol (enum_pdi, cu);
8528
8529 pdi = enum_pdi->die_child;
8530 while (pdi)
91c24f0a 8531 {
7d00ffec 8532 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8533 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8534 else
72bf9492
DJ
8535 add_partial_symbol (pdi, cu);
8536 pdi = pdi->die_sibling;
91c24f0a 8537 }
91c24f0a
DC
8538}
8539
6caca83c
CC
8540/* Return the initial uleb128 in the die at INFO_PTR. */
8541
8542static unsigned int
d521ce57 8543peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8544{
8545 unsigned int bytes_read;
8546
8547 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8548}
8549
685af9cd
TT
8550/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8551 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8552
4bb7a0a7
DJ
8553 Return the corresponding abbrev, or NULL if the number is zero (indicating
8554 an empty DIE). In either case *BYTES_READ will be set to the length of
8555 the initial number. */
8556
7c32eebb 8557static const struct abbrev_info *
685af9cd
TT
8558peek_die_abbrev (const die_reader_specs &reader,
8559 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8560{
685af9cd 8561 dwarf2_cu *cu = reader.cu;
0280fdcc 8562 bfd *abfd = reader.abfd;
685af9cd
TT
8563 unsigned int abbrev_number
8564 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8565
8566 if (abbrev_number == 0)
8567 return NULL;
8568
7c32eebb
TT
8569 const abbrev_info *abbrev
8570 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8571 if (!abbrev)
8572 {
422b9917 8573 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8574 " at offset %s [in module %s]"),
422b9917 8575 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8576 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8577 }
8578
8579 return abbrev;
8580}
8581
93311388
DE
8582/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8583 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8584 DIE. Any children of the skipped DIEs will also be skipped. */
8585
d521ce57
TT
8586static const gdb_byte *
8587skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8588{
4bb7a0a7
DJ
8589 while (1)
8590 {
685af9cd 8591 unsigned int bytes_read;
7c32eebb
TT
8592 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8593 &bytes_read);
685af9cd 8594
4bb7a0a7
DJ
8595 if (abbrev == NULL)
8596 return info_ptr + bytes_read;
8597 else
dee91e82 8598 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8599 }
8600}
8601
93311388
DE
8602/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8603 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8604 abbrev corresponding to that skipped uleb128 should be passed in
8605 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8606 children. */
8607
d521ce57
TT
8608static const gdb_byte *
8609skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8610 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8611{
8612 unsigned int bytes_read;
8613 struct attribute attr;
dee91e82
DE
8614 bfd *abfd = reader->abfd;
8615 struct dwarf2_cu *cu = reader->cu;
d521ce57 8616 const gdb_byte *buffer = reader->buffer;
f664829e 8617 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8618 unsigned int form, i;
8619
8620 for (i = 0; i < abbrev->num_attrs; i++)
8621 {
8622 /* The only abbrev we care about is DW_AT_sibling. */
8623 if (abbrev->attrs[i].name == DW_AT_sibling)
8624 {
7a5f294d 8625 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8626 if (attr.form == DW_FORM_ref_addr)
b98664d3 8627 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8628 else
b9502d3f 8629 {
0826b30a 8630 sect_offset off = attr.get_ref_die_offset ();
9c541725 8631 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8632
8633 if (sibling_ptr < info_ptr)
b98664d3 8634 complaint (_("DW_AT_sibling points backwards"));
22869d73 8635 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8636 reader->die_section->overflow_complaint ();
b9502d3f
WN
8637 else
8638 return sibling_ptr;
8639 }
4bb7a0a7
DJ
8640 }
8641
8642 /* If it isn't DW_AT_sibling, skip this attribute. */
8643 form = abbrev->attrs[i].form;
8644 skip_attribute:
8645 switch (form)
8646 {
4bb7a0a7 8647 case DW_FORM_ref_addr:
ae411497
TT
8648 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8649 and later it is offset sized. */
8650 if (cu->header.version == 2)
8651 info_ptr += cu->header.addr_size;
8652 else
8653 info_ptr += cu->header.offset_size;
8654 break;
36586728
TT
8655 case DW_FORM_GNU_ref_alt:
8656 info_ptr += cu->header.offset_size;
8657 break;
ae411497 8658 case DW_FORM_addr:
4bb7a0a7
DJ
8659 info_ptr += cu->header.addr_size;
8660 break;
8661 case DW_FORM_data1:
8662 case DW_FORM_ref1:
8663 case DW_FORM_flag:
8fe0f950 8664 case DW_FORM_strx1:
4bb7a0a7
DJ
8665 info_ptr += 1;
8666 break;
2dc7f7b3 8667 case DW_FORM_flag_present:
43988095 8668 case DW_FORM_implicit_const:
2dc7f7b3 8669 break;
4bb7a0a7
DJ
8670 case DW_FORM_data2:
8671 case DW_FORM_ref2:
8fe0f950 8672 case DW_FORM_strx2:
4bb7a0a7
DJ
8673 info_ptr += 2;
8674 break;
8fe0f950
AT
8675 case DW_FORM_strx3:
8676 info_ptr += 3;
8677 break;
4bb7a0a7
DJ
8678 case DW_FORM_data4:
8679 case DW_FORM_ref4:
8fe0f950 8680 case DW_FORM_strx4:
4bb7a0a7
DJ
8681 info_ptr += 4;
8682 break;
8683 case DW_FORM_data8:
8684 case DW_FORM_ref8:
55f1336d 8685 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8686 info_ptr += 8;
8687 break;
0224619f
JK
8688 case DW_FORM_data16:
8689 info_ptr += 16;
8690 break;
4bb7a0a7 8691 case DW_FORM_string:
9b1c24c8 8692 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8693 info_ptr += bytes_read;
8694 break;
2dc7f7b3 8695 case DW_FORM_sec_offset:
4bb7a0a7 8696 case DW_FORM_strp:
36586728 8697 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8698 info_ptr += cu->header.offset_size;
8699 break;
2dc7f7b3 8700 case DW_FORM_exprloc:
4bb7a0a7
DJ
8701 case DW_FORM_block:
8702 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8703 info_ptr += bytes_read;
8704 break;
8705 case DW_FORM_block1:
8706 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8707 break;
8708 case DW_FORM_block2:
8709 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8710 break;
8711 case DW_FORM_block4:
8712 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8713 break;
336d760d 8714 case DW_FORM_addrx:
cf532bd1 8715 case DW_FORM_strx:
4bb7a0a7
DJ
8716 case DW_FORM_sdata:
8717 case DW_FORM_udata:
8718 case DW_FORM_ref_udata:
3019eac3
DE
8719 case DW_FORM_GNU_addr_index:
8720 case DW_FORM_GNU_str_index:
18a8505e 8721 case DW_FORM_rnglistx:
41144253 8722 case DW_FORM_loclistx:
d521ce57 8723 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8724 break;
8725 case DW_FORM_indirect:
8726 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8727 info_ptr += bytes_read;
8728 /* We need to continue parsing from here, so just go back to
8729 the top. */
8730 goto skip_attribute;
8731
8732 default:
3e43a32a
MS
8733 error (_("Dwarf Error: Cannot handle %s "
8734 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8735 dwarf_form_name (form),
8736 bfd_get_filename (abfd));
8737 }
8738 }
8739
8740 if (abbrev->has_children)
dee91e82 8741 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8742 else
8743 return info_ptr;
8744}
8745
93311388 8746/* Locate ORIG_PDI's sibling.
dee91e82 8747 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8748
d521ce57 8749static const gdb_byte *
dee91e82
DE
8750locate_pdi_sibling (const struct die_reader_specs *reader,
8751 struct partial_die_info *orig_pdi,
d521ce57 8752 const gdb_byte *info_ptr)
91c24f0a
DC
8753{
8754 /* Do we know the sibling already? */
72bf9492 8755
91c24f0a
DC
8756 if (orig_pdi->sibling)
8757 return orig_pdi->sibling;
8758
8759 /* Are there any children to deal with? */
8760
8761 if (!orig_pdi->has_children)
8762 return info_ptr;
8763
4bb7a0a7 8764 /* Skip the children the long way. */
91c24f0a 8765
dee91e82 8766 return skip_children (reader, info_ptr);
91c24f0a
DC
8767}
8768
257e7a09 8769/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8770 not NULL. */
c906108c 8771
891813be
TT
8772void
8773dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8774{
976ca316 8775 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8776
976ca316 8777 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8778
077cbab2
TT
8779 /* If this psymtab is constructed from a debug-only objfile, the
8780 has_section_at_zero flag will not necessarily be correct. We
8781 can get the correct value for this flag by looking at the data
8782 associated with the (presumably stripped) associated objfile. */
8783 if (objfile->separate_debug_objfile_backlink)
c906108c 8784 {
976ca316 8785 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8786 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8787
976ca316
SM
8788 per_objfile->per_bfd->has_section_at_zero
8789 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8790 }
98bfdba5 8791
8566b89b 8792 expand_psymtab (objfile);
95554aad 8793
976ca316 8794 process_cu_includes (per_objfile);
c906108c 8795}
9cdd5dbd
DE
8796\f
8797/* Reading in full CUs. */
c906108c 8798
10b3939b
DJ
8799/* Add PER_CU to the queue. */
8800
8801static void
120ce1b5
SM
8802queue_comp_unit (dwarf2_per_cu_data *per_cu,
8803 dwarf2_per_objfile *per_objfile,
95554aad 8804 enum language pretend_language)
10b3939b 8805{
10b3939b 8806 per_cu->queued = 1;
08ac5771
SM
8807
8808 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8809 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8810}
8811
616c069a
SM
8812/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8813
89e63ee4
DE
8814 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8815 dependency.
69d751e3 8816
616c069a
SM
8817 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8818 DIEs, false otherwise.
8819
8820 Explanation: there is an invariant that if a CU is queued for expansion
8821 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8822 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8823 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8824 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8825 invariant is respected.
8826
8827 The caller is therefore not required to load the CU's DIEs (we return false)
8828 if:
8829
8830 - the CU is already expanded, and therefore does not get enqueued
8831 - the CU gets enqueued for expansion, but its DIEs are already loaded
8832
8833 Note that the caller should not use this function's return value as an
8834 indicator of whether the CU's DIEs are loaded right now, it should check
8835 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
8836
8837static int
89e63ee4 8838maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8839 dwarf2_per_cu_data *per_cu,
8840 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8841 enum language pretend_language)
8842{
8843 /* We may arrive here during partial symbol reading, if we need full
8844 DIEs to process an unusual case (e.g. template arguments). Do
8845 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8846 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8847 {
7188ed02
SM
8848 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8849
8850 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8851 return 1;
8852 return 0;
8853 }
8854
8855 /* Mark the dependence relation so that we don't flush PER_CU
8856 too early. */
89e63ee4
DE
8857 if (dependent_cu != NULL)
8858 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8859
8860 /* If it's already on the queue, we have nothing to do. */
8861 if (per_cu->queued)
de53369b
SM
8862 {
8863 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8864 loaded. */
8865 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
8866
8867 /* If the CU is queued for expansion, it should not already be
8868 expanded. */
8869 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8870
8871 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
8872 return 0;
8873 }
0907af0c 8874
616c069a
SM
8875 bool queued = false;
8876 if (!per_objfile->symtab_set_p (per_cu))
8877 {
8878 /* Add it to the queue. */
8879 queue_comp_unit (per_cu, per_objfile, pretend_language);
8880 queued = true;
8881 }
8882
0907af0c
DE
8883 /* If the compilation unit is already loaded, just mark it as
8884 used. */
7188ed02
SM
8885 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8886 if (cu != nullptr)
616c069a 8887 cu->last_used = 0;
0907af0c 8888
616c069a
SM
8889 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8890 and the DIEs are not already loaded. */
8891 return queued && cu == nullptr;
0907af0c
DE
8892}
8893
10b3939b
DJ
8894/* Process the queue. */
8895
8896static void
976ca316 8897process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 8898{
6f738b01
SM
8899 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8900 objfile_name (per_objfile->objfile));
45cfd468 8901
03dd20cc
DJ
8902 /* The queue starts out with one item, but following a DIE reference
8903 may load a new CU, adding it to the end of the queue. */
08ac5771 8904 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 8905 {
08ac5771 8906 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 8907 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 8908
976ca316 8909 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 8910 {
976ca316 8911 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 8912
7188ed02
SM
8913 /* Skip dummy CUs. */
8914 if (cu != nullptr)
73be47f5 8915 {
7188ed02
SM
8916 unsigned int debug_print_threshold;
8917 char buf[100];
8918
8919 if (per_cu->is_debug_types)
8920 {
8921 struct signatured_type *sig_type =
8922 (struct signatured_type *) per_cu;
8923
8924 sprintf (buf, "TU %s at offset %s",
8925 hex_string (sig_type->signature),
8926 sect_offset_str (per_cu->sect_off));
8927 /* There can be 100s of TUs.
8928 Only print them in verbose mode. */
8929 debug_print_threshold = 2;
8930 }
8931 else
8932 {
8933 sprintf (buf, "CU at offset %s",
8934 sect_offset_str (per_cu->sect_off));
8935 debug_print_threshold = 1;
8936 }
247f5c4f 8937
7188ed02 8938 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8939 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 8940
7188ed02
SM
8941 if (per_cu->is_debug_types)
8942 process_full_type_unit (cu, item.pretend_language);
8943 else
8944 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 8945
7188ed02 8946 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8947 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 8948 }
f4dc4d17 8949 }
10b3939b 8950
7188ed02 8951 per_cu->queued = 0;
08ac5771 8952 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
8953 }
8954
6f738b01
SM
8955 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8956 objfile_name (per_objfile->objfile));
10b3939b
DJ
8957}
8958
10b3939b
DJ
8959/* Read in full symbols for PST, and anything it depends on. */
8960
8566b89b
TT
8961void
8962dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8963{
af758d11 8964 gdb_assert (!readin_p (objfile));
95554aad 8965
17ee85fc
TT
8966 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8967 free_cached_comp_units freer (per_objfile);
48993951 8968 expand_dependencies (objfile);
aaa75496 8969
97a1449a 8970 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 8971 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
8972}
8973
af758d11
SM
8974/* See psympriv.h. */
8975
8976bool
8977dwarf2_psymtab::readin_p (struct objfile *objfile) const
8978{
8979 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8980 return per_objfile->symtab_set_p (per_cu_data);
8981}
8982
8983/* See psympriv.h. */
8984
8985compunit_symtab *
8986dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8987{
8988 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8989 return per_objfile->get_symtab (per_cu_data);
8990}
8991
dee91e82
DE
8992/* Trivial hash function for die_info: the hash value of a DIE
8993 is its offset in .debug_info for this objfile. */
10b3939b 8994
dee91e82
DE
8995static hashval_t
8996die_hash (const void *item)
10b3939b 8997{
9a3c8263 8998 const struct die_info *die = (const struct die_info *) item;
6502dd73 8999
9c541725 9000 return to_underlying (die->sect_off);
dee91e82 9001}
63d06c5c 9002
dee91e82
DE
9003/* Trivial comparison function for die_info structures: two DIEs
9004 are equal if they have the same offset. */
98bfdba5 9005
dee91e82
DE
9006static int
9007die_eq (const void *item_lhs, const void *item_rhs)
9008{
9a3c8263
SM
9009 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9010 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9011
9c541725 9012 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9013}
c906108c 9014
4a636814
SM
9015/* Load the DIEs associated with PER_CU into memory.
9016
9017 In some cases, the caller, while reading partial symbols, will need to load
9018 the full symbols for the CU for some reason. It will already have a
9019 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9020 rather than creating a new one. */
c906108c 9021
dee91e82 9022static void
ab432490
SM
9023load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9024 dwarf2_per_objfile *per_objfile,
4a636814 9025 dwarf2_cu *existing_cu,
c0ab21c2
TT
9026 bool skip_partial,
9027 enum language pretend_language)
dee91e82 9028{
c0ab21c2
TT
9029 gdb_assert (! this_cu->is_debug_types);
9030
7188ed02 9031 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9032 if (reader.dummy_p)
9033 return;
9034
9035 struct dwarf2_cu *cu = reader.cu;
9036 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9037
dee91e82
DE
9038 gdb_assert (cu->die_hash == NULL);
9039 cu->die_hash =
9040 htab_create_alloc_ex (cu->header.length / 12,
9041 die_hash,
9042 die_eq,
9043 NULL,
9044 &cu->comp_unit_obstack,
9045 hashtab_obstack_allocate,
9046 dummy_obstack_deallocate);
e142c38c 9047
3e225074 9048 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9049 reader.comp_unit_die->child
9050 = read_die_and_siblings (&reader, reader.info_ptr,
9051 &info_ptr, reader.comp_unit_die);
9052 cu->dies = reader.comp_unit_die;
dee91e82 9053 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9054
9055 /* We try not to read any attributes in this function, because not
9cdd5dbd 9056 all CUs needed for references have been loaded yet, and symbol
10b3939b 9057 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9058 or we won't be able to build types correctly.
9059 Similarly, if we do not read the producer, we can not apply
9060 producer-specific interpretation. */
c0ab21c2 9061 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9062
9063 reader.keep ();
10b3939b
DJ
9064}
9065
3da10d80
KS
9066/* Add a DIE to the delayed physname list. */
9067
9068static void
9069add_to_method_list (struct type *type, int fnfield_index, int index,
9070 const char *name, struct die_info *die,
9071 struct dwarf2_cu *cu)
9072{
9073 struct delayed_method_info mi;
9074 mi.type = type;
9075 mi.fnfield_index = fnfield_index;
9076 mi.index = index;
9077 mi.name = name;
9078 mi.die = die;
c89b44cd 9079 cu->method_list.push_back (mi);
3da10d80
KS
9080}
9081
3693fdb3
PA
9082/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9083 "const" / "volatile". If so, decrements LEN by the length of the
9084 modifier and return true. Otherwise return false. */
9085
9086template<size_t N>
9087static bool
9088check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9089{
9090 size_t mod_len = sizeof (mod) - 1;
9091 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9092 {
9093 len -= mod_len;
9094 return true;
9095 }
9096 return false;
9097}
9098
3da10d80
KS
9099/* Compute the physnames of any methods on the CU's method list.
9100
9101 The computation of method physnames is delayed in order to avoid the
9102 (bad) condition that one of the method's formal parameters is of an as yet
9103 incomplete type. */
9104
9105static void
9106compute_delayed_physnames (struct dwarf2_cu *cu)
9107{
3693fdb3 9108 /* Only C++ delays computing physnames. */
c89b44cd 9109 if (cu->method_list.empty ())
3693fdb3
PA
9110 return;
9111 gdb_assert (cu->language == language_cplus);
9112
52941706 9113 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9114 {
1d06ead6 9115 const char *physname;
3da10d80 9116 struct fn_fieldlist *fn_flp
c89b44cd
TT
9117 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9118 physname = dwarf2_physname (mi.name, mi.die, cu);
9119 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9120 = physname ? physname : "";
3693fdb3
PA
9121
9122 /* Since there's no tag to indicate whether a method is a
9123 const/volatile overload, extract that information out of the
9124 demangled name. */
9125 if (physname != NULL)
9126 {
9127 size_t len = strlen (physname);
9128
9129 while (1)
9130 {
9131 if (physname[len] == ')') /* shortcut */
9132 break;
9133 else if (check_modifier (physname, len, " const"))
c89b44cd 9134 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9135 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9136 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9137 else
9138 break;
9139 }
9140 }
3da10d80 9141 }
c89b44cd
TT
9142
9143 /* The list is no longer needed. */
9144 cu->method_list.clear ();
3da10d80
KS
9145}
9146
a766d390
DE
9147/* Go objects should be embedded in a DW_TAG_module DIE,
9148 and it's not clear if/how imported objects will appear.
9149 To keep Go support simple until that's worked out,
9150 go back through what we've read and create something usable.
9151 We could do this while processing each DIE, and feels kinda cleaner,
9152 but that way is more invasive.
9153 This is to, for example, allow the user to type "p var" or "b main"
9154 without having to specify the package name, and allow lookups
9155 of module.object to work in contexts that use the expression
9156 parser. */
9157
9158static void
9159fixup_go_packaging (struct dwarf2_cu *cu)
9160{
421d1616 9161 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9162 struct pending *list;
9163 int i;
9164
c24bdb02 9165 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9166 list != NULL;
9167 list = list->next)
a766d390
DE
9168 {
9169 for (i = 0; i < list->nsyms; ++i)
9170 {
9171 struct symbol *sym = list->symbol[i];
9172
c1b5c1eb 9173 if (sym->language () == language_go
a766d390
DE
9174 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9175 {
421d1616
TT
9176 gdb::unique_xmalloc_ptr<char> this_package_name
9177 (go_symbol_package_name (sym));
a766d390
DE
9178
9179 if (this_package_name == NULL)
9180 continue;
9181 if (package_name == NULL)
421d1616 9182 package_name = std::move (this_package_name);
a766d390
DE
9183 else
9184 {
5e22e966 9185 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9186 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9187 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9188 (symbol_symtab (sym) != NULL
9189 ? symtab_to_filename_for_display
9190 (symbol_symtab (sym))
e3b94546 9191 : objfile_name (objfile)),
421d1616 9192 this_package_name.get (), package_name.get ());
a766d390
DE
9193 }
9194 }
9195 }
9196 }
9197
9198 if (package_name != NULL)
9199 {
5e22e966 9200 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9201 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9202 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9203 saved_package_name);
a766d390
DE
9204 struct symbol *sym;
9205
8c14c3a3 9206 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9207 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9208 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9209 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9210 e.g., "main" finds the "main" module and not C's main(). */
9211 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9212 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9213 SYMBOL_TYPE (sym) = type;
9214
c24bdb02 9215 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9216 }
9217}
9218
c9317f21
TT
9219/* Allocate a fully-qualified name consisting of the two parts on the
9220 obstack. */
9221
9222static const char *
9223rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9224{
9225 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9226}
9227
9c6a1327
TT
9228/* A helper that allocates a variant part to attach to a Rust enum
9229 type. OBSTACK is where the results should be allocated. TYPE is
9230 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9231 discriminant. It must be the index of one of the fields of TYPE,
9232 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9233 DEFAULT_INDEX is the index of the default field; or -1 if there is
9234 no default. RANGES is indexed by "effective" field number (the
9235 field index, but omitting the discriminant and default fields) and
9236 must hold the discriminant values used by the variants. Note that
9237 RANGES must have a lifetime at least as long as OBSTACK -- either
9238 already allocated on it, or static. */
c9317f21 9239
9c6a1327
TT
9240static void
9241alloc_rust_variant (struct obstack *obstack, struct type *type,
9242 int discriminant_index, int default_index,
9243 gdb::array_view<discriminant_range> ranges)
9244{
57d02173
TT
9245 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9246 gdb_assert (discriminant_index == -1
9247 || (discriminant_index >= 0
9248 && discriminant_index < type->num_fields ()));
c9317f21 9249 gdb_assert (default_index == -1
1f704f76 9250 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9251
9c6a1327 9252 /* We have one variant for each non-discriminant field. */
57d02173
TT
9253 int n_variants = type->num_fields ();
9254 if (discriminant_index != -1)
9255 --n_variants;
c9317f21 9256
9c6a1327
TT
9257 variant *variants = new (obstack) variant[n_variants];
9258 int var_idx = 0;
9259 int range_idx = 0;
1f704f76 9260 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9261 {
9262 if (i == discriminant_index)
9263 continue;
c9317f21 9264
9c6a1327
TT
9265 variants[var_idx].first_field = i;
9266 variants[var_idx].last_field = i + 1;
9267
9268 /* The default field does not need a range, but other fields do.
9269 We skipped the discriminant above. */
9270 if (i != default_index)
9271 {
9272 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9273 ++range_idx;
9274 }
c9317f21 9275
9c6a1327
TT
9276 ++var_idx;
9277 }
9278
9279 gdb_assert (range_idx == ranges.size ());
9280 gdb_assert (var_idx == n_variants);
9281
9282 variant_part *part = new (obstack) variant_part;
9283 part->discriminant_index = discriminant_index;
57d02173
TT
9284 /* If there is no discriminant, then whether it is signed is of no
9285 consequence. */
9286 part->is_unsigned
9287 = (discriminant_index == -1
9288 ? false
c6d940a9 9289 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9290 part->variants = gdb::array_view<variant> (variants, n_variants);
9291
9292 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9293 gdb::array_view<variant_part> *prop_value
9294 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9295
9c6a1327 9296 struct dynamic_prop prop;
8c2e4e06 9297 prop.set_variant_parts (prop_value);
9c6a1327 9298
5c54719c 9299 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9300}
9301
9302/* Some versions of rustc emitted enums in an unusual way.
9303
9304 Ordinary enums were emitted as unions. The first element of each
9305 structure in the union was named "RUST$ENUM$DISR". This element
9306 held the discriminant.
9307
9308 These versions of Rust also implemented the "non-zero"
9309 optimization. When the enum had two values, and one is empty and
9310 the other holds a pointer that cannot be zero, the pointer is used
9311 as the discriminant, with a zero value meaning the empty variant.
9312 Here, the union's first member is of the form
9313 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9314 where the fieldnos are the indices of the fields that should be
9315 traversed in order to find the field (which may be several fields deep)
9316 and the variantname is the name of the variant of the case when the
9317 field is zero.
9318
9319 This function recognizes whether TYPE is of one of these forms,
9320 and, if so, smashes it to be a variant type. */
9321
9322static void
9323quirk_rust_enum (struct type *type, struct objfile *objfile)
9324{
78134374 9325 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9326
9327 /* We don't need to deal with empty enums. */
1f704f76 9328 if (type->num_fields () == 0)
c9317f21
TT
9329 return;
9330
9331#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9332 if (type->num_fields () == 1
c9317f21
TT
9333 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9334 {
9335 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9336
9337 /* Decode the field name to find the offset of the
9338 discriminant. */
9339 ULONGEST bit_offset = 0;
940da03e 9340 struct type *field_type = type->field (0).type ();
c9317f21
TT
9341 while (name[0] >= '0' && name[0] <= '9')
9342 {
9343 char *tail;
9344 unsigned long index = strtoul (name, &tail, 10);
9345 name = tail;
9346 if (*name != '$'
1f704f76 9347 || index >= field_type->num_fields ()
c9317f21
TT
9348 || (TYPE_FIELD_LOC_KIND (field_type, index)
9349 != FIELD_LOC_KIND_BITPOS))
9350 {
b98664d3 9351 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9352 "[in module %s]"),
9353 TYPE_FIELD_NAME (type, 0),
9354 objfile_name (objfile));
9355 return;
9356 }
9357 ++name;
9358
9359 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9360 field_type = field_type->field (index).type ();
c9317f21
TT
9361 }
9362
9c6a1327
TT
9363 /* Smash this type to be a structure type. We have to do this
9364 because the type has already been recorded. */
67607e24 9365 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9366 type->set_num_fields (3);
9c6a1327 9367 /* Save the field we care about. */
ceacbf6e 9368 struct field saved_field = type->field (0);
3cabb6b0
SM
9369 type->set_fields
9370 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9371
9c6a1327 9372 /* Put the discriminant at index 0. */
5d14b6e5 9373 type->field (0).set_type (field_type);
9c6a1327
TT
9374 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9375 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9376 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9377
9378 /* The order of fields doesn't really matter, so put the real
9379 field at index 1 and the data-less field at index 2. */
ceacbf6e 9380 type->field (1) = saved_field;
9c6a1327 9381 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9382 = rust_last_path_segment (type->field (1).type ()->name ());
9383 type->field (1).type ()->set_name
7d93a1e0 9384 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9385 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9386
9387 const char *dataless_name
7d93a1e0 9388 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9389 name);
9390 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9391 dataless_name);
5d14b6e5 9392 type->field (2).set_type (dataless_type);
c9317f21
TT
9393 /* NAME points into the original discriminant name, which
9394 already has the correct lifetime. */
9c6a1327 9395 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9396 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9397
9c6a1327
TT
9398 /* Indicate that this is a variant type. */
9399 static discriminant_range ranges[1] = { { 0, 0 } };
9400 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9401 }
77c2dba3
TT
9402 /* A union with a single anonymous field is probably an old-style
9403 univariant enum. */
1f704f76 9404 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9405 {
c9317f21
TT
9406 /* Smash this type to be a structure type. We have to do this
9407 because the type has already been recorded. */
67607e24 9408 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9409
940da03e 9410 struct type *field_type = type->field (0).type ();
c9317f21 9411 const char *variant_name
7d93a1e0 9412 = rust_last_path_segment (field_type->name ());
9c6a1327 9413 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9414 field_type->set_name
9415 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9416 type->name (), variant_name));
57d02173
TT
9417
9418 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9419 }
9420 else
9421 {
9422 struct type *disr_type = nullptr;
1f704f76 9423 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9424 {
940da03e 9425 disr_type = type->field (i).type ();
c9317f21 9426
78134374 9427 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9428 {
9429 /* All fields of a true enum will be structs. */
9430 return;
9431 }
1f704f76 9432 else if (disr_type->num_fields () == 0)
c9317f21
TT
9433 {
9434 /* Could be data-less variant, so keep going. */
a037790e 9435 disr_type = nullptr;
c9317f21
TT
9436 }
9437 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9438 "RUST$ENUM$DISR") != 0)
9439 {
9440 /* Not a Rust enum. */
9441 return;
9442 }
9443 else
9444 {
9445 /* Found one. */
9446 break;
9447 }
9448 }
9449
9450 /* If we got here without a discriminant, then it's probably
9451 just a union. */
9452 if (disr_type == nullptr)
9453 return;
9454
9455 /* Smash this type to be a structure type. We have to do this
9456 because the type has already been recorded. */
67607e24 9457 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9458
9c6a1327 9459 /* Make space for the discriminant field. */
ceacbf6e 9460 struct field *disr_field = &disr_type->field (0);
9c6a1327 9461 field *new_fields
1f704f76 9462 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9463 * sizeof (struct field)));
80fc5e77 9464 memcpy (new_fields + 1, type->fields (),
1f704f76 9465 type->num_fields () * sizeof (struct field));
3cabb6b0 9466 type->set_fields (new_fields);
1f704f76 9467 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9468
9469 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9470 type->field (0) = *disr_field;
9c6a1327
TT
9471 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9472 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9473
9474 /* We need a way to find the correct discriminant given a
9475 variant name. For convenience we build a map here. */
b6cdac4b 9476 struct type *enum_type = disr_field->type ();
c9317f21 9477 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9478 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9479 {
9480 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9481 {
9482 const char *name
9483 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9484 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9485 }
9486 }
9487
1f704f76 9488 int n_fields = type->num_fields ();
9c6a1327
TT
9489 /* We don't need a range entry for the discriminant, but we do
9490 need one for every other field, as there is no default
9491 variant. */
9492 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9493 discriminant_range,
9494 n_fields - 1);
c9317f21
TT
9495 /* Skip the discriminant here. */
9496 for (int i = 1; i < n_fields; ++i)
9497 {
9498 /* Find the final word in the name of this variant's type.
9499 That name can be used to look up the correct
9500 discriminant. */
9501 const char *variant_name
940da03e 9502 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9503
9504 auto iter = discriminant_map.find (variant_name);
9505 if (iter != discriminant_map.end ())
9c6a1327 9506 {
57d02173
TT
9507 ranges[i - 1].low = iter->second;
9508 ranges[i - 1].high = iter->second;
9c6a1327 9509 }
c9317f21 9510
57d02173
TT
9511 /* In Rust, each element should have the size of the
9512 enclosing enum. */
9513 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9514
bedda9ac 9515 /* Remove the discriminant field, if it exists. */
940da03e 9516 struct type *sub_type = type->field (i).type ();
1f704f76 9517 if (sub_type->num_fields () > 0)
bedda9ac 9518 {
5e33d5f4 9519 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9520 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9521 }
9c6a1327 9522 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9523 sub_type->set_name
9524 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9525 type->name (), variant_name));
c9317f21 9526 }
9c6a1327
TT
9527
9528 /* Indicate that this is a variant type. */
a1520ad8 9529 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9530 gdb::array_view<discriminant_range> (ranges,
9531 n_fields - 1));
c9317f21
TT
9532 }
9533}
9534
9535/* Rewrite some Rust unions to be structures with variants parts. */
9536
9537static void
9538rust_union_quirks (struct dwarf2_cu *cu)
9539{
9540 gdb_assert (cu->language == language_rust);
52941706 9541 for (type *type_ : cu->rust_unions)
5e22e966 9542 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9543 /* We don't need this any more. */
9544 cu->rust_unions.clear ();
c9317f21
TT
9545}
9546
8adb8487
TT
9547/* See read.h. */
9548
9549type_unit_group_unshareable *
9550dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9551{
9552 auto iter = this->m_type_units.find (tu_group);
9553 if (iter != this->m_type_units.end ())
9554 return iter->second.get ();
9555
9556 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9557 type_unit_group_unshareable *result = uniq.get ();
9558 this->m_type_units[tu_group] = std::move (uniq);
9559 return result;
9560}
9561
e286671b
TT
9562struct type *
9563dwarf2_per_objfile::get_type_for_signatured_type
9564 (signatured_type *sig_type) const
9565{
9566 auto iter = this->m_type_map.find (sig_type);
9567 if (iter == this->m_type_map.end ())
9568 return nullptr;
9569
9570 return iter->second;
9571}
9572
9573void dwarf2_per_objfile::set_type_for_signatured_type
9574 (signatured_type *sig_type, struct type *type)
9575{
9576 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9577
9578 this->m_type_map[sig_type] = type;
9579}
9580
95554aad
TT
9581/* A helper function for computing the list of all symbol tables
9582 included by PER_CU. */
9583
9584static void
4c39bc03 9585recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9586 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9587 dwarf2_per_cu_data *per_cu,
9588 dwarf2_per_objfile *per_objfile,
43f3e411 9589 struct compunit_symtab *immediate_parent)
95554aad 9590{
af758d11 9591 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9592 if (*slot != NULL)
9593 {
9594 /* This inclusion and its children have been processed. */
9595 return;
9596 }
9597
9598 *slot = per_cu;
af758d11 9599
95554aad 9600 /* Only add a CU if it has a symbol table. */
43182c09 9601 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9602 if (cust != NULL)
ec94af83
DE
9603 {
9604 /* If this is a type unit only add its symbol table if we haven't
9605 seen it yet (type unit per_cu's can share symtabs). */
9606 if (per_cu->is_debug_types)
9607 {
43f3e411 9608 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9609 if (*slot == NULL)
9610 {
43f3e411 9611 *slot = cust;
4c39bc03 9612 result->push_back (cust);
43f3e411
DE
9613 if (cust->user == NULL)
9614 cust->user = immediate_parent;
ec94af83
DE
9615 }
9616 }
9617 else
f9125b6c 9618 {
4c39bc03 9619 result->push_back (cust);
43f3e411
DE
9620 if (cust->user == NULL)
9621 cust->user = immediate_parent;
f9125b6c 9622 }
ec94af83 9623 }
95554aad 9624
ae640021
AB
9625 if (!per_cu->imported_symtabs_empty ())
9626 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9627 {
9628 recursively_compute_inclusions (result, all_children,
43182c09
SM
9629 all_type_symtabs, ptr, per_objfile,
9630 cust);
ae640021 9631 }
95554aad
TT
9632}
9633
43f3e411 9634/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9635 PER_CU. */
9636
9637static void
43182c09
SM
9638compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9639 dwarf2_per_objfile *per_objfile)
95554aad 9640{
f4dc4d17
DE
9641 gdb_assert (! per_cu->is_debug_types);
9642
ae640021 9643 if (!per_cu->imported_symtabs_empty ())
95554aad 9644 {
ae640021 9645 int len;
4c39bc03 9646 std::vector<compunit_symtab *> result_symtabs;
43182c09 9647 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9648
9649 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9650 if (cust == NULL)
95554aad
TT
9651 return;
9652
280a9412
TT
9653 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9654 htab_eq_pointer,
9655 NULL, xcalloc, xfree));
9656 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9657 htab_eq_pointer,
9658 NULL, xcalloc, xfree));
95554aad 9659
ae640021 9660 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9661 {
280a9412
TT
9662 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9663 all_type_symtabs.get (), ptr,
9664 per_objfile, cust);
ec94af83 9665 }
95554aad 9666
ec94af83 9667 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9668 len = result_symtabs.size ();
43f3e411 9669 cust->includes
f6e649dd 9670 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9671 struct compunit_symtab *, len + 1);
4c39bc03
TT
9672 memcpy (cust->includes, result_symtabs.data (),
9673 len * sizeof (compunit_symtab *));
43f3e411 9674 cust->includes[len] = NULL;
95554aad
TT
9675 }
9676}
9677
9678/* Compute the 'includes' field for the symtabs of all the CUs we just
9679 read. */
9680
9681static void
976ca316 9682process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9683{
976ca316 9684 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9685 {
9686 if (! iter->is_debug_types)
976ca316 9687 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9688 }
95554aad 9689
976ca316 9690 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9691}
9692
8fc0b21d 9693/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9694 already been loaded into memory. */
9695
9696static void
8fc0b21d 9697process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9698{
976ca316
SM
9699 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9700 struct objfile *objfile = per_objfile->objfile;
08feed99 9701 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9702 CORE_ADDR lowpc, highpc;
43f3e411 9703 struct compunit_symtab *cust;
10b3939b 9704 CORE_ADDR baseaddr;
4359dff1 9705 struct block *static_block;
3e29f34a 9706 CORE_ADDR addr;
10b3939b 9707
b3b3bada 9708 baseaddr = objfile->text_section_offset ();
10b3939b 9709
c89b44cd
TT
9710 /* Clear the list here in case something was left over. */
9711 cu->method_list.clear ();
10b3939b 9712
95554aad
TT
9713 cu->language = pretend_language;
9714 cu->language_defn = language_def (cu->language);
9715
1c47ec3e
TV
9716 dwarf2_find_base_address (cu->dies, cu);
9717
702cf3f5
AB
9718 /* Before we start reading the top-level DIE, ensure it has a valid tag
9719 type. */
9720 switch (cu->dies->tag)
9721 {
9722 case DW_TAG_compile_unit:
9723 case DW_TAG_partial_unit:
9724 case DW_TAG_type_unit:
9725 break;
9726 default:
9727 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9728 dwarf_tag_name (cu->dies->tag),
9729 sect_offset_str (cu->per_cu->sect_off),
9730 objfile_name (per_objfile->objfile));
9731 }
9732
c906108c 9733 /* Do line number decoding in read_file_scope () */
10b3939b 9734 process_die (cu->dies, cu);
c906108c 9735
a766d390
DE
9736 /* For now fudge the Go package. */
9737 if (cu->language == language_go)
9738 fixup_go_packaging (cu);
9739
5f48f8f3 9740 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9741 should be complete, and it should now be safe to compute all of the
9742 physnames. */
9743 compute_delayed_physnames (cu);
3da10d80 9744
c9317f21
TT
9745 if (cu->language == language_rust)
9746 rust_union_quirks (cu);
9747
fae299cd
DC
9748 /* Some compilers don't define a DW_AT_high_pc attribute for the
9749 compilation unit. If the DW_AT_high_pc is missing, synthesize
9750 it, by scanning the DIE's below the compilation unit. */
10b3939b 9751 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9752
3e29f34a 9753 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9754 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9755
9756 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9757 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9758 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9759 addrmap to help ensure it has an accurate map of pc values belonging to
9760 this comp unit. */
9761 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9762
c24bdb02 9763 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9764 SECT_OFF_TEXT (objfile),
9765 0);
c906108c 9766
43f3e411 9767 if (cust != NULL)
c906108c 9768 {
df15bd07 9769 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9770
8be455d7
JK
9771 /* Set symtab language to language from DW_AT_language. If the
9772 compilation is from a C file generated by language preprocessors, do
9773 not set the language if it was already deduced by start_subfile. */
43f3e411 9774 if (!(cu->language == language_c
40e3ad0e 9775 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9776 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9777
9778 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9779 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9780 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9781 there were bugs in prologue debug info, fixed later in GCC-4.5
9782 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9783
9784 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9785 needed, it would be wrong due to missing DW_AT_producer there.
9786
9787 Still one can confuse GDB by using non-standard GCC compilation
9788 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9789 */
ab260dad 9790 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9791 cust->locations_valid = 1;
e0d00bc7
JK
9792
9793 if (gcc_4_minor >= 5)
43f3e411 9794 cust->epilogue_unwind_valid = 1;
96408a79 9795
43f3e411 9796 cust->call_site_htab = cu->call_site_htab;
c906108c 9797 }
9291a0cd 9798
976ca316 9799 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9800
95554aad 9801 /* Push it for inclusion processing later. */
976ca316 9802 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9803
9804 /* Not needed any more. */
c24bdb02 9805 cu->reset_builder ();
f4dc4d17 9806}
45cfd468 9807
8fc0b21d 9808/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9809 already been loaded into memory. */
9810
9811static void
8fc0b21d 9812process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9813 enum language pretend_language)
9814{
976ca316
SM
9815 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9816 struct objfile *objfile = per_objfile->objfile;
43f3e411 9817 struct compunit_symtab *cust;
0186c6a7
DE
9818 struct signatured_type *sig_type;
9819
8fc0b21d
SM
9820 gdb_assert (cu->per_cu->is_debug_types);
9821 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9822
c89b44cd
TT
9823 /* Clear the list here in case something was left over. */
9824 cu->method_list.clear ();
f4dc4d17 9825
f4dc4d17
DE
9826 cu->language = pretend_language;
9827 cu->language_defn = language_def (cu->language);
9828
9829 /* The symbol tables are set up in read_type_unit_scope. */
9830 process_die (cu->dies, cu);
9831
9832 /* For now fudge the Go package. */
9833 if (cu->language == language_go)
9834 fixup_go_packaging (cu);
9835
5f48f8f3 9836 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9837 should be complete, and it should now be safe to compute all of the
9838 physnames. */
9839 compute_delayed_physnames (cu);
f4dc4d17 9840
c9317f21
TT
9841 if (cu->language == language_rust)
9842 rust_union_quirks (cu);
9843
f4dc4d17
DE
9844 /* TUs share symbol tables.
9845 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9846 of it with end_expandable_symtab. Otherwise, complete the addition of
9847 this TU's symbols to the existing symtab. */
8adb8487 9848 type_unit_group_unshareable *tug_unshare =
976ca316 9849 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9850 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9851 {
c24bdb02
KS
9852 buildsym_compunit *builder = cu->get_builder ();
9853 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9854 tug_unshare->compunit_symtab = cust;
f4dc4d17 9855
43f3e411 9856 if (cust != NULL)
f4dc4d17
DE
9857 {
9858 /* Set symtab language to language from DW_AT_language. If the
9859 compilation is from a C file generated by language preprocessors,
9860 do not set the language if it was already deduced by
9861 start_subfile. */
43f3e411
DE
9862 if (!(cu->language == language_c
9863 && COMPUNIT_FILETABS (cust)->language != language_c))
9864 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9865 }
9866 }
9867 else
9868 {
c24bdb02 9869 cu->get_builder ()->augment_type_symtab ();
8adb8487 9870 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9871 }
9872
976ca316 9873 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9874
9875 /* Not needed any more. */
c24bdb02 9876 cu->reset_builder ();
c906108c
SS
9877}
9878
95554aad
TT
9879/* Process an imported unit DIE. */
9880
9881static void
9882process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9883{
9884 struct attribute *attr;
9885
f4dc4d17
DE
9886 /* For now we don't handle imported units in type units. */
9887 if (cu->per_cu->is_debug_types)
9888 {
9889 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9890 " supported in type units [in module %s]"),
5e22e966 9891 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9892 }
9893
95554aad
TT
9894 attr = dwarf2_attr (die, DW_AT_import, cu);
9895 if (attr != NULL)
9896 {
0826b30a 9897 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9898 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9899 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9900 dwarf2_per_cu_data *per_cu
ab432490 9901 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9902
58990295
TV
9903 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9904 into another compilation unit, at root level. Regard this as a hint,
9905 and ignore it. */
9906 if (die->parent && die->parent->parent == NULL
9907 && per_cu->unit_type == DW_UT_compile
9908 && per_cu->lang == language_cplus)
9909 return;
9910
69d751e3 9911 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9912 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
9913 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9914 false, cu->language);
95554aad 9915
ae640021 9916 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9917 }
9918}
9919
4c8aa72d
PA
9920/* RAII object that represents a process_die scope: i.e.,
9921 starts/finishes processing a DIE. */
9922class process_die_scope
adde2bff 9923{
4c8aa72d
PA
9924public:
9925 process_die_scope (die_info *die, dwarf2_cu *cu)
9926 : m_die (die), m_cu (cu)
9927 {
9928 /* We should only be processing DIEs not already in process. */
9929 gdb_assert (!m_die->in_process);
9930 m_die->in_process = true;
9931 }
8c3cb9fa 9932
4c8aa72d
PA
9933 ~process_die_scope ()
9934 {
9935 m_die->in_process = false;
9936
9937 /* If we're done processing the DIE for the CU that owns the line
9938 header, we don't need the line header anymore. */
9939 if (m_cu->line_header_die_owner == m_die)
9940 {
9941 delete m_cu->line_header;
9942 m_cu->line_header = NULL;
9943 m_cu->line_header_die_owner = NULL;
9944 }
9945 }
9946
9947private:
9948 die_info *m_die;
9949 dwarf2_cu *m_cu;
9950};
adde2bff 9951
c906108c
SS
9952/* Process a die and its children. */
9953
9954static void
e7c27a73 9955process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9956{
4c8aa72d 9957 process_die_scope scope (die, cu);
adde2bff 9958
c906108c
SS
9959 switch (die->tag)
9960 {
9961 case DW_TAG_padding:
9962 break;
9963 case DW_TAG_compile_unit:
95554aad 9964 case DW_TAG_partial_unit:
e7c27a73 9965 read_file_scope (die, cu);
c906108c 9966 break;
348e048f
DE
9967 case DW_TAG_type_unit:
9968 read_type_unit_scope (die, cu);
9969 break;
c906108c 9970 case DW_TAG_subprogram:
0a4b0913
AB
9971 /* Nested subprograms in Fortran get a prefix. */
9972 if (cu->language == language_fortran
9973 && die->parent != NULL
9974 && die->parent->tag == DW_TAG_subprogram)
9975 cu->processing_has_namespace_info = true;
9976 /* Fall through. */
c906108c 9977 case DW_TAG_inlined_subroutine:
edb3359d 9978 read_func_scope (die, cu);
c906108c
SS
9979 break;
9980 case DW_TAG_lexical_block:
14898363
L
9981 case DW_TAG_try_block:
9982 case DW_TAG_catch_block:
e7c27a73 9983 read_lexical_block_scope (die, cu);
c906108c 9984 break;
216f72a1 9985 case DW_TAG_call_site:
96408a79
SA
9986 case DW_TAG_GNU_call_site:
9987 read_call_site_scope (die, cu);
9988 break;
c906108c 9989 case DW_TAG_class_type:
680b30c7 9990 case DW_TAG_interface_type:
c906108c
SS
9991 case DW_TAG_structure_type:
9992 case DW_TAG_union_type:
134d01f1 9993 process_structure_scope (die, cu);
c906108c
SS
9994 break;
9995 case DW_TAG_enumeration_type:
134d01f1 9996 process_enumeration_scope (die, cu);
c906108c 9997 break;
134d01f1 9998
f792889a
DJ
9999 /* These dies have a type, but processing them does not create
10000 a symbol or recurse to process the children. Therefore we can
10001 read them on-demand through read_type_die. */
c906108c 10002 case DW_TAG_subroutine_type:
72019c9c 10003 case DW_TAG_set_type:
c906108c 10004 case DW_TAG_pointer_type:
c906108c 10005 case DW_TAG_ptr_to_member_type:
c906108c 10006 case DW_TAG_reference_type:
4297a3f0 10007 case DW_TAG_rvalue_reference_type:
c906108c 10008 case DW_TAG_string_type:
c906108c 10009 break;
134d01f1 10010
d8f62e84
TT
10011 case DW_TAG_array_type:
10012 /* We only need to handle this case for Ada -- in other
10013 languages, it's normal for the compiler to emit a typedef
10014 instead. */
10015 if (cu->language != language_ada)
10016 break;
10017 /* FALLTHROUGH */
c906108c 10018 case DW_TAG_base_type:
a02abb62 10019 case DW_TAG_subrange_type:
cb249c71 10020 case DW_TAG_typedef:
134d01f1 10021 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 10022 DW_AT_name. */
f792889a 10023 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10024 break;
c906108c 10025 case DW_TAG_common_block:
e7c27a73 10026 read_common_block (die, cu);
c906108c
SS
10027 break;
10028 case DW_TAG_common_inclusion:
10029 break;
d9fa45fe 10030 case DW_TAG_namespace:
9068261f 10031 cu->processing_has_namespace_info = true;
e7c27a73 10032 read_namespace (die, cu);
d9fa45fe 10033 break;
5d7cb8df 10034 case DW_TAG_module:
9068261f 10035 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10036 read_module (die, cu);
10037 break;
d9fa45fe 10038 case DW_TAG_imported_declaration:
9068261f 10039 cu->processing_has_namespace_info = true;
74921315
KS
10040 if (read_namespace_alias (die, cu))
10041 break;
86a73007
TT
10042 /* The declaration is not a global namespace alias. */
10043 /* Fall through. */
d9fa45fe 10044 case DW_TAG_imported_module:
9068261f 10045 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10046 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10047 || cu->language != language_fortran))
b98664d3 10048 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10049 dwarf_tag_name (die->tag));
10050 read_import_statement (die, cu);
d9fa45fe 10051 break;
95554aad
TT
10052
10053 case DW_TAG_imported_unit:
10054 process_imported_unit_die (die, cu);
10055 break;
10056
71a3c369
TT
10057 case DW_TAG_variable:
10058 read_variable (die, cu);
10059 break;
10060
c906108c 10061 default:
e7c27a73 10062 new_symbol (die, NULL, cu);
c906108c
SS
10063 break;
10064 }
10065}
ca69b9e6
DE
10066\f
10067/* DWARF name computation. */
c906108c 10068
94af9270
KS
10069/* A helper function for dwarf2_compute_name which determines whether DIE
10070 needs to have the name of the scope prepended to the name listed in the
10071 die. */
10072
10073static int
10074die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10075{
1c809c68
TT
10076 struct attribute *attr;
10077
94af9270
KS
10078 switch (die->tag)
10079 {
10080 case DW_TAG_namespace:
10081 case DW_TAG_typedef:
10082 case DW_TAG_class_type:
10083 case DW_TAG_interface_type:
10084 case DW_TAG_structure_type:
10085 case DW_TAG_union_type:
10086 case DW_TAG_enumeration_type:
10087 case DW_TAG_enumerator:
10088 case DW_TAG_subprogram:
08a76f8a 10089 case DW_TAG_inlined_subroutine:
94af9270 10090 case DW_TAG_member:
74921315 10091 case DW_TAG_imported_declaration:
94af9270
KS
10092 return 1;
10093
10094 case DW_TAG_variable:
c2b0a229 10095 case DW_TAG_constant:
94af9270
KS
10096 /* We only need to prefix "globally" visible variables. These include
10097 any variable marked with DW_AT_external or any variable that
10098 lives in a namespace. [Variables in anonymous namespaces
10099 require prefixing, but they are not DW_AT_external.] */
10100
10101 if (dwarf2_attr (die, DW_AT_specification, cu))
10102 {
10103 struct dwarf2_cu *spec_cu = cu;
9a619af0 10104
94af9270
KS
10105 return die_needs_namespace (die_specification (die, &spec_cu),
10106 spec_cu);
10107 }
10108
1c809c68 10109 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10110 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10111 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10112 return 0;
10113 /* A variable in a lexical block of some kind does not need a
10114 namespace, even though in C++ such variables may be external
10115 and have a mangled name. */
10116 if (die->parent->tag == DW_TAG_lexical_block
10117 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10118 || die->parent->tag == DW_TAG_catch_block
10119 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10120 return 0;
10121 return 1;
94af9270
KS
10122
10123 default:
10124 return 0;
10125 }
10126}
10127
73b9be8b
KS
10128/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10129 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10130 defined for the given DIE. */
10131
10132static struct attribute *
10133dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10134{
10135 struct attribute *attr;
10136
10137 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10138 if (attr == NULL)
10139 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10140
10141 return attr;
10142}
10143
10144/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10145 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10146 defined for the given DIE. */
10147
10148static const char *
10149dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10150{
10151 const char *linkage_name;
10152
10153 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10154 if (linkage_name == NULL)
10155 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10156
787de330
TT
10157 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10158 See https://github.com/rust-lang/rust/issues/32925. */
10159 if (cu->language == language_rust && linkage_name != NULL
10160 && strchr (linkage_name, '{') != NULL)
10161 linkage_name = NULL;
10162
73b9be8b
KS
10163 return linkage_name;
10164}
10165
94af9270 10166/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10167 compute the physname for the object, which include a method's:
9c37b5ae 10168 - formal parameters (C++),
a766d390 10169 - receiver type (Go),
a766d390
DE
10170
10171 The term "physname" is a bit confusing.
10172 For C++, for example, it is the demangled name.
10173 For Go, for example, it's the mangled name.
94af9270 10174
af6b7be1
JB
10175 For Ada, return the DIE's linkage name rather than the fully qualified
10176 name. PHYSNAME is ignored..
10177
5989a64e 10178 The result is allocated on the objfile->per_bfd's obstack and
45940949 10179 canonicalized. */
94af9270
KS
10180
10181static const char *
15d034d0
TT
10182dwarf2_compute_name (const char *name,
10183 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10184 int physname)
10185{
5e22e966 10186 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10187
94af9270
KS
10188 if (name == NULL)
10189 name = dwarf2_name (die, cu);
10190
2ee7123e
DE
10191 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10192 but otherwise compute it by typename_concat inside GDB.
10193 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10194 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10195 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10196 will set the demangled name to the result of dwarf2_full_name, and it is
10197 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10198 if (cu->language == language_ada
10199 || (cu->language == language_fortran && physname))
10200 {
10201 /* For Ada unit, we prefer the linkage name over the name, as
10202 the former contains the exported name, which the user expects
10203 to be able to reference. Ideally, we want the user to be able
10204 to reference this entity using either natural or linkage name,
10205 but we haven't started looking at this enhancement yet. */
73b9be8b 10206 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10207
2ee7123e
DE
10208 if (linkage_name != NULL)
10209 return linkage_name;
f55ee35c
JK
10210 }
10211
94af9270
KS
10212 /* These are the only languages we know how to qualify names in. */
10213 if (name != NULL
9c37b5ae 10214 && (cu->language == language_cplus
c44af4eb
TT
10215 || cu->language == language_fortran || cu->language == language_d
10216 || cu->language == language_rust))
94af9270
KS
10217 {
10218 if (die_needs_namespace (die, cu))
10219 {
0d5cff50 10220 const char *prefix;
34a68019 10221 const char *canonical_name = NULL;
94af9270 10222
d7e74731
PA
10223 string_file buf;
10224
94af9270 10225 prefix = determine_prefix (die, cu);
94af9270
KS
10226 if (*prefix != '\0')
10227 {
43816ebc
TT
10228 gdb::unique_xmalloc_ptr<char> prefixed_name
10229 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10230
43816ebc 10231 buf.puts (prefixed_name.get ());
94af9270
KS
10232 }
10233 else
d7e74731 10234 buf.puts (name);
94af9270 10235
98bfdba5
PA
10236 /* Template parameters may be specified in the DIE's DW_AT_name, or
10237 as children with DW_TAG_template_type_param or
10238 DW_TAG_value_type_param. If the latter, add them to the name
10239 here. If the name already has template parameters, then
10240 skip this step; some versions of GCC emit both, and
10241 it is more efficient to use the pre-computed name.
10242
10243 Something to keep in mind about this process: it is very
10244 unlikely, or in some cases downright impossible, to produce
10245 something that will match the mangled name of a function.
10246 If the definition of the function has the same debug info,
10247 we should be able to match up with it anyway. But fallbacks
10248 using the minimal symbol, for instance to find a method
10249 implemented in a stripped copy of libstdc++, will not work.
10250 If we do not have debug info for the definition, we will have to
10251 match them up some other way.
10252
10253 When we do name matching there is a related problem with function
10254 templates; two instantiated function templates are allowed to
10255 differ only by their return types, which we do not add here. */
10256
10257 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10258 {
10259 struct attribute *attr;
10260 struct die_info *child;
10261 int first = 1;
2c75ccb2 10262 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10263
10264 die->building_fullname = 1;
10265
10266 for (child = die->child; child != NULL; child = child->sibling)
10267 {
10268 struct type *type;
12df843f 10269 LONGEST value;
d521ce57 10270 const gdb_byte *bytes;
98bfdba5
PA
10271 struct dwarf2_locexpr_baton *baton;
10272 struct value *v;
10273
10274 if (child->tag != DW_TAG_template_type_param
10275 && child->tag != DW_TAG_template_value_param)
10276 continue;
10277
10278 if (first)
10279 {
d7e74731 10280 buf.puts ("<");
98bfdba5
PA
10281 first = 0;
10282 }
10283 else
d7e74731 10284 buf.puts (", ");
98bfdba5
PA
10285
10286 attr = dwarf2_attr (child, DW_AT_type, cu);
10287 if (attr == NULL)
10288 {
b98664d3 10289 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10290 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10291 continue;
10292 }
10293 type = die_type (child, cu);
10294
10295 if (child->tag == DW_TAG_template_type_param)
10296 {
2c75ccb2
AB
10297 cplus_lang->print_type (type, "", &buf, -1, 0,
10298 &type_print_raw_options);
98bfdba5
PA
10299 continue;
10300 }
10301
10302 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10303 if (attr == NULL)
10304 {
b98664d3 10305 complaint (_("template parameter missing "
3e43a32a 10306 "DW_AT_const_value"));
d7e74731 10307 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10308 continue;
10309 }
10310
10311 dwarf2_const_value_attr (attr, type, name,
10312 &cu->comp_unit_obstack, cu,
10313 &value, &bytes, &baton);
10314
20ce4123 10315 if (type->has_no_signedness ())
98bfdba5
PA
10316 /* GDB prints characters as NUMBER 'CHAR'. If that's
10317 changed, this can use value_print instead. */
2c75ccb2 10318 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10319 else
10320 {
10321 struct value_print_options opts;
10322
10323 if (baton != NULL)
10324 v = dwarf2_evaluate_loc_desc (type, NULL,
10325 baton->data,
10326 baton->size,
9f47c707
SM
10327 baton->per_cu,
10328 baton->per_objfile);
98bfdba5
PA
10329 else if (bytes != NULL)
10330 {
10331 v = allocate_value (type);
10332 memcpy (value_contents_writeable (v), bytes,
10333 TYPE_LENGTH (type));
10334 }
10335 else
10336 v = value_from_longest (type, value);
10337
3e43a32a
MS
10338 /* Specify decimal so that we do not depend on
10339 the radix. */
98bfdba5
PA
10340 get_formatted_print_options (&opts, 'd');
10341 opts.raw = 1;
d7e74731 10342 value_print (v, &buf, &opts);
98bfdba5 10343 release_value (v);
98bfdba5
PA
10344 }
10345 }
10346
10347 die->building_fullname = 0;
10348
10349 if (!first)
10350 {
10351 /* Close the argument list, with a space if necessary
10352 (nested templates). */
d7e74731
PA
10353 if (!buf.empty () && buf.string ().back () == '>')
10354 buf.puts (" >");
98bfdba5 10355 else
d7e74731 10356 buf.puts (">");
98bfdba5
PA
10357 }
10358 }
10359
9c37b5ae 10360 /* For C++ methods, append formal parameter type
94af9270 10361 information, if PHYSNAME. */
6e70227d 10362
94af9270 10363 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10364 && cu->language == language_cplus)
94af9270
KS
10365 {
10366 struct type *type = read_type_die (die, cu);
10367
d7e74731 10368 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10369 &type_print_raw_options);
94af9270 10370
9c37b5ae 10371 if (cu->language == language_cplus)
94af9270 10372 {
60430eff
DJ
10373 /* Assume that an artificial first parameter is
10374 "this", but do not crash if it is not. RealView
10375 marks unnamed (and thus unused) parameters as
10376 artificial; there is no way to differentiate
10377 the two cases. */
1f704f76 10378 if (type->num_fields () > 0
94af9270 10379 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10380 && type->field (0).type ()->code () == TYPE_CODE_PTR
10381 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10382 buf.puts (" const");
94af9270
KS
10383 }
10384 }
10385
d7e74731 10386 const std::string &intermediate_name = buf.string ();
94af9270
KS
10387
10388 if (cu->language == language_cplus)
34a68019 10389 canonical_name
322a8516 10390 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10391 objfile);
34a68019
TT
10392
10393 /* If we only computed INTERMEDIATE_NAME, or if
10394 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10395 intern it. */
322a8516 10396 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10397 name = objfile->intern (intermediate_name);
34a68019
TT
10398 else
10399 name = canonical_name;
94af9270
KS
10400 }
10401 }
10402
10403 return name;
10404}
10405
0114d602
DJ
10406/* Return the fully qualified name of DIE, based on its DW_AT_name.
10407 If scope qualifiers are appropriate they will be added. The result
34a68019 10408 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10409 not have a name. NAME may either be from a previous call to
10410 dwarf2_name or NULL.
10411
9c37b5ae 10412 The output string will be canonicalized (if C++). */
0114d602
DJ
10413
10414static const char *
15d034d0 10415dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10416{
94af9270
KS
10417 return dwarf2_compute_name (name, die, cu, 0);
10418}
0114d602 10419
94af9270
KS
10420/* Construct a physname for the given DIE in CU. NAME may either be
10421 from a previous call to dwarf2_name or NULL. The result will be
10422 allocated on the objfile_objstack or NULL if the DIE does not have a
10423 name.
0114d602 10424
9c37b5ae 10425 The output string will be canonicalized (if C++). */
0114d602 10426
94af9270 10427static const char *
15d034d0 10428dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10429{
5e22e966 10430 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10431 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10432 int need_copy = 1;
10433
10434 /* In this case dwarf2_compute_name is just a shortcut not building anything
10435 on its own. */
10436 if (!die_needs_namespace (die, cu))
10437 return dwarf2_compute_name (name, die, cu, 1);
10438
906bb4c5
TT
10439 if (cu->language != language_rust)
10440 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10441
10442 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10443 has computed. */
791afaa2 10444 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10445 if (mangled != NULL)
900e11f9 10446 {
900e11f9 10447
d3355e4d 10448 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10449 {
10450 /* Do nothing (do not demangle the symbol name). */
10451 }
a766d390
DE
10452 else
10453 {
0eb876f5
JB
10454 /* Use DMGL_RET_DROP for C++ template functions to suppress
10455 their return type. It is easier for GDB users to search
10456 for such functions as `name(params)' than `long name(params)'.
10457 In such case the minimal symbol names do not match the full
10458 symbol names but for template functions there is never a need
10459 to look up their definition from their declaration so
10460 the only disadvantage remains the minimal symbol variant
10461 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10462 demangled.reset (gdb_demangle (mangled,
10463 (DMGL_PARAMS | DMGL_ANSI
10464 | DMGL_RET_DROP)));
a766d390 10465 }
900e11f9 10466 if (demangled)
791afaa2 10467 canon = demangled.get ();
900e11f9
JK
10468 else
10469 {
10470 canon = mangled;
10471 need_copy = 0;
10472 }
10473 }
10474
10475 if (canon == NULL || check_physname)
10476 {
10477 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10478
10479 if (canon != NULL && strcmp (physname, canon) != 0)
10480 {
10481 /* It may not mean a bug in GDB. The compiler could also
10482 compute DW_AT_linkage_name incorrectly. But in such case
10483 GDB would need to be bug-to-bug compatible. */
10484
b98664d3 10485 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10486 "(from linkage <%s>) - DIE at %s [in module %s]"),
10487 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10488 objfile_name (objfile));
900e11f9
JK
10489
10490 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10491 is available here - over computed PHYSNAME. It is safer
10492 against both buggy GDB and buggy compilers. */
10493
10494 retval = canon;
10495 }
10496 else
10497 {
10498 retval = physname;
10499 need_copy = 0;
10500 }
10501 }
10502 else
10503 retval = canon;
10504
10505 if (need_copy)
be1e3d3e 10506 retval = objfile->intern (retval);
900e11f9 10507
900e11f9 10508 return retval;
0114d602
DJ
10509}
10510
74921315
KS
10511/* Inspect DIE in CU for a namespace alias. If one exists, record
10512 a new symbol for it.
10513
10514 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10515
10516static int
10517read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10518{
10519 struct attribute *attr;
10520
10521 /* If the die does not have a name, this is not a namespace
10522 alias. */
10523 attr = dwarf2_attr (die, DW_AT_name, cu);
10524 if (attr != NULL)
10525 {
10526 int num;
10527 struct die_info *d = die;
10528 struct dwarf2_cu *imported_cu = cu;
10529
10530 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10531 keep inspecting DIEs until we hit the underlying import. */
10532#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10533 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10534 {
10535 attr = dwarf2_attr (d, DW_AT_import, cu);
10536 if (attr == NULL)
10537 break;
10538
10539 d = follow_die_ref (d, attr, &imported_cu);
10540 if (d->tag != DW_TAG_imported_declaration)
10541 break;
10542 }
10543
10544 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10545 {
b98664d3 10546 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10547 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10548 return 0;
10549 }
10550
10551 if (attr != NULL)
10552 {
10553 struct type *type;
0826b30a 10554 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10555
aa66c379 10556 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10557 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10558 {
10559 /* This declaration is a global namespace alias. Add
10560 a symbol for it whose type is the aliased namespace. */
10561 new_symbol (die, type, cu);
10562 return 1;
10563 }
10564 }
10565 }
10566
10567 return 0;
10568}
10569
22cee43f 10570/* Return the using directives repository (global or local?) to use in the
804d2729 10571 current context for CU.
22cee43f
PMR
10572
10573 For Ada, imported declarations can materialize renamings, which *may* be
10574 global. However it is impossible (for now?) in DWARF to distinguish
10575 "external" imported declarations and "static" ones. As all imported
10576 declarations seem to be static in all other languages, make them all CU-wide
10577 global only in Ada. */
10578
10579static struct using_direct **
804d2729 10580using_directives (struct dwarf2_cu *cu)
22cee43f 10581{
c24bdb02
KS
10582 if (cu->language == language_ada
10583 && cu->get_builder ()->outermost_context_p ())
10584 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10585 else
c24bdb02 10586 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10587}
10588
27aa8d6a
SW
10589/* Read the import statement specified by the given die and record it. */
10590
10591static void
10592read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10593{
5e22e966 10594 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10595 struct attribute *import_attr;
32019081 10596 struct die_info *imported_die, *child_die;
de4affc9 10597 struct dwarf2_cu *imported_cu;
27aa8d6a 10598 const char *imported_name;
794684b6 10599 const char *imported_name_prefix;
13387711
SW
10600 const char *canonical_name;
10601 const char *import_alias;
10602 const char *imported_declaration = NULL;
794684b6 10603 const char *import_prefix;
eb1e02fd 10604 std::vector<const char *> excludes;
13387711 10605
27aa8d6a
SW
10606 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10607 if (import_attr == NULL)
10608 {
b98664d3 10609 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10610 dwarf_tag_name (die->tag));
10611 return;
10612 }
10613
de4affc9
CC
10614 imported_cu = cu;
10615 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10616 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10617 if (imported_name == NULL)
10618 {
10619 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10620
dda83cd7
SM
10621 The import in the following code:
10622 namespace A
10623 {
10624 typedef int B;
10625 }
10626
10627 int main ()
10628 {
10629 using A::B;
10630 B b;
10631 return b;
10632 }
10633
10634 ...
10635 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10636 <52> DW_AT_decl_file : 1
10637 <53> DW_AT_decl_line : 6
10638 <54> DW_AT_import : <0x75>
10639 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10640 <59> DW_AT_name : B
10641 <5b> DW_AT_decl_file : 1
10642 <5c> DW_AT_decl_line : 2
10643 <5d> DW_AT_type : <0x6e>
10644 ...
10645 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10646 <76> DW_AT_byte_size : 4
10647 <77> DW_AT_encoding : 5 (signed)
10648
10649 imports the wrong die ( 0x75 instead of 0x58 ).
10650 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10651 return;
10652 }
10653
82856980
SW
10654 /* Figure out the local name after import. */
10655 import_alias = dwarf2_name (die, cu);
27aa8d6a 10656
794684b6
SW
10657 /* Figure out where the statement is being imported to. */
10658 import_prefix = determine_prefix (die, cu);
10659
10660 /* Figure out what the scope of the imported die is and prepend it
10661 to the name of the imported die. */
de4affc9 10662 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10663
f55ee35c
JK
10664 if (imported_die->tag != DW_TAG_namespace
10665 && imported_die->tag != DW_TAG_module)
794684b6 10666 {
13387711
SW
10667 imported_declaration = imported_name;
10668 canonical_name = imported_name_prefix;
794684b6 10669 }
13387711 10670 else if (strlen (imported_name_prefix) > 0)
12aaed36 10671 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10672 imported_name_prefix,
10673 (cu->language == language_d ? "." : "::"),
10674 imported_name, (char *) NULL);
13387711
SW
10675 else
10676 canonical_name = imported_name;
794684b6 10677
32019081
JK
10678 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10679 for (child_die = die->child; child_die && child_die->tag;
436c571c 10680 child_die = child_die->sibling)
32019081
JK
10681 {
10682 /* DWARF-4: A Fortran use statement with a “rename list” may be
10683 represented by an imported module entry with an import attribute
10684 referring to the module and owned entries corresponding to those
10685 entities that are renamed as part of being imported. */
10686
10687 if (child_die->tag != DW_TAG_imported_declaration)
10688 {
b98664d3 10689 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10690 "- DIE at %s [in module %s]"),
10691 sect_offset_str (child_die->sect_off),
10692 objfile_name (objfile));
32019081
JK
10693 continue;
10694 }
10695
10696 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10697 if (import_attr == NULL)
10698 {
b98664d3 10699 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10700 dwarf_tag_name (child_die->tag));
10701 continue;
10702 }
10703
10704 imported_cu = cu;
10705 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10706 &imported_cu);
10707 imported_name = dwarf2_name (imported_die, imported_cu);
10708 if (imported_name == NULL)
10709 {
b98664d3 10710 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10711 "imported name - DIE at %s [in module %s]"),
10712 sect_offset_str (child_die->sect_off),
10713 objfile_name (objfile));
32019081
JK
10714 continue;
10715 }
10716
eb1e02fd 10717 excludes.push_back (imported_name);
32019081
JK
10718
10719 process_die (child_die, cu);
10720 }
10721
804d2729 10722 add_using_directive (using_directives (cu),
22cee43f
PMR
10723 import_prefix,
10724 canonical_name,
10725 import_alias,
10726 imported_declaration,
10727 excludes,
10728 0,
10729 &objfile->objfile_obstack);
27aa8d6a
SW
10730}
10731
5230b05a
WT
10732/* ICC<14 does not output the required DW_AT_declaration on incomplete
10733 types, but gives them a size of zero. Starting with version 14,
10734 ICC is compatible with GCC. */
10735
9068261f 10736static bool
5230b05a
WT
10737producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10738{
10739 if (!cu->checked_producer)
10740 check_producer (cu);
10741
10742 return cu->producer_is_icc_lt_14;
10743}
10744
eb77c9df
AB
10745/* ICC generates a DW_AT_type for C void functions. This was observed on
10746 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10747 which says that void functions should not have a DW_AT_type. */
10748
10749static bool
10750producer_is_icc (struct dwarf2_cu *cu)
10751{
10752 if (!cu->checked_producer)
10753 check_producer (cu);
10754
10755 return cu->producer_is_icc;
10756}
10757
1b80a9fa
JK
10758/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10759 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10760 this, it was first present in GCC release 4.3.0. */
10761
9068261f 10762static bool
1b80a9fa
JK
10763producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10764{
10765 if (!cu->checked_producer)
10766 check_producer (cu);
10767
10768 return cu->producer_is_gcc_lt_4_3;
10769}
10770
d721ba37
PA
10771static file_and_directory
10772find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10773{
d721ba37
PA
10774 file_and_directory res;
10775
9291a0cd
TT
10776 /* Find the filename. Do not use dwarf2_name here, since the filename
10777 is not a source language identifier. */
d721ba37
PA
10778 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10779 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10780
d721ba37
PA
10781 if (res.comp_dir == NULL
10782 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10783 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10784 {
d721ba37
PA
10785 res.comp_dir_storage = ldirname (res.name);
10786 if (!res.comp_dir_storage.empty ())
10787 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10788 }
d721ba37 10789 if (res.comp_dir != NULL)
9291a0cd
TT
10790 {
10791 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10792 directory, get rid of it. */
d721ba37 10793 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10794
d721ba37
PA
10795 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10796 res.comp_dir = cp + 1;
9291a0cd
TT
10797 }
10798
d721ba37
PA
10799 if (res.name == NULL)
10800 res.name = "<unknown>";
10801
10802 return res;
9291a0cd
TT
10803}
10804
f4dc4d17
DE
10805/* Handle DW_AT_stmt_list for a compilation unit.
10806 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10807 COMP_DIR is the compilation directory. LOWPC is passed to
10808 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10809
10810static void
10811handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10812 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10813{
976ca316 10814 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10815 struct attribute *attr;
527f3840
JK
10816 struct line_header line_header_local;
10817 hashval_t line_header_local_hash;
527f3840
JK
10818 void **slot;
10819 int decode_mapping;
2ab95328 10820
f4dc4d17
DE
10821 gdb_assert (! cu->per_cu->is_debug_types);
10822
2ab95328 10823 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 10824 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
10825 return;
10826
d4df075e 10827 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
10828
10829 /* The line header hash table is only created if needed (it exists to
10830 prevent redundant reading of the line table for partial_units).
10831 If we're given a partial_unit, we'll need it. If we're given a
10832 compile_unit, then use the line header hash table if it's already
10833 created, but don't create one just yet. */
10834
976ca316 10835 if (per_objfile->line_header_hash == NULL
527f3840 10836 && die->tag == DW_TAG_partial_unit)
2ab95328 10837 {
976ca316 10838 per_objfile->line_header_hash
d15acc42
TT
10839 .reset (htab_create_alloc (127, line_header_hash_voidp,
10840 line_header_eq_voidp,
10841 free_line_header_voidp,
10842 xcalloc, xfree));
527f3840 10843 }
2ab95328 10844
9c541725 10845 line_header_local.sect_off = line_offset;
527f3840
JK
10846 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10847 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10848 if (per_objfile->line_header_hash != NULL)
527f3840 10849 {
976ca316 10850 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10851 &line_header_local,
10852 line_header_local_hash, NO_INSERT);
10853
10854 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10855 is not present in *SLOT (since if there is something in *SLOT then
10856 it will be for a partial_unit). */
10857 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10858 {
527f3840 10859 gdb_assert (*slot != NULL);
9a3c8263 10860 cu->line_header = (struct line_header *) *slot;
527f3840 10861 return;
dee91e82 10862 }
2ab95328 10863 }
527f3840
JK
10864
10865 /* dwarf_decode_line_header does not yet provide sufficient information.
10866 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10867 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10868 if (lh == NULL)
527f3840 10869 return;
4c8aa72d
PA
10870
10871 cu->line_header = lh.release ();
10872 cu->line_header_die_owner = die;
527f3840 10873
976ca316 10874 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10875 slot = NULL;
10876 else
10877 {
976ca316 10878 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10879 &line_header_local,
10880 line_header_local_hash, INSERT);
10881 gdb_assert (slot != NULL);
10882 }
10883 if (slot != NULL && *slot == NULL)
10884 {
10885 /* This newly decoded line number information unit will be owned
10886 by line_header_hash hash table. */
10887 *slot = cu->line_header;
4c8aa72d 10888 cu->line_header_die_owner = NULL;
527f3840
JK
10889 }
10890 else
10891 {
10892 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 10893 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
10894 line_header for this CU - it may happen at most once for each line
10895 number information unit. And if we're not using line_header_hash
10896 then this is what we want as well. */
10897 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10898 }
10899 decode_mapping = (die->tag != DW_TAG_partial_unit);
10900 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10901 decode_mapping);
fff8551c 10902
2ab95328
TT
10903}
10904
95554aad 10905/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10906
c906108c 10907static void
e7c27a73 10908read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10909{
976ca316
SM
10910 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10911 struct objfile *objfile = per_objfile->objfile;
08feed99 10912 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10913 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10914 CORE_ADDR highpc = ((CORE_ADDR) 0);
10915 struct attribute *attr;
c906108c 10916 struct die_info *child_die;
e142c38c 10917 CORE_ADDR baseaddr;
6e70227d 10918
380618d6 10919 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10920 baseaddr = objfile->text_section_offset ();
c906108c 10921
fae299cd 10922 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10923
10924 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10925 from finish_block. */
2acceee2 10926 if (lowpc == ((CORE_ADDR) -1))
c906108c 10927 lowpc = highpc;
3e29f34a 10928 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10929
d721ba37 10930 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10931
f4b8a18d
KW
10932 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10933 standardised yet. As a workaround for the language detection we fall
10934 back to the DW_AT_producer string. */
10935 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10936 cu->language = language_opencl;
10937
3019eac3
DE
10938 /* Similar hack for Go. */
10939 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10940 set_cu_language (DW_LANG_Go, cu);
10941
c24bdb02 10942 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10943
10944 /* Decode line number information if present. We do this before
10945 processing child DIEs, so that the line header table is available
10946 for DW_AT_decl_file. */
d721ba37 10947 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10948
10949 /* Process all dies in compilation unit. */
10950 if (die->child != NULL)
10951 {
10952 child_die = die->child;
10953 while (child_die && child_die->tag)
10954 {
10955 process_die (child_die, cu);
436c571c 10956 child_die = child_die->sibling;
3019eac3
DE
10957 }
10958 }
10959
10960 /* Decode macro information, if present. Dwarf 2 macro information
10961 refers to information in the line number info statement program
10962 header, so we can only read it if we've read the header
10963 successfully. */
0af92d60
JK
10964 attr = dwarf2_attr (die, DW_AT_macros, cu);
10965 if (attr == NULL)
10966 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 10967 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
10968 {
10969 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10970 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10971
529908cb 10972 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
10973 }
10974 else
10975 {
10976 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 10977 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 10978 {
529908cb 10979 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 10980
43f3e411 10981 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10982 }
10983 }
3019eac3
DE
10984}
10985
c24bdb02
KS
10986void
10987dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10988{
f4dc4d17
DE
10989 struct type_unit_group *tu_group;
10990 int first_time;
3019eac3 10991 struct attribute *attr;
9c541725 10992 unsigned int i;
0186c6a7 10993 struct signatured_type *sig_type;
3019eac3 10994
f4dc4d17 10995 gdb_assert (per_cu->is_debug_types);
0186c6a7 10996 sig_type = (struct signatured_type *) per_cu;
3019eac3 10997
c24bdb02 10998 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10999
f4dc4d17 11000 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11001 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11002 if (sig_type->type_unit_group == NULL)
c24bdb02 11003 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11004 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11005
11006 /* If we've already processed this stmt_list there's no real need to
11007 do it again, we could fake it and just recreate the part we need
11008 (file name,index -> symtab mapping). If data shows this optimization
11009 is useful we can do it then. */
8adb8487
TT
11010 type_unit_group_unshareable *tug_unshare
11011 = per_objfile->get_type_unit_group_unshareable (tu_group);
11012 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11013
11014 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11015 debug info. */
fff8551c 11016 line_header_up lh;
d4df075e 11017 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11018 {
d4df075e 11019 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11020 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11021 }
11022 if (lh == NULL)
11023 {
11024 if (first_time)
c24bdb02 11025 start_symtab ("", NULL, 0);
f4dc4d17
DE
11026 else
11027 {
8adb8487 11028 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11029 gdb_assert (m_builder == nullptr);
8adb8487 11030 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11031 m_builder.reset (new struct buildsym_compunit
11032 (COMPUNIT_OBJFILE (cust), "",
11033 COMPUNIT_DIRNAME (cust),
11034 compunit_language (cust),
11035 0, cust));
770479f2 11036 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11037 }
f4dc4d17 11038 return;
3019eac3
DE
11039 }
11040
c24bdb02
KS
11041 line_header = lh.release ();
11042 line_header_die_owner = die;
3019eac3 11043
f4dc4d17
DE
11044 if (first_time)
11045 {
c24bdb02 11046 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11047
1fd60fc0
DE
11048 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11049 still initializing it, and our caller (a few levels up)
11050 process_full_type_unit still needs to know if this is the first
11051 time. */
11052
8adb8487 11053 tug_unshare->symtabs
4ac93832
TT
11054 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11055 struct symtab *, line_header->file_names_size ());
3019eac3 11056
7ba99d21
AT
11057 auto &file_names = line_header->file_names ();
11058 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11059 {
7ba99d21 11060 file_entry &fe = file_names[i];
c24bdb02
KS
11061 dwarf2_start_subfile (this, fe.name,
11062 fe.include_dir (line_header));
11063 buildsym_compunit *b = get_builder ();
11064 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11065 {
4c8aa72d
PA
11066 /* NOTE: start_subfile will recognize when it's been
11067 passed a file it has already seen. So we can't
11068 assume there's a simple mapping from
11069 cu->line_header->file_names to subfiles, plus
11070 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11071 b->get_current_subfile ()->symtab
11072 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11073 }
11074
c24bdb02 11075 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11076 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11077 }
11078 }
11079 else
3019eac3 11080 {
c24bdb02 11081 gdb_assert (m_builder == nullptr);
8adb8487 11082 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11083 m_builder.reset (new struct buildsym_compunit
11084 (COMPUNIT_OBJFILE (cust), "",
11085 COMPUNIT_DIRNAME (cust),
11086 compunit_language (cust),
11087 0, cust));
770479f2 11088 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11089
7ba99d21
AT
11090 auto &file_names = line_header->file_names ();
11091 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11092 {
7ba99d21 11093 file_entry &fe = file_names[i];
8adb8487 11094 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11095 }
3019eac3
DE
11096 }
11097
f4dc4d17
DE
11098 /* The main symtab is allocated last. Type units don't have DW_AT_name
11099 so they don't have a "real" (so to speak) symtab anyway.
11100 There is later code that will assign the main symtab to all symbols
11101 that don't have one. We need to handle the case of a symbol with a
11102 missing symtab (DW_AT_decl_file) anyway. */
11103}
3019eac3 11104
f4dc4d17
DE
11105/* Process DW_TAG_type_unit.
11106 For TUs we want to skip the first top level sibling if it's not the
11107 actual type being defined by this TU. In this case the first top
11108 level sibling is there to provide context only. */
3019eac3 11109
f4dc4d17
DE
11110static void
11111read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11112{
11113 struct die_info *child_die;
3019eac3 11114
f4dc4d17
DE
11115 prepare_one_comp_unit (cu, die, language_minimal);
11116
11117 /* Initialize (or reinitialize) the machinery for building symtabs.
11118 We do this before processing child DIEs, so that the line header table
11119 is available for DW_AT_decl_file. */
c24bdb02 11120 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11121
11122 if (die->child != NULL)
11123 {
11124 child_die = die->child;
11125 while (child_die && child_die->tag)
11126 {
11127 process_die (child_die, cu);
436c571c 11128 child_die = child_die->sibling;
f4dc4d17
DE
11129 }
11130 }
3019eac3
DE
11131}
11132\f
80626a55
DE
11133/* DWO/DWP files.
11134
11135 http://gcc.gnu.org/wiki/DebugFission
11136 http://gcc.gnu.org/wiki/DebugFissionDWP
11137
11138 To simplify handling of both DWO files ("object" files with the DWARF info)
11139 and DWP files (a file with the DWOs packaged up into one file), we treat
11140 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11141
11142static hashval_t
11143hash_dwo_file (const void *item)
11144{
9a3c8263 11145 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11146 hashval_t hash;
3019eac3 11147
a2ce51a0
DE
11148 hash = htab_hash_string (dwo_file->dwo_name);
11149 if (dwo_file->comp_dir != NULL)
11150 hash += htab_hash_string (dwo_file->comp_dir);
11151 return hash;
3019eac3
DE
11152}
11153
11154static int
11155eq_dwo_file (const void *item_lhs, const void *item_rhs)
11156{
9a3c8263
SM
11157 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11158 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11159
a2ce51a0
DE
11160 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11161 return 0;
11162 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11163 return lhs->comp_dir == rhs->comp_dir;
11164 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11165}
11166
11167/* Allocate a hash table for DWO files. */
11168
51ac9db5 11169static htab_up
298e9637 11170allocate_dwo_file_hash_table ()
3019eac3 11171{
51ac9db5
SM
11172 auto delete_dwo_file = [] (void *item)
11173 {
11174 struct dwo_file *dwo_file = (struct dwo_file *) item;
11175
11176 delete dwo_file;
11177 };
11178
bc68fb19
TT
11179 return htab_up (htab_create_alloc (41,
11180 hash_dwo_file,
11181 eq_dwo_file,
11182 delete_dwo_file,
11183 xcalloc, xfree));
3019eac3
DE
11184}
11185
80626a55
DE
11186/* Lookup DWO file DWO_NAME. */
11187
11188static void **
976ca316 11189lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11190 const char *dwo_name,
11191 const char *comp_dir)
80626a55
DE
11192{
11193 struct dwo_file find_entry;
11194 void **slot;
11195
976ca316
SM
11196 if (per_objfile->per_bfd->dwo_files == NULL)
11197 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11198
0ac5b59e
DE
11199 find_entry.dwo_name = dwo_name;
11200 find_entry.comp_dir = comp_dir;
976ca316 11201 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11202 INSERT);
80626a55
DE
11203
11204 return slot;
11205}
11206
3019eac3
DE
11207static hashval_t
11208hash_dwo_unit (const void *item)
11209{
9a3c8263 11210 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11211
11212 /* This drops the top 32 bits of the id, but is ok for a hash. */
11213 return dwo_unit->signature;
11214}
11215
11216static int
11217eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11218{
9a3c8263
SM
11219 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11220 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11221
11222 /* The signature is assumed to be unique within the DWO file.
11223 So while object file CU dwo_id's always have the value zero,
11224 that's OK, assuming each object file DWO file has only one CU,
11225 and that's the rule for now. */
11226 return lhs->signature == rhs->signature;
11227}
11228
11229/* Allocate a hash table for DWO CUs,TUs.
11230 There is one of these tables for each of CUs,TUs for each DWO file. */
11231
b0b6a987 11232static htab_up
298e9637 11233allocate_dwo_unit_table ()
3019eac3
DE
11234{
11235 /* Start out with a pretty small number.
11236 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11237 return htab_up (htab_create_alloc (3,
11238 hash_dwo_unit,
11239 eq_dwo_unit,
11240 NULL, xcalloc, xfree));
3019eac3
DE
11241}
11242
19c3d4c9 11243/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11244
11245static void
19c3d4c9
DE
11246create_dwo_cu_reader (const struct die_reader_specs *reader,
11247 const gdb_byte *info_ptr,
11248 struct die_info *comp_unit_die,
c0ab21c2
TT
11249 struct dwo_file *dwo_file,
11250 struct dwo_unit *dwo_unit)
3019eac3
DE
11251{
11252 struct dwarf2_cu *cu = reader->cu;
9c541725 11253 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11254 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11255
a084a2a6
AT
11256 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11257 if (!signature.has_value ())
3019eac3 11258 {
b98664d3 11259 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11260 " its dwo_id [in module %s]"),
9d8780f0 11261 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11262 return;
11263 }
11264
3019eac3 11265 dwo_unit->dwo_file = dwo_file;
a084a2a6 11266 dwo_unit->signature = *signature;
8a0459fd 11267 dwo_unit->section = section;
9c541725 11268 dwo_unit->sect_off = sect_off;
3019eac3
DE
11269 dwo_unit->length = cu->per_cu->length;
11270
6f738b01
SM
11271 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11272 sect_offset_str (sect_off),
11273 hex_string (dwo_unit->signature));
3019eac3
DE
11274}
11275
33c5cd75 11276/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11277 Note: This function processes DWO files only, not DWP files. */
3019eac3 11278
33c5cd75 11279static void
976ca316 11280create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11281 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11282 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11283{
976ca316
SM
11284 struct objfile *objfile = per_objfile->objfile;
11285 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11286 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11287
96b79293 11288 section.read (objfile);
33c5cd75 11289 info_ptr = section.buffer;
3019eac3
DE
11290
11291 if (info_ptr == NULL)
33c5cd75 11292 return;
3019eac3 11293
6f738b01
SM
11294 dwarf_read_debug_printf ("Reading %s for %s:",
11295 section.get_name (),
11296 section.get_file_name ());
3019eac3 11297
33c5cd75 11298 end_ptr = info_ptr + section.size;
3019eac3
DE
11299 while (info_ptr < end_ptr)
11300 {
11301 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11302 struct dwo_unit read_unit {};
33c5cd75
DB
11303 struct dwo_unit *dwo_unit;
11304 void **slot;
11305 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11306
11307 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11308 per_cu.per_bfd = per_bfd;
3019eac3 11309 per_cu.is_debug_types = 0;
33c5cd75
DB
11310 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11311 per_cu.section = &section;
11312
976ca316 11313 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11314 if (!reader.dummy_p)
11315 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11316 &dwo_file, &read_unit);
33c5cd75
DB
11317 info_ptr += per_cu.length;
11318
11319 // If the unit could not be parsed, skip it.
c0ab21c2 11320 if (read_unit.dwo_file == NULL)
33c5cd75 11321 continue;
3019eac3 11322
33c5cd75 11323 if (cus_htab == NULL)
298e9637 11324 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11325
1859c670 11326 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11327 struct dwo_unit);
c0ab21c2 11328 *dwo_unit = read_unit;
b0b6a987 11329 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11330 gdb_assert (slot != NULL);
11331 if (*slot != NULL)
19c3d4c9 11332 {
33c5cd75
DB
11333 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11334 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11335
b98664d3 11336 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11337 " the entry at offset %s, signature %s"),
11338 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11339 hex_string (dwo_unit->signature));
19c3d4c9 11340 }
33c5cd75 11341 *slot = (void *)dwo_unit;
3019eac3 11342 }
3019eac3
DE
11343}
11344
80626a55
DE
11345/* DWP file .debug_{cu,tu}_index section format:
11346 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11347 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11348
11349 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11350 officially standard DWP format was published with DWARF v5 and is called
11351 Version 5. There are no versions 3 or 4.
80626a55 11352
d2415c6c
DE
11353 DWP Version 1:
11354
80626a55
DE
11355 Both index sections have the same format, and serve to map a 64-bit
11356 signature to a set of section numbers. Each section begins with a header,
11357 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11358 indexes, and a pool of 32-bit section numbers. The index sections will be
11359 aligned at 8-byte boundaries in the file.
11360
d2415c6c
DE
11361 The index section header consists of:
11362
11363 V, 32 bit version number
11364 -, 32 bits unused
11365 N, 32 bit number of compilation units or type units in the index
11366 M, 32 bit number of slots in the hash table
80626a55 11367
d2415c6c 11368 Numbers are recorded using the byte order of the application binary.
80626a55 11369
d2415c6c
DE
11370 The hash table begins at offset 16 in the section, and consists of an array
11371 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11372 order of the application binary). Unused slots in the hash table are 0.
11373 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11374
d2415c6c
DE
11375 The parallel table begins immediately after the hash table
11376 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11377 array of 32-bit indexes (using the byte order of the application binary),
11378 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11379 table contains a 32-bit index into the pool of section numbers. For unused
11380 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11381
73869dc2
DE
11382 The pool of section numbers begins immediately following the hash table
11383 (at offset 16 + 12 * M from the beginning of the section). The pool of
11384 section numbers consists of an array of 32-bit words (using the byte order
11385 of the application binary). Each item in the array is indexed starting
11386 from 0. The hash table entry provides the index of the first section
11387 number in the set. Additional section numbers in the set follow, and the
11388 set is terminated by a 0 entry (section number 0 is not used in ELF).
11389
11390 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11391 section must be the first entry in the set, and the .debug_abbrev.dwo must
11392 be the second entry. Other members of the set may follow in any order.
11393
11394 ---
11395
d2854d8d 11396 DWP Versions 2 and 5:
73869dc2 11397
d2854d8d 11398 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11399 and the entries in the index tables are now offsets into these sections.
11400 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11401 section.
11402
11403 Index Section Contents:
11404 Header
11405 Hash Table of Signatures dwp_hash_table.hash_table
11406 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11407 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11408 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11409
11410 The index section header consists of:
11411
11412 V, 32 bit version number
11413 L, 32 bit number of columns in the table of section offsets
11414 N, 32 bit number of compilation units or type units in the index
11415 M, 32 bit number of slots in the hash table
11416
11417 Numbers are recorded using the byte order of the application binary.
11418
11419 The hash table has the same format as version 1.
11420 The parallel table of indices has the same format as version 1,
11421 except that the entries are origin-1 indices into the table of sections
11422 offsets and the table of section sizes.
11423
11424 The table of offsets begins immediately following the parallel table
11425 (at offset 16 + 12 * M from the beginning of the section). The table is
11426 a two-dimensional array of 32-bit words (using the byte order of the
11427 application binary), with L columns and N+1 rows, in row-major order.
11428 Each row in the array is indexed starting from 0. The first row provides
11429 a key to the remaining rows: each column in this row provides an identifier
11430 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11431 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11432
11433 DW_SECT_INFO 1 .debug_info.dwo
11434 DW_SECT_TYPES 2 .debug_types.dwo
11435 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11436 DW_SECT_LINE 4 .debug_line.dwo
11437 DW_SECT_LOC 5 .debug_loc.dwo
11438 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11439 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11440 DW_SECT_MACRO 8 .debug_macro.dwo
11441
d2854d8d
CT
11442 The section identifiers for Version 5 are:
11443
11444 DW_SECT_INFO_V5 1 .debug_info.dwo
11445 DW_SECT_RESERVED_V5 2 --
11446 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11447 DW_SECT_LINE_V5 4 .debug_line.dwo
11448 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11449 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11450 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11451 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11452
73869dc2
DE
11453 The offsets provided by the CU and TU index sections are the base offsets
11454 for the contributions made by each CU or TU to the corresponding section
11455 in the package file. Each CU and TU header contains an abbrev_offset
11456 field, used to find the abbreviations table for that CU or TU within the
11457 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11458 be interpreted as relative to the base offset given in the index section.
11459 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11460 should be interpreted as relative to the base offset for .debug_line.dwo,
11461 and offsets into other debug sections obtained from DWARF attributes should
11462 also be interpreted as relative to the corresponding base offset.
11463
11464 The table of sizes begins immediately following the table of offsets.
11465 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11466 with L columns and N rows, in row-major order. Each row in the array is
11467 indexed starting from 1 (row 0 is shared by the two tables).
11468
11469 ---
11470
11471 Hash table lookup is handled the same in version 1 and 2:
11472
11473 We assume that N and M will not exceed 2^32 - 1.
11474 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11475
d2415c6c
DE
11476 Given a 64-bit compilation unit signature or a type signature S, an entry
11477 in the hash table is located as follows:
80626a55 11478
d2415c6c
DE
11479 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11480 the low-order k bits all set to 1.
80626a55 11481
d2415c6c 11482 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11483
d2415c6c
DE
11484 3) If the hash table entry at index H matches the signature, use that
11485 entry. If the hash table entry at index H is unused (all zeroes),
11486 terminate the search: the signature is not present in the table.
80626a55 11487
d2415c6c 11488 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11489
d2415c6c 11490 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11491 to stop at an unused slot or find the match. */
80626a55
DE
11492
11493/* Create a hash table to map DWO IDs to their CU/TU entry in
11494 .debug_{info,types}.dwo in DWP_FILE.
11495 Returns NULL if there isn't one.
11496 Note: This function processes DWP files only, not DWO files. */
11497
11498static struct dwp_hash_table *
976ca316 11499create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11500 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11501{
976ca316 11502 struct objfile *objfile = per_objfile->objfile;
400174b1 11503 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11504 const gdb_byte *index_ptr, *index_end;
80626a55 11505 struct dwarf2_section_info *index;
73869dc2 11506 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11507 struct dwp_hash_table *htab;
11508
11509 if (is_debug_types)
11510 index = &dwp_file->sections.tu_index;
11511 else
11512 index = &dwp_file->sections.cu_index;
11513
96b79293 11514 if (index->empty ())
80626a55 11515 return NULL;
96b79293 11516 index->read (objfile);
80626a55
DE
11517
11518 index_ptr = index->buffer;
11519 index_end = index_ptr + index->size;
11520
d2854d8d
CT
11521 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11522 For now it's safe to just read 4 bytes (particularly as it's difficult to
11523 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11524 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11525 index_ptr += 4;
d2854d8d 11526 if (version == 2 || version == 5)
73869dc2
DE
11527 nr_columns = read_4_bytes (dbfd, index_ptr);
11528 else
11529 nr_columns = 0;
11530 index_ptr += 4;
80626a55
DE
11531 nr_units = read_4_bytes (dbfd, index_ptr);
11532 index_ptr += 4;
11533 nr_slots = read_4_bytes (dbfd, index_ptr);
11534 index_ptr += 4;
11535
d2854d8d 11536 if (version != 1 && version != 2 && version != 5)
80626a55 11537 {
21aa081e 11538 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11539 " [in module %s]"),
21aa081e 11540 pulongest (version), dwp_file->name);
80626a55
DE
11541 }
11542 if (nr_slots != (nr_slots & -nr_slots))
11543 {
21aa081e 11544 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11545 " is not power of 2 [in module %s]"),
21aa081e 11546 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11547 }
11548
976ca316 11549 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11550 htab->version = version;
11551 htab->nr_columns = nr_columns;
80626a55
DE
11552 htab->nr_units = nr_units;
11553 htab->nr_slots = nr_slots;
11554 htab->hash_table = index_ptr;
11555 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11556
11557 /* Exit early if the table is empty. */
11558 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11559 || (version == 2 && nr_columns == 0)
11560 || (version == 5 && nr_columns == 0))
73869dc2
DE
11561 {
11562 /* All must be zero. */
11563 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11564 || (version == 2 && nr_columns != 0)
11565 || (version == 5 && nr_columns != 0))
73869dc2 11566 {
b98664d3 11567 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11568 " all zero [in modules %s]"),
11569 dwp_file->name);
11570 }
11571 return htab;
11572 }
11573
11574 if (version == 1)
11575 {
11576 htab->section_pool.v1.indices =
11577 htab->unit_table + sizeof (uint32_t) * nr_slots;
11578 /* It's harder to decide whether the section is too small in v1.
11579 V1 is deprecated anyway so we punt. */
11580 }
d2854d8d 11581 else if (version == 2)
73869dc2
DE
11582 {
11583 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11584 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11585 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11586 /* Reverse map for error checking. */
11587 int ids_seen[DW_SECT_MAX + 1];
11588 int i;
11589
11590 if (nr_columns < 2)
11591 {
11592 error (_("Dwarf Error: bad DWP hash table, too few columns"
11593 " in section table [in module %s]"),
11594 dwp_file->name);
11595 }
11596 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11597 {
11598 error (_("Dwarf Error: bad DWP hash table, too many columns"
11599 " in section table [in module %s]"),
11600 dwp_file->name);
11601 }
04fd5eed
GB
11602 memset (ids, 255, sizeof_ids);
11603 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11604 for (i = 0; i < nr_columns; ++i)
11605 {
11606 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11607
11608 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11609 {
11610 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11611 " in section table [in module %s]"),
11612 id, dwp_file->name);
11613 }
11614 if (ids_seen[id] != -1)
11615 {
11616 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11617 " id %d in section table [in module %s]"),
11618 id, dwp_file->name);
11619 }
11620 ids_seen[id] = i;
11621 ids[i] = id;
11622 }
11623 /* Must have exactly one info or types section. */
11624 if (((ids_seen[DW_SECT_INFO] != -1)
11625 + (ids_seen[DW_SECT_TYPES] != -1))
11626 != 1)
11627 {
11628 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11629 " DWO info/types section [in module %s]"),
11630 dwp_file->name);
11631 }
11632 /* Must have an abbrev section. */
11633 if (ids_seen[DW_SECT_ABBREV] == -1)
11634 {
11635 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11636 " section [in module %s]"),
11637 dwp_file->name);
11638 }
11639 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11640 htab->section_pool.v2.sizes =
11641 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11642 * nr_units * nr_columns);
11643 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11644 * nr_units * nr_columns))
11645 > index_end)
11646 {
11647 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11648 " [in module %s]"),
11649 dwp_file->name);
11650 }
11651 }
d2854d8d
CT
11652 else /* version == 5 */
11653 {
11654 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11655 int *ids = htab->section_pool.v5.section_ids;
11656 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11657 /* Reverse map for error checking. */
11658 int ids_seen[DW_SECT_MAX_V5 + 1];
11659
11660 if (nr_columns < 2)
11661 {
11662 error (_("Dwarf Error: bad DWP hash table, too few columns"
11663 " in section table [in module %s]"),
11664 dwp_file->name);
11665 }
11666 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11667 {
11668 error (_("Dwarf Error: bad DWP hash table, too many columns"
11669 " in section table [in module %s]"),
11670 dwp_file->name);
11671 }
11672 memset (ids, 255, sizeof_ids);
11673 memset (ids_seen, 255, sizeof (ids_seen));
11674 for (int i = 0; i < nr_columns; ++i)
11675 {
11676 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11677
11678 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11679 {
11680 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11681 " in section table [in module %s]"),
11682 id, dwp_file->name);
11683 }
11684 if (ids_seen[id] != -1)
11685 {
11686 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11687 " id %d in section table [in module %s]"),
11688 id, dwp_file->name);
11689 }
11690 ids_seen[id] = i;
11691 ids[i] = id;
11692 }
11693 /* Must have seen an info section. */
11694 if (ids_seen[DW_SECT_INFO_V5] == -1)
11695 {
11696 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11697 " DWO info/types section [in module %s]"),
11698 dwp_file->name);
11699 }
11700 /* Must have an abbrev section. */
11701 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11702 {
11703 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11704 " section [in module %s]"),
11705 dwp_file->name);
11706 }
11707 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11708 htab->section_pool.v5.sizes
11709 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11710 * nr_units * nr_columns);
11711 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11712 * nr_units * nr_columns))
11713 > index_end)
11714 {
11715 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11716 " [in module %s]"),
11717 dwp_file->name);
11718 }
11719 }
80626a55
DE
11720
11721 return htab;
11722}
11723
11724/* Update SECTIONS with the data from SECTP.
11725
5bb6e9dd
TT
11726 This function is like the other "locate" section routines, but in
11727 this context the sections to read comes from the DWP V1 hash table,
11728 not the full ELF section table.
80626a55
DE
11729
11730 The result is non-zero for success, or zero if an error was found. */
11731
11732static int
73869dc2
DE
11733locate_v1_virtual_dwo_sections (asection *sectp,
11734 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11735{
11736 const struct dwop_section_names *names = &dwop_section_names;
11737
fbedd546 11738 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11739 {
11740 /* There can be only one. */
049412e3 11741 if (sections->abbrev.s.section != NULL)
80626a55 11742 return 0;
049412e3 11743 sections->abbrev.s.section = sectp;
fd361982 11744 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11745 }
fbedd546
TT
11746 else if (names->info_dwo.matches (sectp->name)
11747 || names->types_dwo.matches (sectp->name))
80626a55
DE
11748 {
11749 /* There can be only one. */
049412e3 11750 if (sections->info_or_types.s.section != NULL)
80626a55 11751 return 0;
049412e3 11752 sections->info_or_types.s.section = sectp;
fd361982 11753 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11754 }
fbedd546 11755 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
11756 {
11757 /* There can be only one. */
049412e3 11758 if (sections->line.s.section != NULL)
80626a55 11759 return 0;
049412e3 11760 sections->line.s.section = sectp;
fd361982 11761 sections->line.size = bfd_section_size (sectp);
80626a55 11762 }
fbedd546 11763 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
11764 {
11765 /* There can be only one. */
049412e3 11766 if (sections->loc.s.section != NULL)
80626a55 11767 return 0;
049412e3 11768 sections->loc.s.section = sectp;
fd361982 11769 sections->loc.size = bfd_section_size (sectp);
80626a55 11770 }
fbedd546 11771 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
11772 {
11773 /* There can be only one. */
049412e3 11774 if (sections->macinfo.s.section != NULL)
80626a55 11775 return 0;
049412e3 11776 sections->macinfo.s.section = sectp;
fd361982 11777 sections->macinfo.size = bfd_section_size (sectp);
80626a55 11778 }
fbedd546 11779 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
11780 {
11781 /* There can be only one. */
049412e3 11782 if (sections->macro.s.section != NULL)
80626a55 11783 return 0;
049412e3 11784 sections->macro.s.section = sectp;
fd361982 11785 sections->macro.size = bfd_section_size (sectp);
80626a55 11786 }
fbedd546 11787 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
11788 {
11789 /* There can be only one. */
049412e3 11790 if (sections->str_offsets.s.section != NULL)
80626a55 11791 return 0;
049412e3 11792 sections->str_offsets.s.section = sectp;
fd361982 11793 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11794 }
11795 else
11796 {
11797 /* No other kind of section is valid. */
11798 return 0;
11799 }
11800
11801 return 1;
11802}
11803
73869dc2
DE
11804/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11805 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11806 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11807 This is for DWP version 1 files. */
80626a55
DE
11808
11809static struct dwo_unit *
976ca316 11810create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11811 struct dwp_file *dwp_file,
73869dc2
DE
11812 uint32_t unit_index,
11813 const char *comp_dir,
11814 ULONGEST signature, int is_debug_types)
80626a55 11815{
73869dc2
DE
11816 const struct dwp_hash_table *dwp_htab =
11817 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11818 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11819 const char *kind = is_debug_types ? "TU" : "CU";
11820 struct dwo_file *dwo_file;
11821 struct dwo_unit *dwo_unit;
73869dc2 11822 struct virtual_v1_dwo_sections sections;
80626a55 11823 void **dwo_file_slot;
80626a55
DE
11824 int i;
11825
73869dc2
DE
11826 gdb_assert (dwp_file->version == 1);
11827
6f738b01
SM
11828 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11829 kind, pulongest (unit_index), hex_string (signature),
11830 dwp_file->name);
80626a55 11831
19ac8c2e 11832 /* Fetch the sections of this DWO unit.
80626a55
DE
11833 Put a limit on the number of sections we look for so that bad data
11834 doesn't cause us to loop forever. */
11835
73869dc2 11836#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11837 (1 /* .debug_info or .debug_types */ \
11838 + 1 /* .debug_abbrev */ \
11839 + 1 /* .debug_line */ \
11840 + 1 /* .debug_loc */ \
11841 + 1 /* .debug_str_offsets */ \
19ac8c2e 11842 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11843 + 1 /* trailing zero */)
11844
11845 memset (&sections, 0, sizeof (sections));
80626a55 11846
73869dc2 11847 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11848 {
11849 asection *sectp;
11850 uint32_t section_nr =
11851 read_4_bytes (dbfd,
73869dc2
DE
11852 dwp_htab->section_pool.v1.indices
11853 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11854
11855 if (section_nr == 0)
11856 break;
11857 if (section_nr >= dwp_file->num_sections)
11858 {
11859 error (_("Dwarf Error: bad DWP hash table, section number too large"
11860 " [in module %s]"),
11861 dwp_file->name);
11862 }
11863
11864 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11865 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11866 {
11867 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11868 " [in module %s]"),
11869 dwp_file->name);
11870 }
11871 }
11872
11873 if (i < 2
96b79293
TT
11874 || sections.info_or_types.empty ()
11875 || sections.abbrev.empty ())
80626a55
DE
11876 {
11877 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11878 " [in module %s]"),
11879 dwp_file->name);
11880 }
73869dc2 11881 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11882 {
11883 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11884 " [in module %s]"),
11885 dwp_file->name);
11886 }
11887
11888 /* It's easier for the rest of the code if we fake a struct dwo_file and
11889 have dwo_unit "live" in that. At least for now.
11890
11891 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11892 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11893 file, we can combine them back into a virtual DWO file to save space
11894 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11895 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11896
791afaa2
TT
11897 std::string virtual_dwo_name =
11898 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11899 sections.abbrev.get_id (),
11900 sections.line.get_id (),
11901 sections.loc.get_id (),
11902 sections.str_offsets.get_id ());
80626a55 11903 /* Can we use an existing virtual DWO file? */
976ca316 11904 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11905 comp_dir);
80626a55
DE
11906 /* Create one if necessary. */
11907 if (*dwo_file_slot == NULL)
11908 {
6f738b01
SM
11909 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11910 virtual_dwo_name.c_str ());
11911
51ac9db5 11912 dwo_file = new struct dwo_file;
976ca316 11913 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11914 dwo_file->comp_dir = comp_dir;
80626a55
DE
11915 dwo_file->sections.abbrev = sections.abbrev;
11916 dwo_file->sections.line = sections.line;
11917 dwo_file->sections.loc = sections.loc;
11918 dwo_file->sections.macinfo = sections.macinfo;
11919 dwo_file->sections.macro = sections.macro;
11920 dwo_file->sections.str_offsets = sections.str_offsets;
11921 /* The "str" section is global to the entire DWP file. */
11922 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11923 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11924 there's no need to record it in dwo_file.
11925 Also, we can't simply record type sections in dwo_file because
11926 we record a pointer into the vector in dwo_unit. As we collect more
11927 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11928 for it, invalidating all copies of pointers into the previous
11929 contents. */
80626a55
DE
11930 *dwo_file_slot = dwo_file;
11931 }
11932 else
11933 {
6f738b01
SM
11934 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11935 virtual_dwo_name.c_str ());
11936
9a3c8263 11937 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11938 }
80626a55 11939
976ca316 11940 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11941 dwo_unit->dwo_file = dwo_file;
11942 dwo_unit->signature = signature;
8d749320 11943 dwo_unit->section =
976ca316 11944 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11945 *dwo_unit->section = sections.info_or_types;
57d63ce2 11946 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11947
11948 return dwo_unit;
11949}
11950
d2854d8d
CT
11951/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11952 simplify them. Given a pointer to the containing section SECTION, and
11953 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11954 virtual section of just that piece. */
73869dc2
DE
11955
11956static struct dwarf2_section_info
d2854d8d
CT
11957create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11958 struct dwarf2_section_info *section,
11959 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
11960{
11961 struct dwarf2_section_info result;
11962 asection *sectp;
11963
11964 gdb_assert (section != NULL);
11965 gdb_assert (!section->is_virtual);
11966
11967 memset (&result, 0, sizeof (result));
11968 result.s.containing_section = section;
dc4ccb6f 11969 result.is_virtual = true;
73869dc2
DE
11970
11971 if (size == 0)
11972 return result;
11973
96b79293 11974 sectp = section->get_bfd_section ();
73869dc2
DE
11975
11976 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11977 bounds of the real section. This is a pretty-rare event, so just
11978 flag an error (easier) instead of a warning and trying to cope. */
11979 if (sectp == NULL
fd361982 11980 || offset + size > bfd_section_size (sectp))
73869dc2 11981 {
d2854d8d 11982 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 11983 " in section %s [in module %s]"),
fd361982 11984 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 11985 objfile_name (per_objfile->objfile));
73869dc2
DE
11986 }
11987
11988 result.virtual_offset = offset;
11989 result.size = size;
11990 return result;
11991}
11992
11993/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11994 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11995 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11996 This is for DWP version 2 files. */
11997
11998static struct dwo_unit *
976ca316 11999create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12000 struct dwp_file *dwp_file,
73869dc2
DE
12001 uint32_t unit_index,
12002 const char *comp_dir,
12003 ULONGEST signature, int is_debug_types)
12004{
73869dc2
DE
12005 const struct dwp_hash_table *dwp_htab =
12006 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12007 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12008 const char *kind = is_debug_types ? "TU" : "CU";
12009 struct dwo_file *dwo_file;
12010 struct dwo_unit *dwo_unit;
d2854d8d 12011 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12012 void **dwo_file_slot;
73869dc2
DE
12013 int i;
12014
12015 gdb_assert (dwp_file->version == 2);
12016
6f738b01
SM
12017 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12018 kind, pulongest (unit_index), hex_string (signature),
12019 dwp_file->name);
73869dc2
DE
12020
12021 /* Fetch the section offsets of this DWO unit. */
12022
12023 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12024
12025 for (i = 0; i < dwp_htab->nr_columns; ++i)
12026 {
12027 uint32_t offset = read_4_bytes (dbfd,
12028 dwp_htab->section_pool.v2.offsets
12029 + (((unit_index - 1) * dwp_htab->nr_columns
12030 + i)
12031 * sizeof (uint32_t)));
12032 uint32_t size = read_4_bytes (dbfd,
12033 dwp_htab->section_pool.v2.sizes
12034 + (((unit_index - 1) * dwp_htab->nr_columns
12035 + i)
12036 * sizeof (uint32_t)));
12037
12038 switch (dwp_htab->section_pool.v2.section_ids[i])
12039 {
12040 case DW_SECT_INFO:
12041 case DW_SECT_TYPES:
12042 sections.info_or_types_offset = offset;
12043 sections.info_or_types_size = size;
12044 break;
12045 case DW_SECT_ABBREV:
12046 sections.abbrev_offset = offset;
12047 sections.abbrev_size = size;
12048 break;
12049 case DW_SECT_LINE:
12050 sections.line_offset = offset;
12051 sections.line_size = size;
12052 break;
12053 case DW_SECT_LOC:
12054 sections.loc_offset = offset;
12055 sections.loc_size = size;
12056 break;
12057 case DW_SECT_STR_OFFSETS:
12058 sections.str_offsets_offset = offset;
12059 sections.str_offsets_size = size;
12060 break;
12061 case DW_SECT_MACINFO:
12062 sections.macinfo_offset = offset;
12063 sections.macinfo_size = size;
12064 break;
12065 case DW_SECT_MACRO:
12066 sections.macro_offset = offset;
12067 sections.macro_size = size;
12068 break;
12069 }
12070 }
12071
12072 /* It's easier for the rest of the code if we fake a struct dwo_file and
12073 have dwo_unit "live" in that. At least for now.
12074
12075 The DWP file can be made up of a random collection of CUs and TUs.
12076 However, for each CU + set of TUs that came from the same original DWO
12077 file, we can combine them back into a virtual DWO file to save space
12078 (fewer struct dwo_file objects to allocate). Remember that for really
12079 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12080
791afaa2
TT
12081 std::string virtual_dwo_name =
12082 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12083 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12084 (long) (sections.line_size ? sections.line_offset : 0),
12085 (long) (sections.loc_size ? sections.loc_offset : 0),
12086 (long) (sections.str_offsets_size
12087 ? sections.str_offsets_offset : 0));
73869dc2 12088 /* Can we use an existing virtual DWO file? */
976ca316 12089 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12090 comp_dir);
73869dc2
DE
12091 /* Create one if necessary. */
12092 if (*dwo_file_slot == NULL)
12093 {
6f738b01
SM
12094 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12095 virtual_dwo_name.c_str ());
12096
51ac9db5 12097 dwo_file = new struct dwo_file;
976ca316 12098 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12099 dwo_file->comp_dir = comp_dir;
12100 dwo_file->sections.abbrev =
d2854d8d
CT
12101 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12102 sections.abbrev_offset,
12103 sections.abbrev_size);
73869dc2 12104 dwo_file->sections.line =
d2854d8d
CT
12105 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12106 sections.line_offset,
12107 sections.line_size);
73869dc2 12108 dwo_file->sections.loc =
d2854d8d
CT
12109 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12110 sections.loc_offset, sections.loc_size);
73869dc2 12111 dwo_file->sections.macinfo =
d2854d8d
CT
12112 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12113 sections.macinfo_offset,
12114 sections.macinfo_size);
73869dc2 12115 dwo_file->sections.macro =
d2854d8d
CT
12116 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12117 sections.macro_offset,
12118 sections.macro_size);
73869dc2 12119 dwo_file->sections.str_offsets =
d2854d8d
CT
12120 create_dwp_v2_or_v5_section (per_objfile,
12121 &dwp_file->sections.str_offsets,
12122 sections.str_offsets_offset,
12123 sections.str_offsets_size);
73869dc2
DE
12124 /* The "str" section is global to the entire DWP file. */
12125 dwo_file->sections.str = dwp_file->sections.str;
12126 /* The info or types section is assigned below to dwo_unit,
12127 there's no need to record it in dwo_file.
12128 Also, we can't simply record type sections in dwo_file because
12129 we record a pointer into the vector in dwo_unit. As we collect more
12130 types we'll grow the vector and eventually have to reallocate space
12131 for it, invalidating all copies of pointers into the previous
12132 contents. */
12133 *dwo_file_slot = dwo_file;
12134 }
12135 else
12136 {
6f738b01
SM
12137 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12138 virtual_dwo_name.c_str ());
12139
9a3c8263 12140 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12141 }
73869dc2 12142
976ca316 12143 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12144 dwo_unit->dwo_file = dwo_file;
12145 dwo_unit->signature = signature;
8d749320 12146 dwo_unit->section =
976ca316 12147 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12148 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12149 (per_objfile,
d2854d8d
CT
12150 is_debug_types
12151 ? &dwp_file->sections.types
12152 : &dwp_file->sections.info,
12153 sections.info_or_types_offset,
12154 sections.info_or_types_size);
12155 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12156
12157 return dwo_unit;
12158}
12159
12160/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12161 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12162 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12163 This is for DWP version 5 files. */
12164
12165static struct dwo_unit *
12166create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12167 struct dwp_file *dwp_file,
12168 uint32_t unit_index,
12169 const char *comp_dir,
12170 ULONGEST signature, int is_debug_types)
12171{
12172 const struct dwp_hash_table *dwp_htab
12173 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12174 bfd *dbfd = dwp_file->dbfd.get ();
12175 const char *kind = is_debug_types ? "TU" : "CU";
12176 struct dwo_file *dwo_file;
12177 struct dwo_unit *dwo_unit;
12178 struct virtual_v2_or_v5_dwo_sections sections {};
12179 void **dwo_file_slot;
12180
12181 gdb_assert (dwp_file->version == 5);
12182
6f738b01
SM
12183 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12184 kind, pulongest (unit_index), hex_string (signature),
12185 dwp_file->name);
d2854d8d
CT
12186
12187 /* Fetch the section offsets of this DWO unit. */
12188
12189 /* memset (&sections, 0, sizeof (sections)); */
12190
12191 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12192 {
12193 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12194 dwp_htab->section_pool.v5.offsets
12195 + (((unit_index - 1)
12196 * dwp_htab->nr_columns
12197 + i)
12198 * sizeof (uint32_t)));
d2854d8d 12199 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12200 dwp_htab->section_pool.v5.sizes
12201 + (((unit_index - 1) * dwp_htab->nr_columns
12202 + i)
12203 * sizeof (uint32_t)));
d2854d8d
CT
12204
12205 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12206 {
12207 case DW_SECT_ABBREV_V5:
12208 sections.abbrev_offset = offset;
12209 sections.abbrev_size = size;
12210 break;
12211 case DW_SECT_INFO_V5:
12212 sections.info_or_types_offset = offset;
12213 sections.info_or_types_size = size;
12214 break;
12215 case DW_SECT_LINE_V5:
12216 sections.line_offset = offset;
12217 sections.line_size = size;
12218 break;
12219 case DW_SECT_LOCLISTS_V5:
12220 sections.loclists_offset = offset;
12221 sections.loclists_size = size;
12222 break;
12223 case DW_SECT_MACRO_V5:
12224 sections.macro_offset = offset;
12225 sections.macro_size = size;
12226 break;
12227 case DW_SECT_RNGLISTS_V5:
12228 sections.rnglists_offset = offset;
12229 sections.rnglists_size = size;
12230 break;
12231 case DW_SECT_STR_OFFSETS_V5:
12232 sections.str_offsets_offset = offset;
12233 sections.str_offsets_size = size;
12234 break;
12235 case DW_SECT_RESERVED_V5:
12236 default:
12237 break;
12238 }
d2854d8d
CT
12239 }
12240
12241 /* It's easier for the rest of the code if we fake a struct dwo_file and
12242 have dwo_unit "live" in that. At least for now.
12243
12244 The DWP file can be made up of a random collection of CUs and TUs.
12245 However, for each CU + set of TUs that came from the same original DWO
12246 file, we can combine them back into a virtual DWO file to save space
12247 (fewer struct dwo_file objects to allocate). Remember that for really
12248 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12249
12250 std::string virtual_dwo_name =
12251 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12252 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12253 (long) (sections.line_size ? sections.line_offset : 0),
12254 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12255 (long) (sections.str_offsets_size
12256 ? sections.str_offsets_offset : 0),
12257 (long) (sections.macro_size ? sections.macro_offset : 0),
12258 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12259 /* Can we use an existing virtual DWO file? */
12260 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12261 virtual_dwo_name.c_str (),
12262 comp_dir);
d2854d8d
CT
12263 /* Create one if necessary. */
12264 if (*dwo_file_slot == NULL)
12265 {
6f738b01
SM
12266 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12267 virtual_dwo_name.c_str ());
12268
d2854d8d
CT
12269 dwo_file = new struct dwo_file;
12270 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12271 dwo_file->comp_dir = comp_dir;
12272 dwo_file->sections.abbrev =
dda83cd7
SM
12273 create_dwp_v2_or_v5_section (per_objfile,
12274 &dwp_file->sections.abbrev,
12275 sections.abbrev_offset,
12276 sections.abbrev_size);
d2854d8d 12277 dwo_file->sections.line =
dda83cd7
SM
12278 create_dwp_v2_or_v5_section (per_objfile,
12279 &dwp_file->sections.line,
12280 sections.line_offset, sections.line_size);
d2854d8d 12281 dwo_file->sections.macro =
dda83cd7
SM
12282 create_dwp_v2_or_v5_section (per_objfile,
12283 &dwp_file->sections.macro,
12284 sections.macro_offset,
12285 sections.macro_size);
d2854d8d 12286 dwo_file->sections.loclists =
dda83cd7
SM
12287 create_dwp_v2_or_v5_section (per_objfile,
12288 &dwp_file->sections.loclists,
12289 sections.loclists_offset,
12290 sections.loclists_size);
d2854d8d 12291 dwo_file->sections.rnglists =
dda83cd7
SM
12292 create_dwp_v2_or_v5_section (per_objfile,
12293 &dwp_file->sections.rnglists,
12294 sections.rnglists_offset,
12295 sections.rnglists_size);
d2854d8d 12296 dwo_file->sections.str_offsets =
dda83cd7
SM
12297 create_dwp_v2_or_v5_section (per_objfile,
12298 &dwp_file->sections.str_offsets,
12299 sections.str_offsets_offset,
12300 sections.str_offsets_size);
d2854d8d
CT
12301 /* The "str" section is global to the entire DWP file. */
12302 dwo_file->sections.str = dwp_file->sections.str;
12303 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12304 there's no need to record it in dwo_file.
12305 Also, we can't simply record type sections in dwo_file because
12306 we record a pointer into the vector in dwo_unit. As we collect more
12307 types we'll grow the vector and eventually have to reallocate space
12308 for it, invalidating all copies of pointers into the previous
12309 contents. */
d2854d8d
CT
12310 *dwo_file_slot = dwo_file;
12311 }
12312 else
12313 {
6f738b01
SM
12314 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12315 virtual_dwo_name.c_str ());
12316
d2854d8d
CT
12317 dwo_file = (struct dwo_file *) *dwo_file_slot;
12318 }
12319
12320 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12321 dwo_unit->dwo_file = dwo_file;
12322 dwo_unit->signature = signature;
12323 dwo_unit->section
12324 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12325 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12326 &dwp_file->sections.info,
12327 sections.info_or_types_offset,
12328 sections.info_or_types_size);
73869dc2
DE
12329 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12330
12331 return dwo_unit;
12332}
12333
57d63ce2
DE
12334/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12335 Returns NULL if the signature isn't found. */
80626a55
DE
12336
12337static struct dwo_unit *
976ca316 12338lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12339 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12340 ULONGEST signature, int is_debug_types)
80626a55 12341{
57d63ce2
DE
12342 const struct dwp_hash_table *dwp_htab =
12343 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12344 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12345 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12346 uint32_t hash = signature & mask;
12347 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12348 unsigned int i;
12349 void **slot;
870f88f7 12350 struct dwo_unit find_dwo_cu;
80626a55
DE
12351
12352 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12353 find_dwo_cu.signature = signature;
19ac8c2e 12354 slot = htab_find_slot (is_debug_types
48b490f2
TT
12355 ? dwp_file->loaded_tus.get ()
12356 : dwp_file->loaded_cus.get (),
19ac8c2e 12357 &find_dwo_cu, INSERT);
80626a55
DE
12358
12359 if (*slot != NULL)
9a3c8263 12360 return (struct dwo_unit *) *slot;
80626a55
DE
12361
12362 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12363 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12364 {
12365 ULONGEST signature_in_table;
12366
12367 signature_in_table =
57d63ce2 12368 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12369 if (signature_in_table == signature)
12370 {
57d63ce2
DE
12371 uint32_t unit_index =
12372 read_4_bytes (dbfd,
12373 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12374
73869dc2
DE
12375 if (dwp_file->version == 1)
12376 {
976ca316
SM
12377 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12378 unit_index, comp_dir,
12379 signature, is_debug_types);
73869dc2 12380 }
d2854d8d 12381 else if (dwp_file->version == 2)
73869dc2 12382 {
976ca316
SM
12383 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12384 unit_index, comp_dir,
12385 signature, is_debug_types);
73869dc2 12386 }
d2854d8d
CT
12387 else /* version == 5 */
12388 {
12389 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12390 unit_index, comp_dir,
12391 signature, is_debug_types);
12392 }
9a3c8263 12393 return (struct dwo_unit *) *slot;
80626a55
DE
12394 }
12395 if (signature_in_table == 0)
12396 return NULL;
12397 hash = (hash + hash2) & mask;
12398 }
12399
12400 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12401 " [in module %s]"),
12402 dwp_file->name);
12403}
12404
ab5088bf 12405/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12406 Open the file specified by FILE_NAME and hand it off to BFD for
12407 preliminary analysis. Return a newly initialized bfd *, which
12408 includes a canonicalized copy of FILE_NAME.
80626a55 12409 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12410 SEARCH_CWD is true if the current directory is to be searched.
12411 It will be searched before debug-file-directory.
13aaf454
DE
12412 If successful, the file is added to the bfd include table of the
12413 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12414 If unable to find/open the file, return NULL.
3019eac3
DE
12415 NOTE: This function is derived from symfile_bfd_open. */
12416
192b62ce 12417static gdb_bfd_ref_ptr
976ca316 12418try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12419 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12420{
24b9144d 12421 int desc;
9c02c129
DE
12422 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12423 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12424 to debug_file_directory. */
e0cc99a6 12425 const char *search_path;
9c02c129
DE
12426 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12427
e0cc99a6 12428 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12429 if (search_cwd)
12430 {
12431 if (*debug_file_directory != '\0')
e0cc99a6
TT
12432 {
12433 search_path_holder.reset (concat (".", dirname_separator_string,
12434 debug_file_directory,
12435 (char *) NULL));
12436 search_path = search_path_holder.get ();
12437 }
6ac97d4c 12438 else
e0cc99a6 12439 search_path = ".";
6ac97d4c 12440 }
9c02c129 12441 else
e0cc99a6 12442 search_path = debug_file_directory;
3019eac3 12443
56d467f4
CT
12444 /* Add the path for the executable binary to the list of search paths. */
12445 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12446 search_path_holder.reset (concat (objfile_dir.c_str (),
12447 dirname_separator_string,
12448 search_path, nullptr));
12449 search_path = search_path_holder.get ();
12450
24b9144d 12451 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12452 if (is_dwp)
12453 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12454
12455 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12456 desc = openp (search_path, flags, file_name,
3019eac3
DE
12457 O_RDONLY | O_BINARY, &absolute_name);
12458 if (desc < 0)
12459 return NULL;
12460
e0cc99a6
TT
12461 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12462 gnutarget, desc));
9c02c129
DE
12463 if (sym_bfd == NULL)
12464 return NULL;
192b62ce 12465 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12466
192b62ce
TT
12467 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12468 return NULL;
3019eac3 12469
13aaf454
DE
12470 /* Success. Record the bfd as having been included by the objfile's bfd.
12471 This is important because things like demangled_names_hash lives in the
12472 objfile's per_bfd space and may have references to things like symbol
12473 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12474 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12475
3019eac3
DE
12476 return sym_bfd;
12477}
12478
ab5088bf 12479/* Try to open DWO file FILE_NAME.
3019eac3
DE
12480 COMP_DIR is the DW_AT_comp_dir attribute.
12481 The result is the bfd handle of the file.
12482 If there is a problem finding or opening the file, return NULL.
12483 Upon success, the canonicalized path of the file is stored in the bfd,
12484 same as symfile_bfd_open. */
12485
192b62ce 12486static gdb_bfd_ref_ptr
976ca316 12487open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12488 const char *file_name, const char *comp_dir)
3019eac3 12489{
80626a55 12490 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12491 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12492 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12493
12494 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12495
12496 if (comp_dir != NULL)
12497 {
43816ebc
TT
12498 gdb::unique_xmalloc_ptr<char> path_to_try
12499 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12500
12501 /* NOTE: If comp_dir is a relative path, this will also try the
12502 search path, which seems useful. */
976ca316 12503 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12504 0 /*is_dwp*/,
192b62ce 12505 1 /*search_cwd*/));
3019eac3
DE
12506 if (abfd != NULL)
12507 return abfd;
12508 }
12509
12510 /* That didn't work, try debug-file-directory, which, despite its name,
12511 is a list of paths. */
12512
12513 if (*debug_file_directory == '\0')
12514 return NULL;
12515
976ca316 12516 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12517 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12518}
12519
80626a55
DE
12520/* This function is mapped across the sections and remembers the offset and
12521 size of each of the DWO debugging sections we are interested in. */
12522
12523static void
5bb6e9dd
TT
12524dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12525 dwo_sections *dwo_sections)
80626a55 12526{
80626a55
DE
12527 const struct dwop_section_names *names = &dwop_section_names;
12528
fbedd546 12529 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12530 {
049412e3 12531 dwo_sections->abbrev.s.section = sectp;
fd361982 12532 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12533 }
fbedd546 12534 else if (names->info_dwo.matches (sectp->name))
80626a55 12535 {
049412e3 12536 dwo_sections->info.s.section = sectp;
fd361982 12537 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12538 }
fbedd546 12539 else if (names->line_dwo.matches (sectp->name))
80626a55 12540 {
049412e3 12541 dwo_sections->line.s.section = sectp;
fd361982 12542 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12543 }
fbedd546 12544 else if (names->loc_dwo.matches (sectp->name))
80626a55 12545 {
049412e3 12546 dwo_sections->loc.s.section = sectp;
fd361982 12547 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12548 }
fbedd546 12549 else if (names->loclists_dwo.matches (sectp->name))
41144253 12550 {
12551 dwo_sections->loclists.s.section = sectp;
12552 dwo_sections->loclists.size = bfd_section_size (sectp);
12553 }
fbedd546 12554 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12555 {
049412e3 12556 dwo_sections->macinfo.s.section = sectp;
fd361982 12557 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12558 }
fbedd546 12559 else if (names->macro_dwo.matches (sectp->name))
80626a55 12560 {
049412e3 12561 dwo_sections->macro.s.section = sectp;
fd361982 12562 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12563 }
fbedd546 12564 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12565 {
12566 dwo_sections->rnglists.s.section = sectp;
12567 dwo_sections->rnglists.size = bfd_section_size (sectp);
12568 }
fbedd546 12569 else if (names->str_dwo.matches (sectp->name))
80626a55 12570 {
049412e3 12571 dwo_sections->str.s.section = sectp;
fd361982 12572 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12573 }
fbedd546 12574 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12575 {
049412e3 12576 dwo_sections->str_offsets.s.section = sectp;
fd361982 12577 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12578 }
fbedd546 12579 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12580 {
12581 struct dwarf2_section_info type_section;
12582
12583 memset (&type_section, 0, sizeof (type_section));
049412e3 12584 type_section.s.section = sectp;
fd361982 12585 type_section.size = bfd_section_size (sectp);
fd5866f6 12586 dwo_sections->types.push_back (type_section);
80626a55
DE
12587 }
12588}
12589
ab5088bf 12590/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12591 by PER_CU. This is for the non-DWP case.
80626a55 12592 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12593
12594static struct dwo_file *
4ab09049
SM
12595open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12596 const char *comp_dir)
3019eac3 12597{
976ca316 12598 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12599
976ca316 12600 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12601 if (dbfd == NULL)
12602 {
6f738b01
SM
12603 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12604
80626a55
DE
12605 return NULL;
12606 }
263db9a1 12607
51ac9db5 12608 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12609 dwo_file->dwo_name = dwo_name;
12610 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12611 dwo_file->dbfd = std::move (dbfd);
3019eac3 12612
5bb6e9dd
TT
12613 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12614 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12615 &dwo_file->sections);
3019eac3 12616
976ca316
SM
12617 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12618 dwo_file->cus);
3019eac3 12619
d2854d8d
CT
12620 if (cu->per_cu->dwarf_version < 5)
12621 {
12622 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12623 dwo_file->sections.types, dwo_file->tus);
12624 }
12625 else
12626 {
12627 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12628 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12629 rcuh_kind::COMPILE);
d2854d8d 12630 }
3019eac3 12631
6f738b01 12632 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12633
263db9a1 12634 return dwo_file.release ();
3019eac3
DE
12635}
12636
80626a55 12637/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12638 size of each of the DWP debugging sections common to version 1 and 2 that
12639 we are interested in. */
3019eac3 12640
80626a55 12641static void
73869dc2 12642dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12643 dwp_file *dwp_file)
3019eac3 12644{
80626a55
DE
12645 const struct dwop_section_names *names = &dwop_section_names;
12646 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12647
80626a55 12648 /* Record the ELF section number for later lookup: this is what the
73869dc2 12649 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12650 gdb_assert (elf_section_nr < dwp_file->num_sections);
12651 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12652
80626a55 12653 /* Look for specific sections that we need. */
fbedd546 12654 if (names->str_dwo.matches (sectp->name))
80626a55 12655 {
049412e3 12656 dwp_file->sections.str.s.section = sectp;
fd361982 12657 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12658 }
fbedd546 12659 else if (names->cu_index.matches (sectp->name))
80626a55 12660 {
049412e3 12661 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12662 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12663 }
fbedd546 12664 else if (names->tu_index.matches (sectp->name))
80626a55 12665 {
049412e3 12666 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12667 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12668 }
12669}
3019eac3 12670
73869dc2
DE
12671/* This function is mapped across the sections and remembers the offset and
12672 size of each of the DWP version 2 debugging sections that we are interested
12673 in. This is split into a separate function because we don't know if we
d2854d8d 12674 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12675
12676static void
12677dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12678{
9a3c8263 12679 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12680 const struct dwop_section_names *names = &dwop_section_names;
12681 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12682
12683 /* Record the ELF section number for later lookup: this is what the
12684 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12685 gdb_assert (elf_section_nr < dwp_file->num_sections);
12686 dwp_file->elf_sections[elf_section_nr] = sectp;
12687
12688 /* Look for specific sections that we need. */
fbedd546 12689 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12690 {
049412e3 12691 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12692 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12693 }
fbedd546 12694 else if (names->info_dwo.matches (sectp->name))
73869dc2 12695 {
049412e3 12696 dwp_file->sections.info.s.section = sectp;
fd361982 12697 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12698 }
fbedd546 12699 else if (names->line_dwo.matches (sectp->name))
73869dc2 12700 {
049412e3 12701 dwp_file->sections.line.s.section = sectp;
fd361982 12702 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12703 }
fbedd546 12704 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12705 {
049412e3 12706 dwp_file->sections.loc.s.section = sectp;
fd361982 12707 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12708 }
fbedd546 12709 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12710 {
049412e3 12711 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12712 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12713 }
fbedd546 12714 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12715 {
049412e3 12716 dwp_file->sections.macro.s.section = sectp;
fd361982 12717 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12718 }
fbedd546 12719 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12720 {
049412e3 12721 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12722 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12723 }
fbedd546 12724 else if (names->types_dwo.matches (sectp->name))
73869dc2 12725 {
049412e3 12726 dwp_file->sections.types.s.section = sectp;
fd361982 12727 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12728 }
12729}
12730
d2854d8d
CT
12731/* This function is mapped across the sections and remembers the offset and
12732 size of each of the DWP version 5 debugging sections that we are interested
12733 in. This is split into a separate function because we don't know if we
12734 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12735
12736static void
12737dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12738{
12739 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12740 const struct dwop_section_names *names = &dwop_section_names;
12741 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12742
12743 /* Record the ELF section number for later lookup: this is what the
12744 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12745 gdb_assert (elf_section_nr < dwp_file->num_sections);
12746 dwp_file->elf_sections[elf_section_nr] = sectp;
12747
12748 /* Look for specific sections that we need. */
fbedd546 12749 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12750 {
12751 dwp_file->sections.abbrev.s.section = sectp;
12752 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12753 }
fbedd546 12754 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12755 {
12756 dwp_file->sections.info.s.section = sectp;
12757 dwp_file->sections.info.size = bfd_section_size (sectp);
12758 }
fbedd546 12759 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12760 {
12761 dwp_file->sections.line.s.section = sectp;
12762 dwp_file->sections.line.size = bfd_section_size (sectp);
12763 }
fbedd546 12764 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
12765 {
12766 dwp_file->sections.loclists.s.section = sectp;
12767 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12768 }
fbedd546 12769 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
12770 {
12771 dwp_file->sections.macro.s.section = sectp;
12772 dwp_file->sections.macro.size = bfd_section_size (sectp);
12773 }
fbedd546 12774 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
12775 {
12776 dwp_file->sections.rnglists.s.section = sectp;
12777 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12778 }
fbedd546 12779 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
12780 {
12781 dwp_file->sections.str_offsets.s.section = sectp;
12782 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12783 }
12784}
12785
80626a55 12786/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12787
80626a55
DE
12788static hashval_t
12789hash_dwp_loaded_cutus (const void *item)
12790{
9a3c8263 12791 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12792
80626a55
DE
12793 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12794 return dwo_unit->signature;
3019eac3
DE
12795}
12796
80626a55 12797/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12798
80626a55
DE
12799static int
12800eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12801{
9a3c8263
SM
12802 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12803 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12804
80626a55
DE
12805 return dua->signature == dub->signature;
12806}
3019eac3 12807
80626a55 12808/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12809
48b490f2 12810static htab_up
298e9637 12811allocate_dwp_loaded_cutus_table ()
80626a55 12812{
48b490f2
TT
12813 return htab_up (htab_create_alloc (3,
12814 hash_dwp_loaded_cutus,
12815 eq_dwp_loaded_cutus,
12816 NULL, xcalloc, xfree));
80626a55 12817}
3019eac3 12818
ab5088bf
DE
12819/* Try to open DWP file FILE_NAME.
12820 The result is the bfd handle of the file.
12821 If there is a problem finding or opening the file, return NULL.
12822 Upon success, the canonicalized path of the file is stored in the bfd,
12823 same as symfile_bfd_open. */
12824
192b62ce 12825static gdb_bfd_ref_ptr
976ca316 12826open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12827{
976ca316 12828 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12829 1 /*is_dwp*/,
192b62ce 12830 1 /*search_cwd*/));
6ac97d4c
DE
12831 if (abfd != NULL)
12832 return abfd;
12833
12834 /* Work around upstream bug 15652.
12835 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12836 [Whether that's a "bug" is debatable, but it is getting in our way.]
12837 We have no real idea where the dwp file is, because gdb's realpath-ing
12838 of the executable's path may have discarded the needed info.
12839 [IWBN if the dwp file name was recorded in the executable, akin to
12840 .gnu_debuglink, but that doesn't exist yet.]
12841 Strip the directory from FILE_NAME and search again. */
12842 if (*debug_file_directory != '\0')
12843 {
12844 /* Don't implicitly search the current directory here.
12845 If the user wants to search "." to handle this case,
12846 it must be added to debug-file-directory. */
976ca316
SM
12847 return try_open_dwop_file (per_objfile, lbasename (file_name),
12848 1 /*is_dwp*/,
6ac97d4c
DE
12849 0 /*search_cwd*/);
12850 }
12851
12852 return NULL;
ab5088bf
DE
12853}
12854
80626a55
DE
12855/* Initialize the use of the DWP file for the current objfile.
12856 By convention the name of the DWP file is ${objfile}.dwp.
12857 The result is NULL if it can't be found. */
a766d390 12858
400174b1 12859static std::unique_ptr<struct dwp_file>
976ca316 12860open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12861{
976ca316 12862 struct objfile *objfile = per_objfile->objfile;
80626a55 12863
82bf32bc
JK
12864 /* Try to find first .dwp for the binary file before any symbolic links
12865 resolving. */
6c447423
DE
12866
12867 /* If the objfile is a debug file, find the name of the real binary
12868 file and get the name of dwp file from there. */
d721ba37 12869 std::string dwp_name;
6c447423
DE
12870 if (objfile->separate_debug_objfile_backlink != NULL)
12871 {
12872 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12873 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12874
d721ba37 12875 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12876 }
12877 else
d721ba37
PA
12878 dwp_name = objfile->original_name;
12879
12880 dwp_name += ".dwp";
80626a55 12881
976ca316 12882 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12883 if (dbfd == NULL
12884 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12885 {
12886 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12887 dwp_name = objfile_name (objfile);
12888 dwp_name += ".dwp";
976ca316 12889 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12890 }
12891
80626a55
DE
12892 if (dbfd == NULL)
12893 {
6f738b01
SM
12894 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12895
400174b1 12896 return std::unique_ptr<dwp_file> ();
3019eac3 12897 }
400174b1
TT
12898
12899 const char *name = bfd_get_filename (dbfd.get ());
12900 std::unique_ptr<struct dwp_file> dwp_file
12901 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12902
0a0f4c01 12903 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12904 dwp_file->elf_sections =
976ca316 12905 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12906 dwp_file->num_sections, asection *);
12907
5bb6e9dd
TT
12908 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12909 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12910 dwp_file.get ());
80626a55 12911
976ca316 12912 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12913
976ca316 12914 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12915
73869dc2 12916 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12917 if (dwp_file->cus && dwp_file->tus
12918 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12919 {
12920 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12921 pretty bizarre. We use pulongest here because that's the established
4d65956b 12922 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12923 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12924 " TU version %s [in DWP file %s]"),
12925 pulongest (dwp_file->cus->version),
d721ba37 12926 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12927 }
08302ed2
DE
12928
12929 if (dwp_file->cus)
12930 dwp_file->version = dwp_file->cus->version;
12931 else if (dwp_file->tus)
12932 dwp_file->version = dwp_file->tus->version;
12933 else
12934 dwp_file->version = 2;
73869dc2 12935
5bb6e9dd
TT
12936 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12937 {
12938 if (dwp_file->version == 2)
12939 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12940 dwp_file.get ());
12941 else
12942 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12943 dwp_file.get ());
12944 }
73869dc2 12945
298e9637
SM
12946 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12947 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12948
6f738b01
SM
12949 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12950 dwarf_read_debug_printf (" %s CUs, %s TUs",
12951 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12952 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12953
12954 return dwp_file;
3019eac3 12955}
c906108c 12956
ab5088bf
DE
12957/* Wrapper around open_and_init_dwp_file, only open it once. */
12958
12959static struct dwp_file *
976ca316 12960get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12961{
976ca316 12962 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12963 {
976ca316
SM
12964 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12965 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12966 }
976ca316 12967 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12968}
12969
80626a55
DE
12970/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12971 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12972 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12973 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12974 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12975
12976 This is called, for example, when wanting to read a variable with a
12977 complex location. Therefore we don't want to do file i/o for every call.
12978 Therefore we don't want to look for a DWO file on every call.
12979 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12980 then we check if we've already seen DWO_NAME, and only THEN do we check
12981 for a DWO file.
12982
1c658ad5 12983 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12984 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12985
3019eac3 12986static struct dwo_unit *
4ab09049 12987lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12988 ULONGEST signature, int is_debug_types)
3019eac3 12989{
976ca316
SM
12990 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12991 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12992 const char *kind = is_debug_types ? "TU" : "CU";
12993 void **dwo_file_slot;
3019eac3 12994 struct dwo_file *dwo_file;
80626a55 12995 struct dwp_file *dwp_file;
cb1df416 12996
6a506a2d
DE
12997 /* First see if there's a DWP file.
12998 If we have a DWP file but didn't find the DWO inside it, don't
12999 look for the original DWO file. It makes gdb behave differently
13000 depending on whether one is debugging in the build tree. */
cf2c3c16 13001
976ca316 13002 dwp_file = get_dwp_file (per_objfile);
80626a55 13003 if (dwp_file != NULL)
cf2c3c16 13004 {
80626a55
DE
13005 const struct dwp_hash_table *dwp_htab =
13006 is_debug_types ? dwp_file->tus : dwp_file->cus;
13007
13008 if (dwp_htab != NULL)
13009 {
13010 struct dwo_unit *dwo_cutu =
976ca316
SM
13011 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13012 is_debug_types);
80626a55
DE
13013
13014 if (dwo_cutu != NULL)
13015 {
6f738b01
SM
13016 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13017 kind, hex_string (signature),
13018 host_address_to_string (dwo_cutu));
13019
80626a55
DE
13020 return dwo_cutu;
13021 }
13022 }
13023 }
6a506a2d 13024 else
80626a55 13025 {
6a506a2d 13026 /* No DWP file, look for the DWO file. */
80626a55 13027
976ca316 13028 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13029 if (*dwo_file_slot == NULL)
80626a55 13030 {
6a506a2d 13031 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13032 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13033 }
6a506a2d 13034 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13035 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13036
6a506a2d 13037 if (dwo_file != NULL)
19c3d4c9 13038 {
6a506a2d
DE
13039 struct dwo_unit *dwo_cutu = NULL;
13040
13041 if (is_debug_types && dwo_file->tus)
13042 {
13043 struct dwo_unit find_dwo_cutu;
13044
13045 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13046 find_dwo_cutu.signature = signature;
9a3c8263 13047 dwo_cutu
b0b6a987
TT
13048 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13049 &find_dwo_cutu);
6a506a2d 13050 }
33c5cd75 13051 else if (!is_debug_types && dwo_file->cus)
80626a55 13052 {
33c5cd75
DB
13053 struct dwo_unit find_dwo_cutu;
13054
13055 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13056 find_dwo_cutu.signature = signature;
b0b6a987 13057 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13058 &find_dwo_cutu);
6a506a2d
DE
13059 }
13060
13061 if (dwo_cutu != NULL)
13062 {
6f738b01
SM
13063 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13064 kind, dwo_name, hex_string (signature),
13065 host_address_to_string (dwo_cutu));
13066
6a506a2d 13067 return dwo_cutu;
80626a55
DE
13068 }
13069 }
2e276125 13070 }
9cdd5dbd 13071
80626a55
DE
13072 /* We didn't find it. This could mean a dwo_id mismatch, or
13073 someone deleted the DWO/DWP file, or the search path isn't set up
13074 correctly to find the file. */
13075
6f738b01
SM
13076 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13077 kind, dwo_name, hex_string (signature));
3019eac3 13078
6656a72d
DE
13079 /* This is a warning and not a complaint because it can be caused by
13080 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13081 {
13082 /* Print the name of the DWP file if we looked there, helps the user
13083 better diagnose the problem. */
791afaa2 13084 std::string dwp_text;
43942612
DE
13085
13086 if (dwp_file != NULL)
791afaa2
TT
13087 dwp_text = string_printf (" [in DWP file %s]",
13088 lbasename (dwp_file->name));
43942612 13089
9d8780f0 13090 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13091 " [in module %s]"),
4ab09049
SM
13092 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13093 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13094 }
3019eac3 13095 return NULL;
5fb290d7
DJ
13096}
13097
80626a55
DE
13098/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13099 See lookup_dwo_cutu_unit for details. */
13100
13101static struct dwo_unit *
4ab09049 13102lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13103 ULONGEST signature)
13104{
4ab09049
SM
13105 gdb_assert (!cu->per_cu->is_debug_types);
13106
13107 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13108}
13109
13110/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13111 See lookup_dwo_cutu_unit for details. */
13112
13113static struct dwo_unit *
4ab09049 13114lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13115{
4ab09049
SM
13116 gdb_assert (cu->per_cu->is_debug_types);
13117
13118 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13119
13120 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13121}
13122
89e63ee4
DE
13123/* Traversal function for queue_and_load_all_dwo_tus. */
13124
13125static int
13126queue_and_load_dwo_tu (void **slot, void *info)
13127{
13128 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13129 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13130 ULONGEST signature = dwo_unit->signature;
d460f660 13131 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13132
13133 if (sig_type != NULL)
13134 {
13135 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13136
13137 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13138 a real dependency of PER_CU on SIG_TYPE. That is detected later
13139 while processing PER_CU. */
120ce1b5 13140 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13141 load_full_type_unit (sig_cu, cu->per_objfile);
13142 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13143 }
13144
13145 return 1;
13146}
13147
1b555f17 13148/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13149 The DWO may have the only definition of the type, though it may not be
13150 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13151 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13152
13153static void
1b555f17 13154queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13155{
13156 struct dwo_unit *dwo_unit;
13157 struct dwo_file *dwo_file;
13158
1b555f17
SM
13159 gdb_assert (cu != nullptr);
13160 gdb_assert (!cu->per_cu->is_debug_types);
13161 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13162
1b555f17 13163 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13164 gdb_assert (dwo_unit != NULL);
13165
13166 dwo_file = dwo_unit->dwo_file;
13167 if (dwo_file->tus != NULL)
1b555f17 13168 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13169}
13170
3019eac3 13171/* Read in various DIEs. */
348e048f 13172
d389af10 13173/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13174 Inherit only the children of the DW_AT_abstract_origin DIE not being
13175 already referenced by DW_AT_abstract_origin from the children of the
13176 current DIE. */
d389af10
JK
13177
13178static void
13179inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13180{
13181 struct die_info *child_die;
791afaa2 13182 sect_offset *offsetp;
d389af10
JK
13183 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13184 struct die_info *origin_die;
13185 /* Iterator of the ORIGIN_DIE children. */
13186 struct die_info *origin_child_die;
d389af10 13187 struct attribute *attr;
cd02d79d
PA
13188 struct dwarf2_cu *origin_cu;
13189 struct pending **origin_previous_list_in_scope;
d389af10
JK
13190
13191 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13192 if (!attr)
13193 return;
13194
cd02d79d
PA
13195 /* Note that following die references may follow to a die in a
13196 different cu. */
13197
13198 origin_cu = cu;
13199 origin_die = follow_die_ref (die, attr, &origin_cu);
13200
13201 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13202 symbols in. */
13203 origin_previous_list_in_scope = origin_cu->list_in_scope;
13204 origin_cu->list_in_scope = cu->list_in_scope;
13205
edb3359d
DJ
13206 if (die->tag != origin_die->tag
13207 && !(die->tag == DW_TAG_inlined_subroutine
13208 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13209 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13210 sect_offset_str (die->sect_off),
13211 sect_offset_str (origin_die->sect_off));
d389af10 13212
34dc0f95
TBA
13213 /* Find if the concrete and abstract trees are structurally the
13214 same. This is a shallow traversal and it is not bullet-proof;
13215 the compiler can trick the debugger into believing that the trees
13216 are isomorphic, whereas they actually are not. However, the
13217 likelyhood of this happening is pretty low, and a full-fledged
13218 check would be an overkill. */
13219 bool are_isomorphic = true;
13220 die_info *concrete_child = die->child;
13221 die_info *abstract_child = origin_die->child;
13222 while (concrete_child != nullptr || abstract_child != nullptr)
13223 {
13224 if (concrete_child == nullptr
13225 || abstract_child == nullptr
13226 || concrete_child->tag != abstract_child->tag)
13227 {
13228 are_isomorphic = false;
13229 break;
13230 }
13231
13232 concrete_child = concrete_child->sibling;
13233 abstract_child = abstract_child->sibling;
13234 }
13235
13236 /* Walk the origin's children in parallel to the concrete children.
13237 This helps match an origin child in case the debug info misses
13238 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13239 origin tree may not have the same tree structure as the concrete
13240 DIE, though. */
13241 die_info *corresponding_abstract_child
13242 = are_isomorphic ? origin_die->child : nullptr;
13243
791afaa2 13244 std::vector<sect_offset> offsets;
d389af10 13245
3ea89b92
PMR
13246 for (child_die = die->child;
13247 child_die && child_die->tag;
436c571c 13248 child_die = child_die->sibling)
3ea89b92
PMR
13249 {
13250 struct die_info *child_origin_die;
13251 struct dwarf2_cu *child_origin_cu;
13252
13253 /* We are trying to process concrete instance entries:
216f72a1 13254 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13255 it's not relevant to our analysis here. i.e. detecting DIEs that are
13256 present in the abstract instance but not referenced in the concrete
13257 one. */
216f72a1 13258 if (child_die->tag == DW_TAG_call_site
dda83cd7 13259 || child_die->tag == DW_TAG_GNU_call_site)
34dc0f95
TBA
13260 {
13261 if (are_isomorphic)
13262 corresponding_abstract_child
13263 = corresponding_abstract_child->sibling;
13264 continue;
13265 }
3ea89b92 13266
c38f313d
DJ
13267 /* For each CHILD_DIE, find the corresponding child of
13268 ORIGIN_DIE. If there is more than one layer of
13269 DW_AT_abstract_origin, follow them all; there shouldn't be,
13270 but GCC versions at least through 4.4 generate this (GCC PR
13271 40573). */
3ea89b92
PMR
13272 child_origin_die = child_die;
13273 child_origin_cu = cu;
c38f313d
DJ
13274 while (1)
13275 {
cd02d79d
PA
13276 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13277 child_origin_cu);
c38f313d
DJ
13278 if (attr == NULL)
13279 break;
cd02d79d
PA
13280 child_origin_die = follow_die_ref (child_origin_die, attr,
13281 &child_origin_cu);
c38f313d
DJ
13282 }
13283
34dc0f95
TBA
13284 /* If missing DW_AT_abstract_origin, try the corresponding child
13285 of the origin. Clang emits such lexical scopes. */
13286 if (child_origin_die == child_die
13287 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13288 && are_isomorphic
13289 && child_die->tag == DW_TAG_lexical_block)
13290 child_origin_die = corresponding_abstract_child;
13291
d389af10
JK
13292 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13293 counterpart may exist. */
c38f313d 13294 if (child_origin_die != child_die)
d389af10 13295 {
edb3359d
DJ
13296 if (child_die->tag != child_origin_die->tag
13297 && !(child_die->tag == DW_TAG_inlined_subroutine
13298 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13299 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13300 "different tags"),
9d8780f0
SM
13301 sect_offset_str (child_die->sect_off),
13302 sect_offset_str (child_origin_die->sect_off));
c38f313d 13303 if (child_origin_die->parent != origin_die)
b98664d3 13304 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13305 "different parents"),
9d8780f0
SM
13306 sect_offset_str (child_die->sect_off),
13307 sect_offset_str (child_origin_die->sect_off));
c38f313d 13308 else
791afaa2 13309 offsets.push_back (child_origin_die->sect_off);
d389af10 13310 }
34dc0f95
TBA
13311
13312 if (are_isomorphic)
13313 corresponding_abstract_child = corresponding_abstract_child->sibling;
d389af10 13314 }
791afaa2
TT
13315 std::sort (offsets.begin (), offsets.end ());
13316 sect_offset *offsets_end = offsets.data () + offsets.size ();
13317 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13318 if (offsetp[-1] == *offsetp)
b98664d3 13319 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13320 "to DIE %s as their abstract origin"),
13321 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13322
791afaa2 13323 offsetp = offsets.data ();
d389af10
JK
13324 origin_child_die = origin_die->child;
13325 while (origin_child_die && origin_child_die->tag)
13326 {
13327 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13328 while (offsetp < offsets_end
9c541725 13329 && *offsetp < origin_child_die->sect_off)
d389af10 13330 offsetp++;
b64f50a1 13331 if (offsetp >= offsets_end
9c541725 13332 || *offsetp > origin_child_die->sect_off)
d389af10 13333 {
adde2bff
DE
13334 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13335 Check whether we're already processing ORIGIN_CHILD_DIE.
13336 This can happen with mutually referenced abstract_origins.
13337 PR 16581. */
13338 if (!origin_child_die->in_process)
13339 process_die (origin_child_die, origin_cu);
d389af10 13340 }
436c571c 13341 origin_child_die = origin_child_die->sibling;
d389af10 13342 }
cd02d79d 13343 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13344
13345 if (cu != origin_cu)
13346 compute_delayed_physnames (origin_cu);
d389af10
JK
13347}
13348
c906108c 13349static void
e7c27a73 13350read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13351{
5e22e966 13352 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13353 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13354 struct context_stack *newobj;
c906108c
SS
13355 CORE_ADDR lowpc;
13356 CORE_ADDR highpc;
13357 struct die_info *child_die;
edb3359d 13358 struct attribute *attr, *call_line, *call_file;
15d034d0 13359 const char *name;
e142c38c 13360 CORE_ADDR baseaddr;
801e3a5b 13361 struct block *block;
edb3359d 13362 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13363 std::vector<struct symbol *> template_args;
34eaf542 13364 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13365
13366 if (inlined_func)
13367 {
13368 /* If we do not have call site information, we can't show the
13369 caller of this inlined function. That's too confusing, so
13370 only use the scope for local variables. */
13371 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13372 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13373 if (call_line == NULL || call_file == NULL)
13374 {
13375 read_lexical_block_scope (die, cu);
13376 return;
13377 }
13378 }
c906108c 13379
b3b3bada 13380 baseaddr = objfile->text_section_offset ();
e142c38c 13381
94af9270 13382 name = dwarf2_name (die, cu);
c906108c 13383
e8d05480
JB
13384 /* Ignore functions with missing or empty names. These are actually
13385 illegal according to the DWARF standard. */
13386 if (name == NULL)
13387 {
b98664d3 13388 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13389 sect_offset_str (die->sect_off));
e8d05480
JB
13390 return;
13391 }
13392
13393 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13394 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13395 <= PC_BOUNDS_INVALID)
e8d05480 13396 {
ae4d0c03 13397 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13398 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13399 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13400 "for subprogram DIE at %s"),
13401 sect_offset_str (die->sect_off));
e8d05480
JB
13402 return;
13403 }
c906108c 13404
3e29f34a
MR
13405 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13406 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13407
34eaf542
TT
13408 /* If we have any template arguments, then we must allocate a
13409 different sort of symbol. */
436c571c 13410 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13411 {
13412 if (child_die->tag == DW_TAG_template_type_param
13413 || child_die->tag == DW_TAG_template_value_param)
13414 {
8c14c3a3 13415 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13416 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13417 break;
13418 }
13419 }
13420
702cf3f5 13421 gdb_assert (cu->get_builder () != nullptr);
c24bdb02 13422 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13423 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13424 (struct symbol *) templ_func);
4c2df51b 13425
81873cc8 13426 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13427 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13428 cu->language);
13429
4cecd739
DJ
13430 /* If there is a location expression for DW_AT_frame_base, record
13431 it. */
e142c38c 13432 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13433 if (attr != nullptr)
fe978cb0 13434 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13435
63e43d3a
PMR
13436 /* If there is a location for the static link, record it. */
13437 newobj->static_link = NULL;
13438 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13439 if (attr != nullptr)
63e43d3a 13440 {
224c3ddb
SM
13441 newobj->static_link
13442 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13443 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13444 cu->addr_type ());
63e43d3a
PMR
13445 }
13446
c24bdb02 13447 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13448
639d11d3 13449 if (die->child != NULL)
c906108c 13450 {
639d11d3 13451 child_die = die->child;
c906108c
SS
13452 while (child_die && child_die->tag)
13453 {
34eaf542
TT
13454 if (child_die->tag == DW_TAG_template_type_param
13455 || child_die->tag == DW_TAG_template_value_param)
13456 {
13457 struct symbol *arg = new_symbol (child_die, NULL, cu);
13458
f1078f66 13459 if (arg != NULL)
2f4732b0 13460 template_args.push_back (arg);
34eaf542
TT
13461 }
13462 else
13463 process_die (child_die, cu);
436c571c 13464 child_die = child_die->sibling;
c906108c
SS
13465 }
13466 }
13467
d389af10
JK
13468 inherit_abstract_dies (die, cu);
13469
4a811a97
UW
13470 /* If we have a DW_AT_specification, we might need to import using
13471 directives from the context of the specification DIE. See the
13472 comment in determine_prefix. */
13473 if (cu->language == language_cplus
13474 && dwarf2_attr (die, DW_AT_specification, cu))
13475 {
13476 struct dwarf2_cu *spec_cu = cu;
13477 struct die_info *spec_die = die_specification (die, &spec_cu);
13478
13479 while (spec_die)
13480 {
13481 child_die = spec_die->child;
13482 while (child_die && child_die->tag)
13483 {
13484 if (child_die->tag == DW_TAG_imported_module)
13485 process_die (child_die, spec_cu);
436c571c 13486 child_die = child_die->sibling;
4a811a97
UW
13487 }
13488
13489 /* In some cases, GCC generates specification DIEs that
13490 themselves contain DW_AT_specification attributes. */
13491 spec_die = die_specification (spec_die, &spec_cu);
13492 }
13493 }
13494
c24bdb02 13495 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13496 /* Make a block for the local symbols within. */
c24bdb02 13497 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13498 cstk.static_link, lowpc, highpc);
801e3a5b 13499
df8a16a1 13500 /* For C++, set the block's scope. */
45280282
IB
13501 if ((cu->language == language_cplus
13502 || cu->language == language_fortran
c44af4eb
TT
13503 || cu->language == language_d
13504 || cu->language == language_rust)
4d4ec4e5 13505 && cu->processing_has_namespace_info)
195a3f6c
TT
13506 block_set_scope (block, determine_prefix (die, cu),
13507 &objfile->objfile_obstack);
df8a16a1 13508
801e3a5b
JB
13509 /* If we have address ranges, record them. */
13510 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13511
a60f3166 13512 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13513
34eaf542 13514 /* Attach template arguments to function. */
2f4732b0 13515 if (!template_args.empty ())
34eaf542
TT
13516 {
13517 gdb_assert (templ_func != NULL);
13518
2f4732b0 13519 templ_func->n_template_arguments = template_args.size ();
34eaf542 13520 templ_func->template_arguments
dda83cd7 13521 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13522 templ_func->n_template_arguments);
34eaf542 13523 memcpy (templ_func->template_arguments,
2f4732b0 13524 template_args.data (),
34eaf542 13525 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13526
13527 /* Make sure that the symtab is set on the new symbols. Even
13528 though they don't appear in this symtab directly, other parts
13529 of gdb assume that symbols do, and this is reasonably
13530 true. */
8634679f 13531 for (symbol *sym : template_args)
3e1d3d8c 13532 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13533 }
13534
208d8187
JB
13535 /* In C++, we can have functions nested inside functions (e.g., when
13536 a function declares a class that has methods). This means that
13537 when we finish processing a function scope, we may need to go
13538 back to building a containing block's symbol lists. */
c24bdb02
KS
13539 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13540 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13541
921e78cf
JB
13542 /* If we've finished processing a top-level function, subsequent
13543 symbols go in the file symbol list. */
c24bdb02
KS
13544 if (cu->get_builder ()->outermost_context_p ())
13545 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13546}
13547
13548/* Process all the DIES contained within a lexical block scope. Start
13549 a new scope, process the dies, and then close the scope. */
13550
13551static void
e7c27a73 13552read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13553{
5e22e966 13554 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13555 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13556 CORE_ADDR lowpc, highpc;
13557 struct die_info *child_die;
e142c38c
DJ
13558 CORE_ADDR baseaddr;
13559
b3b3bada 13560 baseaddr = objfile->text_section_offset ();
c906108c
SS
13561
13562 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13563 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13564 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13565 be nasty. Might be easier to properly extend generic blocks to
af34e669 13566 describe ranges. */
e385593e
JK
13567 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13568 {
13569 case PC_BOUNDS_NOT_PRESENT:
13570 /* DW_TAG_lexical_block has no attributes, process its children as if
13571 there was no wrapping by that DW_TAG_lexical_block.
13572 GCC does no longer produces such DWARF since GCC r224161. */
13573 for (child_die = die->child;
13574 child_die != NULL && child_die->tag;
436c571c 13575 child_die = child_die->sibling)
4f7bc5ed
TT
13576 {
13577 /* We might already be processing this DIE. This can happen
13578 in an unusual circumstance -- where a subroutine A
13579 appears lexically in another subroutine B, but A actually
13580 inlines B. The recursion is broken here, rather than in
13581 inherit_abstract_dies, because it seems better to simply
13582 drop concrete children here. */
13583 if (!child_die->in_process)
13584 process_die (child_die, cu);
13585 }
e385593e
JK
13586 return;
13587 case PC_BOUNDS_INVALID:
13588 return;
13589 }
3e29f34a
MR
13590 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13591 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13592
c24bdb02 13593 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13594 if (die->child != NULL)
c906108c 13595 {
639d11d3 13596 child_die = die->child;
c906108c
SS
13597 while (child_die && child_die->tag)
13598 {
e7c27a73 13599 process_die (child_die, cu);
436c571c 13600 child_die = child_die->sibling;
c906108c
SS
13601 }
13602 }
3ea89b92 13603 inherit_abstract_dies (die, cu);
c24bdb02 13604 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13605
c24bdb02
KS
13606 if (*cu->get_builder ()->get_local_symbols () != NULL
13607 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13608 {
801e3a5b 13609 struct block *block
dda83cd7 13610 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13611 cstk.start_addr, highpc);
801e3a5b
JB
13612
13613 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13614 do here, means that recording a parent's ranges entails
13615 walking across all its children's ranges as they appear in
13616 the address map, which is quadratic behavior.
13617
13618 It would be nicer to record the parent's ranges before
13619 traversing its children, simply overriding whatever you find
13620 there. But since we don't even decide whether to create a
13621 block until after we've traversed its children, that's hard
13622 to do. */
801e3a5b 13623 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13624 }
c24bdb02
KS
13625 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13626 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13627}
13628
216f72a1 13629/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13630
13631static void
13632read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13633{
5e22e966 13634 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13635 struct objfile *objfile = per_objfile->objfile;
08feed99 13636 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13637 CORE_ADDR pc, baseaddr;
13638 struct attribute *attr;
13639 struct call_site *call_site, call_site_local;
13640 void **slot;
13641 int nparams;
13642 struct die_info *child_die;
13643
b3b3bada 13644 baseaddr = objfile->text_section_offset ();
96408a79 13645
216f72a1
JK
13646 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13647 if (attr == NULL)
13648 {
13649 /* This was a pre-DWARF-5 GNU extension alias
13650 for DW_AT_call_return_pc. */
13651 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13652 }
96408a79
SA
13653 if (!attr)
13654 {
b98664d3 13655 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13656 "DIE %s [in module %s]"),
13657 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13658 return;
13659 }
95f982e5 13660 pc = attr->as_address () + baseaddr;
3e29f34a 13661 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13662
13663 if (cu->call_site_htab == NULL)
13664 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13665 NULL, &objfile->objfile_obstack,
13666 hashtab_obstack_allocate, NULL);
13667 call_site_local.pc = pc;
13668 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13669 if (*slot != NULL)
13670 {
b98664d3 13671 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13672 "DIE %s [in module %s]"),
13673 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13674 objfile_name (objfile));
96408a79
SA
13675 return;
13676 }
13677
13678 /* Count parameters at the caller. */
13679
13680 nparams = 0;
13681 for (child_die = die->child; child_die && child_die->tag;
436c571c 13682 child_die = child_die->sibling)
96408a79 13683 {
216f72a1 13684 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13685 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13686 {
b98664d3 13687 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13688 "DW_TAG_call_site child DIE %s [in module %s]"),
13689 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13690 objfile_name (objfile));
96408a79
SA
13691 continue;
13692 }
13693
13694 nparams++;
13695 }
13696
224c3ddb
SM
13697 call_site
13698 = ((struct call_site *)
13699 obstack_alloc (&objfile->objfile_obstack,
13700 sizeof (*call_site)
13701 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13702 *slot = call_site;
13703 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13704 call_site->pc = pc;
13705
216f72a1
JK
13706 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13707 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13708 {
13709 struct die_info *func_die;
13710
13711 /* Skip also over DW_TAG_inlined_subroutine. */
13712 for (func_die = die->parent;
13713 func_die && func_die->tag != DW_TAG_subprogram
13714 && func_die->tag != DW_TAG_subroutine_type;
13715 func_die = func_die->parent);
13716
216f72a1
JK
13717 /* DW_AT_call_all_calls is a superset
13718 of DW_AT_call_all_tail_calls. */
96408a79 13719 if (func_die
dda83cd7
SM
13720 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13721 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13722 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13723 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13724 {
13725 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13726 not complete. But keep CALL_SITE for look ups via call_site_htab,
13727 both the initial caller containing the real return address PC and
13728 the final callee containing the current PC of a chain of tail
13729 calls do not need to have the tail call list complete. But any
13730 function candidate for a virtual tail call frame searched via
13731 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13732 determined unambiguously. */
13733 }
13734 else
13735 {
13736 struct type *func_type = NULL;
13737
13738 if (func_die)
13739 func_type = get_die_type (func_die, cu);
13740 if (func_type != NULL)
13741 {
78134374 13742 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13743
13744 /* Enlist this call site to the function. */
13745 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13746 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13747 }
13748 else
b98664d3 13749 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13750 "DIE %s [in module %s]"),
13751 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13752 }
13753 }
13754
216f72a1
JK
13755 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13756 if (attr == NULL)
13757 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13758 if (attr == NULL)
13759 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13760 if (attr == NULL)
216f72a1
JK
13761 {
13762 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13763 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13764 }
96408a79 13765 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13766 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13767 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13768 else if (attr->form_is_block ())
96408a79
SA
13769 {
13770 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13771 struct dwarf_block *block = attr->as_block ();
96408a79 13772
8d749320 13773 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13774 dlbaton->data = block->data;
13775 dlbaton->size = block->size;
a50264ba 13776 dlbaton->per_objfile = per_objfile;
96408a79
SA
13777 dlbaton->per_cu = cu->per_cu;
13778
13779 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13780 }
cd6c91b4 13781 else if (attr->form_is_ref ())
96408a79 13782 {
96408a79
SA
13783 struct dwarf2_cu *target_cu = cu;
13784 struct die_info *target_die;
13785
ac9ec31b 13786 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13787 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13788 if (die_is_declaration (target_die, target_cu))
13789 {
7d45c7c3 13790 const char *target_physname;
9112db09
JK
13791
13792 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13793 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13794 if (target_physname == NULL)
9112db09 13795 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13796 if (target_physname == NULL)
b98664d3 13797 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13798 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13799 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13800 else
7d455152 13801 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13802 }
13803 else
13804 {
13805 CORE_ADDR lowpc;
13806
13807 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13808 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13809 <= PC_BOUNDS_INVALID)
b98664d3 13810 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13811 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13812 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13813 else
3e29f34a
MR
13814 {
13815 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13816 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13817 }
96408a79
SA
13818 }
13819 }
13820 else
b98664d3 13821 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13822 "block nor reference, for DIE %s [in module %s]"),
13823 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13824
13825 call_site->per_cu = cu->per_cu;
9f47c707 13826 call_site->per_objfile = per_objfile;
96408a79
SA
13827
13828 for (child_die = die->child;
13829 child_die && child_die->tag;
436c571c 13830 child_die = child_die->sibling)
96408a79 13831 {
96408a79 13832 struct call_site_parameter *parameter;
1788b2d3 13833 struct attribute *loc, *origin;
96408a79 13834
216f72a1 13835 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13836 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13837 {
13838 /* Already printed the complaint above. */
13839 continue;
13840 }
13841
13842 gdb_assert (call_site->parameter_count < nparams);
13843 parameter = &call_site->parameter[call_site->parameter_count];
13844
1788b2d3
JK
13845 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13846 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13847 register is contained in DW_AT_call_value. */
96408a79 13848
24c5c679 13849 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13850 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13851 if (origin == NULL)
13852 {
13853 /* This was a pre-DWARF-5 GNU extension alias
13854 for DW_AT_call_parameter. */
13855 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13856 }
cd6c91b4 13857 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13858 {
1788b2d3 13859 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13860
0826b30a 13861 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13862 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13863 {
13864 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13865 binding can be done only inside one CU. Such referenced DIE
13866 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13867 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13868 "DW_TAG_call_site child DIE %s [in module %s]"),
13869 sect_offset_str (child_die->sect_off),
9c541725 13870 objfile_name (objfile));
d76b7dbc
JK
13871 continue;
13872 }
9c541725
PA
13873 parameter->u.param_cu_off
13874 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13875 }
4fc6c0d5 13876 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13877 {
b98664d3 13878 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13879 "DW_TAG_call_site child DIE %s [in module %s]"),
13880 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13881 continue;
13882 }
24c5c679 13883 else
96408a79 13884 {
9d2246fc
TT
13885 struct dwarf_block *block = loc->as_block ();
13886
24c5c679 13887 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 13888 (block->data, &block->data[block->size]);
24c5c679
JK
13889 if (parameter->u.dwarf_reg != -1)
13890 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
13891 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13892 &block->data[block->size],
24c5c679
JK
13893 &parameter->u.fb_offset))
13894 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13895 else
13896 {
b98664d3 13897 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13898 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13899 "DW_TAG_call_site child DIE %s "
24c5c679 13900 "[in module %s]"),
9d8780f0 13901 sect_offset_str (child_die->sect_off),
9c541725 13902 objfile_name (objfile));
24c5c679
JK
13903 continue;
13904 }
96408a79
SA
13905 }
13906
216f72a1
JK
13907 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13908 if (attr == NULL)
13909 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13910 if (attr == NULL || !attr->form_is_block ())
96408a79 13911 {
b98664d3 13912 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13913 "DW_TAG_call_site child DIE %s [in module %s]"),
13914 sect_offset_str (child_die->sect_off),
9c541725 13915 objfile_name (objfile));
96408a79
SA
13916 continue;
13917 }
9d2246fc
TT
13918
13919 struct dwarf_block *block = attr->as_block ();
13920 parameter->value = block->data;
13921 parameter->value_size = block->size;
96408a79
SA
13922
13923 /* Parameters are not pre-cleared by memset above. */
13924 parameter->data_value = NULL;
13925 parameter->data_value_size = 0;
13926 call_site->parameter_count++;
13927
216f72a1
JK
13928 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13929 if (attr == NULL)
13930 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13931 if (attr != nullptr)
96408a79 13932 {
4fc6c0d5 13933 if (!attr->form_is_block ())
b98664d3 13934 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13935 "DW_TAG_call_site child DIE %s [in module %s]"),
13936 sect_offset_str (child_die->sect_off),
9c541725 13937 objfile_name (objfile));
96408a79
SA
13938 else
13939 {
9d2246fc
TT
13940 block = attr->as_block ();
13941 parameter->data_value = block->data;
13942 parameter->data_value_size = block->size;
96408a79
SA
13943 }
13944 }
13945 }
13946}
13947
71a3c369
TT
13948/* Helper function for read_variable. If DIE represents a virtual
13949 table, then return the type of the concrete object that is
13950 associated with the virtual table. Otherwise, return NULL. */
13951
13952static struct type *
13953rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13954{
13955 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13956 if (attr == NULL)
13957 return NULL;
13958
13959 /* Find the type DIE. */
13960 struct die_info *type_die = NULL;
13961 struct dwarf2_cu *type_cu = cu;
13962
cd6c91b4 13963 if (attr->form_is_ref ())
71a3c369
TT
13964 type_die = follow_die_ref (die, attr, &type_cu);
13965 if (type_die == NULL)
13966 return NULL;
13967
13968 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13969 return NULL;
13970 return die_containing_type (type_die, type_cu);
13971}
13972
13973/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13974
13975static void
13976read_variable (struct die_info *die, struct dwarf2_cu *cu)
13977{
13978 struct rust_vtable_symbol *storage = NULL;
13979
13980 if (cu->language == language_rust)
13981 {
13982 struct type *containing_type = rust_containing_type (die, cu);
13983
13984 if (containing_type != NULL)
13985 {
5e22e966 13986 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13987
8c14c3a3 13988 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13989 storage->concrete_type = containing_type;
cf724bc9 13990 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13991 }
13992 }
13993
e4a62c65
TV
13994 struct symbol *res = new_symbol (die, NULL, cu, storage);
13995 struct attribute *abstract_origin
13996 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13997 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13998 if (res == NULL && loc && abstract_origin)
13999 {
14000 /* We have a variable without a name, but with a location and an abstract
14001 origin. This may be a concrete instance of an abstract variable
14002 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14003 later. */
14004 struct dwarf2_cu *origin_cu = cu;
14005 struct die_info *origin_die
14006 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14007 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14008 per_objfile->per_bfd->abstract_to_concrete
14009 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14010 }
71a3c369
TT
14011}
14012
43988095
JK
14013/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14014 reading .debug_rnglists.
14015 Callback's type should be:
14016 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14017 Return true if the attributes are present and valid, otherwise,
14018 return false. */
14019
14020template <typename Callback>
14021static bool
14022dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14023 dwarf_tag tag, Callback &&callback)
43988095 14024{
976ca316
SM
14025 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14026 struct objfile *objfile = per_objfile->objfile;
43988095 14027 bfd *obfd = objfile->obfd;
43988095 14028 /* Base address selection entry. */
2b24b6e4 14029 gdb::optional<CORE_ADDR> base;
43988095 14030 const gdb_byte *buffer;
43988095
JK
14031 CORE_ADDR baseaddr;
14032 bool overflow = false;
d0ce17d8
CT
14033 ULONGEST addr_index;
14034 struct dwarf2_section_info *rnglists_section;
43988095 14035
43988095 14036 base = cu->base_address;
d0ce17d8
CT
14037 rnglists_section = cu_debug_rnglists_section (cu, tag);
14038 rnglists_section->read (objfile);
43988095 14039
d0ce17d8 14040 if (offset >= rnglists_section->size)
43988095 14041 {
b98664d3 14042 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14043 offset);
14044 return false;
14045 }
d0ce17d8 14046 buffer = rnglists_section->buffer + offset;
43988095 14047
b3b3bada 14048 baseaddr = objfile->text_section_offset ();
43988095
JK
14049
14050 while (1)
14051 {
7814882a
JK
14052 /* Initialize it due to a false compiler warning. */
14053 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14054 const gdb_byte *buf_end = (rnglists_section->buffer
14055 + rnglists_section->size);
43988095
JK
14056 unsigned int bytes_read;
14057
14058 if (buffer == buf_end)
14059 {
14060 overflow = true;
14061 break;
14062 }
14063 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14064 switch (rlet)
14065 {
14066 case DW_RLE_end_of_list:
14067 break;
14068 case DW_RLE_base_address:
14069 if (buffer + cu->header.addr_size > buf_end)
14070 {
14071 overflow = true;
14072 break;
14073 }
c8a7a66f 14074 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14075 buffer += bytes_read;
14076 break;
dda83cd7
SM
14077 case DW_RLE_base_addressx:
14078 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14079 buffer += bytes_read;
14080 base = read_addr_index (cu, addr_index);
14081 break;
43988095
JK
14082 case DW_RLE_start_length:
14083 if (buffer + cu->header.addr_size > buf_end)
14084 {
14085 overflow = true;
14086 break;
14087 }
c8a7a66f
TT
14088 range_beginning = cu->header.read_address (obfd, buffer,
14089 &bytes_read);
43988095
JK
14090 buffer += bytes_read;
14091 range_end = (range_beginning
14092 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14093 buffer += bytes_read;
14094 if (buffer > buf_end)
14095 {
14096 overflow = true;
14097 break;
14098 }
14099 break;
d0ce17d8 14100 case DW_RLE_startx_length:
dda83cd7
SM
14101 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14102 buffer += bytes_read;
14103 range_beginning = read_addr_index (cu, addr_index);
14104 if (buffer > buf_end)
14105 {
14106 overflow = true;
14107 break;
14108 }
14109 range_end = (range_beginning
14110 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14111 buffer += bytes_read;
14112 break;
43988095
JK
14113 case DW_RLE_offset_pair:
14114 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14115 buffer += bytes_read;
14116 if (buffer > buf_end)
14117 {
14118 overflow = true;
14119 break;
14120 }
14121 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14122 buffer += bytes_read;
14123 if (buffer > buf_end)
14124 {
14125 overflow = true;
14126 break;
14127 }
14128 break;
14129 case DW_RLE_start_end:
14130 if (buffer + 2 * cu->header.addr_size > buf_end)
14131 {
14132 overflow = true;
14133 break;
14134 }
c8a7a66f
TT
14135 range_beginning = cu->header.read_address (obfd, buffer,
14136 &bytes_read);
43988095 14137 buffer += bytes_read;
c8a7a66f 14138 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14139 buffer += bytes_read;
14140 break;
d0ce17d8 14141 case DW_RLE_startx_endx:
dda83cd7
SM
14142 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14143 buffer += bytes_read;
14144 range_beginning = read_addr_index (cu, addr_index);
14145 if (buffer > buf_end)
14146 {
14147 overflow = true;
14148 break;
14149 }
14150 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14151 buffer += bytes_read;
14152 range_end = read_addr_index (cu, addr_index);
14153 break;
43988095 14154 default:
b98664d3 14155 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14156 return false;
14157 }
14158 if (rlet == DW_RLE_end_of_list || overflow)
14159 break;
14160 if (rlet == DW_RLE_base_address)
14161 continue;
14162
43988095
JK
14163 if (range_beginning > range_end)
14164 {
14165 /* Inverted range entries are invalid. */
b98664d3 14166 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14167 return false;
14168 }
14169
14170 /* Empty range entries have no effect. */
14171 if (range_beginning == range_end)
14172 continue;
14173
d0ce17d8
CT
14174 /* Only DW_RLE_offset_pair needs the base address added. */
14175 if (rlet == DW_RLE_offset_pair)
14176 {
14177 if (!base.has_value ())
14178 {
14179 /* We have no valid base address for the DW_RLE_offset_pair. */
14180 complaint (_("Invalid .debug_rnglists data (no base address for "
14181 "DW_RLE_offset_pair)"));
14182 return false;
14183 }
14184
14185 range_beginning += *base;
14186 range_end += *base;
14187 }
43988095
JK
14188
14189 /* A not-uncommon case of bad debug info.
14190 Don't pollute the addrmap with bad data. */
14191 if (range_beginning + baseaddr == 0
976ca316 14192 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14193 {
b98664d3 14194 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14195 " [in module %s]"), objfile_name (objfile));
14196 continue;
14197 }
14198
14199 callback (range_beginning, range_end);
14200 }
14201
14202 if (overflow)
14203 {
b98664d3 14204 complaint (_("Offset %d is not terminated "
43988095
JK
14205 "for DW_AT_ranges attribute"),
14206 offset);
14207 return false;
14208 }
14209
14210 return true;
14211}
14212
14213/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14214 Callback's type should be:
14215 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14216 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14217
43988095 14218template <typename Callback>
43039443 14219static int
d0ce17d8 14220dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14221 Callback &&callback)
43039443 14222{
5e22e966
SM
14223 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14224 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14225 struct comp_unit_head *cu_header = &cu->header;
14226 bfd *obfd = objfile->obfd;
14227 unsigned int addr_size = cu_header->addr_size;
14228 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14229 /* Base address selection entry. */
2b24b6e4 14230 gdb::optional<CORE_ADDR> base;
43039443 14231 unsigned int dummy;
d521ce57 14232 const gdb_byte *buffer;
ff013f42 14233 CORE_ADDR baseaddr;
43039443 14234
43988095 14235 if (cu_header->version >= 5)
d0ce17d8 14236 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14237
d00adf39 14238 base = cu->base_address;
43039443 14239
5e22e966
SM
14240 per_objfile->per_bfd->ranges.read (objfile);
14241 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14242 {
b98664d3 14243 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14244 offset);
14245 return 0;
14246 }
5e22e966 14247 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14248
b3b3bada 14249 baseaddr = objfile->text_section_offset ();
ff013f42 14250
43039443
JK
14251 while (1)
14252 {
14253 CORE_ADDR range_beginning, range_end;
14254
c8a7a66f 14255 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14256 buffer += addr_size;
c8a7a66f 14257 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14258 buffer += addr_size;
14259 offset += 2 * addr_size;
14260
14261 /* An end of list marker is a pair of zero addresses. */
14262 if (range_beginning == 0 && range_end == 0)
14263 /* Found the end of list entry. */
14264 break;
14265
14266 /* Each base address selection entry is a pair of 2 values.
14267 The first is the largest possible address, the second is
14268 the base address. Check for a base address here. */
14269 if ((range_beginning & mask) == mask)
14270 {
28d2bfb9
AB
14271 /* If we found the largest possible address, then we already
14272 have the base address in range_end. */
14273 base = range_end;
43039443
JK
14274 continue;
14275 }
14276
2b24b6e4 14277 if (!base.has_value ())
43039443
JK
14278 {
14279 /* We have no valid base address for the ranges
14280 data. */
b98664d3 14281 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14282 return 0;
14283 }
14284
9277c30c
UW
14285 if (range_beginning > range_end)
14286 {
14287 /* Inverted range entries are invalid. */
b98664d3 14288 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14289 return 0;
14290 }
14291
14292 /* Empty range entries have no effect. */
14293 if (range_beginning == range_end)
14294 continue;
14295
2b24b6e4
TT
14296 range_beginning += *base;
14297 range_end += *base;
43039443 14298
01093045
DE
14299 /* A not-uncommon case of bad debug info.
14300 Don't pollute the addrmap with bad data. */
14301 if (range_beginning + baseaddr == 0
5e22e966 14302 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14303 {
b98664d3 14304 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14305 " [in module %s]"), objfile_name (objfile));
01093045
DE
14306 continue;
14307 }
14308
5f46c5a5
JK
14309 callback (range_beginning, range_end);
14310 }
14311
14312 return 1;
14313}
14314
14315/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14316 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 14317 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
14318
14319static int
14320dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14321 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14322 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14323{
5e22e966 14324 struct objfile *objfile = cu->per_objfile->objfile;
84685904 14325 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 14326 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14327 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14328 int low_set = 0;
14329 CORE_ADDR low = 0;
14330 CORE_ADDR high = 0;
14331 int retval;
14332
d0ce17d8 14333 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14334 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14335 {
9277c30c 14336 if (ranges_pst != NULL)
3e29f34a
MR
14337 {
14338 CORE_ADDR lowpc;
14339 CORE_ADDR highpc;
14340
79748972
TT
14341 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14342 range_beginning + baseaddr)
14343 - baseaddr);
14344 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14345 range_end + baseaddr)
14346 - baseaddr);
84685904 14347 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 14348 lowpc, highpc - 1, ranges_pst);
3e29f34a 14349 }
ff013f42 14350
43039443
JK
14351 /* FIXME: This is recording everything as a low-high
14352 segment of consecutive addresses. We should have a
14353 data structure for discontiguous block ranges
14354 instead. */
14355 if (! low_set)
14356 {
14357 low = range_beginning;
14358 high = range_end;
14359 low_set = 1;
14360 }
14361 else
14362 {
14363 if (range_beginning < low)
14364 low = range_beginning;
14365 if (range_end > high)
14366 high = range_end;
14367 }
5f46c5a5
JK
14368 });
14369 if (!retval)
14370 return 0;
43039443
JK
14371
14372 if (! low_set)
14373 /* If the first entry is an end-of-list marker, the range
14374 describes an empty scope, i.e. no instructions. */
14375 return 0;
14376
14377 if (low_return)
14378 *low_return = low;
14379 if (high_return)
14380 *high_return = high;
14381 return 1;
14382}
14383
3a2b436a
JK
14384/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14385 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14386 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14387
3a2b436a 14388static enum pc_bounds_kind
af34e669 14389dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14390 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14391 dwarf2_psymtab *pst)
c906108c 14392{
976ca316 14393 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14394 struct attribute *attr;
91da1414 14395 struct attribute *attr_high;
af34e669
DJ
14396 CORE_ADDR low = 0;
14397 CORE_ADDR high = 0;
e385593e 14398 enum pc_bounds_kind ret;
c906108c 14399
91da1414
MW
14400 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14401 if (attr_high)
af34e669 14402 {
e142c38c 14403 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14404 if (attr != nullptr)
dda83cd7 14405 {
95f982e5
TT
14406 low = attr->as_address ();
14407 high = attr_high->as_address ();
cd6c91b4 14408 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14409 high += low;
91da1414 14410 }
af34e669
DJ
14411 else
14412 /* Found high w/o low attribute. */
e385593e 14413 return PC_BOUNDS_INVALID;
af34e669
DJ
14414
14415 /* Found consecutive range of addresses. */
3a2b436a 14416 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14417 }
c906108c 14418 else
af34e669 14419 {
e142c38c 14420 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14421 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14422 {
2b0c7f41
SM
14423 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14424 on DWARF version). */
14425 ULONGEST ranges_offset = attr->as_unsigned ();
14426
14427 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14428 this value. */
14429 if (die->tag != DW_TAG_compile_unit)
14430 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14431
af34e669 14432 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14433 .debug_ranges section. */
d0ce17d8
CT
14434 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14435 die->tag))
e385593e 14436 return PC_BOUNDS_INVALID;
43039443 14437 /* Found discontinuous range of addresses. */
3a2b436a 14438 ret = PC_BOUNDS_RANGES;
af34e669 14439 }
e385593e
JK
14440 else
14441 return PC_BOUNDS_NOT_PRESENT;
af34e669 14442 }
c906108c 14443
48fbe735 14444 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14445 if (high <= low)
e385593e 14446 return PC_BOUNDS_INVALID;
c906108c
SS
14447
14448 /* When using the GNU linker, .gnu.linkonce. sections are used to
14449 eliminate duplicate copies of functions and vtables and such.
14450 The linker will arbitrarily choose one and discard the others.
14451 The AT_*_pc values for such functions refer to local labels in
14452 these sections. If the section from that file was discarded, the
14453 labels are not in the output, so the relocs get a value of 0.
14454 If this is a discarded function, mark the pc bounds as invalid,
14455 so that GDB will ignore it. */
976ca316 14456 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14457 return PC_BOUNDS_INVALID;
c906108c
SS
14458
14459 *lowpc = low;
96408a79
SA
14460 if (highpc)
14461 *highpc = high;
af34e669 14462 return ret;
c906108c
SS
14463}
14464
b084d499
JB
14465/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14466 its low and high PC addresses. Do nothing if these addresses could not
14467 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14468 and HIGHPC to the high address if greater than HIGHPC. */
14469
14470static void
14471dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14472 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14473 struct dwarf2_cu *cu)
b084d499
JB
14474{
14475 CORE_ADDR low, high;
14476 struct die_info *child = die->child;
14477
e385593e 14478 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14479 {
325fac50
PA
14480 *lowpc = std::min (*lowpc, low);
14481 *highpc = std::max (*highpc, high);
b084d499
JB
14482 }
14483
14484 /* If the language does not allow nested subprograms (either inside
14485 subprograms or lexical blocks), we're done. */
14486 if (cu->language != language_ada)
14487 return;
6e70227d 14488
b084d499
JB
14489 /* Check all the children of the given DIE. If it contains nested
14490 subprograms, then check their pc bounds. Likewise, we need to
14491 check lexical blocks as well, as they may also contain subprogram
14492 definitions. */
14493 while (child && child->tag)
14494 {
14495 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14496 || child->tag == DW_TAG_lexical_block)
14497 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14498 child = child->sibling;
b084d499
JB
14499 }
14500}
14501
fae299cd
DC
14502/* Get the low and high pc's represented by the scope DIE, and store
14503 them in *LOWPC and *HIGHPC. If the correct values can't be
14504 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14505
14506static void
14507get_scope_pc_bounds (struct die_info *die,
14508 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14509 struct dwarf2_cu *cu)
14510{
14511 CORE_ADDR best_low = (CORE_ADDR) -1;
14512 CORE_ADDR best_high = (CORE_ADDR) 0;
14513 CORE_ADDR current_low, current_high;
14514
3a2b436a 14515 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14516 >= PC_BOUNDS_RANGES)
fae299cd
DC
14517 {
14518 best_low = current_low;
14519 best_high = current_high;
14520 }
14521 else
14522 {
14523 struct die_info *child = die->child;
14524
14525 while (child && child->tag)
14526 {
14527 switch (child->tag) {
14528 case DW_TAG_subprogram:
dda83cd7 14529 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14530 break;
14531 case DW_TAG_namespace:
f55ee35c 14532 case DW_TAG_module:
fae299cd
DC
14533 /* FIXME: carlton/2004-01-16: Should we do this for
14534 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14535 that current GCC's always emit the DIEs corresponding
14536 to definitions of methods of classes as children of a
14537 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14538 the DIEs giving the declarations, which could be
14539 anywhere). But I don't see any reason why the
14540 standards says that they have to be there. */
14541 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14542
14543 if (current_low != ((CORE_ADDR) -1))
14544 {
325fac50
PA
14545 best_low = std::min (best_low, current_low);
14546 best_high = std::max (best_high, current_high);
fae299cd
DC
14547 }
14548 break;
14549 default:
0963b4bd 14550 /* Ignore. */
fae299cd
DC
14551 break;
14552 }
14553
436c571c 14554 child = child->sibling;
fae299cd
DC
14555 }
14556 }
14557
14558 *lowpc = best_low;
14559 *highpc = best_high;
14560}
14561
801e3a5b
JB
14562/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14563 in DIE. */
380bca97 14564
801e3a5b
JB
14565static void
14566dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14567 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14568{
5e22e966 14569 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14570 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14571 struct attribute *attr;
91da1414 14572 struct attribute *attr_high;
801e3a5b 14573
91da1414
MW
14574 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14575 if (attr_high)
801e3a5b 14576 {
801e3a5b 14577 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14578 if (attr != nullptr)
dda83cd7 14579 {
95f982e5
TT
14580 CORE_ADDR low = attr->as_address ();
14581 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14582
cd6c91b4 14583 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14584 high += low;
9a619af0 14585
3e29f34a
MR
14586 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14587 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14588 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14589 }
801e3a5b
JB
14590 }
14591
14592 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14593 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14594 {
2b0c7f41
SM
14595 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14596 on DWARF version). */
14597 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14598
2b0c7f41
SM
14599 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14600 this value. */
14601 if (die->tag != DW_TAG_compile_unit)
14602 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14603
2d5f09ec 14604 std::vector<blockrange> blockvec;
2b0c7f41 14605 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14606 [&] (CORE_ADDR start, CORE_ADDR end)
14607 {
58fdfd2c
JK
14608 start += baseaddr;
14609 end += baseaddr;
5f46c5a5
JK
14610 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14611 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14612 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14613 blockvec.emplace_back (start, end);
5f46c5a5 14614 });
2d5f09ec
KB
14615
14616 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14617 }
14618}
14619
685b1105
JK
14620/* Check whether the producer field indicates either of GCC < 4.6, or the
14621 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14622
685b1105
JK
14623static void
14624check_producer (struct dwarf2_cu *cu)
60d5a603 14625{
38360086 14626 int major, minor;
60d5a603
JK
14627
14628 if (cu->producer == NULL)
14629 {
14630 /* For unknown compilers expect their behavior is DWARF version
14631 compliant.
14632
14633 GCC started to support .debug_types sections by -gdwarf-4 since
14634 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14635 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14636 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14637 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14638 }
b1ffba5a 14639 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14640 {
38360086
MW
14641 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14642 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14643 }
5230b05a 14644 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14645 {
14646 cu->producer_is_icc = true;
14647 cu->producer_is_icc_lt_14 = major < 14;
14648 }
c258c396
JD
14649 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14650 cu->producer_is_codewarrior = true;
685b1105
JK
14651 else
14652 {
14653 /* For other non-GCC compilers, expect their behavior is DWARF version
14654 compliant. */
60d5a603
JK
14655 }
14656
9068261f 14657 cu->checked_producer = true;
685b1105 14658}
ba919b58 14659
685b1105
JK
14660/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14661 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14662 during 4.6.0 experimental. */
14663
9068261f 14664static bool
685b1105
JK
14665producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14666{
14667 if (!cu->checked_producer)
14668 check_producer (cu);
14669
14670 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14671}
14672
c258c396
JD
14673
14674/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14675 with incorrect is_stmt attributes. */
14676
14677static bool
14678producer_is_codewarrior (struct dwarf2_cu *cu)
14679{
14680 if (!cu->checked_producer)
14681 check_producer (cu);
14682
14683 return cu->producer_is_codewarrior;
14684}
14685
bf23a268
TT
14686/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14687 If that attribute is not available, return the appropriate
14688 default. */
60d5a603
JK
14689
14690static enum dwarf_access_attribute
bf23a268 14691dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14692{
bf23a268
TT
14693 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14694 if (attr != nullptr)
14695 {
14696 LONGEST value = attr->constant_value (-1);
14697 if (value == DW_ACCESS_public
14698 || value == DW_ACCESS_protected
14699 || value == DW_ACCESS_private)
14700 return (dwarf_access_attribute) value;
14701 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14702 plongest (value));
14703 }
14704
60d5a603
JK
14705 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14706 {
14707 /* The default DWARF 2 accessibility for members is public, the default
14708 accessibility for inheritance is private. */
14709
14710 if (die->tag != DW_TAG_inheritance)
14711 return DW_ACCESS_public;
14712 else
14713 return DW_ACCESS_private;
14714 }
14715 else
14716 {
14717 /* DWARF 3+ defines the default accessibility a different way. The same
14718 rules apply now for DW_TAG_inheritance as for the members and it only
14719 depends on the container kind. */
14720
14721 if (die->parent->tag == DW_TAG_class_type)
14722 return DW_ACCESS_private;
14723 else
14724 return DW_ACCESS_public;
14725 }
14726}
14727
74ac6d43
TT
14728/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14729 offset. If the attribute was not found return 0, otherwise return
14730 1. If it was found but could not properly be handled, set *OFFSET
14731 to 0. */
14732
14733static int
14734handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14735 LONGEST *offset)
14736{
14737 struct attribute *attr;
14738
14739 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14740 if (attr != NULL)
14741 {
14742 *offset = 0;
14743
14744 /* Note that we do not check for a section offset first here.
14745 This is because DW_AT_data_member_location is new in DWARF 4,
14746 so if we see it, we can assume that a constant form is really
14747 a constant and not a section offset. */
cd6c91b4 14748 if (attr->form_is_constant ())
0826b30a 14749 *offset = attr->constant_value (0);
cd6c91b4 14750 else if (attr->form_is_section_offset ())
74ac6d43 14751 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14752 else if (attr->form_is_block ())
9d2246fc 14753 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14754 else
14755 dwarf2_complex_location_expr_complaint ();
14756
14757 return 1;
14758 }
14759
14760 return 0;
14761}
14762
7d79de9a
TT
14763/* Look for DW_AT_data_member_location and store the results in FIELD. */
14764
14765static void
14766handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14767 struct field *field)
14768{
14769 struct attribute *attr;
14770
14771 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14772 if (attr != NULL)
14773 {
14774 if (attr->form_is_constant ())
14775 {
14776 LONGEST offset = attr->constant_value (0);
14777 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14778 }
14779 else if (attr->form_is_section_offset ())
14780 dwarf2_complex_location_expr_complaint ();
14781 else if (attr->form_is_block ())
14782 {
14783 bool handled;
9d2246fc 14784 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14785 if (handled)
14786 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14787 else
14788 {
5e22e966
SM
14789 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14790 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14791 struct dwarf2_locexpr_baton *dlbaton
14792 = XOBNEW (&objfile->objfile_obstack,
14793 struct dwarf2_locexpr_baton);
9d2246fc
TT
14794 dlbaton->data = attr->as_block ()->data;
14795 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14796 /* When using this baton, we want to compute the address
14797 of the field, not the value. This is why
14798 is_reference is set to false here. */
14799 dlbaton->is_reference = false;
5e22e966 14800 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14801 dlbaton->per_cu = cu->per_cu;
14802
14803 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14804 }
14805 }
14806 else
14807 dwarf2_complex_location_expr_complaint ();
14808 }
14809}
14810
c906108c
SS
14811/* Add an aggregate field to the field list. */
14812
14813static void
107d2387 14814dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14815 struct dwarf2_cu *cu)
6e70227d 14816{
5e22e966 14817 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14818 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14819 struct nextfield *new_field;
14820 struct attribute *attr;
14821 struct field *fp;
15d034d0 14822 const char *fieldname = "";
c906108c 14823
7d0ccb61
DJ
14824 if (die->tag == DW_TAG_inheritance)
14825 {
be2daae6
TT
14826 fip->baseclasses.emplace_back ();
14827 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14828 }
14829 else
14830 {
be2daae6
TT
14831 fip->fields.emplace_back ();
14832 new_field = &fip->fields.back ();
7d0ccb61 14833 }
be2daae6 14834
9c6a1327
TT
14835 new_field->offset = die->sect_off;
14836
bf23a268 14837 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 14838 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 14839 fip->non_public_fields = true;
60d5a603 14840
e142c38c 14841 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14842 if (attr != nullptr)
23dca5c3 14843 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
14844 else
14845 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14846
14847 fp = &new_field->field;
a9a9bd0f 14848
e142c38c 14849 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14850 {
a9a9bd0f 14851 /* Data member other than a C++ static data member. */
6e70227d 14852
c906108c 14853 /* Get type of field. */
5d14b6e5 14854 fp->set_type (die_type (die, cu));
c906108c 14855
d6a843b5 14856 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14857
c906108c 14858 /* Get bit size of field (zero if none). */
e142c38c 14859 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14860 if (attr != nullptr)
c906108c 14861 {
529908cb 14862 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
14863 }
14864 else
14865 {
14866 FIELD_BITSIZE (*fp) = 0;
14867 }
14868
14869 /* Get bit offset of field. */
7d79de9a 14870 handle_data_member_location (die, cu, fp);
e142c38c 14871 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 14872 if (attr != nullptr && attr->form_is_constant ())
c906108c 14873 {
d5a22e77 14874 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14875 {
14876 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
14877 additional bit offset from the MSB of the containing
14878 anonymous object to the MSB of the field. We don't
14879 have to do anything special since we don't need to
14880 know the size of the anonymous object. */
529908cb 14881 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 14882 + attr->constant_value (0)));
c906108c
SS
14883 }
14884 else
14885 {
14886 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
14887 MSB of the anonymous object, subtract off the number of
14888 bits from the MSB of the field to the MSB of the
14889 object, and then subtract off the number of bits of
14890 the field itself. The result is the bit offset of
14891 the LSB of the field. */
c906108c 14892 int anonymous_size;
7c184d33 14893 int bit_offset = attr->constant_value (0);
c906108c 14894
e142c38c 14895 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 14896 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
14897 {
14898 /* The size of the anonymous object containing
14899 the bit field is explicit, so use the
14900 indicated size (in bytes). */
7c184d33 14901 anonymous_size = attr->constant_value (0);
c906108c
SS
14902 }
14903 else
14904 {
14905 /* The size of the anonymous object containing
14906 the bit field must be inferred from the type
14907 attribute of the data member containing the
14908 bit field. */
5d14b6e5 14909 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14910 }
f41f5e61
PA
14911 SET_FIELD_BITPOS (*fp,
14912 (FIELD_BITPOS (*fp)
14913 + anonymous_size * bits_per_byte
14914 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14915 }
14916 }
da5b30da
AA
14917 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14918 if (attr != NULL)
14919 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14920 + attr->constant_value (0)));
c906108c
SS
14921
14922 /* Get name of field. */
39cbfefa
DJ
14923 fieldname = dwarf2_name (die, cu);
14924 if (fieldname == NULL)
14925 fieldname = "";
d8151005
DJ
14926
14927 /* The name is already allocated along with this objfile, so we don't
14928 need to duplicate it for the type. */
14929 fp->name = fieldname;
c906108c
SS
14930
14931 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 14932 pointer or virtual base class pointer) to private. */
e142c38c 14933 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14934 {
d48cc9dd 14935 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 14936 new_field->accessibility = DW_ACCESS_private;
264fc0e2 14937 fip->non_public_fields = true;
c906108c
SS
14938 }
14939 }
a9a9bd0f 14940 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14941 {
a9a9bd0f
DC
14942 /* C++ static member. */
14943
14944 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14945 is a declaration, but all versions of G++ as of this writing
14946 (so through at least 3.2.1) incorrectly generate
14947 DW_TAG_variable tags. */
6e70227d 14948
ff355380 14949 const char *physname;
c906108c 14950
a9a9bd0f 14951 /* Get name of field. */
39cbfefa
DJ
14952 fieldname = dwarf2_name (die, cu);
14953 if (fieldname == NULL)
c906108c
SS
14954 return;
14955
254e6b9e 14956 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14957 if (attr
14958 /* Only create a symbol if this is an external value.
14959 new_symbol checks this and puts the value in the global symbol
14960 table, which we want. If it is not external, new_symbol
14961 will try to put the value in cu->list_in_scope which is wrong. */
14962 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14963 {
14964 /* A static const member, not much different than an enum as far as
14965 we're concerned, except that we can support more types. */
14966 new_symbol (die, NULL, cu);
14967 }
14968
2df3850c 14969 /* Get physical name. */
ff355380 14970 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14971
d8151005
DJ
14972 /* The name is already allocated along with this objfile, so we don't
14973 need to duplicate it for the type. */
14974 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14975 fp->set_type (die_type (die, cu));
d8151005 14976 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14977 }
14978 else if (die->tag == DW_TAG_inheritance)
14979 {
74ac6d43 14980 /* C++ base class field. */
7d79de9a 14981 handle_data_member_location (die, cu, fp);
c906108c 14982 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14983 fp->set_type (die_type (die, cu));
14984 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14985 }
2ddeaf8a
TT
14986 else
14987 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14988}
14989
883fd55a
KS
14990/* Can the type given by DIE define another type? */
14991
14992static bool
14993type_can_define_types (const struct die_info *die)
14994{
14995 switch (die->tag)
14996 {
14997 case DW_TAG_typedef:
14998 case DW_TAG_class_type:
14999 case DW_TAG_structure_type:
15000 case DW_TAG_union_type:
15001 case DW_TAG_enumeration_type:
15002 return true;
15003
15004 default:
15005 return false;
15006 }
15007}
15008
15009/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15010
15011static void
883fd55a
KS
15012dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15013 struct dwarf2_cu *cu)
6e70227d 15014{
be2daae6
TT
15015 struct decl_field fp;
15016 memset (&fp, 0, sizeof (fp));
98751a41 15017
883fd55a 15018 gdb_assert (type_can_define_types (die));
98751a41 15019
883fd55a 15020 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15021 fp.name = dwarf2_name (die, cu);
15022 fp.type = read_type_die (die, cu);
98751a41 15023
c191a687 15024 /* Save accessibility. */
bf23a268 15025 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15026 switch (accessibility)
15027 {
15028 case DW_ACCESS_public:
15029 /* The assumed value if neither private nor protected. */
15030 break;
15031 case DW_ACCESS_private:
be2daae6 15032 fp.is_private = 1;
c191a687
KS
15033 break;
15034 case DW_ACCESS_protected:
be2daae6 15035 fp.is_protected = 1;
c191a687 15036 break;
c191a687
KS
15037 }
15038
883fd55a 15039 if (die->tag == DW_TAG_typedef)
be2daae6 15040 fip->typedef_field_list.push_back (fp);
883fd55a 15041 else
be2daae6 15042 fip->nested_types_list.push_back (fp);
98751a41
JK
15043}
15044
9c6a1327
TT
15045/* A convenience typedef that's used when finding the discriminant
15046 field for a variant part. */
1b95cdb7
SM
15047typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15048 offset_map_type;
9c6a1327
TT
15049
15050/* Compute the discriminant range for a given variant. OBSTACK is
15051 where the results will be stored. VARIANT is the variant to
15052 process. IS_UNSIGNED indicates whether the discriminant is signed
15053 or unsigned. */
15054
15055static const gdb::array_view<discriminant_range>
15056convert_variant_range (struct obstack *obstack, const variant_field &variant,
15057 bool is_unsigned)
15058{
15059 std::vector<discriminant_range> ranges;
15060
15061 if (variant.default_branch)
15062 return {};
15063
15064 if (variant.discr_list_data == nullptr)
15065 {
15066 discriminant_range r
15067 = {variant.discriminant_value, variant.discriminant_value};
15068 ranges.push_back (r);
15069 }
15070 else
15071 {
15072 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15073 variant.discr_list_data->size);
15074 while (!data.empty ())
15075 {
15076 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15077 {
15078 complaint (_("invalid discriminant marker: %d"), data[0]);
15079 break;
15080 }
15081 bool is_range = data[0] == DW_DSC_range;
15082 data = data.slice (1);
15083
15084 ULONGEST low, high;
15085 unsigned int bytes_read;
15086
15087 if (data.empty ())
15088 {
15089 complaint (_("DW_AT_discr_list missing low value"));
15090 break;
15091 }
15092 if (is_unsigned)
15093 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15094 else
15095 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15096 &bytes_read);
15097 data = data.slice (bytes_read);
15098
15099 if (is_range)
15100 {
15101 if (data.empty ())
15102 {
15103 complaint (_("DW_AT_discr_list missing high value"));
15104 break;
15105 }
15106 if (is_unsigned)
15107 high = read_unsigned_leb128 (nullptr, data.data (),
15108 &bytes_read);
15109 else
15110 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15111 &bytes_read);
15112 data = data.slice (bytes_read);
15113 }
15114 else
15115 high = low;
15116
15117 ranges.push_back ({ low, high });
15118 }
15119 }
15120
15121 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15122 ranges.size ());
15123 std::copy (ranges.begin (), ranges.end (), result);
15124 return gdb::array_view<discriminant_range> (result, ranges.size ());
15125}
15126
15127static const gdb::array_view<variant_part> create_variant_parts
15128 (struct obstack *obstack,
15129 const offset_map_type &offset_map,
15130 struct field_info *fi,
15131 const std::vector<variant_part_builder> &variant_parts);
15132
15133/* Fill in a "struct variant" for a given variant field. RESULT is
15134 the variant to fill in. OBSTACK is where any needed allocations
15135 will be done. OFFSET_MAP holds the mapping from section offsets to
15136 fields for the type. FI describes the fields of the type we're
15137 processing. FIELD is the variant field we're converting. */
15138
15139static void
15140create_one_variant (variant &result, struct obstack *obstack,
15141 const offset_map_type &offset_map,
15142 struct field_info *fi, const variant_field &field)
15143{
15144 result.discriminants = convert_variant_range (obstack, field, false);
15145 result.first_field = field.first_field + fi->baseclasses.size ();
15146 result.last_field = field.last_field + fi->baseclasses.size ();
15147 result.parts = create_variant_parts (obstack, offset_map, fi,
15148 field.variant_parts);
15149}
15150
15151/* Fill in a "struct variant_part" for a given variant part. RESULT
15152 is the variant part to fill in. OBSTACK is where any needed
15153 allocations will be done. OFFSET_MAP holds the mapping from
15154 section offsets to fields for the type. FI describes the fields of
15155 the type we're processing. BUILDER is the variant part to be
15156 converted. */
15157
15158static void
15159create_one_variant_part (variant_part &result,
15160 struct obstack *obstack,
15161 const offset_map_type &offset_map,
15162 struct field_info *fi,
15163 const variant_part_builder &builder)
15164{
15165 auto iter = offset_map.find (builder.discriminant_offset);
15166 if (iter == offset_map.end ())
15167 {
15168 result.discriminant_index = -1;
15169 /* Doesn't matter. */
15170 result.is_unsigned = false;
15171 }
15172 else
15173 {
15174 result.discriminant_index = iter->second;
15175 result.is_unsigned
c6d940a9 15176 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15177 }
15178
15179 size_t n = builder.variants.size ();
15180 variant *output = new (obstack) variant[n];
15181 for (size_t i = 0; i < n; ++i)
15182 create_one_variant (output[i], obstack, offset_map, fi,
15183 builder.variants[i]);
15184
15185 result.variants = gdb::array_view<variant> (output, n);
15186}
15187
15188/* Create a vector of variant parts that can be attached to a type.
15189 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15190 holds the mapping from section offsets to fields for the type. FI
15191 describes the fields of the type we're processing. VARIANT_PARTS
15192 is the vector to convert. */
15193
15194static const gdb::array_view<variant_part>
15195create_variant_parts (struct obstack *obstack,
15196 const offset_map_type &offset_map,
15197 struct field_info *fi,
15198 const std::vector<variant_part_builder> &variant_parts)
15199{
15200 if (variant_parts.empty ())
15201 return {};
15202
15203 size_t n = variant_parts.size ();
15204 variant_part *result = new (obstack) variant_part[n];
15205 for (size_t i = 0; i < n; ++i)
15206 create_one_variant_part (result[i], obstack, offset_map, fi,
15207 variant_parts[i]);
15208
15209 return gdb::array_view<variant_part> (result, n);
15210}
15211
15212/* Compute the variant part vector for FIP, attaching it to TYPE when
15213 done. */
15214
15215static void
15216add_variant_property (struct field_info *fip, struct type *type,
15217 struct dwarf2_cu *cu)
15218{
15219 /* Map section offsets of fields to their field index. Note the
15220 field index here does not take the number of baseclasses into
15221 account. */
15222 offset_map_type offset_map;
15223 for (int i = 0; i < fip->fields.size (); ++i)
15224 offset_map[fip->fields[i].offset] = i;
15225
5e22e966 15226 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15227 gdb::array_view<variant_part> parts
15228 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15229 fip->variant_parts);
15230
15231 struct dynamic_prop prop;
8c2e4e06
SM
15232 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15233 obstack_copy (&objfile->objfile_obstack, &parts,
15234 sizeof (parts)));
9c6a1327 15235
5c54719c 15236 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15237}
15238
c906108c
SS
15239/* Create the vector of fields, and attach it to the type. */
15240
15241static void
fba45db2 15242dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15243 struct dwarf2_cu *cu)
c906108c 15244{
317f7127 15245 int nfields = fip->nfields ();
c906108c
SS
15246
15247 /* Record the field count, allocate space for the array of fields,
15248 and create blank accessibility bitfields if necessary. */
5e33d5f4 15249 type->set_num_fields (nfields);
3cabb6b0
SM
15250 type->set_fields
15251 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15252
b4ba55a1 15253 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15254 {
15255 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15256
15257 TYPE_FIELD_PRIVATE_BITS (type) =
15258 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15259 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15260
15261 TYPE_FIELD_PROTECTED_BITS (type) =
15262 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15263 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15264
774b6a14
TT
15265 TYPE_FIELD_IGNORE_BITS (type) =
15266 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15267 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15268 }
15269
15270 /* If the type has baseclasses, allocate and clear a bit vector for
15271 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15272 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15273 {
be2daae6 15274 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15275 unsigned char *pointer;
c906108c
SS
15276
15277 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15278 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15279 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15280 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15281 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15282 }
15283
9c6a1327
TT
15284 if (!fip->variant_parts.empty ())
15285 add_variant_property (fip, type, cu);
2ddeaf8a 15286
be2daae6
TT
15287 /* Copy the saved-up fields into the field vector. */
15288 for (int i = 0; i < nfields; ++i)
c906108c 15289 {
be2daae6
TT
15290 struct nextfield &field
15291 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15292 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15293
ceacbf6e 15294 type->field (i) = field.field;
be2daae6 15295 switch (field.accessibility)
c906108c 15296 {
c5aa993b 15297 case DW_ACCESS_private:
b4ba55a1 15298 if (cu->language != language_ada)
be2daae6 15299 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15300 break;
c906108c 15301
c5aa993b 15302 case DW_ACCESS_protected:
b4ba55a1 15303 if (cu->language != language_ada)
be2daae6 15304 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15305 break;
c906108c 15306
c5aa993b
JM
15307 case DW_ACCESS_public:
15308 break;
c906108c 15309
c5aa993b
JM
15310 default:
15311 /* Unknown accessibility. Complain and treat it as public. */
15312 {
b98664d3 15313 complaint (_("unsupported accessibility %d"),
be2daae6 15314 field.accessibility);
c5aa993b
JM
15315 }
15316 break;
c906108c 15317 }
be2daae6 15318 if (i < fip->baseclasses.size ())
c906108c 15319 {
be2daae6 15320 switch (field.virtuality)
c906108c 15321 {
c5aa993b
JM
15322 case DW_VIRTUALITY_virtual:
15323 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15324 if (cu->language == language_ada)
a73c6dcd 15325 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15326 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15327 break;
c906108c
SS
15328 }
15329 }
c906108c
SS
15330 }
15331}
15332
7d27a96d
TT
15333/* Return true if this member function is a constructor, false
15334 otherwise. */
15335
15336static int
15337dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15338{
15339 const char *fieldname;
fe978cb0 15340 const char *type_name;
7d27a96d
TT
15341 int len;
15342
15343 if (die->parent == NULL)
15344 return 0;
15345
15346 if (die->parent->tag != DW_TAG_structure_type
15347 && die->parent->tag != DW_TAG_union_type
15348 && die->parent->tag != DW_TAG_class_type)
15349 return 0;
15350
15351 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15352 type_name = dwarf2_name (die->parent, cu);
15353 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15354 return 0;
15355
15356 len = strlen (fieldname);
fe978cb0
PA
15357 return (strncmp (fieldname, type_name, len) == 0
15358 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15359}
15360
c906108c
SS
15361/* Add a member function to the proper fieldlist. */
15362
15363static void
107d2387 15364dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15365 struct type *type, struct dwarf2_cu *cu)
c906108c 15366{
5e22e966 15367 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15368 struct attribute *attr;
c906108c 15369 int i;
be2daae6 15370 struct fnfieldlist *flp = nullptr;
c906108c 15371 struct fn_field *fnp;
15d034d0 15372 const char *fieldname;
f792889a 15373 struct type *this_type;
c906108c 15374
b4ba55a1 15375 if (cu->language == language_ada)
a73c6dcd 15376 error (_("unexpected member function in Ada type"));
b4ba55a1 15377
2df3850c 15378 /* Get name of member function. */
39cbfefa
DJ
15379 fieldname = dwarf2_name (die, cu);
15380 if (fieldname == NULL)
2df3850c 15381 return;
c906108c 15382
c906108c 15383 /* Look up member function name in fieldlist. */
be2daae6 15384 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15385 {
27bfe10e 15386 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15387 {
15388 flp = &fip->fnfieldlists[i];
15389 break;
15390 }
c906108c
SS
15391 }
15392
be2daae6
TT
15393 /* Create a new fnfieldlist if necessary. */
15394 if (flp == nullptr)
c906108c 15395 {
be2daae6
TT
15396 fip->fnfieldlists.emplace_back ();
15397 flp = &fip->fnfieldlists.back ();
c906108c 15398 flp->name = fieldname;
be2daae6 15399 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15400 }
15401
be2daae6
TT
15402 /* Create a new member function field and add it to the vector of
15403 fnfieldlists. */
15404 flp->fnfields.emplace_back ();
15405 fnp = &flp->fnfields.back ();
3da10d80
KS
15406
15407 /* Delay processing of the physname until later. */
9c37b5ae 15408 if (cu->language == language_cplus)
be2daae6
TT
15409 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15410 die, cu);
3da10d80
KS
15411 else
15412 {
1d06ead6 15413 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15414 fnp->physname = physname ? physname : "";
15415 }
15416
c906108c 15417 fnp->type = alloc_type (objfile);
f792889a 15418 this_type = read_type_die (die, cu);
78134374 15419 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15420 {
1f704f76 15421 int nparams = this_type->num_fields ();
c906108c 15422
f792889a 15423 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15424 of the method itself (TYPE_CODE_METHOD). */
15425 smash_to_method_type (fnp->type, type,
f792889a 15426 TYPE_TARGET_TYPE (this_type),
80fc5e77 15427 this_type->fields (),
1f704f76 15428 this_type->num_fields (),
a409645d 15429 this_type->has_varargs ());
c906108c
SS
15430
15431 /* Handle static member functions.
dda83cd7
SM
15432 Dwarf2 has no clean way to discern C++ static and non-static
15433 member functions. G++ helps GDB by marking the first
15434 parameter for non-static member functions (which is the this
15435 pointer) as artificial. We obtain this information from
15436 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15437 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15438 fnp->voffset = VOFFSET_STATIC;
15439 }
15440 else
b98664d3 15441 complaint (_("member function type missing for '%s'"),
3da10d80 15442 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15443
15444 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15445 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15446 fnp->fcontext = die_containing_type (die, cu);
c906108c 15447
3e43a32a
MS
15448 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15449 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15450
15451 /* Get accessibility. */
bf23a268 15452 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15453 switch (accessibility)
c906108c 15454 {
60d5a603
JK
15455 case DW_ACCESS_private:
15456 fnp->is_private = 1;
15457 break;
15458 case DW_ACCESS_protected:
15459 fnp->is_protected = 1;
15460 break;
c906108c
SS
15461 }
15462
b02dede2 15463 /* Check for artificial methods. */
e142c38c 15464 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15465 if (attr && attr->as_boolean ())
b02dede2
DJ
15466 fnp->is_artificial = 1;
15467
e35000a7
TBA
15468 /* Check for defaulted methods. */
15469 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15470 if (attr != nullptr)
15471 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15472
15473 /* Check for deleted methods. */
15474 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15475 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15476 fnp->is_deleted = 1;
15477
7d27a96d
TT
15478 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15479
0d564a31 15480 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15481 function. For older versions of GCC, this is an offset in the
15482 appropriate virtual table, as specified by DW_AT_containing_type.
15483 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15484 to the object address. */
15485
e142c38c 15486 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15487 if (attr != nullptr)
8e19ed76 15488 {
9d2246fc 15489 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15490 {
9d2246fc
TT
15491 struct dwarf_block *block = attr->as_block ();
15492
15493 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15494 {
15495 /* Old-style GCC. */
9d2246fc 15496 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15497 }
9d2246fc
TT
15498 else if (block->data[0] == DW_OP_deref
15499 || (block->size > 1
15500 && block->data[0] == DW_OP_deref_size
15501 && block->data[1] == cu->header.addr_size))
aec5aa8b 15502 {
9d2246fc 15503 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15504 if ((fnp->voffset % cu->header.addr_size) != 0)
15505 dwarf2_complex_location_expr_complaint ();
15506 else
15507 fnp->voffset /= cu->header.addr_size;
15508 fnp->voffset += 2;
15509 }
15510 else
15511 dwarf2_complex_location_expr_complaint ();
15512
15513 if (!fnp->fcontext)
7e993ebf
KS
15514 {
15515 /* If there is no `this' field and no DW_AT_containing_type,
15516 we cannot actually find a base class context for the
15517 vtable! */
1f704f76 15518 if (this_type->num_fields () == 0
7e993ebf
KS
15519 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15520 {
b98664d3 15521 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15522 "function \"%s\" (offset %s)"),
15523 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15524 }
15525 else
15526 {
15527 fnp->fcontext
940da03e 15528 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15529 }
15530 }
aec5aa8b 15531 }
cd6c91b4 15532 else if (attr->form_is_section_offset ())
dda83cd7 15533 {
4d3c2250 15534 dwarf2_complex_location_expr_complaint ();
dda83cd7 15535 }
8e19ed76 15536 else
dda83cd7 15537 {
4d3c2250
KB
15538 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15539 fieldname);
dda83cd7 15540 }
0d564a31 15541 }
d48cc9dd
DJ
15542 else
15543 {
15544 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15545 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15546 {
15547 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15548 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15549 "but the vtable offset is not specified"),
9d8780f0 15550 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15551 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15552 TYPE_CPLUS_DYNAMIC (type) = 1;
15553 }
15554 }
c906108c
SS
15555}
15556
15557/* Create the vector of member function fields, and attach it to the type. */
15558
15559static void
fba45db2 15560dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15561 struct dwarf2_cu *cu)
c906108c 15562{
b4ba55a1 15563 if (cu->language == language_ada)
a73c6dcd 15564 error (_("unexpected member functions in Ada type"));
b4ba55a1 15565
c906108c
SS
15566 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15567 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15568 TYPE_ALLOC (type,
15569 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15570
be2daae6 15571 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15572 {
be2daae6 15573 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15574 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15575
be2daae6
TT
15576 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15577 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15578 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15579 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15580
15581 for (int k = 0; k < nf.fnfields.size (); ++k)
15582 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15583 }
15584
be2daae6 15585 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15586}
15587
1168df01
JB
15588/* Returns non-zero if NAME is the name of a vtable member in CU's
15589 language, zero otherwise. */
15590static int
15591is_vtable_name (const char *name, struct dwarf2_cu *cu)
15592{
15593 static const char vptr[] = "_vptr";
15594
9c37b5ae
TT
15595 /* Look for the C++ form of the vtable. */
15596 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15597 return 1;
15598
15599 return 0;
15600}
15601
c0dd20ea 15602/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15603 functions, with the ABI-specified layout. If TYPE describes
15604 such a structure, smash it into a member function type.
61049d3b
DJ
15605
15606 GCC shouldn't do this; it should just output pointer to member DIEs.
15607 This is GCC PR debug/28767. */
c0dd20ea 15608
0b92b5bb
TT
15609static void
15610quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15611{
09e2d7c7 15612 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15613
15614 /* Check for a structure with no name and two children. */
1f704f76 15615 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15616 return;
c0dd20ea
DJ
15617
15618 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15619 if (TYPE_FIELD_NAME (type, 0) == NULL
15620 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15621 || TYPE_FIELD_NAME (type, 1) == NULL
15622 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15623 return;
c0dd20ea
DJ
15624
15625 /* Find the type of the method. */
940da03e 15626 pfn_type = type->field (0).type ();
c0dd20ea 15627 if (pfn_type == NULL
78134374
SM
15628 || pfn_type->code () != TYPE_CODE_PTR
15629 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15630 return;
c0dd20ea
DJ
15631
15632 /* Look for the "this" argument. */
15633 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15634 if (pfn_type->num_fields () == 0
940da03e
SM
15635 /* || pfn_type->field (0).type () == NULL */
15636 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15637 return;
c0dd20ea 15638
940da03e 15639 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15640 new_type = alloc_type (objfile);
09e2d7c7 15641 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15642 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15643 pfn_type->has_varargs ());
0b92b5bb 15644 smash_to_methodptr_type (type, new_type);
c0dd20ea 15645}
1168df01 15646
e26624c6
TT
15647/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15648 requires rewriting, then copy it and return the updated copy.
15649 Otherwise return nullptr. */
15650
15651static struct type *
15652rewrite_array_type (struct type *type)
15653{
15654 if (type->code () != TYPE_CODE_ARRAY)
15655 return nullptr;
15656
15657 struct type *index_type = type->index_type ();
15658 range_bounds *current_bounds = index_type->bounds ();
15659
15660 /* Handle multi-dimensional arrays. */
15661 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15662 if (new_target == nullptr)
15663 {
15664 /* Maybe we don't need to rewrite this array. */
15665 if (current_bounds->low.kind () == PROP_CONST
15666 && current_bounds->high.kind () == PROP_CONST)
15667 return nullptr;
15668 }
15669
15670 /* Either the target type was rewritten, or the bounds have to be
15671 updated. Either way we want to copy the type and update
15672 everything. */
15673 struct type *copy = copy_type (type);
15674 int nfields = copy->num_fields ();
15675 field *new_fields
15676 = ((struct field *) TYPE_ZALLOC (copy,
15677 nfields * sizeof (struct field)));
15678 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15679 copy->set_fields (new_fields);
15680 if (new_target != nullptr)
15681 TYPE_TARGET_TYPE (copy) = new_target;
15682
15683 struct type *index_copy = copy_type (index_type);
15684 range_bounds *bounds
15685 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15686 sizeof (range_bounds));
15687 *bounds = *current_bounds;
15688 bounds->low.set_const_val (1);
15689 bounds->high.set_const_val (0);
15690 index_copy->set_bounds (bounds);
15691 copy->set_index_type (index_copy);
15692
15693 return copy;
15694}
15695
57567375
TT
15696/* While some versions of GCC will generate complicated DWARF for an
15697 array (see quirk_ada_thick_pointer), more recent versions were
15698 modified to emit an explicit thick pointer structure. However, in
15699 this case, the array still has DWARF expressions for its ranges,
15700 and these must be ignored. */
15701
15702static void
15703quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15704 struct type *type)
15705{
15706 gdb_assert (cu->language == language_ada);
15707
15708 /* Check for a structure with two children. */
15709 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15710 return;
15711
15712 /* Check for P_ARRAY and P_BOUNDS members. */
15713 if (TYPE_FIELD_NAME (type, 0) == NULL
15714 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15715 || TYPE_FIELD_NAME (type, 1) == NULL
15716 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15717 return;
15718
15719 /* Make sure we're looking at a pointer to an array. */
15720 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15721 return;
57567375 15722
e26624c6
TT
15723 /* The Ada code already knows how to handle these types, so all that
15724 we need to do is turn the bounds into static bounds. However, we
15725 don't want to rewrite existing array or index types in-place,
15726 because those may be referenced in other contexts where this
15727 rewriting is undesirable. */
15728 struct type *new_ary_type
15729 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15730 if (new_ary_type != nullptr)
15731 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15732}
15733
2b4424c3
TT
15734/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15735 appropriate error checking and issuing complaints if there is a
15736 problem. */
15737
15738static ULONGEST
15739get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15740{
15741 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15742
15743 if (attr == nullptr)
15744 return 0;
15745
cd6c91b4 15746 if (!attr->form_is_constant ())
2b4424c3 15747 {
b98664d3 15748 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15749 " - DIE at %s [in module %s]"),
15750 sect_offset_str (die->sect_off),
5e22e966 15751 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15752 return 0;
15753 }
15754
529908cb
TT
15755 LONGEST val = attr->constant_value (0);
15756 if (val < 0)
2b4424c3 15757 {
529908cb
TT
15758 complaint (_("DW_AT_alignment value must not be negative"
15759 " - DIE at %s [in module %s]"),
15760 sect_offset_str (die->sect_off),
15761 objfile_name (cu->per_objfile->objfile));
15762 return 0;
2b4424c3 15763 }
529908cb 15764 ULONGEST align = val;
2b4424c3
TT
15765
15766 if (align == 0)
15767 {
b98664d3 15768 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15769 " - DIE at %s [in module %s]"),
15770 sect_offset_str (die->sect_off),
5e22e966 15771 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15772 return 0;
15773 }
15774 if ((align & (align - 1)) != 0)
15775 {
b98664d3 15776 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15777 " - DIE at %s [in module %s]"),
15778 sect_offset_str (die->sect_off),
5e22e966 15779 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15780 return 0;
15781 }
15782
15783 return align;
15784}
15785
15786/* If the DIE has a DW_AT_alignment attribute, use its value to set
15787 the alignment for TYPE. */
15788
15789static void
15790maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15791 struct type *type)
15792{
15793 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15794 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15795 " - DIE at %s [in module %s]"),
15796 sect_offset_str (die->sect_off),
5e22e966 15797 objfile_name (cu->per_objfile->objfile));
2b4424c3 15798}
685b1105 15799
e35000a7
TBA
15800/* Check if the given VALUE is a valid enum dwarf_calling_convention
15801 constant for a type, according to DWARF5 spec, Table 5.5. */
15802
15803static bool
15804is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15805{
15806 switch (value)
15807 {
15808 case DW_CC_normal:
15809 case DW_CC_pass_by_reference:
15810 case DW_CC_pass_by_value:
15811 return true;
15812
15813 default:
15814 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15815 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15816 return false;
15817 }
15818}
15819
d0922fcf
TBA
15820/* Check if the given VALUE is a valid enum dwarf_calling_convention
15821 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15822 also according to GNU-specific values (see include/dwarf2.h). */
15823
15824static bool
15825is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15826{
15827 switch (value)
15828 {
15829 case DW_CC_normal:
15830 case DW_CC_program:
15831 case DW_CC_nocall:
15832 return true;
15833
15834 case DW_CC_GNU_renesas_sh:
15835 case DW_CC_GNU_borland_fastcall_i386:
15836 case DW_CC_GDB_IBM_OpenCL:
15837 return true;
15838
15839 default:
15840 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15841 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15842 return false;
15843 }
15844}
15845
c906108c 15846/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15847 (definition) to create a type for the structure or union. Fill in
15848 the type's name and general properties; the members will not be
83655187
DE
15849 processed until process_structure_scope. A symbol table entry for
15850 the type will also not be done until process_structure_scope (assuming
15851 the type has a name).
c906108c 15852
c767944b
DJ
15853 NOTE: we need to call these functions regardless of whether or not the
15854 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15855 structure or union. This gets the type entered into our set of
83655187 15856 user defined types. */
c906108c 15857
f792889a 15858static struct type *
134d01f1 15859read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15860{
5e22e966 15861 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15862 struct type *type;
15863 struct attribute *attr;
15d034d0 15864 const char *name;
c906108c 15865
348e048f
DE
15866 /* If the definition of this type lives in .debug_types, read that type.
15867 Don't follow DW_AT_specification though, that will take us back up
15868 the chain and we want to go down. */
052c8bb8 15869 attr = die->attr (DW_AT_signature);
435d3d88 15870 if (attr != nullptr)
348e048f 15871 {
ac9ec31b 15872 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15873
ac9ec31b 15874 /* The type's CU may not be the same as CU.
02142a6c 15875 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15876 return set_die_type (die, type, cu);
15877 }
15878
c0dd20ea 15879 type = alloc_type (objfile);
c906108c 15880 INIT_CPLUS_SPECIFIC (type);
93311388 15881
39cbfefa
DJ
15882 name = dwarf2_name (die, cu);
15883 if (name != NULL)
c906108c 15884 {
987504bb 15885 if (cu->language == language_cplus
c44af4eb
TT
15886 || cu->language == language_d
15887 || cu->language == language_rust)
63d06c5c 15888 {
15d034d0 15889 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15890
15891 /* dwarf2_full_name might have already finished building the DIE's
15892 type. If so, there is no need to continue. */
15893 if (get_die_type (die, cu) != NULL)
15894 return get_die_type (die, cu);
15895
d0e39ea2 15896 type->set_name (full_name);
63d06c5c
DC
15897 }
15898 else
15899 {
d8151005
DJ
15900 /* The name is already allocated along with this objfile, so
15901 we don't need to duplicate it for the type. */
d0e39ea2 15902 type->set_name (name);
63d06c5c 15903 }
c906108c
SS
15904 }
15905
15906 if (die->tag == DW_TAG_structure_type)
15907 {
67607e24 15908 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15909 }
15910 else if (die->tag == DW_TAG_union_type)
15911 {
67607e24 15912 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15913 }
15914 else
15915 {
67607e24 15916 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15917 }
15918
0cc2414c 15919 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
aa70e35c 15920 type->set_is_declared_class (true);
0cc2414c 15921
e35000a7
TBA
15922 /* Store the calling convention in the type if it's available in
15923 the die. Otherwise the calling convention remains set to
15924 the default value DW_CC_normal. */
15925 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15926 if (attr != nullptr
529908cb 15927 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
15928 {
15929 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15930 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 15931 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
15932 }
15933
e142c38c 15934 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15935 if (attr != nullptr)
c906108c 15936 {
cd6c91b4 15937 if (attr->form_is_constant ())
dda83cd7 15938 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
15939 else
15940 {
f8e89861 15941 struct dynamic_prop prop;
293e7e51 15942 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15943 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 15944 TYPE_LENGTH (type) = 0;
155bfbd3 15945 }
c906108c
SS
15946 }
15947 else
15948 {
15949 TYPE_LENGTH (type) = 0;
15950 }
15951
2b4424c3
TT
15952 maybe_set_alignment (cu, die, type);
15953
5230b05a 15954 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15955 {
5230b05a
WT
15956 /* ICC<14 does not output the required DW_AT_declaration on
15957 incomplete types, but gives them a size of zero. */
b4b73759 15958 type->set_is_stub (true);
685b1105
JK
15959 }
15960 else
9baccff6 15961 type->set_stub_is_supported (true);
685b1105 15962
dc718098 15963 if (die_is_declaration (die, cu))
b4b73759 15964 type->set_is_stub (true);
a6c727b2
DJ
15965 else if (attr == NULL && die->child == NULL
15966 && producer_is_realview (cu->producer))
15967 /* RealView does not output the required DW_AT_declaration
15968 on incomplete types. */
b4b73759 15969 type->set_is_stub (true);
dc718098 15970
c906108c
SS
15971 /* We need to add the type field to the die immediately so we don't
15972 infinitely recurse when dealing with pointers to the structure
0963b4bd 15973 type within the structure itself. */
1c379e20 15974 set_die_type (die, type, cu);
c906108c 15975
7e314c57
JK
15976 /* set_die_type should be already done. */
15977 set_descriptive_type (type, die, cu);
15978
c767944b
DJ
15979 return type;
15980}
15981
9c6a1327
TT
15982static void handle_struct_member_die
15983 (struct die_info *child_die,
15984 struct type *type,
15985 struct field_info *fi,
15986 std::vector<struct symbol *> *template_args,
15987 struct dwarf2_cu *cu);
15988
15989/* A helper for handle_struct_member_die that handles
15990 DW_TAG_variant_part. */
15991
15992static void
15993handle_variant_part (struct die_info *die, struct type *type,
15994 struct field_info *fi,
15995 std::vector<struct symbol *> *template_args,
15996 struct dwarf2_cu *cu)
15997{
15998 variant_part_builder *new_part;
15999 if (fi->current_variant_part == nullptr)
16000 {
16001 fi->variant_parts.emplace_back ();
16002 new_part = &fi->variant_parts.back ();
16003 }
16004 else if (!fi->current_variant_part->processing_variant)
16005 {
16006 complaint (_("nested DW_TAG_variant_part seen "
16007 "- DIE at %s [in module %s]"),
16008 sect_offset_str (die->sect_off),
5e22e966 16009 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16010 return;
16011 }
16012 else
16013 {
16014 variant_field &current = fi->current_variant_part->variants.back ();
16015 current.variant_parts.emplace_back ();
16016 new_part = &current.variant_parts.back ();
16017 }
16018
16019 /* When we recurse, we want callees to add to this new variant
16020 part. */
16021 scoped_restore save_current_variant_part
16022 = make_scoped_restore (&fi->current_variant_part, new_part);
16023
16024 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16025 if (discr == NULL)
16026 {
16027 /* It's a univariant form, an extension we support. */
16028 }
16029 else if (discr->form_is_ref ())
16030 {
16031 struct dwarf2_cu *target_cu = cu;
16032 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16033
16034 new_part->discriminant_offset = target_die->sect_off;
16035 }
16036 else
16037 {
16038 complaint (_("DW_AT_discr does not have DIE reference form"
16039 " - DIE at %s [in module %s]"),
16040 sect_offset_str (die->sect_off),
5e22e966 16041 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16042 }
16043
16044 for (die_info *child_die = die->child;
16045 child_die != NULL;
16046 child_die = child_die->sibling)
16047 handle_struct_member_die (child_die, type, fi, template_args, cu);
16048}
16049
16050/* A helper for handle_struct_member_die that handles
16051 DW_TAG_variant. */
16052
16053static void
16054handle_variant (struct die_info *die, struct type *type,
16055 struct field_info *fi,
16056 std::vector<struct symbol *> *template_args,
16057 struct dwarf2_cu *cu)
16058{
16059 if (fi->current_variant_part == nullptr)
16060 {
16061 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16062 "- DIE at %s [in module %s]"),
16063 sect_offset_str (die->sect_off),
5e22e966 16064 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16065 return;
16066 }
16067 if (fi->current_variant_part->processing_variant)
16068 {
16069 complaint (_("nested DW_TAG_variant seen "
16070 "- DIE at %s [in module %s]"),
16071 sect_offset_str (die->sect_off),
5e22e966 16072 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16073 return;
16074 }
16075
16076 scoped_restore save_processing_variant
16077 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16078 true);
16079
16080 fi->current_variant_part->variants.emplace_back ();
16081 variant_field &variant = fi->current_variant_part->variants.back ();
16082 variant.first_field = fi->fields.size ();
16083
16084 /* In a variant we want to get the discriminant and also add a
16085 field for our sole member child. */
16086 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16087 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16088 {
16089 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16090 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16091 variant.default_branch = true;
16092 else
9d2246fc 16093 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16094 }
16095 else
cae21f8e 16096 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16097
16098 for (die_info *variant_child = die->child;
16099 variant_child != NULL;
16100 variant_child = variant_child->sibling)
16101 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16102
16103 variant.last_field = fi->fields.size ();
16104}
16105
2ddeaf8a
TT
16106/* A helper for process_structure_scope that handles a single member
16107 DIE. */
16108
16109static void
16110handle_struct_member_die (struct die_info *child_die, struct type *type,
16111 struct field_info *fi,
16112 std::vector<struct symbol *> *template_args,
16113 struct dwarf2_cu *cu)
16114{
16115 if (child_die->tag == DW_TAG_member
9c6a1327 16116 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16117 {
16118 /* NOTE: carlton/2002-11-05: A C++ static data member
16119 should be a DW_TAG_member that is a declaration, but
16120 all versions of G++ as of this writing (so through at
16121 least 3.2.1) incorrectly generate DW_TAG_variable
16122 tags for them instead. */
16123 dwarf2_add_field (fi, child_die, cu);
16124 }
16125 else if (child_die->tag == DW_TAG_subprogram)
16126 {
16127 /* Rust doesn't have member functions in the C++ sense.
16128 However, it does emit ordinary functions as children
16129 of a struct DIE. */
16130 if (cu->language == language_rust)
16131 read_func_scope (child_die, cu);
16132 else
16133 {
16134 /* C++ member function. */
16135 dwarf2_add_member_fn (fi, child_die, type, cu);
16136 }
16137 }
16138 else if (child_die->tag == DW_TAG_inheritance)
16139 {
16140 /* C++ base class field. */
16141 dwarf2_add_field (fi, child_die, cu);
16142 }
16143 else if (type_can_define_types (child_die))
16144 dwarf2_add_type_defn (fi, child_die, cu);
16145 else if (child_die->tag == DW_TAG_template_type_param
16146 || child_die->tag == DW_TAG_template_value_param)
16147 {
16148 struct symbol *arg = new_symbol (child_die, NULL, cu);
16149
16150 if (arg != NULL)
16151 template_args->push_back (arg);
16152 }
9c6a1327
TT
16153 else if (child_die->tag == DW_TAG_variant_part)
16154 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16155 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16156 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16157}
16158
c767944b
DJ
16159/* Finish creating a structure or union type, including filling in
16160 its members and creating a symbol for it. */
16161
16162static void
16163process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16164{
5e22e966 16165 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16166 struct die_info *child_die;
c767944b
DJ
16167 struct type *type;
16168
16169 type = get_die_type (die, cu);
16170 if (type == NULL)
16171 type = read_structure_type (die, cu);
16172
3e1d3d8c 16173 bool has_template_parameters = false;
e142c38c 16174 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16175 {
16176 struct field_info fi;
2f4732b0 16177 std::vector<struct symbol *> template_args;
c906108c 16178
639d11d3 16179 child_die = die->child;
c906108c
SS
16180
16181 while (child_die && child_die->tag)
16182 {
2ddeaf8a 16183 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16184 child_die = child_die->sibling;
c906108c
SS
16185 }
16186
34eaf542 16187 /* Attach template arguments to type. */
2f4732b0 16188 if (!template_args.empty ())
34eaf542 16189 {
3e1d3d8c 16190 has_template_parameters = true;
34eaf542 16191 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16192 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16193 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16194 = XOBNEWVEC (&objfile->objfile_obstack,
16195 struct symbol *,
16196 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16197 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16198 template_args.data (),
34eaf542
TT
16199 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16200 * sizeof (struct symbol *)));
34eaf542
TT
16201 }
16202
c906108c 16203 /* Attach fields and member functions to the type. */
317f7127 16204 if (fi.nfields () > 0)
e7c27a73 16205 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16206 if (!fi.fnfieldlists.empty ())
c906108c 16207 {
e7c27a73 16208 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16209
c5aa993b 16210 /* Get the type which refers to the base class (possibly this
c906108c 16211 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16212 class from the DW_AT_containing_type attribute. This use of
16213 DW_AT_containing_type is a GNU extension. */
c906108c 16214
e142c38c 16215 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16216 {
e7c27a73 16217 struct type *t = die_containing_type (die, cu);
c906108c 16218
ae6ae975 16219 set_type_vptr_basetype (type, t);
c906108c
SS
16220 if (type == t)
16221 {
c906108c
SS
16222 int i;
16223
16224 /* Our own class provides vtbl ptr. */
1f704f76 16225 for (i = t->num_fields () - 1;
c906108c
SS
16226 i >= TYPE_N_BASECLASSES (t);
16227 --i)
16228 {
0d5cff50 16229 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16230
dda83cd7 16231 if (is_vtable_name (fieldname, cu))
c906108c 16232 {
ae6ae975 16233 set_type_vptr_fieldno (type, i);
c906108c
SS
16234 break;
16235 }
16236 }
16237
16238 /* Complain if virtual function table field not found. */
16239 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16240 complaint (_("virtual function table pointer "
3e43a32a 16241 "not found when defining class '%s'"),
7d93a1e0 16242 type->name () ? type->name () : "");
c906108c
SS
16243 }
16244 else
16245 {
ae6ae975 16246 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16247 }
16248 }
f6235d4c 16249 else if (cu->producer
61012eef 16250 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16251 {
16252 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16253 of the containing type, but the vtable pointer is
16254 always named __vfp. */
f6235d4c
EZ
16255
16256 int i;
16257
1f704f76 16258 for (i = type->num_fields () - 1;
f6235d4c
EZ
16259 i >= TYPE_N_BASECLASSES (type);
16260 --i)
16261 {
16262 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16263 {
ae6ae975
DE
16264 set_type_vptr_fieldno (type, i);
16265 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16266 break;
16267 }
16268 }
16269 }
c906108c 16270 }
98751a41
JK
16271
16272 /* Copy fi.typedef_field_list linked list elements content into the
16273 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16274 if (!fi.typedef_field_list.empty ())
98751a41 16275 {
be2daae6 16276 int count = fi.typedef_field_list.size ();
98751a41 16277
a0d7a4ff 16278 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16279 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16280 = ((struct decl_field *)
be2daae6
TT
16281 TYPE_ALLOC (type,
16282 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16283 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16284
be2daae6
TT
16285 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16286 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16287 }
c767944b 16288
883fd55a
KS
16289 /* Copy fi.nested_types_list linked list elements content into the
16290 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16291 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16292 {
be2daae6 16293 int count = fi.nested_types_list.size ();
883fd55a
KS
16294
16295 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16296 TYPE_NESTED_TYPES_ARRAY (type)
16297 = ((struct decl_field *)
be2daae6
TT
16298 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16299 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16300
be2daae6
TT
16301 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16302 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16303 }
c906108c 16304 }
63d06c5c 16305
bb5ed363 16306 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16307 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16308 cu->rust_unions.push_back (type);
57567375
TT
16309 else if (cu->language == language_ada)
16310 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16311
90aeadfc
DC
16312 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16313 snapshots) has been known to create a die giving a declaration
16314 for a class that has, as a child, a die giving a definition for a
16315 nested class. So we have to process our children even if the
16316 current die is a declaration. Normally, of course, a declaration
16317 won't have any children at all. */
134d01f1 16318
ca040673
DE
16319 child_die = die->child;
16320
90aeadfc
DC
16321 while (child_die != NULL && child_die->tag)
16322 {
16323 if (child_die->tag == DW_TAG_member
16324 || child_die->tag == DW_TAG_variable
34eaf542
TT
16325 || child_die->tag == DW_TAG_inheritance
16326 || child_die->tag == DW_TAG_template_value_param
16327 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16328 {
90aeadfc 16329 /* Do nothing. */
134d01f1 16330 }
90aeadfc
DC
16331 else
16332 process_die (child_die, cu);
134d01f1 16333
436c571c 16334 child_die = child_die->sibling;
134d01f1
DJ
16335 }
16336
fa4028e9
JB
16337 /* Do not consider external references. According to the DWARF standard,
16338 these DIEs are identified by the fact that they have no byte_size
16339 attribute, and a declaration attribute. */
16340 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16341 || !die_is_declaration (die, cu)
16342 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16343 {
16344 struct symbol *sym = new_symbol (die, type, cu);
16345
16346 if (has_template_parameters)
16347 {
a776957c
TT
16348 struct symtab *symtab;
16349 if (sym != nullptr)
16350 symtab = symbol_symtab (sym);
16351 else if (cu->line_header != nullptr)
16352 {
16353 /* Any related symtab will do. */
16354 symtab
7ba99d21 16355 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16356 }
16357 else
16358 {
16359 symtab = nullptr;
16360 complaint (_("could not find suitable "
16361 "symtab for template parameter"
16362 " - DIE at %s [in module %s]"),
16363 sect_offset_str (die->sect_off),
16364 objfile_name (objfile));
16365 }
16366
16367 if (symtab != nullptr)
16368 {
16369 /* Make sure that the symtab is set on the new symbols.
16370 Even though they don't appear in this symtab directly,
16371 other parts of gdb assume that symbols do, and this is
16372 reasonably true. */
16373 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16374 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16375 }
3e1d3d8c
TT
16376 }
16377 }
134d01f1
DJ
16378}
16379
ed6acedd
TT
16380/* Assuming DIE is an enumeration type, and TYPE is its associated
16381 type, update TYPE using some information only available in DIE's
16382 children. In particular, the fields are computed. */
55426c9d
JB
16383
16384static void
16385update_enumeration_type_from_children (struct die_info *die,
16386 struct type *type,
16387 struct dwarf2_cu *cu)
16388{
60f7655a 16389 struct die_info *child_die;
55426c9d
JB
16390 int unsigned_enum = 1;
16391 int flag_enum = 1;
55426c9d 16392
8268c778 16393 auto_obstack obstack;
ed6acedd 16394 std::vector<struct field> fields;
55426c9d 16395
60f7655a
DE
16396 for (child_die = die->child;
16397 child_die != NULL && child_die->tag;
436c571c 16398 child_die = child_die->sibling)
55426c9d
JB
16399 {
16400 struct attribute *attr;
16401 LONGEST value;
16402 const gdb_byte *bytes;
16403 struct dwarf2_locexpr_baton *baton;
16404 const char *name;
60f7655a 16405
55426c9d
JB
16406 if (child_die->tag != DW_TAG_enumerator)
16407 continue;
16408
16409 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16410 if (attr == NULL)
16411 continue;
16412
16413 name = dwarf2_name (child_die, cu);
16414 if (name == NULL)
16415 name = "<anonymous enumerator>";
16416
16417 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16418 &value, &bytes, &baton);
16419 if (value < 0)
16420 {
16421 unsigned_enum = 0;
16422 flag_enum = 0;
16423 }
55426c9d 16424 else
edd45eb0
SM
16425 {
16426 if (count_one_bits_ll (value) >= 2)
16427 flag_enum = 0;
edd45eb0 16428 }
55426c9d 16429
ed6acedd
TT
16430 fields.emplace_back ();
16431 struct field &field = fields.back ();
16432 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16433 SET_FIELD_ENUMVAL (field, value);
16434 }
16435
16436 if (!fields.empty ())
16437 {
5e33d5f4 16438 type->set_num_fields (fields.size ());
3cabb6b0
SM
16439 type->set_fields
16440 ((struct field *)
16441 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16442 memcpy (type->fields (), fields.data (),
ed6acedd 16443 sizeof (struct field) * fields.size ());
55426c9d
JB
16444 }
16445
16446 if (unsigned_enum)
653223d3
SM
16447 type->set_is_unsigned (true);
16448
55426c9d 16449 if (flag_enum)
9902b327 16450 type->set_is_flag_enum (true);
55426c9d
JB
16451}
16452
134d01f1
DJ
16453/* Given a DW_AT_enumeration_type die, set its type. We do not
16454 complete the type's fields yet, or create any symbols. */
c906108c 16455
f792889a 16456static struct type *
134d01f1 16457read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16458{
5e22e966 16459 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16460 struct type *type;
c906108c 16461 struct attribute *attr;
0114d602 16462 const char *name;
134d01f1 16463
348e048f
DE
16464 /* If the definition of this type lives in .debug_types, read that type.
16465 Don't follow DW_AT_specification though, that will take us back up
16466 the chain and we want to go down. */
052c8bb8 16467 attr = die->attr (DW_AT_signature);
435d3d88 16468 if (attr != nullptr)
348e048f 16469 {
ac9ec31b 16470 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16471
ac9ec31b 16472 /* The type's CU may not be the same as CU.
02142a6c 16473 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16474 return set_die_type (die, type, cu);
16475 }
16476
c906108c
SS
16477 type = alloc_type (objfile);
16478
67607e24 16479 type->set_code (TYPE_CODE_ENUM);
94af9270 16480 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16481 if (name != NULL)
d0e39ea2 16482 type->set_name (name);
c906108c 16483
0626fc76
TT
16484 attr = dwarf2_attr (die, DW_AT_type, cu);
16485 if (attr != NULL)
16486 {
16487 struct type *underlying_type = die_type (die, cu);
16488
16489 TYPE_TARGET_TYPE (type) = underlying_type;
16490 }
16491
e142c38c 16492 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16493 if (attr != nullptr)
c906108c 16494 {
529908cb 16495 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16496 }
16497 else
16498 {
16499 TYPE_LENGTH (type) = 0;
16500 }
16501
2b4424c3
TT
16502 maybe_set_alignment (cu, die, type);
16503
137033e9
JB
16504 /* The enumeration DIE can be incomplete. In Ada, any type can be
16505 declared as private in the package spec, and then defined only
16506 inside the package body. Such types are known as Taft Amendment
16507 Types. When another package uses such a type, an incomplete DIE
16508 may be generated by the compiler. */
02eb380e 16509 if (die_is_declaration (die, cu))
b4b73759 16510 type->set_is_stub (true);
02eb380e 16511
0626fc76
TT
16512 /* If this type has an underlying type that is not a stub, then we
16513 may use its attributes. We always use the "unsigned" attribute
16514 in this situation, because ordinarily we guess whether the type
16515 is unsigned -- but the guess can be wrong and the underlying type
16516 can tell us the reality. However, we defer to a local size
16517 attribute if one exists, because this lets the compiler override
16518 the underlying type if needed. */
e46d3488 16519 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16520 {
9e7c9a03
HD
16521 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16522 underlying_type = check_typedef (underlying_type);
653223d3
SM
16523
16524 type->set_is_unsigned (underlying_type->is_unsigned ());
16525
0626fc76 16526 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16527 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16528
2b4424c3 16529 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16530 && TYPE_RAW_ALIGN (underlying_type) != 0)
16531 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16532 }
16533
aa70e35c 16534 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
3d567982 16535
ed6acedd
TT
16536 set_die_type (die, type, cu);
16537
16538 /* Finish the creation of this type by using the enum's children.
16539 Note that, as usual, this must come after set_die_type to avoid
16540 infinite recursion when trying to compute the names of the
16541 enumerators. */
16542 update_enumeration_type_from_children (die, type, cu);
16543
16544 return type;
134d01f1
DJ
16545}
16546
16547/* Given a pointer to a die which begins an enumeration, process all
16548 the dies that define the members of the enumeration, and create the
16549 symbol for the enumeration type.
16550
16551 NOTE: We reverse the order of the element list. */
16552
16553static void
16554process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16555{
f792889a 16556 struct type *this_type;
134d01f1 16557
f792889a
DJ
16558 this_type = get_die_type (die, cu);
16559 if (this_type == NULL)
16560 this_type = read_enumeration_type (die, cu);
9dc481d3 16561
639d11d3 16562 if (die->child != NULL)
c906108c 16563 {
9dc481d3 16564 struct die_info *child_die;
15d034d0 16565 const char *name;
9dc481d3 16566
639d11d3 16567 child_die = die->child;
c906108c
SS
16568 while (child_die && child_die->tag)
16569 {
16570 if (child_die->tag != DW_TAG_enumerator)
16571 {
e7c27a73 16572 process_die (child_die, cu);
c906108c
SS
16573 }
16574 else
16575 {
39cbfefa
DJ
16576 name = dwarf2_name (child_die, cu);
16577 if (name)
ed6acedd 16578 new_symbol (child_die, this_type, cu);
c906108c
SS
16579 }
16580
436c571c 16581 child_die = child_die->sibling;
c906108c 16582 }
c906108c 16583 }
134d01f1 16584
6c83ed52
TT
16585 /* If we are reading an enum from a .debug_types unit, and the enum
16586 is a declaration, and the enum is not the signatured type in the
16587 unit, then we do not want to add a symbol for it. Adding a
16588 symbol would in some cases obscure the true definition of the
16589 enum, giving users an incomplete type when the definition is
16590 actually available. Note that we do not want to do this for all
16591 enums which are just declarations, because C++0x allows forward
16592 enum declarations. */
3019eac3 16593 if (cu->per_cu->is_debug_types
6c83ed52
TT
16594 && die_is_declaration (die, cu))
16595 {
52dc124a 16596 struct signatured_type *sig_type;
6c83ed52 16597
c0f78cd4 16598 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16599 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16600 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16601 return;
16602 }
16603
f792889a 16604 new_symbol (die, this_type, cu);
c906108c
SS
16605}
16606
57567375
TT
16607/* Helper function for quirk_ada_thick_pointer that examines a bounds
16608 expression for an index type and finds the corresponding field
16609 offset in the hidden "P_BOUNDS" structure. Returns true on success
16610 and updates *FIELD, false if it fails to recognize an
16611 expression. */
16612
16613static bool
16614recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16615 int *bounds_offset, struct field *field,
16616 struct dwarf2_cu *cu)
16617{
16618 struct attribute *attr = dwarf2_attr (die, name, cu);
16619 if (attr == nullptr || !attr->form_is_block ())
16620 return false;
16621
16622 const struct dwarf_block *block = attr->as_block ();
16623 const gdb_byte *start = block->data;
16624 const gdb_byte *end = block->data + block->size;
16625
16626 /* The expression to recognize generally looks like:
16627
16628 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16629 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16630
16631 However, the second "plus_uconst" may be missing:
16632
16633 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16634 DW_OP_deref_size: 4)
16635
16636 This happens when the field is at the start of the structure.
16637
16638 Also, the final deref may not be sized:
16639
16640 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16641 DW_OP_deref)
16642
16643 This happens when the size of the index type happens to be the
16644 same as the architecture's word size. This can occur with or
16645 without the second plus_uconst. */
16646
16647 if (end - start < 2)
16648 return false;
16649 if (*start++ != DW_OP_push_object_address)
16650 return false;
16651 if (*start++ != DW_OP_plus_uconst)
16652 return false;
16653
16654 uint64_t this_bound_off;
16655 start = gdb_read_uleb128 (start, end, &this_bound_off);
16656 if (start == nullptr || (int) this_bound_off != this_bound_off)
16657 return false;
16658 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16659 is consistent among all bounds. */
16660 if (*bounds_offset == -1)
16661 *bounds_offset = this_bound_off;
16662 else if (*bounds_offset != this_bound_off)
16663 return false;
16664
16665 if (start == end || *start++ != DW_OP_deref)
16666 return false;
16667
16668 int offset = 0;
16669 if (start ==end)
16670 return false;
16671 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16672 {
16673 /* This means an offset of 0. */
16674 }
16675 else if (*start++ != DW_OP_plus_uconst)
16676 return false;
16677 else
16678 {
16679 /* The size is the parameter to DW_OP_plus_uconst. */
16680 uint64_t val;
16681 start = gdb_read_uleb128 (start, end, &val);
16682 if (start == nullptr)
16683 return false;
16684 if ((int) val != val)
16685 return false;
16686 offset = val;
16687 }
16688
16689 if (start == end)
16690 return false;
16691
16692 uint64_t size;
16693 if (*start == DW_OP_deref_size)
16694 {
16695 start = gdb_read_uleb128 (start + 1, end, &size);
16696 if (start == nullptr)
16697 return false;
16698 }
16699 else if (*start == DW_OP_deref)
16700 {
16701 size = cu->header.addr_size;
16702 ++start;
16703 }
16704 else
16705 return false;
16706
16707 SET_FIELD_BITPOS (*field, 8 * offset);
16708 if (size != TYPE_LENGTH (field->type ()))
16709 FIELD_BITSIZE (*field) = 8 * size;
16710
16711 return true;
16712}
16713
16714/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16715 some kinds of Ada arrays:
16716
16717 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16718 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16719 <11e0> DW_AT_data_location: 2 byte block: 97 6
16720 (DW_OP_push_object_address; DW_OP_deref)
16721 <11e3> DW_AT_type : <0x1173>
16722 <11e7> DW_AT_sibling : <0x1201>
16723 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16724 <11ec> DW_AT_type : <0x1206>
16725 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16726 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16727 DW_OP_deref_size: 4)
16728 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16729 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16730 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16731
16732 This actually represents a "thick pointer", which is a structure
16733 with two elements: one that is a pointer to the array data, and one
16734 that is a pointer to another structure; this second structure holds
16735 the array bounds.
16736
16737 This returns a new type on success, or nullptr if this didn't
16738 recognize the type. */
16739
16740static struct type *
16741quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16742 struct type *type)
16743{
16744 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16745 /* So far we've only seen this with block form. */
16746 if (attr == nullptr || !attr->form_is_block ())
16747 return nullptr;
16748
16749 /* Note that this will fail if the structure layout is changed by
16750 the compiler. However, we have no good way to recognize some
16751 other layout, because we don't know what expression the compiler
16752 might choose to emit should this happen. */
16753 struct dwarf_block *blk = attr->as_block ();
16754 if (blk->size != 2
16755 || blk->data[0] != DW_OP_push_object_address
16756 || blk->data[1] != DW_OP_deref)
16757 return nullptr;
16758
16759 int bounds_offset = -1;
16760 int max_align = -1;
16761 std::vector<struct field> range_fields;
16762 for (struct die_info *child_die = die->child;
16763 child_die;
16764 child_die = child_die->sibling)
16765 {
16766 if (child_die->tag == DW_TAG_subrange_type)
16767 {
16768 struct type *underlying = read_subrange_index_type (child_die, cu);
16769
16770 int this_align = type_align (underlying);
16771 if (this_align > max_align)
16772 max_align = this_align;
16773
16774 range_fields.emplace_back ();
16775 range_fields.emplace_back ();
16776
16777 struct field &lower = range_fields[range_fields.size () - 2];
16778 struct field &upper = range_fields[range_fields.size () - 1];
16779
16780 lower.set_type (underlying);
16781 FIELD_ARTIFICIAL (lower) = 1;
16782
16783 upper.set_type (underlying);
16784 FIELD_ARTIFICIAL (upper) = 1;
16785
16786 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16787 &bounds_offset, &lower, cu)
16788 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16789 &bounds_offset, &upper, cu))
16790 return nullptr;
16791 }
16792 }
16793
16794 /* This shouldn't really happen, but double-check that we found
16795 where the bounds are stored. */
16796 if (bounds_offset == -1)
16797 return nullptr;
16798
16799 struct objfile *objfile = cu->per_objfile->objfile;
16800 for (int i = 0; i < range_fields.size (); i += 2)
16801 {
16802 char name[20];
16803
16804 /* Set the name of each field in the bounds. */
16805 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16806 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16807 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16808 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16809 }
16810
16811 struct type *bounds = alloc_type (objfile);
16812 bounds->set_code (TYPE_CODE_STRUCT);
16813
16814 bounds->set_num_fields (range_fields.size ());
16815 bounds->set_fields
16816 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16817 * sizeof (struct field))));
16818 memcpy (bounds->fields (), range_fields.data (),
16819 bounds->num_fields () * sizeof (struct field));
16820
16821 int last_fieldno = range_fields.size () - 1;
16822 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16823 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16824 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16825
16826 /* Rewrite the existing array type in place. Specifically, we
16827 remove any dynamic properties we might have read, and we replace
16828 the index types. */
16829 struct type *iter = type;
16830 for (int i = 0; i < range_fields.size (); i += 2)
16831 {
16832 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16833 iter->main_type->dyn_prop_list = nullptr;
16834 iter->set_index_type
16835 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16836 iter = TYPE_TARGET_TYPE (iter);
16837 }
16838
16839 struct type *result = alloc_type (objfile);
16840 result->set_code (TYPE_CODE_STRUCT);
16841
16842 result->set_num_fields (2);
16843 result->set_fields
16844 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16845 * sizeof (struct field))));
16846
16847 /* The names are chosen to coincide with what the compiler does with
16848 -fgnat-encodings=all, which the Ada code in gdb already
16849 understands. */
16850 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16851 result->field (0).set_type (lookup_pointer_type (type));
16852
16853 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16854 result->field (1).set_type (lookup_pointer_type (bounds));
16855 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16856
16857 result->set_name (type->name ());
16858 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16859 + TYPE_LENGTH (result->field (1).type ()));
16860
16861 return result;
16862}
16863
c906108c
SS
16864/* Extract all information from a DW_TAG_array_type DIE and put it in
16865 the DIE's type field. For now, this only handles one dimensional
16866 arrays. */
16867
f792889a 16868static struct type *
e7c27a73 16869read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16870{
5e22e966 16871 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16872 struct die_info *child_die;
7e314c57 16873 struct type *type;
c906108c 16874 struct type *element_type, *range_type, *index_type;
c906108c 16875 struct attribute *attr;
15d034d0 16876 const char *name;
a405673c 16877 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16878 unsigned int bit_stride = 0;
c906108c 16879
e7c27a73 16880 element_type = die_type (die, cu);
c906108c 16881
7e314c57
JK
16882 /* The die_type call above may have already set the type for this DIE. */
16883 type = get_die_type (die, cu);
16884 if (type)
16885 return type;
16886
dc53a7ad
JB
16887 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16888 if (attr != NULL)
a405673c
JB
16889 {
16890 int stride_ok;
293e7e51 16891 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16892
16893 byte_stride_prop
16894 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16895 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16896 prop_type);
a405673c
JB
16897 if (!stride_ok)
16898 {
b98664d3 16899 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16900 " - DIE at %s [in module %s]"),
16901 sect_offset_str (die->sect_off),
5e22e966 16902 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16903 /* Ignore this attribute. We will likely not be able to print
16904 arrays of this type correctly, but there is little we can do
16905 to help if we cannot read the attribute's value. */
16906 byte_stride_prop = NULL;
16907 }
16908 }
dc53a7ad
JB
16909
16910 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16911 if (attr != NULL)
529908cb 16912 bit_stride = attr->constant_value (0);
dc53a7ad 16913
c906108c
SS
16914 /* Irix 6.2 native cc creates array types without children for
16915 arrays with unspecified length. */
639d11d3 16916 if (die->child == NULL)
c906108c 16917 {
46bf5051 16918 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16919 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16920 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16921 byte_stride_prop, bit_stride);
f792889a 16922 return set_die_type (die, type, cu);
c906108c
SS
16923 }
16924
791afaa2 16925 std::vector<struct type *> range_types;
639d11d3 16926 child_die = die->child;
c906108c
SS
16927 while (child_die && child_die->tag)
16928 {
16929 if (child_die->tag == DW_TAG_subrange_type)
16930 {
f792889a 16931 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16932
dda83cd7
SM
16933 if (child_type != NULL)
16934 {
0963b4bd 16935 /* The range type was succesfully read. Save it for the
dda83cd7 16936 array type creation. */
791afaa2 16937 range_types.push_back (child_type);
dda83cd7 16938 }
c906108c 16939 }
436c571c 16940 child_die = child_die->sibling;
c906108c
SS
16941 }
16942
cf2b2075
TV
16943 if (range_types.empty ())
16944 {
16945 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16946 sect_offset_str (die->sect_off),
16947 objfile_name (cu->per_objfile->objfile));
16948 return NULL;
16949 }
16950
c906108c
SS
16951 /* Dwarf2 dimensions are output from left to right, create the
16952 necessary array types in backwards order. */
7ca2d3a3 16953
c906108c 16954 type = element_type;
7ca2d3a3
DL
16955
16956 if (read_array_order (die, cu) == DW_ORD_col_major)
16957 {
16958 int i = 0;
9a619af0 16959
791afaa2 16960 while (i < range_types.size ())
10f6a3ad
TT
16961 {
16962 type = create_array_type_with_stride (NULL, type, range_types[i++],
16963 byte_stride_prop, bit_stride);
16964 bit_stride = 0;
16965 byte_stride_prop = nullptr;
16966 }
7ca2d3a3
DL
16967 }
16968 else
16969 {
791afaa2 16970 size_t ndim = range_types.size ();
7ca2d3a3 16971 while (ndim-- > 0)
10f6a3ad
TT
16972 {
16973 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16974 byte_stride_prop, bit_stride);
16975 bit_stride = 0;
16976 byte_stride_prop = nullptr;
16977 }
7ca2d3a3 16978 }
c906108c 16979
cf2b2075
TV
16980 gdb_assert (type != element_type);
16981
f5f8a009
EZ
16982 /* Understand Dwarf2 support for vector types (like they occur on
16983 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16984 array type. This is not part of the Dwarf2/3 standard yet, but a
16985 custom vendor extension. The main difference between a regular
16986 array and the vector variant is that vectors are passed by value
16987 to functions. */
e142c38c 16988 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16989 if (attr != nullptr)
ea37ba09 16990 make_vector_type (type);
f5f8a009 16991
dbc98a8b
KW
16992 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16993 implementation may choose to implement triple vectors using this
16994 attribute. */
16995 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 16996 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 16997 {
529908cb
TT
16998 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16999 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 17000 else
b98664d3 17001 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 17002 "than the total size of elements"));
dbc98a8b
KW
17003 }
17004
39cbfefa
DJ
17005 name = dwarf2_name (die, cu);
17006 if (name)
d0e39ea2 17007 type->set_name (name);
6e70227d 17008
2b4424c3
TT
17009 maybe_set_alignment (cu, die, type);
17010
57567375
TT
17011 struct type *replacement_type = nullptr;
17012 if (cu->language == language_ada)
17013 {
17014 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17015 if (replacement_type != nullptr)
17016 type = replacement_type;
17017 }
17018
0963b4bd 17019 /* Install the type in the die. */
57567375 17020 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
17021
17022 /* set_die_type should be already done. */
b4ba55a1
JB
17023 set_descriptive_type (type, die, cu);
17024
7e314c57 17025 return type;
c906108c
SS
17026}
17027
7ca2d3a3 17028static enum dwarf_array_dim_ordering
6e70227d 17029read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17030{
17031 struct attribute *attr;
17032
17033 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17034
435d3d88 17035 if (attr != nullptr)
1bc397c5
TT
17036 {
17037 LONGEST val = attr->constant_value (-1);
17038 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17039 return (enum dwarf_array_dim_ordering) val;
17040 }
7ca2d3a3 17041
0963b4bd
MS
17042 /* GNU F77 is a special case, as at 08/2004 array type info is the
17043 opposite order to the dwarf2 specification, but data is still
17044 laid out as per normal fortran.
7ca2d3a3 17045
0963b4bd
MS
17046 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17047 version checking. */
7ca2d3a3 17048
905e0470
PM
17049 if (cu->language == language_fortran
17050 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17051 {
17052 return DW_ORD_row_major;
17053 }
17054
3a3440fb 17055 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
17056 {
17057 case array_column_major:
17058 return DW_ORD_col_major;
17059 case array_row_major:
17060 default:
17061 return DW_ORD_row_major;
17062 };
17063}
17064
72019c9c 17065/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17066 the DIE's type field. */
72019c9c 17067
f792889a 17068static struct type *
72019c9c
GM
17069read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17070{
7e314c57
JK
17071 struct type *domain_type, *set_type;
17072 struct attribute *attr;
f792889a 17073
7e314c57
JK
17074 domain_type = die_type (die, cu);
17075
17076 /* The die_type call above may have already set the type for this DIE. */
17077 set_type = get_die_type (die, cu);
17078 if (set_type)
17079 return set_type;
17080
17081 set_type = create_set_type (NULL, domain_type);
17082
17083 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17084 if (attr != nullptr && attr->form_is_unsigned ())
17085 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17086
2b4424c3
TT
17087 maybe_set_alignment (cu, die, set_type);
17088
f792889a 17089 return set_die_type (die, set_type, cu);
72019c9c 17090}
7ca2d3a3 17091
0971de02
TT
17092/* A helper for read_common_block that creates a locexpr baton.
17093 SYM is the symbol which we are marking as computed.
17094 COMMON_DIE is the DIE for the common block.
17095 COMMON_LOC is the location expression attribute for the common
17096 block itself.
17097 MEMBER_LOC is the location expression attribute for the particular
17098 member of the common block that we are processing.
17099 CU is the CU from which the above come. */
17100
17101static void
17102mark_common_block_symbol_computed (struct symbol *sym,
17103 struct die_info *common_die,
17104 struct attribute *common_loc,
17105 struct attribute *member_loc,
17106 struct dwarf2_cu *cu)
17107{
5e22e966 17108 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17109 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17110 struct dwarf2_locexpr_baton *baton;
17111 gdb_byte *ptr;
17112 unsigned int cu_off;
08feed99 17113 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17114 LONGEST offset = 0;
17115
17116 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17117 gdb_assert (common_loc->form_is_block ());
17118 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17119 || member_loc->form_is_constant ());
0971de02 17120
8d749320 17121 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17122 baton->per_objfile = per_objfile;
0971de02
TT
17123 baton->per_cu = cu->per_cu;
17124 gdb_assert (baton->per_cu);
17125
17126 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17127
cd6c91b4 17128 if (member_loc->form_is_constant ())
0971de02 17129 {
0826b30a 17130 offset = member_loc->constant_value (0);
0971de02
TT
17131 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17132 }
17133 else
9d2246fc 17134 baton->size += member_loc->as_block ()->size;
0971de02 17135
224c3ddb 17136 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17137 baton->data = ptr;
17138
17139 *ptr++ = DW_OP_call4;
9c541725 17140 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17141 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17142 ptr += 4;
17143
cd6c91b4 17144 if (member_loc->form_is_constant ())
0971de02
TT
17145 {
17146 *ptr++ = DW_OP_addr;
17147 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17148 ptr += cu->header.addr_size;
17149 }
17150 else
17151 {
17152 /* We have to copy the data here, because DW_OP_call4 will only
17153 use a DW_AT_location attribute. */
9d2246fc
TT
17154 struct dwarf_block *block = member_loc->as_block ();
17155 memcpy (ptr, block->data, block->size);
17156 ptr += block->size;
0971de02
TT
17157 }
17158
17159 *ptr++ = DW_OP_plus;
17160 gdb_assert (ptr - baton->data == baton->size);
17161
0971de02 17162 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17163 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17164}
17165
4357ac6c
TT
17166/* Create appropriate locally-scoped variables for all the
17167 DW_TAG_common_block entries. Also create a struct common_block
17168 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17169 is used to separate the common blocks name namespace from regular
4357ac6c 17170 variable names. */
c906108c
SS
17171
17172static void
e7c27a73 17173read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17174{
0971de02
TT
17175 struct attribute *attr;
17176
17177 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17178 if (attr != nullptr)
0971de02
TT
17179 {
17180 /* Support the .debug_loc offsets. */
4fc6c0d5 17181 if (attr->form_is_block ())
dda83cd7 17182 {
0971de02 17183 /* Ok. */
dda83cd7 17184 }
cd6c91b4 17185 else if (attr->form_is_section_offset ())
dda83cd7 17186 {
0971de02
TT
17187 dwarf2_complex_location_expr_complaint ();
17188 attr = NULL;
dda83cd7 17189 }
0971de02 17190 else
dda83cd7 17191 {
0971de02
TT
17192 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17193 "common block member");
17194 attr = NULL;
dda83cd7 17195 }
0971de02
TT
17196 }
17197
639d11d3 17198 if (die->child != NULL)
c906108c 17199 {
5e22e966 17200 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17201 struct die_info *child_die;
17202 size_t n_entries = 0, size;
17203 struct common_block *common_block;
17204 struct symbol *sym;
74ac6d43 17205
4357ac6c
TT
17206 for (child_die = die->child;
17207 child_die && child_die->tag;
436c571c 17208 child_die = child_die->sibling)
4357ac6c
TT
17209 ++n_entries;
17210
17211 size = (sizeof (struct common_block)
17212 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17213 common_block
17214 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17215 size);
4357ac6c
TT
17216 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17217 common_block->n_entries = 0;
17218
17219 for (child_die = die->child;
17220 child_die && child_die->tag;
436c571c 17221 child_die = child_die->sibling)
4357ac6c
TT
17222 {
17223 /* Create the symbol in the DW_TAG_common_block block in the current
17224 symbol scope. */
e7c27a73 17225 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17226 if (sym != NULL)
17227 {
17228 struct attribute *member_loc;
17229
17230 common_block->contents[common_block->n_entries++] = sym;
17231
17232 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17233 cu);
17234 if (member_loc)
17235 {
17236 /* GDB has handled this for a long time, but it is
17237 not specified by DWARF. It seems to have been
17238 emitted by gfortran at least as recently as:
17239 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17240 complaint (_("Variable in common block has "
0971de02 17241 "DW_AT_data_member_location "
9d8780f0
SM
17242 "- DIE at %s [in module %s]"),
17243 sect_offset_str (child_die->sect_off),
518817b3 17244 objfile_name (objfile));
0971de02 17245
cd6c91b4 17246 if (member_loc->form_is_section_offset ())
0971de02 17247 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17248 else if (member_loc->form_is_constant ()
4fc6c0d5 17249 || member_loc->form_is_block ())
0971de02 17250 {
435d3d88 17251 if (attr != nullptr)
0971de02
TT
17252 mark_common_block_symbol_computed (sym, die, attr,
17253 member_loc, cu);
17254 }
17255 else
17256 dwarf2_complex_location_expr_complaint ();
17257 }
17258 }
c906108c 17259 }
4357ac6c
TT
17260
17261 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17262 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17263 }
17264}
17265
0114d602 17266/* Create a type for a C++ namespace. */
d9fa45fe 17267
0114d602
DJ
17268static struct type *
17269read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17270{
5e22e966 17271 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17272 const char *previous_prefix, *name;
9219021c 17273 int is_anonymous;
0114d602
DJ
17274 struct type *type;
17275
17276 /* For extensions, reuse the type of the original namespace. */
17277 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17278 {
17279 struct die_info *ext_die;
17280 struct dwarf2_cu *ext_cu = cu;
9a619af0 17281
0114d602
DJ
17282 ext_die = dwarf2_extension (die, &ext_cu);
17283 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17284
17285 /* EXT_CU may not be the same as CU.
02142a6c 17286 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17287 return set_die_type (die, type, cu);
17288 }
9219021c 17289
e142c38c 17290 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17291
17292 /* Now build the name of the current namespace. */
17293
0114d602
DJ
17294 previous_prefix = determine_prefix (die, cu);
17295 if (previous_prefix[0] != '\0')
17296 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17297 previous_prefix, name, 0, cu);
0114d602
DJ
17298
17299 /* Create the type. */
19f392bc 17300 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17301
60531b24 17302 return set_die_type (die, type, cu);
0114d602
DJ
17303}
17304
22cee43f 17305/* Read a namespace scope. */
0114d602
DJ
17306
17307static void
17308read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17309{
5e22e966 17310 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17311 int is_anonymous;
9219021c 17312
5c4e30ca
DC
17313 /* Add a symbol associated to this if we haven't seen the namespace
17314 before. Also, add a using directive if it's an anonymous
17315 namespace. */
9219021c 17316
f2f0e013 17317 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17318 {
17319 struct type *type;
17320
0114d602 17321 type = read_type_die (die, cu);
e7c27a73 17322 new_symbol (die, type, cu);
5c4e30ca 17323
e8e80198 17324 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17325 if (is_anonymous)
0114d602
DJ
17326 {
17327 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17328
eb1e02fd 17329 std::vector<const char *> excludes;
804d2729 17330 add_using_directive (using_directives (cu),
7d93a1e0 17331 previous_prefix, type->name (), NULL,
eb1e02fd 17332 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17333 }
5c4e30ca 17334 }
9219021c 17335
639d11d3 17336 if (die->child != NULL)
d9fa45fe 17337 {
639d11d3 17338 struct die_info *child_die = die->child;
6e70227d 17339
d9fa45fe
DC
17340 while (child_die && child_die->tag)
17341 {
e7c27a73 17342 process_die (child_die, cu);
436c571c 17343 child_die = child_die->sibling;
d9fa45fe
DC
17344 }
17345 }
38d518c9
EZ
17346}
17347
f55ee35c
JK
17348/* Read a Fortran module as type. This DIE can be only a declaration used for
17349 imported module. Still we need that type as local Fortran "use ... only"
17350 declaration imports depend on the created type in determine_prefix. */
17351
17352static struct type *
17353read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17354{
5e22e966 17355 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17356 const char *module_name;
f55ee35c
JK
17357 struct type *type;
17358
17359 module_name = dwarf2_name (die, cu);
19f392bc 17360 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17361
f55ee35c
JK
17362 return set_die_type (die, type, cu);
17363}
17364
5d7cb8df
JK
17365/* Read a Fortran module. */
17366
17367static void
17368read_module (struct die_info *die, struct dwarf2_cu *cu)
17369{
17370 struct die_info *child_die = die->child;
530e8392
KB
17371 struct type *type;
17372
17373 type = read_type_die (die, cu);
17374 new_symbol (die, type, cu);
5d7cb8df 17375
5d7cb8df
JK
17376 while (child_die && child_die->tag)
17377 {
17378 process_die (child_die, cu);
436c571c 17379 child_die = child_die->sibling;
5d7cb8df
JK
17380 }
17381}
17382
38d518c9
EZ
17383/* Return the name of the namespace represented by DIE. Set
17384 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17385 namespace. */
17386
17387static const char *
e142c38c 17388namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17389{
17390 struct die_info *current_die;
17391 const char *name = NULL;
17392
17393 /* Loop through the extensions until we find a name. */
17394
17395 for (current_die = die;
17396 current_die != NULL;
f2f0e013 17397 current_die = dwarf2_extension (die, &cu))
38d518c9 17398 {
96553a0c
DE
17399 /* We don't use dwarf2_name here so that we can detect the absence
17400 of a name -> anonymous namespace. */
7d45c7c3 17401 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17402
38d518c9
EZ
17403 if (name != NULL)
17404 break;
17405 }
17406
17407 /* Is it an anonymous namespace? */
17408
17409 *is_anonymous = (name == NULL);
17410 if (*is_anonymous)
2b1dbab0 17411 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17412
17413 return name;
d9fa45fe
DC
17414}
17415
c906108c
SS
17416/* Extract all information from a DW_TAG_pointer_type DIE and add to
17417 the user defined type vector. */
17418
f792889a 17419static struct type *
e7c27a73 17420read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17421{
5e22e966 17422 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17423 struct comp_unit_head *cu_header = &cu->header;
c906108c 17424 struct type *type;
8b2dbe47
KB
17425 struct attribute *attr_byte_size;
17426 struct attribute *attr_address_class;
17427 int byte_size, addr_class;
7e314c57
JK
17428 struct type *target_type;
17429
17430 target_type = die_type (die, cu);
c906108c 17431
7e314c57
JK
17432 /* The die_type call above may have already set the type for this DIE. */
17433 type = get_die_type (die, cu);
17434 if (type)
17435 return type;
17436
17437 type = lookup_pointer_type (target_type);
8b2dbe47 17438
e142c38c 17439 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17440 if (attr_byte_size)
529908cb 17441 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17442 else
8b2dbe47
KB
17443 byte_size = cu_header->addr_size;
17444
e142c38c 17445 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17446 if (attr_address_class)
529908cb 17447 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17448 else
17449 addr_class = DW_ADDR_none;
17450
2b4424c3
TT
17451 ULONGEST alignment = get_alignment (cu, die);
17452
17453 /* If the pointer size, alignment, or address class is different
17454 than the default, create a type variant marked as such and set
17455 the length accordingly. */
17456 if (TYPE_LENGTH (type) != byte_size
17457 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17458 && alignment != TYPE_RAW_ALIGN (type))
17459 || addr_class != DW_ADDR_none)
c906108c 17460 {
5e2b427d 17461 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17462 {
314ad88d
PA
17463 type_instance_flags type_flags
17464 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17465 addr_class);
876cecd0
TT
17466 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17467 == 0);
8b2dbe47
KB
17468 type = make_type_with_address_space (type, type_flags);
17469 }
17470 else if (TYPE_LENGTH (type) != byte_size)
17471 {
b98664d3 17472 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17473 }
2b4424c3
TT
17474 else if (TYPE_RAW_ALIGN (type) != alignment)
17475 {
b98664d3 17476 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17477 " - DIE at %s [in module %s]"),
17478 sect_offset_str (die->sect_off),
5e22e966 17479 objfile_name (cu->per_objfile->objfile));
2b4424c3 17480 }
6e70227d 17481 else
9a619af0
MS
17482 {
17483 /* Should we also complain about unhandled address classes? */
17484 }
c906108c 17485 }
8b2dbe47
KB
17486
17487 TYPE_LENGTH (type) = byte_size;
2b4424c3 17488 set_type_align (type, alignment);
f792889a 17489 return set_die_type (die, type, cu);
c906108c
SS
17490}
17491
17492/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17493 the user defined type vector. */
17494
f792889a 17495static struct type *
e7c27a73 17496read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17497{
17498 struct type *type;
17499 struct type *to_type;
17500 struct type *domain;
17501
e7c27a73
DJ
17502 to_type = die_type (die, cu);
17503 domain = die_containing_type (die, cu);
0d5de010 17504
7e314c57
JK
17505 /* The calls above may have already set the type for this DIE. */
17506 type = get_die_type (die, cu);
17507 if (type)
17508 return type;
17509
78134374 17510 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17511 type = lookup_methodptr_type (to_type);
78134374 17512 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17513 {
5e22e966 17514 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17515
17516 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17517 to_type->fields (), to_type->num_fields (),
a409645d 17518 to_type->has_varargs ());
7078baeb
TT
17519 type = lookup_methodptr_type (new_type);
17520 }
0d5de010
DJ
17521 else
17522 type = lookup_memberptr_type (to_type, domain);
c906108c 17523
f792889a 17524 return set_die_type (die, type, cu);
c906108c
SS
17525}
17526
4297a3f0 17527/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17528 the user defined type vector. */
17529
f792889a 17530static struct type *
4297a3f0 17531read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17532 enum type_code refcode)
c906108c 17533{
e7c27a73 17534 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17535 struct type *type, *target_type;
c906108c
SS
17536 struct attribute *attr;
17537
4297a3f0
AV
17538 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17539
7e314c57
JK
17540 target_type = die_type (die, cu);
17541
17542 /* The die_type call above may have already set the type for this DIE. */
17543 type = get_die_type (die, cu);
17544 if (type)
17545 return type;
17546
4297a3f0 17547 type = lookup_reference_type (target_type, refcode);
e142c38c 17548 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17549 if (attr != nullptr)
c906108c 17550 {
529908cb 17551 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17552 }
17553 else
17554 {
107d2387 17555 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17556 }
2b4424c3 17557 maybe_set_alignment (cu, die, type);
f792889a 17558 return set_die_type (die, type, cu);
c906108c
SS
17559}
17560
cf363f18
MW
17561/* Add the given cv-qualifiers to the element type of the array. GCC
17562 outputs DWARF type qualifiers that apply to an array, not the
17563 element type. But GDB relies on the array element type to carry
17564 the cv-qualifiers. This mimics section 6.7.3 of the C99
17565 specification. */
17566
17567static struct type *
17568add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17569 struct type *base_type, int cnst, int voltl)
17570{
17571 struct type *el_type, *inner_array;
17572
17573 base_type = copy_type (base_type);
17574 inner_array = base_type;
17575
78134374 17576 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17577 {
17578 TYPE_TARGET_TYPE (inner_array) =
17579 copy_type (TYPE_TARGET_TYPE (inner_array));
17580 inner_array = TYPE_TARGET_TYPE (inner_array);
17581 }
17582
17583 el_type = TYPE_TARGET_TYPE (inner_array);
17584 cnst |= TYPE_CONST (el_type);
17585 voltl |= TYPE_VOLATILE (el_type);
17586 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17587
17588 return set_die_type (die, base_type, cu);
17589}
17590
f792889a 17591static struct type *
e7c27a73 17592read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17593{
f792889a 17594 struct type *base_type, *cv_type;
c906108c 17595
e7c27a73 17596 base_type = die_type (die, cu);
7e314c57
JK
17597
17598 /* The die_type call above may have already set the type for this DIE. */
17599 cv_type = get_die_type (die, cu);
17600 if (cv_type)
17601 return cv_type;
17602
2f608a3a
KW
17603 /* In case the const qualifier is applied to an array type, the element type
17604 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17605 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17606 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17607
f792889a
DJ
17608 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17609 return set_die_type (die, cv_type, cu);
c906108c
SS
17610}
17611
f792889a 17612static struct type *
e7c27a73 17613read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17614{
f792889a 17615 struct type *base_type, *cv_type;
c906108c 17616
e7c27a73 17617 base_type = die_type (die, cu);
7e314c57
JK
17618
17619 /* The die_type call above may have already set the type for this DIE. */
17620 cv_type = get_die_type (die, cu);
17621 if (cv_type)
17622 return cv_type;
17623
cf363f18
MW
17624 /* In case the volatile qualifier is applied to an array type, the
17625 element type is so qualified, not the array type (section 6.7.3
17626 of C99). */
78134374 17627 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17628 return add_array_cv_type (die, cu, base_type, 0, 1);
17629
f792889a
DJ
17630 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17631 return set_die_type (die, cv_type, cu);
c906108c
SS
17632}
17633
06d66ee9
TT
17634/* Handle DW_TAG_restrict_type. */
17635
17636static struct type *
17637read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17638{
17639 struct type *base_type, *cv_type;
17640
17641 base_type = die_type (die, cu);
17642
17643 /* The die_type call above may have already set the type for this DIE. */
17644 cv_type = get_die_type (die, cu);
17645 if (cv_type)
17646 return cv_type;
17647
17648 cv_type = make_restrict_type (base_type);
17649 return set_die_type (die, cv_type, cu);
17650}
17651
a2c2acaf
MW
17652/* Handle DW_TAG_atomic_type. */
17653
17654static struct type *
17655read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17656{
17657 struct type *base_type, *cv_type;
17658
17659 base_type = die_type (die, cu);
17660
17661 /* The die_type call above may have already set the type for this DIE. */
17662 cv_type = get_die_type (die, cu);
17663 if (cv_type)
17664 return cv_type;
17665
17666 cv_type = make_atomic_type (base_type);
17667 return set_die_type (die, cv_type, cu);
17668}
17669
c906108c
SS
17670/* Extract all information from a DW_TAG_string_type DIE and add to
17671 the user defined type vector. It isn't really a user defined type,
17672 but it behaves like one, with other DIE's using an AT_user_def_type
17673 attribute to reference it. */
17674
f792889a 17675static struct type *
e7c27a73 17676read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17677{
5e22e966 17678 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17679 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17680 struct type *type, *range_type, *index_type, *char_type;
17681 struct attribute *attr;
216a7e6b
AB
17682 struct dynamic_prop prop;
17683 bool length_is_constant = true;
17684 LONGEST length;
17685
17686 /* There are a couple of places where bit sizes might be made use of
17687 when parsing a DW_TAG_string_type, however, no producer that we know
17688 of make use of these. Handling bit sizes that are a multiple of the
17689 byte size is easy enough, but what about other bit sizes? Lets deal
17690 with that problem when we have to. Warn about these attributes being
17691 unsupported, then parse the type and ignore them like we always
17692 have. */
17693 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17694 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17695 {
17696 static bool warning_printed = false;
17697 if (!warning_printed)
17698 {
17699 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17700 "currently supported on DW_TAG_string_type."));
17701 warning_printed = true;
17702 }
17703 }
c906108c 17704
e142c38c 17705 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17706 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17707 {
17708 /* The string length describes the location at which the length of
17709 the string can be found. The size of the length field can be
17710 specified with one of the attributes below. */
17711 struct type *prop_type;
17712 struct attribute *len
17713 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17714 if (len == nullptr)
17715 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17716 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17717 {
17718 /* Pass 0 as the default as we know this attribute is constant
17719 and the default value will not be returned. */
0826b30a 17720 LONGEST sz = len->constant_value (0);
293e7e51 17721 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17722 }
17723 else
17724 {
17725 /* If the size is not specified then we assume it is the size of
17726 an address on this target. */
293e7e51 17727 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17728 }
17729
17730 /* Convert the attribute into a dynamic property. */
17731 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17732 length = 1;
17733 else
17734 length_is_constant = false;
17735 }
17736 else if (attr != nullptr)
17737 {
17738 /* This DW_AT_string_length just contains the length with no
17739 indirection. There's no need to create a dynamic property in this
17740 case. Pass 0 for the default value as we know it will not be
17741 returned in this case. */
0826b30a 17742 length = attr->constant_value (0);
216a7e6b
AB
17743 }
17744 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17745 {
216a7e6b 17746 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17747 length = attr->constant_value (1);
c906108c
SS
17748 }
17749 else
17750 {
216a7e6b
AB
17751 /* Use 1 as a fallback length if we have nothing else. */
17752 length = 1;
c906108c 17753 }
6ccb9162 17754
46bf5051 17755 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17756 if (length_is_constant)
17757 range_type = create_static_range_type (NULL, index_type, 1, length);
17758 else
17759 {
17760 struct dynamic_prop low_bound;
17761
8c2e4e06 17762 low_bound.set_const_val (1);
216a7e6b
AB
17763 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17764 }
3b7538c0
UW
17765 char_type = language_string_char_type (cu->language_defn, gdbarch);
17766 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17767
f792889a 17768 return set_die_type (die, type, cu);
c906108c
SS
17769}
17770
4d804846
JB
17771/* Assuming that DIE corresponds to a function, returns nonzero
17772 if the function is prototyped. */
17773
17774static int
17775prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17776{
17777 struct attribute *attr;
17778
17779 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17780 if (attr && attr->as_boolean ())
4d804846
JB
17781 return 1;
17782
17783 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17784 is only meaningful for C, but the concept also extends to other
4d804846
JB
17785 languages that allow unprototyped functions (Eg: Objective C).
17786 For all other languages, assume that functions are always
17787 prototyped. */
17788 if (cu->language != language_c
17789 && cu->language != language_objc
17790 && cu->language != language_opencl)
17791 return 1;
17792
17793 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17794 prototyped and unprototyped functions; default to prototyped,
17795 since that is more common in modern code (and RealView warns
17796 about unprototyped functions). */
17797 if (producer_is_realview (cu->producer))
17798 return 1;
17799
17800 return 0;
17801}
17802
c906108c
SS
17803/* Handle DIES due to C code like:
17804
17805 struct foo
c5aa993b
JM
17806 {
17807 int (*funcp)(int a, long l);
17808 int b;
17809 };
c906108c 17810
0963b4bd 17811 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17812
f792889a 17813static struct type *
e7c27a73 17814read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17815{
5e22e966 17816 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17817 struct type *type; /* Type that this function returns. */
17818 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17819 struct attribute *attr;
17820
e7c27a73 17821 type = die_type (die, cu);
7e314c57
JK
17822
17823 /* The die_type call above may have already set the type for this DIE. */
17824 ftype = get_die_type (die, cu);
17825 if (ftype)
17826 return ftype;
17827
0c8b41f1 17828 ftype = lookup_function_type (type);
c906108c 17829
4d804846 17830 if (prototyped_function_p (die, cu))
27e69b7a 17831 ftype->set_is_prototyped (true);
c906108c 17832
c055b101
CV
17833 /* Store the calling convention in the type if it's available in
17834 the subroutine die. Otherwise set the calling convention to
17835 the default value DW_CC_normal. */
17836 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17837 if (attr != nullptr
529908cb 17838 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17839 TYPE_CALLING_CONVENTION (ftype)
529908cb 17840 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17841 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17842 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17843 else
17844 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17845
743649fd
MW
17846 /* Record whether the function returns normally to its caller or not
17847 if the DWARF producer set that information. */
17848 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17849 if (attr && attr->as_boolean ())
743649fd
MW
17850 TYPE_NO_RETURN (ftype) = 1;
17851
76c10ea2
GM
17852 /* We need to add the subroutine type to the die immediately so
17853 we don't infinitely recurse when dealing with parameters
0963b4bd 17854 declared as the same subroutine type. */
76c10ea2 17855 set_die_type (die, ftype, cu);
6e70227d 17856
639d11d3 17857 if (die->child != NULL)
c906108c 17858 {
bb5ed363 17859 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17860 struct die_info *child_die;
8072405b 17861 int nparams, iparams;
c906108c
SS
17862
17863 /* Count the number of parameters.
dda83cd7
SM
17864 FIXME: GDB currently ignores vararg functions, but knows about
17865 vararg member functions. */
8072405b 17866 nparams = 0;
639d11d3 17867 child_die = die->child;
c906108c
SS
17868 while (child_die && child_die->tag)
17869 {
17870 if (child_die->tag == DW_TAG_formal_parameter)
17871 nparams++;
17872 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17873 ftype->set_has_varargs (true);
17874
436c571c 17875 child_die = child_die->sibling;
c906108c
SS
17876 }
17877
17878 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17879 ftype->set_num_fields (nparams);
3cabb6b0
SM
17880 ftype->set_fields
17881 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17882
8072405b
JK
17883 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17884 even if we error out during the parameters reading below. */
17885 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17886 ftype->field (iparams).set_type (void_type);
8072405b
JK
17887
17888 iparams = 0;
639d11d3 17889 child_die = die->child;
c906108c
SS
17890 while (child_die && child_die->tag)
17891 {
17892 if (child_die->tag == DW_TAG_formal_parameter)
17893 {
3ce3b1ba
PA
17894 struct type *arg_type;
17895
17896 /* DWARF version 2 has no clean way to discern C++
17897 static and non-static member functions. G++ helps
17898 GDB by marking the first parameter for non-static
17899 member functions (which is the this pointer) as
17900 artificial. We pass this information to
17901 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17902
17903 DWARF version 3 added DW_AT_object_pointer, which GCC
17904 4.5 does not yet generate. */
e142c38c 17905 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17906 if (attr != nullptr)
c45bc3f8 17907 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 17908 else
9c37b5ae 17909 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17910 arg_type = die_type (child_die, cu);
17911
17912 /* RealView does not mark THIS as const, which the testsuite
17913 expects. GCC marks THIS as const in method definitions,
17914 but not in the class specifications (GCC PR 43053). */
17915 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17916 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17917 {
17918 int is_this = 0;
17919 struct dwarf2_cu *arg_cu = cu;
17920 const char *name = dwarf2_name (child_die, cu);
17921
17922 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17923 if (attr != nullptr)
3ce3b1ba
PA
17924 {
17925 /* If the compiler emits this, use it. */
17926 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17927 is_this = 1;
17928 }
17929 else if (name && strcmp (name, "this") == 0)
17930 /* Function definitions will have the argument names. */
17931 is_this = 1;
17932 else if (name == NULL && iparams == 0)
17933 /* Declarations may not have the names, so like
17934 elsewhere in GDB, assume an artificial first
17935 argument is "this". */
17936 is_this = 1;
17937
17938 if (is_this)
17939 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17940 arg_type, 0);
17941 }
17942
5d14b6e5 17943 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17944 iparams++;
17945 }
436c571c 17946 child_die = child_die->sibling;
c906108c
SS
17947 }
17948 }
17949
76c10ea2 17950 return ftype;
c906108c
SS
17951}
17952
f792889a 17953static struct type *
e7c27a73 17954read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17955{
5e22e966 17956 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17957 const char *name = NULL;
3c8e0968 17958 struct type *this_type, *target_type;
c906108c 17959
94af9270 17960 name = dwarf2_full_name (NULL, die, cu);
19f392bc 17961 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 17962 this_type->set_target_is_stub (true);
f792889a 17963 set_die_type (die, this_type, cu);
3c8e0968
DE
17964 target_type = die_type (die, cu);
17965 if (target_type != this_type)
17966 TYPE_TARGET_TYPE (this_type) = target_type;
17967 else
17968 {
17969 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17970 spec and cause infinite loops in GDB. */
b98664d3 17971 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17972 "- DIE at %s [in module %s]"),
17973 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17974 TYPE_TARGET_TYPE (this_type) = NULL;
17975 }
e4003a34
TV
17976 if (name == NULL)
17977 {
17978 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17979 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17980 Handle these by just returning the target type, rather than
17981 constructing an anonymous typedef type and trying to handle this
17982 elsewhere. */
17983 set_die_type (die, target_type, cu);
17984 return target_type;
17985 }
f792889a 17986 return this_type;
c906108c
SS
17987}
17988
a625a8c9
TT
17989/* Helper for get_dwarf2_rational_constant that computes the value of
17990 a given gmp_mpz given an attribute. */
17991
17992static void
17993get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17994{
17995 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17996 location expression that pushes an implicit value. */
17997 if (attr->form == DW_FORM_exprloc)
17998 {
17999 dwarf_block *blk = attr->as_block ();
18000 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18001 {
18002 uint64_t len;
18003 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18004 blk->data + blk->size,
18005 &len);
18006 if (ptr - blk->data + len <= blk->size)
18007 {
18008 mpz_import (value->val, len,
18009 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18010 1, 0, 0, ptr);
18011 return;
18012 }
18013 }
18014
18015 /* On failure set it to 1. */
18016 *value = gdb_mpz (1);
18017 }
18018 else if (attr->form_is_block ())
18019 {
18020 dwarf_block *blk = attr->as_block ();
18021 mpz_import (value->val, blk->size,
18022 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18023 1, 0, 0, blk->data);
18024 }
18025 else
18026 *value = gdb_mpz (attr->constant_value (1));
18027}
18028
09584414
JB
18029/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18030 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18031
18032 If the numerator and/or numerator attribute is missing,
18033 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18034 untouched. */
18035
18036static void
18037get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 18038 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
18039{
18040 struct attribute *num_attr, *denom_attr;
18041
18042 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18043 if (num_attr == nullptr)
18044 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18045 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18046
18047 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18048 if (denom_attr == nullptr)
18049 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18050 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18051
18052 if (num_attr == nullptr || denom_attr == nullptr)
18053 return;
18054
a625a8c9
TT
18055 get_mpz (cu, numerator, num_attr);
18056 get_mpz (cu, denominator, denom_attr);
09584414
JB
18057}
18058
18059/* Same as get_dwarf2_rational_constant, but extracting an unsigned
18060 rational constant, rather than a signed one.
18061
18062 If the rational constant has a negative value, a complaint
18063 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18064
18065static void
18066get_dwarf2_unsigned_rational_constant (struct die_info *die,
18067 struct dwarf2_cu *cu,
5cde1d82
TT
18068 gdb_mpz *numerator,
18069 gdb_mpz *denominator)
09584414 18070{
5cde1d82
TT
18071 gdb_mpz num (1);
18072 gdb_mpz denom (1);
09584414
JB
18073
18074 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 18075 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 18076 {
5cde1d82
TT
18077 mpz_neg (num.val, num.val);
18078 mpz_neg (denom.val, denom.val);
09584414 18079 }
5cde1d82 18080 else if (mpz_sgn (num.val) == -1)
09584414
JB
18081 {
18082 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18083 " in DIE at %s"),
18084 sect_offset_str (die->sect_off));
18085 return;
18086 }
5cde1d82 18087 else if (mpz_sgn (denom.val) == -1)
09584414
JB
18088 {
18089 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18090 " in DIE at %s"),
18091 sect_offset_str (die->sect_off));
18092 return;
18093 }
18094
5cde1d82
TT
18095 *numerator = std::move (num);
18096 *denominator = std::move (denom);
09584414
JB
18097}
18098
bbcdf9ab
TT
18099/* Assuming that ENCODING is a string whose contents starting at the
18100 K'th character is "_nn" where "nn" is a decimal number, scan that
18101 number and set RESULT to the value. K is updated to point to the
18102 character immediately following the number.
18103
18104 If the string does not conform to the format described above, false
18105 is returned, and K may or may not be changed. */
18106
18107static bool
18108ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18109{
18110 /* The next character should be an underscore ('_') followed
18111 by a digit. */
18112 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18113 return false;
18114
18115 /* Skip the underscore. */
18116 k++;
18117 int start = k;
18118
18119 /* Determine the number of digits for our number. */
18120 while (isdigit (encoding[k]))
18121 k++;
18122 if (k == start)
18123 return false;
18124
18125 std::string copy (&encoding[start], k - start);
18126 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18127 return false;
18128
18129 return true;
18130}
18131
18132/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18133 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18134 DENOM, update OFFSET, and return true on success. Return false on
18135 failure. */
18136
18137static bool
18138ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18139 gdb_mpz *num, gdb_mpz *denom)
18140{
18141 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18142 return false;
18143 return ada_get_gnat_encoded_number (encoding, offset, denom);
18144}
18145
09584414 18146/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
18147 of the corresponding TYPE by setting its type-specific data. CU is
18148 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18149 encodings. It is nullptr if the GNAT encoding should be
18150 ignored. */
09584414
JB
18151
18152static void
bbcdf9ab
TT
18153finish_fixed_point_type (struct type *type, const char *suffix,
18154 struct die_info *die, struct dwarf2_cu *cu)
09584414 18155{
09584414
JB
18156 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18157 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18158
bbcdf9ab
TT
18159 /* If GNAT encodings are preferred, don't examine the
18160 attributes. */
18161 struct attribute *attr = nullptr;
18162 if (suffix == nullptr)
18163 {
18164 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18165 if (attr == nullptr)
18166 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18167 if (attr == nullptr)
18168 attr = dwarf2_attr (die, DW_AT_small, cu);
18169 }
09584414 18170
5cde1d82
TT
18171 /* Numerator and denominator of our fixed-point type's scaling factor.
18172 The default is a scaling factor of 1, which we use as a fallback
18173 when we are not able to decode it (problem with the debugging info,
18174 unsupported forms, bug in GDB, etc...). Using that as the default
18175 allows us to at least print the unscaled value, which might still
18176 be useful to a user. */
18177 gdb_mpz scale_num (1);
18178 gdb_mpz scale_denom (1);
18179
09584414
JB
18180 if (attr == nullptr)
18181 {
bbcdf9ab
TT
18182 int offset = 0;
18183 if (suffix != nullptr
18184 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18185 &scale_denom)
18186 /* The number might be encoded as _nn_dd_nn_dd, where the
18187 second ratio is the 'small value. In this situation, we
18188 want the second value. */
18189 && (suffix[offset] != '_'
18190 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18191 &scale_denom)))
18192 {
18193 /* Found it. */
18194 }
18195 else
18196 {
18197 /* Scaling factor not found. Assume a scaling factor of 1,
18198 and hope for the best. At least the user will be able to
18199 see the encoded value. */
18200 scale_num = 1;
18201 scale_denom = 1;
18202 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18203 sect_offset_str (die->sect_off));
18204 }
09584414
JB
18205 }
18206 else if (attr->name == DW_AT_binary_scale)
18207 {
18208 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18209 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18210
5cde1d82 18211 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18212 }
18213 else if (attr->name == DW_AT_decimal_scale)
18214 {
18215 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18216 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18217
5cde1d82 18218 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18219 }
18220 else if (attr->name == DW_AT_small)
18221 {
18222 struct die_info *scale_die;
18223 struct dwarf2_cu *scale_cu = cu;
18224
18225 scale_die = follow_die_ref (die, attr, &scale_cu);
18226 if (scale_die->tag == DW_TAG_constant)
18227 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18228 &scale_num, &scale_denom);
18229 else
18230 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18231 " (DIE at %s)"),
18232 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18233 }
18234 else
18235 {
18236 complaint (_("unsupported scale attribute %s for fixed-point type"
18237 " (DIE at %s)"),
18238 dwarf_attr_name (attr->name),
18239 sect_offset_str (die->sect_off));
18240 }
18241
2a12c336 18242 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18243 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18244 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18245 mpq_canonicalize (scaling_factor.val);
18246}
18247
bbcdf9ab
TT
18248/* The gnat-encoding suffix for fixed point. */
18249
18250#define GNAT_FIXED_POINT_SUFFIX "___XF_"
18251
18252/* If NAME encodes an Ada fixed-point type, return a pointer to the
18253 "XF" suffix of the name. The text after this is what encodes the
18254 'small and 'delta information. Otherwise, return nullptr. */
18255
18256static const char *
18257gnat_encoded_fixed_point_type_info (const char *name)
18258{
18259 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18260}
18261
9b790ce7
UW
18262/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18263 (which may be different from NAME) to the architecture back-end to allow
18264 it to guess the correct format if necessary. */
18265
18266static struct type *
18267dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18268 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18269{
08feed99 18270 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18271 const struct floatformat **format;
18272 struct type *type;
18273
18274 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18275 if (format)
103a685e 18276 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18277 else
77b7c781 18278 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18279
18280 return type;
18281}
18282
eb77c9df
AB
18283/* Allocate an integer type of size BITS and name NAME. */
18284
18285static struct type *
18286dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18287 int bits, int unsigned_p, const char *name)
18288{
18289 struct type *type;
18290
18291 /* Versions of Intel's C Compiler generate an integer type called "void"
18292 instead of using DW_TAG_unspecified_type. This has been seen on
18293 at least versions 14, 17, and 18. */
35ee2dc2
AB
18294 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18295 && strcmp (name, "void") == 0)
eb77c9df
AB
18296 type = objfile_type (objfile)->builtin_void;
18297 else
18298 type = init_integer_type (objfile, bits, unsigned_p, name);
18299
18300 return type;
18301}
18302
09584414
JB
18303/* Return true if DIE has a DW_AT_small attribute whose value is
18304 a constant rational, where both the numerator and denominator
18305 are equal to zero.
18306
18307 CU is the DIE's Compilation Unit. */
18308
18309static bool
18310has_zero_over_zero_small_attribute (struct die_info *die,
18311 struct dwarf2_cu *cu)
18312{
18313 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18314 if (attr == nullptr)
18315 return false;
18316
18317 struct dwarf2_cu *scale_cu = cu;
18318 struct die_info *scale_die
18319 = follow_die_ref (die, attr, &scale_cu);
18320
18321 if (scale_die->tag != DW_TAG_constant)
18322 return false;
18323
5cde1d82 18324 gdb_mpz num (1), denom (1);
09584414 18325 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18326 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18327}
18328
8bdc1658
AB
18329/* Initialise and return a floating point type of size BITS suitable for
18330 use as a component of a complex number. The NAME_HINT is passed through
18331 when initialising the floating point type and is the name of the complex
18332 type.
18333
18334 As DWARF doesn't currently provide an explicit name for the components
18335 of a complex number, but it can be helpful to have these components
18336 named, we try to select a suitable name based on the size of the
18337 component. */
18338static struct type *
18339dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18340 struct objfile *objfile,
103a685e
TT
18341 int bits, const char *name_hint,
18342 enum bfd_endian byte_order)
8bdc1658 18343{
08feed99 18344 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18345 struct type *tt = nullptr;
18346
35add35e
AB
18347 /* Try to find a suitable floating point builtin type of size BITS.
18348 We're going to use the name of this type as the name for the complex
18349 target type that we are about to create. */
1db455a7 18350 switch (cu->language)
8bdc1658 18351 {
1db455a7
AB
18352 case language_fortran:
18353 switch (bits)
18354 {
18355 case 32:
18356 tt = builtin_f_type (gdbarch)->builtin_real;
18357 break;
18358 case 64:
18359 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18360 break;
18361 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18362 case 128:
18363 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18364 break;
18365 }
8bdc1658 18366 break;
1db455a7
AB
18367 default:
18368 switch (bits)
18369 {
18370 case 32:
18371 tt = builtin_type (gdbarch)->builtin_float;
18372 break;
18373 case 64:
18374 tt = builtin_type (gdbarch)->builtin_double;
18375 break;
18376 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18377 case 128:
18378 tt = builtin_type (gdbarch)->builtin_long_double;
18379 break;
18380 }
8bdc1658
AB
18381 break;
18382 }
18383
35add35e
AB
18384 /* If the type we found doesn't match the size we were looking for, then
18385 pretend we didn't find a type at all, the complex target type we
18386 create will then be nameless. */
a12e5744 18387 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18388 tt = nullptr;
18389
7d93a1e0 18390 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18391 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18392}
18393
c906108c
SS
18394/* Find a representation of a given base type and install
18395 it in the TYPE field of the die. */
18396
f792889a 18397static struct type *
e7c27a73 18398read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18399{
5e22e966 18400 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18401 struct type *type;
18402 struct attribute *attr;
19f392bc 18403 int encoding = 0, bits = 0;
15d034d0 18404 const char *name;
34877895 18405 gdbarch *arch;
c906108c 18406
e142c38c 18407 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18408 if (attr != nullptr && attr->form_is_constant ())
18409 encoding = attr->constant_value (0);
e142c38c 18410 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18411 if (attr != nullptr)
529908cb 18412 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18413 name = dwarf2_name (die, cu);
6ccb9162 18414 if (!name)
34877895 18415 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18416
08feed99 18417 arch = objfile->arch ();
103a685e
TT
18418 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18419
34877895 18420 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18421 if (attr != nullptr && attr->form_is_constant ())
103a685e 18422 {
529908cb 18423 int endianity = attr->constant_value (0);
103a685e
TT
18424
18425 switch (endianity)
18426 {
18427 case DW_END_big:
18428 byte_order = BFD_ENDIAN_BIG;
18429 break;
18430 case DW_END_little:
18431 byte_order = BFD_ENDIAN_LITTLE;
18432 break;
18433 default:
18434 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18435 break;
18436 }
18437 }
6ccb9162 18438
09584414
JB
18439 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18440 && cu->language == language_ada
18441 && has_zero_over_zero_small_attribute (die, cu))
18442 {
18443 /* brobecker/2018-02-24: This is a fixed point type for which
18444 the scaling factor is represented as fraction whose value
18445 does not make sense (zero divided by zero), so we should
18446 normally never see these. However, there is a small category
18447 of fixed point types for which GNAT is unable to provide
18448 the scaling factor via the standard DWARF mechanisms, and
18449 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18450 This is likely what this DIE is about. */
09584414
JB
18451 encoding = (encoding == DW_ATE_signed_fixed
18452 ? DW_ATE_signed
18453 : DW_ATE_unsigned);
18454 }
18455
bbcdf9ab
TT
18456 /* With GNAT encodings, fixed-point information will be encoded in
18457 the type name. Note that this can also occur with the above
18458 zero-over-zero case, which is why this is a separate "if" rather
18459 than an "else if". */
18460 const char *gnat_encoding_suffix = nullptr;
18461 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18462 && cu->language == language_ada
18463 && name != nullptr)
18464 {
18465 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18466 if (gnat_encoding_suffix != nullptr)
18467 {
18468 gdb_assert (startswith (gnat_encoding_suffix,
18469 GNAT_FIXED_POINT_SUFFIX));
18470 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18471 name, gnat_encoding_suffix - name);
18472 /* Use -1 here so that SUFFIX points at the "_" after the
18473 "XF". */
18474 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18475
18476 encoding = (encoding == DW_ATE_signed
18477 ? DW_ATE_signed_fixed
18478 : DW_ATE_unsigned_fixed);
18479 }
18480 }
18481
6ccb9162 18482 switch (encoding)
c906108c 18483 {
6ccb9162
UW
18484 case DW_ATE_address:
18485 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18486 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18487 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18488 break;
18489 case DW_ATE_boolean:
19f392bc 18490 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18491 break;
18492 case DW_ATE_complex_float:
103a685e
TT
18493 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18494 byte_order);
78134374 18495 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18496 {
18497 if (name == nullptr)
18498 {
18499 struct obstack *obstack
5e22e966 18500 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18501 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18502 nullptr);
18503 }
18504 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18505 }
18506 else
18507 type = init_complex_type (name, type);
6ccb9162
UW
18508 break;
18509 case DW_ATE_decimal_float:
19f392bc 18510 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18511 break;
18512 case DW_ATE_float:
103a685e 18513 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18514 break;
18515 case DW_ATE_signed:
eb77c9df 18516 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18517 break;
18518 case DW_ATE_unsigned:
3b2b8fea
TT
18519 if (cu->language == language_fortran
18520 && name
61012eef 18521 && startswith (name, "character("))
19f392bc
UW
18522 type = init_character_type (objfile, bits, 1, name);
18523 else
eb77c9df 18524 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18525 break;
18526 case DW_ATE_signed_char:
6e70227d 18527 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18528 || cu->language == language_pascal
18529 || cu->language == language_fortran)
19f392bc
UW
18530 type = init_character_type (objfile, bits, 0, name);
18531 else
eb77c9df 18532 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18533 break;
18534 case DW_ATE_unsigned_char:
868a0084 18535 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18536 || cu->language == language_pascal
c44af4eb
TT
18537 || cu->language == language_fortran
18538 || cu->language == language_rust)
19f392bc
UW
18539 type = init_character_type (objfile, bits, 1, name);
18540 else
eb77c9df 18541 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18542 break;
75079b2b 18543 case DW_ATE_UTF:
53e710ac 18544 {
53e710ac
PA
18545 if (bits == 16)
18546 type = builtin_type (arch)->builtin_char16;
18547 else if (bits == 32)
18548 type = builtin_type (arch)->builtin_char32;
18549 else
18550 {
b98664d3 18551 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18552 bits);
eb77c9df 18553 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18554 }
18555 return set_die_type (die, type, cu);
18556 }
75079b2b 18557 break;
09584414
JB
18558 case DW_ATE_signed_fixed:
18559 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18560 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18561 break;
18562 case DW_ATE_unsigned_fixed:
18563 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18564 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18565 break;
75079b2b 18566
6ccb9162 18567 default:
b98664d3 18568 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18569 dwarf_type_encoding_name (encoding));
77b7c781 18570 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18571 break;
c906108c 18572 }
6ccb9162 18573
0114d602 18574 if (name && strcmp (name, "char") == 0)
15152a54 18575 type->set_has_no_signedness (true);
0114d602 18576
2b4424c3
TT
18577 maybe_set_alignment (cu, die, type);
18578
db558e34 18579 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18580
20a5fcbd
TT
18581 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18582 {
18583 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18584 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18585 {
529908cb 18586 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18587 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18588 /* Only use the attributes if they make sense together. */
18589 if (attr == nullptr
529908cb
TT
18590 || (attr->as_unsigned () + real_bit_size
18591 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18592 {
18593 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18594 = real_bit_size;
18595 if (attr != nullptr)
18596 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18597 = attr->as_unsigned ();
20a5fcbd
TT
18598 }
18599 }
18600 }
18601
f792889a 18602 return set_die_type (die, type, cu);
c906108c
SS
18603}
18604
80180f79
SA
18605/* Parse dwarf attribute if it's a block, reference or constant and put the
18606 resulting value of the attribute into struct bound_prop.
18607 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18608
18609static int
18610attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18611 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18612 struct type *default_type)
80180f79
SA
18613{
18614 struct dwarf2_property_baton *baton;
5e22e966 18615 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18616 struct objfile *objfile = per_objfile->objfile;
18617 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18618
9a49df9d
AB
18619 gdb_assert (default_type != NULL);
18620
80180f79
SA
18621 if (attr == NULL || prop == NULL)
18622 return 0;
18623
4fc6c0d5 18624 if (attr->form_is_block ())
80180f79 18625 {
8d749320 18626 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18627 baton->property_type = default_type;
80180f79 18628 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18629 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18630
18631 struct dwarf_block *block = attr->as_block ();
18632 baton->locexpr.size = block->size;
18633 baton->locexpr.data = block->data;
216a7e6b
AB
18634 switch (attr->name)
18635 {
18636 case DW_AT_string_length:
18637 baton->locexpr.is_reference = true;
18638 break;
18639 default:
18640 baton->locexpr.is_reference = false;
18641 break;
18642 }
8c2e4e06
SM
18643
18644 prop->set_locexpr (baton);
18645 gdb_assert (prop->baton () != NULL);
80180f79 18646 }
cd6c91b4 18647 else if (attr->form_is_ref ())
80180f79
SA
18648 {
18649 struct dwarf2_cu *target_cu = cu;
18650 struct die_info *target_die;
18651 struct attribute *target_attr;
18652
18653 target_die = follow_die_ref (die, attr, &target_cu);
18654 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18655 if (target_attr == NULL)
18656 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18657 target_cu);
80180f79
SA
18658 if (target_attr == NULL)
18659 return 0;
18660
df25ebbd 18661 switch (target_attr->name)
80180f79 18662 {
df25ebbd 18663 case DW_AT_location:
cd6c91b4 18664 if (target_attr->form_is_section_offset ())
df25ebbd 18665 {
8d749320 18666 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18667 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18668 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18669 prop->set_loclist (baton);
18670 gdb_assert (prop->baton () != NULL);
df25ebbd 18671 }
4fc6c0d5 18672 else if (target_attr->form_is_block ())
df25ebbd 18673 {
8d749320 18674 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18675 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18676 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18677 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18678 struct dwarf_block *block = target_attr->as_block ();
18679 baton->locexpr.size = block->size;
18680 baton->locexpr.data = block->data;
9a49df9d 18681 baton->locexpr.is_reference = true;
8c2e4e06
SM
18682 prop->set_locexpr (baton);
18683 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18684 }
18685 else
18686 {
18687 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18688 "dynamic property");
18689 return 0;
18690 }
18691 break;
18692 case DW_AT_data_member_location:
18693 {
18694 LONGEST offset;
18695
18696 if (!handle_data_member_location (target_die, target_cu,
18697 &offset))
18698 return 0;
18699
8d749320 18700 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18701 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18702 target_cu);
df25ebbd
JB
18703 baton->offset_info.offset = offset;
18704 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18705 prop->set_addr_offset (baton);
df25ebbd
JB
18706 break;
18707 }
80180f79
SA
18708 }
18709 }
cd6c91b4 18710 else if (attr->form_is_constant ())
8c2e4e06 18711 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18712 else
18713 {
18714 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18715 dwarf2_name (die, cu));
18716 return 0;
18717 }
18718
18719 return 1;
18720}
18721
09ba997f 18722/* See read.h. */
9a49df9d 18723
09ba997f 18724struct type *
293e7e51 18725dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18726{
9a49df9d
AB
18727 struct type *int_type;
18728
18729 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18730#define TRY_TYPE(F) \
18731 int_type = (unsigned_p \
18732 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18733 : objfile_type (objfile)->builtin_ ## F); \
18734 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18735 return int_type
18736
18737 TRY_TYPE (char);
18738 TRY_TYPE (short);
18739 TRY_TYPE (int);
18740 TRY_TYPE (long);
18741 TRY_TYPE (long_long);
18742
18743#undef TRY_TYPE
18744
18745 gdb_assert_not_reached ("unable to find suitable integer type");
18746}
18747
09ba997f 18748/* See read.h. */
11a8b164 18749
09ba997f 18750struct type *
293e7e51 18751dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18752{
293e7e51
SM
18753 int addr_size = this->per_cu->addr_size ();
18754 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18755}
18756
b86352cf
AB
18757/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18758 present (which is valid) then compute the default type based on the
18759 compilation units address size. */
18760
18761static struct type *
18762read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18763{
18764 struct type *index_type = die_type (die, cu);
18765
18766 /* Dwarf-2 specifications explicitly allows to create subrange types
18767 without specifying a base type.
18768 In that case, the base type must be set to the type of
18769 the lower bound, upper bound or count, in that order, if any of these
18770 three attributes references an object that has a type.
18771 If no base type is found, the Dwarf-2 specifications say that
18772 a signed integer type of size equal to the size of an address should
18773 be used.
18774 For the following C code: `extern char gdb_int [];'
18775 GCC produces an empty range DIE.
18776 FIXME: muller/2010-05-28: Possible references to object for low bound,
18777 high bound or count are not yet handled by this code. */
78134374 18778 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18779 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18780
18781 return index_type;
18782}
18783
a02abb62
JB
18784/* Read the given DW_AT_subrange DIE. */
18785
f792889a 18786static struct type *
a02abb62
JB
18787read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18788{
4c9ad8c2 18789 struct type *base_type, *orig_base_type;
a02abb62
JB
18790 struct type *range_type;
18791 struct attribute *attr;
729efb13 18792 struct dynamic_prop low, high;
4fae6e18 18793 int low_default_is_valid;
c451ebe5 18794 int high_bound_is_count = 0;
15d034d0 18795 const char *name;
d359392f 18796 ULONGEST negative_mask;
e77813c8 18797
b86352cf
AB
18798 orig_base_type = read_subrange_index_type (die, cu);
18799
4c9ad8c2
TT
18800 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18801 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18802 creating the range type, but we use the result of check_typedef
18803 when examining properties of the type. */
18804 base_type = check_typedef (orig_base_type);
a02abb62 18805
7e314c57
JK
18806 /* The die_type call above may have already set the type for this DIE. */
18807 range_type = get_die_type (die, cu);
18808 if (range_type)
18809 return range_type;
18810
8c2e4e06 18811 high.set_const_val (0);
729efb13 18812
4fae6e18
JK
18813 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18814 omitting DW_AT_lower_bound. */
18815 switch (cu->language)
6e70227d 18816 {
4fae6e18
JK
18817 case language_c:
18818 case language_cplus:
8c2e4e06 18819 low.set_const_val (0);
4fae6e18
JK
18820 low_default_is_valid = 1;
18821 break;
18822 case language_fortran:
8c2e4e06 18823 low.set_const_val (1);
4fae6e18
JK
18824 low_default_is_valid = 1;
18825 break;
18826 case language_d:
4fae6e18 18827 case language_objc:
c44af4eb 18828 case language_rust:
8c2e4e06 18829 low.set_const_val (0);
4fae6e18
JK
18830 low_default_is_valid = (cu->header.version >= 4);
18831 break;
18832 case language_ada:
18833 case language_m2:
18834 case language_pascal:
8c2e4e06 18835 low.set_const_val (1);
4fae6e18
JK
18836 low_default_is_valid = (cu->header.version >= 4);
18837 break;
18838 default:
8c2e4e06 18839 low.set_const_val (0);
4fae6e18
JK
18840 low_default_is_valid = 0;
18841 break;
a02abb62
JB
18842 }
18843
e142c38c 18844 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18845 if (attr != nullptr)
9a49df9d 18846 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18847 else if (!low_default_is_valid)
b98664d3 18848 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18849 "- DIE at %s [in module %s]"),
18850 sect_offset_str (die->sect_off),
5e22e966 18851 objfile_name (cu->per_objfile->objfile));
a02abb62 18852
506f5c41
TV
18853 struct attribute *attr_ub, *attr_count;
18854 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18855 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18856 {
506f5c41 18857 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18858 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18859 {
c451ebe5 18860 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18861 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18862 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18863 else
18864 high_bound_is_count = 1;
c2ff108b 18865 }
506f5c41
TV
18866 else
18867 {
18868 if (attr_ub != NULL)
18869 complaint (_("Unresolved DW_AT_upper_bound "
18870 "- DIE at %s [in module %s]"),
18871 sect_offset_str (die->sect_off),
5e22e966 18872 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18873 if (attr_count != NULL)
18874 complaint (_("Unresolved DW_AT_count "
18875 "- DIE at %s [in module %s]"),
18876 sect_offset_str (die->sect_off),
5e22e966 18877 objfile_name (cu->per_objfile->objfile));
506f5c41 18878 }
e77813c8 18879 }
a02abb62 18880
4e962e74
TT
18881 LONGEST bias = 0;
18882 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18883 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18884 bias = bias_attr->constant_value (0);
4e962e74 18885
dbb9c2b1
JB
18886 /* Normally, the DWARF producers are expected to use a signed
18887 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18888 But this is unfortunately not always the case, as witnessed
18889 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18890 is used instead. To work around that ambiguity, we treat
18891 the bounds as signed, and thus sign-extend their values, when
18892 the base type is signed. */
6e70227d 18893 negative_mask =
d359392f 18894 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18895 if (low.kind () == PROP_CONST
c6d940a9 18896 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18897 low.set_const_val (low.const_val () | negative_mask);
18898 if (high.kind () == PROP_CONST
c6d940a9 18899 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18900 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18901
5bbd8269
AB
18902 /* Check for bit and byte strides. */
18903 struct dynamic_prop byte_stride_prop;
18904 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18905 if (attr_byte_stride != nullptr)
18906 {
293e7e51 18907 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18908 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18909 prop_type);
18910 }
18911
18912 struct dynamic_prop bit_stride_prop;
18913 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18914 if (attr_bit_stride != nullptr)
18915 {
18916 /* It only makes sense to have either a bit or byte stride. */
18917 if (attr_byte_stride != nullptr)
18918 {
18919 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18920 "- DIE at %s [in module %s]"),
18921 sect_offset_str (die->sect_off),
5e22e966 18922 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18923 attr_bit_stride = nullptr;
18924 }
18925 else
18926 {
293e7e51 18927 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18928 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18929 prop_type);
18930 }
18931 }
18932
18933 if (attr_byte_stride != nullptr
18934 || attr_bit_stride != nullptr)
18935 {
18936 bool byte_stride_p = (attr_byte_stride != nullptr);
18937 struct dynamic_prop *stride
18938 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18939
18940 range_type
18941 = create_range_type_with_stride (NULL, orig_base_type, &low,
18942 &high, bias, stride, byte_stride_p);
18943 }
18944 else
18945 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18946
c451ebe5 18947 if (high_bound_is_count)
599088e3 18948 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18949
c2ff108b
JK
18950 /* Ada expects an empty array on no boundary attributes. */
18951 if (attr == NULL && cu->language != language_ada)
8c2e4e06 18952 range_type->bounds ()->high.set_undefined ();
c2ff108b 18953
39cbfefa
DJ
18954 name = dwarf2_name (die, cu);
18955 if (name)
d0e39ea2 18956 range_type->set_name (name);
6e70227d 18957
e142c38c 18958 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18959 if (attr != nullptr)
529908cb 18960 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 18961
2b4424c3
TT
18962 maybe_set_alignment (cu, die, range_type);
18963
7e314c57
JK
18964 set_die_type (die, range_type, cu);
18965
18966 /* set_die_type should be already done. */
b4ba55a1
JB
18967 set_descriptive_type (range_type, die, cu);
18968
7e314c57 18969 return range_type;
a02abb62 18970}
6e70227d 18971
f792889a 18972static struct type *
81a17f79
JB
18973read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18974{
18975 struct type *type;
81a17f79 18976
5e22e966 18977 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18978 type->set_name (dwarf2_name (die, cu));
81a17f79 18979
74a2f8ff 18980 /* In Ada, an unspecified type is typically used when the description
85102364 18981 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18982 such a type, we treat it as a stub, and try to resolve it later on,
18983 when needed. */
18984 if (cu->language == language_ada)
b4b73759 18985 type->set_is_stub (true);
74a2f8ff 18986
f792889a 18987 return set_die_type (die, type, cu);
81a17f79 18988}
a02abb62 18989
639d11d3
DC
18990/* Read a single die and all its descendents. Set the die's sibling
18991 field to NULL; set other fields in the die correctly, and set all
18992 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18993 location of the info_ptr after reading all of those dies. PARENT
18994 is the parent of the die in question. */
18995
18996static struct die_info *
dee91e82 18997read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18998 const gdb_byte *info_ptr,
18999 const gdb_byte **new_info_ptr,
dee91e82 19000 struct die_info *parent)
639d11d3
DC
19001{
19002 struct die_info *die;
d521ce57 19003 const gdb_byte *cur_ptr;
639d11d3 19004
3e225074 19005 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
19006 if (die == NULL)
19007 {
19008 *new_info_ptr = cur_ptr;
19009 return NULL;
19010 }
93311388 19011 store_in_ref_table (die, reader->cu);
639d11d3 19012
3e225074 19013 if (die->has_children)
bf6af496 19014 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
19015 else
19016 {
19017 die->child = NULL;
19018 *new_info_ptr = cur_ptr;
19019 }
19020
19021 die->sibling = NULL;
19022 die->parent = parent;
19023 return die;
19024}
19025
19026/* Read a die, all of its descendents, and all of its siblings; set
19027 all of the fields of all of the dies correctly. Arguments are as
19028 in read_die_and_children. */
19029
19030static struct die_info *
bf6af496 19031read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
19032 const gdb_byte *info_ptr,
19033 const gdb_byte **new_info_ptr,
bf6af496 19034 struct die_info *parent)
639d11d3
DC
19035{
19036 struct die_info *first_die, *last_sibling;
d521ce57 19037 const gdb_byte *cur_ptr;
639d11d3 19038
c906108c 19039 cur_ptr = info_ptr;
639d11d3
DC
19040 first_die = last_sibling = NULL;
19041
19042 while (1)
c906108c 19043 {
639d11d3 19044 struct die_info *die
dee91e82 19045 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 19046
1d325ec1 19047 if (die == NULL)
c906108c 19048 {
639d11d3
DC
19049 *new_info_ptr = cur_ptr;
19050 return first_die;
c906108c 19051 }
1d325ec1
DJ
19052
19053 if (!first_die)
19054 first_die = die;
c906108c 19055 else
1d325ec1
DJ
19056 last_sibling->sibling = die;
19057
19058 last_sibling = die;
c906108c 19059 }
c906108c
SS
19060}
19061
bf6af496
DE
19062/* Read a die, all of its descendents, and all of its siblings; set
19063 all of the fields of all of the dies correctly. Arguments are as
19064 in read_die_and_children.
19065 This the main entry point for reading a DIE and all its children. */
19066
19067static struct die_info *
19068read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
19069 const gdb_byte *info_ptr,
19070 const gdb_byte **new_info_ptr,
bf6af496
DE
19071 struct die_info *parent)
19072{
19073 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19074 new_info_ptr, parent);
19075
b4f54984 19076 if (dwarf_die_debug)
bf6af496
DE
19077 {
19078 fprintf_unfiltered (gdb_stdlog,
19079 "Read die from %s@0x%x of %s:\n",
96b79293 19080 reader->die_section->get_name (),
bf6af496
DE
19081 (unsigned) (info_ptr - reader->die_section->buffer),
19082 bfd_get_filename (reader->abfd));
b4f54984 19083 dump_die (die, dwarf_die_debug);
bf6af496
DE
19084 }
19085
19086 return die;
19087}
19088
3019eac3
DE
19089/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19090 attributes.
19091 The caller is responsible for filling in the extra attributes
19092 and updating (*DIEP)->num_attrs.
19093 Set DIEP to point to a newly allocated die with its information,
3e225074 19094 except for its child, sibling, and parent fields. */
93311388 19095
d521ce57 19096static const gdb_byte *
3019eac3 19097read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19098 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19099 int num_extra_attrs)
93311388 19100{
b64f50a1 19101 unsigned int abbrev_number, bytes_read, i;
7c32eebb 19102 const struct abbrev_info *abbrev;
93311388
DE
19103 struct die_info *die;
19104 struct dwarf2_cu *cu = reader->cu;
19105 bfd *abfd = reader->abfd;
19106
9c541725 19107 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19108 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19109 info_ptr += bytes_read;
19110 if (!abbrev_number)
19111 {
19112 *diep = NULL;
93311388
DE
19113 return info_ptr;
19114 }
19115
685af9cd 19116 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19117 if (!abbrev)
348e048f
DE
19118 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19119 abbrev_number,
19120 bfd_get_filename (abfd));
19121
3019eac3 19122 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19123 die->sect_off = sect_off;
93311388
DE
19124 die->tag = abbrev->tag;
19125 die->abbrev = abbrev_number;
3e225074 19126 die->has_children = abbrev->has_children;
93311388 19127
3019eac3
DE
19128 /* Make the result usable.
19129 The caller needs to update num_attrs after adding the extra
19130 attributes. */
93311388
DE
19131 die->num_attrs = abbrev->num_attrs;
19132
7a5f294d 19133 bool any_need_reprocess = false;
93311388 19134 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19135 {
7a5f294d
TT
19136 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19137 info_ptr);
19138 if (die->attrs[i].requires_reprocessing_p ())
19139 any_need_reprocess = true;
18a8505e
AT
19140 }
19141
052c8bb8 19142 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19143 if (attr != nullptr && attr->form_is_unsigned ())
19144 cu->str_offsets_base = attr->as_unsigned ();
93311388 19145
41144253 19146 attr = die->attr (DW_AT_loclists_base);
19147 if (attr != nullptr)
529908cb 19148 cu->loclist_base = attr->as_unsigned ();
41144253 19149
a39fdb41 19150 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19151 if (maybe_addr_base.has_value ())
19152 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19153
19154 attr = die->attr (DW_AT_rnglists_base);
19155 if (attr != nullptr)
2b0c7f41 19156 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 19157
7a5f294d
TT
19158 if (any_need_reprocess)
19159 {
19160 for (i = 0; i < abbrev->num_attrs; ++i)
19161 {
19162 if (die->attrs[i].requires_reprocessing_p ())
19163 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19164 }
19165 }
93311388 19166 *diep = die;
93311388
DE
19167 return info_ptr;
19168}
19169
3019eac3
DE
19170/* Read a die and all its attributes.
19171 Set DIEP to point to a newly allocated die with its information,
3e225074 19172 except for its child, sibling, and parent fields. */
3019eac3 19173
d521ce57 19174static const gdb_byte *
3019eac3 19175read_full_die (const struct die_reader_specs *reader,
3e225074 19176 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19177{
d521ce57 19178 const gdb_byte *result;
bf6af496 19179
3e225074 19180 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19181
b4f54984 19182 if (dwarf_die_debug)
bf6af496
DE
19183 {
19184 fprintf_unfiltered (gdb_stdlog,
19185 "Read die from %s@0x%x of %s:\n",
96b79293 19186 reader->die_section->get_name (),
bf6af496
DE
19187 (unsigned) (info_ptr - reader->die_section->buffer),
19188 bfd_get_filename (reader->abfd));
b4f54984 19189 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19190 }
19191
19192 return result;
3019eac3 19193}
433df2d4 19194\f
c906108c 19195
72bf9492
DJ
19196/* Returns nonzero if TAG represents a type that we might generate a partial
19197 symbol for. */
19198
19199static int
d8f62e84 19200is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19201{
19202 switch (tag)
19203 {
19204#if 0
19205 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19206 that we don't at present. Note that normally this does not
19207 matter, mainly because C compilers don't give names to these
19208 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19209 case DW_TAG_file_type:
19210 case DW_TAG_ptr_to_member_type:
19211 case DW_TAG_set_type:
19212 case DW_TAG_string_type:
19213 case DW_TAG_subroutine_type:
19214#endif
d8f62e84
TT
19215
19216 /* GNAT may emit an array with a name, but no typedef, so we
19217 need to make a symbol in this case. */
19218 case DW_TAG_array_type:
19219 return lang == language_ada;
19220
72bf9492
DJ
19221 case DW_TAG_base_type:
19222 case DW_TAG_class_type:
680b30c7 19223 case DW_TAG_interface_type:
72bf9492
DJ
19224 case DW_TAG_enumeration_type:
19225 case DW_TAG_structure_type:
19226 case DW_TAG_subrange_type:
19227 case DW_TAG_typedef:
19228 case DW_TAG_union_type:
19229 return 1;
19230 default:
19231 return 0;
19232 }
19233}
19234
19235/* Load all DIEs that are interesting for partial symbols into memory. */
19236
19237static struct partial_die_info *
dee91e82 19238load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19239 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19240{
dee91e82 19241 struct dwarf2_cu *cu = reader->cu;
5e22e966 19242 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19243 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19244 unsigned int bytes_read;
5afb4e99 19245 unsigned int load_all = 0;
72bf9492
DJ
19246 int nesting_level = 1;
19247
19248 parent_die = NULL;
19249 last_die = NULL;
19250
7adf1e79
DE
19251 gdb_assert (cu->per_cu != NULL);
19252 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19253 load_all = 1;
19254
72bf9492
DJ
19255 cu->partial_dies
19256 = htab_create_alloc_ex (cu->header.length / 12,
19257 partial_die_hash,
19258 partial_die_eq,
19259 NULL,
19260 &cu->comp_unit_obstack,
19261 hashtab_obstack_allocate,
19262 dummy_obstack_deallocate);
19263
72bf9492
DJ
19264 while (1)
19265 {
7c32eebb
TT
19266 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19267 &bytes_read);
72bf9492
DJ
19268
19269 /* A NULL abbrev means the end of a series of children. */
19270 if (abbrev == NULL)
19271 {
19272 if (--nesting_level == 0)
cd9983dd
YQ
19273 return first_die;
19274
72bf9492
DJ
19275 info_ptr += bytes_read;
19276 last_die = parent_die;
19277 parent_die = parent_die->die_parent;
19278 continue;
19279 }
19280
98bfdba5
PA
19281 /* Check for template arguments. We never save these; if
19282 they're seen, we just mark the parent, and go on our way. */
19283 if (parent_die != NULL
19284 && cu->language == language_cplus
19285 && (abbrev->tag == DW_TAG_template_type_param
19286 || abbrev->tag == DW_TAG_template_value_param))
19287 {
19288 parent_die->has_template_arguments = 1;
19289
19290 if (!load_all)
19291 {
19292 /* We don't need a partial DIE for the template argument. */
dee91e82 19293 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19294 continue;
19295 }
19296 }
19297
0d99eb77 19298 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19299 Skip their other children. */
19300 if (!load_all
19301 && cu->language == language_cplus
19302 && parent_die != NULL
f9b5d5ea
TV
19303 && parent_die->tag == DW_TAG_subprogram
19304 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19305 {
dee91e82 19306 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19307 continue;
19308 }
19309
5afb4e99
DJ
19310 /* Check whether this DIE is interesting enough to save. Normally
19311 we would not be interested in members here, but there may be
19312 later variables referencing them via DW_AT_specification (for
19313 static members). */
19314 if (!load_all
d8f62e84 19315 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19316 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19317 && abbrev->tag != DW_TAG_enumerator
19318 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19319 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19320 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19321 && abbrev->tag != DW_TAG_variable
5afb4e99 19322 && abbrev->tag != DW_TAG_namespace
f55ee35c 19323 && abbrev->tag != DW_TAG_module
95554aad 19324 && abbrev->tag != DW_TAG_member
74921315
KS
19325 && abbrev->tag != DW_TAG_imported_unit
19326 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19327 {
19328 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19329 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19330 continue;
19331 }
19332
6f06d47b
YQ
19333 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19334 abbrev);
cd9983dd 19335
48fbe735 19336 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19337
19338 /* This two-pass algorithm for processing partial symbols has a
19339 high cost in cache pressure. Thus, handle some simple cases
19340 here which cover the majority of C partial symbols. DIEs
19341 which neither have specification tags in them, nor could have
19342 specification tags elsewhere pointing at them, can simply be
19343 processed and discarded.
19344
19345 This segment is also optional; scan_partial_symbols and
19346 add_partial_symbol will handle these DIEs if we chain
19347 them in normally. When compilers which do not emit large
19348 quantities of duplicate debug information are more common,
19349 this code can probably be removed. */
19350
19351 /* Any complete simple types at the top level (pretty much all
19352 of them, for a language without namespaces), can be processed
19353 directly. */
19354 if (parent_die == NULL
cd9983dd
YQ
19355 && pdi.has_specification == 0
19356 && pdi.is_declaration == 0
19357 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19358 || pdi.tag == DW_TAG_base_type
d8f62e84 19359 || pdi.tag == DW_TAG_array_type
cd9983dd 19360 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19361 {
7d00ffec 19362 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19363 add_partial_symbol (&pdi, cu);
19364
cd9983dd 19365 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19366 continue;
19367 }
19368
d8228535
JK
19369 /* The exception for DW_TAG_typedef with has_children above is
19370 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19371 type_name_or_error will error on such types later.
d8228535
JK
19372
19373 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19374 it could not find the child DIEs referenced later, this is checked
19375 above. In correct DWARF DW_TAG_typedef should have no children. */
19376
cd9983dd 19377 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19378 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19379 "- DIE at %s [in module %s]"),
cd9983dd 19380 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19381
72bf9492
DJ
19382 /* If we're at the second level, and we're an enumerator, and
19383 our parent has no specification (meaning possibly lives in a
19384 namespace elsewhere), then we can add the partial symbol now
19385 instead of queueing it. */
cd9983dd 19386 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19387 && parent_die != NULL
19388 && parent_die->die_parent == NULL
19389 && parent_die->tag == DW_TAG_enumeration_type
19390 && parent_die->has_specification == 0)
19391 {
7d00ffec 19392 if (pdi.raw_name == NULL)
b98664d3 19393 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19394 else if (building_psymtab)
f0fbb768 19395 add_partial_symbol (&pdi, cu);
72bf9492 19396
cd9983dd 19397 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19398 continue;
19399 }
19400
cd9983dd 19401 struct partial_die_info *part_die
6f06d47b 19402 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19403
72bf9492
DJ
19404 /* We'll save this DIE so link it in. */
19405 part_die->die_parent = parent_die;
19406 part_die->die_sibling = NULL;
19407 part_die->die_child = NULL;
19408
19409 if (last_die && last_die == parent_die)
19410 last_die->die_child = part_die;
19411 else if (last_die)
19412 last_die->die_sibling = part_die;
19413
19414 last_die = part_die;
19415
19416 if (first_die == NULL)
19417 first_die = part_die;
19418
19419 /* Maybe add the DIE to the hash table. Not all DIEs that we
19420 find interesting need to be in the hash table, because we
19421 also have the parent/sibling/child chains; only those that we
19422 might refer to by offset later during partial symbol reading.
19423
19424 For now this means things that might have be the target of a
19425 DW_AT_specification, DW_AT_abstract_origin, or
19426 DW_AT_extension. DW_AT_extension will refer only to
19427 namespaces; DW_AT_abstract_origin refers to functions (and
19428 many things under the function DIE, but we do not recurse
19429 into function DIEs during partial symbol reading) and
19430 possibly variables as well; DW_AT_specification refers to
19431 declarations. Declarations ought to have the DW_AT_declaration
19432 flag. It happens that GCC forgets to put it in sometimes, but
19433 only for functions, not for types.
19434
19435 Adding more things than necessary to the hash table is harmless
19436 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19437 wasted time in find_partial_die, when we reread the compilation
19438 unit with load_all_dies set. */
72bf9492 19439
5afb4e99 19440 if (load_all
72929c62 19441 || abbrev->tag == DW_TAG_constant
5afb4e99 19442 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19443 || abbrev->tag == DW_TAG_variable
19444 || abbrev->tag == DW_TAG_namespace
19445 || part_die->is_declaration)
19446 {
19447 void **slot;
19448
19449 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19450 to_underlying (part_die->sect_off),
19451 INSERT);
72bf9492
DJ
19452 *slot = part_die;
19453 }
19454
72bf9492 19455 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19456 we have no reason to follow the children of structures; for other
98bfdba5
PA
19457 languages we have to, so that we can get at method physnames
19458 to infer fully qualified class names, for DW_AT_specification,
19459 and for C++ template arguments. For C++, we also look one level
19460 inside functions to find template arguments (if the name of the
19461 function does not already contain the template arguments).
bc30ff58 19462
0a4b0913
AB
19463 For Ada and Fortran, we need to scan the children of subprograms
19464 and lexical blocks as well because these languages allow the
19465 definition of nested entities that could be interesting for the
19466 debugger, such as nested subprograms for instance. */
72bf9492 19467 if (last_die->has_children
5afb4e99
DJ
19468 && (load_all
19469 || last_die->tag == DW_TAG_namespace
f55ee35c 19470 || last_die->tag == DW_TAG_module
72bf9492 19471 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19472 || (cu->language == language_cplus
19473 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19474 && (last_die->raw_name == NULL
19475 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19476 || (cu->language != language_c
19477 && (last_die->tag == DW_TAG_class_type
680b30c7 19478 || last_die->tag == DW_TAG_interface_type
72bf9492 19479 || last_die->tag == DW_TAG_structure_type
bc30ff58 19480 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19481 || ((cu->language == language_ada
19482 || cu->language == language_fortran)
bc30ff58
JB
19483 && (last_die->tag == DW_TAG_subprogram
19484 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19485 {
19486 nesting_level++;
19487 parent_die = last_die;
19488 continue;
19489 }
19490
19491 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19492 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19493
19494 /* Back to the top, do it again. */
19495 }
19496}
19497
6f06d47b 19498partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19499 const struct abbrev_info *abbrev)
6f06d47b
YQ
19500 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19501{
19502}
19503
7d00ffec
TT
19504/* See class definition. */
19505
19506const char *
19507partial_die_info::name (dwarf2_cu *cu)
19508{
19509 if (!canonical_name && raw_name != nullptr)
19510 {
19511 struct objfile *objfile = cu->per_objfile->objfile;
19512 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19513 canonical_name = 1;
19514 }
19515
19516 return raw_name;
19517}
19518
35cc7ed7
YQ
19519/* Read a minimal amount of information into the minimal die structure.
19520 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19521
48fbe735
YQ
19522const gdb_byte *
19523partial_die_info::read (const struct die_reader_specs *reader,
19524 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19525{
dee91e82 19526 struct dwarf2_cu *cu = reader->cu;
976ca316 19527 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19528 unsigned int i;
c5aa993b 19529 int has_low_pc_attr = 0;
c906108c 19530 int has_high_pc_attr = 0;
91da1414 19531 int high_pc_relative = 0;
c906108c 19532
fd0a254f 19533 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19534 {
e7da7f8f 19535 attribute attr;
7a5f294d 19536 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19537 /* String and address offsets that need to do the reprocessing have
dda83cd7 19538 already been read at this point, so there is no need to wait until
18a8505e 19539 the loop terminates to do the reprocessing. */
7a5f294d 19540 if (attr.requires_reprocessing_p ())
d0ce17d8 19541 read_attribute_reprocess (reader, &attr, tag);
c906108c 19542 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19543 partial symbol table. */
c906108c
SS
19544 switch (attr.name)
19545 {
19546 case DW_AT_name:
48fbe735 19547 switch (tag)
71c25dea
TT
19548 {
19549 case DW_TAG_compile_unit:
95554aad 19550 case DW_TAG_partial_unit:
348e048f 19551 case DW_TAG_type_unit:
71c25dea
TT
19552 /* Compilation units have a DW_AT_name that is a filename, not
19553 a source language identifier. */
19554 case DW_TAG_enumeration_type:
19555 case DW_TAG_enumerator:
19556 /* These tags always have simple identifiers already; no need
19557 to canonicalize them. */
7d00ffec 19558 canonical_name = 1;
2c830f54 19559 raw_name = attr.as_string ();
71c25dea
TT
19560 break;
19561 default:
7d00ffec 19562 canonical_name = 0;
2c830f54 19563 raw_name = attr.as_string ();
71c25dea
TT
19564 break;
19565 }
c906108c 19566 break;
31ef98ae 19567 case DW_AT_linkage_name:
c906108c 19568 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19569 /* Note that both forms of linkage name might appear. We
19570 assume they will be the same, and we only store the last
19571 one we see. */
95f982e5 19572 linkage_name = attr.as_string ();
c906108c
SS
19573 break;
19574 case DW_AT_low_pc:
19575 has_low_pc_attr = 1;
95f982e5 19576 lowpc = attr.as_address ();
c906108c
SS
19577 break;
19578 case DW_AT_high_pc:
19579 has_high_pc_attr = 1;
95f982e5 19580 highpc = attr.as_address ();
cd6c91b4 19581 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19582 high_pc_relative = 1;
c906108c
SS
19583 break;
19584 case DW_AT_location:
dda83cd7
SM
19585 /* Support the .debug_loc offsets. */
19586 if (attr.form_is_block ())
19587 {
9d2246fc 19588 d.locdesc = attr.as_block ();
dda83cd7
SM
19589 }
19590 else if (attr.form_is_section_offset ())
19591 {
4d3c2250 19592 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19593 }
19594 else
19595 {
4d3c2250
KB
19596 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19597 "partial symbol information");
dda83cd7 19598 }
c906108c 19599 break;
c906108c 19600 case DW_AT_external:
c45bc3f8 19601 is_external = attr.as_boolean ();
c906108c
SS
19602 break;
19603 case DW_AT_declaration:
c45bc3f8 19604 is_declaration = attr.as_boolean ();
c906108c
SS
19605 break;
19606 case DW_AT_type:
48fbe735 19607 has_type = 1;
c906108c
SS
19608 break;
19609 case DW_AT_abstract_origin:
19610 case DW_AT_specification:
72bf9492 19611 case DW_AT_extension:
48fbe735 19612 has_specification = 1;
0826b30a 19613 spec_offset = attr.get_ref_die_offset ();
48fbe735 19614 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19615 || cu->per_cu->is_dwz);
c906108c
SS
19616 break;
19617 case DW_AT_sibling:
19618 /* Ignore absolute siblings, they might point outside of
19619 the current compile unit. */
19620 if (attr.form == DW_FORM_ref_addr)
b98664d3 19621 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19622 else
b9502d3f 19623 {
48fbe735 19624 const gdb_byte *buffer = reader->buffer;
0826b30a 19625 sect_offset off = attr.get_ref_die_offset ();
9c541725 19626 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19627
19628 if (sibling_ptr < info_ptr)
b98664d3 19629 complaint (_("DW_AT_sibling points backwards"));
22869d73 19630 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19631 reader->die_section->overflow_complaint ();
b9502d3f 19632 else
48fbe735 19633 sibling = sibling_ptr;
b9502d3f 19634 }
c906108c 19635 break;
dda83cd7
SM
19636 case DW_AT_byte_size:
19637 has_byte_size = 1;
19638 break;
19639 case DW_AT_const_value:
19640 has_const_value = 1;
19641 break;
68511cec
CES
19642 case DW_AT_calling_convention:
19643 /* DWARF doesn't provide a way to identify a program's source-level
19644 entry point. DW_AT_calling_convention attributes are only meant
19645 to describe functions' calling conventions.
19646
19647 However, because it's a necessary piece of information in
0c1b455e
TT
19648 Fortran, and before DWARF 4 DW_CC_program was the only
19649 piece of debugging information whose definition refers to
19650 a 'main program' at all, several compilers marked Fortran
19651 main programs with DW_CC_program --- even when those
19652 functions use the standard calling conventions.
19653
19654 Although DWARF now specifies a way to provide this
19655 information, we support this practice for backward
19656 compatibility. */
529908cb 19657 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19658 && cu->language == language_fortran)
48fbe735 19659 main_subprogram = 1;
68511cec 19660 break;
481860b3 19661 case DW_AT_inline:
529908cb
TT
19662 {
19663 LONGEST value = attr.constant_value (-1);
19664 if (value == DW_INL_inlined
19665 || value == DW_INL_declared_inlined)
19666 may_be_inlined = 1;
19667 }
481860b3 19668 break;
95554aad
TT
19669
19670 case DW_AT_import:
48fbe735 19671 if (tag == DW_TAG_imported_unit)
36586728 19672 {
0826b30a 19673 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19674 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19675 || cu->per_cu->is_dwz);
19676 }
95554aad
TT
19677 break;
19678
0c1b455e 19679 case DW_AT_main_subprogram:
c45bc3f8 19680 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19681 break;
19682
05caa1d2
TT
19683 case DW_AT_ranges:
19684 {
2b0c7f41
SM
19685 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19686 on DWARF version). */
19687 ULONGEST ranges_offset = attr.as_unsigned ();
19688
19689 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19690 this value. */
19691 if (tag != DW_TAG_compile_unit)
19692 ranges_offset += cu->gnu_ranges_base;
19693
05caa1d2 19694 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19695 nullptr, tag))
05caa1d2
TT
19696 has_pc_info = 1;
19697 }
19698 break;
19699
c906108c
SS
19700 default:
19701 break;
19702 }
19703 }
19704
10d06d82
TT
19705 /* For Ada, if both the name and the linkage name appear, we prefer
19706 the latter. This lets "catch exception" work better, regardless
19707 of the order in which the name and linkage name were emitted.
19708 Really, though, this is just a workaround for the fact that gdb
19709 doesn't store both the name and the linkage name. */
19710 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19711 raw_name = linkage_name;
10d06d82 19712
91da1414 19713 if (high_pc_relative)
48fbe735 19714 highpc += lowpc;
91da1414 19715
9373cf26
JK
19716 if (has_low_pc_attr && has_high_pc_attr)
19717 {
19718 /* When using the GNU linker, .gnu.linkonce. sections are used to
19719 eliminate duplicate copies of functions and vtables and such.
19720 The linker will arbitrarily choose one and discard the others.
19721 The AT_*_pc values for such functions refer to local labels in
19722 these sections. If the section from that file was discarded, the
19723 labels are not in the output, so the relocs get a value of 0.
19724 If this is a discarded function, mark the pc bounds as invalid,
19725 so that GDB will ignore it. */
976ca316 19726 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19727 {
976ca316 19728 struct objfile *objfile = per_objfile->objfile;
08feed99 19729 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19730
b98664d3 19731 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19732 "for DIE at %s [in module %s]"),
48fbe735
YQ
19733 paddress (gdbarch, lowpc),
19734 sect_offset_str (sect_off),
9d8780f0 19735 objfile_name (objfile));
9373cf26
JK
19736 }
19737 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19738 else if (lowpc >= highpc)
9373cf26 19739 {
976ca316 19740 struct objfile *objfile = per_objfile->objfile;
08feed99 19741 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19742
b98664d3 19743 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19744 "for DIE at %s [in module %s]"),
48fbe735
YQ
19745 paddress (gdbarch, lowpc),
19746 paddress (gdbarch, highpc),
19747 sect_offset_str (sect_off),
9c541725 19748 objfile_name (objfile));
9373cf26
JK
19749 }
19750 else
48fbe735 19751 has_pc_info = 1;
9373cf26 19752 }
85cbf3d3 19753
c906108c
SS
19754 return info_ptr;
19755}
19756
72bf9492
DJ
19757/* Find a cached partial DIE at OFFSET in CU. */
19758
d590ff25
YQ
19759struct partial_die_info *
19760dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19761{
19762 struct partial_die_info *lookup_die = NULL;
6f06d47b 19763 struct partial_die_info part_die (sect_off);
72bf9492 19764
9a3c8263 19765 lookup_die = ((struct partial_die_info *)
d590ff25 19766 htab_find_with_hash (partial_dies, &part_die,
9c541725 19767 to_underlying (sect_off)));
72bf9492 19768
72bf9492
DJ
19769 return lookup_die;
19770}
19771
348e048f
DE
19772/* Find a partial DIE at OFFSET, which may or may not be in CU,
19773 except in the case of .debug_types DIEs which do not reference
19774 outside their CU (they do however referencing other types via
55f1336d 19775 DW_FORM_ref_sig8). */
72bf9492 19776
122cf0f2 19777static const struct cu_partial_die_info
9c541725 19778find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19779{
976ca316
SM
19780 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19781 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19782 struct partial_die_info *pd = NULL;
72bf9492 19783
36586728 19784 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19785 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19786 {
d590ff25 19787 pd = cu->find_partial_die (sect_off);
5afb4e99 19788 if (pd != NULL)
fb816e8b 19789 return { cu, pd };
0d99eb77
DE
19790 /* We missed recording what we needed.
19791 Load all dies and try again. */
5afb4e99 19792 }
0d99eb77
DE
19793 else
19794 {
19795 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19796 if (cu->per_cu->is_debug_types)
0d99eb77 19797 {
9d8780f0
SM
19798 error (_("Dwarf Error: Type Unit at offset %s contains"
19799 " external reference to offset %s [in module %s].\n"),
19800 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19801 bfd_get_filename (objfile->obfd));
19802 }
7188ed02
SM
19803 dwarf2_per_cu_data *per_cu
19804 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19805 per_objfile);
72bf9492 19806
976ca316 19807 cu = per_objfile->get_cu (per_cu);
7188ed02 19808 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19809 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19810
976ca316 19811 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19812
19813 cu->last_used = 0;
19814 pd = cu->find_partial_die (sect_off);
0d99eb77 19815 }
5afb4e99 19816
dee91e82
DE
19817 /* If we didn't find it, and not all dies have been loaded,
19818 load them all and try again. */
19819
7188ed02 19820 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19821 {
7188ed02 19822 cu->per_cu->load_all_dies = 1;
fd820528
DE
19823
19824 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19825 THIS_CU->cu may already be in use. So we can't just free it and
19826 replace its DIEs with the ones we read in. Instead, we leave those
19827 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19828 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19829 set. */
976ca316 19830 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19831
7188ed02 19832 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19833 }
19834
19835 if (pd == NULL)
521894aa 19836 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19837 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19838 return { cu, pd };
72bf9492
DJ
19839}
19840
abc72ce4
DE
19841/* See if we can figure out if the class lives in a namespace. We do
19842 this by looking for a member function; its demangled name will
19843 contain namespace info, if there is any. */
19844
19845static void
19846guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19847 struct dwarf2_cu *cu)
19848{
19849 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19850 what template types look like, because the demangler
19851 frequently doesn't give the same name as the debug info. We
19852 could fix this by only using the demangled name to get the
19853 prefix (but see comment in read_structure_type). */
19854
19855 struct partial_die_info *real_pdi;
19856 struct partial_die_info *child_pdi;
19857
19858 /* If this DIE (this DIE's specification, if any) has a parent, then
19859 we should not do this. We'll prepend the parent's fully qualified
19860 name when we create the partial symbol. */
19861
19862 real_pdi = struct_pdi;
19863 while (real_pdi->has_specification)
fb816e8b 19864 {
122cf0f2
AB
19865 auto res = find_partial_die (real_pdi->spec_offset,
19866 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19867 real_pdi = res.pdi;
19868 cu = res.cu;
19869 }
abc72ce4
DE
19870
19871 if (real_pdi->die_parent != NULL)
19872 return;
19873
19874 for (child_pdi = struct_pdi->die_child;
19875 child_pdi != NULL;
19876 child_pdi = child_pdi->die_sibling)
19877 {
19878 if (child_pdi->tag == DW_TAG_subprogram
19879 && child_pdi->linkage_name != NULL)
19880 {
43816ebc 19881 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19882 (cu->language_defn->class_name_from_physname
19883 (child_pdi->linkage_name));
abc72ce4
DE
19884 if (actual_class_name != NULL)
19885 {
5e22e966 19886 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19887 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19888 struct_pdi->canonical_name = 1;
abc72ce4
DE
19889 }
19890 break;
19891 }
19892 }
19893}
19894
25c11aca
TV
19895/* Return true if a DIE with TAG may have the DW_AT_const_value
19896 attribute. */
19897
19898static bool
19899can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19900{
19901 switch (tag)
19902 {
19903 case DW_TAG_constant:
19904 case DW_TAG_enumerator:
19905 case DW_TAG_formal_parameter:
19906 case DW_TAG_template_value_param:
19907 case DW_TAG_variable:
19908 return true;
19909 }
19910
19911 return false;
19912}
19913
52356b79
YQ
19914void
19915partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19916{
abc72ce4
DE
19917 /* Once we've fixed up a die, there's no point in doing so again.
19918 This also avoids a memory leak if we were to call
19919 guess_partial_die_structure_name multiple times. */
52356b79 19920 if (fixup_called)
abc72ce4
DE
19921 return;
19922
72bf9492
DJ
19923 /* If we found a reference attribute and the DIE has no name, try
19924 to find a name in the referred to DIE. */
19925
7d00ffec 19926 if (raw_name == NULL && has_specification)
72bf9492
DJ
19927 {
19928 struct partial_die_info *spec_die;
72bf9492 19929
122cf0f2 19930 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19931 spec_die = res.pdi;
19932 cu = res.cu;
72bf9492 19933
52356b79 19934 spec_die->fixup (cu);
72bf9492 19935
7d00ffec 19936 if (spec_die->raw_name)
72bf9492 19937 {
7d00ffec
TT
19938 raw_name = spec_die->raw_name;
19939 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19940
19941 /* Copy DW_AT_external attribute if it is set. */
19942 if (spec_die->is_external)
52356b79 19943 is_external = spec_die->is_external;
72bf9492
DJ
19944 }
19945 }
19946
25c11aca
TV
19947 if (!has_const_value && has_specification
19948 && can_have_DW_AT_const_value_p (tag))
19949 {
19950 struct partial_die_info *spec_die;
19951
19952 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19953 spec_die = res.pdi;
19954 cu = res.cu;
19955
19956 spec_die->fixup (cu);
19957
19958 if (spec_die->has_const_value)
19959 {
19960 /* Copy DW_AT_const_value attribute if it is set. */
19961 has_const_value = spec_die->has_const_value;
19962 }
19963 }
19964
72bf9492 19965 /* Set default names for some unnamed DIEs. */
72bf9492 19966
7d00ffec
TT
19967 if (raw_name == NULL && tag == DW_TAG_namespace)
19968 {
19969 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19970 canonical_name = 1;
19971 }
72bf9492 19972
abc72ce4
DE
19973 /* If there is no parent die to provide a namespace, and there are
19974 children, see if we can determine the namespace from their linkage
122d1940 19975 name. */
abc72ce4 19976 if (cu->language == language_cplus
5e22e966 19977 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19978 && die_parent == NULL
19979 && has_children
19980 && (tag == DW_TAG_class_type
19981 || tag == DW_TAG_structure_type
19982 || tag == DW_TAG_union_type))
19983 guess_partial_die_structure_name (this, cu);
abc72ce4 19984
53832f31
TT
19985 /* GCC might emit a nameless struct or union that has a linkage
19986 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19987 if (raw_name == NULL
52356b79
YQ
19988 && (tag == DW_TAG_class_type
19989 || tag == DW_TAG_interface_type
19990 || tag == DW_TAG_structure_type
19991 || tag == DW_TAG_union_type)
19992 && linkage_name != NULL)
53832f31 19993 {
43816ebc
TT
19994 gdb::unique_xmalloc_ptr<char> demangled
19995 (gdb_demangle (linkage_name, DMGL_TYPES));
19996 if (demangled != nullptr)
53832f31 19997 {
96408a79
SA
19998 const char *base;
19999
20000 /* Strip any leading namespaces/classes, keep only the base name.
20001 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
20002 base = strrchr (demangled.get (), ':');
20003 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
20004 base++;
20005 else
43816ebc 20006 base = demangled.get ();
96408a79 20007
5e22e966 20008 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20009 raw_name = objfile->intern (base);
20010 canonical_name = 1;
53832f31
TT
20011 }
20012 }
20013
52356b79 20014 fixup_called = 1;
72bf9492
DJ
20015}
20016
d0ce17d8 20017/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
20018 contents from the given SECTION in the HEADER.
20019
20020 HEADER_OFFSET is the offset of the header in the section. */
41144253 20021static void
d0ce17d8 20022read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
20023 struct dwarf2_section_info *section,
20024 sect_offset header_offset)
41144253 20025{
20026 unsigned int bytes_read;
20027 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
20028 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20029
41144253 20030 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20031 info_ptr += bytes_read;
a1c40103 20032
41144253 20033 header->version = read_2_bytes (abfd, info_ptr);
20034 info_ptr += 2;
a1c40103 20035
41144253 20036 header->addr_size = read_1_byte (abfd, info_ptr);
20037 info_ptr += 1;
a1c40103 20038
41144253 20039 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20040 info_ptr += 1;
a1c40103 20041
41144253 20042 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20043}
20044
20045/* Return the DW_AT_loclists_base value for the CU. */
20046static ULONGEST
20047lookup_loclist_base (struct dwarf2_cu *cu)
20048{
20049 /* For the .dwo unit, the loclist_base points to the first offset following
20050 the header. The header consists of the following entities-
20051 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20052 bit format)
20053 2. version (2 bytes)
20054 3. address size (1 byte)
20055 4. segment selector size (1 byte)
20056 5. offset entry count (4 bytes)
20057 These sizes are derived as per the DWARFv5 standard. */
20058 if (cu->dwo_unit != nullptr)
20059 {
20060 if (cu->header.initial_length_size == 4)
20061 return LOCLIST_HEADER_SIZE32;
20062 return LOCLIST_HEADER_SIZE64;
20063 }
20064 return cu->loclist_base;
20065}
20066
20067/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20068 array of offsets in the .debug_loclists section. */
e57933dc
SM
20069
20070static sect_offset
41144253 20071read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20072{
976ca316
SM
20073 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20074 struct objfile *objfile = per_objfile->objfile;
41144253 20075 bfd *abfd = objfile->obfd;
a1c40103
SM
20076 ULONGEST loclist_header_size =
20077 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20078 : LOCLIST_HEADER_SIZE64);
41144253 20079 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
20080
20081 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20082 ULONGEST start_offset =
20083 loclist_base + loclist_index * cu->header.offset_size;
20084
a1c40103 20085 /* Get loclists section. */
41144253 20086 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20087
a1c40103 20088 /* Read the loclists section content. */
41144253 20089 section->read (objfile);
20090 if (section->buffer == NULL)
a0c1eeba
SM
20091 error (_("DW_FORM_loclistx used without .debug_loclists "
20092 "section [in module %s]"), objfile_name (objfile));
20093
a1c40103
SM
20094 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20095 so if loclist_base is smaller than the header size, we have a problem. */
20096 if (loclist_base < loclist_header_size)
20097 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20098 objfile_name (objfile));
20099
20100 /* Read the header of the loclists contribution. */
d0ce17d8 20101 struct loclists_rnglists_header header;
a1c40103
SM
20102 read_loclists_rnglists_header (&header, section,
20103 (sect_offset) (loclist_base - loclist_header_size));
20104
20105 /* Verify the loclist index is valid. */
41144253 20106 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
20107 error (_("DW_FORM_loclistx pointing outside of "
20108 ".debug_loclists offset array [in module %s]"),
20109 objfile_name (objfile));
20110
05787bad
SM
20111 /* Validate that reading won't go beyond the end of the section. */
20112 if (start_offset + cu->header.offset_size > section->size)
20113 error (_("Reading DW_FORM_loclistx index beyond end of"
20114 ".debug_loclists section [in module %s]"),
20115 objfile_name (objfile));
20116
20117 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 20118
20119 if (cu->header.offset_size == 4)
e57933dc 20120 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 20121 else
e57933dc 20122 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 20123}
20124
d0ce17d8
CT
20125/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20126 array of offsets in the .debug_rnglists section. */
e57933dc
SM
20127
20128static sect_offset
d0ce17d8
CT
20129read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20130 dwarf_tag tag)
20131{
20132 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20133 struct objfile *objfile = dwarf2_per_objfile->objfile;
20134 bfd *abfd = objfile->obfd;
20135 ULONGEST rnglist_header_size =
20136 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20137 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
20138
20139 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20140 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20141 doesn't apply. */
d0ce17d8 20142 ULONGEST rnglist_base =
2b0c7f41 20143 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
20144
20145 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
20146 ULONGEST start_offset =
20147 rnglist_base + rnglist_index * cu->header.offset_size;
20148
20149 /* Get rnglists section. */
20150 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20151
20152 /* Read the rnglists section content. */
20153 section->read (objfile);
20154 if (section->buffer == nullptr)
20155 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20156 "[in module %s]"),
20157 objfile_name (objfile));
20158
a1c40103
SM
20159 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20160 so if rnglist_base is smaller than the header size, we have a problem. */
20161 if (rnglist_base < rnglist_header_size)
20162 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20163 objfile_name (objfile));
20164
20165 /* Read the header of the rnglists contribution. */
d0ce17d8 20166 struct loclists_rnglists_header header;
a1c40103
SM
20167 read_loclists_rnglists_header (&header, section,
20168 (sect_offset) (rnglist_base - rnglist_header_size));
20169
20170 /* Verify the rnglist index is valid. */
d0ce17d8
CT
20171 if (rnglist_index >= header.offset_entry_count)
20172 error (_("DW_FORM_rnglistx index pointing outside of "
20173 ".debug_rnglists offset array [in module %s]"),
20174 objfile_name (objfile));
20175
d0ce17d8 20176 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20177 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20178 error (_("Reading DW_FORM_rnglistx index beyond end of"
20179 ".debug_rnglists section [in module %s]"),
20180 objfile_name (objfile));
20181
20182 const gdb_byte *info_ptr = section->buffer + start_offset;
20183
20184 if (cu->header.offset_size == 4)
e57933dc 20185 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 20186 else
e57933dc 20187 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
20188}
20189
18a8505e
AT
20190/* Process the attributes that had to be skipped in the first round. These
20191 attributes are the ones that need str_offsets_base or addr_base attributes.
20192 They could not have been processed in the first round, because at the time
20193 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20194static void
20195read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20196 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20197{
20198 struct dwarf2_cu *cu = reader->cu;
20199 switch (attr->form)
20200 {
20201 case DW_FORM_addrx:
20202 case DW_FORM_GNU_addr_index:
36d378cf
TT
20203 attr->set_address (read_addr_index (cu,
20204 attr->as_unsigned_reprocess ()));
dda83cd7 20205 break;
41144253 20206 case DW_FORM_loclistx:
b1829e1b 20207 {
e57933dc 20208 sect_offset loclists_sect_off
b1829e1b
SM
20209 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20210
e57933dc 20211 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
20212 }
20213 break;
d0ce17d8 20214 case DW_FORM_rnglistx:
b1829e1b 20215 {
e57933dc 20216 sect_offset rnglists_sect_off
b1829e1b
SM
20217 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20218
e57933dc 20219 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 20220 }
dda83cd7 20221 break;
18a8505e
AT
20222 case DW_FORM_strx:
20223 case DW_FORM_strx1:
20224 case DW_FORM_strx2:
20225 case DW_FORM_strx3:
20226 case DW_FORM_strx4:
20227 case DW_FORM_GNU_str_index:
20228 {
fe56917a 20229 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20230 gdb_assert (!attr->canonical_string_p ());
18a8505e 20231 if (reader->dwo_file != NULL)
c6481205
TT
20232 attr->set_string_noncanonical (read_dwo_str_index (reader,
20233 str_index));
18a8505e 20234 else
c6481205
TT
20235 attr->set_string_noncanonical (read_stub_str_index (cu,
20236 str_index));
18a8505e
AT
20237 break;
20238 }
20239 default:
20240 gdb_assert_not_reached (_("Unexpected DWARF form."));
20241 }
20242}
20243
a8329558 20244/* Read an attribute value described by an attribute form. */
c906108c 20245
d521ce57 20246static const gdb_byte *
dee91e82
DE
20247read_attribute_value (const struct die_reader_specs *reader,
20248 struct attribute *attr, unsigned form,
7a5f294d 20249 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20250{
dee91e82 20251 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20252 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20253 struct objfile *objfile = per_objfile->objfile;
dee91e82 20254 bfd *abfd = reader->abfd;
e7c27a73 20255 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20256 unsigned int bytes_read;
20257 struct dwarf_block *blk;
20258
aead7601 20259 attr->form = (enum dwarf_form) form;
a8329558 20260 switch (form)
c906108c 20261 {
c906108c 20262 case DW_FORM_ref_addr:
7c290a04
TT
20263 if (cu_header->version == 2)
20264 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 20265 &bytes_read));
ae411497 20266 else
7c290a04 20267 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20268 &bytes_read));
ae411497
TT
20269 info_ptr += bytes_read;
20270 break;
36586728 20271 case DW_FORM_GNU_ref_alt:
7c290a04 20272 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20273 &bytes_read));
36586728
TT
20274 info_ptr += bytes_read;
20275 break;
ae411497 20276 case DW_FORM_addr:
08feed99
TT
20277 {
20278 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 20279 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
20280 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20281 attr->set_address (addr);
08feed99
TT
20282 info_ptr += bytes_read;
20283 }
c906108c
SS
20284 break;
20285 case DW_FORM_block2:
7b5a2f43 20286 blk = dwarf_alloc_block (cu);
c906108c
SS
20287 blk->size = read_2_bytes (abfd, info_ptr);
20288 info_ptr += 2;
20289 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20290 info_ptr += blk->size;
9d2246fc 20291 attr->set_block (blk);
c906108c
SS
20292 break;
20293 case DW_FORM_block4:
7b5a2f43 20294 blk = dwarf_alloc_block (cu);
c906108c
SS
20295 blk->size = read_4_bytes (abfd, info_ptr);
20296 info_ptr += 4;
20297 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20298 info_ptr += blk->size;
9d2246fc 20299 attr->set_block (blk);
c906108c
SS
20300 break;
20301 case DW_FORM_data2:
414ad644 20302 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20303 info_ptr += 2;
20304 break;
20305 case DW_FORM_data4:
414ad644 20306 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20307 info_ptr += 4;
20308 break;
20309 case DW_FORM_data8:
414ad644 20310 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20311 info_ptr += 8;
20312 break;
0224619f
JK
20313 case DW_FORM_data16:
20314 blk = dwarf_alloc_block (cu);
20315 blk->size = 16;
20316 blk->data = read_n_bytes (abfd, info_ptr, 16);
20317 info_ptr += 16;
9d2246fc 20318 attr->set_block (blk);
0224619f 20319 break;
2dc7f7b3 20320 case DW_FORM_sec_offset:
7c290a04 20321 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20322 &bytes_read));
2dc7f7b3
TT
20323 info_ptr += bytes_read;
20324 break;
41144253 20325 case DW_FORM_loclistx:
20326 {
7a5f294d
TT
20327 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20328 &bytes_read));
20329 info_ptr += bytes_read;
41144253 20330 }
20331 break;
c906108c 20332 case DW_FORM_string:
c6481205
TT
20333 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20334 &bytes_read));
c906108c
SS
20335 info_ptr += bytes_read;
20336 break;
4bdf3d34 20337 case DW_FORM_strp:
36586728
TT
20338 if (!cu->per_cu->is_dwz)
20339 {
c6481205
TT
20340 attr->set_string_noncanonical
20341 (read_indirect_string (per_objfile,
20342 abfd, info_ptr, cu_header,
20343 &bytes_read));
36586728
TT
20344 info_ptr += bytes_read;
20345 break;
20346 }
20347 /* FALLTHROUGH */
43988095
JK
20348 case DW_FORM_line_strp:
20349 if (!cu->per_cu->is_dwz)
20350 {
c6481205
TT
20351 attr->set_string_noncanonical
20352 (per_objfile->read_line_string (info_ptr, cu_header,
20353 &bytes_read));
43988095
JK
20354 info_ptr += bytes_read;
20355 break;
20356 }
20357 /* FALLTHROUGH */
36586728
TT
20358 case DW_FORM_GNU_strp_alt:
20359 {
a7308ce0 20360 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
20361 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20362 &bytes_read);
36586728 20363
c6481205
TT
20364 attr->set_string_noncanonical
20365 (dwz->read_string (objfile, str_offset));
36586728
TT
20366 info_ptr += bytes_read;
20367 }
4bdf3d34 20368 break;
2dc7f7b3 20369 case DW_FORM_exprloc:
c906108c 20370 case DW_FORM_block:
7b5a2f43 20371 blk = dwarf_alloc_block (cu);
c906108c
SS
20372 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20373 info_ptr += bytes_read;
20374 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20375 info_ptr += blk->size;
9d2246fc 20376 attr->set_block (blk);
c906108c
SS
20377 break;
20378 case DW_FORM_block1:
7b5a2f43 20379 blk = dwarf_alloc_block (cu);
c906108c
SS
20380 blk->size = read_1_byte (abfd, info_ptr);
20381 info_ptr += 1;
20382 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20383 info_ptr += blk->size;
9d2246fc 20384 attr->set_block (blk);
c906108c
SS
20385 break;
20386 case DW_FORM_data1:
c906108c 20387 case DW_FORM_flag:
414ad644 20388 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20389 info_ptr += 1;
20390 break;
2dc7f7b3 20391 case DW_FORM_flag_present:
414ad644 20392 attr->set_unsigned (1);
2dc7f7b3 20393 break;
c906108c 20394 case DW_FORM_sdata:
1bc397c5 20395 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20396 info_ptr += bytes_read;
20397 break;
18a8505e 20398 case DW_FORM_rnglistx:
7a5f294d
TT
20399 {
20400 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20401 &bytes_read));
20402 info_ptr += bytes_read;
20403 }
20404 break;
d0ce17d8 20405 case DW_FORM_udata:
414ad644 20406 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20407 info_ptr += bytes_read;
20408 break;
20409 case DW_FORM_ref1:
7c290a04 20410 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20411 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20412 info_ptr += 1;
20413 break;
20414 case DW_FORM_ref2:
7c290a04 20415 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20416 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20417 info_ptr += 2;
20418 break;
20419 case DW_FORM_ref4:
7c290a04 20420 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20421 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20422 info_ptr += 4;
20423 break;
613e1657 20424 case DW_FORM_ref8:
7c290a04 20425 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20426 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20427 info_ptr += 8;
20428 break;
55f1336d 20429 case DW_FORM_ref_sig8:
630ed6b9 20430 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20431 info_ptr += 8;
20432 break;
c906108c 20433 case DW_FORM_ref_udata:
7c290a04 20434 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20435 + read_unsigned_leb128 (abfd, info_ptr,
20436 &bytes_read)));
c906108c
SS
20437 info_ptr += bytes_read;
20438 break;
c906108c 20439 case DW_FORM_indirect:
a8329558
KW
20440 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20441 info_ptr += bytes_read;
43988095
JK
20442 if (form == DW_FORM_implicit_const)
20443 {
20444 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20445 info_ptr += bytes_read;
20446 }
20447 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20448 info_ptr);
43988095
JK
20449 break;
20450 case DW_FORM_implicit_const:
1bc397c5 20451 attr->set_signed (implicit_const);
a8329558 20452 break;
336d760d 20453 case DW_FORM_addrx:
3019eac3 20454 case DW_FORM_GNU_addr_index:
fe56917a
TT
20455 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20456 &bytes_read));
3019eac3
DE
20457 info_ptr += bytes_read;
20458 break;
cf532bd1 20459 case DW_FORM_strx:
15f18d14
AT
20460 case DW_FORM_strx1:
20461 case DW_FORM_strx2:
20462 case DW_FORM_strx3:
20463 case DW_FORM_strx4:
3019eac3 20464 case DW_FORM_GNU_str_index:
3019eac3 20465 {
15f18d14
AT
20466 ULONGEST str_index;
20467 if (form == DW_FORM_strx1)
20468 {
20469 str_index = read_1_byte (abfd, info_ptr);
20470 info_ptr += 1;
20471 }
20472 else if (form == DW_FORM_strx2)
20473 {
20474 str_index = read_2_bytes (abfd, info_ptr);
20475 info_ptr += 2;
20476 }
20477 else if (form == DW_FORM_strx3)
20478 {
20479 str_index = read_3_bytes (abfd, info_ptr);
20480 info_ptr += 3;
20481 }
20482 else if (form == DW_FORM_strx4)
20483 {
20484 str_index = read_4_bytes (abfd, info_ptr);
20485 info_ptr += 4;
20486 }
20487 else
20488 {
20489 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20490 info_ptr += bytes_read;
20491 }
fe56917a 20492 attr->set_unsigned_reprocess (str_index);
7a5f294d 20493 }
3019eac3 20494 break;
c906108c 20495 default:
8a3fe4f8 20496 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20497 dwarf_form_name (form),
20498 bfd_get_filename (abfd));
c906108c 20499 }
28e94949 20500
36586728 20501 /* Super hack. */
cd6c91b4 20502 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20503 attr->form = DW_FORM_GNU_ref_alt;
20504
28e94949
JB
20505 /* We have seen instances where the compiler tried to emit a byte
20506 size attribute of -1 which ended up being encoded as an unsigned
20507 0xffffffff. Although 0xffffffff is technically a valid size value,
20508 an object of this size seems pretty unlikely so we can relatively
20509 safely treat these cases as if the size attribute was invalid and
20510 treat them as zero by default. */
20511 if (attr->name == DW_AT_byte_size
20512 && form == DW_FORM_data4
529908cb 20513 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20514 {
20515 complaint
dda83cd7
SM
20516 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20517 hex_string (attr->as_unsigned ()));
414ad644 20518 attr->set_unsigned (0);
01c66ae6 20519 }
28e94949 20520
c906108c
SS
20521 return info_ptr;
20522}
20523
a8329558
KW
20524/* Read an attribute described by an abbreviated attribute. */
20525
d521ce57 20526static const gdb_byte *
dee91e82 20527read_attribute (const struct die_reader_specs *reader,
4444f407 20528 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20529 const gdb_byte *info_ptr)
a8329558
KW
20530{
20531 attr->name = abbrev->name;
c6481205 20532 attr->string_is_canonical = 0;
fe56917a 20533 attr->requires_reprocessing = 0;
43988095 20534 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20535 abbrev->implicit_const, info_ptr);
a8329558
KW
20536}
20537
43988095
JK
20538/* Return pointer to string at .debug_str offset STR_OFFSET. */
20539
20540static const char *
976ca316 20541read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20542 LONGEST str_offset)
43988095 20543{
976ca316
SM
20544 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20545 str_offset, "DW_FORM_strp");
c906108c
SS
20546}
20547
43988095
JK
20548/* Return pointer to string at .debug_str offset as read from BUF.
20549 BUF is assumed to be in a compilation unit described by CU_HEADER.
20550 Return *BYTES_READ_PTR count of bytes read from BUF. */
20551
d521ce57 20552static const char *
976ca316 20553read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20554 const gdb_byte *buf,
cf2c3c16
TT
20555 const struct comp_unit_head *cu_header,
20556 unsigned int *bytes_read_ptr)
20557{
8266302d 20558 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20559
976ca316 20560 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20561}
20562
86c0bb4c 20563/* See read.h. */
43988095 20564
86c0bb4c
TT
20565const char *
20566dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20567 const struct comp_unit_head *cu_header,
20568 unsigned int *bytes_read_ptr)
43988095 20569{
86c0bb4c 20570 bfd *abfd = objfile->obfd;
8266302d 20571 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20572
5989a64e 20573 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20574}
20575
3019eac3 20576/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20577 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20578 ADDR_SIZE is the size of addresses from the CU header. */
20579
20580static CORE_ADDR
976ca316
SM
20581read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20582 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20583{
976ca316 20584 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20585 bfd *abfd = objfile->obfd;
20586 const gdb_byte *info_ptr;
18a8505e 20587 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20588
976ca316
SM
20589 per_objfile->per_bfd->addr.read (objfile);
20590 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20591 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20592 objfile_name (objfile));
18a8505e 20593 if (addr_base_or_zero + addr_index * addr_size
976ca316 20594 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20595 error (_("DW_FORM_addr_index pointing outside of "
20596 ".debug_addr section [in module %s]"),
4262abfb 20597 objfile_name (objfile));
976ca316
SM
20598 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20599 + addr_index * addr_size);
3019eac3
DE
20600 if (addr_size == 4)
20601 return bfd_get_32 (abfd, info_ptr);
20602 else
20603 return bfd_get_64 (abfd, info_ptr);
20604}
20605
20606/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20607
20608static CORE_ADDR
20609read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20610{
5e22e966 20611 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20612 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20613}
20614
20615/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20616
20617static CORE_ADDR
d521ce57 20618read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20619 unsigned int *bytes_read)
20620{
5e22e966 20621 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20622 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20623
20624 return read_addr_index (cu, addr_index);
20625}
20626
450a1bfc 20627/* See read.h. */
3019eac3
DE
20628
20629CORE_ADDR
82ca3f51 20630dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20631 dwarf2_per_objfile *per_objfile,
82ca3f51 20632 unsigned int addr_index)
3019eac3 20633{
976ca316 20634 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20635 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20636 int addr_size;
20637
3019eac3
DE
20638 /* We need addr_base and addr_size.
20639 If we don't have PER_CU->cu, we have to get it.
20640 Nasty, but the alternative is storing the needed info in PER_CU,
20641 which at this point doesn't seem justified: it's not clear how frequently
20642 it would get used and it would increase the size of every PER_CU.
20643 Entry points like dwarf2_per_cu_addr_size do a similar thing
20644 so we're not in uncharted territory here.
20645 Alas we need to be a bit more complicated as addr_base is contained
20646 in the DIE.
20647
20648 We don't need to read the entire CU(/TU).
20649 We just need the header and top level die.
a1b64ce1 20650
3019eac3 20651 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20652 For now we skip this optimization. */
3019eac3
DE
20653
20654 if (cu != NULL)
20655 {
20656 addr_base = cu->addr_base;
20657 addr_size = cu->header.addr_size;
20658 }
20659 else
20660 {
976ca316 20661 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20662 addr_base = reader.cu->addr_base;
20663 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20664 }
20665
976ca316 20666 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20667}
20668
18a8505e
AT
20669/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20670 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20671 DWO file. */
3019eac3 20672
d521ce57 20673static const char *
18a8505e
AT
20674read_str_index (struct dwarf2_cu *cu,
20675 struct dwarf2_section_info *str_section,
20676 struct dwarf2_section_info *str_offsets_section,
20677 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20678{
976ca316
SM
20679 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20680 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20681 const char *objf_name = objfile_name (objfile);
3019eac3 20682 bfd *abfd = objfile->obfd;
d521ce57 20683 const gdb_byte *info_ptr;
3019eac3 20684 ULONGEST str_offset;
cf532bd1 20685 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20686
96b79293
TT
20687 str_section->read (objfile);
20688 str_offsets_section->read (objfile);
73869dc2 20689 if (str_section->buffer == NULL)
18a8505e 20690 error (_("%s used without %s section"
9d8780f0 20691 " in CU at offset %s [in module %s]"),
96b79293 20692 form_name, str_section->get_name (),
dda83cd7 20693 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20694 if (str_offsets_section->buffer == NULL)
18a8505e 20695 error (_("%s used without %s section"
9d8780f0 20696 " in CU at offset %s [in module %s]"),
96b79293 20697 form_name, str_section->get_name (),
dda83cd7 20698 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20699 info_ptr = (str_offsets_section->buffer
18a8505e 20700 + str_offsets_base
3019eac3
DE
20701 + str_index * cu->header.offset_size);
20702 if (cu->header.offset_size == 4)
20703 str_offset = bfd_get_32 (abfd, info_ptr);
20704 else
20705 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20706 if (str_offset >= str_section->size)
57d63ce2 20707 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20708 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20709 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20710 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20711}
20712
18a8505e
AT
20713/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20714
20715static const char *
20716read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20717{
20718 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20719 ? reader->cu->header.addr_size : 0;
20720 return read_str_index (reader->cu,
20721 &reader->dwo_file->sections.str,
20722 &reader->dwo_file->sections.str_offsets,
20723 str_offsets_base, str_index);
20724}
20725
20726/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20727
20728static const char *
20729read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20730{
5e22e966 20731 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20732 const char *objf_name = objfile_name (objfile);
20733 static const char form_name[] = "DW_FORM_GNU_str_index";
20734 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20735
20736 if (!cu->str_offsets_base.has_value ())
20737 error (_("%s used in Fission stub without %s"
20738 " in CU at offset 0x%lx [in module %s]"),
20739 form_name, str_offsets_attr_name,
20740 (long) cu->header.offset_size, objf_name);
20741
20742 return read_str_index (cu,
5e22e966
SM
20743 &cu->per_objfile->per_bfd->str,
20744 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20745 *cu->str_offsets_base, str_index);
20746}
20747
3019eac3
DE
20748/* Return the length of an LEB128 number in BUF. */
20749
20750static int
20751leb128_size (const gdb_byte *buf)
20752{
20753 const gdb_byte *begin = buf;
20754 gdb_byte byte;
20755
20756 while (1)
20757 {
20758 byte = *buf++;
20759 if ((byte & 128) == 0)
20760 return buf - begin;
20761 }
20762}
20763
c906108c 20764static void
e142c38c 20765set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20766{
20767 switch (lang)
20768 {
20769 case DW_LANG_C89:
76bee0cc 20770 case DW_LANG_C99:
0cfd832f 20771 case DW_LANG_C11:
c906108c 20772 case DW_LANG_C:
d1be3247 20773 case DW_LANG_UPC:
e142c38c 20774 cu->language = language_c;
c906108c 20775 break;
9c37b5ae 20776 case DW_LANG_Java:
c906108c 20777 case DW_LANG_C_plus_plus:
0cfd832f
MW
20778 case DW_LANG_C_plus_plus_11:
20779 case DW_LANG_C_plus_plus_14:
e142c38c 20780 cu->language = language_cplus;
c906108c 20781 break;
6aecb9c2
JB
20782 case DW_LANG_D:
20783 cu->language = language_d;
20784 break;
c906108c
SS
20785 case DW_LANG_Fortran77:
20786 case DW_LANG_Fortran90:
b21b22e0 20787 case DW_LANG_Fortran95:
f7de9aab
MW
20788 case DW_LANG_Fortran03:
20789 case DW_LANG_Fortran08:
e142c38c 20790 cu->language = language_fortran;
c906108c 20791 break;
a766d390
DE
20792 case DW_LANG_Go:
20793 cu->language = language_go;
20794 break;
c906108c 20795 case DW_LANG_Mips_Assembler:
e142c38c 20796 cu->language = language_asm;
c906108c
SS
20797 break;
20798 case DW_LANG_Ada83:
8aaf0b47 20799 case DW_LANG_Ada95:
bc5f45f8
JB
20800 cu->language = language_ada;
20801 break;
72019c9c
GM
20802 case DW_LANG_Modula2:
20803 cu->language = language_m2;
20804 break;
fe8e67fd
PM
20805 case DW_LANG_Pascal83:
20806 cu->language = language_pascal;
20807 break;
22566fbd
DJ
20808 case DW_LANG_ObjC:
20809 cu->language = language_objc;
20810 break;
c44af4eb
TT
20811 case DW_LANG_Rust:
20812 case DW_LANG_Rust_old:
20813 cu->language = language_rust;
20814 break;
c906108c
SS
20815 case DW_LANG_Cobol74:
20816 case DW_LANG_Cobol85:
c906108c 20817 default:
e142c38c 20818 cu->language = language_minimal;
c906108c
SS
20819 break;
20820 }
e142c38c 20821 cu->language_defn = language_def (cu->language);
c906108c
SS
20822}
20823
20824/* Return the named attribute or NULL if not there. */
20825
20826static struct attribute *
e142c38c 20827dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20828{
a48e046c 20829 for (;;)
c906108c 20830 {
a48e046c
TT
20831 unsigned int i;
20832 struct attribute *spec = NULL;
20833
20834 for (i = 0; i < die->num_attrs; ++i)
20835 {
20836 if (die->attrs[i].name == name)
20837 return &die->attrs[i];
20838 if (die->attrs[i].name == DW_AT_specification
20839 || die->attrs[i].name == DW_AT_abstract_origin)
20840 spec = &die->attrs[i];
20841 }
20842
20843 if (!spec)
20844 break;
c906108c 20845
f2f0e013 20846 die = follow_die_ref (die, spec, &cu);
f2f0e013 20847 }
c5aa993b 20848
c906108c
SS
20849 return NULL;
20850}
20851
7d45c7c3
KB
20852/* Return the string associated with a string-typed attribute, or NULL if it
20853 is either not found or is of an incorrect type. */
20854
20855static const char *
20856dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20857{
20858 struct attribute *attr;
20859 const char *str = NULL;
20860
20861 attr = dwarf2_attr (die, name, cu);
20862
20863 if (attr != NULL)
20864 {
95f982e5 20865 str = attr->as_string ();
e61108c9 20866 if (str == nullptr)
dda83cd7 20867 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20868 "DIE at %s in module %s"),
20869 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20870 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20871 }
20872
20873 return str;
20874}
20875
a084a2a6 20876/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20877 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20878static const char *
20879dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20880{
20881 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20882 if (dwo_name == nullptr)
20883 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20884 return dwo_name;
20885}
20886
05cf31d1
JB
20887/* Return non-zero iff the attribute NAME is defined for the given DIE,
20888 and holds a non-zero value. This function should only be used for
2dc7f7b3 20889 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20890
20891static int
20892dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20893{
20894 struct attribute *attr = dwarf2_attr (die, name, cu);
20895
c45bc3f8 20896 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20897}
20898
3ca72b44 20899static int
e142c38c 20900die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20901{
05cf31d1
JB
20902 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20903 which value is non-zero. However, we have to be careful with
20904 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20905 (via dwarf2_flag_true_p) follows this attribute. So we may
20906 end up accidently finding a declaration attribute that belongs
20907 to a different DIE referenced by the specification attribute,
20908 even though the given DIE does not have a declaration attribute. */
20909 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20910 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20911}
20912
63d06c5c 20913/* Return the die giving the specification for DIE, if there is
f2f0e013 20914 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20915 containing the return value on output. If there is no
20916 specification, but there is an abstract origin, that is
20917 returned. */
63d06c5c
DC
20918
20919static struct die_info *
f2f0e013 20920die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20921{
f2f0e013
DJ
20922 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20923 *spec_cu);
63d06c5c 20924
edb3359d
DJ
20925 if (spec_attr == NULL)
20926 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20927
63d06c5c
DC
20928 if (spec_attr == NULL)
20929 return NULL;
20930 else
f2f0e013 20931 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20932}
c906108c 20933
527f3840
JK
20934/* Stub for free_line_header to match void * callback types. */
20935
20936static void
20937free_line_header_voidp (void *arg)
20938{
9a3c8263 20939 struct line_header *lh = (struct line_header *) arg;
527f3840 20940
fff8551c 20941 delete lh;
527f3840
JK
20942}
20943
83769d0b 20944/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20945
20946static struct dwarf2_section_info *
20947get_debug_line_section (struct dwarf2_cu *cu)
20948{
20949 struct dwarf2_section_info *section;
976ca316 20950 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20951
20952 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20953 DWO file. */
20954 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20955 section = &cu->dwo_unit->dwo_file->sections.line;
20956 else if (cu->per_cu->is_dwz)
20957 {
a7308ce0 20958 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
20959
20960 section = &dwz->line;
20961 }
20962 else
976ca316 20963 section = &per_objfile->per_bfd->line;
36586728
TT
20964
20965 return section;
20966}
20967
debd256d 20968/* Read the statement program header starting at OFFSET in
3019eac3 20969 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20970 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20971 Returns NULL if there is a problem reading the header, e.g., if it
20972 has a version we don't understand.
debd256d
JB
20973
20974 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20975 the returned object point into the dwarf line section buffer,
20976 and must not be freed. */
ae2de4f8 20977
fff8551c 20978static line_header_up
9c541725 20979dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20980{
3019eac3 20981 struct dwarf2_section_info *section;
976ca316 20982 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20983
36586728 20984 section = get_debug_line_section (cu);
976ca316 20985 section->read (per_objfile->objfile);
3019eac3 20986 if (section->buffer == NULL)
debd256d 20987 {
3019eac3 20988 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20989 complaint (_("missing .debug_line.dwo section"));
3019eac3 20990 else
b98664d3 20991 complaint (_("missing .debug_line section"));
debd256d
JB
20992 return 0;
20993 }
20994
0df7ad3a 20995 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20996 per_objfile, section, &cu->header);
debd256d 20997}
c906108c 20998
c6da4cef 20999/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 21000 Return the file name of the psymtab for the given file_entry.
c6da4cef 21001 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
21002 If space for the result is malloc'd, *NAME_HOLDER will be set.
21003 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 21004
d521ce57 21005static const char *
7ba99d21 21006psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 21007 const dwarf2_psymtab *pst,
c89b44cd
TT
21008 const char *comp_dir,
21009 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 21010{
d521ce57
TT
21011 const char *include_name = fe.name;
21012 const char *include_name_to_compare = include_name;
72b9f47f 21013 const char *pst_filename;
c6da4cef
DE
21014 int file_is_pst;
21015
8c43009f 21016 const char *dir_name = fe.include_dir (lh);
c6da4cef 21017
c89b44cd 21018 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
21019 if (!IS_ABSOLUTE_PATH (include_name)
21020 && (dir_name != NULL || comp_dir != NULL))
21021 {
21022 /* Avoid creating a duplicate psymtab for PST.
21023 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21024 Before we do the comparison, however, we need to account
21025 for DIR_NAME and COMP_DIR.
21026 First prepend dir_name (if non-NULL). If we still don't
21027 have an absolute path prepend comp_dir (if non-NULL).
21028 However, the directory we record in the include-file's
21029 psymtab does not contain COMP_DIR (to match the
21030 corresponding symtab(s)).
21031
21032 Example:
21033
21034 bash$ cd /tmp
21035 bash$ gcc -g ./hello.c
21036 include_name = "hello.c"
21037 dir_name = "."
21038 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
21039 DW_AT_name = "./hello.c"
21040
21041 */
c6da4cef
DE
21042
21043 if (dir_name != NULL)
21044 {
c89b44cd
TT
21045 name_holder->reset (concat (dir_name, SLASH_STRING,
21046 include_name, (char *) NULL));
21047 include_name = name_holder->get ();
c6da4cef 21048 include_name_to_compare = include_name;
c6da4cef
DE
21049 }
21050 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21051 {
c89b44cd
TT
21052 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21053 include_name, (char *) NULL));
21054 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
21055 }
21056 }
21057
21058 pst_filename = pst->filename;
c89b44cd 21059 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
21060 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21061 {
c89b44cd
TT
21062 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21063 pst_filename, (char *) NULL));
21064 pst_filename = copied_name.get ();
c6da4cef
DE
21065 }
21066
1e3fad37 21067 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 21068
c6da4cef
DE
21069 if (file_is_pst)
21070 return NULL;
21071 return include_name;
21072}
21073
d9b3de22
DE
21074/* State machine to track the state of the line number program. */
21075
6f77053d 21076class lnp_state_machine
d9b3de22 21077{
6f77053d
PA
21078public:
21079 /* Initialize a machine state for the start of a line number
21080 program. */
804d2729
TT
21081 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21082 bool record_lines_p);
6f77053d 21083
8c43009f
PA
21084 file_entry *current_file ()
21085 {
21086 /* lh->file_names is 0-based, but the file name numbers in the
21087 statement program are 1-based. */
6f77053d
PA
21088 return m_line_header->file_name_at (m_file);
21089 }
21090
21091 /* Record the line in the state machine. END_SEQUENCE is true if
21092 we're processing the end of a sequence. */
21093 void record_line (bool end_sequence);
21094
a8caed5d 21095 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21096 nop-out rest of the lines in this sequence. */
6f77053d
PA
21097 void check_line_address (struct dwarf2_cu *cu,
21098 const gdb_byte *line_ptr,
7ab6656f 21099 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21100
21101 void handle_set_discriminator (unsigned int discriminator)
21102 {
21103 m_discriminator = discriminator;
21104 m_line_has_non_zero_discriminator |= discriminator != 0;
21105 }
21106
21107 /* Handle DW_LNE_set_address. */
21108 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21109 {
21110 m_op_index = 0;
21111 address += baseaddr;
21112 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21113 }
21114
21115 /* Handle DW_LNS_advance_pc. */
21116 void handle_advance_pc (CORE_ADDR adjust);
21117
21118 /* Handle a special opcode. */
21119 void handle_special_opcode (unsigned char op_code);
21120
21121 /* Handle DW_LNS_advance_line. */
21122 void handle_advance_line (int line_delta)
21123 {
21124 advance_line (line_delta);
21125 }
21126
21127 /* Handle DW_LNS_set_file. */
21128 void handle_set_file (file_name_index file);
21129
21130 /* Handle DW_LNS_negate_stmt. */
21131 void handle_negate_stmt ()
21132 {
21133 m_is_stmt = !m_is_stmt;
21134 }
21135
21136 /* Handle DW_LNS_const_add_pc. */
21137 void handle_const_add_pc ();
21138
21139 /* Handle DW_LNS_fixed_advance_pc. */
21140 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21141 {
21142 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21143 m_op_index = 0;
21144 }
21145
21146 /* Handle DW_LNS_copy. */
21147 void handle_copy ()
21148 {
21149 record_line (false);
21150 m_discriminator = 0;
21151 }
21152
21153 /* Handle DW_LNE_end_sequence. */
21154 void handle_end_sequence ()
21155 {
804d2729 21156 m_currently_recording_lines = true;
6f77053d
PA
21157 }
21158
21159private:
21160 /* Advance the line by LINE_DELTA. */
21161 void advance_line (int line_delta)
21162 {
21163 m_line += line_delta;
21164
21165 if (line_delta != 0)
21166 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21167 }
21168
804d2729
TT
21169 struct dwarf2_cu *m_cu;
21170
6f77053d
PA
21171 gdbarch *m_gdbarch;
21172
21173 /* True if we're recording lines.
21174 Otherwise we're building partial symtabs and are just interested in
21175 finding include files mentioned by the line number program. */
21176 bool m_record_lines_p;
21177
8c43009f 21178 /* The line number header. */
6f77053d 21179 line_header *m_line_header;
8c43009f 21180
6f77053d
PA
21181 /* These are part of the standard DWARF line number state machine,
21182 and initialized according to the DWARF spec. */
d9b3de22 21183
6f77053d 21184 unsigned char m_op_index = 0;
7ba99d21
AT
21185 /* The line table index of the current file. */
21186 file_name_index m_file = 1;
6f77053d
PA
21187 unsigned int m_line = 1;
21188
21189 /* These are initialized in the constructor. */
21190
21191 CORE_ADDR m_address;
21192 bool m_is_stmt;
21193 unsigned int m_discriminator;
d9b3de22
DE
21194
21195 /* Additional bits of state we need to track. */
21196
21197 /* The last file that we called dwarf2_start_subfile for.
21198 This is only used for TLLs. */
6f77053d 21199 unsigned int m_last_file = 0;
d9b3de22 21200 /* The last file a line number was recorded for. */
6f77053d 21201 struct subfile *m_last_subfile = NULL;
d9b3de22 21202
1313c56e
AB
21203 /* The address of the last line entry. */
21204 CORE_ADDR m_last_address;
21205
21206 /* Set to true when a previous line at the same address (using
21207 m_last_address) had m_is_stmt true. This is reset to false when a
21208 line entry at a new address (m_address different to m_last_address) is
21209 processed. */
21210 bool m_stmt_at_address = false;
21211
804d2729
TT
21212 /* When true, record the lines we decode. */
21213 bool m_currently_recording_lines = false;
d9b3de22
DE
21214
21215 /* The last line number that was recorded, used to coalesce
21216 consecutive entries for the same line. This can happen, for
21217 example, when discriminators are present. PR 17276. */
6f77053d
PA
21218 unsigned int m_last_line = 0;
21219 bool m_line_has_non_zero_discriminator = false;
8c43009f 21220};
d9b3de22 21221
6f77053d
PA
21222void
21223lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21224{
21225 CORE_ADDR addr_adj = (((m_op_index + adjust)
21226 / m_line_header->maximum_ops_per_instruction)
21227 * m_line_header->minimum_instruction_length);
21228 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21229 m_op_index = ((m_op_index + adjust)
21230 % m_line_header->maximum_ops_per_instruction);
21231}
d9b3de22 21232
6f77053d
PA
21233void
21234lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21235{
6f77053d 21236 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21237 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21238 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21239 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21240 / m_line_header->maximum_ops_per_instruction)
21241 * m_line_header->minimum_instruction_length);
21242 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21243 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21244 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21245
258bf0ee 21246 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21247 advance_line (line_delta);
21248 record_line (false);
21249 m_discriminator = 0;
21250}
d9b3de22 21251
6f77053d
PA
21252void
21253lnp_state_machine::handle_set_file (file_name_index file)
21254{
21255 m_file = file;
21256
21257 const file_entry *fe = current_file ();
21258 if (fe == NULL)
21259 dwarf2_debug_line_missing_file_complaint ();
21260 else if (m_record_lines_p)
21261 {
21262 const char *dir = fe->include_dir (m_line_header);
21263
c24bdb02 21264 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21265 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21266 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21267 }
21268}
21269
21270void
21271lnp_state_machine::handle_const_add_pc ()
21272{
21273 CORE_ADDR adjust
21274 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21275
21276 CORE_ADDR addr_adj
21277 = (((m_op_index + adjust)
21278 / m_line_header->maximum_ops_per_instruction)
21279 * m_line_header->minimum_instruction_length);
21280
21281 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21282 m_op_index = ((m_op_index + adjust)
21283 % m_line_header->maximum_ops_per_instruction);
21284}
d9b3de22 21285
a05a36a5
DE
21286/* Return non-zero if we should add LINE to the line number table.
21287 LINE is the line to add, LAST_LINE is the last line that was added,
21288 LAST_SUBFILE is the subfile for LAST_LINE.
21289 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21290 had a non-zero discriminator.
21291
21292 We have to be careful in the presence of discriminators.
21293 E.g., for this line:
21294
21295 for (i = 0; i < 100000; i++);
21296
21297 clang can emit four line number entries for that one line,
21298 each with a different discriminator.
21299 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21300
21301 However, we want gdb to coalesce all four entries into one.
21302 Otherwise the user could stepi into the middle of the line and
21303 gdb would get confused about whether the pc really was in the
21304 middle of the line.
21305
21306 Things are further complicated by the fact that two consecutive
21307 line number entries for the same line is a heuristic used by gcc
21308 to denote the end of the prologue. So we can't just discard duplicate
21309 entries, we have to be selective about it. The heuristic we use is
21310 that we only collapse consecutive entries for the same line if at least
21311 one of those entries has a non-zero discriminator. PR 17276.
21312
21313 Note: Addresses in the line number state machine can never go backwards
21314 within one sequence, thus this coalescing is ok. */
21315
21316static int
804d2729
TT
21317dwarf_record_line_p (struct dwarf2_cu *cu,
21318 unsigned int line, unsigned int last_line,
a05a36a5
DE
21319 int line_has_non_zero_discriminator,
21320 struct subfile *last_subfile)
21321{
c24bdb02 21322 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21323 return 1;
21324 if (line != last_line)
21325 return 1;
21326 /* Same line for the same file that we've seen already.
21327 As a last check, for pr 17276, only record the line if the line
21328 has never had a non-zero discriminator. */
21329 if (!line_has_non_zero_discriminator)
21330 return 1;
21331 return 0;
21332}
21333
804d2729
TT
21334/* Use the CU's builder to record line number LINE beginning at
21335 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21336
21337static void
d9b3de22 21338dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21339 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21340 struct dwarf2_cu *cu)
252a6764
DE
21341{
21342 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21343
27e0867f
DE
21344 if (dwarf_line_debug)
21345 {
21346 fprintf_unfiltered (gdb_stdlog,
21347 "Recording line %u, file %s, address %s\n",
21348 line, lbasename (subfile->name),
21349 paddress (gdbarch, address));
21350 }
21351
804d2729 21352 if (cu != nullptr)
8c95582d 21353 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21354}
21355
21356/* Subroutine of dwarf_decode_lines_1 to simplify it.
21357 Mark the end of a set of line number records.
d9b3de22 21358 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21359 If SUBFILE is NULL the request is ignored. */
21360
21361static void
21362dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21363 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21364{
27e0867f
DE
21365 if (subfile == NULL)
21366 return;
21367
21368 if (dwarf_line_debug)
21369 {
21370 fprintf_unfiltered (gdb_stdlog,
21371 "Finishing current line, file %s, address %s\n",
21372 lbasename (subfile->name),
21373 paddress (gdbarch, address));
21374 }
21375
8c95582d 21376 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21377}
21378
6f77053d
PA
21379void
21380lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21381{
d9b3de22
DE
21382 if (dwarf_line_debug)
21383 {
21384 fprintf_unfiltered (gdb_stdlog,
21385 "Processing actual line %u: file %u,"
94a72be7 21386 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21387 m_line, m_file,
6f77053d 21388 paddress (m_gdbarch, m_address),
94a72be7
AB
21389 m_is_stmt, m_discriminator,
21390 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21391 }
21392
6f77053d 21393 file_entry *fe = current_file ();
8c43009f
PA
21394
21395 if (fe == NULL)
d9b3de22
DE
21396 dwarf2_debug_line_missing_file_complaint ();
21397 /* For now we ignore lines not starting on an instruction boundary.
21398 But not when processing end_sequence for compatibility with the
21399 previous version of the code. */
6f77053d 21400 else if (m_op_index == 0 || end_sequence)
d9b3de22 21401 {
8c43009f 21402 fe->included_p = 1;
8c95582d 21403 if (m_record_lines_p)
d9b3de22 21404 {
1313c56e
AB
21405 /* When we switch files we insert an end maker in the first file,
21406 switch to the second file and add a new line entry. The
21407 problem is that the end marker inserted in the first file will
21408 discard any previous line entries at the same address. If the
21409 line entries in the first file are marked as is-stmt, while
21410 the new line in the second file is non-stmt, then this means
21411 the end marker will discard is-stmt lines so we can have a
21412 non-stmt line. This means that there are less addresses at
21413 which the user can insert a breakpoint.
21414
21415 To improve this we track the last address in m_last_address,
21416 and whether we have seen an is-stmt at this address. Then
21417 when switching files, if we have seen a stmt at the current
21418 address, and we are switching to create a non-stmt line, then
21419 discard the new line. */
21420 bool file_changed
21421 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21422 bool ignore_this_line
dda83cd7
SM
21423 = ((file_changed && !end_sequence && m_last_address == m_address
21424 && !m_is_stmt && m_stmt_at_address)
21425 || (!end_sequence && m_line == 0));
1313c56e
AB
21426
21427 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21428 {
804d2729
TT
21429 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21430 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21431 }
21432
1313c56e 21433 if (!end_sequence && !ignore_this_line)
d9b3de22 21434 {
8c95582d
AB
21435 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21436
804d2729 21437 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21438 m_line_has_non_zero_discriminator,
21439 m_last_subfile))
d9b3de22 21440 {
c24bdb02 21441 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21442 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21443 builder->get_current_subfile (),
8c95582d 21444 m_line, m_address, is_stmt,
804d2729 21445 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21446 }
c24bdb02 21447 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21448 m_last_line = m_line;
d9b3de22
DE
21449 }
21450 }
21451 }
1313c56e
AB
21452
21453 /* Track whether we have seen any m_is_stmt true at m_address in case we
21454 have multiple line table entries all at m_address. */
21455 if (m_last_address != m_address)
21456 {
21457 m_stmt_at_address = false;
21458 m_last_address = m_address;
21459 }
21460 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21461}
21462
804d2729
TT
21463lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21464 line_header *lh, bool record_lines_p)
d9b3de22 21465{
804d2729 21466 m_cu = cu;
6f77053d
PA
21467 m_gdbarch = arch;
21468 m_record_lines_p = record_lines_p;
21469 m_line_header = lh;
d9b3de22 21470
804d2729 21471 m_currently_recording_lines = true;
d9b3de22 21472
d9b3de22
DE
21473 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21474 was a line entry for it so that the backend has a chance to adjust it
21475 and also record it in case it needs it. This is currently used by MIPS
21476 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21477 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21478 m_is_stmt = lh->default_is_stmt;
21479 m_discriminator = 0;
1313c56e
AB
21480
21481 m_last_address = m_address;
21482 m_stmt_at_address = false;
252a6764
DE
21483}
21484
6f77053d
PA
21485void
21486lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21487 const gdb_byte *line_ptr,
7ab6656f 21488 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21489{
a8caed5d
FS
21490 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21491 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21492 located at 0x0. In this case, additionally check that if
21493 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21494
a8caed5d
FS
21495 if ((address == 0 && address < unrelocated_lowpc)
21496 || address == (CORE_ADDR) -1)
924c2928
DE
21497 {
21498 /* This line table is for a function which has been
21499 GCd by the linker. Ignore it. PR gdb/12528 */
21500
5e22e966 21501 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21502 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21503
b98664d3 21504 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21505 line_offset, objfile_name (objfile));
804d2729
TT
21506 m_currently_recording_lines = false;
21507 /* Note: m_currently_recording_lines is left as false until we see
21508 DW_LNE_end_sequence. */
924c2928
DE
21509 }
21510}
21511
f3f5162e 21512/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21513 Process the line number information in LH.
21514 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21515 program in order to set included_p for every referenced header. */
debd256d 21516
c906108c 21517static void
43f3e411
DE
21518dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21519 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21520{
d521ce57
TT
21521 const gdb_byte *line_ptr, *extended_end;
21522 const gdb_byte *line_end;
a8c50c1f 21523 unsigned int bytes_read, extended_len;
699ca60a 21524 unsigned char op_code, extended_op;
e142c38c 21525 CORE_ADDR baseaddr;
5e22e966 21526 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21527 bfd *abfd = objfile->obfd;
08feed99 21528 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21529 /* True if we're recording line info (as opposed to building partial
21530 symtabs and just interested in finding include files mentioned by
21531 the line number program). */
21532 bool record_lines_p = !decode_for_pst_p;
e142c38c 21533
b3b3bada 21534 baseaddr = objfile->text_section_offset ();
c906108c 21535
debd256d
JB
21536 line_ptr = lh->statement_program_start;
21537 line_end = lh->statement_program_end;
c906108c
SS
21538
21539 /* Read the statement sequences until there's nothing left. */
21540 while (line_ptr < line_end)
21541 {
6f77053d
PA
21542 /* The DWARF line number program state machine. Reset the state
21543 machine at the start of each sequence. */
804d2729 21544 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21545 bool end_sequence = false;
d9b3de22 21546
8c43009f 21547 if (record_lines_p)
c906108c 21548 {
8c43009f
PA
21549 /* Start a subfile for the current file of the state
21550 machine. */
21551 const file_entry *fe = state_machine.current_file ();
21552
21553 if (fe != NULL)
804d2729 21554 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21555 }
21556
a738430d 21557 /* Decode the table. */
d9b3de22 21558 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21559 {
21560 op_code = read_1_byte (abfd, line_ptr);
21561 line_ptr += 1;
9aa1fe7e 21562
debd256d 21563 if (op_code >= lh->opcode_base)
6e70227d 21564 {
8e07a239 21565 /* Special opcode. */
6f77053d 21566 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21567 }
21568 else switch (op_code)
c906108c
SS
21569 {
21570 case DW_LNS_extended_op:
3e43a32a
MS
21571 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21572 &bytes_read);
473b7be6 21573 line_ptr += bytes_read;
a8c50c1f 21574 extended_end = line_ptr + extended_len;
c906108c
SS
21575 extended_op = read_1_byte (abfd, line_ptr);
21576 line_ptr += 1;
8f34b746
TV
21577 if (DW_LNE_lo_user <= extended_op
21578 && extended_op <= DW_LNE_hi_user)
21579 {
21580 /* Vendor extension, ignore. */
21581 line_ptr = extended_end;
21582 break;
21583 }
c906108c
SS
21584 switch (extended_op)
21585 {
21586 case DW_LNE_end_sequence:
6f77053d
PA
21587 state_machine.handle_end_sequence ();
21588 end_sequence = true;
c906108c
SS
21589 break;
21590 case DW_LNE_set_address:
d9b3de22
DE
21591 {
21592 CORE_ADDR address
c8a7a66f 21593 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21594 line_ptr += bytes_read;
6f77053d
PA
21595
21596 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21597 lowpc - baseaddr, address);
6f77053d 21598 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21599 }
c906108c
SS
21600 break;
21601 case DW_LNE_define_file:
dda83cd7
SM
21602 {
21603 const char *cur_file;
ecfb656c
PA
21604 unsigned int mod_time, length;
21605 dir_index dindex;
6e70227d 21606
dda83cd7 21607 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21608 &bytes_read);
dda83cd7
SM
21609 line_ptr += bytes_read;
21610 dindex = (dir_index)
21611 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21612 line_ptr += bytes_read;
21613 mod_time =
21614 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21615 line_ptr += bytes_read;
21616 length =
21617 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21618 line_ptr += bytes_read;
21619 lh->add_file_name (cur_file, dindex, mod_time, length);
21620 }
c906108c 21621 break;
d0c6ba3d 21622 case DW_LNE_set_discriminator:
6f77053d
PA
21623 {
21624 /* The discriminator is not interesting to the
21625 debugger; just ignore it. We still need to
21626 check its value though:
21627 if there are consecutive entries for the same
21628 (non-prologue) line we want to coalesce them.
21629 PR 17276. */
21630 unsigned int discr
21631 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21632 line_ptr += bytes_read;
21633
21634 state_machine.handle_set_discriminator (discr);
21635 }
d0c6ba3d 21636 break;
c906108c 21637 default:
b98664d3 21638 complaint (_("mangled .debug_line section"));
debd256d 21639 return;
c906108c 21640 }
a8c50c1f
DJ
21641 /* Make sure that we parsed the extended op correctly. If e.g.
21642 we expected a different address size than the producer used,
21643 we may have read the wrong number of bytes. */
21644 if (line_ptr != extended_end)
21645 {
b98664d3 21646 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21647 return;
21648 }
c906108c
SS
21649 break;
21650 case DW_LNS_copy:
6f77053d 21651 state_machine.handle_copy ();
c906108c
SS
21652 break;
21653 case DW_LNS_advance_pc:
2dc7f7b3
TT
21654 {
21655 CORE_ADDR adjust
21656 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21657 line_ptr += bytes_read;
6f77053d
PA
21658
21659 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21660 }
c906108c
SS
21661 break;
21662 case DW_LNS_advance_line:
a05a36a5
DE
21663 {
21664 int line_delta
21665 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21666 line_ptr += bytes_read;
6f77053d
PA
21667
21668 state_machine.handle_advance_line (line_delta);
a05a36a5 21669 }
c906108c
SS
21670 break;
21671 case DW_LNS_set_file:
d9b3de22 21672 {
6f77053d 21673 file_name_index file
ecfb656c
PA
21674 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21675 &bytes_read);
d9b3de22 21676 line_ptr += bytes_read;
8c43009f 21677
6f77053d 21678 state_machine.handle_set_file (file);
d9b3de22 21679 }
c906108c
SS
21680 break;
21681 case DW_LNS_set_column:
0ad93d4f 21682 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21683 line_ptr += bytes_read;
21684 break;
21685 case DW_LNS_negate_stmt:
6f77053d 21686 state_machine.handle_negate_stmt ();
c906108c
SS
21687 break;
21688 case DW_LNS_set_basic_block:
c906108c 21689 break;
c2c6d25f
JM
21690 /* Add to the address register of the state machine the
21691 address increment value corresponding to special opcode
a738430d
MK
21692 255. I.e., this value is scaled by the minimum
21693 instruction length since special opcode 255 would have
b021a221 21694 scaled the increment. */
c906108c 21695 case DW_LNS_const_add_pc:
6f77053d 21696 state_machine.handle_const_add_pc ();
c906108c
SS
21697 break;
21698 case DW_LNS_fixed_advance_pc:
3e29f34a 21699 {
6f77053d 21700 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21701 line_ptr += 2;
6f77053d
PA
21702
21703 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21704 }
c906108c 21705 break;
9aa1fe7e 21706 default:
a738430d
MK
21707 {
21708 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21709 int i;
a738430d 21710
debd256d 21711 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21712 {
21713 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21714 line_ptr += bytes_read;
21715 }
21716 }
c906108c
SS
21717 }
21718 }
d9b3de22
DE
21719
21720 if (!end_sequence)
21721 dwarf2_debug_line_missing_end_sequence_complaint ();
21722
21723 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21724 in which case we still finish recording the last line). */
6f77053d 21725 state_machine.record_line (true);
c906108c 21726 }
f3f5162e
DE
21727}
21728
21729/* Decode the Line Number Program (LNP) for the given line_header
21730 structure and CU. The actual information extracted and the type
21731 of structures created from the LNP depends on the value of PST.
21732
21733 1. If PST is NULL, then this procedure uses the data from the program
21734 to create all necessary symbol tables, and their linetables.
21735
21736 2. If PST is not NULL, this procedure reads the program to determine
21737 the list of files included by the unit represented by PST, and
21738 builds all the associated partial symbol tables.
21739
21740 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21741 It is used for relative paths in the line table.
21742 NOTE: When processing partial symtabs (pst != NULL),
21743 comp_dir == pst->dirname.
21744
21745 NOTE: It is important that psymtabs have the same file name (via strcmp)
21746 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21747 symtab we don't use it in the name of the psymtabs we create.
21748 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21749 A good testcase for this is mb-inline.exp.
21750
527f3840
JK
21751 LOWPC is the lowest address in CU (or 0 if not known).
21752
21753 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21754 for its PC<->lines mapping information. Otherwise only the filename
21755 table is read in. */
f3f5162e
DE
21756
21757static void
21758dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21759 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21760 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21761{
5e22e966 21762 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21763 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21764
527f3840
JK
21765 if (decode_mapping)
21766 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21767
21768 if (decode_for_pst_p)
21769 {
aaa75496 21770 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21771 create the psymtab of each included file. */
7ba99d21 21772 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21773 if (file_entry.included_p == 1)
21774 {
c89b44cd 21775 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21776 const char *include_name =
7ba99d21
AT
21777 psymtab_include_file_name (lh, file_entry, pst,
21778 comp_dir, &name_holder);
c6da4cef 21779 if (include_name != NULL)
7b249e47
TT
21780 dwarf2_create_include_psymtab
21781 (cu->per_objfile->per_bfd, include_name, pst,
21782 cu->per_objfile->per_bfd->partial_symtabs.get (),
0072c873 21783 objfile->per_bfd);
dda83cd7 21784 }
aaa75496 21785 }
cb1df416
DJ
21786 else
21787 {
21788 /* Make sure a symtab is created for every file, even files
21789 which contain only variables (i.e. no code with associated
21790 line numbers). */
c24bdb02
KS
21791 buildsym_compunit *builder = cu->get_builder ();
21792 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21793
7ba99d21 21794 for (auto &fe : lh->file_names ())
cb1df416 21795 {
804d2729 21796 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21797 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21798 {
c24bdb02 21799 builder->get_current_subfile ()->symtab
804d2729 21800 = allocate_symtab (cust,
c24bdb02 21801 builder->get_current_subfile ()->name);
43f3e411 21802 }
c24bdb02 21803 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21804 }
21805 }
c906108c
SS
21806}
21807
21808/* Start a subfile for DWARF. FILENAME is the name of the file and
21809 DIRNAME the name of the source directory which contains FILENAME
4d663531 21810 or NULL if not known.
c906108c
SS
21811 This routine tries to keep line numbers from identical absolute and
21812 relative file names in a common subfile.
21813
21814 Using the `list' example from the GDB testsuite, which resides in
21815 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21816 of /srcdir/list0.c yields the following debugging information for list0.c:
21817
c5aa993b 21818 DW_AT_name: /srcdir/list0.c
4d663531 21819 DW_AT_comp_dir: /compdir
357e46e7 21820 files.files[0].name: list0.h
c5aa993b 21821 files.files[0].dir: /srcdir
357e46e7 21822 files.files[1].name: list0.c
c5aa993b 21823 files.files[1].dir: /srcdir
c906108c
SS
21824
21825 The line number information for list0.c has to end up in a single
4f1520fb
FR
21826 subfile, so that `break /srcdir/list0.c:1' works as expected.
21827 start_subfile will ensure that this happens provided that we pass the
21828 concatenation of files.files[1].dir and files.files[1].name as the
21829 subfile's name. */
c906108c
SS
21830
21831static void
804d2729
TT
21832dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21833 const char *dirname)
c906108c 21834{
43816ebc 21835 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21836
4d663531 21837 /* In order not to lose the line information directory,
4f1520fb
FR
21838 we concatenate it to the filename when it makes sense.
21839 Note that the Dwarf3 standard says (speaking of filenames in line
21840 information): ``The directory index is ignored for file names
21841 that represent full path names''. Thus ignoring dirname in the
21842 `else' branch below isn't an issue. */
c906108c 21843
d5166ae1 21844 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21845 {
43816ebc
TT
21846 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21847 filename = copy.get ();
d521ce57 21848 }
c906108c 21849
c24bdb02 21850 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21851}
21852
804d2729
TT
21853/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21854 buildsym_compunit constructor. */
f4dc4d17 21855
c24bdb02
KS
21856struct compunit_symtab *
21857dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21858 CORE_ADDR low_pc)
f4dc4d17 21859{
c24bdb02 21860 gdb_assert (m_builder == nullptr);
43f3e411 21861
c24bdb02 21862 m_builder.reset (new struct buildsym_compunit
f6e649dd 21863 (this->per_objfile->objfile,
c24bdb02 21864 name, comp_dir, language, low_pc));
93b8bea4 21865
c24bdb02 21866 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21867
c24bdb02
KS
21868 get_builder ()->record_debugformat ("DWARF 2");
21869 get_builder ()->record_producer (producer);
f4dc4d17 21870
c24bdb02 21871 processing_has_namespace_info = false;
43f3e411 21872
c24bdb02 21873 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21874}
21875
4c2df51b
DJ
21876static void
21877var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21878 struct dwarf2_cu *cu)
4c2df51b 21879{
5e22e966 21880 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21881 struct comp_unit_head *cu_header = &cu->header;
21882
4c2df51b
DJ
21883 /* NOTE drow/2003-01-30: There used to be a comment and some special
21884 code here to turn a symbol with DW_AT_external and a
21885 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21886 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21887 with some versions of binutils) where shared libraries could have
21888 relocations against symbols in their debug information - the
21889 minimal symbol would have the right address, but the debug info
21890 would not. It's no longer necessary, because we will explicitly
21891 apply relocations when we read in the debug information now. */
21892
21893 /* A DW_AT_location attribute with no contents indicates that a
21894 variable has been optimized away. */
9d2246fc 21895 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21896 {
f1e6e072 21897 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21898 return;
21899 }
21900
21901 /* Handle one degenerate form of location expression specially, to
21902 preserve GDB's previous behavior when section offsets are
336d760d
AT
21903 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21904 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21905
9d2246fc
TT
21906 if (attr->form_is_block ())
21907 {
21908 struct dwarf_block *block = attr->as_block ();
21909
21910 if ((block->data[0] == DW_OP_addr
21911 && block->size == 1 + cu_header->addr_size)
21912 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21913 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21914 && (block->size
21915 == 1 + leb128_size (&block->data[1]))))
21916 {
21917 unsigned int dummy;
21918
21919 if (block->data[0] == DW_OP_addr)
21920 SET_SYMBOL_VALUE_ADDRESS
21921 (sym, cu->header.read_address (objfile->obfd,
21922 block->data + 1,
38583298 21923 &dummy));
9d2246fc
TT
21924 else
21925 SET_SYMBOL_VALUE_ADDRESS
21926 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21927 &dummy));
21928 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21929 fixup_symbol_section (sym, objfile);
21930 SET_SYMBOL_VALUE_ADDRESS
21931 (sym,
21932 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 21933 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
21934 return;
21935 }
4c2df51b
DJ
21936 }
21937
21938 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21939 expression evaluator, and use LOC_COMPUTED only when necessary
21940 (i.e. when the value of a register or memory location is
21941 referenced, or a thread-local block, etc.). Then again, it might
21942 not be worthwhile. I'm assuming that it isn't unless performance
21943 or memory numbers show me otherwise. */
21944
f1e6e072 21945 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21946
f1e6e072 21947 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21948 cu->has_loclist = true;
4c2df51b
DJ
21949}
21950
c906108c
SS
21951/* Given a pointer to a DWARF information entry, figure out if we need
21952 to make a symbol table entry for it, and if so, create a new entry
21953 and return a pointer to it.
21954 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21955 used the passed type.
21956 If SPACE is not NULL, use it to hold the new symbol. If it is
21957 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21958
21959static struct symbol *
5e2db402
TT
21960new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21961 struct symbol *space)
c906108c 21962{
976ca316
SM
21963 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21964 struct objfile *objfile = per_objfile->objfile;
08feed99 21965 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21966 struct symbol *sym = NULL;
15d034d0 21967 const char *name;
c906108c
SS
21968 struct attribute *attr = NULL;
21969 struct attribute *attr2 = NULL;
e142c38c 21970 CORE_ADDR baseaddr;
e37fd15a
SW
21971 struct pending **list_to_add = NULL;
21972
edb3359d 21973 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21974
b3b3bada 21975 baseaddr = objfile->text_section_offset ();
c906108c 21976
94af9270 21977 name = dwarf2_name (die, cu);
c906108c
SS
21978 if (name)
21979 {
34eaf542 21980 int suppress_add = 0;
94af9270 21981
34eaf542
TT
21982 if (space)
21983 sym = space;
21984 else
8c14c3a3 21985 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21986 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21987
21988 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21989 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
21990 /* Fortran does not have mangling standard and the mangling does differ
21991 between gfortran, iFort etc. */
bcfe6157
TT
21992 const char *physname
21993 = (cu->language == language_fortran
21994 ? dwarf2_full_name (name, die, cu)
21995 : dwarf2_physname (name, die, cu));
21996 const char *linkagename = dw2_linkage_name (die, cu);
21997
21998 if (linkagename == nullptr || cu->language == language_ada)
21999 sym->set_linkage_name (physname);
22000 else
22001 {
22002 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22003 sym->set_linkage_name (linkagename);
22004 }
f55ee35c 22005
c906108c 22006 /* Default assumptions.
dda83cd7 22007 Use the passed type or decode it from the die. */
176620f1 22008 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 22009 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
22010 if (type != NULL)
22011 SYMBOL_TYPE (sym) = type;
22012 else
e7c27a73 22013 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
22014 attr = dwarf2_attr (die,
22015 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22016 cu);
435d3d88 22017 if (attr != nullptr)
529908cb 22018 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 22019
edb3359d
DJ
22020 attr = dwarf2_attr (die,
22021 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22022 cu);
aa659cfa 22023 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 22024 {
529908cb 22025 file_name_index file_index
aa659cfa 22026 = (file_name_index) attr->as_nonnegative ();
8c43009f 22027 struct file_entry *fe;
9a619af0 22028
ecfb656c
PA
22029 if (cu->line_header != NULL)
22030 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
22031 else
22032 fe = NULL;
22033
22034 if (fe == NULL)
b98664d3 22035 complaint (_("file index out of range"));
8c43009f
PA
22036 else
22037 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
22038 }
22039
c906108c
SS
22040 switch (die->tag)
22041 {
22042 case DW_TAG_label:
e142c38c 22043 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 22044 if (attr != nullptr)
3e29f34a
MR
22045 {
22046 CORE_ADDR addr;
22047
95f982e5 22048 addr = attr->as_address ();
3e29f34a 22049 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 22050 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 22051 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 22052 }
8f5c6526
TV
22053 else
22054 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
22055 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22056 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 22057 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
22058 break;
22059 case DW_TAG_subprogram:
22060 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22061 finish_block. */
f1e6e072 22062 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 22063 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 22064 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
22065 || cu->language == language_ada
22066 || cu->language == language_fortran)
c906108c 22067 {
dda83cd7
SM
22068 /* Subprograms marked external are stored as a global symbol.
22069 Ada and Fortran subprograms, whether marked external or
22070 not, are always stored as a global symbol, because we want
22071 to be able to access them globally. For instance, we want
22072 to be able to break on a nested subprogram without having
22073 to specify the context. */
c24bdb02 22074 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
22075 }
22076 else
22077 {
e37fd15a 22078 list_to_add = cu->list_in_scope;
c906108c
SS
22079 }
22080 break;
edb3359d
DJ
22081 case DW_TAG_inlined_subroutine:
22082 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22083 finish_block. */
f1e6e072 22084 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22085 SYMBOL_INLINED (sym) = 1;
481860b3 22086 list_to_add = cu->list_in_scope;
edb3359d 22087 break;
34eaf542
TT
22088 case DW_TAG_template_value_param:
22089 suppress_add = 1;
22090 /* Fall through. */
72929c62 22091 case DW_TAG_constant:
c906108c 22092 case DW_TAG_variable:
254e6b9e 22093 case DW_TAG_member:
0963b4bd
MS
22094 /* Compilation with minimal debug info may result in
22095 variables with missing type entries. Change the
22096 misleading `void' type to something sensible. */
78134374 22097 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22098 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22099
e142c38c 22100 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22101 /* In the case of DW_TAG_member, we should only be called for
22102 static const members. */
22103 if (die->tag == DW_TAG_member)
22104 {
3863f96c
DE
22105 /* dwarf2_add_field uses die_is_declaration,
22106 so we do the same. */
254e6b9e
DE
22107 gdb_assert (die_is_declaration (die, cu));
22108 gdb_assert (attr);
22109 }
435d3d88 22110 if (attr != nullptr)
c906108c 22111 {
e7c27a73 22112 dwarf2_const_value (attr, sym, cu);
e142c38c 22113 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22114 if (!suppress_add)
34eaf542 22115 {
c45bc3f8 22116 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22117 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22118 else
e37fd15a 22119 list_to_add = cu->list_in_scope;
34eaf542 22120 }
c906108c
SS
22121 break;
22122 }
e142c38c 22123 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22124 if (attr != nullptr)
c906108c 22125 {
e7c27a73 22126 var_decode_location (attr, sym, cu);
e142c38c 22127 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22128
22129 /* Fortran explicitly imports any global symbols to the local
22130 scope by DW_TAG_common_block. */
22131 if (cu->language == language_fortran && die->parent
22132 && die->parent->tag == DW_TAG_common_block)
22133 attr2 = NULL;
22134
caac4577
JG
22135 if (SYMBOL_CLASS (sym) == LOC_STATIC
22136 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22137 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22138 {
22139 /* When a static variable is eliminated by the linker,
22140 the corresponding debug information is not stripped
22141 out, but the variable address is set to null;
22142 do not add such variables into symbol table. */
22143 }
c45bc3f8 22144 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22145 {
4b610737
TT
22146 if (SYMBOL_CLASS (sym) == LOC_STATIC
22147 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22148 && per_objfile->per_bfd->can_copy)
4b610737
TT
22149 {
22150 /* A global static variable might be subject to
22151 copy relocation. We first check for a local
22152 minsym, though, because maybe the symbol was
22153 marked hidden, in which case this would not
22154 apply. */
22155 bound_minimal_symbol found
22156 = (lookup_minimal_symbol_linkage
987012b8 22157 (sym->linkage_name (), objfile));
4b610737
TT
22158 if (found.minsym != nullptr)
22159 sym->maybe_copied = 1;
22160 }
f55ee35c 22161
1c809c68
TT
22162 /* A variable with DW_AT_external is never static,
22163 but it may be block-scoped. */
804d2729 22164 list_to_add
c24bdb02
KS
22165 = ((cu->list_in_scope
22166 == cu->get_builder ()->get_file_symbols ())
22167 ? cu->get_builder ()->get_global_symbols ()
804d2729 22168 : cu->list_in_scope);
1c809c68 22169 }
c906108c 22170 else
e37fd15a 22171 list_to_add = cu->list_in_scope;
c906108c
SS
22172 }
22173 else
22174 {
22175 /* We do not know the address of this symbol.
dda83cd7
SM
22176 If it is an external symbol and we have type information
22177 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22178 The address of the variable will then be determined from
22179 the minimal symbol table whenever the variable is
22180 referenced. */
e142c38c 22181 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22182
22183 /* Fortran explicitly imports any global symbols to the local
22184 scope by DW_TAG_common_block. */
22185 if (cu->language == language_fortran && die->parent
22186 && die->parent->tag == DW_TAG_common_block)
22187 {
22188 /* SYMBOL_CLASS doesn't matter here because
22189 read_common_block is going to reset it. */
22190 if (!suppress_add)
22191 list_to_add = cu->list_in_scope;
22192 }
c45bc3f8 22193 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22194 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22195 {
0fe7935b
DJ
22196 /* A variable with DW_AT_external is never static, but it
22197 may be block-scoped. */
804d2729 22198 list_to_add
c24bdb02
KS
22199 = ((cu->list_in_scope
22200 == cu->get_builder ()->get_file_symbols ())
22201 ? cu->get_builder ()->get_global_symbols ()
804d2729 22202 : cu->list_in_scope);
0fe7935b 22203
f1e6e072 22204 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22205 }
442ddf59
JK
22206 else if (!die_is_declaration (die, cu))
22207 {
22208 /* Use the default LOC_OPTIMIZED_OUT class. */
22209 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22210 if (!suppress_add)
22211 list_to_add = cu->list_in_scope;
442ddf59 22212 }
c906108c
SS
22213 }
22214 break;
22215 case DW_TAG_formal_parameter:
a60f3166
TT
22216 {
22217 /* If we are inside a function, mark this as an argument. If
22218 not, we might be looking at an argument to an inlined function
22219 when we do not have enough information to show inlined frames;
22220 pretend it's a local variable in that case so that the user can
22221 still see it. */
804d2729 22222 struct context_stack *curr
c24bdb02 22223 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22224 if (curr != nullptr && curr->name != nullptr)
22225 SYMBOL_IS_ARGUMENT (sym) = 1;
22226 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22227 if (attr != nullptr)
a60f3166
TT
22228 {
22229 var_decode_location (attr, sym, cu);
22230 }
22231 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22232 if (attr != nullptr)
a60f3166
TT
22233 {
22234 dwarf2_const_value (attr, sym, cu);
22235 }
f346a30d 22236
a60f3166
TT
22237 list_to_add = cu->list_in_scope;
22238 }
c906108c
SS
22239 break;
22240 case DW_TAG_unspecified_parameters:
22241 /* From varargs functions; gdb doesn't seem to have any
22242 interest in this information, so just ignore it for now.
22243 (FIXME?) */
22244 break;
34eaf542
TT
22245 case DW_TAG_template_type_param:
22246 suppress_add = 1;
22247 /* Fall through. */
c906108c 22248 case DW_TAG_class_type:
680b30c7 22249 case DW_TAG_interface_type:
c906108c
SS
22250 case DW_TAG_structure_type:
22251 case DW_TAG_union_type:
72019c9c 22252 case DW_TAG_set_type:
c906108c 22253 case DW_TAG_enumeration_type:
f1e6e072 22254 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22255 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22256
63d06c5c 22257 {
9c37b5ae 22258 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22259 really ever be static objects: otherwise, if you try
22260 to, say, break of a class's method and you're in a file
22261 which doesn't mention that class, it won't work unless
22262 the check for all static symbols in lookup_symbol_aux
22263 saves you. See the OtherFileClass tests in
22264 gdb.c++/namespace.exp. */
22265
e37fd15a 22266 if (!suppress_add)
34eaf542 22267 {
c24bdb02 22268 buildsym_compunit *builder = cu->get_builder ();
804d2729 22269 list_to_add
c24bdb02 22270 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22271 && cu->language == language_cplus
c24bdb02 22272 ? builder->get_global_symbols ()
804d2729 22273 : cu->list_in_scope);
63d06c5c 22274
64382290 22275 /* The semantics of C++ state that "struct foo {
9c37b5ae 22276 ... }" also defines a typedef for "foo". */
64382290 22277 if (cu->language == language_cplus
45280282 22278 || cu->language == language_ada
c44af4eb
TT
22279 || cu->language == language_d
22280 || cu->language == language_rust)
64382290
TT
22281 {
22282 /* The symbol's name is already allocated along
22283 with this objfile, so we don't need to
22284 duplicate it for the type. */
7d93a1e0 22285 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22286 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22287 }
63d06c5c
DC
22288 }
22289 }
c906108c
SS
22290 break;
22291 case DW_TAG_typedef:
f1e6e072 22292 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22293 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22294 list_to_add = cu->list_in_scope;
63d06c5c 22295 break;
d8f62e84 22296 case DW_TAG_array_type:
c906108c 22297 case DW_TAG_base_type:
dda83cd7 22298 case DW_TAG_subrange_type:
f1e6e072 22299 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22300 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22301 list_to_add = cu->list_in_scope;
c906108c
SS
22302 break;
22303 case DW_TAG_enumerator:
e142c38c 22304 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22305 if (attr != nullptr)
c906108c 22306 {
e7c27a73 22307 dwarf2_const_value (attr, sym, cu);
c906108c 22308 }
63d06c5c
DC
22309 {
22310 /* NOTE: carlton/2003-11-10: See comment above in the
22311 DW_TAG_class_type, etc. block. */
22312
804d2729 22313 list_to_add
c24bdb02 22314 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22315 && cu->language == language_cplus
c24bdb02 22316 ? cu->get_builder ()->get_global_symbols ()
804d2729 22317 : cu->list_in_scope);
63d06c5c 22318 }
c906108c 22319 break;
74921315 22320 case DW_TAG_imported_declaration:
5c4e30ca 22321 case DW_TAG_namespace:
f1e6e072 22322 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22323 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22324 break;
530e8392
KB
22325 case DW_TAG_module:
22326 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22327 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22328 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22329 break;
4357ac6c 22330 case DW_TAG_common_block:
f1e6e072 22331 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22332 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22333 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22334 break;
c906108c
SS
22335 default:
22336 /* Not a tag we recognize. Hopefully we aren't processing
22337 trash data, but since we must specifically ignore things
22338 we don't recognize, there is nothing else we should do at
0963b4bd 22339 this point. */
b98664d3 22340 complaint (_("unsupported tag: '%s'"),
4d3c2250 22341 dwarf_tag_name (die->tag));
c906108c
SS
22342 break;
22343 }
df8a16a1 22344
e37fd15a
SW
22345 if (suppress_add)
22346 {
22347 sym->hash_next = objfile->template_symbols;
22348 objfile->template_symbols = sym;
22349 list_to_add = NULL;
22350 }
22351
22352 if (list_to_add != NULL)
d3cb6808 22353 add_symbol_to_list (sym, list_to_add);
e37fd15a 22354
df8a16a1
DJ
22355 /* For the benefit of old versions of GCC, check for anonymous
22356 namespaces based on the demangled name. */
4d4ec4e5 22357 if (!cu->processing_has_namespace_info
94af9270 22358 && cu->language == language_cplus)
c24bdb02 22359 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22360 }
22361 return (sym);
22362}
22363
98bfdba5
PA
22364/* Given an attr with a DW_FORM_dataN value in host byte order,
22365 zero-extend it as appropriate for the symbol's type. The DWARF
22366 standard (v4) is not entirely clear about the meaning of using
22367 DW_FORM_dataN for a constant with a signed type, where the type is
22368 wider than the data. The conclusion of a discussion on the DWARF
22369 list was that this is unspecified. We choose to always zero-extend
22370 because that is the interpretation long in use by GCC. */
c906108c 22371
98bfdba5 22372static gdb_byte *
ff39bb5e 22373dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22374 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22375{
5e22e966 22376 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22377 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22378 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22379 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22380
22381 if (bits < sizeof (*value) * 8)
22382 {
22383 l &= ((LONGEST) 1 << bits) - 1;
22384 *value = l;
22385 }
22386 else if (bits == sizeof (*value) * 8)
22387 *value = l;
22388 else
22389 {
224c3ddb 22390 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22391 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22392 return bytes;
22393 }
22394
22395 return NULL;
22396}
22397
22398/* Read a constant value from an attribute. Either set *VALUE, or if
22399 the value does not fit in *VALUE, set *BYTES - either already
22400 allocated on the objfile obstack, or newly allocated on OBSTACK,
22401 or, set *BATON, if we translated the constant to a location
22402 expression. */
22403
22404static void
ff39bb5e 22405dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22406 const char *name, struct obstack *obstack,
22407 struct dwarf2_cu *cu,
d521ce57 22408 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22409 struct dwarf2_locexpr_baton **baton)
22410{
5e22e966 22411 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22412 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22413 struct comp_unit_head *cu_header = &cu->header;
c906108c 22414 struct dwarf_block *blk;
98bfdba5
PA
22415 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22416 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22417
22418 *value = 0;
22419 *bytes = NULL;
22420 *baton = NULL;
c906108c
SS
22421
22422 switch (attr->form)
22423 {
22424 case DW_FORM_addr:
336d760d 22425 case DW_FORM_addrx:
3019eac3 22426 case DW_FORM_GNU_addr_index:
ac56253d 22427 {
ac56253d
TT
22428 gdb_byte *data;
22429
98bfdba5
PA
22430 if (TYPE_LENGTH (type) != cu_header->addr_size)
22431 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22432 cu_header->addr_size,
98bfdba5 22433 TYPE_LENGTH (type));
ac56253d
TT
22434 /* Symbols of this form are reasonably rare, so we just
22435 piggyback on the existing location code rather than writing
22436 a new implementation of symbol_computed_ops. */
8d749320 22437 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22438 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22439 (*baton)->per_cu = cu->per_cu;
22440 gdb_assert ((*baton)->per_cu);
ac56253d 22441
98bfdba5 22442 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22443 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22444 (*baton)->data = data;
ac56253d
TT
22445
22446 data[0] = DW_OP_addr;
22447 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22448 byte_order, attr->as_address ());
ac56253d 22449 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22450 }
c906108c 22451 break;
4ac36638 22452 case DW_FORM_string:
93b5768b 22453 case DW_FORM_strp:
cf532bd1 22454 case DW_FORM_strx:
3019eac3 22455 case DW_FORM_GNU_str_index:
36586728 22456 case DW_FORM_GNU_strp_alt:
c6481205 22457 /* The string is already allocated on the objfile obstack, point
98bfdba5 22458 directly to it. */
2c830f54 22459 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22460 break;
c906108c
SS
22461 case DW_FORM_block1:
22462 case DW_FORM_block2:
22463 case DW_FORM_block4:
22464 case DW_FORM_block:
2dc7f7b3 22465 case DW_FORM_exprloc:
0224619f 22466 case DW_FORM_data16:
9d2246fc 22467 blk = attr->as_block ();
98bfdba5
PA
22468 if (TYPE_LENGTH (type) != blk->size)
22469 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22470 TYPE_LENGTH (type));
22471 *bytes = blk->data;
c906108c 22472 break;
2df3850c
JM
22473
22474 /* The DW_AT_const_value attributes are supposed to carry the
22475 symbol's value "represented as it would be on the target
22476 architecture." By the time we get here, it's already been
22477 converted to host endianness, so we just need to sign- or
22478 zero-extend it as appropriate. */
22479 case DW_FORM_data1:
3aef2284 22480 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22481 break;
c906108c 22482 case DW_FORM_data2:
3aef2284 22483 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22484 break;
c906108c 22485 case DW_FORM_data4:
3aef2284 22486 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22487 break;
c906108c 22488 case DW_FORM_data8:
3aef2284 22489 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22490 break;
22491
c906108c 22492 case DW_FORM_sdata:
663c44ac 22493 case DW_FORM_implicit_const:
1bc397c5 22494 *value = attr->as_signed ();
2df3850c
JM
22495 break;
22496
c906108c 22497 case DW_FORM_udata:
529908cb 22498 *value = attr->as_unsigned ();
c906108c 22499 break;
2df3850c 22500
c906108c 22501 default:
b98664d3 22502 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22503 dwarf_form_name (attr->form));
98bfdba5 22504 *value = 0;
c906108c
SS
22505 break;
22506 }
22507}
22508
2df3850c 22509
98bfdba5
PA
22510/* Copy constant value from an attribute to a symbol. */
22511
2df3850c 22512static void
ff39bb5e 22513dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22514 struct dwarf2_cu *cu)
2df3850c 22515{
5e22e966 22516 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22517 LONGEST value;
d521ce57 22518 const gdb_byte *bytes;
98bfdba5 22519 struct dwarf2_locexpr_baton *baton;
2df3850c 22520
98bfdba5 22521 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22522 sym->print_name (),
98bfdba5
PA
22523 &objfile->objfile_obstack, cu,
22524 &value, &bytes, &baton);
2df3850c 22525
98bfdba5
PA
22526 if (baton != NULL)
22527 {
98bfdba5 22528 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22529 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22530 }
22531 else if (bytes != NULL)
22532 {
22533 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22534 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22535 }
22536 else
22537 {
22538 SYMBOL_VALUE (sym) = value;
f1e6e072 22539 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22540 }
2df3850c
JM
22541}
22542
c906108c
SS
22543/* Return the type of the die in question using its DW_AT_type attribute. */
22544
22545static struct type *
e7c27a73 22546die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22547{
c906108c 22548 struct attribute *type_attr;
c906108c 22549
e142c38c 22550 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22551 if (!type_attr)
22552 {
5e22e966 22553 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22554 /* A missing DW_AT_type represents a void type. */
518817b3 22555 return objfile_type (objfile)->builtin_void;
c906108c 22556 }
348e048f 22557
673bfd45 22558 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22559}
22560
b4ba55a1
JB
22561/* True iff CU's producer generates GNAT Ada auxiliary information
22562 that allows to find parallel types through that information instead
22563 of having to do expensive parallel lookups by type name. */
22564
22565static int
22566need_gnat_info (struct dwarf2_cu *cu)
22567{
de4cb04a
JB
22568 /* Assume that the Ada compiler was GNAT, which always produces
22569 the auxiliary information. */
22570 return (cu->language == language_ada);
b4ba55a1
JB
22571}
22572
b4ba55a1
JB
22573/* Return the auxiliary type of the die in question using its
22574 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22575 attribute is not present. */
22576
22577static struct type *
22578die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22579{
b4ba55a1 22580 struct attribute *type_attr;
b4ba55a1
JB
22581
22582 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22583 if (!type_attr)
22584 return NULL;
22585
673bfd45 22586 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22587}
22588
22589/* If DIE has a descriptive_type attribute, then set the TYPE's
22590 descriptive type accordingly. */
22591
22592static void
22593set_descriptive_type (struct type *type, struct die_info *die,
22594 struct dwarf2_cu *cu)
22595{
22596 struct type *descriptive_type = die_descriptive_type (die, cu);
22597
22598 if (descriptive_type)
22599 {
22600 ALLOCATE_GNAT_AUX_TYPE (type);
22601 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22602 }
22603}
22604
c906108c
SS
22605/* Return the containing type of the die in question using its
22606 DW_AT_containing_type attribute. */
22607
22608static struct type *
e7c27a73 22609die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22610{
c906108c 22611 struct attribute *type_attr;
5e22e966 22612 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22613
e142c38c 22614 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22615 if (!type_attr)
22616 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22617 "[in module %s]"), objfile_name (objfile));
33ac96f0 22618
673bfd45 22619 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22620}
22621
ac9ec31b
DE
22622/* Return an error marker type to use for the ill formed type in DIE/CU. */
22623
22624static struct type *
22625build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22626{
976ca316
SM
22627 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22628 struct objfile *objfile = per_objfile->objfile;
528e1572 22629 char *saved;
ac9ec31b 22630
528e1572
SM
22631 std::string message
22632 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22633 objfile_name (objfile),
22634 sect_offset_str (cu->header.sect_off),
22635 sect_offset_str (die->sect_off));
efba19b0 22636 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22637
19f392bc 22638 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22639}
22640
673bfd45 22641/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22642 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22643 DW_AT_containing_type.
673bfd45
DE
22644 If there is no type substitute an error marker. */
22645
c906108c 22646static struct type *
ff39bb5e 22647lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22648 struct dwarf2_cu *cu)
c906108c 22649{
976ca316
SM
22650 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22651 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22652 struct type *this_type;
22653
ac9ec31b
DE
22654 gdb_assert (attr->name == DW_AT_type
22655 || attr->name == DW_AT_GNAT_descriptive_type
22656 || attr->name == DW_AT_containing_type);
22657
673bfd45
DE
22658 /* First see if we have it cached. */
22659
36586728
TT
22660 if (attr->form == DW_FORM_GNU_ref_alt)
22661 {
22662 struct dwarf2_per_cu_data *per_cu;
0826b30a 22663 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22664
976ca316
SM
22665 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22666 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22667 }
cd6c91b4 22668 else if (attr->form_is_ref ())
673bfd45 22669 {
0826b30a 22670 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22671
976ca316 22672 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22673 }
55f1336d 22674 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22675 {
630ed6b9 22676 ULONGEST signature = attr->as_signature ();
673bfd45 22677
ac9ec31b 22678 return get_signatured_type (die, signature, cu);
673bfd45
DE
22679 }
22680 else
22681 {
b98664d3 22682 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22683 " at %s [in module %s]"),
22684 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22685 objfile_name (objfile));
ac9ec31b 22686 return build_error_marker_type (cu, die);
673bfd45
DE
22687 }
22688
22689 /* If not cached we need to read it in. */
22690
22691 if (this_type == NULL)
22692 {
ac9ec31b 22693 struct die_info *type_die = NULL;
673bfd45
DE
22694 struct dwarf2_cu *type_cu = cu;
22695
cd6c91b4 22696 if (attr->form_is_ref ())
ac9ec31b
DE
22697 type_die = follow_die_ref (die, attr, &type_cu);
22698 if (type_die == NULL)
22699 return build_error_marker_type (cu, die);
22700 /* If we find the type now, it's probably because the type came
3019eac3
DE
22701 from an inter-CU reference and the type's CU got expanded before
22702 ours. */
ac9ec31b 22703 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22704 }
22705
22706 /* If we still don't have a type use an error marker. */
22707
22708 if (this_type == NULL)
ac9ec31b 22709 return build_error_marker_type (cu, die);
673bfd45 22710
f792889a 22711 return this_type;
c906108c
SS
22712}
22713
673bfd45
DE
22714/* Return the type in DIE, CU.
22715 Returns NULL for invalid types.
22716
02142a6c 22717 This first does a lookup in die_type_hash,
673bfd45
DE
22718 and only reads the die in if necessary.
22719
22720 NOTE: This can be called when reading in partial or full symbols. */
22721
f792889a 22722static struct type *
e7c27a73 22723read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22724{
f792889a
DJ
22725 struct type *this_type;
22726
22727 this_type = get_die_type (die, cu);
22728 if (this_type)
22729 return this_type;
22730
673bfd45
DE
22731 return read_type_die_1 (die, cu);
22732}
22733
22734/* Read the type in DIE, CU.
22735 Returns NULL for invalid types. */
22736
22737static struct type *
22738read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22739{
22740 struct type *this_type = NULL;
22741
c906108c
SS
22742 switch (die->tag)
22743 {
22744 case DW_TAG_class_type:
680b30c7 22745 case DW_TAG_interface_type:
c906108c
SS
22746 case DW_TAG_structure_type:
22747 case DW_TAG_union_type:
f792889a 22748 this_type = read_structure_type (die, cu);
c906108c
SS
22749 break;
22750 case DW_TAG_enumeration_type:
f792889a 22751 this_type = read_enumeration_type (die, cu);
c906108c
SS
22752 break;
22753 case DW_TAG_subprogram:
22754 case DW_TAG_subroutine_type:
edb3359d 22755 case DW_TAG_inlined_subroutine:
f792889a 22756 this_type = read_subroutine_type (die, cu);
c906108c
SS
22757 break;
22758 case DW_TAG_array_type:
f792889a 22759 this_type = read_array_type (die, cu);
c906108c 22760 break;
72019c9c 22761 case DW_TAG_set_type:
f792889a 22762 this_type = read_set_type (die, cu);
72019c9c 22763 break;
c906108c 22764 case DW_TAG_pointer_type:
f792889a 22765 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22766 break;
22767 case DW_TAG_ptr_to_member_type:
f792889a 22768 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22769 break;
22770 case DW_TAG_reference_type:
4297a3f0
AV
22771 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22772 break;
22773 case DW_TAG_rvalue_reference_type:
22774 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22775 break;
22776 case DW_TAG_const_type:
f792889a 22777 this_type = read_tag_const_type (die, cu);
c906108c
SS
22778 break;
22779 case DW_TAG_volatile_type:
f792889a 22780 this_type = read_tag_volatile_type (die, cu);
c906108c 22781 break;
06d66ee9
TT
22782 case DW_TAG_restrict_type:
22783 this_type = read_tag_restrict_type (die, cu);
22784 break;
c906108c 22785 case DW_TAG_string_type:
f792889a 22786 this_type = read_tag_string_type (die, cu);
c906108c
SS
22787 break;
22788 case DW_TAG_typedef:
f792889a 22789 this_type = read_typedef (die, cu);
c906108c 22790 break;
a02abb62 22791 case DW_TAG_subrange_type:
f792889a 22792 this_type = read_subrange_type (die, cu);
a02abb62 22793 break;
c906108c 22794 case DW_TAG_base_type:
f792889a 22795 this_type = read_base_type (die, cu);
c906108c 22796 break;
81a17f79 22797 case DW_TAG_unspecified_type:
f792889a 22798 this_type = read_unspecified_type (die, cu);
81a17f79 22799 break;
0114d602
DJ
22800 case DW_TAG_namespace:
22801 this_type = read_namespace_type (die, cu);
22802 break;
f55ee35c
JK
22803 case DW_TAG_module:
22804 this_type = read_module_type (die, cu);
22805 break;
a2c2acaf
MW
22806 case DW_TAG_atomic_type:
22807 this_type = read_tag_atomic_type (die, cu);
22808 break;
c906108c 22809 default:
b98664d3 22810 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22811 dwarf_tag_name (die->tag));
c906108c
SS
22812 break;
22813 }
63d06c5c 22814
f792889a 22815 return this_type;
63d06c5c
DC
22816}
22817
abc72ce4
DE
22818/* See if we can figure out if the class lives in a namespace. We do
22819 this by looking for a member function; its demangled name will
22820 contain namespace info, if there is any.
22821 Return the computed name or NULL.
22822 Space for the result is allocated on the objfile's obstack.
22823 This is the full-die version of guess_partial_die_structure_name.
22824 In this case we know DIE has no useful parent. */
22825
43816ebc 22826static const char *
abc72ce4
DE
22827guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22828{
22829 struct die_info *spec_die;
22830 struct dwarf2_cu *spec_cu;
22831 struct die_info *child;
5e22e966 22832 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22833
22834 spec_cu = cu;
22835 spec_die = die_specification (die, &spec_cu);
22836 if (spec_die != NULL)
22837 {
22838 die = spec_die;
22839 cu = spec_cu;
22840 }
22841
22842 for (child = die->child;
22843 child != NULL;
22844 child = child->sibling)
22845 {
22846 if (child->tag == DW_TAG_subprogram)
22847 {
73b9be8b 22848 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22849
7d45c7c3 22850 if (linkage_name != NULL)
abc72ce4 22851 {
43816ebc 22852 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22853 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22854 const char *name = NULL;
abc72ce4
DE
22855
22856 if (actual_name != NULL)
22857 {
15d034d0 22858 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22859
22860 if (die_name != NULL
43816ebc 22861 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22862 {
22863 /* Strip off the class name from the full name.
22864 We want the prefix. */
22865 int die_name_len = strlen (die_name);
43816ebc
TT
22866 int actual_name_len = strlen (actual_name.get ());
22867 const char *ptr = actual_name.get ();
abc72ce4
DE
22868
22869 /* Test for '::' as a sanity check. */
22870 if (actual_name_len > die_name_len + 2
43816ebc 22871 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22872 name = obstack_strndup (
e3b94546 22873 &objfile->per_bfd->storage_obstack,
43816ebc 22874 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22875 }
22876 }
abc72ce4
DE
22877 return name;
22878 }
22879 }
22880 }
22881
22882 return NULL;
22883}
22884
96408a79
SA
22885/* GCC might emit a nameless typedef that has a linkage name. Determine the
22886 prefix part in such case. See
22887 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22888
a121b7c1 22889static const char *
96408a79
SA
22890anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22891{
22892 struct attribute *attr;
e6a959d6 22893 const char *base;
96408a79
SA
22894
22895 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22896 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22897 return NULL;
22898
7d45c7c3 22899 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22900 return NULL;
22901
73b9be8b 22902 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22903 const char *attr_name = attr->as_string ();
22904 if (attr == NULL || attr_name == NULL)
96408a79
SA
22905 return NULL;
22906
22907 /* dwarf2_name had to be already called. */
3b64bf15 22908 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22909
22910 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22911 base = strrchr (attr_name, ':');
22912 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22913 return "";
22914
5e22e966 22915 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22916 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22917 attr_name,
22918 &base[-1] - attr_name);
96408a79
SA
22919}
22920
fdde2d81 22921/* Return the name of the namespace/class that DIE is defined within,
0114d602 22922 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22923
0114d602
DJ
22924 For example, if we're within the method foo() in the following
22925 code:
22926
22927 namespace N {
22928 class C {
22929 void foo () {
22930 }
22931 };
22932 }
22933
22934 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22935
0d5cff50 22936static const char *
e142c38c 22937determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22938{
976ca316 22939 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22940 struct die_info *parent, *spec_die;
22941 struct dwarf2_cu *spec_cu;
22942 struct type *parent_type;
a121b7c1 22943 const char *retval;
63d06c5c 22944
9c37b5ae 22945 if (cu->language != language_cplus
c44af4eb
TT
22946 && cu->language != language_fortran && cu->language != language_d
22947 && cu->language != language_rust)
0114d602
DJ
22948 return "";
22949
96408a79
SA
22950 retval = anonymous_struct_prefix (die, cu);
22951 if (retval)
22952 return retval;
22953
0114d602
DJ
22954 /* We have to be careful in the presence of DW_AT_specification.
22955 For example, with GCC 3.4, given the code
22956
22957 namespace N {
22958 void foo() {
22959 // Definition of N::foo.
22960 }
22961 }
22962
22963 then we'll have a tree of DIEs like this:
22964
22965 1: DW_TAG_compile_unit
22966 2: DW_TAG_namespace // N
22967 3: DW_TAG_subprogram // declaration of N::foo
22968 4: DW_TAG_subprogram // definition of N::foo
22969 DW_AT_specification // refers to die #3
22970
22971 Thus, when processing die #4, we have to pretend that we're in
22972 the context of its DW_AT_specification, namely the contex of die
22973 #3. */
22974 spec_cu = cu;
22975 spec_die = die_specification (die, &spec_cu);
22976 if (spec_die == NULL)
22977 parent = die->parent;
22978 else
63d06c5c 22979 {
0114d602
DJ
22980 parent = spec_die->parent;
22981 cu = spec_cu;
63d06c5c 22982 }
0114d602
DJ
22983
22984 if (parent == NULL)
22985 return "";
98bfdba5
PA
22986 else if (parent->building_fullname)
22987 {
22988 const char *name;
22989 const char *parent_name;
22990
22991 /* It has been seen on RealView 2.2 built binaries,
22992 DW_TAG_template_type_param types actually _defined_ as
22993 children of the parent class:
22994
22995 enum E {};
22996 template class <class Enum> Class{};
22997 Class<enum E> class_e;
22998
dda83cd7
SM
22999 1: DW_TAG_class_type (Class)
23000 2: DW_TAG_enumeration_type (E)
23001 3: DW_TAG_enumerator (enum1:0)
23002 3: DW_TAG_enumerator (enum2:1)
23003 ...
23004 2: DW_TAG_template_type_param
23005 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
23006
23007 Besides being broken debug info, it can put GDB into an
23008 infinite loop. Consider:
23009
23010 When we're building the full name for Class<E>, we'll start
23011 at Class, and go look over its template type parameters,
23012 finding E. We'll then try to build the full name of E, and
23013 reach here. We're now trying to build the full name of E,
23014 and look over the parent DIE for containing scope. In the
23015 broken case, if we followed the parent DIE of E, we'd again
23016 find Class, and once again go look at its template type
23017 arguments, etc., etc. Simply don't consider such parent die
23018 as source-level parent of this die (it can't be, the language
23019 doesn't allow it), and break the loop here. */
23020 name = dwarf2_name (die, cu);
23021 parent_name = dwarf2_name (parent, cu);
b98664d3 23022 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
23023 name ? name : "<unknown>",
23024 parent_name ? parent_name : "<unknown>");
23025 return "";
23026 }
63d06c5c 23027 else
0114d602
DJ
23028 switch (parent->tag)
23029 {
63d06c5c 23030 case DW_TAG_namespace:
0114d602 23031 parent_type = read_type_die (parent, cu);
acebe513
UW
23032 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23033 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23034 Work around this problem here. */
23035 if (cu->language == language_cplus
7d93a1e0 23036 && strcmp (parent_type->name (), "::") == 0)
acebe513 23037 return "";
0114d602 23038 /* We give a name to even anonymous namespaces. */
7d93a1e0 23039 return parent_type->name ();
63d06c5c 23040 case DW_TAG_class_type:
680b30c7 23041 case DW_TAG_interface_type:
63d06c5c 23042 case DW_TAG_structure_type:
0114d602 23043 case DW_TAG_union_type:
f55ee35c 23044 case DW_TAG_module:
0114d602 23045 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
23046 if (parent_type->name () != NULL)
23047 return parent_type->name ();
0114d602
DJ
23048 else
23049 /* An anonymous structure is only allowed non-static data
23050 members; no typedefs, no member functions, et cetera.
23051 So it does not need a prefix. */
23052 return "";
abc72ce4 23053 case DW_TAG_compile_unit:
95554aad 23054 case DW_TAG_partial_unit:
abc72ce4
DE
23055 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23056 if (cu->language == language_cplus
976ca316 23057 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
23058 && die->child != NULL
23059 && (die->tag == DW_TAG_class_type
23060 || die->tag == DW_TAG_structure_type
23061 || die->tag == DW_TAG_union_type))
23062 {
43816ebc 23063 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
23064 if (name != NULL)
23065 return name;
23066 }
23067 return "";
0a4b0913
AB
23068 case DW_TAG_subprogram:
23069 /* Nested subroutines in Fortran get a prefix with the name
23070 of the parent's subroutine. */
23071 if (cu->language == language_fortran)
23072 {
23073 if ((die->tag == DW_TAG_subprogram)
23074 && (dwarf2_name (parent, cu) != NULL))
23075 return dwarf2_name (parent, cu);
23076 }
23077 return determine_prefix (parent, cu);
3d567982
TT
23078 case DW_TAG_enumeration_type:
23079 parent_type = read_type_die (parent, cu);
3bc440a2 23080 if (parent_type->is_declared_class ())
3d567982 23081 {
7d93a1e0
SM
23082 if (parent_type->name () != NULL)
23083 return parent_type->name ();
3d567982
TT
23084 return "";
23085 }
23086 /* Fall through. */
63d06c5c 23087 default:
8176b9b8 23088 return determine_prefix (parent, cu);
63d06c5c 23089 }
63d06c5c
DC
23090}
23091
3e43a32a
MS
23092/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23093 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23094 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23095 an obconcat, otherwise allocate storage for the result. The CU argument is
23096 used to determine the language and hence, the appropriate separator. */
987504bb 23097
f55ee35c 23098#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23099
23100static char *
f55ee35c 23101typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23102 int physname, struct dwarf2_cu *cu)
63d06c5c 23103{
f55ee35c 23104 const char *lead = "";
5c315b68 23105 const char *sep;
63d06c5c 23106
3e43a32a
MS
23107 if (suffix == NULL || suffix[0] == '\0'
23108 || prefix == NULL || prefix[0] == '\0')
987504bb 23109 sep = "";
45280282
IB
23110 else if (cu->language == language_d)
23111 {
23112 /* For D, the 'main' function could be defined in any module, but it
23113 should never be prefixed. */
23114 if (strcmp (suffix, "D main") == 0)
23115 {
23116 prefix = "";
23117 sep = "";
23118 }
23119 else
23120 sep = ".";
23121 }
f55ee35c
JK
23122 else if (cu->language == language_fortran && physname)
23123 {
23124 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23125 DW_AT_MIPS_linkage_name is preferred and used instead. */
23126
23127 lead = "__";
23128 sep = "_MOD_";
23129 }
987504bb
JJ
23130 else
23131 sep = "::";
63d06c5c 23132
6dd47d34
DE
23133 if (prefix == NULL)
23134 prefix = "";
23135 if (suffix == NULL)
23136 suffix = "";
23137
987504bb
JJ
23138 if (obs == NULL)
23139 {
3e43a32a 23140 char *retval
224c3ddb
SM
23141 = ((char *)
23142 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23143
f55ee35c
JK
23144 strcpy (retval, lead);
23145 strcat (retval, prefix);
6dd47d34
DE
23146 strcat (retval, sep);
23147 strcat (retval, suffix);
63d06c5c
DC
23148 return retval;
23149 }
987504bb
JJ
23150 else
23151 {
23152 /* We have an obstack. */
f55ee35c 23153 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23154 }
63d06c5c
DC
23155}
23156
71c25dea
TT
23157/* Get name of a die, return NULL if not found. */
23158
15d034d0
TT
23159static const char *
23160dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23161 struct objfile *objfile)
71c25dea
TT
23162{
23163 if (name && cu->language == language_cplus)
23164 {
596dc4ad
TT
23165 gdb::unique_xmalloc_ptr<char> canon_name
23166 = cp_canonicalize_string (name);
71c25dea 23167
596dc4ad
TT
23168 if (canon_name != nullptr)
23169 name = objfile->intern (canon_name.get ());
71c25dea
TT
23170 }
23171
23172 return name;
c906108c
SS
23173}
23174
96553a0c
DE
23175/* Get name of a die, return NULL if not found.
23176 Anonymous namespaces are converted to their magic string. */
9219021c 23177
15d034d0 23178static const char *
e142c38c 23179dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23180{
23181 struct attribute *attr;
5e22e966 23182 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23183
e142c38c 23184 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23185 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23186 if (attr_name == nullptr
96553a0c 23187 && die->tag != DW_TAG_namespace
53832f31
TT
23188 && die->tag != DW_TAG_class_type
23189 && die->tag != DW_TAG_interface_type
23190 && die->tag != DW_TAG_structure_type
23191 && die->tag != DW_TAG_union_type)
71c25dea
TT
23192 return NULL;
23193
23194 switch (die->tag)
23195 {
23196 case DW_TAG_compile_unit:
95554aad 23197 case DW_TAG_partial_unit:
71c25dea
TT
23198 /* Compilation units have a DW_AT_name that is a filename, not
23199 a source language identifier. */
23200 case DW_TAG_enumeration_type:
23201 case DW_TAG_enumerator:
23202 /* These tags always have simple identifiers already; no need
23203 to canonicalize them. */
2c830f54 23204 return attr_name;
907af001 23205
96553a0c 23206 case DW_TAG_namespace:
2c830f54
TT
23207 if (attr_name != nullptr)
23208 return attr_name;
96553a0c
DE
23209 return CP_ANONYMOUS_NAMESPACE_STR;
23210
907af001
UW
23211 case DW_TAG_class_type:
23212 case DW_TAG_interface_type:
23213 case DW_TAG_structure_type:
23214 case DW_TAG_union_type:
23215 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23216 structures or unions. These were of the form "._%d" in GCC 4.1,
23217 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23218 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23219 if (attr_name != nullptr
23220 && (startswith (attr_name, "._")
23221 || startswith (attr_name, "<anonymous")))
907af001 23222 return NULL;
53832f31
TT
23223
23224 /* GCC might emit a nameless typedef that has a linkage name. See
23225 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23226 if (!attr || attr_name == NULL)
53832f31 23227 {
73b9be8b 23228 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23229 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23230 if (attr == NULL || attr_name == NULL)
53832f31
TT
23231 return NULL;
23232
2c830f54 23233 /* Avoid demangling attr_name the second time on a second
df5c6c50 23234 call for the same DIE. */
3b64bf15 23235 if (!attr->canonical_string_p ())
53832f31 23236 {
43816ebc 23237 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23238 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23239 if (demangled == nullptr)
23240 return nullptr;
43816ebc 23241
c6481205 23242 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23243 attr_name = attr->as_string ();
53832f31 23244 }
67430cd0 23245
2c830f54
TT
23246 /* Strip any leading namespaces/classes, keep only the
23247 base name. DW_AT_name for named DIEs does not
23248 contain the prefixes. */
23249 const char *base = strrchr (attr_name, ':');
23250 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23251 return &base[1];
23252 else
2c830f54 23253 return attr_name;
53832f31 23254 }
907af001
UW
23255 break;
23256
71c25dea 23257 default:
907af001
UW
23258 break;
23259 }
23260
3b64bf15 23261 if (!attr->canonical_string_p ())
c6481205
TT
23262 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23263 objfile));
2c830f54 23264 return attr->as_string ();
9219021c
DC
23265}
23266
23267/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23268 is none. *EXT_CU is the CU containing DIE on input, and the CU
23269 containing the return value on output. */
9219021c
DC
23270
23271static struct die_info *
f2f0e013 23272dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23273{
23274 struct attribute *attr;
9219021c 23275
f2f0e013 23276 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23277 if (attr == NULL)
23278 return NULL;
23279
f2f0e013 23280 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23281}
23282
f9aca02d 23283static void
d97bc12b 23284dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23285{
23286 unsigned int i;
23287
d97bc12b 23288 print_spaces (indent, f);
9d8780f0 23289 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23290 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23291 sect_offset_str (die->sect_off));
d97bc12b
DE
23292
23293 if (die->parent != NULL)
23294 {
23295 print_spaces (indent, f);
9d8780f0
SM
23296 fprintf_unfiltered (f, " parent at offset: %s\n",
23297 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23298 }
23299
23300 print_spaces (indent, f);
23301 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23302 dwarf_bool_name (die->child != NULL));
c906108c 23303
d97bc12b
DE
23304 print_spaces (indent, f);
23305 fprintf_unfiltered (f, " attributes:\n");
23306
c906108c
SS
23307 for (i = 0; i < die->num_attrs; ++i)
23308 {
d97bc12b
DE
23309 print_spaces (indent, f);
23310 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23311 dwarf_attr_name (die->attrs[i].name),
23312 dwarf_form_name (die->attrs[i].form));
d97bc12b 23313
c906108c
SS
23314 switch (die->attrs[i].form)
23315 {
c906108c 23316 case DW_FORM_addr:
336d760d 23317 case DW_FORM_addrx:
3019eac3 23318 case DW_FORM_GNU_addr_index:
d97bc12b 23319 fprintf_unfiltered (f, "address: ");
36d378cf 23320 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23321 break;
23322 case DW_FORM_block2:
23323 case DW_FORM_block4:
23324 case DW_FORM_block:
23325 case DW_FORM_block1:
56eb65bd 23326 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23327 pulongest (die->attrs[i].as_block ()->size));
c906108c 23328 break;
2dc7f7b3 23329 case DW_FORM_exprloc:
56eb65bd 23330 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23331 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23332 break;
0224619f
JK
23333 case DW_FORM_data16:
23334 fprintf_unfiltered (f, "constant of 16 bytes");
23335 break;
4568ecf9
DE
23336 case DW_FORM_ref_addr:
23337 fprintf_unfiltered (f, "ref address: ");
529908cb 23338 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23339 break;
36586728
TT
23340 case DW_FORM_GNU_ref_alt:
23341 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23342 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23343 break;
10b3939b
DJ
23344 case DW_FORM_ref1:
23345 case DW_FORM_ref2:
23346 case DW_FORM_ref4:
4568ecf9
DE
23347 case DW_FORM_ref8:
23348 case DW_FORM_ref_udata:
d97bc12b 23349 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23350 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23351 break;
c906108c
SS
23352 case DW_FORM_data1:
23353 case DW_FORM_data2:
23354 case DW_FORM_data4:
ce5d95e1 23355 case DW_FORM_data8:
c906108c 23356 case DW_FORM_udata:
43bbcdc2 23357 fprintf_unfiltered (f, "constant: %s",
529908cb 23358 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23359 break;
2dc7f7b3
TT
23360 case DW_FORM_sec_offset:
23361 fprintf_unfiltered (f, "section offset: %s",
529908cb 23362 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23363 break;
55f1336d 23364 case DW_FORM_ref_sig8:
ac9ec31b 23365 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23366 hex_string (die->attrs[i].as_signature ()));
348e048f 23367 break;
c906108c 23368 case DW_FORM_string:
4bdf3d34 23369 case DW_FORM_strp:
43988095 23370 case DW_FORM_line_strp:
cf532bd1 23371 case DW_FORM_strx:
3019eac3 23372 case DW_FORM_GNU_str_index:
36586728 23373 case DW_FORM_GNU_strp_alt:
8285870a 23374 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23375 die->attrs[i].as_string ()
23376 ? die->attrs[i].as_string () : "",
23377 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23378 break;
23379 case DW_FORM_flag:
c45bc3f8 23380 if (die->attrs[i].as_boolean ())
d97bc12b 23381 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23382 else
d97bc12b 23383 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23384 break;
2dc7f7b3
TT
23385 case DW_FORM_flag_present:
23386 fprintf_unfiltered (f, "flag: TRUE");
23387 break;
a8329558 23388 case DW_FORM_indirect:
0963b4bd
MS
23389 /* The reader will have reduced the indirect form to
23390 the "base form" so this form should not occur. */
5f48f8f3 23391 fprintf_unfiltered (f,
3e43a32a 23392 "unexpected attribute form: DW_FORM_indirect");
a8329558 23393 break;
1bc397c5 23394 case DW_FORM_sdata:
663c44ac
JK
23395 case DW_FORM_implicit_const:
23396 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23397 plongest (die->attrs[i].as_signed ()));
663c44ac 23398 break;
c906108c 23399 default:
d97bc12b 23400 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23401 die->attrs[i].form);
d97bc12b 23402 break;
c906108c 23403 }
d97bc12b 23404 fprintf_unfiltered (f, "\n");
c906108c
SS
23405 }
23406}
23407
f9aca02d 23408static void
d97bc12b 23409dump_die_for_error (struct die_info *die)
c906108c 23410{
d97bc12b
DE
23411 dump_die_shallow (gdb_stderr, 0, die);
23412}
23413
23414static void
23415dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23416{
23417 int indent = level * 4;
23418
23419 gdb_assert (die != NULL);
23420
23421 if (level >= max_level)
23422 return;
23423
23424 dump_die_shallow (f, indent, die);
23425
23426 if (die->child != NULL)
c906108c 23427 {
d97bc12b
DE
23428 print_spaces (indent, f);
23429 fprintf_unfiltered (f, " Children:");
23430 if (level + 1 < max_level)
23431 {
23432 fprintf_unfiltered (f, "\n");
23433 dump_die_1 (f, level + 1, max_level, die->child);
23434 }
23435 else
23436 {
3e43a32a
MS
23437 fprintf_unfiltered (f,
23438 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23439 }
23440 }
23441
23442 if (die->sibling != NULL && level > 0)
23443 {
23444 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23445 }
23446}
23447
d97bc12b
DE
23448/* This is called from the pdie macro in gdbinit.in.
23449 It's not static so gcc will keep a copy callable from gdb. */
23450
23451void
23452dump_die (struct die_info *die, int max_level)
23453{
23454 dump_die_1 (gdb_stdlog, 0, max_level, die);
23455}
23456
f9aca02d 23457static void
51545339 23458store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23459{
51545339 23460 void **slot;
c906108c 23461
9c541725
PA
23462 slot = htab_find_slot_with_hash (cu->die_hash, die,
23463 to_underlying (die->sect_off),
b64f50a1 23464 INSERT);
51545339
DJ
23465
23466 *slot = die;
c906108c
SS
23467}
23468
348e048f
DE
23469/* Follow reference or signature attribute ATTR of SRC_DIE.
23470 On entry *REF_CU is the CU of SRC_DIE.
23471 On exit *REF_CU is the CU of the result. */
23472
23473static struct die_info *
ff39bb5e 23474follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23475 struct dwarf2_cu **ref_cu)
23476{
23477 struct die_info *die;
23478
cd6c91b4 23479 if (attr->form_is_ref ())
348e048f 23480 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23481 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23482 die = follow_die_sig (src_die, attr, ref_cu);
23483 else
23484 {
23485 dump_die_for_error (src_die);
23486 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23487 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23488 }
23489
23490 return die;
03dd20cc
DJ
23491}
23492
5c631832 23493/* Follow reference OFFSET.
673bfd45
DE
23494 On entry *REF_CU is the CU of the source die referencing OFFSET.
23495 On exit *REF_CU is the CU of the result.
23496 Returns NULL if OFFSET is invalid. */
f504f079 23497
f9aca02d 23498static struct die_info *
9c541725 23499follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23500 struct dwarf2_cu **ref_cu)
c906108c 23501{
10b3939b 23502 struct die_info temp_die;
f2f0e013 23503 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23504 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23505
348e048f
DE
23506 gdb_assert (cu->per_cu != NULL);
23507
98bfdba5
PA
23508 target_cu = cu;
23509
17e593e9
SM
23510 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23511 "source CU contains target offset: %d",
23512 sect_offset_str (cu->per_cu->sect_off),
23513 sect_offset_str (sect_off),
23514 cu->header.offset_in_cu_p (sect_off));
23515
3019eac3 23516 if (cu->per_cu->is_debug_types)
348e048f
DE
23517 {
23518 /* .debug_types CUs cannot reference anything outside their CU.
23519 If they need to, they have to reference a signatured type via
55f1336d 23520 DW_FORM_ref_sig8. */
4057dfde 23521 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23522 return NULL;
348e048f 23523 }
36586728 23524 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23525 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23526 {
23527 struct dwarf2_per_cu_data *per_cu;
9a619af0 23528
9c541725 23529 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23530 per_objfile);
03dd20cc 23531
17e593e9
SM
23532 dwarf_read_debug_printf_v ("target CU offset: %s, "
23533 "target CU DIEs loaded: %d",
23534 sect_offset_str (per_cu->sect_off),
23535 per_objfile->get_cu (per_cu) != nullptr);
23536
616c069a
SM
23537 /* If necessary, add it to the queue and load its DIEs.
23538
23539 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23540 it doesn't mean they are currently loaded. Since we require them
23541 to be loaded, we must check for ourselves. */
23542 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23543 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23544 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23545 false, cu->language);
03dd20cc 23546
976ca316 23547 target_cu = per_objfile->get_cu (per_cu);
616c069a 23548 gdb_assert (target_cu != nullptr);
10b3939b 23549 }
98bfdba5
PA
23550 else if (cu->dies == NULL)
23551 {
23552 /* We're loading full DIEs during partial symbol reading. */
976ca316 23553 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23554 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23555 language_minimal);
98bfdba5 23556 }
c906108c 23557
f2f0e013 23558 *ref_cu = target_cu;
9c541725 23559 temp_die.sect_off = sect_off;
c24bdb02
KS
23560
23561 if (target_cu != cu)
23562 target_cu->ancestor = cu;
23563
9a3c8263 23564 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23565 &temp_die,
23566 to_underlying (sect_off));
5c631832 23567}
10b3939b 23568
5c631832
JK
23569/* Follow reference attribute ATTR of SRC_DIE.
23570 On entry *REF_CU is the CU of SRC_DIE.
23571 On exit *REF_CU is the CU of the result. */
23572
23573static struct die_info *
ff39bb5e 23574follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23575 struct dwarf2_cu **ref_cu)
23576{
0826b30a 23577 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23578 struct dwarf2_cu *cu = *ref_cu;
23579 struct die_info *die;
23580
9c541725 23581 die = follow_die_offset (sect_off,
36586728
TT
23582 (attr->form == DW_FORM_GNU_ref_alt
23583 || cu->per_cu->is_dwz),
23584 ref_cu);
5c631832 23585 if (!die)
9d8780f0
SM
23586 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23587 "at %s [in module %s]"),
23588 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23589 objfile_name (cu->per_objfile->objfile));
348e048f 23590
5c631832
JK
23591 return die;
23592}
23593
d4c9a4f8 23594/* See read.h. */
5c631832
JK
23595
23596struct dwarf2_locexpr_baton
9c541725 23597dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23598 dwarf2_per_cu_data *per_cu,
976ca316 23599 dwarf2_per_objfile *per_objfile,
041d9819
SM
23600 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23601 bool resolve_abstract_p)
5c631832 23602{
5c631832
JK
23603 struct die_info *die;
23604 struct attribute *attr;
23605 struct dwarf2_locexpr_baton retval;
976ca316 23606 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23607
976ca316 23608 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23609 if (cu == nullptr)
976ca316 23610 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23611
23612 if (cu == nullptr)
cc12ce38
DE
23613 {
23614 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23615 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23616 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23617 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23618 }
918dd910 23619
9c541725 23620 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23621 if (!die)
9d8780f0
SM
23622 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23623 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23624
23625 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23626 if (!attr && resolve_abstract_p
976ca316
SM
23627 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23628 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23629 {
041d9819 23630 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23631 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23632 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23633
3360b6e7 23634 for (const auto &cand_off
976ca316 23635 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23636 {
3360b6e7
TV
23637 struct dwarf2_cu *cand_cu = cu;
23638 struct die_info *cand
23639 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23640 if (!cand
23641 || !cand->parent
e4a62c65
TV
23642 || cand->parent->tag != DW_TAG_subprogram)
23643 continue;
23644
23645 CORE_ADDR pc_low, pc_high;
23646 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23647 if (pc_low == ((CORE_ADDR) -1))
23648 continue;
23649 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23650 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23651 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23652 continue;
23653
23654 die = cand;
23655 attr = dwarf2_attr (die, DW_AT_location, cu);
23656 break;
23657 }
23658 }
23659
5c631832
JK
23660 if (!attr)
23661 {
e103e986
JK
23662 /* DWARF: "If there is no such attribute, then there is no effect.".
23663 DATA is ignored if SIZE is 0. */
5c631832 23664
e103e986 23665 retval.data = NULL;
5c631832
JK
23666 retval.size = 0;
23667 }
cd6c91b4 23668 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23669 {
23670 struct dwarf2_loclist_baton loclist_baton;
041d9819 23671 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23672 size_t size;
23673
23674 fill_in_loclist_baton (cu, &loclist_baton, attr);
23675
23676 retval.data = dwarf2_find_location_expression (&loclist_baton,
23677 &size, pc);
23678 retval.size = size;
23679 }
5c631832
JK
23680 else
23681 {
4fc6c0d5 23682 if (!attr->form_is_block ())
9d8780f0 23683 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23684 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23685 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23686
9d2246fc
TT
23687 struct dwarf_block *block = attr->as_block ();
23688 retval.data = block->data;
23689 retval.size = block->size;
5c631832 23690 }
976ca316 23691 retval.per_objfile = per_objfile;
5c631832 23692 retval.per_cu = cu->per_cu;
918dd910 23693
976ca316 23694 per_objfile->age_comp_units ();
918dd910 23695
5c631832 23696 return retval;
348e048f
DE
23697}
23698
d4c9a4f8 23699/* See read.h. */
8b9737bf
TT
23700
23701struct dwarf2_locexpr_baton
23702dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23703 dwarf2_per_cu_data *per_cu,
14095eb3 23704 dwarf2_per_objfile *per_objfile,
041d9819 23705 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23706{
9c541725 23707 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23708
14095eb3 23709 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23710 get_frame_pc);
8b9737bf
TT
23711}
23712
b6807d98
TT
23713/* Write a constant of a given type as target-ordered bytes into
23714 OBSTACK. */
23715
23716static const gdb_byte *
23717write_constant_as_bytes (struct obstack *obstack,
23718 enum bfd_endian byte_order,
23719 struct type *type,
23720 ULONGEST value,
23721 LONGEST *len)
23722{
23723 gdb_byte *result;
23724
23725 *len = TYPE_LENGTH (type);
224c3ddb 23726 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23727 store_unsigned_integer (result, *len, byte_order, value);
23728
23729 return result;
23730}
23731
d4c9a4f8 23732/* See read.h. */
b6807d98
TT
23733
23734const gdb_byte *
9c541725 23735dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23736 dwarf2_per_cu_data *per_cu,
14095eb3 23737 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23738 obstack *obstack,
b6807d98
TT
23739 LONGEST *len)
23740{
b6807d98
TT
23741 struct die_info *die;
23742 struct attribute *attr;
23743 const gdb_byte *result = NULL;
23744 struct type *type;
23745 LONGEST value;
23746 enum bfd_endian byte_order;
14095eb3 23747 struct objfile *objfile = per_objfile->objfile;
b6807d98 23748
7188ed02 23749 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23750 if (cu == nullptr)
23751 cu = load_cu (per_cu, per_objfile, false);
23752
23753 if (cu == nullptr)
cc12ce38
DE
23754 {
23755 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23756 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23757 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23758 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23759 }
b6807d98 23760
9c541725 23761 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23762 if (!die)
9d8780f0
SM
23763 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23764 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23765
23766 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23767 if (attr == NULL)
23768 return NULL;
23769
e3b94546 23770 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23771 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23772
23773 switch (attr->form)
23774 {
23775 case DW_FORM_addr:
336d760d 23776 case DW_FORM_addrx:
b6807d98
TT
23777 case DW_FORM_GNU_addr_index:
23778 {
23779 gdb_byte *tem;
23780
23781 *len = cu->header.addr_size;
224c3ddb 23782 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23783 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23784 result = tem;
23785 }
23786 break;
23787 case DW_FORM_string:
23788 case DW_FORM_strp:
cf532bd1 23789 case DW_FORM_strx:
b6807d98
TT
23790 case DW_FORM_GNU_str_index:
23791 case DW_FORM_GNU_strp_alt:
c6481205 23792 /* The string is already allocated on the objfile obstack, point
b6807d98 23793 directly to it. */
2c830f54
TT
23794 {
23795 const char *attr_name = attr->as_string ();
23796 result = (const gdb_byte *) attr_name;
23797 *len = strlen (attr_name);
23798 }
b6807d98
TT
23799 break;
23800 case DW_FORM_block1:
23801 case DW_FORM_block2:
23802 case DW_FORM_block4:
23803 case DW_FORM_block:
23804 case DW_FORM_exprloc:
0224619f 23805 case DW_FORM_data16:
9d2246fc
TT
23806 {
23807 struct dwarf_block *block = attr->as_block ();
23808 result = block->data;
23809 *len = block->size;
23810 }
b6807d98
TT
23811 break;
23812
23813 /* The DW_AT_const_value attributes are supposed to carry the
23814 symbol's value "represented as it would be on the target
23815 architecture." By the time we get here, it's already been
23816 converted to host endianness, so we just need to sign- or
23817 zero-extend it as appropriate. */
23818 case DW_FORM_data1:
23819 type = die_type (die, cu);
23820 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23821 if (result == NULL)
23822 result = write_constant_as_bytes (obstack, byte_order,
23823 type, value, len);
23824 break;
23825 case DW_FORM_data2:
23826 type = die_type (die, cu);
23827 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23828 if (result == NULL)
23829 result = write_constant_as_bytes (obstack, byte_order,
23830 type, value, len);
23831 break;
23832 case DW_FORM_data4:
23833 type = die_type (die, cu);
23834 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23835 if (result == NULL)
23836 result = write_constant_as_bytes (obstack, byte_order,
23837 type, value, len);
23838 break;
23839 case DW_FORM_data8:
23840 type = die_type (die, cu);
23841 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23842 if (result == NULL)
23843 result = write_constant_as_bytes (obstack, byte_order,
23844 type, value, len);
23845 break;
23846
23847 case DW_FORM_sdata:
663c44ac 23848 case DW_FORM_implicit_const:
b6807d98
TT
23849 type = die_type (die, cu);
23850 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23851 type, attr->as_signed (), len);
b6807d98
TT
23852 break;
23853
23854 case DW_FORM_udata:
23855 type = die_type (die, cu);
23856 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23857 type, attr->as_unsigned (), len);
b6807d98
TT
23858 break;
23859
23860 default:
b98664d3 23861 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23862 dwarf_form_name (attr->form));
23863 break;
23864 }
23865
23866 return result;
23867}
23868
d4c9a4f8 23869/* See read.h. */
7942e96e
AA
23870
23871struct type *
9c541725 23872dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23873 dwarf2_per_cu_data *per_cu,
23874 dwarf2_per_objfile *per_objfile)
7942e96e 23875{
7942e96e
AA
23876 struct die_info *die;
23877
7188ed02 23878 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23879 if (cu == nullptr)
23880 cu = load_cu (per_cu, per_objfile, false);
23881
23882 if (cu == nullptr)
23883 return nullptr;
7942e96e 23884
9c541725 23885 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23886 if (!die)
23887 return NULL;
23888
23889 return die_type (die, cu);
23890}
23891
8cb5117c 23892/* See read.h. */
8a9b8146
TT
23893
23894struct type *
b64f50a1 23895dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23896 dwarf2_per_cu_data *per_cu,
23897 dwarf2_per_objfile *per_objfile)
8a9b8146 23898{
9c541725 23899 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23900 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23901}
23902
ac9ec31b 23903/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23904 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23905 On exit *REF_CU is the CU of the result.
23906 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23907
23908static struct die_info *
ac9ec31b
DE
23909follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23910 struct dwarf2_cu **ref_cu)
348e048f 23911{
348e048f 23912 struct die_info temp_die;
c24bdb02 23913 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 23914 struct die_info *die;
976ca316 23915 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23916
348e048f 23917
ac9ec31b
DE
23918 /* While it might be nice to assert sig_type->type == NULL here,
23919 we can get here for DW_AT_imported_declaration where we need
23920 the DIE not the type. */
348e048f 23921
616c069a 23922 /* If necessary, add it to the queue and load its DIEs.
348e048f 23923
616c069a
SM
23924 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23925 it doesn't mean they are currently loaded. Since we require them
23926 to be loaded, we must check for ourselves. */
976ca316 23927 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
616c069a
SM
23928 language_minimal)
23929 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
976ca316 23930 read_signatured_type (sig_type, per_objfile);
348e048f 23931
976ca316 23932 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 23933 gdb_assert (sig_cu != NULL);
9c541725
PA
23934 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23935 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23936 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23937 to_underlying (temp_die.sect_off));
348e048f
DE
23938 if (die)
23939 {
796a7ff8
DE
23940 /* For .gdb_index version 7 keep track of included TUs.
23941 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23942 if (per_objfile->per_bfd->index_table != NULL
23943 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23944 {
ae640021 23945 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23946 }
23947
348e048f 23948 *ref_cu = sig_cu;
c24bdb02
KS
23949 if (sig_cu != cu)
23950 sig_cu->ancestor = cu;
23951
348e048f
DE
23952 return die;
23953 }
23954
ac9ec31b
DE
23955 return NULL;
23956}
23957
23958/* Follow signatured type referenced by ATTR in SRC_DIE.
23959 On entry *REF_CU is the CU of SRC_DIE.
23960 On exit *REF_CU is the CU of the result.
23961 The result is the DIE of the type.
23962 If the referenced type cannot be found an error is thrown. */
23963
23964static struct die_info *
ff39bb5e 23965follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23966 struct dwarf2_cu **ref_cu)
23967{
630ed6b9 23968 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23969 struct signatured_type *sig_type;
23970 struct die_info *die;
23971
23972 gdb_assert (attr->form == DW_FORM_ref_sig8);
23973
a2ce51a0 23974 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23975 /* sig_type will be NULL if the signatured type is missing from
23976 the debug info. */
23977 if (sig_type == NULL)
23978 {
23979 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
23980 " from DIE at %s [in module %s]"),
23981 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23982 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23983 }
23984
23985 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23986 if (die == NULL)
23987 {
23988 dump_die_for_error (src_die);
23989 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23990 " from DIE at %s [in module %s]"),
23991 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23992 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23993 }
23994
23995 return die;
23996}
23997
23998/* Get the type specified by SIGNATURE referenced in DIE/CU,
23999 reading in and processing the type unit if necessary. */
24000
24001static struct type *
24002get_signatured_type (struct die_info *die, ULONGEST signature,
24003 struct dwarf2_cu *cu)
24004{
976ca316 24005 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
24006 struct signatured_type *sig_type;
24007 struct dwarf2_cu *type_cu;
24008 struct die_info *type_die;
24009 struct type *type;
24010
a2ce51a0 24011 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
24012 /* sig_type will be NULL if the signatured type is missing from
24013 the debug info. */
24014 if (sig_type == NULL)
24015 {
b98664d3 24016 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24017 " from DIE at %s [in module %s]"),
24018 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24019 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24020 return build_error_marker_type (cu, die);
24021 }
24022
24023 /* If we already know the type we're done. */
976ca316 24024 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
24025 if (type != nullptr)
24026 return type;
ac9ec31b
DE
24027
24028 type_cu = cu;
24029 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24030 if (type_die != NULL)
24031 {
24032 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24033 is created. This is important, for example, because for c++ classes
24034 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24035 type = read_type_die (type_die, type_cu);
24036 if (type == NULL)
24037 {
b98664d3 24038 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24039 " referenced from DIE at %s [in module %s]"),
24040 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24041 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24042 type = build_error_marker_type (cu, die);
24043 }
24044 }
24045 else
24046 {
b98664d3 24047 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24048 " from DIE at %s [in module %s]"),
24049 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24050 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24051 type = build_error_marker_type (cu, die);
24052 }
e286671b 24053
976ca316 24054 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
24055
24056 return type;
24057}
24058
24059/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24060 reading in and processing the type unit if necessary. */
24061
24062static struct type *
ff39bb5e 24063get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24064 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24065{
24066 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 24067 if (attr->form_is_ref ())
ac9ec31b
DE
24068 {
24069 struct dwarf2_cu *type_cu = cu;
24070 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24071
24072 return read_type_die (type_die, type_cu);
24073 }
24074 else if (attr->form == DW_FORM_ref_sig8)
24075 {
630ed6b9 24076 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
24077 }
24078 else
24079 {
976ca316 24080 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24081
b98664d3 24082 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24083 " at %s [in module %s]"),
24084 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 24085 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24086 return build_error_marker_type (cu, die);
24087 }
348e048f
DE
24088}
24089
e5fe5e75 24090/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24091
24092static void
ab432490
SM
24093load_full_type_unit (dwarf2_per_cu_data *per_cu,
24094 dwarf2_per_objfile *per_objfile)
348e048f 24095{
52dc124a 24096 struct signatured_type *sig_type;
348e048f 24097
f4dc4d17 24098 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24099 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24100
6721b2ec
DE
24101 /* We have the per_cu, but we need the signatured_type.
24102 Fortunately this is an easy translation. */
24103 gdb_assert (per_cu->is_debug_types);
24104 sig_type = (struct signatured_type *) per_cu;
348e048f 24105
7188ed02 24106 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24107
ab432490 24108 read_signatured_type (sig_type, per_objfile);
348e048f 24109
7188ed02 24110 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24111}
24112
3019eac3
DE
24113/* Read in a signatured type and build its CU and DIEs.
24114 If the type is a stub for the real type in a DWO file,
24115 read in the real type from the DWO file as well. */
dee91e82
DE
24116
24117static void
ab432490
SM
24118read_signatured_type (signatured_type *sig_type,
24119 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24120{
24121 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24122
3019eac3 24123 gdb_assert (per_cu->is_debug_types);
7188ed02 24124 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24125
2e671100 24126 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24127
24128 if (!reader.dummy_p)
24129 {
24130 struct dwarf2_cu *cu = reader.cu;
24131 const gdb_byte *info_ptr = reader.info_ptr;
24132
24133 gdb_assert (cu->die_hash == NULL);
24134 cu->die_hash =
24135 htab_create_alloc_ex (cu->header.length / 12,
24136 die_hash,
24137 die_eq,
24138 NULL,
24139 &cu->comp_unit_obstack,
24140 hashtab_obstack_allocate,
24141 dummy_obstack_deallocate);
24142
3e225074 24143 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24144 reader.comp_unit_die->child
24145 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24146 reader.comp_unit_die);
24147 cu->dies = reader.comp_unit_die;
24148 /* comp_unit_die is not stored in die_hash, no need. */
24149
24150 /* We try not to read any attributes in this function, because
24151 not all CUs needed for references have been loaded yet, and
24152 symbol table processing isn't initialized. But we have to
24153 set the CU language, or we won't be able to build types
24154 correctly. Similarly, if we do not read the producer, we can
24155 not apply producer-specific interpretation. */
24156 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24157
24158 reader.keep ();
c0ab21c2
TT
24159 }
24160
7ee85ab1 24161 sig_type->per_cu.tu_read = 1;
c906108c
SS
24162}
24163
c906108c
SS
24164/* Decode simple location descriptions.
24165 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24166 the location and return the value. If COMPUTED is non-null, it is
24167 set to true to indicate that decoding was successful, and false
24168 otherwise. If COMPUTED is null, then this function may emit a
24169 complaint. */
c906108c
SS
24170
24171static CORE_ADDR
7d79de9a 24172decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24173{
5e22e966 24174 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24175 size_t i;
24176 size_t size = blk->size;
d521ce57 24177 const gdb_byte *data = blk->data;
21ae7a4d
JK
24178 CORE_ADDR stack[64];
24179 int stacki;
24180 unsigned int bytes_read, unsnd;
24181 gdb_byte op;
c906108c 24182
7d79de9a
TT
24183 if (computed != nullptr)
24184 *computed = false;
24185
21ae7a4d
JK
24186 i = 0;
24187 stacki = 0;
24188 stack[stacki] = 0;
24189 stack[++stacki] = 0;
24190
24191 while (i < size)
24192 {
24193 op = data[i++];
24194 switch (op)
24195 {
24196 case DW_OP_lit0:
24197 case DW_OP_lit1:
24198 case DW_OP_lit2:
24199 case DW_OP_lit3:
24200 case DW_OP_lit4:
24201 case DW_OP_lit5:
24202 case DW_OP_lit6:
24203 case DW_OP_lit7:
24204 case DW_OP_lit8:
24205 case DW_OP_lit9:
24206 case DW_OP_lit10:
24207 case DW_OP_lit11:
24208 case DW_OP_lit12:
24209 case DW_OP_lit13:
24210 case DW_OP_lit14:
24211 case DW_OP_lit15:
24212 case DW_OP_lit16:
24213 case DW_OP_lit17:
24214 case DW_OP_lit18:
24215 case DW_OP_lit19:
24216 case DW_OP_lit20:
24217 case DW_OP_lit21:
24218 case DW_OP_lit22:
24219 case DW_OP_lit23:
24220 case DW_OP_lit24:
24221 case DW_OP_lit25:
24222 case DW_OP_lit26:
24223 case DW_OP_lit27:
24224 case DW_OP_lit28:
24225 case DW_OP_lit29:
24226 case DW_OP_lit30:
24227 case DW_OP_lit31:
24228 stack[++stacki] = op - DW_OP_lit0;
24229 break;
f1bea926 24230
21ae7a4d
JK
24231 case DW_OP_reg0:
24232 case DW_OP_reg1:
24233 case DW_OP_reg2:
24234 case DW_OP_reg3:
24235 case DW_OP_reg4:
24236 case DW_OP_reg5:
24237 case DW_OP_reg6:
24238 case DW_OP_reg7:
24239 case DW_OP_reg8:
24240 case DW_OP_reg9:
24241 case DW_OP_reg10:
24242 case DW_OP_reg11:
24243 case DW_OP_reg12:
24244 case DW_OP_reg13:
24245 case DW_OP_reg14:
24246 case DW_OP_reg15:
24247 case DW_OP_reg16:
24248 case DW_OP_reg17:
24249 case DW_OP_reg18:
24250 case DW_OP_reg19:
24251 case DW_OP_reg20:
24252 case DW_OP_reg21:
24253 case DW_OP_reg22:
24254 case DW_OP_reg23:
24255 case DW_OP_reg24:
24256 case DW_OP_reg25:
24257 case DW_OP_reg26:
24258 case DW_OP_reg27:
24259 case DW_OP_reg28:
24260 case DW_OP_reg29:
24261 case DW_OP_reg30:
24262 case DW_OP_reg31:
24263 stack[++stacki] = op - DW_OP_reg0;
24264 if (i < size)
7d79de9a
TT
24265 {
24266 if (computed == nullptr)
24267 dwarf2_complex_location_expr_complaint ();
24268 else
24269 return 0;
24270 }
21ae7a4d 24271 break;
c906108c 24272
21ae7a4d
JK
24273 case DW_OP_regx:
24274 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24275 i += bytes_read;
24276 stack[++stacki] = unsnd;
24277 if (i < size)
7d79de9a
TT
24278 {
24279 if (computed == nullptr)
24280 dwarf2_complex_location_expr_complaint ();
24281 else
24282 return 0;
24283 }
21ae7a4d 24284 break;
c906108c 24285
21ae7a4d 24286 case DW_OP_addr:
c8a7a66f
TT
24287 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24288 &bytes_read);
21ae7a4d
JK
24289 i += bytes_read;
24290 break;
d53d4ac5 24291
21ae7a4d
JK
24292 case DW_OP_const1u:
24293 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24294 i += 1;
24295 break;
24296
24297 case DW_OP_const1s:
24298 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24299 i += 1;
24300 break;
24301
24302 case DW_OP_const2u:
24303 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24304 i += 2;
24305 break;
24306
24307 case DW_OP_const2s:
24308 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24309 i += 2;
24310 break;
d53d4ac5 24311
21ae7a4d
JK
24312 case DW_OP_const4u:
24313 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24314 i += 4;
24315 break;
24316
24317 case DW_OP_const4s:
24318 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24319 i += 4;
24320 break;
24321
585861ea
JK
24322 case DW_OP_const8u:
24323 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24324 i += 8;
24325 break;
24326
21ae7a4d
JK
24327 case DW_OP_constu:
24328 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24329 &bytes_read);
24330 i += bytes_read;
24331 break;
24332
24333 case DW_OP_consts:
24334 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24335 i += bytes_read;
24336 break;
24337
24338 case DW_OP_dup:
24339 stack[stacki + 1] = stack[stacki];
24340 stacki++;
24341 break;
24342
24343 case DW_OP_plus:
24344 stack[stacki - 1] += stack[stacki];
24345 stacki--;
24346 break;
24347
24348 case DW_OP_plus_uconst:
24349 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24350 &bytes_read);
24351 i += bytes_read;
24352 break;
24353
24354 case DW_OP_minus:
24355 stack[stacki - 1] -= stack[stacki];
24356 stacki--;
24357 break;
24358
24359 case DW_OP_deref:
24360 /* If we're not the last op, then we definitely can't encode
24361 this using GDB's address_class enum. This is valid for partial
24362 global symbols, although the variable's address will be bogus
24363 in the psymtab. */
24364 if (i < size)
7d79de9a
TT
24365 {
24366 if (computed == nullptr)
24367 dwarf2_complex_location_expr_complaint ();
24368 else
24369 return 0;
24370 }
21ae7a4d
JK
24371 break;
24372
dda83cd7 24373 case DW_OP_GNU_push_tls_address:
4aa4e28b 24374 case DW_OP_form_tls_address:
21ae7a4d
JK
24375 /* The top of the stack has the offset from the beginning
24376 of the thread control block at which the variable is located. */
24377 /* Nothing should follow this operator, so the top of stack would
24378 be returned. */
24379 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24380 address will be bogus in the psymtab. Make it always at least
24381 non-zero to not look as a variable garbage collected by linker
24382 which have DW_OP_addr 0. */
21ae7a4d 24383 if (i < size)
7d79de9a
TT
24384 {
24385 if (computed == nullptr)
24386 dwarf2_complex_location_expr_complaint ();
24387 else
24388 return 0;
24389 }
585861ea 24390 stack[stacki]++;
dda83cd7 24391 break;
21ae7a4d
JK
24392
24393 case DW_OP_GNU_uninit:
7d79de9a
TT
24394 if (computed != nullptr)
24395 return 0;
21ae7a4d
JK
24396 break;
24397
336d760d 24398 case DW_OP_addrx:
3019eac3 24399 case DW_OP_GNU_addr_index:
49f6c839 24400 case DW_OP_GNU_const_index:
3019eac3
DE
24401 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24402 &bytes_read);
24403 i += bytes_read;
24404 break;
24405
21ae7a4d 24406 default:
7d79de9a
TT
24407 if (computed == nullptr)
24408 {
24409 const char *name = get_DW_OP_name (op);
21ae7a4d 24410
7d79de9a
TT
24411 if (name)
24412 complaint (_("unsupported stack op: '%s'"),
24413 name);
24414 else
24415 complaint (_("unsupported stack op: '%02x'"),
24416 op);
24417 }
21ae7a4d
JK
24418
24419 return (stack[stacki]);
d53d4ac5 24420 }
3c6e0cb3 24421
21ae7a4d 24422 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24423 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24424 if (stacki >= ARRAY_SIZE (stack) - 1)
24425 {
7d79de9a
TT
24426 if (computed == nullptr)
24427 complaint (_("location description stack overflow"));
21ae7a4d
JK
24428 return 0;
24429 }
24430
24431 if (stacki <= 0)
24432 {
7d79de9a
TT
24433 if (computed == nullptr)
24434 complaint (_("location description stack underflow"));
21ae7a4d
JK
24435 return 0;
24436 }
24437 }
7d79de9a
TT
24438
24439 if (computed != nullptr)
24440 *computed = true;
21ae7a4d 24441 return (stack[stacki]);
c906108c
SS
24442}
24443
24444/* memory allocation interface */
24445
c906108c 24446static struct dwarf_block *
7b5a2f43 24447dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24448{
8d749320 24449 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24450}
24451
c906108c 24452static struct die_info *
b60c80d6 24453dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24454{
24455 struct die_info *die;
b60c80d6
DJ
24456 size_t size = sizeof (struct die_info);
24457
24458 if (num_attrs > 1)
24459 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24460
b60c80d6 24461 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24462 memset (die, 0, sizeof (struct die_info));
24463 return (die);
24464}
2e276125
JB
24465
24466\f
a036ba48 24467
c90ec28a 24468/* Macro support. */
cf2c3c16 24469
9eac9650
TT
24470/* An overload of dwarf_decode_macros that finds the correct section
24471 and ensures it is read in before calling the other overload. */
24472
24473static void
24474dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24475 int section_is_gnu)
24476{
976ca316
SM
24477 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24478 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24479 const struct line_header *lh = cu->line_header;
9eac9650
TT
24480 unsigned int offset_size = cu->header.offset_size;
24481 struct dwarf2_section_info *section;
24482 const char *section_name;
24483
24484 if (cu->dwo_unit != nullptr)
24485 {
24486 if (section_is_gnu)
24487 {
24488 section = &cu->dwo_unit->dwo_file->sections.macro;
24489 section_name = ".debug_macro.dwo";
24490 }
24491 else
24492 {
24493 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24494 section_name = ".debug_macinfo.dwo";
24495 }
24496 }
24497 else
24498 {
24499 if (section_is_gnu)
24500 {
976ca316 24501 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24502 section_name = ".debug_macro";
24503 }
24504 else
24505 {
976ca316 24506 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24507 section_name = ".debug_macinfo";
24508 }
24509 }
24510
24511 section->read (objfile);
24512 if (section->buffer == nullptr)
24513 {
24514 complaint (_("missing %s section"), section_name);
24515 return;
24516 }
24517
24518 buildsym_compunit *builder = cu->get_builder ();
24519
048fde1e 24520 struct dwarf2_section_info *str_offsets_section;
24521 struct dwarf2_section_info *str_section;
24522 ULONGEST str_offsets_base;
24523
24524 if (cu->dwo_unit != nullptr)
24525 {
24526 str_offsets_section = &cu->dwo_unit->dwo_file
24527 ->sections.str_offsets;
24528 str_section = &cu->dwo_unit->dwo_file->sections.str;
24529 str_offsets_base = cu->header.addr_size;
24530 }
24531 else
24532 {
24533 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24534 str_section = &per_objfile->per_bfd->str;
24535 str_offsets_base = *cu->str_offsets_base;
24536 }
24537
976ca316 24538 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24539 offset_size, offset, str_section, str_offsets_section,
24540 str_offsets_base, section_is_gnu);
9eac9650
TT
24541}
24542
3019eac3
DE
24543/* Return the .debug_loc section to use for CU.
24544 For DWO files use .debug_loc.dwo. */
24545
24546static struct dwarf2_section_info *
24547cu_debug_loc_section (struct dwarf2_cu *cu)
24548{
976ca316 24549 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24550
3019eac3 24551 if (cu->dwo_unit)
43988095
JK
24552 {
24553 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24554
43988095
JK
24555 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24556 }
976ca316
SM
24557 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24558 : &per_objfile->per_bfd->loc);
3019eac3
DE
24559}
24560
d0ce17d8
CT
24561/* Return the .debug_rnglists section to use for CU. */
24562static struct dwarf2_section_info *
24563cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24564{
24565 if (cu->header.version < 5)
24566 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24567 cu->header.version);
24568 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24569
24570 /* Make sure we read the .debug_rnglists section from the file that
24571 contains the DW_AT_ranges attribute we are reading. Normally that
24572 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24573 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24574 program. */
24575 if (cu->dwo_unit != nullptr
24576 && tag != DW_TAG_compile_unit
24577 && tag != DW_TAG_skeleton_unit)
24578 {
24579 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24580
24581 if (sections->rnglists.size > 0)
24582 return &sections->rnglists;
24583 else
24584 error (_(".debug_rnglists section is missing from .dwo file."));
24585 }
24586 return &dwarf2_per_objfile->per_bfd->rnglists;
24587}
24588
8cf6f0b1
TT
24589/* A helper function that fills in a dwarf2_loclist_baton. */
24590
24591static void
24592fill_in_loclist_baton (struct dwarf2_cu *cu,
24593 struct dwarf2_loclist_baton *baton,
ff39bb5e 24594 const struct attribute *attr)
8cf6f0b1 24595{
976ca316 24596 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24597 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24598
976ca316 24599 section->read (per_objfile->objfile);
8cf6f0b1 24600
976ca316 24601 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24602 baton->per_cu = cu->per_cu;
24603 gdb_assert (baton->per_cu);
24604 /* We don't know how long the location list is, but make sure we
24605 don't run off the edge of the section. */
d4df075e
TT
24606 baton->size = section->size - attr->as_unsigned ();
24607 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24608 if (cu->base_address.has_value ())
24609 baton->base_address = *cu->base_address;
24610 else
24611 baton->base_address = 0;
f664829e 24612 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24613}
24614
4c2df51b 24615static void
ff39bb5e 24616dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24617 struct dwarf2_cu *cu, int is_block)
4c2df51b 24618{
976ca316
SM
24619 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24620 struct objfile *objfile = per_objfile->objfile;
3019eac3 24621 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24622
cd6c91b4 24623 if (attr->form_is_section_offset ()
3019eac3 24624 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24625 the section. If so, fall through to the complaint in the
24626 other branch. */
d4df075e 24627 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24628 {
0d53c4c4 24629 struct dwarf2_loclist_baton *baton;
4c2df51b 24630
8d749320 24631 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24632
8cf6f0b1 24633 fill_in_loclist_baton (cu, baton, attr);
be391dca 24634
2b24b6e4 24635 if (!cu->base_address.has_value ())
b98664d3 24636 complaint (_("Location list used without "
3e43a32a 24637 "specifying the CU base address."));
4c2df51b 24638
f1e6e072
TT
24639 SYMBOL_ACLASS_INDEX (sym) = (is_block
24640 ? dwarf2_loclist_block_index
24641 : dwarf2_loclist_index);
0d53c4c4
DJ
24642 SYMBOL_LOCATION_BATON (sym) = baton;
24643 }
24644 else
24645 {
24646 struct dwarf2_locexpr_baton *baton;
24647
8d749320 24648 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24649 baton->per_objfile = per_objfile;
ae0d2f24
UW
24650 baton->per_cu = cu->per_cu;
24651 gdb_assert (baton->per_cu);
0d53c4c4 24652
4fc6c0d5 24653 if (attr->form_is_block ())
0d53c4c4
DJ
24654 {
24655 /* Note that we're just copying the block's data pointer
24656 here, not the actual data. We're still pointing into the
6502dd73
DJ
24657 info_buffer for SYM's objfile; right now we never release
24658 that buffer, but when we do clean up properly this may
24659 need to change. */
9d2246fc
TT
24660 struct dwarf_block *block = attr->as_block ();
24661 baton->size = block->size;
24662 baton->data = block->data;
0d53c4c4
DJ
24663 }
24664 else
24665 {
24666 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24667 sym->natural_name ());
0d53c4c4 24668 baton->size = 0;
0d53c4c4 24669 }
6e70227d 24670
f1e6e072
TT
24671 SYMBOL_ACLASS_INDEX (sym) = (is_block
24672 ? dwarf2_locexpr_block_index
24673 : dwarf2_locexpr_index);
0d53c4c4
DJ
24674 SYMBOL_LOCATION_BATON (sym) = baton;
24675 }
4c2df51b 24676}
6502dd73 24677
2e6a9f79 24678/* See read.h. */
96408a79 24679
2e6a9f79
SM
24680const comp_unit_head *
24681dwarf2_per_cu_data::get_header () const
96408a79 24682{
2e6a9f79
SM
24683 if (!m_header_read_in)
24684 {
24685 const gdb_byte *info_ptr
24686 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24687
2e6a9f79 24688 memset (&m_header, 0, sizeof (m_header));
96408a79 24689
2e6a9f79
SM
24690 read_comp_unit_head (&m_header, info_ptr, this->section,
24691 rcuh_kind::COMPILE);
a9f172c6
TT
24692
24693 m_header_read_in = true;
2e6a9f79 24694 }
96408a79 24695
2e6a9f79 24696 return &m_header;
96408a79
SA
24697}
24698
09ba997f 24699/* See read.h. */
ae0d2f24 24700
98714339 24701int
09ba997f 24702dwarf2_per_cu_data::addr_size () const
ae0d2f24 24703{
2e6a9f79 24704 return this->get_header ()->addr_size;
ae0d2f24
UW
24705}
24706
09ba997f 24707/* See read.h. */
9eae7c52
TT
24708
24709int
09ba997f 24710dwarf2_per_cu_data::offset_size () const
9eae7c52 24711{
2e6a9f79 24712 return this->get_header ()->offset_size;
96408a79
SA
24713}
24714
09ba997f 24715/* See read.h. */
96408a79
SA
24716
24717int
09ba997f 24718dwarf2_per_cu_data::ref_addr_size () const
96408a79 24719{
2e6a9f79 24720 const comp_unit_head *header = this->get_header ();
96408a79 24721
2e6a9f79
SM
24722 if (header->version == 2)
24723 return header->addr_size;
96408a79 24724 else
2e6a9f79 24725 return header->offset_size;
181cebd4
JK
24726}
24727
09ba997f 24728/* See read.h. */
9aa1f1e3 24729
09ba997f 24730struct type *
293e7e51 24731dwarf2_cu::addr_type () const
9a49df9d 24732{
293e7e51 24733 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24734 struct type *void_type = objfile_type (objfile)->builtin_void;
24735 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24736 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24737
24738 if (TYPE_LENGTH (addr_type) == addr_size)
24739 return addr_type;
24740
c6d940a9 24741 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24742 return addr_type;
24743}
24744
22b6cd70
TT
24745/* A helper function for dwarf2_find_containing_comp_unit that returns
24746 the index of the result, and that searches a vector. It will
24747 return a result even if the offset in question does not actually
24748 occur in any CU. This is separate so that it can be unit
24749 tested. */
ae038cb0 24750
22b6cd70
TT
24751static int
24752dwarf2_find_containing_comp_unit
24753 (sect_offset sect_off,
24754 unsigned int offset_in_dwz,
24755 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24756{
ae038cb0
DJ
24757 int low, high;
24758
ae038cb0 24759 low = 0;
22b6cd70 24760 high = all_comp_units.size () - 1;
ae038cb0
DJ
24761 while (high > low)
24762 {
36586728 24763 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24764 int mid = low + (high - low) / 2;
9a619af0 24765
22b6cd70 24766 mid_cu = all_comp_units[mid];
36586728 24767 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24768 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24769 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24770 high = mid;
24771 else
24772 low = mid + 1;
24773 }
24774 gdb_assert (low == high);
22b6cd70
TT
24775 return low;
24776}
24777
24778/* Locate the .debug_info compilation unit from CU's objfile which contains
24779 the DIE at OFFSET. Raises an error on failure. */
24780
24781static struct dwarf2_per_cu_data *
24782dwarf2_find_containing_comp_unit (sect_offset sect_off,
24783 unsigned int offset_in_dwz,
976ca316 24784 dwarf2_per_objfile *per_objfile)
22b6cd70 24785{
976ca316
SM
24786 int low = dwarf2_find_containing_comp_unit
24787 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24788 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24789
45b8ae0c 24790 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24791 {
36586728 24792 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24793 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24794 "offset %s [in module %s]"),
24795 sect_offset_str (sect_off),
976ca316 24796 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24797
976ca316 24798 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24799 <= sect_off);
976ca316 24800 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
24801 }
24802 else
24803 {
976ca316 24804 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24805 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24806 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24807 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24808 return this_cu;
24809 }
24810}
24811
22b6cd70
TT
24812#if GDB_SELF_TEST
24813
24814namespace selftests {
24815namespace find_containing_comp_unit {
24816
24817static void
24818run_test ()
24819{
24820 struct dwarf2_per_cu_data one {};
24821 struct dwarf2_per_cu_data two {};
24822 struct dwarf2_per_cu_data three {};
24823 struct dwarf2_per_cu_data four {};
24824
24825 one.length = 5;
24826 two.sect_off = sect_offset (one.length);
24827 two.length = 7;
24828
24829 three.length = 5;
24830 three.is_dwz = 1;
24831 four.sect_off = sect_offset (three.length);
24832 four.length = 7;
24833 four.is_dwz = 1;
24834
24835 std::vector<dwarf2_per_cu_data *> units;
24836 units.push_back (&one);
24837 units.push_back (&two);
24838 units.push_back (&three);
24839 units.push_back (&four);
24840
24841 int result;
24842
24843 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24844 SELF_CHECK (units[result] == &one);
24845 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24846 SELF_CHECK (units[result] == &one);
24847 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24848 SELF_CHECK (units[result] == &two);
24849
24850 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24851 SELF_CHECK (units[result] == &three);
24852 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24853 SELF_CHECK (units[result] == &three);
24854 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24855 SELF_CHECK (units[result] == &four);
24856}
24857
24858}
24859}
24860
24861#endif /* GDB_SELF_TEST */
24862
9e021579 24863/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 24864
9e021579
SM
24865dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24866 dwarf2_per_objfile *per_objfile)
24867 : per_cu (per_cu),
24868 per_objfile (per_objfile),
9068261f
AB
24869 mark (false),
24870 has_loclist (false),
24871 checked_producer (false),
24872 producer_is_gxx_lt_4_6 (false),
24873 producer_is_gcc_lt_4_3 (false),
eb77c9df 24874 producer_is_icc (false),
9068261f 24875 producer_is_icc_lt_14 (false),
c258c396 24876 producer_is_codewarrior (false),
9068261f 24877 processing_has_namespace_info (false)
93311388 24878{
9816fde3
JK
24879}
24880
24881/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24882
24883static void
95554aad
TT
24884prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24885 enum language pretend_language)
9816fde3
JK
24886{
24887 struct attribute *attr;
24888
24889 /* Set the language we're debugging. */
24890 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24891 if (attr != nullptr)
529908cb 24892 set_cu_language (attr->constant_value (0), cu);
9816fde3 24893 else
9cded63f 24894 {
95554aad 24895 cu->language = pretend_language;
9cded63f
TT
24896 cu->language_defn = language_def (cu->language);
24897 }
dee91e82 24898
7d45c7c3 24899 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24900}
24901
7188ed02 24902/* See read.h. */
ae038cb0 24903
7188ed02
SM
24904dwarf2_cu *
24905dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24906{
7188ed02
SM
24907 auto it = m_dwarf2_cus.find (per_cu);
24908 if (it == m_dwarf2_cus.end ())
24909 return nullptr;
ae038cb0 24910
7188ed02
SM
24911 return it->second;
24912}
24913
24914/* See read.h. */
24915
24916void
24917dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24918{
24919 gdb_assert (this->get_cu (per_cu) == nullptr);
24920
24921 m_dwarf2_cus[per_cu] = cu;
24922}
24923
24924/* See read.h. */
24925
24926void
24927dwarf2_per_objfile::age_comp_units ()
24928{
17e593e9
SM
24929 dwarf_read_debug_printf_v ("running");
24930
08ac5771
SM
24931 /* This is not expected to be called in the middle of CU expansion. There is
24932 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24933 loaded in memory. Calling age_comp_units while the queue is in use could
24934 make us free the DIEs for a CU that is in the queue and therefore break
24935 that invariant. */
24936 gdb_assert (!this->per_bfd->queue.has_value ());
24937
7188ed02
SM
24938 /* Start by clearing all marks. */
24939 for (auto pair : m_dwarf2_cus)
24940 pair.second->mark = false;
24941
24942 /* Traverse all CUs, mark them and their dependencies if used recently
24943 enough. */
24944 for (auto pair : m_dwarf2_cus)
ae038cb0 24945 {
7188ed02
SM
24946 dwarf2_cu *cu = pair.second;
24947
24948 cu->last_used++;
24949 if (cu->last_used <= dwarf_max_cache_age)
24950 dwarf2_mark (cu);
ae038cb0
DJ
24951 }
24952
7188ed02
SM
24953 /* Delete all CUs still not marked. */
24954 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24955 {
7188ed02 24956 dwarf2_cu *cu = it->second;
ae038cb0 24957
7188ed02 24958 if (!cu->mark)
ae038cb0 24959 {
17e593e9
SM
24960 dwarf_read_debug_printf_v ("deleting old CU %s",
24961 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
24962 delete cu;
24963 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24964 }
24965 else
7188ed02 24966 it++;
ae038cb0
DJ
24967 }
24968}
24969
7188ed02 24970/* See read.h. */
ae038cb0 24971
7188ed02
SM
24972void
24973dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24974{
7188ed02
SM
24975 auto it = m_dwarf2_cus.find (per_cu);
24976 if (it == m_dwarf2_cus.end ())
24977 return;
ae038cb0 24978
7188ed02 24979 delete it->second;
ae038cb0 24980
7188ed02
SM
24981 m_dwarf2_cus.erase (it);
24982}
ae038cb0 24983
7188ed02
SM
24984dwarf2_per_objfile::~dwarf2_per_objfile ()
24985{
24986 remove_all_cus ();
ae038cb0
DJ
24987}
24988
dee91e82
DE
24989/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24990 We store these in a hash table separate from the DIEs, and preserve them
24991 when the DIEs are flushed out of cache.
24992
24993 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24994 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24995 or the type may come from a DWO file. Furthermore, while it's more logical
24996 to use per_cu->section+offset, with Fission the section with the data is in
24997 the DWO file but we don't know that section at the point we need it.
24998 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24999 because we can enter the lookup routine, get_die_type_at_offset, from
25000 outside this file, and thus won't necessarily have PER_CU->cu.
25001 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25002
dee91e82 25003struct dwarf2_per_cu_offset_and_type
1c379e20 25004{
dee91e82 25005 const struct dwarf2_per_cu_data *per_cu;
9c541725 25006 sect_offset sect_off;
1c379e20
DJ
25007 struct type *type;
25008};
25009
dee91e82 25010/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25011
25012static hashval_t
dee91e82 25013per_cu_offset_and_type_hash (const void *item)
1c379e20 25014{
9a3c8263
SM
25015 const struct dwarf2_per_cu_offset_and_type *ofs
25016 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25017
9c541725 25018 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25019}
25020
dee91e82 25021/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25022
25023static int
dee91e82 25024per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25025{
9a3c8263
SM
25026 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25027 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25028 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25029 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25030
dee91e82 25031 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25032 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25033}
25034
25035/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25036 table if necessary. For convenience, return TYPE.
25037
25038 The DIEs reading must have careful ordering to:
85102364 25039 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25040 reading current DIE.
25041 * Not trying to dereference contents of still incompletely read in types
25042 while reading in other DIEs.
25043 * Enable referencing still incompletely read in types just by a pointer to
25044 the type without accessing its fields.
25045
25046 Therefore caller should follow these rules:
25047 * Try to fetch any prerequisite types we may need to build this DIE type
25048 before building the type and calling set_die_type.
e71ec853 25049 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25050 possible before fetching more types to complete the current type.
25051 * Make the type as complete as possible before fetching more types. */
1c379e20 25052
f792889a 25053static struct type *
57567375
TT
25054set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25055 bool skip_data_location)
1c379e20 25056{
976ca316 25057 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 25058 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 25059 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
25060 struct attribute *attr;
25061 struct dynamic_prop prop;
1c379e20 25062
b4ba55a1
JB
25063 /* For Ada types, make sure that the gnat-specific data is always
25064 initialized (if not already set). There are a few types where
25065 we should not be doing so, because the type-specific area is
25066 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25067 where the type-specific area is used to store the floatformat).
25068 But this is not a problem, because the gnat-specific information
25069 is actually not needed for these types. */
25070 if (need_gnat_info (cu)
78134374
SM
25071 && type->code () != TYPE_CODE_FUNC
25072 && type->code () != TYPE_CODE_FLT
25073 && type->code () != TYPE_CODE_METHODPTR
25074 && type->code () != TYPE_CODE_MEMBERPTR
25075 && type->code () != TYPE_CODE_METHOD
09584414 25076 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
25077 && !HAVE_GNAT_AUX_INFO (type))
25078 INIT_GNAT_SPECIFIC (type);
25079
3f2f83dd
KB
25080 /* Read DW_AT_allocated and set in type. */
25081 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 25082 if (attr != NULL)
3f2f83dd 25083 {
293e7e51 25084 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25085 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25086 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 25087 }
3f2f83dd
KB
25088
25089 /* Read DW_AT_associated and set in type. */
25090 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 25091 if (attr != NULL)
3f2f83dd 25092 {
293e7e51 25093 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25094 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25095 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 25096 }
3f2f83dd 25097
3cdcd0ce 25098 /* Read DW_AT_data_location and set in type. */
57567375
TT
25099 if (!skip_data_location)
25100 {
25101 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25102 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25103 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25104 }
3cdcd0ce 25105
976ca316
SM
25106 if (per_objfile->die_type_hash == NULL)
25107 per_objfile->die_type_hash
0335378b
TT
25108 = htab_up (htab_create_alloc (127,
25109 per_cu_offset_and_type_hash,
25110 per_cu_offset_and_type_eq,
25111 NULL, xcalloc, xfree));
1c379e20 25112
dee91e82 25113 ofs.per_cu = cu->per_cu;
9c541725 25114 ofs.sect_off = die->sect_off;
1c379e20 25115 ofs.type = type;
dee91e82 25116 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25117 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25118 if (*slot)
b98664d3 25119 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25120 sect_offset_str (die->sect_off));
8d749320
SM
25121 *slot = XOBNEW (&objfile->objfile_obstack,
25122 struct dwarf2_per_cu_offset_and_type);
1c379e20 25123 **slot = ofs;
f792889a 25124 return type;
1c379e20
DJ
25125}
25126
9c541725 25127/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25128 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25129
25130static struct type *
9c541725 25131get_die_type_at_offset (sect_offset sect_off,
aa66c379 25132 dwarf2_per_cu_data *per_cu,
976ca316 25133 dwarf2_per_objfile *per_objfile)
1c379e20 25134{
dee91e82 25135 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25136
976ca316 25137 if (per_objfile->die_type_hash == NULL)
f792889a 25138 return NULL;
1c379e20 25139
dee91e82 25140 ofs.per_cu = per_cu;
9c541725 25141 ofs.sect_off = sect_off;
9a3c8263 25142 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25143 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25144 if (slot)
25145 return slot->type;
25146 else
25147 return NULL;
25148}
25149
02142a6c 25150/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25151 or return NULL if DIE does not have a saved type. */
25152
25153static struct type *
25154get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25155{
aa66c379 25156 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25157}
25158
10b3939b
DJ
25159/* Add a dependence relationship from CU to REF_PER_CU. */
25160
25161static void
25162dwarf2_add_dependence (struct dwarf2_cu *cu,
25163 struct dwarf2_per_cu_data *ref_per_cu)
25164{
25165 void **slot;
25166
25167 if (cu->dependencies == NULL)
25168 cu->dependencies
25169 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25170 NULL, &cu->comp_unit_obstack,
25171 hashtab_obstack_allocate,
25172 dummy_obstack_deallocate);
25173
25174 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25175 if (*slot == NULL)
25176 *slot = ref_per_cu;
25177}
1c379e20 25178
f504f079
DE
25179/* Subroutine of dwarf2_mark to pass to htab_traverse.
25180 Set the mark field in every compilation unit in the
7188ed02
SM
25181 cache that we must keep because we are keeping CU.
25182
25183 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25184
10b3939b
DJ
25185static int
25186dwarf2_mark_helper (void **slot, void *data)
25187{
7188ed02
SM
25188 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25189 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25190 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25191
25192 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25193 reading of the chain. As such dependencies remain valid it is not much
25194 useful to track and undo them during QUIT cleanups. */
7188ed02 25195 if (cu == nullptr)
d07ed419
JK
25196 return 1;
25197
7188ed02 25198 if (cu->mark)
10b3939b 25199 return 1;
10b3939b 25200
7188ed02
SM
25201 cu->mark = true;
25202
25203 if (cu->dependencies != nullptr)
25204 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25205
25206 return 1;
25207}
25208
f504f079
DE
25209/* Set the mark field in CU and in every other compilation unit in the
25210 cache that we must keep because we are keeping CU. */
25211
ae038cb0
DJ
25212static void
25213dwarf2_mark (struct dwarf2_cu *cu)
25214{
25215 if (cu->mark)
25216 return;
7188ed02 25217
9068261f 25218 cu->mark = true;
ae038cb0 25219
7188ed02
SM
25220 if (cu->dependencies != nullptr)
25221 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25222}
25223
72bf9492
DJ
25224/* Trivial hash function for partial_die_info: the hash value of a DIE
25225 is its offset in .debug_info for this objfile. */
25226
25227static hashval_t
25228partial_die_hash (const void *item)
25229{
9a3c8263
SM
25230 const struct partial_die_info *part_die
25231 = (const struct partial_die_info *) item;
9a619af0 25232
9c541725 25233 return to_underlying (part_die->sect_off);
72bf9492
DJ
25234}
25235
25236/* Trivial comparison function for partial_die_info structures: two DIEs
25237 are equal if they have the same offset. */
25238
25239static int
25240partial_die_eq (const void *item_lhs, const void *item_rhs)
25241{
9a3c8263
SM
25242 const struct partial_die_info *part_die_lhs
25243 = (const struct partial_die_info *) item_lhs;
25244 const struct partial_die_info *part_die_rhs
25245 = (const struct partial_die_info *) item_rhs;
9a619af0 25246
9c541725 25247 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25248}
25249
3c3bb058
AB
25250struct cmd_list_element *set_dwarf_cmdlist;
25251struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25252
9291a0cd 25253static void
cd4fb1b2
SM
25254show_check_physname (struct ui_file *file, int from_tty,
25255 struct cmd_list_element *c, const char *value)
9291a0cd 25256{
cd4fb1b2
SM
25257 fprintf_filtered (file,
25258 _("Whether to check \"physname\" is %s.\n"),
25259 value);
9291a0cd
TT
25260}
25261
6c265988 25262void _initialize_dwarf2_read ();
cd4fb1b2 25263void
6c265988 25264_initialize_dwarf2_read ()
9291a0cd 25265{
0743fc83 25266 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25267Set DWARF specific variables.\n\
590042fc 25268Configure DWARF variables such as the cache size."),
0743fc83
TT
25269 &set_dwarf_cmdlist, "maintenance set dwarf ",
25270 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25271
0743fc83 25272 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25273Show DWARF specific variables.\n\
25274Show DWARF variables such as the cache size."),
0743fc83
TT
25275 &show_dwarf_cmdlist, "maintenance show dwarf ",
25276 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25277
cd4fb1b2
SM
25278 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25279 &dwarf_max_cache_age, _("\
25280Set the upper bound on the age of cached DWARF compilation units."), _("\
25281Show the upper bound on the age of cached DWARF compilation units."), _("\
25282A higher limit means that cached compilation units will be stored\n\
25283in memory longer, and more total memory will be used. Zero disables\n\
25284caching, which can slow down startup."),
25285 NULL,
25286 show_dwarf_max_cache_age,
25287 &set_dwarf_cmdlist,
25288 &show_dwarf_cmdlist);
156942c7 25289
cd4fb1b2
SM
25290 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25291Set debugging of the DWARF reader."), _("\
25292Show debugging of the DWARF reader."), _("\
25293When enabled (non-zero), debugging messages are printed during DWARF\n\
25294reading and symtab expansion. A value of 1 (one) provides basic\n\
25295information. A value greater than 1 provides more verbose information."),
25296 NULL,
25297 NULL,
25298 &setdebuglist, &showdebuglist);
9291a0cd 25299
cd4fb1b2
SM
25300 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25301Set debugging of the DWARF DIE reader."), _("\
25302Show debugging of the DWARF DIE reader."), _("\
25303When enabled (non-zero), DIEs are dumped after they are read in.\n\
25304The value is the maximum depth to print."),
25305 NULL,
25306 NULL,
25307 &setdebuglist, &showdebuglist);
9291a0cd 25308
cd4fb1b2
SM
25309 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25310Set debugging of the dwarf line reader."), _("\
25311Show debugging of the dwarf line reader."), _("\
25312When enabled (non-zero), line number entries are dumped as they are read in.\n\
25313A value of 1 (one) provides basic information.\n\
25314A value greater than 1 provides more verbose information."),
25315 NULL,
25316 NULL,
25317 &setdebuglist, &showdebuglist);
437afbb8 25318
cd4fb1b2
SM
25319 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25320Set cross-checking of \"physname\" code against demangler."), _("\
25321Show cross-checking of \"physname\" code against demangler."), _("\
25322When enabled, GDB's internal \"physname\" code is checked against\n\
25323the demangler."),
25324 NULL, show_check_physname,
25325 &setdebuglist, &showdebuglist);
900e11f9 25326
e615022a
DE
25327 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25328 no_class, &use_deprecated_index_sections, _("\
25329Set whether to use deprecated gdb_index sections."), _("\
25330Show whether to use deprecated gdb_index sections."), _("\
25331When enabled, deprecated .gdb_index sections are used anyway.\n\
25332Normally they are ignored either because of a missing feature or\n\
25333performance issue.\n\
25334Warning: This option must be enabled before gdb reads the file."),
25335 NULL,
25336 NULL,
25337 &setlist, &showlist);
25338
f1e6e072
TT
25339 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25340 &dwarf2_locexpr_funcs);
25341 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25342 &dwarf2_loclist_funcs);
25343
25344 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25345 &dwarf2_block_frame_base_locexpr_funcs);
25346 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25347 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25348
25349#if GDB_SELF_TEST
25350 selftests::register_test ("dw2_expand_symtabs_matching",
25351 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25352 selftests::register_test ("dwarf2_find_containing_comp_unit",
25353 selftests::find_containing_comp_unit::run_test);
c62446b1 25354#endif
6502dd73 25355}
This page took 5.983281 seconds and 4 git commands to generate.