gdb: remove TYPE_FIELD_TYPE macro
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
17ee85fc
TT
108/* This is used to store the data that is always per objfile. */
109static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
110
111/* These are used to store the dwarf2_per_bfd objects.
112
113 objfiles having the same BFD, which doesn't require relocations, are going to
114 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
115
116 Other objfiles are not going to share a dwarf2_per_bfd with any other
117 objfiles, so they'll have their own version kept in the _objfile_data_key
118 version. */
119static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
120static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 121
f1e6e072
TT
122/* The "aclass" indices for various kinds of computed DWARF symbols. */
123
124static int dwarf2_locexpr_index;
125static int dwarf2_loclist_index;
126static int dwarf2_locexpr_block_index;
127static int dwarf2_loclist_block_index;
128
41144253 129/* Size of .debug_loclists section header for 32-bit DWARF format. */
130#define LOCLIST_HEADER_SIZE32 12
131
132/* Size of .debug_loclists section header for 64-bit DWARF format. */
133#define LOCLIST_HEADER_SIZE64 20
134
3f563c84
PA
135/* An index into a (C++) symbol name component in a symbol name as
136 recorded in the mapped_index's symbol table. For each C++ symbol
137 in the symbol table, we record one entry for the start of each
138 component in the symbol in a table of name components, and then
139 sort the table, in order to be able to binary search symbol names,
140 ignoring leading namespaces, both completion and regular look up.
141 For example, for symbol "A::B::C", we'll have an entry that points
142 to "A::B::C", another that points to "B::C", and another for "C".
143 Note that function symbols in GDB index have no parameter
144 information, just the function/method names. You can convert a
145 name_component to a "const char *" using the
146 'mapped_index::symbol_name_at(offset_type)' method. */
147
148struct name_component
149{
150 /* Offset in the symbol name where the component starts. Stored as
151 a (32-bit) offset instead of a pointer to save memory and improve
152 locality on 64-bit architectures. */
153 offset_type name_offset;
154
155 /* The symbol's index in the symbol and constant pool tables of a
156 mapped_index. */
157 offset_type idx;
158};
159
44ed8f3e
PA
160/* Base class containing bits shared by both .gdb_index and
161 .debug_name indexes. */
162
163struct mapped_index_base
164{
22ca247e
TT
165 mapped_index_base () = default;
166 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
167
44ed8f3e
PA
168 /* The name_component table (a sorted vector). See name_component's
169 description above. */
170 std::vector<name_component> name_components;
171
172 /* How NAME_COMPONENTS is sorted. */
173 enum case_sensitivity name_components_casing;
174
175 /* Return the number of names in the symbol table. */
176 virtual size_t symbol_name_count () const = 0;
177
178 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
179 virtual const char *symbol_name_at
180 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
181
182 /* Return whether the name at IDX in the symbol table should be
183 ignored. */
184 virtual bool symbol_name_slot_invalid (offset_type idx) const
185 {
186 return false;
187 }
188
189 /* Build the symbol name component sorted vector, if we haven't
190 yet. */
fcf23d5b 191 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
192
193 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
194 possible matches for LN_NO_PARAMS in the name component
195 vector. */
196 std::pair<std::vector<name_component>::const_iterator,
197 std::vector<name_component>::const_iterator>
3b00ef10 198 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
199 enum language lang,
200 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
201
202 /* Prevent deleting/destroying via a base class pointer. */
203protected:
204 ~mapped_index_base() = default;
205};
206
9291a0cd
TT
207/* A description of the mapped index. The file format is described in
208 a comment by the code that writes the index. */
fc898b42 209struct mapped_index final : public mapped_index_base
9291a0cd 210{
f00a2de2
PA
211 /* A slot/bucket in the symbol table hash. */
212 struct symbol_table_slot
213 {
214 const offset_type name;
215 const offset_type vec;
216 };
217
559a7a62 218 /* Index data format version. */
3063847f 219 int version = 0;
559a7a62 220
f00a2de2
PA
221 /* The address table data. */
222 gdb::array_view<const gdb_byte> address_table;
b11b1f88 223
3876f04e 224 /* The symbol table, implemented as a hash table. */
f00a2de2 225 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 226
9291a0cd 227 /* A pointer to the constant pool. */
3063847f 228 const char *constant_pool = nullptr;
3f563c84 229
44ed8f3e
PA
230 bool symbol_name_slot_invalid (offset_type idx) const override
231 {
232 const auto &bucket = this->symbol_table[idx];
9ab08412 233 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 234 }
5c58de74 235
3f563c84
PA
236 /* Convenience method to get at the name of the symbol at IDX in the
237 symbol table. */
fcf23d5b
SM
238 const char *symbol_name_at
239 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
f00a2de2 240 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 241
44ed8f3e
PA
242 size_t symbol_name_count () const override
243 { return this->symbol_table.size (); }
9291a0cd
TT
244};
245
927aa2e7
JK
246/* A description of the mapped .debug_names.
247 Uninitialized map has CU_COUNT 0. */
fc898b42 248struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
249{
250 bfd_endian dwarf5_byte_order;
251 bool dwarf5_is_dwarf64;
252 bool augmentation_is_gdb;
253 uint8_t offset_size;
254 uint32_t cu_count = 0;
255 uint32_t tu_count, bucket_count, name_count;
256 const gdb_byte *cu_table_reordered, *tu_table_reordered;
257 const uint32_t *bucket_table_reordered, *hash_table_reordered;
258 const gdb_byte *name_table_string_offs_reordered;
259 const gdb_byte *name_table_entry_offs_reordered;
260 const gdb_byte *entry_pool;
261
262 struct index_val
263 {
264 ULONGEST dwarf_tag;
265 struct attr
266 {
267 /* Attribute name DW_IDX_*. */
268 ULONGEST dw_idx;
269
270 /* Attribute form DW_FORM_*. */
271 ULONGEST form;
272
273 /* Value if FORM is DW_FORM_implicit_const. */
274 LONGEST implicit_const;
275 };
276 std::vector<attr> attr_vec;
277 };
278
279 std::unordered_map<ULONGEST, index_val> abbrev_map;
280
fcf23d5b
SM
281 const char *namei_to_name
282 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
283
284 /* Implementation of the mapped_index_base virtual interface, for
285 the name_components cache. */
286
fcf23d5b
SM
287 const char *symbol_name_at
288 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
289 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
290
291 size_t symbol_name_count () const override
292 { return this->name_count; }
927aa2e7
JK
293};
294
cd4fb1b2 295/* See dwarf2read.h. */
ed2dc618 296
cd4fb1b2 297dwarf2_per_objfile *
ed2dc618
SM
298get_dwarf2_per_objfile (struct objfile *objfile)
299{
5bfd760d 300 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 301}
c906108c 302
251d32d9 303/* Default names of the debugging sections. */
c906108c 304
233a11ab
CS
305/* Note that if the debugging section has been compressed, it might
306 have a name like .zdebug_info. */
307
9cdd5dbd
DE
308static const struct dwarf2_debug_sections dwarf2_elf_names =
309{
251d32d9
TG
310 { ".debug_info", ".zdebug_info" },
311 { ".debug_abbrev", ".zdebug_abbrev" },
312 { ".debug_line", ".zdebug_line" },
313 { ".debug_loc", ".zdebug_loc" },
43988095 314 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 315 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 316 { ".debug_macro", ".zdebug_macro" },
251d32d9 317 { ".debug_str", ".zdebug_str" },
18a8505e 318 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 319 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 320 { ".debug_ranges", ".zdebug_ranges" },
43988095 321 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 322 { ".debug_types", ".zdebug_types" },
3019eac3 323 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
324 { ".debug_frame", ".zdebug_frame" },
325 { ".eh_frame", NULL },
24d3216f 326 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
327 { ".debug_names", ".zdebug_names" },
328 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 329 23
251d32d9 330};
c906108c 331
80626a55 332/* List of DWO/DWP sections. */
3019eac3 333
80626a55 334static const struct dwop_section_names
3019eac3
DE
335{
336 struct dwarf2_section_names abbrev_dwo;
337 struct dwarf2_section_names info_dwo;
338 struct dwarf2_section_names line_dwo;
339 struct dwarf2_section_names loc_dwo;
43988095 340 struct dwarf2_section_names loclists_dwo;
09262596
DE
341 struct dwarf2_section_names macinfo_dwo;
342 struct dwarf2_section_names macro_dwo;
3019eac3
DE
343 struct dwarf2_section_names str_dwo;
344 struct dwarf2_section_names str_offsets_dwo;
345 struct dwarf2_section_names types_dwo;
80626a55
DE
346 struct dwarf2_section_names cu_index;
347 struct dwarf2_section_names tu_index;
3019eac3 348}
80626a55 349dwop_section_names =
3019eac3
DE
350{
351 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
352 { ".debug_info.dwo", ".zdebug_info.dwo" },
353 { ".debug_line.dwo", ".zdebug_line.dwo" },
354 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 355 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
356 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
357 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
358 { ".debug_str.dwo", ".zdebug_str.dwo" },
359 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
360 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
361 { ".debug_cu_index", ".zdebug_cu_index" },
362 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
363};
364
c906108c
SS
365/* local data types */
366
41144253 367/* The location list section (.debug_loclists) begins with a header,
368 which contains the following information. */
369struct loclist_header
370{
371 /* A 4-byte or 12-byte length containing the length of the
372 set of entries for this compilation unit, not including the
373 length field itself. */
374 unsigned int length;
375
376 /* A 2-byte version identifier. */
377 short version;
378
379 /* A 1-byte unsigned integer containing the size in bytes of an address on
380 the target system. */
381 unsigned char addr_size;
382
383 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
384 on the target system. */
385 unsigned char segment_collector_size;
386
387 /* A 4-byte count of the number of offsets that follow the header. */
388 unsigned int offset_entry_count;
389};
390
3da10d80
KS
391/* Type used for delaying computation of method physnames.
392 See comments for compute_delayed_physnames. */
393struct delayed_method_info
394{
395 /* The type to which the method is attached, i.e., its parent class. */
396 struct type *type;
397
398 /* The index of the method in the type's function fieldlists. */
399 int fnfield_index;
400
401 /* The index of the method in the fieldlist. */
402 int index;
403
404 /* The name of the DIE. */
405 const char *name;
406
407 /* The DIE associated with this method. */
408 struct die_info *die;
409};
410
e7c27a73
DJ
411/* Internal state when decoding a particular compilation unit. */
412struct dwarf2_cu
413{
9e021579
SM
414 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
415 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
416
417 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
418
c24bdb02
KS
419 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
420 Create the set of symtabs used by this TU, or if this TU is sharing
421 symtabs with another TU and the symtabs have already been created
422 then restore those symtabs in the line header.
423 We don't need the pc/line-number mapping for type units. */
424 void setup_type_unit_groups (struct die_info *die);
425
426 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
427 buildsym_compunit constructor. */
428 struct compunit_symtab *start_symtab (const char *name,
429 const char *comp_dir,
430 CORE_ADDR low_pc);
431
432 /* Reset the builder. */
433 void reset_builder () { m_builder.reset (); }
434
293e7e51
SM
435 /* Return a type that is a generic pointer type, the size of which
436 matches the address size given in the compilation unit header for
437 this CU. */
438 struct type *addr_type () const;
439
440 /* Find an integer type the same size as the address size given in
441 the compilation unit header for this CU. UNSIGNED_P controls if
442 the integer is unsigned or not. */
443 struct type *addr_sized_int_type (bool unsigned_p) const;
444
d00adf39 445 /* The header of the compilation unit. */
fcd3b13d 446 struct comp_unit_head header {};
e142c38c 447
d00adf39 448 /* Base address of this compilation unit. */
2b24b6e4 449 gdb::optional<CORE_ADDR> base_address;
d00adf39 450
e142c38c 451 /* The language we are debugging. */
fcd3b13d
SM
452 enum language language = language_unknown;
453 const struct language_defn *language_defn = nullptr;
e142c38c 454
fcd3b13d 455 const char *producer = nullptr;
b0f35d58 456
c24bdb02 457private:
804d2729
TT
458 /* The symtab builder for this CU. This is only non-NULL when full
459 symbols are being read. */
c24bdb02 460 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 461
c24bdb02 462public:
e142c38c
DJ
463 /* The generic symbol table building routines have separate lists for
464 file scope symbols and all all other scopes (local scopes). So
465 we need to select the right one to pass to add_symbol_to_list().
466 We do it by keeping a pointer to the correct list in list_in_scope.
467
468 FIXME: The original dwarf code just treated the file scope as the
469 first local scope, and all other local scopes as nested local
470 scopes, and worked fine. Check to see if we really need to
471 distinguish these in buildsym.c. */
fcd3b13d 472 struct pending **list_in_scope = nullptr;
e142c38c 473
b64f50a1
JK
474 /* Hash table holding all the loaded partial DIEs
475 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 476 htab_t partial_dies = nullptr;
72bf9492
DJ
477
478 /* Storage for things with the same lifetime as this read-in compilation
479 unit, including partial DIEs. */
fcd3b13d 480 auto_obstack comp_unit_obstack;
72bf9492 481
69d751e3 482 /* Backlink to our per_cu entry. */
ae038cb0
DJ
483 struct dwarf2_per_cu_data *per_cu;
484
9e021579 485 /* The dwarf2_per_objfile that owns this. */
976ca316 486 dwarf2_per_objfile *per_objfile;
9e021579 487
ae038cb0 488 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 489 int last_used = 0;
ae038cb0 490
b64f50a1
JK
491 /* A hash table of DIE cu_offset for following references with
492 die_info->offset.sect_off as hash. */
fcd3b13d 493 htab_t die_hash = nullptr;
10b3939b
DJ
494
495 /* Full DIEs if read in. */
fcd3b13d 496 struct die_info *dies = nullptr;
10b3939b
DJ
497
498 /* A set of pointers to dwarf2_per_cu_data objects for compilation
499 units referenced by this one. Only set during full symbol processing;
500 partial symbol tables do not have dependencies. */
fcd3b13d 501 htab_t dependencies = nullptr;
10b3939b 502
cb1df416 503 /* Header data from the line table, during full symbol processing. */
fcd3b13d 504 struct line_header *line_header = nullptr;
4c8aa72d 505 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 506 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
507 this is the DW_TAG_compile_unit die for this CU. We'll hold on
508 to the line header as long as this DIE is being processed. See
509 process_die_scope. */
fcd3b13d 510 die_info *line_header_die_owner = nullptr;
cb1df416 511
3da10d80
KS
512 /* A list of methods which need to have physnames computed
513 after all type information has been read. */
c89b44cd 514 std::vector<delayed_method_info> method_list;
3da10d80 515
96408a79 516 /* To be copied to symtab->call_site_htab. */
fcd3b13d 517 htab_t call_site_htab = nullptr;
96408a79 518
034e5797
DE
519 /* Non-NULL if this CU came from a DWO file.
520 There is an invariant here that is important to remember:
521 Except for attributes copied from the top level DIE in the "main"
522 (or "stub") file in preparation for reading the DWO file
18a8505e 523 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
524 Either there isn't a DWO file (in which case this is NULL and the point
525 is moot), or there is and either we're not going to read it (in which
526 case this is NULL) or there is and we are reading it (in which case this
527 is non-NULL). */
fcd3b13d 528 struct dwo_unit *dwo_unit = nullptr;
3019eac3 529
18a8505e 530 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 531 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 532 gdb::optional<ULONGEST> addr_base;
3019eac3 533
18a8505e 534 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 535 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 536 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
537 be used without needing to know whether DWO files are in use or not.
538 N.B. This does not apply to DW_AT_ranges appearing in
539 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
540 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 541 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 542 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 543 ULONGEST ranges_base = 0;
2e3cf129 544
41144253 545 /* The DW_AT_loclists_base attribute if present. */
546 ULONGEST loclist_base = 0;
547
c9317f21
TT
548 /* When reading debug info generated by older versions of rustc, we
549 have to rewrite some union types to be struct types with a
550 variant part. This rewriting must be done after the CU is fully
551 read in, because otherwise at the point of rewriting some struct
552 type might not have been fully processed. So, we keep a list of
553 all such types here and process them after expansion. */
554 std::vector<struct type *> rust_unions;
555
18a8505e
AT
556 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
557 files, the value is implicitly zero. For DWARF 5 version DWO files, the
558 value is often implicit and is the size of the header of
559 .debug_str_offsets section (8 or 4, depending on the address size). */
560 gdb::optional<ULONGEST> str_offsets_base;
561
ae038cb0 562 /* Mark used when releasing cached dies. */
9068261f 563 bool mark : 1;
ae038cb0 564
8be455d7
JK
565 /* This CU references .debug_loc. See the symtab->locations_valid field.
566 This test is imperfect as there may exist optimized debug code not using
567 any location list and still facing inlining issues if handled as
568 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 569 bool has_loclist : 1;
ba919b58 570
9068261f 571 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
572 if all the producer_is_* fields are valid. This information is cached
573 because profiling CU expansion showed excessive time spent in
574 producer_is_gxx_lt_4_6. */
9068261f
AB
575 bool checked_producer : 1;
576 bool producer_is_gxx_lt_4_6 : 1;
577 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 578 bool producer_is_icc : 1;
9068261f 579 bool producer_is_icc_lt_14 : 1;
c258c396 580 bool producer_is_codewarrior : 1;
4d4ec4e5 581
9068261f 582 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
583 debugging info for C++ namespaces. GCC 3.3.x did not produce
584 this information, but later versions do. */
585
9068261f 586 bool processing_has_namespace_info : 1;
d590ff25
YQ
587
588 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
589
590 /* If this CU was inherited by another CU (via specification,
591 abstract_origin, etc), this is the ancestor CU. */
592 dwarf2_cu *ancestor;
593
594 /* Get the buildsym_compunit for this CU. */
595 buildsym_compunit *get_builder ()
596 {
597 /* If this CU has a builder associated with it, use that. */
598 if (m_builder != nullptr)
599 return m_builder.get ();
600
601 /* Otherwise, search ancestors for a valid builder. */
602 if (ancestor != nullptr)
603 return ancestor->get_builder ();
604
605 return nullptr;
606 }
e7c27a73
DJ
607};
608
094b34ac
DE
609/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
610 This includes type_unit_group and quick_file_names. */
611
612struct stmt_list_hash
613{
614 /* The DWO unit this table is from or NULL if there is none. */
615 struct dwo_unit *dwo_unit;
616
617 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 618 sect_offset line_sect_off;
094b34ac
DE
619};
620
5989a64e 621/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
622 an object of this type. This contains elements of type unit groups
623 that can be shared across objfiles. The non-shareable parts are in
624 type_unit_group_unshareable. */
f4dc4d17
DE
625
626struct type_unit_group
627{
0186c6a7 628 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
629 To simplify things we create an artificial CU that "includes" all the
630 type units using this stmt_list so that the rest of the code still has
197400e8 631 a "per_cu" handle on the symtab. */
094b34ac
DE
632 struct dwarf2_per_cu_data per_cu;
633
0186c6a7
DE
634 /* The TUs that share this DW_AT_stmt_list entry.
635 This is added to while parsing type units to build partial symtabs,
636 and is deleted afterwards and not used again. */
a8b3b8e9 637 std::vector<signatured_type *> *tus;
f4dc4d17 638
094b34ac
DE
639 /* The data used to construct the hash key. */
640 struct stmt_list_hash hash;
f4dc4d17
DE
641};
642
73869dc2 643/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
644
645struct dwo_sections
646{
647 struct dwarf2_section_info abbrev;
3019eac3
DE
648 struct dwarf2_section_info line;
649 struct dwarf2_section_info loc;
43988095 650 struct dwarf2_section_info loclists;
09262596
DE
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
3019eac3
DE
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
80626a55
DE
655 /* In the case of a virtual DWO file, these two are unused. */
656 struct dwarf2_section_info info;
fd5866f6 657 std::vector<dwarf2_section_info> types;
3019eac3
DE
658};
659
c88ee1f0 660/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
661
662struct dwo_unit
663{
664 /* Backlink to the containing struct dwo_file. */
665 struct dwo_file *dwo_file;
666
667 /* The "id" that distinguishes this CU/TU.
668 .debug_info calls this "dwo_id", .debug_types calls this "signature".
669 Since signatures came first, we stick with it for consistency. */
670 ULONGEST signature;
671
672 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 673 struct dwarf2_section_info *section;
3019eac3 674
9c541725
PA
675 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
676 sect_offset sect_off;
3019eac3
DE
677 unsigned int length;
678
679 /* For types, offset in the type's DIE of the type defined by this TU. */
680 cu_offset type_offset_in_tu;
681};
682
73869dc2
DE
683/* include/dwarf2.h defines the DWP section codes.
684 It defines a max value but it doesn't define a min value, which we
685 use for error checking, so provide one. */
686
687enum dwp_v2_section_ids
688{
689 DW_SECT_MIN = 1
690};
691
80626a55 692/* Data for one DWO file.
57d63ce2
DE
693
694 This includes virtual DWO files (a virtual DWO file is a DWO file as it
695 appears in a DWP file). DWP files don't really have DWO files per se -
696 comdat folding of types "loses" the DWO file they came from, and from
697 a high level view DWP files appear to contain a mass of random types.
698 However, to maintain consistency with the non-DWP case we pretend DWP
699 files contain virtual DWO files, and we assign each TU with one virtual
700 DWO file (generally based on the line and abbrev section offsets -
701 a heuristic that seems to work in practice). */
3019eac3
DE
702
703struct dwo_file
704{
51ac9db5
SM
705 dwo_file () = default;
706 DISABLE_COPY_AND_ASSIGN (dwo_file);
707
18a8505e 708 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
51ac9db5 712 const char *dwo_name = nullptr;
0ac5b59e
DE
713
714 /* The DW_AT_comp_dir attribute. */
51ac9db5 715 const char *comp_dir = nullptr;
3019eac3 716
80626a55
DE
717 /* The bfd, when the file is open. Otherwise this is NULL.
718 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 719 gdb_bfd_ref_ptr dbfd;
3019eac3 720
73869dc2
DE
721 /* The sections that make up this DWO file.
722 Remember that for virtual DWO files in DWP V2, these are virtual
723 sections (for lack of a better name). */
51ac9db5 724 struct dwo_sections sections {};
3019eac3 725
33c5cd75
DB
726 /* The CUs in the file.
727 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
728 an extension to handle LLVM's Link Time Optimization output (where
729 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 730 htab_up cus;
3019eac3
DE
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
b0b6a987 734 htab_up tus;
3019eac3
DE
735};
736
80626a55
DE
737/* These sections are what may appear in a DWP file. */
738
739struct dwp_sections
740{
73869dc2 741 /* These are used by both DWP version 1 and 2. */
80626a55
DE
742 struct dwarf2_section_info str;
743 struct dwarf2_section_info cu_index;
744 struct dwarf2_section_info tu_index;
73869dc2
DE
745
746 /* These are only used by DWP version 2 files.
747 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
748 sections are referenced by section number, and are not recorded here.
749 In DWP version 2 there is at most one copy of all these sections, each
750 section being (effectively) comprised of the concatenation of all of the
751 individual sections that exist in the version 1 format.
752 To keep the code simple we treat each of these concatenated pieces as a
753 section itself (a virtual section?). */
754 struct dwarf2_section_info abbrev;
755 struct dwarf2_section_info info;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str_offsets;
761 struct dwarf2_section_info types;
80626a55
DE
762};
763
73869dc2
DE
764/* These sections are what may appear in a virtual DWO file in DWP version 1.
765 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 766
73869dc2 767struct virtual_v1_dwo_sections
80626a55
DE
768{
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 776 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
777 struct dwarf2_section_info info_or_types;
778};
779
73869dc2
DE
780/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
781 In version 2, the sections of the DWO files are concatenated together
782 and stored in one section of that name. Thus each ELF section contains
783 several "virtual" sections. */
784
785struct virtual_v2_dwo_sections
786{
787 bfd_size_type abbrev_offset;
788 bfd_size_type abbrev_size;
789
790 bfd_size_type line_offset;
791 bfd_size_type line_size;
792
793 bfd_size_type loc_offset;
794 bfd_size_type loc_size;
795
796 bfd_size_type macinfo_offset;
797 bfd_size_type macinfo_size;
798
799 bfd_size_type macro_offset;
800 bfd_size_type macro_size;
801
802 bfd_size_type str_offsets_offset;
803 bfd_size_type str_offsets_size;
804
805 /* Each DWP hash table entry records one CU or one TU.
806 That is recorded here, and copied to dwo_unit.section. */
807 bfd_size_type info_or_types_offset;
808 bfd_size_type info_or_types_size;
809};
810
80626a55
DE
811/* Contents of DWP hash tables. */
812
813struct dwp_hash_table
814{
73869dc2 815 uint32_t version, nr_columns;
80626a55 816 uint32_t nr_units, nr_slots;
73869dc2
DE
817 const gdb_byte *hash_table, *unit_table;
818 union
819 {
820 struct
821 {
822 const gdb_byte *indices;
823 } v1;
824 struct
825 {
826 /* This is indexed by column number and gives the id of the section
827 in that column. */
828#define MAX_NR_V2_DWO_SECTIONS \
829 (1 /* .debug_info or .debug_types */ \
830 + 1 /* .debug_abbrev */ \
831 + 1 /* .debug_line */ \
832 + 1 /* .debug_loc */ \
833 + 1 /* .debug_str_offsets */ \
834 + 1 /* .debug_macro or .debug_macinfo */)
835 int section_ids[MAX_NR_V2_DWO_SECTIONS];
836 const gdb_byte *offsets;
837 const gdb_byte *sizes;
838 } v2;
839 } section_pool;
80626a55
DE
840};
841
842/* Data for one DWP file. */
843
844struct dwp_file
845{
400174b1
TT
846 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
847 : name (name_),
848 dbfd (std::move (abfd))
849 {
850 }
851
80626a55
DE
852 /* Name of the file. */
853 const char *name;
854
73869dc2 855 /* File format version. */
400174b1 856 int version = 0;
73869dc2 857
93417882 858 /* The bfd. */
400174b1 859 gdb_bfd_ref_ptr dbfd;
80626a55
DE
860
861 /* Section info for this file. */
400174b1 862 struct dwp_sections sections {};
80626a55 863
57d63ce2 864 /* Table of CUs in the file. */
400174b1 865 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
866
867 /* Table of TUs in the file. */
400174b1 868 const struct dwp_hash_table *tus = nullptr;
80626a55 869
19ac8c2e 870 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
871 htab_up loaded_cus;
872 htab_up loaded_tus;
80626a55 873
73869dc2
DE
874 /* Table to map ELF section numbers to their sections.
875 This is only needed for the DWP V1 file format. */
400174b1
TT
876 unsigned int num_sections = 0;
877 asection **elf_sections = nullptr;
80626a55
DE
878};
879
0963b4bd
MS
880/* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
dee91e82 883 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
884
885struct die_reader_specs
886{
a32a8923 887 /* The bfd of die_section. */
93311388
DE
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
80626a55 893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
894 struct dwo_file *dwo_file;
895
dee91e82 896 /* The section the die comes from.
3019eac3 897 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
d521ce57 901 const gdb_byte *buffer;
f664829e
DE
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
a2ce51a0 905
685af9cd
TT
906 /* The abbreviation table to use when reading the DIEs. */
907 struct abbrev_table *abbrev_table;
93311388
DE
908};
909
c0ab21c2
TT
910/* A subclass of die_reader_specs that holds storage and has complex
911 constructor and destructor behavior. */
912
913class cutu_reader : public die_reader_specs
914{
915public:
916
ab432490
SM
917 cutu_reader (dwarf2_per_cu_data *this_cu,
918 dwarf2_per_objfile *per_objfile,
c0ab21c2 919 struct abbrev_table *abbrev_table,
2e671100 920 dwarf2_cu *existing_cu,
c0ab21c2
TT
921 bool skip_partial);
922
923 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 924 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
925 struct dwarf2_cu *parent_cu = nullptr,
926 struct dwo_file *dwo_file = nullptr);
927
c0ab21c2
TT
928 DISABLE_COPY_AND_ASSIGN (cutu_reader);
929
930 const gdb_byte *info_ptr = nullptr;
931 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
932 bool dummy_p = false;
933
6751ebae
TT
934 /* Release the new CU, putting it on the chain. This cannot be done
935 for dummy CUs. */
936 void keep ();
937
c0ab21c2 938private:
9e021579
SM
939 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
940 dwarf2_per_objfile *per_objfile,
2e671100 941 dwarf2_cu *existing_cu);
c0ab21c2
TT
942
943 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951};
dee91e82 952
c906108c 953/* When we construct a partial symbol table entry we only
0963b4bd 954 need this much information. */
6f06d47b 955struct partial_die_info : public allocate_on_obstack
c906108c 956 {
6f06d47b
YQ
957 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
958
959 /* Disable assign but still keep copy ctor, which is needed
960 load_partial_dies. */
961 partial_die_info& operator=(const partial_die_info& rhs) = delete;
962
52356b79
YQ
963 /* Adjust the partial die before generating a symbol for it. This
964 function may set the is_external flag or change the DIE's
965 name. */
966 void fixup (struct dwarf2_cu *cu);
967
48fbe735
YQ
968 /* Read a minimal amount of information into the minimal die
969 structure. */
970 const gdb_byte *read (const struct die_reader_specs *reader,
971 const struct abbrev_info &abbrev,
972 const gdb_byte *info_ptr);
973
7d00ffec
TT
974 /* Compute the name of this partial DIE. This memoizes the
975 result, so it is safe to call multiple times. */
976 const char *name (dwarf2_cu *cu);
977
72bf9492 978 /* Offset of this DIE. */
6f06d47b 979 const sect_offset sect_off;
72bf9492
DJ
980
981 /* DWARF-2 tag for this DIE. */
6f06d47b 982 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 983
72bf9492 984 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
985 const unsigned int has_children : 1;
986
72bf9492
DJ
987 unsigned int is_external : 1;
988 unsigned int is_declaration : 1;
989 unsigned int has_type : 1;
990 unsigned int has_specification : 1;
991 unsigned int has_pc_info : 1;
481860b3 992 unsigned int may_be_inlined : 1;
72bf9492 993
0c1b455e
TT
994 /* This DIE has been marked DW_AT_main_subprogram. */
995 unsigned int main_subprogram : 1;
996
72bf9492
DJ
997 /* Flag set if the SCOPE field of this structure has been
998 computed. */
999 unsigned int scope_set : 1;
1000
fa4028e9
JB
1001 /* Flag set if the DIE has a byte_size attribute. */
1002 unsigned int has_byte_size : 1;
1003
ff908ebf
AW
1004 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1005 unsigned int has_const_value : 1;
1006
98bfdba5
PA
1007 /* Flag set if any of the DIE's children are template arguments. */
1008 unsigned int has_template_arguments : 1;
1009
52356b79 1010 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1011 unsigned int fixup_called : 1;
1012
36586728
TT
1013 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1014 unsigned int is_dwz : 1;
1015
1016 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1017 unsigned int spec_is_dwz : 1;
1018
7d00ffec
TT
1019 unsigned int canonical_name : 1;
1020
72bf9492 1021 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1022 sometimes a default name for unnamed DIEs. */
7d00ffec 1023 const char *raw_name = nullptr;
72bf9492 1024
abc72ce4 1025 /* The linkage name, if present. */
6f06d47b 1026 const char *linkage_name = nullptr;
abc72ce4 1027
72bf9492
DJ
1028 /* The scope to prepend to our children. This is generally
1029 allocated on the comp_unit_obstack, so will disappear
1030 when this compilation unit leaves the cache. */
6f06d47b 1031 const char *scope = nullptr;
72bf9492 1032
95554aad
TT
1033 /* Some data associated with the partial DIE. The tag determines
1034 which field is live. */
1035 union
1036 {
1037 /* The location description associated with this DIE, if any. */
1038 struct dwarf_block *locdesc;
1039 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1040 sect_offset sect_off;
6f06d47b 1041 } d {};
72bf9492
DJ
1042
1043 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1044 CORE_ADDR lowpc = 0;
1045 CORE_ADDR highpc = 0;
72bf9492 1046
93311388 1047 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1048 DW_AT_sibling, if any. */
48fbe735
YQ
1049 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1050 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1051 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1052
1053 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1054 DW_AT_specification (or DW_AT_abstract_origin or
1055 DW_AT_extension). */
6f06d47b 1056 sect_offset spec_offset {};
72bf9492
DJ
1057
1058 /* Pointers to this DIE's parent, first child, and next sibling,
1059 if any. */
6f06d47b
YQ
1060 struct partial_die_info *die_parent = nullptr;
1061 struct partial_die_info *die_child = nullptr;
1062 struct partial_die_info *die_sibling = nullptr;
1063
1064 friend struct partial_die_info *
1065 dwarf2_cu::find_partial_die (sect_offset sect_off);
1066
1067 private:
1068 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1069 partial_die_info (sect_offset sect_off)
1070 : partial_die_info (sect_off, DW_TAG_padding, 0)
1071 {
1072 }
1073
1074 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1075 int has_children_)
1076 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1077 {
1078 is_external = 0;
1079 is_declaration = 0;
1080 has_type = 0;
1081 has_specification = 0;
1082 has_pc_info = 0;
1083 may_be_inlined = 0;
1084 main_subprogram = 0;
1085 scope_set = 0;
1086 has_byte_size = 0;
1087 has_const_value = 0;
1088 has_template_arguments = 0;
1089 fixup_called = 0;
1090 is_dwz = 0;
1091 spec_is_dwz = 0;
7d00ffec 1092 canonical_name = 0;
6f06d47b 1093 }
c906108c
SS
1094 };
1095
c906108c
SS
1096/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1097 but this would require a corresponding change in unpack_field_as_long
1098 and friends. */
1099static int bits_per_byte = 8;
1100
9c6a1327
TT
1101struct variant_part_builder;
1102
1103/* When reading a variant, we track a bit more information about the
1104 field, and store it in an object of this type. */
2ddeaf8a
TT
1105
1106struct variant_field
1107{
9c6a1327
TT
1108 int first_field = -1;
1109 int last_field = -1;
1110
1111 /* A variant can contain other variant parts. */
1112 std::vector<variant_part_builder> variant_parts;
1113
2ddeaf8a
TT
1114 /* If we see a DW_TAG_variant, then this will be set if this is the
1115 default branch. */
9c6a1327
TT
1116 bool default_branch = false;
1117 /* If we see a DW_AT_discr_value, then this will be the discriminant
1118 value. */
1119 ULONGEST discriminant_value = 0;
1120 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1121 data. */
1122 struct dwarf_block *discr_list_data = nullptr;
1123};
1124
1125/* This represents a DW_TAG_variant_part. */
1126
1127struct variant_part_builder
1128{
1129 /* The offset of the discriminant field. */
1130 sect_offset discriminant_offset {};
1131
1132 /* Variants that are direct children of this variant part. */
1133 std::vector<variant_field> variants;
1134
1135 /* True if we're currently reading a variant. */
1136 bool processing_variant = false;
2ddeaf8a
TT
1137};
1138
52059ffd
TT
1139struct nextfield
1140{
be2daae6
TT
1141 int accessibility = 0;
1142 int virtuality = 0;
9c6a1327
TT
1143 /* Variant parts need to find the discriminant, which is a DIE
1144 reference. We track the section offset of each field to make
1145 this link. */
1146 sect_offset offset;
be2daae6 1147 struct field field {};
52059ffd
TT
1148};
1149
1150struct fnfieldlist
1151{
be2daae6
TT
1152 const char *name = nullptr;
1153 std::vector<struct fn_field> fnfields;
52059ffd
TT
1154};
1155
c906108c
SS
1156/* The routines that read and process dies for a C struct or C++ class
1157 pass lists of data member fields and lists of member function fields
1158 in an instance of a field_info structure, as defined below. */
1159struct field_info
c5aa993b 1160 {
0963b4bd 1161 /* List of data member and baseclasses fields. */
be2daae6
TT
1162 std::vector<struct nextfield> fields;
1163 std::vector<struct nextfield> baseclasses;
c906108c 1164
85102364 1165 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1166 int non_public_fields = 0;
c906108c 1167
c5aa993b
JM
1168 /* Member function fieldlist array, contains name of possibly overloaded
1169 member function, number of overloaded member functions and a pointer
1170 to the head of the member function field chain. */
be2daae6 1171 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1172
1173 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1174 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1175 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1176
1177 /* Nested types defined by this class and the number of elements in this
1178 list. */
be2daae6 1179 std::vector<struct decl_field> nested_types_list;
317f7127 1180
9c6a1327
TT
1181 /* If non-null, this is the variant part we are currently
1182 reading. */
1183 variant_part_builder *current_variant_part = nullptr;
1184 /* This holds all the top-level variant parts attached to the type
1185 we're reading. */
1186 std::vector<variant_part_builder> variant_parts;
1187
317f7127
TT
1188 /* Return the total number of fields (including baseclasses). */
1189 int nfields () const
1190 {
1191 return fields.size () + baseclasses.size ();
1192 }
c5aa993b 1193 };
c906108c 1194
ae038cb0
DJ
1195/* Loaded secondary compilation units are kept in memory until they
1196 have not been referenced for the processing of this many
1197 compilation units. Set this to zero to disable caching. Cache
1198 sizes of up to at least twenty will improve startup time for
1199 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1200static int dwarf_max_cache_age = 5;
920d2a44 1201static void
b4f54984
DE
1202show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1203 struct cmd_list_element *c, const char *value)
920d2a44 1204{
3e43a32a 1205 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1206 "DWARF compilation units is %s.\n"),
920d2a44
AC
1207 value);
1208}
4390d890 1209\f
c906108c
SS
1210/* local function prototypes */
1211
918dd910
JK
1212static void dwarf2_find_base_address (struct die_info *die,
1213 struct dwarf2_cu *cu);
1214
891813be 1215static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1216 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1217 const char *name);
0018ea6f 1218
f1902523
JK
1219static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1220 const gdb_byte *info_ptr,
3e225074 1221 struct die_info *type_unit_die);
f1902523 1222
976ca316 1223static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1224
72bf9492
DJ
1225static void scan_partial_symbols (struct partial_die_info *,
1226 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1227 int, struct dwarf2_cu *);
c906108c 1228
72bf9492
DJ
1229static void add_partial_symbol (struct partial_die_info *,
1230 struct dwarf2_cu *);
63d06c5c 1231
72bf9492
DJ
1232static void add_partial_namespace (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1234 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1235
5d7cb8df 1236static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1237 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1238 struct dwarf2_cu *cu);
1239
72bf9492
DJ
1240static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1241 struct dwarf2_cu *cu);
91c24f0a 1242
bc30ff58
JB
1243static void add_partial_subprogram (struct partial_die_info *pdi,
1244 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1245 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1246
d521ce57 1247static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1248
dee91e82 1249static struct partial_die_info *load_partial_dies
d521ce57 1250 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1251
fb816e8b
TV
1252/* A pair of partial_die_info and compilation unit. */
1253struct cu_partial_die_info
1254{
1255 /* The compilation unit of the partial_die_info. */
1256 struct dwarf2_cu *cu;
1257 /* A partial_die_info. */
1258 struct partial_die_info *pdi;
122cf0f2
AB
1259
1260 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1261 : cu (cu),
1262 pdi (pdi)
405feb71 1263 { /* Nothing. */ }
122cf0f2
AB
1264
1265private:
1266 cu_partial_die_info () = delete;
fb816e8b
TV
1267};
1268
122cf0f2
AB
1269static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1270 struct dwarf2_cu *);
72bf9492 1271
d521ce57
TT
1272static const gdb_byte *read_attribute (const struct die_reader_specs *,
1273 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1274 const gdb_byte *, bool *need_reprocess);
1275
1276static void read_attribute_reprocess (const struct die_reader_specs *reader,
1277 struct attribute *attr);
1278
1279static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1280
976ca316
SM
1281static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1282 dwarf2_section_info *, sect_offset);
f4dc4d17 1283
ed2dc618 1284static const char *read_indirect_string
976ca316 1285 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1286 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1287
ed2dc618 1288static const char *read_indirect_string_at_offset
976ca316 1289 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1290
d521ce57
TT
1291static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1292 const gdb_byte *,
3019eac3
DE
1293 unsigned int *);
1294
18a8505e
AT
1295static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1296 ULONGEST str_index);
1297
1298static const char *read_stub_str_index (struct dwarf2_cu *cu,
1299 ULONGEST str_index);
3019eac3 1300
e142c38c 1301static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1302
e142c38c
DJ
1303static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1304 struct dwarf2_cu *);
c906108c 1305
7d45c7c3
KB
1306static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1307 struct dwarf2_cu *cu);
1308
a084a2a6
AT
1309static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1310
05cf31d1
JB
1311static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1312 struct dwarf2_cu *cu);
1313
e142c38c 1314static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1315
e142c38c 1316static struct die_info *die_specification (struct die_info *die,
f2f0e013 1317 struct dwarf2_cu **);
63d06c5c 1318
9c541725 1319static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1320 struct dwarf2_cu *cu);
debd256d 1321
f3f5162e 1322static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1323 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1324 CORE_ADDR, int decode_mapping);
c906108c 1325
804d2729
TT
1326static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1327 const char *);
c906108c 1328
a14ed312 1329static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1330 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1331
ff39bb5e 1332static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1333 struct dwarf2_cu *);
c906108c 1334
ff39bb5e 1335static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1336 struct type *type,
1337 const char *name,
1338 struct obstack *obstack,
12df843f 1339 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1340 const gdb_byte **bytes,
98bfdba5 1341 struct dwarf2_locexpr_baton **baton);
2df3850c 1342
e7c27a73 1343static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1344
b4ba55a1
JB
1345static int need_gnat_info (struct dwarf2_cu *);
1346
3e43a32a
MS
1347static struct type *die_descriptive_type (struct die_info *,
1348 struct dwarf2_cu *);
b4ba55a1
JB
1349
1350static void set_descriptive_type (struct type *, struct die_info *,
1351 struct dwarf2_cu *);
1352
e7c27a73
DJ
1353static struct type *die_containing_type (struct die_info *,
1354 struct dwarf2_cu *);
c906108c 1355
ff39bb5e 1356static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1357 struct dwarf2_cu *);
c906108c 1358
f792889a 1359static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1360
673bfd45
DE
1361static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1362
0d5cff50 1363static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1364
6e70227d 1365static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1366 const char *suffix, int physname,
1367 struct dwarf2_cu *cu);
63d06c5c 1368
e7c27a73 1369static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1370
348e048f
DE
1371static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1372
e7c27a73 1373static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1374
e7c27a73 1375static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1376
96408a79
SA
1377static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1378
71a3c369
TT
1379static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1380
ff013f42 1381static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1382 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1383
41144253 1384/* Return the .debug_loclists section to use for cu. */
1385static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1386
3a2b436a 1387/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1388 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1389enum pc_bounds_kind
1390{
e385593e 1391 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1392 PC_BOUNDS_NOT_PRESENT,
1393
e385593e
JK
1394 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1395 were present but they do not form a valid range of PC addresses. */
1396 PC_BOUNDS_INVALID,
1397
3a2b436a
JK
1398 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1399 PC_BOUNDS_RANGES,
1400
1401 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1402 PC_BOUNDS_HIGH_LOW,
1403};
1404
1405static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1406 CORE_ADDR *, CORE_ADDR *,
1407 struct dwarf2_cu *,
891813be 1408 dwarf2_psymtab *);
c906108c 1409
fae299cd
DC
1410static void get_scope_pc_bounds (struct die_info *,
1411 CORE_ADDR *, CORE_ADDR *,
1412 struct dwarf2_cu *);
1413
801e3a5b
JB
1414static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1415 CORE_ADDR, struct dwarf2_cu *);
1416
a14ed312 1417static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1418 struct dwarf2_cu *);
c906108c 1419
a14ed312 1420static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1421 struct type *, struct dwarf2_cu *);
c906108c 1422
a14ed312 1423static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1424 struct die_info *, struct type *,
e7c27a73 1425 struct dwarf2_cu *);
c906108c 1426
a14ed312 1427static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1428 struct type *,
1429 struct dwarf2_cu *);
c906108c 1430
134d01f1 1431static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1432
e7c27a73 1433static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1434
e7c27a73 1435static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1436
5d7cb8df
JK
1437static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1438
804d2729 1439static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1440
27aa8d6a
SW
1441static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1442
74921315
KS
1443static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1444
f55ee35c
JK
1445static struct type *read_module_type (struct die_info *die,
1446 struct dwarf2_cu *cu);
1447
38d518c9 1448static const char *namespace_name (struct die_info *die,
e142c38c 1449 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1450
134d01f1 1451static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1452
7d79de9a
TT
1453static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1454 bool * = nullptr);
c906108c 1455
6e70227d 1456static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1457 struct dwarf2_cu *);
1458
bf6af496 1459static struct die_info *read_die_and_siblings_1
d521ce57 1460 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1461 struct die_info *);
639d11d3 1462
dee91e82 1463static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1464 const gdb_byte *info_ptr,
1465 const gdb_byte **new_info_ptr,
639d11d3
DC
1466 struct die_info *parent);
1467
d521ce57
TT
1468static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1469 struct die_info **, const gdb_byte *,
3e225074 1470 int);
3019eac3 1471
d521ce57 1472static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1473 struct die_info **, const gdb_byte *);
93311388 1474
e7c27a73 1475static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1476
15d034d0 1477static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1478 struct objfile *);
71c25dea 1479
15d034d0 1480static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1481
15d034d0 1482static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1483 struct die_info *die,
1484 struct dwarf2_cu *cu);
1485
ca69b9e6
DE
1486static const char *dwarf2_physname (const char *name, struct die_info *die,
1487 struct dwarf2_cu *cu);
1488
e142c38c 1489static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1490 struct dwarf2_cu **);
9219021c 1491
d97bc12b
DE
1492static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1493
1494static void dump_die_for_error (struct die_info *);
1495
1496static void dump_die_1 (struct ui_file *, int level, int max_level,
1497 struct die_info *);
c906108c 1498
d97bc12b 1499/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1500
51545339 1501static void store_in_ref_table (struct die_info *,
10b3939b 1502 struct dwarf2_cu *);
c906108c 1503
348e048f 1504static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1505 const struct attribute *,
348e048f
DE
1506 struct dwarf2_cu **);
1507
10b3939b 1508static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1509 const struct attribute *,
f2f0e013 1510 struct dwarf2_cu **);
c906108c 1511
348e048f 1512static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1513 const struct attribute *,
348e048f
DE
1514 struct dwarf2_cu **);
1515
ac9ec31b
DE
1516static struct type *get_signatured_type (struct die_info *, ULONGEST,
1517 struct dwarf2_cu *);
1518
1519static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1520 const struct attribute *,
ac9ec31b
DE
1521 struct dwarf2_cu *);
1522
ab432490
SM
1523static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1524 dwarf2_per_objfile *per_objfile);
348e048f 1525
ab432490
SM
1526static void read_signatured_type (signatured_type *sig_type,
1527 dwarf2_per_objfile *per_objfile);
348e048f 1528
63e43d3a
PMR
1529static int attr_to_dynamic_prop (const struct attribute *attr,
1530 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1531 struct dynamic_prop *prop, struct type *type);
63e43d3a 1532
c906108c
SS
1533/* memory allocation interface */
1534
7b5a2f43 1535static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1536
b60c80d6 1537static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1538
43f3e411 1539static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1540
8cf6f0b1
TT
1541static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1542 struct dwarf2_loclist_baton *baton,
ff39bb5e 1543 const struct attribute *attr);
8cf6f0b1 1544
ff39bb5e 1545static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1546 struct symbol *sym,
f1e6e072
TT
1547 struct dwarf2_cu *cu,
1548 int is_block);
4c2df51b 1549
d521ce57
TT
1550static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1551 const gdb_byte *info_ptr,
1552 struct abbrev_info *abbrev);
4bb7a0a7 1553
72bf9492
DJ
1554static hashval_t partial_die_hash (const void *item);
1555
1556static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1557
ae038cb0 1558static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1559 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1560 dwarf2_per_objfile *per_objfile);
ae038cb0 1561
9816fde3 1562static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1563 struct die_info *comp_unit_die,
1564 enum language pretend_language);
93311388 1565
f792889a
DJ
1566static struct type *set_die_type (struct die_info *, struct type *,
1567 struct dwarf2_cu *);
1c379e20 1568
976ca316 1569static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1570
976ca316 1571static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1fd400ff 1572
ab432490
SM
1573static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile,
1575 bool skip_partial,
1576 enum language pretend_language);
10b3939b 1577
8fc0b21d 1578static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1579 enum language pretend_language);
10b3939b 1580
8fc0b21d 1581static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1582 enum language pretend_language);
f4dc4d17 1583
10b3939b
DJ
1584static void dwarf2_add_dependence (struct dwarf2_cu *,
1585 struct dwarf2_per_cu_data *);
1586
ae038cb0
DJ
1587static void dwarf2_mark (struct dwarf2_cu *);
1588
b64f50a1 1589static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1590 dwarf2_per_cu_data *per_cu,
1591 dwarf2_per_objfile *per_objfile);
673bfd45 1592
f792889a 1593static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1594
120ce1b5
SM
1595static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1596 dwarf2_per_objfile *per_objfile,
95554aad
TT
1597 enum language pretend_language);
1598
976ca316 1599static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1600
b303c6f6
AB
1601/* Class, the destructor of which frees all allocated queue entries. This
1602 will only have work to do if an error was thrown while processing the
1603 dwarf. If no error was thrown then the queue entries should have all
1604 been processed, and freed, as we went along. */
1605
1606class dwarf2_queue_guard
1607{
1608public:
39856def
TT
1609 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1610 : m_per_objfile (per_objfile)
1611 {
1612 }
b303c6f6
AB
1613
1614 /* Free any entries remaining on the queue. There should only be
1615 entries left if we hit an error while processing the dwarf. */
1616 ~dwarf2_queue_guard ()
1617 {
39856def
TT
1618 /* Ensure that no memory is allocated by the queue. */
1619 std::queue<dwarf2_queue_item> empty;
5989a64e 1620 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1621 }
b303c6f6 1622
39856def 1623 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1624
39856def
TT
1625private:
1626 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1627};
1628
39856def
TT
1629dwarf2_queue_item::~dwarf2_queue_item ()
1630{
1631 /* Anything still marked queued is likely to be in an
1632 inconsistent state, so discard it. */
1633 if (per_cu->queued)
1634 {
7188ed02 1635 per_objfile->remove_cu (per_cu);
39856def
TT
1636 per_cu->queued = 0;
1637 }
1638}
1639
d721ba37
PA
1640/* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643struct file_and_directory
1644{
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657};
1658
1659static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
9291a0cd 1661
298e9637 1662static htab_up allocate_signatured_type_table ();
1fd400ff 1663
298e9637 1664static htab_up allocate_dwo_unit_table ();
3019eac3 1665
57d63ce2 1666static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1667 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1668 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1669
976ca316 1670static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1671
3019eac3 1672static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1673 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1674 ULONGEST signature);
3019eac3
DE
1675
1676static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1677 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1678
1b555f17 1679static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1680
263db9a1
TT
1681/* A unique pointer to a dwo_file. */
1682
51ac9db5 1683typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1684
976ca316 1685static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1686
1b80a9fa 1687static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1688
1689static void free_line_header_voidp (void *arg);
4390d890
DE
1690\f
1691/* Various complaints about symbol reading that don't abort the process. */
1692
4390d890
DE
1693static void
1694dwarf2_debug_line_missing_file_complaint (void)
1695{
b98664d3 1696 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1697}
1698
1699static void
1700dwarf2_debug_line_missing_end_sequence_complaint (void)
1701{
b98664d3 1702 complaint (_(".debug_line section has line "
4390d890
DE
1703 "program sequence without an end"));
1704}
1705
1706static void
1707dwarf2_complex_location_expr_complaint (void)
1708{
b98664d3 1709 complaint (_("location expression too complex"));
4390d890
DE
1710}
1711
1712static void
1713dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1714 int arg3)
1715{
b98664d3 1716 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1717 arg1, arg2, arg3);
1718}
1719
4390d890
DE
1720static void
1721dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1722{
b98664d3 1723 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1724 arg1, arg2);
1725}
527f3840
JK
1726
1727/* Hash function for line_header_hash. */
1728
1729static hashval_t
1730line_header_hash (const struct line_header *ofs)
1731{
9c541725 1732 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1733}
1734
1735/* Hash function for htab_create_alloc_ex for line_header_hash. */
1736
1737static hashval_t
1738line_header_hash_voidp (const void *item)
1739{
9a3c8263 1740 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1741
1742 return line_header_hash (ofs);
1743}
1744
1745/* Equality function for line_header_hash. */
1746
1747static int
1748line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1749{
9a3c8263
SM
1750 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1751 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1752
9c541725 1753 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1754 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1755}
1756
4390d890 1757\f
9291a0cd 1758
330cdd98
PA
1759/* See declaration. */
1760
5989a64e
SM
1761dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1762 bool can_copy_)
c3699833
SM
1763 : obfd (obfd),
1764 can_copy (can_copy_)
330cdd98
PA
1765{
1766 if (names == NULL)
1767 names = &dwarf2_elf_names;
1768
330cdd98
PA
1769 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1770 locate_sections (obfd, sec, *names);
1771}
1772
5989a64e 1773dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1774{
b76e467d 1775 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1776 per_cu->imported_symtabs_free ();
fc8e7e75 1777
b2bdb8cf 1778 for (signatured_type *sig_type : all_type_units)
ae640021 1779 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1780
5989a64e 1781 /* Everything else should be on this->obstack. */
330cdd98
PA
1782}
1783
7188ed02 1784/* See read.h. */
330cdd98
PA
1785
1786void
7188ed02 1787dwarf2_per_objfile::remove_all_cus ()
330cdd98 1788{
7188ed02
SM
1789 for (auto pair : m_dwarf2_cus)
1790 delete pair.second;
330cdd98 1791
7188ed02 1792 m_dwarf2_cus.clear ();
330cdd98
PA
1793}
1794
11ed8cad
TT
1795/* A helper class that calls free_cached_comp_units on
1796 destruction. */
1797
1798class free_cached_comp_units
1799{
1800public:
1801
1802 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1803 : m_per_objfile (per_objfile)
1804 {
1805 }
1806
1807 ~free_cached_comp_units ()
1808 {
7188ed02 1809 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1810 }
1811
1812 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1813
1814private:
1815
1816 dwarf2_per_objfile *m_per_objfile;
1817};
1818
af758d11
SM
1819/* See read.h. */
1820
1821bool
1822dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1823{
1824 gdb_assert (per_cu->index < this->m_symtabs.size ());
1825
1826 return this->m_symtabs[per_cu->index] != nullptr;
1827}
1828
1829/* See read.h. */
1830
1831compunit_symtab *
1832dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1833{
1834 gdb_assert (per_cu->index < this->m_symtabs.size ());
1835
1836 return this->m_symtabs[per_cu->index];
1837}
1838
1839/* See read.h. */
1840
1841void
1842dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1843 compunit_symtab *symtab)
1844{
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1847
1848 this->m_symtabs[per_cu->index] = symtab;
1849}
1850
c906108c 1851/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1852 information and return true if we have enough to do something.
1853 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1854 ELF names are used. CAN_COPY is true for formats where symbol
1855 interposition is possible and so symbol values must follow copy
1856 relocation rules. */
c906108c
SS
1857
1858int
251d32d9 1859dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1860 const struct dwarf2_debug_sections *names,
1861 bool can_copy)
c906108c 1862{
97cbe998
SDJ
1863 if (objfile->flags & OBJF_READNEVER)
1864 return 0;
1865
976ca316 1866 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1867
976ca316 1868 if (per_objfile == NULL)
5989a64e 1869 {
17ee85fc
TT
1870 dwarf2_per_bfd *per_bfd;
1871
1872 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1873 doesn't require relocations and if there aren't partial symbols
1874 from some other reader. */
1875 if (!objfile_has_partial_symbols (objfile)
1876 && !gdb_bfd_requires_relocations (objfile->obfd))
1877 {
1878 /* See if one has been created for this BFD yet. */
1879 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1880
1881 if (per_bfd == nullptr)
1882 {
1883 /* No, create it now. */
1884 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1886 }
1887 }
1888 else
1889 {
1890 /* No sharing possible, create one specifically for this objfile. */
1891 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1892 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1893 }
5989a64e 1894
976ca316 1895 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1896 }
5bfd760d 1897
976ca316
SM
1898 return (!per_objfile->per_bfd->info.is_virtual
1899 && per_objfile->per_bfd->info.s.section != NULL
1900 && !per_objfile->per_bfd->abbrev.is_virtual
1901 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1902}
1903
251d32d9
TG
1904/* When loading sections, we look either for uncompressed section or for
1905 compressed section names. */
233a11ab
CS
1906
1907static int
251d32d9
TG
1908section_is_p (const char *section_name,
1909 const struct dwarf2_section_names *names)
233a11ab 1910{
251d32d9
TG
1911 if (names->normal != NULL
1912 && strcmp (section_name, names->normal) == 0)
1913 return 1;
1914 if (names->compressed != NULL
1915 && strcmp (section_name, names->compressed) == 0)
1916 return 1;
1917 return 0;
233a11ab
CS
1918}
1919
330cdd98 1920/* See declaration. */
c906108c 1921
330cdd98 1922void
5989a64e
SM
1923dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1924 const dwarf2_debug_sections &names)
c906108c 1925{
fd361982 1926 flagword aflag = bfd_section_flags (sectp);
251d32d9 1927
dc7650b8
JK
1928 if ((aflag & SEC_HAS_CONTENTS) == 0)
1929 {
1930 }
950b7495
KS
1931 else if (elf_section_data (sectp)->this_hdr.sh_size
1932 > bfd_get_file_size (abfd))
1933 {
1934 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1935 warning (_("Discarding section %s which has a section size (%s"
1936 ") larger than the file size [in module %s]"),
1937 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1938 bfd_get_filename (abfd));
1939 }
330cdd98 1940 else if (section_is_p (sectp->name, &names.info))
c906108c 1941 {
330cdd98 1942 this->info.s.section = sectp;
fd361982 1943 this->info.size = bfd_section_size (sectp);
c906108c 1944 }
330cdd98 1945 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1946 {
330cdd98 1947 this->abbrev.s.section = sectp;
fd361982 1948 this->abbrev.size = bfd_section_size (sectp);
c906108c 1949 }
330cdd98 1950 else if (section_is_p (sectp->name, &names.line))
c906108c 1951 {
330cdd98 1952 this->line.s.section = sectp;
fd361982 1953 this->line.size = bfd_section_size (sectp);
c906108c 1954 }
330cdd98 1955 else if (section_is_p (sectp->name, &names.loc))
c906108c 1956 {
330cdd98 1957 this->loc.s.section = sectp;
fd361982 1958 this->loc.size = bfd_section_size (sectp);
c906108c 1959 }
330cdd98 1960 else if (section_is_p (sectp->name, &names.loclists))
43988095 1961 {
330cdd98 1962 this->loclists.s.section = sectp;
fd361982 1963 this->loclists.size = bfd_section_size (sectp);
43988095 1964 }
330cdd98 1965 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1966 {
330cdd98 1967 this->macinfo.s.section = sectp;
fd361982 1968 this->macinfo.size = bfd_section_size (sectp);
c906108c 1969 }
330cdd98 1970 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1971 {
330cdd98 1972 this->macro.s.section = sectp;
fd361982 1973 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1974 }
330cdd98 1975 else if (section_is_p (sectp->name, &names.str))
c906108c 1976 {
330cdd98 1977 this->str.s.section = sectp;
fd361982 1978 this->str.size = bfd_section_size (sectp);
c906108c 1979 }
18a8505e
AT
1980 else if (section_is_p (sectp->name, &names.str_offsets))
1981 {
1982 this->str_offsets.s.section = sectp;
1983 this->str_offsets.size = bfd_section_size (sectp);
1984 }
330cdd98 1985 else if (section_is_p (sectp->name, &names.line_str))
43988095 1986 {
330cdd98 1987 this->line_str.s.section = sectp;
fd361982 1988 this->line_str.size = bfd_section_size (sectp);
43988095 1989 }
330cdd98 1990 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1991 {
330cdd98 1992 this->addr.s.section = sectp;
fd361982 1993 this->addr.size = bfd_section_size (sectp);
3019eac3 1994 }
330cdd98 1995 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1996 {
330cdd98 1997 this->frame.s.section = sectp;
fd361982 1998 this->frame.size = bfd_section_size (sectp);
b6af0555 1999 }
330cdd98 2000 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2001 {
330cdd98 2002 this->eh_frame.s.section = sectp;
fd361982 2003 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2004 }
330cdd98 2005 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2006 {
330cdd98 2007 this->ranges.s.section = sectp;
fd361982 2008 this->ranges.size = bfd_section_size (sectp);
af34e669 2009 }
330cdd98 2010 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2011 {
330cdd98 2012 this->rnglists.s.section = sectp;
fd361982 2013 this->rnglists.size = bfd_section_size (sectp);
43988095 2014 }
330cdd98 2015 else if (section_is_p (sectp->name, &names.types))
348e048f 2016 {
8b70b953
TT
2017 struct dwarf2_section_info type_section;
2018
2019 memset (&type_section, 0, sizeof (type_section));
049412e3 2020 type_section.s.section = sectp;
fd361982 2021 type_section.size = bfd_section_size (sectp);
8b70b953 2022
fd5866f6 2023 this->types.push_back (type_section);
348e048f 2024 }
330cdd98 2025 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2026 {
330cdd98 2027 this->gdb_index.s.section = sectp;
fd361982 2028 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2029 }
927aa2e7
JK
2030 else if (section_is_p (sectp->name, &names.debug_names))
2031 {
2032 this->debug_names.s.section = sectp;
fd361982 2033 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2034 }
2035 else if (section_is_p (sectp->name, &names.debug_aranges))
2036 {
2037 this->debug_aranges.s.section = sectp;
fd361982 2038 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2039 }
dce234bc 2040
fd361982
AM
2041 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2042 && bfd_section_vma (sectp) == 0)
330cdd98 2043 this->has_section_at_zero = true;
c906108c
SS
2044}
2045
dce234bc 2046/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2047 SECTION_NAME. */
af34e669 2048
dce234bc 2049void
3017a003
TG
2050dwarf2_get_section_info (struct objfile *objfile,
2051 enum dwarf2_section_enum sect,
d521ce57 2052 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2053 bfd_size_type *sizep)
2054{
976ca316 2055 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2056 struct dwarf2_section_info *info;
a3b2a86b
TT
2057
2058 /* We may see an objfile without any DWARF, in which case we just
2059 return nothing. */
976ca316 2060 if (per_objfile == NULL)
a3b2a86b
TT
2061 {
2062 *sectp = NULL;
2063 *bufp = NULL;
2064 *sizep = 0;
2065 return;
2066 }
3017a003
TG
2067 switch (sect)
2068 {
2069 case DWARF2_DEBUG_FRAME:
976ca316 2070 info = &per_objfile->per_bfd->frame;
3017a003
TG
2071 break;
2072 case DWARF2_EH_FRAME:
976ca316 2073 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2074 break;
2075 default:
2076 gdb_assert_not_reached ("unexpected section");
2077 }
dce234bc 2078
96b79293 2079 info->read (objfile);
dce234bc 2080
96b79293 2081 *sectp = info->get_bfd_section ();
dce234bc
PP
2082 *bufp = info->buffer;
2083 *sizep = info->size;
2084}
2085
36586728
TT
2086/* A helper function to find the sections for a .dwz file. */
2087
2088static void
2089locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2090{
9a3c8263 2091 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2092
2093 /* Note that we only support the standard ELF names, because .dwz
2094 is ELF-only (at the time of writing). */
2095 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2096 {
049412e3 2097 dwz_file->abbrev.s.section = sectp;
fd361982 2098 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2101 {
049412e3 2102 dwz_file->info.s.section = sectp;
fd361982 2103 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2104 }
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2106 {
049412e3 2107 dwz_file->str.s.section = sectp;
fd361982 2108 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2109 }
2110 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2111 {
049412e3 2112 dwz_file->line.s.section = sectp;
fd361982 2113 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2114 }
2115 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2116 {
049412e3 2117 dwz_file->macro.s.section = sectp;
fd361982 2118 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2119 }
2ec9a5e0
TT
2120 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2121 {
049412e3 2122 dwz_file->gdb_index.s.section = sectp;
fd361982 2123 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2124 }
927aa2e7
JK
2125 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2126 {
2127 dwz_file->debug_names.s.section = sectp;
fd361982 2128 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2129 }
36586728
TT
2130}
2131
c4973306 2132/* See dwarf2read.h. */
36586728 2133
c4973306 2134struct dwz_file *
c3699833 2135dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2136{
36586728 2137 const char *filename;
acd13123 2138 bfd_size_type buildid_len_arg;
dc294be5
TT
2139 size_t buildid_len;
2140 bfd_byte *buildid;
36586728 2141
c3699833
SM
2142 if (per_bfd->dwz_file != NULL)
2143 return per_bfd->dwz_file.get ();
36586728 2144
4db1a1dc 2145 bfd_set_error (bfd_error_no_error);
791afaa2 2146 gdb::unique_xmalloc_ptr<char> data
c3699833 2147 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2148 &buildid_len_arg, &buildid));
4db1a1dc
TT
2149 if (data == NULL)
2150 {
2151 if (bfd_get_error () == bfd_error_no_error)
2152 return NULL;
2153 error (_("could not read '.gnu_debugaltlink' section: %s"),
2154 bfd_errmsg (bfd_get_error ()));
2155 }
791afaa2
TT
2156
2157 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2158
acd13123
TT
2159 buildid_len = (size_t) buildid_len_arg;
2160
791afaa2 2161 filename = data.get ();
d721ba37
PA
2162
2163 std::string abs_storage;
36586728
TT
2164 if (!IS_ABSOLUTE_PATH (filename))
2165 {
14278e1f 2166 gdb::unique_xmalloc_ptr<char> abs
c3699833 2167 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2168
14278e1f 2169 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2170 filename = abs_storage.c_str ();
36586728
TT
2171 }
2172
dc294be5
TT
2173 /* First try the file name given in the section. If that doesn't
2174 work, try to use the build-id instead. */
ad80db5b 2175 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2176 if (dwz_bfd != NULL)
36586728 2177 {
192b62ce 2178 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2179 dwz_bfd.reset (nullptr);
36586728
TT
2180 }
2181
dc294be5
TT
2182 if (dwz_bfd == NULL)
2183 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2184
0d79cdc4
AM
2185 if (dwz_bfd == nullptr)
2186 {
2187 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2188 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2189
2190 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2191 buildid_len,
2192 origname,
2193 &alt_filename));
2194
2195 if (fd.get () >= 0)
2196 {
2197 /* File successfully retrieved from server. */
ad80db5b 2198 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2199
2200 if (dwz_bfd == nullptr)
2201 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2202 alt_filename.get ());
2203 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2204 dwz_bfd.reset (nullptr);
2205 }
2206 }
2207
dc294be5
TT
2208 if (dwz_bfd == NULL)
2209 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2210 bfd_get_filename (per_bfd->obfd));
dc294be5 2211
7ff8cb8c
TT
2212 std::unique_ptr<struct dwz_file> result
2213 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2214
7ff8cb8c
TT
2215 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2216 result.get ());
36586728 2217
c3699833
SM
2218 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2219 per_bfd->dwz_file = std::move (result);
2220 return per_bfd->dwz_file.get ();
36586728 2221}
9291a0cd 2222\f
7b9f3c50
DE
2223/* DWARF quick_symbols_functions support. */
2224
2225/* TUs can share .debug_line entries, and there can be a lot more TUs than
2226 unique line tables, so we maintain a separate table of all .debug_line
2227 derived entries to support the sharing.
2228 All the quick functions need is the list of file names. We discard the
2229 line_header when we're done and don't need to record it here. */
2230struct quick_file_names
2231{
094b34ac
DE
2232 /* The data used to construct the hash key. */
2233 struct stmt_list_hash hash;
7b9f3c50
DE
2234
2235 /* The number of entries in file_names, real_names. */
2236 unsigned int num_file_names;
2237
2238 /* The file names from the line table, after being run through
2239 file_full_name. */
2240 const char **file_names;
2241
2242 /* The file names from the line table after being run through
2243 gdb_realpath. These are computed lazily. */
2244 const char **real_names;
2245};
2246
2247/* When using the index (and thus not using psymtabs), each CU has an
2248 object of this type. This is used to hold information needed by
2249 the various "quick" methods. */
2250struct dwarf2_per_cu_quick_data
2251{
2252 /* The file table. This can be NULL if there was no file table
2253 or it's currently not read in.
5989a64e 2254 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2255 struct quick_file_names *file_names;
2256
7b9f3c50
DE
2257 /* A temporary mark bit used when iterating over all CUs in
2258 expand_symtabs_matching. */
2259 unsigned int mark : 1;
2260
2261 /* True if we've tried to read the file table and found there isn't one.
2262 There will be no point in trying to read it again next time. */
2263 unsigned int no_file_data : 1;
2264};
2265
094b34ac
DE
2266/* Utility hash function for a stmt_list_hash. */
2267
2268static hashval_t
2269hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2270{
2271 hashval_t v = 0;
2272
2273 if (stmt_list_hash->dwo_unit != NULL)
2274 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2275 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2276 return v;
2277}
2278
2279/* Utility equality function for a stmt_list_hash. */
2280
2281static int
2282eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2283 const struct stmt_list_hash *rhs)
2284{
2285 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2286 return 0;
2287 if (lhs->dwo_unit != NULL
2288 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2289 return 0;
2290
9c541725 2291 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2292}
2293
7b9f3c50
DE
2294/* Hash function for a quick_file_names. */
2295
2296static hashval_t
2297hash_file_name_entry (const void *e)
2298{
9a3c8263
SM
2299 const struct quick_file_names *file_data
2300 = (const struct quick_file_names *) e;
7b9f3c50 2301
094b34ac 2302 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2303}
2304
2305/* Equality function for a quick_file_names. */
2306
2307static int
2308eq_file_name_entry (const void *a, const void *b)
2309{
9a3c8263
SM
2310 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2311 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2312
094b34ac 2313 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2314}
2315
2316/* Delete function for a quick_file_names. */
2317
2318static void
2319delete_file_name_entry (void *e)
2320{
9a3c8263 2321 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2322 int i;
2323
2324 for (i = 0; i < file_data->num_file_names; ++i)
2325 {
2326 xfree ((void*) file_data->file_names[i]);
2327 if (file_data->real_names)
2328 xfree ((void*) file_data->real_names[i]);
2329 }
2330
45940949
TT
2331 /* The space for the struct itself lives on the obstack, so we don't
2332 free it here. */
7b9f3c50
DE
2333}
2334
2335/* Create a quick_file_names hash table. */
2336
5895093f 2337static htab_up
7b9f3c50
DE
2338create_quick_file_names_table (unsigned int nr_initial_entries)
2339{
5895093f
TT
2340 return htab_up (htab_create_alloc (nr_initial_entries,
2341 hash_file_name_entry, eq_file_name_entry,
2342 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2343}
9291a0cd 2344
ab432490
SM
2345/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2346 function is unrelated to symtabs, symtab would have to be created afterwards.
2347 You should call age_cached_comp_units after processing the CU. */
918dd910 2348
1b555f17 2349static dwarf2_cu *
ab432490
SM
2350load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2351 bool skip_partial)
918dd910 2352{
3019eac3 2353 if (per_cu->is_debug_types)
ab432490 2354 load_full_type_unit (per_cu, per_objfile);
918dd910 2355 else
ab432490 2356 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2357
7188ed02
SM
2358 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2359 if (cu == nullptr)
1b555f17 2360 return nullptr; /* Dummy CU. */
2dc860c0 2361
7188ed02 2362 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2363
7188ed02 2364 return cu;
918dd910
JK
2365}
2366
97a1449a 2367/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2368
9291a0cd 2369static void
97a1449a 2370dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2371 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2372{
f4dc4d17
DE
2373 /* Skip type_unit_groups, reading the type units they contain
2374 is handled elsewhere. */
197400e8 2375 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2376 return;
2377
b303c6f6
AB
2378 /* The destructor of dwarf2_queue_guard frees any entries left on
2379 the queue. After this point we're guaranteed to leave this function
2380 with the dwarf queue empty. */
39856def 2381 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2382
976ca316 2383 if (!per_objfile->symtab_set_p (per_cu))
95554aad 2384 {
976ca316
SM
2385 queue_comp_unit (per_cu, per_objfile, language_minimal);
2386 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
89e63ee4
DE
2387
2388 /* If we just loaded a CU from a DWO, and we're working with an index
2389 that may badly handle TUs, load all the TUs in that DWO as well.
2390 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2391 if (!per_cu->is_debug_types
1b555f17
SM
2392 && cu != NULL
2393 && cu->dwo_unit != NULL
976ca316
SM
2394 && per_objfile->per_bfd->index_table != NULL
2395 && per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2396 /* DWP files aren't supported yet. */
976ca316 2397 && get_dwp_file (per_objfile) == NULL)
1b555f17 2398 queue_and_load_all_dwo_tus (cu);
95554aad 2399 }
9291a0cd 2400
976ca316 2401 process_queue (per_objfile);
9291a0cd
TT
2402
2403 /* Age the cache, releasing compilation units that have not
2404 been used recently. */
976ca316 2405 per_objfile->age_comp_units ();
9291a0cd
TT
2406}
2407
97a1449a
SM
2408/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2409 the per-objfile for which this symtab is instantiated.
2410
2411 Returns the resulting symbol table. */
2fdf6df6 2412
43f3e411 2413static struct compunit_symtab *
97a1449a 2414dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2415 dwarf2_per_objfile *per_objfile,
97a1449a 2416 bool skip_partial)
9291a0cd 2417{
976ca316 2418 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2419
976ca316 2420 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2421 {
976ca316 2422 free_cached_comp_units freer (per_objfile);
c83dd867 2423 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2424 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2425 process_cu_includes (per_objfile);
9291a0cd 2426 }
f194fefb 2427
976ca316 2428 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2429}
2430
ff4c9fec 2431/* See declaration. */
f4dc4d17 2432
ff4c9fec 2433dwarf2_per_cu_data *
5989a64e 2434dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2435{
b76e467d 2436 if (index >= this->all_comp_units.size ())
ff4c9fec 2437 {
b76e467d 2438 index -= this->all_comp_units.size ();
b2bdb8cf 2439 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2440 return &this->all_type_units[index]->per_cu;
2441 }
f4dc4d17 2442
ff4c9fec
SM
2443 return this->all_comp_units[index];
2444}
f4dc4d17 2445
ff4c9fec 2446/* See declaration. */
2fdf6df6 2447
ff4c9fec 2448dwarf2_per_cu_data *
5989a64e 2449dwarf2_per_bfd::get_cu (int index)
1fd400ff 2450{
b76e467d 2451 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2452
ff4c9fec 2453 return this->all_comp_units[index];
f4dc4d17
DE
2454}
2455
ff4c9fec 2456/* See declaration. */
f4dc4d17 2457
ff4c9fec 2458signatured_type *
5989a64e 2459dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2460{
b2bdb8cf 2461 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2462
ff4c9fec 2463 return this->all_type_units[index];
1fd400ff
TT
2464}
2465
d3473f0c
TT
2466/* See read.h. */
2467
2468dwarf2_per_cu_data *
5989a64e 2469dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2470{
2471 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2472 result->per_bfd = this;
d3473f0c
TT
2473 result->index = m_num_psymtabs++;
2474 return result;
2475}
2476
2477/* See read.h. */
2478
2479signatured_type *
5989a64e 2480dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2481{
2482 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2483 result->per_cu.per_bfd = this;
d3473f0c
TT
2484 result->per_cu.index = m_num_psymtabs++;
2485 return result;
2486}
2487
168c9250 2488/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2489 obstack, and constructed with the specified field values. */
4b514bc8
JK
2490
2491static dwarf2_per_cu_data *
168c9250
SM
2492create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2493 struct dwarf2_section_info *section,
2494 int is_dwz,
2495 sect_offset sect_off, ULONGEST length)
4b514bc8 2496{
168c9250 2497 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2498 the_cu->sect_off = sect_off;
2499 the_cu->length = length;
4b514bc8 2500 the_cu->section = section;
168c9250 2501 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2502 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2503 the_cu->is_dwz = is_dwz;
2504 return the_cu;
2505}
2506
2ec9a5e0
TT
2507/* A helper for create_cus_from_index that handles a given list of
2508 CUs. */
2fdf6df6 2509
74a0d9f6 2510static void
168c9250 2511create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2512 const gdb_byte *cu_list, offset_type n_elements,
2513 struct dwarf2_section_info *section,
b76e467d 2514 int is_dwz)
9291a0cd 2515{
12359b5e 2516 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2517 {
74a0d9f6 2518 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2519
2520 sect_offset sect_off
2521 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2522 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2523 cu_list += 2 * 8;
2524
b76e467d 2525 dwarf2_per_cu_data *per_cu
168c9250
SM
2526 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2527 length);
2528 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2529 }
9291a0cd
TT
2530}
2531
2ec9a5e0 2532/* Read the CU list from the mapped index, and use it to create all
168c9250 2533 the CU objects for PER_BFD. */
2ec9a5e0 2534
74a0d9f6 2535static void
168c9250 2536create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2537 const gdb_byte *cu_list, offset_type cu_list_elements,
2538 const gdb_byte *dwz_list, offset_type dwz_elements)
2539{
168c9250
SM
2540 gdb_assert (per_bfd->all_comp_units.empty ());
2541 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2542
168c9250
SM
2543 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2544 &per_bfd->info, 0);
2ec9a5e0
TT
2545
2546 if (dwz_elements == 0)
74a0d9f6 2547 return;
2ec9a5e0 2548
168c9250
SM
2549 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2550 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2551 &dwz->info, 1);
2ec9a5e0
TT
2552}
2553
1fd400ff 2554/* Create the signatured type hash table from the index. */
673bfd45 2555
74a0d9f6 2556static void
12359b5e 2557create_signatured_type_table_from_index
168c9250
SM
2558 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2559 const gdb_byte *bytes, offset_type elements)
1fd400ff 2560{
168c9250
SM
2561 gdb_assert (per_bfd->all_type_units.empty ());
2562 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2563
298e9637 2564 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2565
12359b5e 2566 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2567 {
52dc124a 2568 struct signatured_type *sig_type;
9c541725 2569 ULONGEST signature;
1fd400ff 2570 void **slot;
9c541725 2571 cu_offset type_offset_in_tu;
1fd400ff 2572
74a0d9f6 2573 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2574 sect_offset sect_off
2575 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2576 type_offset_in_tu
2577 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2578 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2579 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2580 bytes += 3 * 8;
2581
168c9250 2582 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2583 sig_type->signature = signature;
9c541725 2584 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2585 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2586 sig_type->per_cu.section = section;
9c541725 2587 sig_type->per_cu.sect_off = sect_off;
52dc124a 2588 sig_type->per_cu.v.quick
168c9250 2589 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2590 struct dwarf2_per_cu_quick_data);
2591
b0b6a987 2592 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2593 *slot = sig_type;
1fd400ff 2594
168c9250 2595 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2596 }
2597
168c9250 2598 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2599}
2600
927aa2e7
JK
2601/* Create the signatured type hash table from .debug_names. */
2602
2603static void
2604create_signatured_type_table_from_debug_names
976ca316 2605 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2606 const mapped_debug_names &map,
2607 struct dwarf2_section_info *section,
2608 struct dwarf2_section_info *abbrev_section)
2609{
976ca316 2610 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2611
96b79293
TT
2612 section->read (objfile);
2613 abbrev_section->read (objfile);
927aa2e7 2614
976ca316
SM
2615 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2616 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2617
298e9637 2618 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2619
2620 for (uint32_t i = 0; i < map.tu_count; ++i)
2621 {
2622 struct signatured_type *sig_type;
927aa2e7 2623 void **slot;
927aa2e7
JK
2624
2625 sect_offset sect_off
2626 = (sect_offset) (extract_unsigned_integer
2627 (map.tu_table_reordered + i * map.offset_size,
2628 map.offset_size,
2629 map.dwarf5_byte_order));
2630
2631 comp_unit_head cu_header;
976ca316 2632 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2633 abbrev_section,
927aa2e7
JK
2634 section->buffer + to_underlying (sect_off),
2635 rcuh_kind::TYPE);
2636
976ca316 2637 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2638 sig_type->signature = cu_header.signature;
2639 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2640 sig_type->per_cu.is_debug_types = 1;
2641 sig_type->per_cu.section = section;
2642 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2643 sig_type->per_cu.v.quick
976ca316 2644 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2645 struct dwarf2_per_cu_quick_data);
2646
b0b6a987 2647 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2648 *slot = sig_type;
2649
976ca316 2650 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2651 }
2652
976ca316 2653 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2654}
2655
9291a0cd
TT
2656/* Read the address map data from the mapped index, and use it to
2657 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2658
9291a0cd 2659static void
976ca316 2660create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2661 struct mapped_index *index)
9291a0cd 2662{
976ca316 2663 struct objfile *objfile = per_objfile->objfile;
08feed99 2664 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2665 const gdb_byte *iter, *end;
9291a0cd 2666 struct addrmap *mutable_map;
9291a0cd
TT
2667 CORE_ADDR baseaddr;
2668
8268c778
PA
2669 auto_obstack temp_obstack;
2670
9291a0cd
TT
2671 mutable_map = addrmap_create_mutable (&temp_obstack);
2672
f00a2de2
PA
2673 iter = index->address_table.data ();
2674 end = iter + index->address_table.size ();
9291a0cd 2675
b3b3bada 2676 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2677
2678 while (iter < end)
2679 {
2680 ULONGEST hi, lo, cu_index;
2681 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2682 iter += 8;
2683 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2684 iter += 8;
2685 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2686 iter += 4;
f652bce2 2687
24a55014 2688 if (lo > hi)
f652bce2 2689 {
b98664d3 2690 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2691 hex_string (lo), hex_string (hi));
24a55014 2692 continue;
f652bce2 2693 }
24a55014 2694
976ca316 2695 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2696 {
b98664d3 2697 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2698 (unsigned) cu_index);
24a55014 2699 continue;
f652bce2 2700 }
24a55014 2701
79748972
TT
2702 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2703 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2704 addrmap_set_empty (mutable_map, lo, hi - 1,
976ca316 2705 per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2706 }
2707
d320c2b5 2708 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2709 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2710}
2711
927aa2e7
JK
2712/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2713 populate the objfile's psymtabs_addrmap. */
2714
2715static void
976ca316 2716create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2717 struct dwarf2_section_info *section)
2718{
976ca316 2719 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2720 bfd *abfd = objfile->obfd;
08feed99 2721 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2722 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2723
2724 auto_obstack temp_obstack;
2725 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2726
2727 std::unordered_map<sect_offset,
2728 dwarf2_per_cu_data *,
2729 gdb::hash_enum<sect_offset>>
2730 debug_info_offset_to_per_cu;
976ca316 2731 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2732 {
927aa2e7
JK
2733 const auto insertpair
2734 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2735 if (!insertpair.second)
2736 {
2737 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2738 "debug_info_offset %s, ignoring .debug_aranges."),
2739 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2740 return;
2741 }
2742 }
2743
96b79293 2744 section->read (objfile);
927aa2e7
JK
2745
2746 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2747
2748 const gdb_byte *addr = section->buffer;
2749
2750 while (addr < section->buffer + section->size)
2751 {
2752 const gdb_byte *const entry_addr = addr;
2753 unsigned int bytes_read;
2754
2755 const LONGEST entry_length = read_initial_length (abfd, addr,
2756 &bytes_read);
2757 addr += bytes_read;
2758
2759 const gdb_byte *const entry_end = addr + entry_length;
2760 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2761 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2762 if (addr + entry_length > section->buffer + section->size)
2763 {
47e3f474 2764 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2765 "length %s exceeds section length %s, "
2766 "ignoring .debug_aranges."),
47e3f474
TV
2767 objfile_name (objfile),
2768 plongest (entry_addr - section->buffer),
927aa2e7
JK
2769 plongest (bytes_read + entry_length),
2770 pulongest (section->size));
2771 return;
2772 }
2773
2774 /* The version number. */
2775 const uint16_t version = read_2_bytes (abfd, addr);
2776 addr += 2;
2777 if (version != 2)
2778 {
47e3f474 2779 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2780 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2781 objfile_name (objfile),
2782 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2783 return;
2784 }
2785
2786 const uint64_t debug_info_offset
2787 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2788 addr += offset_size;
2789 const auto per_cu_it
2790 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2791 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2792 {
47e3f474 2793 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2794 "debug_info_offset %s does not exists, "
2795 "ignoring .debug_aranges."),
47e3f474
TV
2796 objfile_name (objfile),
2797 plongest (entry_addr - section->buffer),
927aa2e7
JK
2798 pulongest (debug_info_offset));
2799 return;
2800 }
2801 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2802
2803 const uint8_t address_size = *addr++;
2804 if (address_size < 1 || address_size > 8)
2805 {
47e3f474 2806 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2807 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2808 objfile_name (objfile),
2809 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2810 return;
2811 }
2812
2813 const uint8_t segment_selector_size = *addr++;
2814 if (segment_selector_size != 0)
2815 {
47e3f474 2816 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2817 "segment_selector_size %u is not supported, "
2818 "ignoring .debug_aranges."),
47e3f474
TV
2819 objfile_name (objfile),
2820 plongest (entry_addr - section->buffer),
927aa2e7
JK
2821 segment_selector_size);
2822 return;
2823 }
2824
2825 /* Must pad to an alignment boundary that is twice the address
2826 size. It is undocumented by the DWARF standard but GCC does
2827 use it. */
2828 for (size_t padding = ((-(addr - section->buffer))
2829 & (2 * address_size - 1));
2830 padding > 0; padding--)
2831 if (*addr++ != 0)
2832 {
47e3f474 2833 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2834 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2835 objfile_name (objfile),
2836 plongest (entry_addr - section->buffer));
927aa2e7
JK
2837 return;
2838 }
2839
2840 for (;;)
2841 {
2842 if (addr + 2 * address_size > entry_end)
2843 {
47e3f474 2844 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2845 "address list is not properly terminated, "
2846 "ignoring .debug_aranges."),
47e3f474
TV
2847 objfile_name (objfile),
2848 plongest (entry_addr - section->buffer));
927aa2e7
JK
2849 return;
2850 }
2851 ULONGEST start = extract_unsigned_integer (addr, address_size,
2852 dwarf5_byte_order);
2853 addr += address_size;
2854 ULONGEST length = extract_unsigned_integer (addr, address_size,
2855 dwarf5_byte_order);
2856 addr += address_size;
2857 if (start == 0 && length == 0)
2858 break;
976ca316 2859 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2860 {
2861 /* Symbol was eliminated due to a COMDAT group. */
2862 continue;
2863 }
2864 ULONGEST end = start + length;
79748972
TT
2865 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2866 - baseaddr);
2867 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2868 - baseaddr);
927aa2e7
JK
2869 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2870 }
2871 }
2872
d320c2b5 2873 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2874 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2875}
2876
9291a0cd
TT
2877/* Find a slot in the mapped index INDEX for the object named NAME.
2878 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2879 constant pool and return true. If NAME cannot be found, return
2880 false. */
2fdf6df6 2881
109483d9 2882static bool
9291a0cd
TT
2883find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2884 offset_type **vec_out)
2885{
0cf03b49 2886 offset_type hash;
9291a0cd 2887 offset_type slot, step;
559a7a62 2888 int (*cmp) (const char *, const char *);
9291a0cd 2889
791afaa2 2890 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2891 if (current_language->la_language == language_cplus
45280282
IB
2892 || current_language->la_language == language_fortran
2893 || current_language->la_language == language_d)
0cf03b49
JK
2894 {
2895 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2896 not contain any. */
a8719064 2897
72998fb3 2898 if (strchr (name, '(') != NULL)
0cf03b49 2899 {
109483d9 2900 without_params = cp_remove_params (name);
0cf03b49 2901
72998fb3 2902 if (without_params != NULL)
791afaa2 2903 name = without_params.get ();
0cf03b49
JK
2904 }
2905 }
2906
559a7a62 2907 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2908 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2909 simulate our NAME being searched is also lowercased. */
2910 hash = mapped_index_string_hash ((index->version == 4
2911 && case_sensitivity == case_sensitive_off
2912 ? 5 : index->version),
2913 name);
2914
f00a2de2
PA
2915 slot = hash & (index->symbol_table.size () - 1);
2916 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2917 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2918
2919 for (;;)
2920 {
9291a0cd 2921 const char *str;
f00a2de2
PA
2922
2923 const auto &bucket = index->symbol_table[slot];
2924 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2925 return false;
9291a0cd 2926
f00a2de2 2927 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2928 if (!cmp (name, str))
9291a0cd
TT
2929 {
2930 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2931 + MAYBE_SWAP (bucket.vec));
109483d9 2932 return true;
9291a0cd
TT
2933 }
2934
f00a2de2 2935 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2936 }
2937}
2938
4485a1c1
SM
2939/* A helper function that reads the .gdb_index from BUFFER and fills
2940 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2941 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2942 ok to use deprecated sections.
2943
2944 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2945 out parameters that are filled in with information about the CU and
2946 TU lists in the section.
2947
4485a1c1 2948 Returns true if all went well, false otherwise. */
2fdf6df6 2949
d33bc52e 2950static bool
3810f182 2951read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2952 bool deprecated_ok,
2953 gdb::array_view<const gdb_byte> buffer,
2954 struct mapped_index *map,
2955 const gdb_byte **cu_list,
2956 offset_type *cu_list_elements,
2957 const gdb_byte **types_list,
2958 offset_type *types_list_elements)
2959{
2960 const gdb_byte *addr = &buffer[0];
82430852 2961
9291a0cd 2962 /* Version check. */
4485a1c1 2963 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2964 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2965 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2966 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2967 indices. */
831adc1f 2968 if (version < 4)
481860b3
GB
2969 {
2970 static int warning_printed = 0;
2971 if (!warning_printed)
2972 {
2973 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2974 filename);
481860b3
GB
2975 warning_printed = 1;
2976 }
2977 return 0;
2978 }
2979 /* Index version 4 uses a different hash function than index version
2980 5 and later.
2981
2982 Versions earlier than 6 did not emit psymbols for inlined
2983 functions. Using these files will cause GDB not to be able to
2984 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2985 indices unless the user has done
2986 "set use-deprecated-index-sections on". */
2ec9a5e0 2987 if (version < 6 && !deprecated_ok)
481860b3
GB
2988 {
2989 static int warning_printed = 0;
2990 if (!warning_printed)
2991 {
e615022a
DE
2992 warning (_("\
2993Skipping deprecated .gdb_index section in %s.\n\
2994Do \"set use-deprecated-index-sections on\" before the file is read\n\
2995to use the section anyway."),
2ec9a5e0 2996 filename);
481860b3
GB
2997 warning_printed = 1;
2998 }
2999 return 0;
3000 }
796a7ff8 3001 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3002 of the TU (for symbols coming from TUs),
3003 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3004 Plus gold-generated indices can have duplicate entries for global symbols,
3005 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3006 These are just performance bugs, and we can't distinguish gdb-generated
3007 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3008
481860b3 3009 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3010 longer backward compatible. */
796a7ff8 3011 if (version > 8)
594e8718 3012 return 0;
9291a0cd 3013
559a7a62 3014 map->version = version;
9291a0cd 3015
4485a1c1 3016 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3017
4485a1c1 3018 int i = 0;
2ec9a5e0
TT
3019 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3020 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3021 / 8);
1fd400ff
TT
3022 ++i;
3023
2ec9a5e0
TT
3024 *types_list = addr + MAYBE_SWAP (metadata[i]);
3025 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3026 - MAYBE_SWAP (metadata[i]))
3027 / 8);
987d643c 3028 ++i;
1fd400ff 3029
f00a2de2
PA
3030 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3031 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3032 map->address_table
3033 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3034 ++i;
3035
f00a2de2
PA
3036 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3037 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3038 map->symbol_table
3039 = gdb::array_view<mapped_index::symbol_table_slot>
3040 ((mapped_index::symbol_table_slot *) symbol_table,
3041 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3042
f00a2de2 3043 ++i;
f9d83a0b 3044 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3045
2ec9a5e0
TT
3046 return 1;
3047}
3048
4485a1c1
SM
3049/* Callback types for dwarf2_read_gdb_index. */
3050
3051typedef gdb::function_view
5989a64e 3052 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3053 get_gdb_index_contents_ftype;
3054typedef gdb::function_view
3055 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3056 get_gdb_index_contents_dwz_ftype;
3057
927aa2e7 3058/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3059 elements of all the CUs and return 1. Otherwise, return 0. */
3060
3061static int
4485a1c1 3062dwarf2_read_gdb_index
976ca316 3063 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3064 get_gdb_index_contents_ftype get_gdb_index_contents,
3065 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3066{
2ec9a5e0
TT
3067 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3068 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3069 struct dwz_file *dwz;
976ca316 3070 struct objfile *objfile = per_objfile->objfile;
f8c41851 3071 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3072
4485a1c1 3073 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3074 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3075
3076 if (main_index_contents.empty ())
3077 return 0;
3078
3063847f 3079 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3080 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3081 use_deprecated_index_sections,
3082 main_index_contents, map.get (), &cu_list,
3083 &cu_list_elements, &types_list,
3084 &types_list_elements))
2ec9a5e0
TT
3085 return 0;
3086
0fefef59 3087 /* Don't use the index if it's empty. */
3063847f 3088 if (map->symbol_table.empty ())
0fefef59
DE
3089 return 0;
3090
2ec9a5e0
TT
3091 /* If there is a .dwz file, read it so we can get its CU list as
3092 well. */
f8c41851 3093 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3094 if (dwz != NULL)
2ec9a5e0 3095 {
2ec9a5e0
TT
3096 struct mapped_index dwz_map;
3097 const gdb_byte *dwz_types_ignore;
3098 offset_type dwz_types_elements_ignore;
3099
4485a1c1
SM
3100 gdb::array_view<const gdb_byte> dwz_index_content
3101 = get_gdb_index_contents_dwz (objfile, dwz);
3102
3103 if (dwz_index_content.empty ())
3104 return 0;
3105
3810f182 3106 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3107 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3108 &dwz_list, &dwz_list_elements,
3109 &dwz_types_ignore,
3110 &dwz_types_elements_ignore))
2ec9a5e0
TT
3111 {
3112 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3113 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3114 return 0;
3115 }
3116 }
3117
f8c41851
SM
3118 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3119 dwz_list_elements);
1fd400ff 3120
8b70b953
TT
3121 if (types_list_elements)
3122 {
8b70b953
TT
3123 /* We can only handle a single .debug_types when we have an
3124 index. */
f8c41851 3125 if (per_bfd->types.size () != 1)
8b70b953
TT
3126 return 0;
3127
f8c41851 3128 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3129
f8c41851 3130 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3131 types_list_elements);
8b70b953 3132 }
9291a0cd 3133
976ca316 3134 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3135
f8c41851
SM
3136 per_bfd->index_table = std::move (map);
3137 per_bfd->using_index = 1;
3138 per_bfd->quick_file_names_table =
3139 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3140
3141 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3142 objfiles using the same BFD. */
3143 gdb_assert (per_bfd->partial_symtabs == nullptr);
3144 per_bfd->partial_symtabs = objfile->partial_symtabs;
9291a0cd
TT
3145
3146 return 1;
3147}
3148
dee91e82 3149/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3150
dee91e82
DE
3151static void
3152dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3153 const gdb_byte *info_ptr,
3e225074 3154 struct die_info *comp_unit_die)
9291a0cd 3155{
dee91e82 3156 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3157 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3158 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3159 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3160 struct attribute *attr;
7b9f3c50
DE
3161 void **slot;
3162 struct quick_file_names *qfn;
9291a0cd 3163
0186c6a7
DE
3164 gdb_assert (! this_cu->is_debug_types);
3165
07261596
TT
3166 /* Our callers never want to match partial units -- instead they
3167 will match the enclosing full CU. */
3168 if (comp_unit_die->tag == DW_TAG_partial_unit)
3169 {
3170 this_cu->v.quick->no_file_data = 1;
3171 return;
3172 }
3173
0186c6a7 3174 lh_cu = this_cu;
7b9f3c50 3175 slot = NULL;
dee91e82 3176
fff8551c 3177 line_header_up lh;
9c541725 3178 sect_offset line_offset {};
fff8551c 3179
dee91e82 3180 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3181 if (attr != nullptr)
9291a0cd 3182 {
7b9f3c50
DE
3183 struct quick_file_names find_entry;
3184
9c541725 3185 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3186
3187 /* We may have already read in this line header (TU line header sharing).
3188 If we have we're done. */
094b34ac 3189 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3190 find_entry.hash.line_sect_off = line_offset;
976ca316 3191 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3192 &find_entry, INSERT);
3193 if (*slot != NULL)
3194 {
9a3c8263 3195 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3196 return;
7b9f3c50
DE
3197 }
3198
3019eac3 3199 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3200 }
3201 if (lh == NULL)
3202 {
094b34ac 3203 lh_cu->v.quick->no_file_data = 1;
dee91e82 3204 return;
9291a0cd
TT
3205 }
3206
976ca316 3207 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3208 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3209 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3210 gdb_assert (slot != NULL);
3211 *slot = qfn;
9291a0cd 3212
d721ba37 3213 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3214
aa391654
TT
3215 int offset = 0;
3216 if (strcmp (fnd.name, "<unknown>") != 0)
3217 ++offset;
3218
7ba99d21 3219 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3220 qfn->file_names =
976ca316 3221 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3222 qfn->num_file_names);
aa391654
TT
3223 if (offset != 0)
3224 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3225 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3226 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3227 fnd.comp_dir).release ();
7b9f3c50 3228 qfn->real_names = NULL;
9291a0cd 3229
094b34ac 3230 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3231}
3232
3233/* A helper for the "quick" functions which attempts to read the line
3234 table for THIS_CU. */
3235
3236static struct quick_file_names *
ab432490
SM
3237dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3238 dwarf2_per_objfile *per_objfile)
dee91e82 3239{
0186c6a7
DE
3240 /* This should never be called for TUs. */
3241 gdb_assert (! this_cu->is_debug_types);
3242 /* Nor type unit groups. */
197400e8 3243 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3244
dee91e82
DE
3245 if (this_cu->v.quick->file_names != NULL)
3246 return this_cu->v.quick->file_names;
3247 /* If we know there is no line data, no point in looking again. */
3248 if (this_cu->v.quick->no_file_data)
3249 return NULL;
3250
ab432490 3251 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3252 if (!reader.dummy_p)
3e225074 3253 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3254
3255 if (this_cu->v.quick->no_file_data)
3256 return NULL;
3257 return this_cu->v.quick->file_names;
9291a0cd
TT
3258}
3259
3260/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3261 real path for a given file name from the line table. */
2fdf6df6 3262
9291a0cd 3263static const char *
976ca316 3264dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3265 struct quick_file_names *qfn, int index)
9291a0cd 3266{
7b9f3c50 3267 if (qfn->real_names == NULL)
976ca316 3268 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3269 qfn->num_file_names, const char *);
9291a0cd 3270
7b9f3c50 3271 if (qfn->real_names[index] == NULL)
14278e1f 3272 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3273
7b9f3c50 3274 return qfn->real_names[index];
9291a0cd
TT
3275}
3276
3277static struct symtab *
3278dw2_find_last_source_symtab (struct objfile *objfile)
3279{
976ca316
SM
3280 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3281 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3282 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3283
43f3e411
DE
3284 if (cust == NULL)
3285 return NULL;
ed2dc618 3286
43f3e411 3287 return compunit_primary_filetab (cust);
9291a0cd
TT
3288}
3289
7b9f3c50
DE
3290/* Traversal function for dw2_forget_cached_source_info. */
3291
3292static int
3293dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3294{
7b9f3c50 3295 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3296
7b9f3c50 3297 if (file_data->real_names)
9291a0cd 3298 {
7b9f3c50 3299 int i;
9291a0cd 3300
7b9f3c50 3301 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3302 {
7b9f3c50
DE
3303 xfree ((void*) file_data->real_names[i]);
3304 file_data->real_names[i] = NULL;
9291a0cd
TT
3305 }
3306 }
7b9f3c50
DE
3307
3308 return 1;
3309}
3310
3311static void
3312dw2_forget_cached_source_info (struct objfile *objfile)
3313{
976ca316 3314 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3315
976ca316 3316 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3317 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3318}
3319
f8eba3c6
TT
3320/* Helper function for dw2_map_symtabs_matching_filename that expands
3321 the symtabs and calls the iterator. */
3322
3323static int
3324dw2_map_expand_apply (struct objfile *objfile,
3325 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3326 const char *name, const char *real_path,
14bc53a8 3327 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3328{
43f3e411 3329 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3330
3331 /* Don't visit already-expanded CUs. */
af758d11
SM
3332 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3333 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3334 return 0;
3335
3336 /* This may expand more than one symtab, and we want to iterate over
3337 all of them. */
97a1449a 3338 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3339
14bc53a8
PA
3340 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3341 last_made, callback);
f8eba3c6
TT
3342}
3343
3344/* Implementation of the map_symtabs_matching_filename method. */
3345
14bc53a8
PA
3346static bool
3347dw2_map_symtabs_matching_filename
3348 (struct objfile *objfile, const char *name, const char *real_path,
3349 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3350{
c011a4f4 3351 const char *name_basename = lbasename (name);
976ca316 3352 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3353
848e3e78
DE
3354 /* The rule is CUs specify all the files, including those used by
3355 any TU, so there's no need to scan TUs here. */
f4dc4d17 3356
976ca316 3357 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3358 {
3d7bb9d9 3359 /* We only need to look at symtabs not already expanded. */
976ca316 3360 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3361 continue;
3362
976ca316 3363 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3364 if (file_data == NULL)
9291a0cd
TT
3365 continue;
3366
b76e467d 3367 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3368 {
7b9f3c50 3369 const char *this_name = file_data->file_names[j];
da235a7c 3370 const char *this_real_name;
9291a0cd 3371
af529f8f 3372 if (compare_filenames_for_search (this_name, name))
9291a0cd 3373 {
f5b95b50 3374 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3375 callback))
3376 return true;
288e77a7 3377 continue;
4aac40c8 3378 }
9291a0cd 3379
c011a4f4
DE
3380 /* Before we invoke realpath, which can get expensive when many
3381 files are involved, do a quick comparison of the basenames. */
3382 if (! basenames_may_differ
3383 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3384 continue;
3385
976ca316 3386 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3387 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3388 {
da235a7c 3389 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3390 callback))
3391 return true;
288e77a7 3392 continue;
da235a7c 3393 }
9291a0cd 3394
da235a7c
JK
3395 if (real_path != NULL)
3396 {
af529f8f
JK
3397 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3398 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3399 if (this_real_name != NULL
af529f8f 3400 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3401 {
f5b95b50 3402 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3403 callback))
3404 return true;
288e77a7 3405 continue;
9291a0cd
TT
3406 }
3407 }
3408 }
3409 }
3410
14bc53a8 3411 return false;
9291a0cd
TT
3412}
3413
da51c347
DE
3414/* Struct used to manage iterating over all CUs looking for a symbol. */
3415
3416struct dw2_symtab_iterator
9291a0cd 3417{
ed2dc618 3418 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3419 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3420 /* If set, only look for symbols that match that block. Valid values are
3421 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3422 gdb::optional<block_enum> block_index;
da51c347
DE
3423 /* The kind of symbol we're looking for. */
3424 domain_enum domain;
3425 /* The list of CUs from the index entry of the symbol,
3426 or NULL if not found. */
3427 offset_type *vec;
3428 /* The next element in VEC to look at. */
3429 int next;
3430 /* The number of elements in VEC, or zero if there is no match. */
3431 int length;
8943b874
DE
3432 /* Have we seen a global version of the symbol?
3433 If so we can ignore all further global instances.
3434 This is to work around gold/15646, inefficient gold-generated
3435 indices. */
3436 int global_seen;
da51c347 3437};
9291a0cd 3438
2b79f376 3439/* Initialize the index symtab iterator ITER. */
2fdf6df6 3440
9291a0cd 3441static void
da51c347 3442dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
976ca316 3443 dwarf2_per_objfile *per_objfile,
c7f839cb 3444 gdb::optional<block_enum> block_index,
da51c347
DE
3445 domain_enum domain,
3446 const char *name)
3447{
976ca316 3448 iter->per_objfile = per_objfile;
da51c347
DE
3449 iter->block_index = block_index;
3450 iter->domain = domain;
3451 iter->next = 0;
8943b874 3452 iter->global_seen = 0;
da51c347 3453
976ca316 3454 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3455
3456 /* index is NULL if OBJF_READNOW. */
3457 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3458 iter->length = MAYBE_SWAP (*iter->vec);
3459 else
3460 {
3461 iter->vec = NULL;
3462 iter->length = 0;
3463 }
3464}
3465
3466/* Return the next matching CU or NULL if there are no more. */
3467
3468static struct dwarf2_per_cu_data *
3469dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3470{
976ca316 3471 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3472
da51c347
DE
3473 for ( ; iter->next < iter->length; ++iter->next)
3474 {
3475 offset_type cu_index_and_attrs =
3476 MAYBE_SWAP (iter->vec[iter->next + 1]);
3477 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3478 gdb_index_symbol_kind symbol_kind =
3479 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3480 /* Only check the symbol attributes if they're present.
3481 Indices prior to version 7 don't record them,
3482 and indices >= 7 may elide them for certain symbols
3483 (gold does this). */
3484 int attrs_valid =
976ca316 3485 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3486 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3487
3190f0c6 3488 /* Don't crash on bad data. */
976ca316
SM
3489 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3490 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3491 {
b98664d3 3492 complaint (_(".gdb_index entry has bad CU index"
976ca316 3493 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3494 continue;
3495 }
3496
976ca316 3497 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3498
da51c347 3499 /* Skip if already read in. */
976ca316 3500 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3501 continue;
3502
8943b874
DE
3503 /* Check static vs global. */
3504 if (attrs_valid)
3505 {
2b79f376
SM
3506 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3507
3508 if (iter->block_index.has_value ())
3509 {
3510 bool want_static = *iter->block_index == STATIC_BLOCK;
3511
3512 if (is_static != want_static)
3513 continue;
3514 }
3515
8943b874 3516 /* Work around gold/15646. */
f030440d
TV
3517 if (!is_static
3518 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3519 {
3520 if (iter->global_seen)
3521 continue;
3522
3523 iter->global_seen = 1;
3524 }
8943b874 3525 }
da51c347
DE
3526
3527 /* Only check the symbol's kind if it has one. */
3528 if (attrs_valid)
3529 {
3530 switch (iter->domain)
3531 {
3532 case VAR_DOMAIN:
3533 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3534 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3535 /* Some types are also in VAR_DOMAIN. */
3536 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3537 continue;
3538 break;
3539 case STRUCT_DOMAIN:
3540 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3541 continue;
3542 break;
3543 case LABEL_DOMAIN:
3544 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3545 continue;
3546 break;
59c35742
AB
3547 case MODULE_DOMAIN:
3548 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3549 continue;
3550 break;
da51c347
DE
3551 default:
3552 break;
3553 }
3554 }
3555
3556 ++iter->next;
3557 return per_cu;
3558 }
3559
3560 return NULL;
3561}
3562
43f3e411 3563static struct compunit_symtab *
c7f839cb 3564dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3565 const char *name, domain_enum domain)
9291a0cd 3566{
43f3e411 3567 struct compunit_symtab *stab_best = NULL;
976ca316 3568 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3569
b5ec771e
PA
3570 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3571
ed2dc618
SM
3572 struct dw2_symtab_iterator iter;
3573 struct dwarf2_per_cu_data *per_cu;
da51c347 3574
976ca316 3575 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3576
ed2dc618
SM
3577 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3578 {
3579 struct symbol *sym, *with_opaque = NULL;
97a1449a 3580 struct compunit_symtab *stab
976ca316 3581 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3582 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3583 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3584
ed2dc618
SM
3585 sym = block_find_symbol (block, name, domain,
3586 block_find_non_opaque_type_preferred,
3587 &with_opaque);
b2e2f908 3588
ed2dc618
SM
3589 /* Some caution must be observed with overloaded functions
3590 and methods, since the index will not contain any overload
3591 information (but NAME might contain it). */
da51c347 3592
ed2dc618
SM
3593 if (sym != NULL
3594 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3595 return stab;
3596 if (with_opaque != NULL
3597 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3598 stab_best = stab;
da51c347 3599
ed2dc618 3600 /* Keep looking through other CUs. */
9291a0cd 3601 }
9291a0cd 3602
da51c347 3603 return stab_best;
9291a0cd
TT
3604}
3605
3606static void
3607dw2_print_stats (struct objfile *objfile)
3608{
976ca316
SM
3609 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3610 int total = (per_objfile->per_bfd->all_comp_units.size ()
3611 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3612 int count = 0;
9291a0cd 3613
ed2dc618 3614 for (int i = 0; i < total; ++i)
9291a0cd 3615 {
976ca316 3616 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3617
976ca316 3618 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3619 ++count;
3620 }
e4a48d9d 3621 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3622 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3623}
3624
779bd270
DE
3625/* This dumps minimal information about the index.
3626 It is called via "mt print objfiles".
3627 One use is to verify .gdb_index has been loaded by the
3628 gdb.dwarf2/gdb-index.exp testcase. */
3629
9291a0cd
TT
3630static void
3631dw2_dump (struct objfile *objfile)
3632{
976ca316 3633 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3634
976ca316 3635 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3636 printf_filtered (".gdb_index:");
976ca316 3637 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3638 {
3639 printf_filtered (" version %d\n",
976ca316 3640 per_objfile->per_bfd->index_table->version);
779bd270
DE
3641 }
3642 else
3643 printf_filtered (" faked for \"readnow\"\n");
3644 printf_filtered ("\n");
9291a0cd
TT
3645}
3646
9291a0cd
TT
3647static void
3648dw2_expand_symtabs_for_function (struct objfile *objfile,
3649 const char *func_name)
3650{
976ca316 3651 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3652
ed2dc618
SM
3653 struct dw2_symtab_iterator iter;
3654 struct dwarf2_per_cu_data *per_cu;
da51c347 3655
976ca316 3656 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3657
ed2dc618 3658 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3659 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3660
9291a0cd
TT
3661}
3662
3663static void
3664dw2_expand_all_symtabs (struct objfile *objfile)
3665{
976ca316
SM
3666 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3667 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3668 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3669
ed2dc618 3670 for (int i = 0; i < total_units; ++i)
9291a0cd 3671 {
976ca316 3672 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3673
58f0c718
TT
3674 /* We don't want to directly expand a partial CU, because if we
3675 read it with the wrong language, then assertion failures can
3676 be triggered later on. See PR symtab/23010. So, tell
3677 dw2_instantiate_symtab to skip partial CUs -- any important
3678 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3679 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3680 }
3681}
3682
3683static void
652a8996
JK
3684dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3685 const char *fullname)
9291a0cd 3686{
976ca316 3687 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3688
3689 /* We don't need to consider type units here.
3690 This is only called for examining code, e.g. expand_line_sal.
3691 There can be an order of magnitude (or more) more type units
3692 than comp units, and we avoid them if we can. */
3693
976ca316 3694 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3695 {
3d7bb9d9 3696 /* We only need to look at symtabs not already expanded. */
976ca316 3697 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3698 continue;
3699
976ca316 3700 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3701 if (file_data == NULL)
9291a0cd
TT
3702 continue;
3703
b76e467d 3704 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3705 {
652a8996
JK
3706 const char *this_fullname = file_data->file_names[j];
3707
3708 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3709 {
976ca316 3710 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3711 break;
3712 }
3713 }
3714 }
3715}
3716
9a0bacfb
TV
3717static void
3718dw2_expand_symtabs_matching_symbol
3719 (mapped_index_base &index,
3720 const lookup_name_info &lookup_name_in,
3721 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3722 enum search_domain kind,
fcf23d5b
SM
3723 gdb::function_view<bool (offset_type)> match_callback,
3724 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3725
3726static void
3727dw2_expand_symtabs_matching_one
97a1449a
SM
3728 (dwarf2_per_cu_data *per_cu,
3729 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3730 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3731 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3732
9291a0cd 3733static void
199b4314
TT
3734dw2_map_matching_symbols
3735 (struct objfile *objfile,
b054970d 3736 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3737 int global,
3738 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3739 symbol_compare_ftype *ordered_compare)
9291a0cd 3740{
1aa98955 3741 /* Used for Ada. */
976ca316 3742 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3743
9a0bacfb
TV
3744 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3745
976ca316 3746 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3747 {
3748 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3749 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3750 using GNU index. */
976ca316 3751 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3752
9a0bacfb
TV
3753 const char *match_name = name.ada ().lookup_name ().c_str ();
3754 auto matcher = [&] (const char *symname)
3755 {
3756 if (ordered_compare == nullptr)
3757 return true;
3758 return ordered_compare (symname, match_name) == 0;
3759 };
3760
3761 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3762 [&] (offset_type namei)
3763 {
3764 struct dw2_symtab_iterator iter;
3765 struct dwarf2_per_cu_data *per_cu;
3766
976ca316 3767 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
9a0bacfb
TV
3768 match_name);
3769 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3770 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3771 nullptr);
9a0bacfb 3772 return true;
976ca316 3773 }, per_objfile);
9a0bacfb
TV
3774 }
3775 else
3776 {
3777 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3778 proceed assuming all symtabs have been read in. */
3779 }
1aa98955
TV
3780
3781 for (compunit_symtab *cust : objfile->compunits ())
3782 {
3783 const struct block *block;
3784
3785 if (cust == NULL)
3786 continue;
3787 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3788 if (!iterate_over_symbols_terminated (block, name,
3789 domain, callback))
3790 return;
3791 }
9291a0cd
TT
3792}
3793
e1ef7d7a
PA
3794/* Starting from a search name, return the string that finds the upper
3795 bound of all strings that start with SEARCH_NAME in a sorted name
3796 list. Returns the empty string to indicate that the upper bound is
3797 the end of the list. */
3798
3799static std::string
3800make_sort_after_prefix_name (const char *search_name)
3801{
3802 /* When looking to complete "func", we find the upper bound of all
3803 symbols that start with "func" by looking for where we'd insert
3804 the closest string that would follow "func" in lexicographical
3805 order. Usually, that's "func"-with-last-character-incremented,
3806 i.e. "fund". Mind non-ASCII characters, though. Usually those
3807 will be UTF-8 multi-byte sequences, but we can't be certain.
3808 Especially mind the 0xff character, which is a valid character in
3809 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3810 rule out compilers allowing it in identifiers. Note that
3811 conveniently, strcmp/strcasecmp are specified to compare
3812 characters interpreted as unsigned char. So what we do is treat
3813 the whole string as a base 256 number composed of a sequence of
3814 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3815 to 0, and carries 1 to the following more-significant position.
3816 If the very first character in SEARCH_NAME ends up incremented
3817 and carries/overflows, then the upper bound is the end of the
3818 list. The string after the empty string is also the empty
3819 string.
3820
3821 Some examples of this operation:
3822
3823 SEARCH_NAME => "+1" RESULT
3824
3825 "abc" => "abd"
3826 "ab\xff" => "ac"
3827 "\xff" "a" "\xff" => "\xff" "b"
3828 "\xff" => ""
3829 "\xff\xff" => ""
3830 "" => ""
3831
3832 Then, with these symbols for example:
3833
3834 func
3835 func1
3836 fund
3837
3838 completing "func" looks for symbols between "func" and
3839 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3840 which finds "func" and "func1", but not "fund".
3841
3842 And with:
3843
3844 funcÿ (Latin1 'ÿ' [0xff])
3845 funcÿ1
3846 fund
3847
3848 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3849 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3850
3851 And with:
3852
3853 ÿÿ (Latin1 'ÿ' [0xff])
3854 ÿÿ1
3855
3856 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3857 the end of the list.
3858 */
3859 std::string after = search_name;
3860 while (!after.empty () && (unsigned char) after.back () == 0xff)
3861 after.pop_back ();
3862 if (!after.empty ())
3863 after.back () = (unsigned char) after.back () + 1;
3864 return after;
3865}
3866
5c58de74 3867/* See declaration. */
61d96d7e 3868
5c58de74
PA
3869std::pair<std::vector<name_component>::const_iterator,
3870 std::vector<name_component>::const_iterator>
44ed8f3e 3871mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3872 (const lookup_name_info &lookup_name_without_params, language lang,
3873 dwarf2_per_objfile *per_objfile) const
3f563c84 3874{
5c58de74
PA
3875 auto *name_cmp
3876 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3877
3b00ef10 3878 const char *lang_name
e0802d59 3879 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3880
3f563c84
PA
3881 /* Comparison function object for lower_bound that matches against a
3882 given symbol name. */
3883 auto lookup_compare_lower = [&] (const name_component &elem,
3884 const char *name)
3885 {
fcf23d5b 3886 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3887 const char *elem_name = elem_qualified + elem.name_offset;
3888 return name_cmp (elem_name, name) < 0;
3889 };
3890
3891 /* Comparison function object for upper_bound that matches against a
3892 given symbol name. */
3893 auto lookup_compare_upper = [&] (const char *name,
3894 const name_component &elem)
3895 {
fcf23d5b 3896 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3897 const char *elem_name = elem_qualified + elem.name_offset;
3898 return name_cmp (name, elem_name) < 0;
3899 };
3900
5c58de74
PA
3901 auto begin = this->name_components.begin ();
3902 auto end = this->name_components.end ();
3f563c84
PA
3903
3904 /* Find the lower bound. */
3905 auto lower = [&] ()
3906 {
3b00ef10 3907 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3908 return begin;
3909 else
3b00ef10 3910 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3911 } ();
3912
3913 /* Find the upper bound. */
3914 auto upper = [&] ()
3915 {
5c58de74 3916 if (lookup_name_without_params.completion_mode ())
3f563c84 3917 {
e1ef7d7a
PA
3918 /* In completion mode, we want UPPER to point past all
3919 symbols names that have the same prefix. I.e., with
3920 these symbols, and completing "func":
3921
3922 function << lower bound
3923 function1
3924 other_function << upper bound
3925
3926 We find the upper bound by looking for the insertion
3927 point of "func"-with-last-character-incremented,
3928 i.e. "fund". */
3b00ef10 3929 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3930 if (after.empty ())
3f563c84 3931 return end;
e6b2f5ef
PA
3932 return std::lower_bound (lower, end, after.c_str (),
3933 lookup_compare_lower);
3f563c84
PA
3934 }
3935 else
3b00ef10 3936 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3937 } ();
3938
5c58de74
PA
3939 return {lower, upper};
3940}
3941
3942/* See declaration. */
3943
3944void
fcf23d5b 3945mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3946{
3947 if (!this->name_components.empty ())
3948 return;
3949
3950 this->name_components_casing = case_sensitivity;
3951 auto *name_cmp
3952 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3953
3954 /* The code below only knows how to break apart components of C++
3955 symbol names (and other languages that use '::' as
3b00ef10 3956 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3957 auto count = this->symbol_name_count ();
3958 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3959 {
44ed8f3e 3960 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3961 continue;
3962
fcf23d5b 3963 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3964
3965 /* Add each name component to the name component table. */
3966 unsigned int previous_len = 0;
3b00ef10
TT
3967
3968 if (strstr (name, "::") != nullptr)
3969 {
3970 for (unsigned int current_len = cp_find_first_component (name);
3971 name[current_len] != '\0';
3972 current_len += cp_find_first_component (name + current_len))
3973 {
3974 gdb_assert (name[current_len] == ':');
3975 this->name_components.push_back ({previous_len, idx});
3976 /* Skip the '::'. */
3977 current_len += 2;
3978 previous_len = current_len;
3979 }
3980 }
3981 else
5c58de74 3982 {
3b00ef10
TT
3983 /* Handle the Ada encoded (aka mangled) form here. */
3984 for (const char *iter = strstr (name, "__");
3985 iter != nullptr;
3986 iter = strstr (iter, "__"))
3987 {
3988 this->name_components.push_back ({previous_len, idx});
3989 iter += 2;
3990 previous_len = iter - name;
3991 }
5c58de74 3992 }
3b00ef10 3993
5c58de74
PA
3994 this->name_components.push_back ({previous_len, idx});
3995 }
3996
3997 /* Sort name_components elements by name. */
3998 auto name_comp_compare = [&] (const name_component &left,
3999 const name_component &right)
4000 {
fcf23d5b
SM
4001 const char *left_qualified
4002 = this->symbol_name_at (left.idx, per_objfile);
4003 const char *right_qualified
4004 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4005
4006 const char *left_name = left_qualified + left.name_offset;
4007 const char *right_name = right_qualified + right.name_offset;
4008
4009 return name_cmp (left_name, right_name) < 0;
4010 };
4011
4012 std::sort (this->name_components.begin (),
4013 this->name_components.end (),
4014 name_comp_compare);
4015}
4016
4017/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4018 mapped_index_base instead of the containing objfile. This is split
4019 to a separate function in order to be able to unit test the
4020 name_components matching using a mock mapped_index_base. For each
5c58de74 4021 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4022 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4023
4024static void
4025dw2_expand_symtabs_matching_symbol
44ed8f3e 4026 (mapped_index_base &index,
5c58de74
PA
4027 const lookup_name_info &lookup_name_in,
4028 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4029 enum search_domain kind,
fcf23d5b
SM
4030 gdb::function_view<bool (offset_type)> match_callback,
4031 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4032{
4033 lookup_name_info lookup_name_without_params
4034 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4035
4036 /* Build the symbol name component sorted vector, if we haven't
4037 yet. */
fcf23d5b 4038 index.build_name_components (per_objfile);
5c58de74 4039
3f563c84
PA
4040 /* The same symbol may appear more than once in the range though.
4041 E.g., if we're looking for symbols that complete "w", and we have
4042 a symbol named "w1::w2", we'll find the two name components for
4043 that same symbol in the range. To be sure we only call the
4044 callback once per symbol, we first collect the symbol name
4045 indexes that matched in a temporary vector and ignore
4046 duplicates. */
4047 std::vector<offset_type> matches;
3f563c84 4048
3b00ef10
TT
4049 struct name_and_matcher
4050 {
4051 symbol_name_matcher_ftype *matcher;
ecc6c606 4052 const char *name;
3b00ef10
TT
4053
4054 bool operator== (const name_and_matcher &other) const
3f563c84 4055 {
ecc6c606 4056 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4057 }
4058 };
4059
4060 /* A vector holding all the different symbol name matchers, for all
4061 languages. */
4062 std::vector<name_and_matcher> matchers;
4063
4064 for (int i = 0; i < nr_languages; i++)
4065 {
4066 enum language lang_e = (enum language) i;
4067
4068 const language_defn *lang = language_def (lang_e);
4069 symbol_name_matcher_ftype *name_matcher
4070 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4071
3b00ef10
TT
4072 name_and_matcher key {
4073 name_matcher,
4074 lookup_name_without_params.language_lookup_name (lang_e)
4075 };
4076
4077 /* Don't insert the same comparison routine more than once.
4078 Note that we do this linear walk. This is not a problem in
4079 practice because the number of supported languages is
4080 low. */
4081 if (std::find (matchers.begin (), matchers.end (), key)
4082 != matchers.end ())
9291a0cd 4083 continue;
3b00ef10
TT
4084 matchers.push_back (std::move (key));
4085
4086 auto bounds
4087 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4088 lang_e, per_objfile);
3b00ef10
TT
4089
4090 /* Now for each symbol name in range, check to see if we have a name
4091 match, and if so, call the MATCH_CALLBACK callback. */
4092
4093 for (; bounds.first != bounds.second; ++bounds.first)
4094 {
fcf23d5b
SM
4095 const char *qualified
4096 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4097
4098 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4099 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4100 continue;
9291a0cd 4101
3b00ef10
TT
4102 matches.push_back (bounds.first->idx);
4103 }
3f563c84
PA
4104 }
4105
4106 std::sort (matches.begin (), matches.end ());
4107
4108 /* Finally call the callback, once per match. */
4109 ULONGEST prev = -1;
4110 for (offset_type idx : matches)
4111 {
4112 if (prev != idx)
4113 {
3b00ef10
TT
4114 if (!match_callback (idx))
4115 break;
3f563c84
PA
4116 prev = idx;
4117 }
4118 }
4119
4120 /* Above we use a type wider than idx's for 'prev', since 0 and
4121 (offset_type)-1 are both possible values. */
4122 static_assert (sizeof (prev) > sizeof (offset_type), "");
4123}
4124
c62446b1
PA
4125#if GDB_SELF_TEST
4126
4127namespace selftests { namespace dw2_expand_symtabs_matching {
4128
a3c5fafd
PA
4129/* A mock .gdb_index/.debug_names-like name index table, enough to
4130 exercise dw2_expand_symtabs_matching_symbol, which works with the
4131 mapped_index_base interface. Builds an index from the symbol list
4132 passed as parameter to the constructor. */
4133class mock_mapped_index : public mapped_index_base
c62446b1
PA
4134{
4135public:
a3c5fafd
PA
4136 mock_mapped_index (gdb::array_view<const char *> symbols)
4137 : m_symbol_table (symbols)
c62446b1
PA
4138 {}
4139
a3c5fafd 4140 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4141
a3c5fafd 4142 /* Return the number of names in the symbol table. */
632e107b 4143 size_t symbol_name_count () const override
c62446b1 4144 {
a3c5fafd 4145 return m_symbol_table.size ();
c62446b1
PA
4146 }
4147
a3c5fafd 4148 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4149 const char *symbol_name_at
4150 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4151 {
4152 return m_symbol_table[idx];
4153 }
c62446b1 4154
a3c5fafd
PA
4155private:
4156 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4157};
4158
4159/* Convenience function that converts a NULL pointer to a "<null>"
4160 string, to pass to print routines. */
4161
4162static const char *
4163string_or_null (const char *str)
4164{
4165 return str != NULL ? str : "<null>";
4166}
4167
4168/* Check if a lookup_name_info built from
4169 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4170 index. EXPECTED_LIST is the list of expected matches, in expected
4171 matching order. If no match expected, then an empty list is
4172 specified. Returns true on success. On failure prints a warning
4173 indicating the file:line that failed, and returns false. */
4174
4175static bool
4176check_match (const char *file, int line,
4177 mock_mapped_index &mock_index,
4178 const char *name, symbol_name_match_type match_type,
4179 bool completion_mode,
fcf23d5b
SM
4180 std::initializer_list<const char *> expected_list,
4181 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4182{
4183 lookup_name_info lookup_name (name, match_type, completion_mode);
4184
4185 bool matched = true;
4186
4187 auto mismatch = [&] (const char *expected_str,
4188 const char *got)
4189 {
4190 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4191 "expected=\"%s\", got=\"%s\"\n"),
4192 file, line,
4193 (match_type == symbol_name_match_type::FULL
4194 ? "FULL" : "WILD"),
4195 name, string_or_null (expected_str), string_or_null (got));
4196 matched = false;
4197 };
4198
4199 auto expected_it = expected_list.begin ();
4200 auto expected_end = expected_list.end ();
4201
a3c5fafd 4202 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4203 NULL, ALL_DOMAIN,
4204 [&] (offset_type idx)
4205 {
fcf23d5b 4206 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4207 const char *expected_str
4208 = expected_it == expected_end ? NULL : *expected_it++;
4209
4210 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4211 mismatch (expected_str, matched_name);
3b00ef10 4212 return true;
fcf23d5b 4213 }, per_objfile);
c62446b1
PA
4214
4215 const char *expected_str
4216 = expected_it == expected_end ? NULL : *expected_it++;
4217 if (expected_str != NULL)
4218 mismatch (expected_str, NULL);
4219
4220 return matched;
4221}
4222
4223/* The symbols added to the mock mapped_index for testing (in
4224 canonical form). */
4225static const char *test_symbols[] = {
4226 "function",
4227 "std::bar",
4228 "std::zfunction",
4229 "std::zfunction2",
4230 "w1::w2",
4231 "ns::foo<char*>",
4232 "ns::foo<int>",
4233 "ns::foo<long>",
a20714ff
PA
4234 "ns2::tmpl<int>::foo2",
4235 "(anonymous namespace)::A::B::C",
c62446b1 4236
e1ef7d7a
PA
4237 /* These are used to check that the increment-last-char in the
4238 matching algorithm for completion doesn't match "t1_fund" when
4239 completing "t1_func". */
4240 "t1_func",
4241 "t1_func1",
4242 "t1_fund",
4243 "t1_fund1",
4244
4245 /* A UTF-8 name with multi-byte sequences to make sure that
4246 cp-name-parser understands this as a single identifier ("função"
4247 is "function" in PT). */
4248 u8"u8função",
4249
4250 /* \377 (0xff) is Latin1 'ÿ'. */
4251 "yfunc\377",
4252
4253 /* \377 (0xff) is Latin1 'ÿ'. */
4254 "\377",
4255 "\377\377123",
4256
c62446b1
PA
4257 /* A name with all sorts of complications. Starts with "z" to make
4258 it easier for the completion tests below. */
4259#define Z_SYM_NAME \
4260 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4261 "::tuple<(anonymous namespace)::ui*, " \
4262 "std::default_delete<(anonymous namespace)::ui>, void>"
4263
4264 Z_SYM_NAME
4265};
4266
a3c5fafd
PA
4267/* Returns true if the mapped_index_base::find_name_component_bounds
4268 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4269 in completion mode. */
5c58de74
PA
4270
4271static bool
a3c5fafd 4272check_find_bounds_finds (mapped_index_base &index,
5c58de74 4273 const char *search_name,
fcf23d5b
SM
4274 gdb::array_view<const char *> expected_syms,
4275 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4276{
4277 lookup_name_info lookup_name (search_name,
4278 symbol_name_match_type::FULL, true);
4279
3b00ef10 4280 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4281 language_cplus,
4282 per_objfile);
5c58de74
PA
4283
4284 size_t distance = std::distance (bounds.first, bounds.second);
4285 if (distance != expected_syms.size ())
4286 return false;
4287
4288 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4289 {
4290 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4291 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4292 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4293 return false;
4294 }
4295
4296 return true;
4297}
4298
4299/* Test the lower-level mapped_index::find_name_component_bounds
4300 method. */
4301
c62446b1 4302static void
5c58de74
PA
4303test_mapped_index_find_name_component_bounds ()
4304{
4305 mock_mapped_index mock_index (test_symbols);
4306
fcf23d5b 4307 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4308
4309 /* Test the lower-level mapped_index::find_name_component_bounds
4310 method in completion mode. */
4311 {
4312 static const char *expected_syms[] = {
4313 "t1_func",
4314 "t1_func1",
5c58de74
PA
4315 };
4316
fcf23d5b
SM
4317 SELF_CHECK (check_find_bounds_finds
4318 (mock_index, "t1_func", expected_syms,
4319 NULL /* per_objfile */));
5c58de74
PA
4320 }
4321
4322 /* Check that the increment-last-char in the name matching algorithm
4323 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4324 {
4325 static const char *expected_syms1[] = {
4326 "\377",
4327 "\377\377123",
4328 };
fcf23d5b
SM
4329 SELF_CHECK (check_find_bounds_finds
4330 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4331
4332 static const char *expected_syms2[] = {
4333 "\377\377123",
4334 };
fcf23d5b
SM
4335 SELF_CHECK (check_find_bounds_finds
4336 (mock_index, "\377\377", expected_syms2,
4337 NULL /* per_objfile */));
5c58de74
PA
4338 }
4339}
4340
4341/* Test dw2_expand_symtabs_matching_symbol. */
4342
4343static void
4344test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4345{
4346 mock_mapped_index mock_index (test_symbols);
4347
4348 /* We let all tests run until the end even if some fails, for debug
4349 convenience. */
4350 bool any_mismatch = false;
4351
4352 /* Create the expected symbols list (an initializer_list). Needed
4353 because lists have commas, and we need to pass them to CHECK,
4354 which is a macro. */
4355#define EXPECT(...) { __VA_ARGS__ }
4356
4357 /* Wrapper for check_match that passes down the current
4358 __FILE__/__LINE__. */
4359#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4360 any_mismatch |= !check_match (__FILE__, __LINE__, \
4361 mock_index, \
4362 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4363 EXPECTED_LIST, NULL)
c62446b1
PA
4364
4365 /* Identity checks. */
4366 for (const char *sym : test_symbols)
4367 {
4368 /* Should be able to match all existing symbols. */
4369 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4370 EXPECT (sym));
4371
4372 /* Should be able to match all existing symbols with
4373 parameters. */
4374 std::string with_params = std::string (sym) + "(int)";
4375 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4376 EXPECT (sym));
4377
4378 /* Should be able to match all existing symbols with
4379 parameters and qualifiers. */
4380 with_params = std::string (sym) + " ( int ) const";
4381 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4382 EXPECT (sym));
4383
4384 /* This should really find sym, but cp-name-parser.y doesn't
4385 know about lvalue/rvalue qualifiers yet. */
4386 with_params = std::string (sym) + " ( int ) &&";
4387 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4388 {});
4389 }
4390
e1ef7d7a
PA
4391 /* Check that the name matching algorithm for completion doesn't get
4392 confused with Latin1 'ÿ' / 0xff. */
4393 {
4394 static const char str[] = "\377";
4395 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4396 EXPECT ("\377", "\377\377123"));
4397 }
4398
4399 /* Check that the increment-last-char in the matching algorithm for
4400 completion doesn't match "t1_fund" when completing "t1_func". */
4401 {
4402 static const char str[] = "t1_func";
4403 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4404 EXPECT ("t1_func", "t1_func1"));
4405 }
4406
c62446b1
PA
4407 /* Check that completion mode works at each prefix of the expected
4408 symbol name. */
4409 {
4410 static const char str[] = "function(int)";
4411 size_t len = strlen (str);
4412 std::string lookup;
4413
4414 for (size_t i = 1; i < len; i++)
4415 {
4416 lookup.assign (str, i);
4417 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4418 EXPECT ("function"));
4419 }
4420 }
4421
4422 /* While "w" is a prefix of both components, the match function
4423 should still only be called once. */
4424 {
4425 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4426 EXPECT ("w1::w2"));
a20714ff
PA
4427 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4428 EXPECT ("w1::w2"));
c62446b1
PA
4429 }
4430
4431 /* Same, with a "complicated" symbol. */
4432 {
4433 static const char str[] = Z_SYM_NAME;
4434 size_t len = strlen (str);
4435 std::string lookup;
4436
4437 for (size_t i = 1; i < len; i++)
4438 {
4439 lookup.assign (str, i);
4440 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4441 EXPECT (Z_SYM_NAME));
4442 }
4443 }
4444
4445 /* In FULL mode, an incomplete symbol doesn't match. */
4446 {
4447 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4448 {});
4449 }
4450
4451 /* A complete symbol with parameters matches any overload, since the
4452 index has no overload info. */
4453 {
4454 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4455 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4456 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4457 EXPECT ("std::zfunction", "std::zfunction2"));
4458 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4459 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4460 }
4461
4462 /* Check that whitespace is ignored appropriately. A symbol with a
4463 template argument list. */
4464 {
4465 static const char expected[] = "ns::foo<int>";
4466 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4467 EXPECT (expected));
a20714ff
PA
4468 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4469 EXPECT (expected));
c62446b1
PA
4470 }
4471
4472 /* Check that whitespace is ignored appropriately. A symbol with a
4473 template argument list that includes a pointer. */
4474 {
4475 static const char expected[] = "ns::foo<char*>";
4476 /* Try both completion and non-completion modes. */
4477 static const bool completion_mode[2] = {false, true};
4478 for (size_t i = 0; i < 2; i++)
4479 {
4480 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4481 completion_mode[i], EXPECT (expected));
a20714ff
PA
4482 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4483 completion_mode[i], EXPECT (expected));
c62446b1
PA
4484
4485 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4486 completion_mode[i], EXPECT (expected));
a20714ff
PA
4487 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4488 completion_mode[i], EXPECT (expected));
c62446b1
PA
4489 }
4490 }
4491
4492 {
4493 /* Check method qualifiers are ignored. */
4494 static const char expected[] = "ns::foo<char*>";
4495 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4496 symbol_name_match_type::FULL, true, EXPECT (expected));
4497 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4498 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4499 CHECK_MATCH ("foo < char * > ( int ) const",
4500 symbol_name_match_type::WILD, true, EXPECT (expected));
4501 CHECK_MATCH ("foo < char * > ( int ) &&",
4502 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4503 }
4504
4505 /* Test lookup names that don't match anything. */
4506 {
a20714ff
PA
4507 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4508 {});
4509
c62446b1
PA
4510 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4511 {});
4512 }
4513
a20714ff
PA
4514 /* Some wild matching tests, exercising "(anonymous namespace)",
4515 which should not be confused with a parameter list. */
4516 {
4517 static const char *syms[] = {
4518 "A::B::C",
4519 "B::C",
4520 "C",
4521 "A :: B :: C ( int )",
4522 "B :: C ( int )",
4523 "C ( int )",
4524 };
4525
4526 for (const char *s : syms)
4527 {
4528 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4529 EXPECT ("(anonymous namespace)::A::B::C"));
4530 }
4531 }
4532
4533 {
4534 static const char expected[] = "ns2::tmpl<int>::foo2";
4535 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4536 EXPECT (expected));
4537 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4538 EXPECT (expected));
4539 }
4540
c62446b1
PA
4541 SELF_CHECK (!any_mismatch);
4542
4543#undef EXPECT
4544#undef CHECK_MATCH
4545}
4546
5c58de74
PA
4547static void
4548run_test ()
4549{
4550 test_mapped_index_find_name_component_bounds ();
4551 test_dw2_expand_symtabs_matching_symbol ();
4552}
4553
c62446b1
PA
4554}} // namespace selftests::dw2_expand_symtabs_matching
4555
4556#endif /* GDB_SELF_TEST */
4557
4b514bc8
JK
4558/* If FILE_MATCHER is NULL or if PER_CU has
4559 dwarf2_per_cu_quick_data::MARK set (see
4560 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4561 EXPANSION_NOTIFY on it. */
4562
4563static void
4564dw2_expand_symtabs_matching_one
97a1449a
SM
4565 (dwarf2_per_cu_data *per_cu,
4566 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4567 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4568 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4569{
4570 if (file_matcher == NULL || per_cu->v.quick->mark)
4571 {
af758d11 4572 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4573
97a1449a
SM
4574 compunit_symtab *symtab
4575 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4576 gdb_assert (symtab != nullptr);
4b514bc8 4577
af758d11
SM
4578 if (expansion_notify != NULL && symtab_was_null)
4579 expansion_notify (symtab);
4b514bc8
JK
4580 }
4581}
4582
3f563c84
PA
4583/* Helper for dw2_expand_matching symtabs. Called on each symbol
4584 matched, to expand corresponding CUs that were marked. IDX is the
4585 index of the symbol name that matched. */
4586
4587static void
4588dw2_expand_marked_cus
976ca316 4589 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4590 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4591 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4592 search_domain kind)
4593{
3f563c84
PA
4594 offset_type *vec, vec_len, vec_idx;
4595 bool global_seen = false;
976ca316 4596 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4597
61920122 4598 vec = (offset_type *) (index.constant_pool
f00a2de2 4599 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4600 vec_len = MAYBE_SWAP (vec[0]);
4601 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4602 {
61920122
PA
4603 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4604 /* This value is only valid for index versions >= 7. */
4605 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4606 gdb_index_symbol_kind symbol_kind =
4607 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4608 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4609 /* Only check the symbol attributes if they're present.
4610 Indices prior to version 7 don't record them,
4611 and indices >= 7 may elide them for certain symbols
4612 (gold does this). */
4613 int attrs_valid =
4614 (index.version >= 7
4615 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4616
4617 /* Work around gold/15646. */
f030440d
TV
4618 if (attrs_valid
4619 && !is_static
4620 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4621 {
f030440d 4622 if (global_seen)
61920122 4623 continue;
f030440d
TV
4624
4625 global_seen = true;
61920122 4626 }
3190f0c6 4627
61920122
PA
4628 /* Only check the symbol's kind if it has one. */
4629 if (attrs_valid)
4630 {
4631 switch (kind)
8943b874 4632 {
61920122
PA
4633 case VARIABLES_DOMAIN:
4634 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4635 continue;
4636 break;
4637 case FUNCTIONS_DOMAIN:
4638 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4639 continue;
61920122
PA
4640 break;
4641 case TYPES_DOMAIN:
4642 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4643 continue;
4644 break;
59c35742
AB
4645 case MODULES_DOMAIN:
4646 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4647 continue;
4648 break;
61920122
PA
4649 default:
4650 break;
8943b874 4651 }
61920122 4652 }
8943b874 4653
61920122 4654 /* Don't crash on bad data. */
976ca316
SM
4655 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4656 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4657 {
b98664d3 4658 complaint (_(".gdb_index entry has bad CU index"
976ca316 4659 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4660 continue;
4661 }
4662
976ca316
SM
4663 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4664 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4665 expansion_notify);
61920122
PA
4666 }
4667}
4668
4b514bc8
JK
4669/* If FILE_MATCHER is non-NULL, set all the
4670 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4671 that match FILE_MATCHER. */
4672
61920122 4673static void
4b514bc8 4674dw_expand_symtabs_matching_file_matcher
976ca316 4675 (dwarf2_per_objfile *per_objfile,
ed2dc618 4676 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4677{
4b514bc8 4678 if (file_matcher == NULL)
61920122
PA
4679 return;
4680
4b514bc8
JK
4681 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4682 htab_eq_pointer,
4683 NULL, xcalloc, xfree));
4684 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4685 htab_eq_pointer,
4686 NULL, xcalloc, xfree));
61920122 4687
4b514bc8
JK
4688 /* The rule is CUs specify all the files, including those used by
4689 any TU, so there's no need to scan TUs here. */
61920122 4690
976ca316 4691 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4692 {
927aa2e7
JK
4693 QUIT;
4694
4695 per_cu->v.quick->mark = 0;
4696
4697 /* We only need to look at symtabs not already expanded. */
976ca316 4698 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4699 continue;
4700
976ca316 4701 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4702 if (file_data == NULL)
4703 continue;
4704
4705 if (htab_find (visited_not_found.get (), file_data) != NULL)
4706 continue;
4707 else if (htab_find (visited_found.get (), file_data) != NULL)
4708 {
4709 per_cu->v.quick->mark = 1;
4710 continue;
4711 }
4712
b76e467d 4713 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4714 {
4715 const char *this_real_name;
4716
4717 if (file_matcher (file_data->file_names[j], false))
4718 {
4719 per_cu->v.quick->mark = 1;
4720 break;
4721 }
4722
4723 /* Before we invoke realpath, which can get expensive when many
4724 files are involved, do a quick comparison of the basenames. */
4725 if (!basenames_may_differ
4726 && !file_matcher (lbasename (file_data->file_names[j]),
4727 true))
4728 continue;
4729
976ca316 4730 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4731 if (file_matcher (this_real_name, false))
4732 {
4733 per_cu->v.quick->mark = 1;
4734 break;
4735 }
4736 }
4737
b76e467d
SM
4738 void **slot = htab_find_slot (per_cu->v.quick->mark
4739 ? visited_found.get ()
4740 : visited_not_found.get (),
4741 file_data, INSERT);
927aa2e7
JK
4742 *slot = file_data;
4743 }
4744}
4745
4746static void
4747dw2_expand_symtabs_matching
4748 (struct objfile *objfile,
4749 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4750 const lookup_name_info *lookup_name,
927aa2e7
JK
4751 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4752 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4753 enum search_domain kind)
4754{
976ca316 4755 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4756
4757 /* index_table is NULL if OBJF_READNOW. */
976ca316 4758 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4759 return;
4760
976ca316 4761 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4762
c1a66c06
TV
4763 if (symbol_matcher == NULL && lookup_name == NULL)
4764 {
976ca316 4765 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4766 {
4767 QUIT;
4768
976ca316 4769 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4770 file_matcher, expansion_notify);
c1a66c06
TV
4771 }
4772 return;
4773 }
4774
976ca316 4775 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4776
c1a66c06 4777 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4778 symbol_matcher,
4779 kind, [&] (offset_type idx)
4780 {
976ca316
SM
4781 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4782 kind);
3b00ef10 4783 return true;
976ca316 4784 }, per_objfile);
927aa2e7
JK
4785}
4786
4787/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4788 symtab. */
4789
4790static struct compunit_symtab *
4791recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4792 CORE_ADDR pc)
4793{
4794 int i;
4795
4796 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4797 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4798 return cust;
4799
4800 if (cust->includes == NULL)
4801 return NULL;
4802
4803 for (i = 0; cust->includes[i]; ++i)
4804 {
4805 struct compunit_symtab *s = cust->includes[i];
4806
4807 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4808 if (s != NULL)
4809 return s;
4810 }
4811
4812 return NULL;
4813}
4814
4815static struct compunit_symtab *
4816dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4817 struct bound_minimal_symbol msymbol,
4818 CORE_ADDR pc,
4819 struct obj_section *section,
4820 int warn_if_readin)
4821{
4822 struct dwarf2_per_cu_data *data;
4823 struct compunit_symtab *result;
4824
d320c2b5 4825 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4826 return NULL;
4827
b3b3bada 4828 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4829 data = (struct dwarf2_per_cu_data *) addrmap_find
4830 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4831 if (!data)
4832 return NULL;
4833
af758d11
SM
4834 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4835 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4836 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4837 paddress (objfile->arch (), pc));
927aa2e7 4838
97a1449a
SM
4839 result = recursively_find_pc_sect_compunit_symtab
4840 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4841
927aa2e7
JK
4842 gdb_assert (result != NULL);
4843 return result;
4844}
4845
4846static void
4847dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4848 void *data, int need_fullname)
4849{
976ca316 4850 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4851
976ca316 4852 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4853 {
976ca316 4854 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4855
4856 htab_up visited (htab_create_alloc (10,
4857 htab_hash_pointer, htab_eq_pointer,
4858 NULL, xcalloc, xfree));
4859
4860 /* The rule is CUs specify all the files, including those used
4861 by any TU, so there's no need to scan TUs here. We can
4862 ignore file names coming from already-expanded CUs. */
4863
976ca316 4864 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4865 {
976ca316 4866 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4867 {
4868 void **slot = htab_find_slot (visited.get (),
4869 per_cu->v.quick->file_names,
4870 INSERT);
4871
4872 *slot = per_cu->v.quick->file_names;
4873 }
4874 }
4875
976ca316 4876 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4877 {
927aa2e7 4878 /* We only need to look at symtabs not already expanded. */
976ca316 4879 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4880 continue;
4881
ab432490 4882 quick_file_names *file_data
976ca316 4883 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4884 if (file_data == NULL)
4885 continue;
4886
b76e467d 4887 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4888 if (*slot)
4889 {
4890 /* Already visited. */
4891 continue;
4892 }
4893 *slot = file_data;
4894
4895 for (int j = 0; j < file_data->num_file_names; ++j)
4896 {
4897 const char *filename = file_data->file_names[j];
976ca316 4898 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4899 }
4900 }
4901 }
4902
976ca316 4903 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4904 {
4905 gdb::unique_xmalloc_ptr<char> this_real_name;
4906
4907 if (need_fullname)
4908 this_real_name = gdb_realpath (filename);
4909 (*fun) (filename, this_real_name.get (), data);
4910 });
4911}
4912
4913static int
4914dw2_has_symbols (struct objfile *objfile)
4915{
4916 return 1;
4917}
4918
4919const struct quick_symbol_functions dwarf2_gdb_index_functions =
4920{
4921 dw2_has_symbols,
4922 dw2_find_last_source_symtab,
4923 dw2_forget_cached_source_info,
4924 dw2_map_symtabs_matching_filename,
4925 dw2_lookup_symbol,
d3214198 4926 NULL,
927aa2e7
JK
4927 dw2_print_stats,
4928 dw2_dump,
927aa2e7
JK
4929 dw2_expand_symtabs_for_function,
4930 dw2_expand_all_symtabs,
4931 dw2_expand_symtabs_with_fullname,
4932 dw2_map_matching_symbols,
4933 dw2_expand_symtabs_matching,
4934 dw2_find_pc_sect_compunit_symtab,
4935 NULL,
4936 dw2_map_symbol_filenames
4937};
4938
4939/* DWARF-5 debug_names reader. */
4940
4941/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4942static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4943
4944/* A helper function that reads the .debug_names section in SECTION
4945 and fills in MAP. FILENAME is the name of the file containing the
4946 section; it is used for error reporting.
4947
4948 Returns true if all went well, false otherwise. */
4949
4950static bool
4951read_debug_names_from_section (struct objfile *objfile,
4952 const char *filename,
4953 struct dwarf2_section_info *section,
4954 mapped_debug_names &map)
4955{
96b79293 4956 if (section->empty ())
927aa2e7
JK
4957 return false;
4958
4959 /* Older elfutils strip versions could keep the section in the main
4960 executable while splitting it for the separate debug info file. */
96b79293 4961 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4962 return false;
4963
96b79293 4964 section->read (objfile);
927aa2e7 4965
08feed99 4966 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4967
4968 const gdb_byte *addr = section->buffer;
4969
96b79293 4970 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4971
4972 unsigned int bytes_read;
4973 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4974 addr += bytes_read;
4975
4976 map.dwarf5_is_dwarf64 = bytes_read != 4;
4977 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4978 if (bytes_read + length != section->size)
4979 {
4980 /* There may be multiple per-CU indices. */
4981 warning (_("Section .debug_names in %s length %s does not match "
4982 "section length %s, ignoring .debug_names."),
4983 filename, plongest (bytes_read + length),
4984 pulongest (section->size));
4985 return false;
4986 }
4987
4988 /* The version number. */
4989 uint16_t version = read_2_bytes (abfd, addr);
4990 addr += 2;
4991 if (version != 5)
4992 {
4993 warning (_("Section .debug_names in %s has unsupported version %d, "
4994 "ignoring .debug_names."),
4995 filename, version);
4996 return false;
4997 }
4998
4999 /* Padding. */
5000 uint16_t padding = read_2_bytes (abfd, addr);
5001 addr += 2;
5002 if (padding != 0)
5003 {
5004 warning (_("Section .debug_names in %s has unsupported padding %d, "
5005 "ignoring .debug_names."),
5006 filename, padding);
5007 return false;
5008 }
5009
5010 /* comp_unit_count - The number of CUs in the CU list. */
5011 map.cu_count = read_4_bytes (abfd, addr);
5012 addr += 4;
5013
5014 /* local_type_unit_count - The number of TUs in the local TU
5015 list. */
5016 map.tu_count = read_4_bytes (abfd, addr);
5017 addr += 4;
5018
5019 /* foreign_type_unit_count - The number of TUs in the foreign TU
5020 list. */
5021 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5022 addr += 4;
5023 if (foreign_tu_count != 0)
5024 {
5025 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5026 "ignoring .debug_names."),
5027 filename, static_cast<unsigned long> (foreign_tu_count));
5028 return false;
5029 }
5030
5031 /* bucket_count - The number of hash buckets in the hash lookup
5032 table. */
5033 map.bucket_count = read_4_bytes (abfd, addr);
5034 addr += 4;
5035
5036 /* name_count - The number of unique names in the index. */
5037 map.name_count = read_4_bytes (abfd, addr);
5038 addr += 4;
5039
5040 /* abbrev_table_size - The size in bytes of the abbreviations
5041 table. */
5042 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5043 addr += 4;
5044
5045 /* augmentation_string_size - The size in bytes of the augmentation
5046 string. This value is rounded up to a multiple of 4. */
5047 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5048 addr += 4;
5049 map.augmentation_is_gdb = ((augmentation_string_size
5050 == sizeof (dwarf5_augmentation))
5051 && memcmp (addr, dwarf5_augmentation,
5052 sizeof (dwarf5_augmentation)) == 0);
5053 augmentation_string_size += (-augmentation_string_size) & 3;
5054 addr += augmentation_string_size;
5055
5056 /* List of CUs */
5057 map.cu_table_reordered = addr;
5058 addr += map.cu_count * map.offset_size;
5059
5060 /* List of Local TUs */
5061 map.tu_table_reordered = addr;
5062 addr += map.tu_count * map.offset_size;
5063
5064 /* Hash Lookup Table */
5065 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5066 addr += map.bucket_count * 4;
5067 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5068 addr += map.name_count * 4;
5069
5070 /* Name Table */
5071 map.name_table_string_offs_reordered = addr;
5072 addr += map.name_count * map.offset_size;
5073 map.name_table_entry_offs_reordered = addr;
5074 addr += map.name_count * map.offset_size;
5075
5076 const gdb_byte *abbrev_table_start = addr;
5077 for (;;)
5078 {
927aa2e7
JK
5079 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5080 addr += bytes_read;
5081 if (index_num == 0)
5082 break;
5083
5084 const auto insertpair
5085 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5086 if (!insertpair.second)
5087 {
5088 warning (_("Section .debug_names in %s has duplicate index %s, "
5089 "ignoring .debug_names."),
5090 filename, pulongest (index_num));
5091 return false;
5092 }
5093 mapped_debug_names::index_val &indexval = insertpair.first->second;
5094 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5095 addr += bytes_read;
5096
5097 for (;;)
5098 {
5099 mapped_debug_names::index_val::attr attr;
5100 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5101 addr += bytes_read;
5102 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5103 addr += bytes_read;
5104 if (attr.form == DW_FORM_implicit_const)
5105 {
5106 attr.implicit_const = read_signed_leb128 (abfd, addr,
5107 &bytes_read);
5108 addr += bytes_read;
5109 }
5110 if (attr.dw_idx == 0 && attr.form == 0)
5111 break;
5112 indexval.attr_vec.push_back (std::move (attr));
5113 }
5114 }
5115 if (addr != abbrev_table_start + abbrev_table_size)
5116 {
5117 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5118 "of size %s vs. written as %u, ignoring .debug_names."),
5119 filename, plongest (addr - abbrev_table_start),
5120 abbrev_table_size);
927aa2e7
JK
5121 return false;
5122 }
5123 map.entry_pool = addr;
5124
5125 return true;
5126}
5127
5128/* A helper for create_cus_from_debug_names that handles the MAP's CU
5129 list. */
5130
5131static void
168c9250 5132create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5133 const mapped_debug_names &map,
5134 dwarf2_section_info &section,
b76e467d 5135 bool is_dwz)
927aa2e7 5136{
3ee6bb11
TV
5137 if (!map.augmentation_is_gdb)
5138 {
5139 for (uint32_t i = 0; i < map.cu_count; ++i)
5140 {
5141 sect_offset sect_off
5142 = (sect_offset) (extract_unsigned_integer
5143 (map.cu_table_reordered + i * map.offset_size,
5144 map.offset_size,
5145 map.dwarf5_byte_order));
5146 /* We don't know the length of the CU, because the CU list in a
5147 .debug_names index can be incomplete, so we can't use the start of
5148 the next CU as end of this CU. We create the CUs here with length 0,
5149 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5150 dwarf2_per_cu_data *per_cu
168c9250
SM
5151 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5152 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5153 }
5154 }
5155
927aa2e7
JK
5156 sect_offset sect_off_prev;
5157 for (uint32_t i = 0; i <= map.cu_count; ++i)
5158 {
5159 sect_offset sect_off_next;
5160 if (i < map.cu_count)
5161 {
5162 sect_off_next
5163 = (sect_offset) (extract_unsigned_integer
5164 (map.cu_table_reordered + i * map.offset_size,
5165 map.offset_size,
5166 map.dwarf5_byte_order));
5167 }
5168 else
5169 sect_off_next = (sect_offset) section.size;
5170 if (i >= 1)
5171 {
5172 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5173 dwarf2_per_cu_data *per_cu
168c9250 5174 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5175 sect_off_prev, length);
168c9250 5176 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5177 }
5178 sect_off_prev = sect_off_next;
5179 }
5180}
5181
5182/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5183 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5184
5185static void
168c9250 5186create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5187 const mapped_debug_names &map,
5188 const mapped_debug_names &dwz_map)
5189{
168c9250
SM
5190 gdb_assert (per_bfd->all_comp_units.empty ());
5191 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5192
168c9250 5193 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5194 false /* is_dwz */);
927aa2e7
JK
5195
5196 if (dwz_map.cu_count == 0)
5197 return;
5198
168c9250
SM
5199 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5200 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5201 true /* is_dwz */);
927aa2e7
JK
5202}
5203
5204/* Read .debug_names. If everything went ok, initialize the "quick"
5205 elements of all the CUs and return true. Otherwise, return false. */
5206
5207static bool
976ca316 5208dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5209{
fcf23d5b
SM
5210 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5211 mapped_debug_names dwz_map;
976ca316 5212 struct objfile *objfile = per_objfile->objfile;
f8c41851 5213 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5214
5215 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5216 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5217 return false;
5218
5219 /* Don't use the index if it's empty. */
22ca247e 5220 if (map->name_count == 0)
927aa2e7
JK
5221 return false;
5222
5223 /* If there is a .dwz file, read it so we can get its CU list as
5224 well. */
f8c41851 5225 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5226 if (dwz != NULL)
5227 {
5228 if (!read_debug_names_from_section (objfile,
00f93c44 5229 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5230 &dwz->debug_names, dwz_map))
5231 {
5232 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5233 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5234 return false;
5235 }
5236 }
5237
f8c41851 5238 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5239
22ca247e 5240 if (map->tu_count != 0)
927aa2e7
JK
5241 {
5242 /* We can only handle a single .debug_types when we have an
5243 index. */
f8c41851 5244 if (per_bfd->types.size () != 1)
927aa2e7
JK
5245 return false;
5246
f8c41851 5247 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5248
5249 create_signatured_type_table_from_debug_names
f8c41851 5250 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5251 }
5252
f8c41851 5253 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5254
f8c41851
SM
5255 per_bfd->debug_names_table = std::move (map);
5256 per_bfd->using_index = 1;
5257 per_bfd->quick_file_names_table =
976ca316 5258 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5259
f8c41851
SM
5260 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5261 objfiles using the same BFD. */
5262 gdb_assert (per_bfd->partial_symtabs == nullptr);
5263 per_bfd->partial_symtabs = objfile->partial_symtabs;
5264
927aa2e7
JK
5265 return true;
5266}
5267
927aa2e7
JK
5268/* Type used to manage iterating over all CUs looking for a symbol for
5269 .debug_names. */
5270
5271class dw2_debug_names_iterator
5272{
5273public:
927aa2e7 5274 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5275 gdb::optional<block_enum> block_index,
5276 domain_enum domain,
fcf23d5b 5277 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5278 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5279 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5280 m_per_objfile (per_objfile)
927aa2e7
JK
5281 {}
5282
5283 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5284 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5285 : m_map (map),
5286 m_search (search),
fcf23d5b
SM
5287 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5288 m_per_objfile (per_objfile)
927aa2e7
JK
5289 {}
5290
3b00ef10
TT
5291 dw2_debug_names_iterator (const mapped_debug_names &map,
5292 block_enum block_index, domain_enum domain,
fcf23d5b 5293 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5294 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5295 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5296 m_per_objfile (per_objfile)
3b00ef10
TT
5297 {}
5298
927aa2e7
JK
5299 /* Return the next matching CU or NULL if there are no more. */
5300 dwarf2_per_cu_data *next ();
5301
5302private:
5303 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5304 const char *name,
5305 dwarf2_per_objfile *per_objfile);
927aa2e7 5306 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5307 uint32_t namei,
5308 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5309
5310 /* The internalized form of .debug_names. */
5311 const mapped_debug_names &m_map;
5312
2b79f376
SM
5313 /* If set, only look for symbols that match that block. Valid values are
5314 GLOBAL_BLOCK and STATIC_BLOCK. */
5315 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5316
5317 /* The kind of symbol we're looking for. */
5318 const domain_enum m_domain = UNDEF_DOMAIN;
5319 const search_domain m_search = ALL_DOMAIN;
5320
5321 /* The list of CUs from the index entry of the symbol, or NULL if
5322 not found. */
5323 const gdb_byte *m_addr;
fcf23d5b
SM
5324
5325 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5326};
5327
5328const char *
fcf23d5b 5329mapped_debug_names::namei_to_name
976ca316 5330 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5331{
5332 const ULONGEST namei_string_offs
5333 = extract_unsigned_integer ((name_table_string_offs_reordered
5334 + namei * offset_size),
5335 offset_size,
5336 dwarf5_byte_order);
976ca316 5337 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5338}
5339
5340/* Find a slot in .debug_names for the object named NAME. If NAME is
5341 found, return pointer to its pool data. If NAME cannot be found,
5342 return NULL. */
5343
5344const gdb_byte *
5345dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5346 (const mapped_debug_names &map, const char *name,
5347 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5348{
5349 int (*cmp) (const char *, const char *);
5350
54ee4252 5351 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5352 if (current_language->la_language == language_cplus
5353 || current_language->la_language == language_fortran
5354 || current_language->la_language == language_d)
5355 {
5356 /* NAME is already canonical. Drop any qualifiers as
5357 .debug_names does not contain any. */
5358
5359 if (strchr (name, '(') != NULL)
5360 {
54ee4252 5361 without_params = cp_remove_params (name);
927aa2e7 5362 if (without_params != NULL)
54ee4252 5363 name = without_params.get ();
927aa2e7
JK
5364 }
5365 }
5366
5367 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5368
5369 const uint32_t full_hash = dwarf5_djb_hash (name);
5370 uint32_t namei
5371 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5372 (map.bucket_table_reordered
5373 + (full_hash % map.bucket_count)), 4,
5374 map.dwarf5_byte_order);
5375 if (namei == 0)
5376 return NULL;
5377 --namei;
5378 if (namei >= map.name_count)
5379 {
b98664d3 5380 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5381 "[in module %s]"),
5382 namei, map.name_count,
fcf23d5b 5383 objfile_name (per_objfile->objfile));
927aa2e7
JK
5384 return NULL;
5385 }
5386
5387 for (;;)
5388 {
5389 const uint32_t namei_full_hash
5390 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5391 (map.hash_table_reordered + namei), 4,
5392 map.dwarf5_byte_order);
5393 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5394 return NULL;
5395
5396 if (full_hash == namei_full_hash)
5397 {
fcf23d5b 5398 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5399
5400#if 0 /* An expensive sanity check. */
5401 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5402 {
b98664d3 5403 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5404 "[in module %s]"),
5405 namei, objfile_name (dwarf2_per_objfile->objfile));
5406 return NULL;
5407 }
5408#endif
5409
5410 if (cmp (namei_string, name) == 0)
5411 {
5412 const ULONGEST namei_entry_offs
5413 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5414 + namei * map.offset_size),
5415 map.offset_size, map.dwarf5_byte_order);
5416 return map.entry_pool + namei_entry_offs;
5417 }
5418 }
5419
5420 ++namei;
5421 if (namei >= map.name_count)
5422 return NULL;
5423 }
5424}
5425
5426const gdb_byte *
5427dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5428 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5429{
5430 if (namei >= map.name_count)
5431 {
b98664d3 5432 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5433 "[in module %s]"),
5434 namei, map.name_count,
fcf23d5b 5435 objfile_name (per_objfile->objfile));
927aa2e7
JK
5436 return NULL;
5437 }
5438
5439 const ULONGEST namei_entry_offs
5440 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5441 + namei * map.offset_size),
5442 map.offset_size, map.dwarf5_byte_order);
5443 return map.entry_pool + namei_entry_offs;
5444}
5445
5446/* See dw2_debug_names_iterator. */
5447
5448dwarf2_per_cu_data *
5449dw2_debug_names_iterator::next ()
5450{
5451 if (m_addr == NULL)
5452 return NULL;
5453
fcf23d5b
SM
5454 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5455 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5456 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5457
5458 again:
5459
5460 unsigned int bytes_read;
5461 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5462 m_addr += bytes_read;
5463 if (abbrev == 0)
5464 return NULL;
5465
5466 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5467 if (indexval_it == m_map.abbrev_map.cend ())
5468 {
b98664d3 5469 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5470 "[in module %s]"),
ed2dc618 5471 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5472 return NULL;
5473 }
5474 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5475 enum class symbol_linkage {
5476 unknown,
5477 static_,
5478 extern_,
23c13d42 5479 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5480 dwarf2_per_cu_data *per_cu = NULL;
5481 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5482 {
5483 ULONGEST ull;
5484 switch (attr.form)
5485 {
5486 case DW_FORM_implicit_const:
5487 ull = attr.implicit_const;
5488 break;
5489 case DW_FORM_flag_present:
5490 ull = 1;
5491 break;
5492 case DW_FORM_udata:
5493 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5494 m_addr += bytes_read;
5495 break;
6dc55ce9 5496 case DW_FORM_ref4:
5497 ull = read_4_bytes (abfd, m_addr);
5498 m_addr += 4;
5499 break;
5500 case DW_FORM_ref8:
5501 ull = read_8_bytes (abfd, m_addr);
5502 m_addr += 8;
5503 break;
5504 case DW_FORM_ref_sig8:
5505 ull = read_8_bytes (abfd, m_addr);
5506 m_addr += 8;
5507 break;
927aa2e7 5508 default:
b98664d3 5509 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5510 dwarf_form_name (attr.form),
ed2dc618 5511 objfile_name (objfile));
927aa2e7
JK
5512 return NULL;
5513 }
5514 switch (attr.dw_idx)
5515 {
5516 case DW_IDX_compile_unit:
5517 /* Don't crash on bad data. */
fcf23d5b 5518 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5519 {
b98664d3 5520 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5521 " [in module %s]"),
5522 pulongest (ull),
fcf23d5b 5523 objfile_name (objfile));
927aa2e7
JK
5524 continue;
5525 }
fcf23d5b 5526 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5527 break;
8af5c486
JK
5528 case DW_IDX_type_unit:
5529 /* Don't crash on bad data. */
fcf23d5b 5530 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5531 {
b98664d3 5532 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5533 " [in module %s]"),
5534 pulongest (ull),
fcf23d5b 5535 objfile_name (objfile));
8af5c486
JK
5536 continue;
5537 }
fcf23d5b 5538 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5539 break;
6dc55ce9 5540 case DW_IDX_die_offset:
5541 /* In a per-CU index (as opposed to a per-module index), index
5542 entries without CU attribute implicitly refer to the single CU. */
5543 if (per_cu == NULL)
fcf23d5b 5544 per_cu = per_bfd->get_cu (0);
6dc55ce9 5545 break;
927aa2e7
JK
5546 case DW_IDX_GNU_internal:
5547 if (!m_map.augmentation_is_gdb)
5548 break;
23c13d42 5549 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5550 break;
5551 case DW_IDX_GNU_external:
5552 if (!m_map.augmentation_is_gdb)
5553 break;
23c13d42 5554 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5555 break;
5556 }
5557 }
5558
5559 /* Skip if already read in. */
fcf23d5b 5560 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5561 goto again;
5562
5563 /* Check static vs global. */
23c13d42 5564 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5565 {
2b79f376 5566 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5567 const bool symbol_is_static =
5568 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5569 if (want_static != symbol_is_static)
2b79f376 5570 goto again;
927aa2e7
JK
5571 }
5572
5573 /* Match dw2_symtab_iter_next, symbol_kind
5574 and debug_names::psymbol_tag. */
5575 switch (m_domain)
5576 {
5577 case VAR_DOMAIN:
5578 switch (indexval.dwarf_tag)
5579 {
5580 case DW_TAG_variable:
5581 case DW_TAG_subprogram:
5582 /* Some types are also in VAR_DOMAIN. */
5583 case DW_TAG_typedef:
5584 case DW_TAG_structure_type:
5585 break;
5586 default:
5587 goto again;
5588 }
5589 break;
5590 case STRUCT_DOMAIN:
5591 switch (indexval.dwarf_tag)
5592 {
5593 case DW_TAG_typedef:
5594 case DW_TAG_structure_type:
5595 break;
5596 default:
5597 goto again;
5598 }
5599 break;
5600 case LABEL_DOMAIN:
5601 switch (indexval.dwarf_tag)
5602 {
5603 case 0:
5604 case DW_TAG_variable:
5605 break;
5606 default:
5607 goto again;
5608 }
5609 break;
59c35742
AB
5610 case MODULE_DOMAIN:
5611 switch (indexval.dwarf_tag)
5612 {
5613 case DW_TAG_module:
5614 break;
5615 default:
5616 goto again;
5617 }
5618 break;
927aa2e7
JK
5619 default:
5620 break;
5621 }
5622
5623 /* Match dw2_expand_symtabs_matching, symbol_kind and
5624 debug_names::psymbol_tag. */
5625 switch (m_search)
4b514bc8 5626 {
927aa2e7
JK
5627 case VARIABLES_DOMAIN:
5628 switch (indexval.dwarf_tag)
4b514bc8 5629 {
927aa2e7
JK
5630 case DW_TAG_variable:
5631 break;
5632 default:
5633 goto again;
4b514bc8 5634 }
927aa2e7
JK
5635 break;
5636 case FUNCTIONS_DOMAIN:
5637 switch (indexval.dwarf_tag)
4b514bc8 5638 {
927aa2e7
JK
5639 case DW_TAG_subprogram:
5640 break;
5641 default:
5642 goto again;
4b514bc8 5643 }
927aa2e7
JK
5644 break;
5645 case TYPES_DOMAIN:
5646 switch (indexval.dwarf_tag)
5647 {
5648 case DW_TAG_typedef:
5649 case DW_TAG_structure_type:
5650 break;
5651 default:
5652 goto again;
5653 }
5654 break;
59c35742
AB
5655 case MODULES_DOMAIN:
5656 switch (indexval.dwarf_tag)
5657 {
5658 case DW_TAG_module:
5659 break;
5660 default:
5661 goto again;
5662 }
927aa2e7
JK
5663 default:
5664 break;
4b514bc8 5665 }
927aa2e7
JK
5666
5667 return per_cu;
4b514bc8 5668}
61920122 5669
927aa2e7 5670static struct compunit_symtab *
c7f839cb 5671dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5672 const char *name, domain_enum domain)
4b514bc8 5673{
976ca316 5674 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5675
976ca316 5676 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5677 if (!mapp)
61920122 5678 {
927aa2e7
JK
5679 /* index is NULL if OBJF_READNOW. */
5680 return NULL;
5681 }
5682 const auto &map = *mapp;
9291a0cd 5683
976ca316 5684 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5685
927aa2e7
JK
5686 struct compunit_symtab *stab_best = NULL;
5687 struct dwarf2_per_cu_data *per_cu;
5688 while ((per_cu = iter.next ()) != NULL)
5689 {
5690 struct symbol *sym, *with_opaque = NULL;
97a1449a 5691 compunit_symtab *stab
976ca316 5692 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5693 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5694 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5695
927aa2e7
JK
5696 sym = block_find_symbol (block, name, domain,
5697 block_find_non_opaque_type_preferred,
5698 &with_opaque);
9703b513 5699
927aa2e7
JK
5700 /* Some caution must be observed with overloaded functions and
5701 methods, since the index will not contain any overload
5702 information (but NAME might contain it). */
a3ec0bb1 5703
927aa2e7 5704 if (sym != NULL
987012b8 5705 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5706 return stab;
5707 if (with_opaque != NULL
987012b8 5708 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5709 stab_best = stab;
9703b513 5710
927aa2e7 5711 /* Keep looking through other CUs. */
9703b513
TT
5712 }
5713
927aa2e7 5714 return stab_best;
9703b513
TT
5715}
5716
927aa2e7
JK
5717/* This dumps minimal information about .debug_names. It is called
5718 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5719 uses this to verify that .debug_names has been loaded. */
9291a0cd 5720
927aa2e7
JK
5721static void
5722dw2_debug_names_dump (struct objfile *objfile)
5723{
976ca316 5724 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5725
976ca316 5726 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5727 printf_filtered (".debug_names:");
976ca316 5728 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5729 printf_filtered (" exists\n");
5730 else
5731 printf_filtered (" faked for \"readnow\"\n");
5732 printf_filtered ("\n");
9291a0cd
TT
5733}
5734
9291a0cd 5735static void
927aa2e7
JK
5736dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5737 const char *func_name)
9291a0cd 5738{
976ca316 5739 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5740
976ca316
SM
5741 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5742 if (per_objfile->per_bfd->debug_names_table)
24c79950 5743 {
976ca316 5744 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5745
fcf23d5b 5746 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5747 per_objfile);
24c79950 5748
927aa2e7
JK
5749 struct dwarf2_per_cu_data *per_cu;
5750 while ((per_cu = iter.next ()) != NULL)
976ca316 5751 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5752 }
5753}
24c79950 5754
3b00ef10
TT
5755static void
5756dw2_debug_names_map_matching_symbols
5757 (struct objfile *objfile,
5758 const lookup_name_info &name, domain_enum domain,
5759 int global,
5760 gdb::function_view<symbol_found_callback_ftype> callback,
5761 symbol_compare_ftype *ordered_compare)
5762{
976ca316 5763 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5764
5765 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5766 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5767 return;
5768
976ca316 5769 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5770 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5771
5772 const char *match_name = name.ada ().lookup_name ().c_str ();
5773 auto matcher = [&] (const char *symname)
5774 {
5775 if (ordered_compare == nullptr)
5776 return true;
5777 return ordered_compare (symname, match_name) == 0;
5778 };
5779
5780 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5781 [&] (offset_type namei)
5782 {
5783 /* The name was matched, now expand corresponding CUs that were
5784 marked. */
fcf23d5b 5785 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5786 per_objfile);
3b00ef10
TT
5787
5788 struct dwarf2_per_cu_data *per_cu;
5789 while ((per_cu = iter.next ()) != NULL)
976ca316 5790 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5791 nullptr);
3b00ef10 5792 return true;
976ca316 5793 }, per_objfile);
3b00ef10
TT
5794
5795 /* It's a shame we couldn't do this inside the
5796 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5797 that have already been expanded. Instead, this loop matches what
5798 the psymtab code does. */
976ca316 5799 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5800 {
976ca316 5801 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5802 if (symtab != nullptr)
3b00ef10
TT
5803 {
5804 const struct block *block
af758d11 5805 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5806 if (!iterate_over_symbols_terminated (block, name,
5807 domain, callback))
5808 break;
5809 }
5810 }
5811}
5812
927aa2e7
JK
5813static void
5814dw2_debug_names_expand_symtabs_matching
5815 (struct objfile *objfile,
5816 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5817 const lookup_name_info *lookup_name,
927aa2e7
JK
5818 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5819 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5820 enum search_domain kind)
5821{
976ca316 5822 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5823
927aa2e7 5824 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5825 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5826 return;
9291a0cd 5827
976ca316 5828 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5829
c1a66c06
TV
5830 if (symbol_matcher == NULL && lookup_name == NULL)
5831 {
976ca316 5832 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5833 {
5834 QUIT;
5835
976ca316
SM
5836 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5837 expansion_notify);
c1a66c06
TV
5838 }
5839 return;
5840 }
5841
976ca316 5842 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5843
c1a66c06 5844 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5845 symbol_matcher,
5846 kind, [&] (offset_type namei)
927aa2e7 5847 {
927aa2e7
JK
5848 /* The name was matched, now expand corresponding CUs that were
5849 marked. */
976ca316 5850 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5851
927aa2e7
JK
5852 struct dwarf2_per_cu_data *per_cu;
5853 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5854 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5855 expansion_notify);
3b00ef10 5856 return true;
976ca316 5857 }, per_objfile);
9291a0cd
TT
5858}
5859
927aa2e7 5860const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5861{
5862 dw2_has_symbols,
5863 dw2_find_last_source_symtab,
5864 dw2_forget_cached_source_info,
f8eba3c6 5865 dw2_map_symtabs_matching_filename,
927aa2e7 5866 dw2_debug_names_lookup_symbol,
d3214198 5867 NULL,
9291a0cd 5868 dw2_print_stats,
927aa2e7 5869 dw2_debug_names_dump,
927aa2e7 5870 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5871 dw2_expand_all_symtabs,
652a8996 5872 dw2_expand_symtabs_with_fullname,
3b00ef10 5873 dw2_debug_names_map_matching_symbols,
927aa2e7 5874 dw2_debug_names_expand_symtabs_matching,
43f3e411 5875 dw2_find_pc_sect_compunit_symtab,
71a3c369 5876 NULL,
9291a0cd
TT
5877 dw2_map_symbol_filenames
5878};
5879
4485a1c1 5880/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5881 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5882
5883template <typename T>
5884static gdb::array_view<const gdb_byte>
5885get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5886{
5887 dwarf2_section_info *section = &section_owner->gdb_index;
5888
96b79293 5889 if (section->empty ())
4485a1c1
SM
5890 return {};
5891
5892 /* Older elfutils strip versions could keep the section in the main
5893 executable while splitting it for the separate debug info file. */
96b79293 5894 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5895 return {};
5896
96b79293 5897 section->read (obj);
4485a1c1 5898
8bebfcda
PA
5899 /* dwarf2_section_info::size is a bfd_size_type, while
5900 gdb::array_view works with size_t. On 32-bit hosts, with
5901 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5902 is 32-bit. So we need an explicit narrowing conversion here.
5903 This is fine, because it's impossible to allocate or mmap an
5904 array/buffer larger than what size_t can represent. */
5905 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5906}
5907
87d6a7aa
SM
5908/* Lookup the index cache for the contents of the index associated to
5909 DWARF2_OBJ. */
5910
5911static gdb::array_view<const gdb_byte>
5989a64e 5912get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5913{
5914 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5915 if (build_id == nullptr)
5916 return {};
5917
5918 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5919 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5920}
5921
5922/* Same as the above, but for DWZ. */
5923
5924static gdb::array_view<const gdb_byte>
5925get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5926{
5927 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5928 if (build_id == nullptr)
5929 return {};
5930
5931 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5932}
5933
3c0aa29a 5934/* See symfile.h. */
9291a0cd 5935
3c0aa29a
PA
5936bool
5937dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5938{
976ca316
SM
5939 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5940 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5941
9291a0cd
TT
5942 /* If we're about to read full symbols, don't bother with the
5943 indices. In this case we also don't care if some other debug
5944 format is making psymtabs, because they are all about to be
5945 expanded anyway. */
5946 if ((objfile->flags & OBJF_READNOW))
5947 {
17ee85fc
TT
5948 /* When using READNOW, the using_index flag (set below) indicates that
5949 PER_BFD was already initialized, when we loaded some other objfile. */
5950 if (per_bfd->using_index)
5951 {
5952 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 5953 per_objfile->resize_symtabs ();
17ee85fc
TT
5954 return true;
5955 }
5956
5957 per_bfd->using_index = 1;
976ca316
SM
5958 create_all_comp_units (per_objfile);
5959 create_all_type_units (per_objfile);
17ee85fc
TT
5960 per_bfd->quick_file_names_table
5961 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5962 per_objfile->resize_symtabs ();
9291a0cd 5963
17ee85fc
TT
5964 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5965 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 5966 {
17ee85fc 5967 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 5968
17ee85fc 5969 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5970 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5971 }
5972
5973 /* Return 1 so that gdb sees the "quick" functions. However,
5974 these functions will be no-ops because we will have expanded
5975 all symtabs. */
3c0aa29a
PA
5976 *index_kind = dw_index_kind::GDB_INDEX;
5977 return true;
9291a0cd
TT
5978 }
5979
17ee85fc
TT
5980 /* Was a debug names index already read when we processed an objfile sharing
5981 PER_BFD? */
5982 if (per_bfd->debug_names_table != nullptr)
5983 {
5984 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 5985 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 5986 per_objfile->resize_symtabs ();
17ee85fc
TT
5987 return true;
5988 }
5989
5990 /* Was a GDB index already read when we processed an objfile sharing
5991 PER_BFD? */
5992 if (per_bfd->index_table != nullptr)
5993 {
5994 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 5995 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 5996 per_objfile->resize_symtabs ();
17ee85fc
TT
5997 return true;
5998 }
5999
976ca316 6000 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
6001 {
6002 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6003 per_objfile->resize_symtabs ();
3c0aa29a
PA
6004 return true;
6005 }
927aa2e7 6006
976ca316 6007 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6008 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6009 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6010 {
6011 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6012 per_objfile->resize_symtabs ();
3c0aa29a
PA
6013 return true;
6014 }
9291a0cd 6015
87d6a7aa 6016 /* ... otherwise, try to find the index in the index cache. */
976ca316 6017 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6018 get_gdb_index_contents_from_cache,
6019 get_gdb_index_contents_from_cache_dwz))
6020 {
6021 global_index_cache.hit ();
6022 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6023 per_objfile->resize_symtabs ();
87d6a7aa
SM
6024 return true;
6025 }
6026
6027 global_index_cache.miss ();
3c0aa29a 6028 return false;
9291a0cd
TT
6029}
6030
6031\f
6032
dce234bc
PP
6033/* Build a partial symbol table. */
6034
6035void
f29dff0a 6036dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6037{
976ca316
SM
6038 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6039 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6040
6041 if (per_bfd->partial_symtabs != nullptr)
6042 {
6043 /* Partial symbols were already read, so now we can simply
6044 attach them. */
6045 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6046 per_objfile->resize_symtabs ();
17ee85fc
TT
6047 return;
6048 }
c9bf0622 6049
6eee24ce 6050 init_psymbol_list (objfile, 1024);
c906108c 6051
a70b8144 6052 try
c9bf0622
TT
6053 {
6054 /* This isn't really ideal: all the data we allocate on the
6055 objfile's obstack is still uselessly kept around. However,
6056 freeing it seems unsafe. */
906768f9 6057 psymtab_discarder psymtabs (objfile);
976ca316 6058 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6059 psymtabs.keep ();
87d6a7aa 6060
976ca316 6061 per_objfile->resize_symtabs ();
af758d11 6062
87d6a7aa 6063 /* (maybe) store an index in the cache. */
976ca316 6064 global_index_cache.store (per_objfile);
c9bf0622 6065 }
230d2906 6066 catch (const gdb_exception_error &except)
492d29ea
PA
6067 {
6068 exception_print (gdb_stderr, except);
6069 }
17ee85fc
TT
6070
6071 /* Finish by setting the local reference to partial symtabs, so that
6072 we don't try to read them again if reading another objfile with the same
6073 BFD. If we can't in fact share, this won't make a difference anyway as
6074 the dwarf2_per_bfd object won't be shared. */
6075 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6076}
c906108c 6077
3b80fe9b
DE
6078/* Find the base address of the compilation unit for range lists and
6079 location lists. It will normally be specified by DW_AT_low_pc.
6080 In DWARF-3 draft 4, the base address could be overridden by
6081 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6082 compilation units with discontinuous ranges. */
6083
6084static void
6085dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6086{
6087 struct attribute *attr;
6088
2b24b6e4 6089 cu->base_address.reset ();
3b80fe9b
DE
6090
6091 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6092 if (attr != nullptr)
2b24b6e4 6093 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6094 else
6095 {
6096 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6097 if (attr != nullptr)
2b24b6e4 6098 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6099 }
6100}
6101
36586728
TT
6102/* Helper function that returns the proper abbrev section for
6103 THIS_CU. */
6104
6105static struct dwarf2_section_info *
6106get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6107{
6108 struct dwarf2_section_info *abbrev;
c3699833 6109 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6110
6111 if (this_cu->is_dwz)
c3699833 6112 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6113 else
c3699833 6114 abbrev = &per_bfd->abbrev;
36586728
TT
6115
6116 return abbrev;
6117}
6118
f4dc4d17
DE
6119/* Fetch the abbreviation table offset from a comp or type unit header. */
6120
6121static sect_offset
976ca316 6122read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6123 struct dwarf2_section_info *section,
9c541725 6124 sect_offset sect_off)
f4dc4d17 6125{
96b79293 6126 bfd *abfd = section->get_bfd_owner ();
d521ce57 6127 const gdb_byte *info_ptr;
ac298888 6128 unsigned int initial_length_size, offset_size;
43988095 6129 uint16_t version;
f4dc4d17 6130
976ca316 6131 section->read (per_objfile->objfile);
9c541725 6132 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6133 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6134 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6135 info_ptr += initial_length_size;
6136
6137 version = read_2_bytes (abfd, info_ptr);
6138 info_ptr += 2;
6139 if (version >= 5)
6140 {
6141 /* Skip unit type and address size. */
6142 info_ptr += 2;
6143 }
6144
24aa364d 6145 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6146}
6147
b83470bf
TT
6148/* A partial symtab that is used only for include files. */
6149struct dwarf2_include_psymtab : public partial_symtab
6150{
6151 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6152 : partial_symtab (filename, objfile)
6153 {
6154 }
6155
6156 void read_symtab (struct objfile *objfile) override
6157 {
194d088f
TV
6158 /* It's an include file, no symbols to read for it.
6159 Everything is in the includer symtab. */
6160
6161 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6162 expansion of the includer psymtab. We use the dependencies[0] field to
6163 model the includer. But if we go the regular route of calling
6164 expand_psymtab here, and having expand_psymtab call expand_dependencies
6165 to expand the includer, we'll only use expand_psymtab on the includer
6166 (making it a non-toplevel psymtab), while if we expand the includer via
6167 another path, we'll use read_symtab (making it a toplevel psymtab).
6168 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6169 psymtab, and trigger read_symtab on the includer here directly. */
6170 includer ()->read_symtab (objfile);
b83470bf
TT
6171 }
6172
6173 void expand_psymtab (struct objfile *objfile) override
6174 {
194d088f
TV
6175 /* This is not called by read_symtab, and should not be called by any
6176 expand_dependencies. */
6177 gdb_assert (false);
b83470bf
TT
6178 }
6179
5717c425 6180 bool readin_p (struct objfile *objfile) const override
b83470bf 6181 {
5717c425 6182 return includer ()->readin_p (objfile);
b83470bf
TT
6183 }
6184
5717c425 6185 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6186 {
6187 return nullptr;
6188 }
6189
6190private:
194d088f
TV
6191 partial_symtab *includer () const
6192 {
6193 /* An include psymtab has exactly one dependency: the psymtab that
6194 includes it. */
6195 gdb_assert (this->number_of_dependencies == 1);
6196 return this->dependencies[0];
6197 }
b83470bf
TT
6198};
6199
aaa75496
JB
6200/* Allocate a new partial symtab for file named NAME and mark this new
6201 partial symtab as being an include of PST. */
6202
6203static void
891813be 6204dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6205 struct objfile *objfile)
6206{
b83470bf 6207 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6208
fbd9ab74 6209 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6210 subpst->dirname = pst->dirname;
fbd9ab74 6211
a9342b62 6212 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6213 subpst->dependencies[0] = pst;
6214 subpst->number_of_dependencies = 1;
aaa75496
JB
6215}
6216
6217/* Read the Line Number Program data and extract the list of files
6218 included by the source file represented by PST. Build an include
d85a05f0 6219 partial symtab for each of these included files. */
aaa75496
JB
6220
6221static void
6222dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6223 struct die_info *die,
891813be 6224 dwarf2_psymtab *pst)
aaa75496 6225{
fff8551c 6226 line_header_up lh;
d85a05f0 6227 struct attribute *attr;
aaa75496 6228
d85a05f0 6229 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6230 if (attr != nullptr)
9c541725 6231 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6232 if (lh == NULL)
6233 return; /* No linetable, so no includes. */
6234
79748972
TT
6235 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6236 that we pass in the raw text_low here; that is ok because we're
6237 only decoding the line table to make include partial symtabs, and
6238 so the addresses aren't really used. */
4ae976d1 6239 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6240 pst->raw_text_low (), 1);
aaa75496
JB
6241}
6242
348e048f 6243static hashval_t
52dc124a 6244hash_signatured_type (const void *item)
348e048f 6245{
9a3c8263
SM
6246 const struct signatured_type *sig_type
6247 = (const struct signatured_type *) item;
9a619af0 6248
348e048f 6249 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6250 return sig_type->signature;
348e048f
DE
6251}
6252
6253static int
52dc124a 6254eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6255{
9a3c8263
SM
6256 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6257 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6258
348e048f
DE
6259 return lhs->signature == rhs->signature;
6260}
6261
1fd400ff
TT
6262/* Allocate a hash table for signatured types. */
6263
b0b6a987 6264static htab_up
298e9637 6265allocate_signatured_type_table ()
1fd400ff 6266{
b0b6a987
TT
6267 return htab_up (htab_create_alloc (41,
6268 hash_signatured_type,
6269 eq_signatured_type,
6270 NULL, xcalloc, xfree));
1fd400ff
TT
6271}
6272
d467dd73 6273/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6274
6275static int
d467dd73 6276add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6277{
9a3c8263 6278 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6279 std::vector<signatured_type *> *all_type_units
6280 = (std::vector<signatured_type *> *) datum;
1fd400ff 6281
b2bdb8cf 6282 all_type_units->push_back (sigt);
1fd400ff
TT
6283
6284 return 1;
6285}
6286
78d4d2c5 6287/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6288 and fill them into TYPES_HTAB. It will process only type units,
6289 therefore DW_UT_type. */
c88ee1f0 6290
78d4d2c5 6291static void
976ca316 6292create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6293 struct dwo_file *dwo_file,
b0b6a987 6294 dwarf2_section_info *section, htab_up &types_htab,
43988095 6295 rcuh_kind section_kind)
348e048f 6296{
976ca316 6297 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6298 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6299 bfd *abfd;
6300 const gdb_byte *info_ptr, *end_ptr;
348e048f 6301
4bdcc0c1
DE
6302 abbrev_section = (dwo_file != NULL
6303 ? &dwo_file->sections.abbrev
976ca316 6304 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6305
b4f54984 6306 if (dwarf_read_debug)
43988095 6307 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6308 section->get_name (),
6309 abbrev_section->get_file_name ());
09406207 6310
96b79293 6311 section->read (objfile);
78d4d2c5 6312 info_ptr = section->buffer;
348e048f 6313
78d4d2c5
JK
6314 if (info_ptr == NULL)
6315 return;
348e048f 6316
78d4d2c5
JK
6317 /* We can't set abfd until now because the section may be empty or
6318 not present, in which case the bfd is unknown. */
96b79293 6319 abfd = section->get_bfd_owner ();
348e048f 6320
c0ab21c2
TT
6321 /* We don't use cutu_reader here because we don't need to read
6322 any dies: the signature is in the header. */
3019eac3 6323
78d4d2c5
JK
6324 end_ptr = info_ptr + section->size;
6325 while (info_ptr < end_ptr)
6326 {
78d4d2c5
JK
6327 struct signatured_type *sig_type;
6328 struct dwo_unit *dwo_tu;
6329 void **slot;
6330 const gdb_byte *ptr = info_ptr;
6331 struct comp_unit_head header;
6332 unsigned int length;
8b70b953 6333
9c541725 6334 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6335
a49dd8dd
JK
6336 /* Initialize it due to a false compiler warning. */
6337 header.signature = -1;
9c541725 6338 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6339
78d4d2c5
JK
6340 /* We need to read the type's signature in order to build the hash
6341 table, but we don't need anything else just yet. */
348e048f 6342
976ca316 6343 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6344 abbrev_section, ptr, section_kind);
348e048f 6345
4057dfde 6346 length = header.get_length ();
6caca83c 6347
78d4d2c5
JK
6348 /* Skip dummy type units. */
6349 if (ptr >= info_ptr + length
43988095
JK
6350 || peek_abbrev_code (abfd, ptr) == 0
6351 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6352 {
6353 info_ptr += length;
6354 continue;
6355 }
dee91e82 6356
78d4d2c5
JK
6357 if (types_htab == NULL)
6358 {
6359 if (dwo_file)
298e9637 6360 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6361 else
298e9637 6362 types_htab = allocate_signatured_type_table ();
78d4d2c5 6363 }
8b70b953 6364
78d4d2c5
JK
6365 if (dwo_file)
6366 {
6367 sig_type = NULL;
976ca316 6368 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6369 dwo_tu->dwo_file = dwo_file;
43988095 6370 dwo_tu->signature = header.signature;
9c541725 6371 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6372 dwo_tu->section = section;
9c541725 6373 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6374 dwo_tu->length = length;
6375 }
6376 else
6377 {
6378 /* N.B.: type_offset is not usable if this type uses a DWO file.
6379 The real type_offset is in the DWO file. */
6380 dwo_tu = NULL;
976ca316 6381 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6382 sig_type->signature = header.signature;
9c541725 6383 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6384 sig_type->per_cu.is_debug_types = 1;
6385 sig_type->per_cu.section = section;
9c541725 6386 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6387 sig_type->per_cu.length = length;
6388 }
6389
b0b6a987 6390 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6391 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6392 INSERT);
6393 gdb_assert (slot != NULL);
6394 if (*slot != NULL)
6395 {
9c541725 6396 sect_offset dup_sect_off;
0349ea22 6397
3019eac3
DE
6398 if (dwo_file)
6399 {
78d4d2c5
JK
6400 const struct dwo_unit *dup_tu
6401 = (const struct dwo_unit *) *slot;
6402
9c541725 6403 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6404 }
6405 else
6406 {
78d4d2c5
JK
6407 const struct signatured_type *dup_tu
6408 = (const struct signatured_type *) *slot;
6409
9c541725 6410 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6411 }
8b70b953 6412
b98664d3 6413 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6414 " the entry at offset %s, signature %s"),
6415 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6416 hex_string (header.signature));
78d4d2c5
JK
6417 }
6418 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6419
78d4d2c5 6420 if (dwarf_read_debug > 1)
9d8780f0
SM
6421 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6422 sect_offset_str (sect_off),
43988095 6423 hex_string (header.signature));
3019eac3 6424
78d4d2c5
JK
6425 info_ptr += length;
6426 }
6427}
3019eac3 6428
78d4d2c5
JK
6429/* Create the hash table of all entries in the .debug_types
6430 (or .debug_types.dwo) section(s).
6431 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6432 otherwise it is NULL.
b3c8eb43 6433
78d4d2c5 6434 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6435
78d4d2c5 6436 Note: This function processes DWO files only, not DWP files. */
348e048f 6437
78d4d2c5 6438static void
976ca316 6439create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6440 struct dwo_file *dwo_file,
fd5866f6 6441 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6442 htab_up &types_htab)
78d4d2c5 6443{
fd5866f6 6444 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6445 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6446 rcuh_kind::TYPE);
3019eac3
DE
6447}
6448
6449/* Create the hash table of all entries in the .debug_types section,
6450 and initialize all_type_units.
6451 The result is zero if there is an error (e.g. missing .debug_types section),
6452 otherwise non-zero. */
6453
6454static int
976ca316 6455create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6456{
b0b6a987 6457 htab_up types_htab;
3019eac3 6458
976ca316
SM
6459 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6460 types_htab, rcuh_kind::COMPILE);
6461 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6462 types_htab);
3019eac3
DE
6463 if (types_htab == NULL)
6464 {
976ca316 6465 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6466 return 0;
6467 }
6468
976ca316 6469 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6470
976ca316
SM
6471 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6472 per_objfile->per_bfd->all_type_units.reserve
6473 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6474
976ca316 6475 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6476 add_signatured_type_cu_to_table,
976ca316 6477 &per_objfile->per_bfd->all_type_units);
1fd400ff 6478
348e048f
DE
6479 return 1;
6480}
6481
5989a64e 6482/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6483 If SLOT is non-NULL, it is the entry to use in the hash table.
6484 Otherwise we find one. */
6485
6486static struct signatured_type *
976ca316 6487add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6488{
976ca316
SM
6489 if (per_objfile->per_bfd->all_type_units.size ()
6490 == per_objfile->per_bfd->all_type_units.capacity ())
6491 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6492
976ca316 6493 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6494
976ca316 6495 per_objfile->resize_symtabs ();
af758d11 6496
976ca316 6497 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6498 sig_type->signature = sig;
6499 sig_type->per_cu.is_debug_types = 1;
976ca316 6500 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6501 {
6502 sig_type->per_cu.v.quick =
976ca316 6503 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6504 struct dwarf2_per_cu_quick_data);
6505 }
6506
6507 if (slot == NULL)
6508 {
976ca316 6509 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6510 sig_type, INSERT);
6511 }
6512 gdb_assert (*slot == NULL);
6513 *slot = sig_type;
6514 /* The rest of sig_type must be filled in by the caller. */
6515 return sig_type;
6516}
6517
a2ce51a0
DE
6518/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6519 Fill in SIG_ENTRY with DWO_ENTRY. */
6520
6521static void
976ca316 6522fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6523 struct signatured_type *sig_entry,
6524 struct dwo_unit *dwo_entry)
6525{
976ca316 6526 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6527
7ee85ab1 6528 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6529 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6530 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6531 if (per_bfd->using_index)
6aa5f3a6
DE
6532 {
6533 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6534 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6535 }
6536 else
6537 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6538 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6539 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6540 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6541 gdb_assert (sig_entry->dwo_unit == NULL);
6542
6543 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6544 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6545 sig_entry->per_cu.length = dwo_entry->length;
6546 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6547 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6548 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6549 sig_entry->dwo_unit = dwo_entry;
6550}
6551
6552/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6553 If we haven't read the TU yet, create the signatured_type data structure
6554 for a TU to be read in directly from a DWO file, bypassing the stub.
6555 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6556 using .gdb_index, then when reading a CU we want to stay in the DWO file
6557 containing that CU. Otherwise we could end up reading several other DWO
6558 files (due to comdat folding) to process the transitive closure of all the
6559 mentioned TUs, and that can be slow. The current DWO file will have every
6560 type signature that it needs.
a2ce51a0
DE
6561 We only do this for .gdb_index because in the psymtab case we already have
6562 to read all the DWOs to build the type unit groups. */
6563
6564static struct signatured_type *
6565lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6566{
976ca316 6567 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6568 struct dwo_file *dwo_file;
6569 struct dwo_unit find_dwo_entry, *dwo_entry;
6570 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6571 void **slot;
a2ce51a0 6572
976ca316 6573 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6574
6aa5f3a6
DE
6575 /* If TU skeletons have been removed then we may not have read in any
6576 TUs yet. */
976ca316
SM
6577 if (per_objfile->per_bfd->signatured_types == NULL)
6578 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6579
6580 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6581 Use the global signatured_types array to do our own comdat-folding
6582 of types. If this is the first time we're reading this TU, and
6583 the TU has an entry in .gdb_index, replace the recorded data from
6584 .gdb_index with this TU. */
a2ce51a0 6585
a2ce51a0 6586 find_sig_entry.signature = sig;
976ca316 6587 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6588 &find_sig_entry, INSERT);
9a3c8263 6589 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6590
6591 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6592 read. Don't reassign the global entry to point to this DWO if that's
6593 the case. Also note that if the TU is already being read, it may not
6594 have come from a DWO, the program may be a mix of Fission-compiled
6595 code and non-Fission-compiled code. */
6596
6597 /* Have we already tried to read this TU?
6598 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6599 needn't exist in the global table yet). */
6600 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6601 return sig_entry;
6602
6aa5f3a6
DE
6603 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6604 dwo_unit of the TU itself. */
6605 dwo_file = cu->dwo_unit->dwo_file;
6606
a2ce51a0
DE
6607 /* Ok, this is the first time we're reading this TU. */
6608 if (dwo_file->tus == NULL)
6609 return NULL;
6610 find_dwo_entry.signature = sig;
b0b6a987
TT
6611 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6612 &find_dwo_entry);
a2ce51a0
DE
6613 if (dwo_entry == NULL)
6614 return NULL;
6615
6aa5f3a6
DE
6616 /* If the global table doesn't have an entry for this TU, add one. */
6617 if (sig_entry == NULL)
976ca316 6618 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6619
976ca316 6620 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6621 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6622 return sig_entry;
6623}
6624
a2ce51a0
DE
6625/* Subroutine of lookup_signatured_type.
6626 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6627 then try the DWP file. If the TU stub (skeleton) has been removed then
6628 it won't be in .gdb_index. */
a2ce51a0
DE
6629
6630static struct signatured_type *
6631lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6632{
976ca316
SM
6633 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6634 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6635 struct dwo_unit *dwo_entry;
6636 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6637 void **slot;
a2ce51a0 6638
976ca316 6639 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6640 gdb_assert (dwp_file != NULL);
6641
6aa5f3a6
DE
6642 /* If TU skeletons have been removed then we may not have read in any
6643 TUs yet. */
976ca316
SM
6644 if (per_objfile->per_bfd->signatured_types == NULL)
6645 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6646
6aa5f3a6 6647 find_sig_entry.signature = sig;
976ca316 6648 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6649 &find_sig_entry, INSERT);
9a3c8263 6650 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6651
6652 /* Have we already tried to read this TU?
6653 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6654 needn't exist in the global table yet). */
6655 if (sig_entry != NULL)
6656 return sig_entry;
6657
a2ce51a0
DE
6658 if (dwp_file->tus == NULL)
6659 return NULL;
976ca316
SM
6660 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6661 1 /* is_debug_types */);
a2ce51a0
DE
6662 if (dwo_entry == NULL)
6663 return NULL;
6664
976ca316
SM
6665 sig_entry = add_type_unit (per_objfile, sig, slot);
6666 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6667
a2ce51a0
DE
6668 return sig_entry;
6669}
6670
380bca97 6671/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6672 Returns NULL if signature SIG is not present in the table.
6673 It is up to the caller to complain about this. */
348e048f
DE
6674
6675static struct signatured_type *
a2ce51a0 6676lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6677{
976ca316 6678 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6679
976ca316 6680 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6681 {
6682 /* We're in a DWO/DWP file, and we're using .gdb_index.
6683 These cases require special processing. */
976ca316 6684 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6685 return lookup_dwo_signatured_type (cu, sig);
6686 else
6687 return lookup_dwp_signatured_type (cu, sig);
6688 }
6689 else
6690 {
6691 struct signatured_type find_entry, *entry;
348e048f 6692
976ca316 6693 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6694 return NULL;
6695 find_entry.signature = sig;
9a3c8263 6696 entry = ((struct signatured_type *)
976ca316 6697 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6698 &find_entry));
a2ce51a0
DE
6699 return entry;
6700 }
348e048f 6701}
18a8505e 6702
42e7ad6c 6703/* Low level DIE reading support. */
348e048f 6704
d85a05f0
DJ
6705/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6706
6707static void
6708init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6709 struct dwarf2_cu *cu,
3019eac3 6710 struct dwarf2_section_info *section,
685af9cd
TT
6711 struct dwo_file *dwo_file,
6712 struct abbrev_table *abbrev_table)
d85a05f0 6713{
fceca515 6714 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6715 reader->abfd = section->get_bfd_owner ();
d85a05f0 6716 reader->cu = cu;
3019eac3 6717 reader->dwo_file = dwo_file;
dee91e82
DE
6718 reader->die_section = section;
6719 reader->buffer = section->buffer;
f664829e 6720 reader->buffer_end = section->buffer + section->size;
685af9cd 6721 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6722}
6723
c0ab21c2 6724/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6725 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6726 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6727 already.
6728
6729 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6730 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6731 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6732 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6733 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6734 STUB_COMP_DIR may be non-NULL.
3e225074 6735 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6736 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6737 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6738 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6739 kept around for at least as long as *RESULT_READER.
6740
b0c7bfa9
DE
6741 The result is non-zero if a valid (non-dummy) DIE was found. */
6742
6743static int
4ab09049 6744read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6745 struct dwo_unit *dwo_unit,
b0c7bfa9 6746 struct die_info *stub_comp_unit_die,
a2ce51a0 6747 const char *stub_comp_dir,
b0c7bfa9 6748 struct die_reader_specs *result_reader,
d521ce57 6749 const gdb_byte **result_info_ptr,
b0c7bfa9 6750 struct die_info **result_comp_unit_die,
685af9cd 6751 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6752{
976ca316 6753 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6754 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6755 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6756 bfd *abfd;
d521ce57 6757 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6758 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6759 int i,num_extra_attrs;
6760 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6761 struct die_info *comp_unit_die;
6762
b0aeadb3
DE
6763 /* At most one of these may be provided. */
6764 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6765
b0c7bfa9
DE
6766 /* These attributes aren't processed until later:
6767 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6768 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6769 referenced later. However, these attributes are found in the stub
6770 which we won't have later. In order to not impose this complication
6771 on the rest of the code, we read them here and copy them to the
6772 DWO CU/TU die. */
b0c7bfa9
DE
6773
6774 stmt_list = NULL;
6775 low_pc = NULL;
6776 high_pc = NULL;
6777 ranges = NULL;
6778 comp_dir = NULL;
6779
6780 if (stub_comp_unit_die != NULL)
6781 {
6782 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6783 DWO file. */
4ab09049 6784 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6785 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6786 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6787 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6788 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6789 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6790
a39fdb41 6791 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6792
18a8505e
AT
6793 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6794 here (if needed). We need the value before we can process
6795 DW_AT_ranges. */
a39fdb41 6796 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6797 }
a2ce51a0
DE
6798 else if (stub_comp_dir != NULL)
6799 {
6800 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6801 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6802 comp_dir->name = DW_AT_comp_dir;
6803 comp_dir->form = DW_FORM_string;
6804 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6805 DW_STRING (comp_dir) = stub_comp_dir;
6806 }
b0c7bfa9
DE
6807
6808 /* Set up for reading the DWO CU/TU. */
6809 cu->dwo_unit = dwo_unit;
685af9cd 6810 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6811 section->read (objfile);
6812 abfd = section->get_bfd_owner ();
9c541725
PA
6813 begin_info_ptr = info_ptr = (section->buffer
6814 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6815 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6816
4ab09049 6817 if (per_cu->is_debug_types)
b0c7bfa9 6818 {
4ab09049 6819 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6820
976ca316
SM
6821 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6822 section, dwo_abbrev_section,
43988095 6823 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6824 /* This is not an assert because it can be caused by bad debug info. */
43988095 6825 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6826 {
6827 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6828 " TU at offset %s [in module %s]"),
a2ce51a0 6829 hex_string (sig_type->signature),
43988095 6830 hex_string (cu->header.signature),
9d8780f0 6831 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6832 bfd_get_filename (abfd));
6833 }
9c541725 6834 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6835 /* For DWOs coming from DWP files, we don't know the CU length
6836 nor the type's offset in the TU until now. */
4057dfde 6837 dwo_unit->length = cu->header.get_length ();
9c541725 6838 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6839
6840 /* Establish the type offset that can be used to lookup the type.
6841 For DWO files, we don't know it until now. */
9c541725
PA
6842 sig_type->type_offset_in_section
6843 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6844 }
6845 else
6846 {
976ca316
SM
6847 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6848 section, dwo_abbrev_section,
43988095 6849 info_ptr, rcuh_kind::COMPILE);
9c541725 6850 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6851 /* For DWOs coming from DWP files, we don't know the CU length
6852 until now. */
4057dfde 6853 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6854 }
6855
685af9cd 6856 *result_dwo_abbrev_table
86de1d91
TT
6857 = abbrev_table::read (objfile, dwo_abbrev_section,
6858 cu->header.abbrev_sect_off);
685af9cd
TT
6859 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6860 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6861
6862 /* Read in the die, but leave space to copy over the attributes
6863 from the stub. This has the benefit of simplifying the rest of
6864 the code - all the work to maintain the illusion of a single
6865 DW_TAG_{compile,type}_unit DIE is done here. */
6866 num_extra_attrs = ((stmt_list != NULL)
6867 + (low_pc != NULL)
6868 + (high_pc != NULL)
6869 + (ranges != NULL)
6870 + (comp_dir != NULL));
6871 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6872 num_extra_attrs);
b0c7bfa9
DE
6873
6874 /* Copy over the attributes from the stub to the DIE we just read in. */
6875 comp_unit_die = *result_comp_unit_die;
6876 i = comp_unit_die->num_attrs;
6877 if (stmt_list != NULL)
6878 comp_unit_die->attrs[i++] = *stmt_list;
6879 if (low_pc != NULL)
6880 comp_unit_die->attrs[i++] = *low_pc;
6881 if (high_pc != NULL)
6882 comp_unit_die->attrs[i++] = *high_pc;
6883 if (ranges != NULL)
6884 comp_unit_die->attrs[i++] = *ranges;
6885 if (comp_dir != NULL)
6886 comp_unit_die->attrs[i++] = *comp_dir;
6887 comp_unit_die->num_attrs += num_extra_attrs;
6888
b4f54984 6889 if (dwarf_die_debug)
bf6af496
DE
6890 {
6891 fprintf_unfiltered (gdb_stdlog,
6892 "Read die from %s@0x%x of %s:\n",
96b79293 6893 section->get_name (),
bf6af496
DE
6894 (unsigned) (begin_info_ptr - section->buffer),
6895 bfd_get_filename (abfd));
b4f54984 6896 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6897 }
6898
b0c7bfa9
DE
6899 /* Skip dummy compilation units. */
6900 if (info_ptr >= begin_info_ptr + dwo_unit->length
6901 || peek_abbrev_code (abfd, info_ptr) == 0)
6902 return 0;
6903
6904 *result_info_ptr = info_ptr;
6905 return 1;
6906}
6907
a084a2a6
AT
6908/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6909 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6910 signature is part of the header. */
6911static gdb::optional<ULONGEST>
6912lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6913{
6914 if (cu->header.version >= 5)
6915 return cu->header.signature;
6916 struct attribute *attr;
6917 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6918 if (attr == nullptr)
6919 return gdb::optional<ULONGEST> ();
6920 return DW_UNSND (attr);
6921}
6922
c0ab21c2 6923/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6924 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6925 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6926
6927static struct dwo_unit *
4ab09049 6928lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6929{
4ab09049 6930 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6931 struct dwo_unit *dwo_unit;
c0ab21c2 6932 const char *comp_dir;
b0c7bfa9 6933
a2ce51a0
DE
6934 gdb_assert (cu != NULL);
6935
b0c7bfa9 6936 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6937 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6938 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6939
4ab09049
SM
6940 if (per_cu->is_debug_types)
6941 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6942 else
6943 {
a084a2a6 6944 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6945
a084a2a6 6946 if (!signature.has_value ())
b0c7bfa9
DE
6947 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6948 " [in module %s]"),
4ab09049
SM
6949 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6950
6951 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6952 }
6953
b0c7bfa9
DE
6954 return dwo_unit;
6955}
6956
c0ab21c2 6957/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6958 See it for a description of the parameters.
fcd3b13d 6959 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6960
c0ab21c2 6961void
9e021579
SM
6962cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6963 dwarf2_per_objfile *per_objfile,
2e671100 6964 dwarf2_cu *existing_cu)
a2ce51a0 6965{
a2ce51a0 6966 struct signatured_type *sig_type;
a2ce51a0
DE
6967
6968 /* Verify we can do the following downcast, and that we have the
6969 data we need. */
6970 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6971 sig_type = (struct signatured_type *) this_cu;
6972 gdb_assert (sig_type->dwo_unit != NULL);
6973
2e671100
SM
6974 dwarf2_cu *cu;
6975
6976 if (existing_cu != nullptr)
6aa5f3a6 6977 {
2e671100
SM
6978 cu = existing_cu;
6979 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6980 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6981 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6982 }
6983 else
6984 {
7188ed02
SM
6985 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6986 in per_objfile yet. */
6987 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6988 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6989 cu = m_new_cu.get ();
6aa5f3a6
DE
6990 }
6991
6992 /* A future optimization, if needed, would be to use an existing
6993 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6994 could share abbrev tables. */
a2ce51a0 6995
2e671100 6996 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6997 NULL /* stub_comp_unit_die */,
6998 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6999 this, &info_ptr,
3e225074 7000 &comp_unit_die,
c0ab21c2 7001 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7002 {
7003 /* Dummy die. */
c0ab21c2 7004 dummy_p = true;
a2ce51a0 7005 }
a2ce51a0
DE
7006}
7007
fd820528 7008/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7009 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7010
f4dc4d17
DE
7011 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7012 Otherwise the table specified in the comp unit header is read in and used.
7013 This is an optimization for when we already have the abbrev table.
7014
2e671100
SM
7015 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7016 allocated. */
aaa75496 7017
ab432490 7018cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7019 dwarf2_per_objfile *per_objfile,
c0ab21c2 7020 struct abbrev_table *abbrev_table,
2e671100 7021 dwarf2_cu *existing_cu,
c0ab21c2
TT
7022 bool skip_partial)
7023 : die_reader_specs {},
6751ebae 7024 m_this_cu (this_cu)
c906108c 7025{
976ca316 7026 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7027 struct dwarf2_section_info *section = this_cu->section;
96b79293 7028 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7029 const gdb_byte *begin_info_ptr;
dee91e82 7030 struct signatured_type *sig_type = NULL;
4bdcc0c1 7031 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7032 /* Non-zero if CU currently points to a DWO file and we need to
7033 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7034 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7035 int rereading_dwo_cu = 0;
c906108c 7036
b4f54984 7037 if (dwarf_die_debug)
9d8780f0 7038 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7039 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7040 sect_offset_str (this_cu->sect_off));
09406207 7041
a2ce51a0
DE
7042 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7043 file (instead of going through the stub), short-circuit all of this. */
7044 if (this_cu->reading_dwo_directly)
7045 {
7046 /* Narrow down the scope of possibilities to have to understand. */
7047 gdb_assert (this_cu->is_debug_types);
7048 gdb_assert (abbrev_table == NULL);
976ca316 7049 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7050 return;
7051 }
7052
dee91e82 7053 /* This is cheap if the section is already read in. */
96b79293 7054 section->read (objfile);
dee91e82 7055
9c541725 7056 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7057
7058 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7059
2e671100
SM
7060 dwarf2_cu *cu;
7061
7062 if (existing_cu != nullptr)
dee91e82 7063 {
2e671100 7064 cu = existing_cu;
42e7ad6c
DE
7065 /* If this CU is from a DWO file we need to start over, we need to
7066 refetch the attributes from the skeleton CU.
7067 This could be optimized by retrieving those attributes from when we
7068 were here the first time: the previous comp_unit_die was stored in
7069 comp_unit_obstack. But there's no data yet that we need this
7070 optimization. */
7071 if (cu->dwo_unit != NULL)
7072 rereading_dwo_cu = 1;
dee91e82
DE
7073 }
7074 else
7075 {
7188ed02
SM
7076 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7077 in per_objfile yet. */
976ca316
SM
7078 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7079 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7080 cu = m_new_cu.get ();
42e7ad6c 7081 }
dee91e82 7082
b0c7bfa9 7083 /* Get the header. */
9c541725 7084 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7085 {
7086 /* We already have the header, there's no need to read it in again. */
9c541725 7087 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7088 }
7089 else
7090 {
3019eac3 7091 if (this_cu->is_debug_types)
dee91e82 7092 {
976ca316
SM
7093 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7094 section, abbrev_section,
7095 info_ptr, rcuh_kind::TYPE);
dee91e82 7096
42e7ad6c
DE
7097 /* Since per_cu is the first member of struct signatured_type,
7098 we can go from a pointer to one to a pointer to the other. */
7099 sig_type = (struct signatured_type *) this_cu;
43988095 7100 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7101 gdb_assert (sig_type->type_offset_in_tu
7102 == cu->header.type_cu_offset_in_tu);
7103 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7104
42e7ad6c
DE
7105 /* LENGTH has not been set yet for type units if we're
7106 using .gdb_index. */
4057dfde 7107 this_cu->length = cu->header.get_length ();
3019eac3
DE
7108
7109 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7110 sig_type->type_offset_in_section =
7111 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7112
7113 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7114 }
7115 else
7116 {
976ca316
SM
7117 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7118 section, abbrev_section,
43988095
JK
7119 info_ptr,
7120 rcuh_kind::COMPILE);
dee91e82 7121
9c541725 7122 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7123 if (this_cu->length == 0)
7124 this_cu->length = cu->header.get_length ();
7125 else
7126 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7127 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7128 }
7129 }
10b3939b 7130
6caca83c 7131 /* Skip dummy compilation units. */
dee91e82 7132 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7133 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7134 {
7135 dummy_p = true;
7136 return;
7137 }
6caca83c 7138
433df2d4
DE
7139 /* If we don't have them yet, read the abbrevs for this compilation unit.
7140 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7141 done. */
f4dc4d17 7142 if (abbrev_table != NULL)
685af9cd
TT
7143 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7144 else
f4dc4d17 7145 {
c0ab21c2 7146 m_abbrev_table_holder
86de1d91
TT
7147 = abbrev_table::read (objfile, abbrev_section,
7148 cu->header.abbrev_sect_off);
c0ab21c2 7149 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7150 }
af703f96 7151
dee91e82 7152 /* Read the top level CU/TU die. */
c0ab21c2 7153 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7154 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7155
58f0c718 7156 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7157 {
7158 dummy_p = true;
7159 return;
7160 }
58f0c718 7161
b0c7bfa9 7162 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7163 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7164 table from the DWO file and pass the ownership over to us. It will be
7165 referenced from READER, so we must make sure to free it after we're done
7166 with READER.
7167
b0c7bfa9
DE
7168 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7169 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7170 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7171 if (dwo_name != nullptr)
3019eac3 7172 {
3019eac3 7173 struct dwo_unit *dwo_unit;
b0c7bfa9 7174 struct die_info *dwo_comp_unit_die;
3019eac3 7175
3e225074 7176 if (comp_unit_die->has_children)
6a506a2d 7177 {
b98664d3 7178 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7179 " has children (offset %s) [in module %s]"),
7180 sect_offset_str (this_cu->sect_off),
7181 bfd_get_filename (abfd));
6a506a2d 7182 }
4ab09049 7183 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7184 if (dwo_unit != NULL)
3019eac3 7185 {
4ab09049 7186 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7187 comp_unit_die, NULL,
c0ab21c2 7188 this, &info_ptr,
3e225074 7189 &dwo_comp_unit_die,
c0ab21c2 7190 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7191 {
7192 /* Dummy die. */
c0ab21c2 7193 dummy_p = true;
6a506a2d
DE
7194 return;
7195 }
7196 comp_unit_die = dwo_comp_unit_die;
7197 }
7198 else
7199 {
7200 /* Yikes, we couldn't find the rest of the DIE, we only have
7201 the stub. A complaint has already been logged. There's
7202 not much more we can do except pass on the stub DIE to
7203 die_reader_func. We don't want to throw an error on bad
7204 debug info. */
3019eac3
DE
7205 }
7206 }
c0ab21c2 7207}
3019eac3 7208
6751ebae
TT
7209void
7210cutu_reader::keep ()
c0ab21c2 7211{
b0c7bfa9 7212 /* Done, clean up. */
6751ebae
TT
7213 gdb_assert (!dummy_p);
7214 if (m_new_cu != NULL)
348e048f 7215 {
7188ed02
SM
7216 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7217 now. */
7218 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7219 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7220 }
dee91e82
DE
7221}
7222
18a8505e
AT
7223/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7224 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7225 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7226
7227 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7228 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7229
7230 We fill in THIS_CU->length.
7231
dee91e82 7232 THIS_CU->cu is always freed when done.
3019eac3 7233 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7234 to care whether it refers to the "main" CU or the DWO CU.
7235
7236 When parent_cu is passed, it is used to provide a default value for
7237 str_offsets_base and addr_base from the parent. */
dee91e82 7238
ab432490 7239cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7240 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7241 struct dwarf2_cu *parent_cu,
7242 struct dwo_file *dwo_file)
7243 : die_reader_specs {},
7244 m_this_cu (this_cu)
dee91e82 7245{
976ca316 7246 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7247 struct dwarf2_section_info *section = this_cu->section;
96b79293 7248 bfd *abfd = section->get_bfd_owner ();
33e80786 7249 struct dwarf2_section_info *abbrev_section;
d521ce57 7250 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7251
b4f54984 7252 if (dwarf_die_debug)
9d8780f0 7253 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7254 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7255 sect_offset_str (this_cu->sect_off));
09406207 7256
976ca316 7257 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7258
33e80786
DE
7259 abbrev_section = (dwo_file != NULL
7260 ? &dwo_file->sections.abbrev
7261 : get_abbrev_section_for_cu (this_cu));
7262
dee91e82 7263 /* This is cheap if the section is already read in. */
96b79293 7264 section->read (objfile);
dee91e82 7265
976ca316 7266 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7267
9c541725 7268 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7269 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7270 section, abbrev_section, info_ptr,
43988095
JK
7271 (this_cu->is_debug_types
7272 ? rcuh_kind::TYPE
7273 : rcuh_kind::COMPILE));
dee91e82 7274
18a8505e
AT
7275 if (parent_cu != nullptr)
7276 {
c0ab21c2
TT
7277 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7278 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7279 }
4057dfde 7280 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7281
7282 /* Skip dummy compilation units. */
7283 if (info_ptr >= begin_info_ptr + this_cu->length
7284 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7285 {
7286 dummy_p = true;
7287 return;
7288 }
72bf9492 7289
c0ab21c2 7290 m_abbrev_table_holder
86de1d91
TT
7291 = abbrev_table::read (objfile, abbrev_section,
7292 m_new_cu->header.abbrev_sect_off);
dee91e82 7293
c0ab21c2
TT
7294 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7295 m_abbrev_table_holder.get ());
3e225074 7296 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7297}
7298
0018ea6f
DE
7299\f
7300/* Type Unit Groups.
dee91e82 7301
0018ea6f
DE
7302 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7303 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7304 so that all types coming from the same compilation (.o file) are grouped
7305 together. A future step could be to put the types in the same symtab as
7306 the CU the types ultimately came from. */
ff013f42 7307
f4dc4d17
DE
7308static hashval_t
7309hash_type_unit_group (const void *item)
7310{
9a3c8263
SM
7311 const struct type_unit_group *tu_group
7312 = (const struct type_unit_group *) item;
f4dc4d17 7313
094b34ac 7314 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7315}
348e048f
DE
7316
7317static int
f4dc4d17 7318eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7319{
9a3c8263
SM
7320 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7321 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7322
094b34ac 7323 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7324}
348e048f 7325
f4dc4d17
DE
7326/* Allocate a hash table for type unit groups. */
7327
eaa5fa8b 7328static htab_up
298e9637 7329allocate_type_unit_groups_table ()
f4dc4d17 7330{
eaa5fa8b
TT
7331 return htab_up (htab_create_alloc (3,
7332 hash_type_unit_group,
7333 eq_type_unit_group,
7334 NULL, xcalloc, xfree));
f4dc4d17 7335}
dee91e82 7336
f4dc4d17
DE
7337/* Type units that don't have DW_AT_stmt_list are grouped into their own
7338 partial symtabs. We combine several TUs per psymtab to not let the size
7339 of any one psymtab grow too big. */
7340#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7341#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7342
094b34ac 7343/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7344 Create the type_unit_group object used to hold one or more TUs. */
7345
7346static struct type_unit_group *
094b34ac 7347create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7348{
976ca316
SM
7349 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7350 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7351 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7352 struct type_unit_group *tu_group;
f4dc4d17 7353
976ca316 7354 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7355 per_cu = &tu_group->per_cu;
1859c670 7356 per_cu->per_bfd = per_bfd;
f4dc4d17 7357
1859c670 7358 if (per_bfd->using_index)
094b34ac 7359 {
1859c670 7360 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7361 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7362 }
7363 else
7364 {
9c541725 7365 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7366 dwarf2_psymtab *pst;
528e1572 7367 std::string name;
094b34ac
DE
7368
7369 /* Give the symtab a useful name for debug purposes. */
7370 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7371 name = string_printf ("<type_units_%d>",
7372 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7373 else
528e1572 7374 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7375
976ca316 7376 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7377 pst->anonymous = true;
094b34ac 7378 }
f4dc4d17 7379
094b34ac 7380 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7381 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7382
7383 return tu_group;
7384}
7385
094b34ac
DE
7386/* Look up the type_unit_group for type unit CU, and create it if necessary.
7387 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7388
7389static struct type_unit_group *
ff39bb5e 7390get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7391{
976ca316
SM
7392 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7393 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7394 struct type_unit_group *tu_group;
7395 void **slot;
7396 unsigned int line_offset;
7397 struct type_unit_group type_unit_group_for_lookup;
7398
976ca316
SM
7399 if (per_objfile->per_bfd->type_unit_groups == NULL)
7400 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7401
7402 /* Do we need to create a new group, or can we use an existing one? */
7403
7404 if (stmt_list)
7405 {
7406 line_offset = DW_UNSND (stmt_list);
7407 ++tu_stats->nr_symtab_sharers;
7408 }
7409 else
7410 {
7411 /* Ugh, no stmt_list. Rare, but we have to handle it.
7412 We can do various things here like create one group per TU or
7413 spread them over multiple groups to split up the expansion work.
7414 To avoid worst case scenarios (too many groups or too large groups)
7415 we, umm, group them in bunches. */
7416 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7417 | (tu_stats->nr_stmt_less_type_units
7418 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7419 ++tu_stats->nr_stmt_less_type_units;
7420 }
7421
094b34ac 7422 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7423 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7424 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7425 &type_unit_group_for_lookup, INSERT);
7426 if (*slot != NULL)
7427 {
9a3c8263 7428 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7429 gdb_assert (tu_group != NULL);
7430 }
7431 else
7432 {
9c541725 7433 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7434 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7435 *slot = tu_group;
7436 ++tu_stats->nr_symtabs;
7437 }
7438
7439 return tu_group;
7440}
0018ea6f
DE
7441\f
7442/* Partial symbol tables. */
7443
7444/* Create a psymtab named NAME and assign it to PER_CU.
7445
7446 The caller must fill in the following details:
7447 dirname, textlow, texthigh. */
7448
891813be 7449static dwarf2_psymtab *
7aa104c4
SM
7450create_partial_symtab (dwarf2_per_cu_data *per_cu,
7451 dwarf2_per_objfile *per_objfile,
7452 const char *name)
0018ea6f 7453{
7aa104c4 7454 struct objfile *objfile = per_objfile->objfile;
891813be 7455 dwarf2_psymtab *pst;
0018ea6f 7456
9f4e76a4 7457 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7458
6d94535f 7459 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7460
7461 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7462 per_cu->v.psymtab = pst;
7463
7464 return pst;
7465}
7466
c0ab21c2 7467/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7468
7469static void
7470process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7471 const gdb_byte *info_ptr,
0018ea6f 7472 struct die_info *comp_unit_die,
c0ab21c2 7473 enum language pretend_language)
0018ea6f
DE
7474{
7475 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7476 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7477 struct objfile *objfile = per_objfile->objfile;
08feed99 7478 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7479 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7480 CORE_ADDR baseaddr;
7481 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7482 dwarf2_psymtab *pst;
3a2b436a 7483 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7484 const char *filename;
0018ea6f 7485
0018ea6f
DE
7486 gdb_assert (! per_cu->is_debug_types);
7487
c0ab21c2 7488 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7489
0018ea6f 7490 /* Allocate a new partial symbol table structure. */
2e927613
TV
7491 gdb::unique_xmalloc_ptr<char> debug_filename;
7492 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7493 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7494 if (filename == NULL)
0018ea6f 7495 filename = "";
2e927613
TV
7496 else if (strcmp (filename, artificial) == 0)
7497 {
7498 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7499 sect_offset_str (per_cu->sect_off),
7500 (char *) NULL));
2e927613
TV
7501 filename = debug_filename.get ();
7502 }
0018ea6f 7503
7aa104c4 7504 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7505
7506 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7507 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7508
b3b3bada 7509 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7510
7511 dwarf2_find_base_address (comp_unit_die, cu);
7512
7513 /* Possibly set the default values of LOWPC and HIGHPC from
7514 `DW_AT_ranges'. */
3a2b436a
JK
7515 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7516 &best_highpc, cu, pst);
7517 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7518 {
7519 CORE_ADDR low
7520 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7521 - baseaddr);
7522 CORE_ADDR high
7523 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7524 - baseaddr - 1);
7525 /* Store the contiguous range if it is not empty; it can be
7526 empty for CUs with no code. */
d320c2b5
TT
7527 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7528 low, high, pst);
79748972 7529 }
0018ea6f
DE
7530
7531 /* Check if comp unit has_children.
7532 If so, read the rest of the partial symbols from this comp unit.
7533 If not, there's no more debug_info for this comp unit. */
3e225074 7534 if (comp_unit_die->has_children)
0018ea6f
DE
7535 {
7536 struct partial_die_info *first_die;
7537 CORE_ADDR lowpc, highpc;
7538
7539 lowpc = ((CORE_ADDR) -1);
7540 highpc = ((CORE_ADDR) 0);
7541
7542 first_die = load_partial_dies (reader, info_ptr, 1);
7543
7544 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7545 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7546
7547 /* If we didn't find a lowpc, set it to highpc to avoid
7548 complaints from `maint check'. */
7549 if (lowpc == ((CORE_ADDR) -1))
7550 lowpc = highpc;
7551
7552 /* If the compilation unit didn't have an explicit address range,
7553 then use the information extracted from its child dies. */
e385593e 7554 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7555 {
7556 best_lowpc = lowpc;
7557 best_highpc = highpc;
7558 }
7559 }
4ae976d1 7560 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7561 best_lowpc + baseaddr)
7562 - baseaddr);
4ae976d1 7563 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7564 best_highpc + baseaddr)
7565 - baseaddr);
0018ea6f 7566
8763cede 7567 end_psymtab_common (objfile, pst);
0018ea6f 7568
ae640021 7569 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7570 {
7571 int i;
ae640021 7572 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7573
7574 /* Fill in 'dependencies' here; we fill in 'users' in a
7575 post-pass. */
7576 pst->number_of_dependencies = len;
a9342b62
TT
7577 pst->dependencies
7578 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7579 for (i = 0; i < len; ++i)
7580 {
7581 pst->dependencies[i]
7582 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7583 }
0018ea6f 7584
ae640021 7585 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7586 }
7587
7588 /* Get the list of files included in the current compilation unit,
7589 and build a psymtab for each of them. */
7590 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7591
b4f54984 7592 if (dwarf_read_debug)
b926417a
TT
7593 fprintf_unfiltered (gdb_stdlog,
7594 "Psymtab for %s unit @%s: %s - %s"
7595 ", %d global, %d static syms\n",
7596 per_cu->is_debug_types ? "type" : "comp",
7597 sect_offset_str (per_cu->sect_off),
7598 paddress (gdbarch, pst->text_low (objfile)),
7599 paddress (gdbarch, pst->text_high (objfile)),
7600 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7601}
7602
7603/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7604 Process compilation unit THIS_CU for a psymtab. */
7605
7606static void
ab432490
SM
7607process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7608 dwarf2_per_objfile *per_objfile,
135f5437 7609 bool want_partial_unit,
b93601f3 7610 enum language pretend_language)
0018ea6f
DE
7611{
7612 /* If this compilation unit was already read in, free the
7613 cached copy in order to read it in again. This is
7614 necessary because we skipped some symbols when we first
7615 read in the compilation unit (see load_partial_dies).
7616 This problem could be avoided, but the benefit is unclear. */
7188ed02 7617 per_objfile->remove_cu (this_cu);
0018ea6f 7618
2e671100 7619 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7620
58990295
TV
7621 switch (reader.comp_unit_die->tag)
7622 {
7623 case DW_TAG_compile_unit:
7624 this_cu->unit_type = DW_UT_compile;
7625 break;
7626 case DW_TAG_partial_unit:
7627 this_cu->unit_type = DW_UT_partial;
7628 break;
7629 default:
7630 abort ();
7631 }
7632
c0ab21c2 7633 if (reader.dummy_p)
f1902523 7634 {
c0ab21c2 7635 /* Nothing. */
f1902523 7636 }
c0ab21c2 7637 else if (this_cu->is_debug_types)
3e225074
TT
7638 build_type_psymtabs_reader (&reader, reader.info_ptr,
7639 reader.comp_unit_die);
135f5437
TT
7640 else if (want_partial_unit
7641 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7642 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7643 reader.comp_unit_die,
c0ab21c2 7644 pretend_language);
0018ea6f 7645
7188ed02 7646 this_cu->lang = reader.cu->language;
58990295 7647
0018ea6f 7648 /* Age out any secondary CUs. */
7188ed02 7649 per_objfile->age_comp_units ();
0018ea6f 7650}
f4dc4d17
DE
7651
7652/* Reader function for build_type_psymtabs. */
7653
7654static void
7655build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7656 const gdb_byte *info_ptr,
3e225074 7657 struct die_info *type_unit_die)
f4dc4d17 7658{
976ca316
SM
7659 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7660 struct objfile *objfile = per_objfile->objfile;
f4dc4d17
DE
7661 struct dwarf2_cu *cu = reader->cu;
7662 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7663 struct signatured_type *sig_type;
f4dc4d17
DE
7664 struct type_unit_group *tu_group;
7665 struct attribute *attr;
7666 struct partial_die_info *first_die;
7667 CORE_ADDR lowpc, highpc;
891813be 7668 dwarf2_psymtab *pst;
f4dc4d17 7669
0186c6a7
DE
7670 gdb_assert (per_cu->is_debug_types);
7671 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7672
3e225074 7673 if (! type_unit_die->has_children)
f4dc4d17
DE
7674 return;
7675
052c8bb8 7676 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7677 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7678
df07e2c7 7679 if (tu_group->tus == nullptr)
a8b3b8e9 7680 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7681 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7682
7683 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7684 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7685 pst->anonymous = true;
f4dc4d17
DE
7686
7687 first_die = load_partial_dies (reader, info_ptr, 1);
7688
7689 lowpc = (CORE_ADDR) -1;
7690 highpc = (CORE_ADDR) 0;
7691 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7692
8763cede 7693 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7694}
7695
73051182
DE
7696/* Struct used to sort TUs by their abbreviation table offset. */
7697
7698struct tu_abbrev_offset
7699{
b2bdb8cf
SM
7700 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7701 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7702 {}
7703
7704 signatured_type *sig_type;
73051182
DE
7705 sect_offset abbrev_offset;
7706};
7707
484cf504 7708/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7709
484cf504
TT
7710static bool
7711sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7712 const struct tu_abbrev_offset &b)
73051182 7713{
484cf504 7714 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7715}
7716
7717/* Efficiently read all the type units.
7718 This does the bulk of the work for build_type_psymtabs.
7719
7720 The efficiency is because we sort TUs by the abbrev table they use and
7721 only read each abbrev table once. In one program there are 200K TUs
7722 sharing 8K abbrev tables.
7723
7724 The main purpose of this function is to support building the
5989a64e 7725 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7726 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7727 can collapse the search space by grouping them by stmt_list.
7728 The savings can be significant, in the same program from above the 200K TUs
7729 share 8K stmt_list tables.
7730
7731 FUNC is expected to call get_type_unit_group, which will create the
7732 struct type_unit_group if necessary and add it to
5989a64e 7733 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7734
7735static void
976ca316 7736build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7737{
976ca316 7738 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7739 abbrev_table_up abbrev_table;
73051182 7740 sect_offset abbrev_offset;
73051182
DE
7741
7742 /* It's up to the caller to not call us multiple times. */
976ca316 7743 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7744
976ca316 7745 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7746 return;
7747
7748 /* TUs typically share abbrev tables, and there can be way more TUs than
7749 abbrev tables. Sort by abbrev table to reduce the number of times we
7750 read each abbrev table in.
7751 Alternatives are to punt or to maintain a cache of abbrev tables.
7752 This is simpler and efficient enough for now.
7753
7754 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7755 symtab to use). Typically TUs with the same abbrev offset have the same
7756 stmt_list value too so in practice this should work well.
7757
7758 The basic algorithm here is:
7759
7760 sort TUs by abbrev table
7761 for each TU with same abbrev table:
7762 read abbrev table if first user
7763 read TU top level DIE
7764 [IWBN if DWO skeletons had DW_AT_stmt_list]
7765 call FUNC */
7766
b4f54984 7767 if (dwarf_read_debug)
73051182
DE
7768 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7769
7770 /* Sort in a separate table to maintain the order of all_type_units
7771 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7772 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7773 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7774
976ca316 7775 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7776 sorted_by_abbrev.emplace_back
976ca316 7777 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7778 sig_type->per_cu.sect_off));
73051182 7779
484cf504
TT
7780 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7781 sort_tu_by_abbrev_offset);
73051182 7782
9c541725 7783 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7784
b2bdb8cf 7785 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7786 {
73051182
DE
7787 /* Switch to the next abbrev table if necessary. */
7788 if (abbrev_table == NULL
b2bdb8cf 7789 || tu.abbrev_offset != abbrev_offset)
73051182 7790 {
b2bdb8cf 7791 abbrev_offset = tu.abbrev_offset;
73051182 7792 abbrev_table =
976ca316
SM
7793 abbrev_table::read (per_objfile->objfile,
7794 &per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7795 ++tu_stats->nr_uniq_abbrev_tables;
7796 }
7797
976ca316 7798 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7799 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7800 if (!reader.dummy_p)
7801 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7802 reader.comp_unit_die);
73051182 7803 }
6aa5f3a6 7804}
73051182 7805
6aa5f3a6
DE
7806/* Print collected type unit statistics. */
7807
7808static void
976ca316 7809print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7810{
976ca316 7811 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7812
7813 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7814 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
976ca316 7815 per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7816 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7817 tu_stats->nr_uniq_abbrev_tables);
7818 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7819 tu_stats->nr_symtabs);
7820 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7821 tu_stats->nr_symtab_sharers);
7822 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7823 tu_stats->nr_stmt_less_type_units);
7824 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7825 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7826}
7827
f4dc4d17
DE
7828/* Traversal function for build_type_psymtabs. */
7829
7830static int
7831build_type_psymtab_dependencies (void **slot, void *info)
7832{
976ca316
SM
7833 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7834 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 7835 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7836 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7837 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7838 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7839 int i;
7840
7841 gdb_assert (len > 0);
197400e8 7842 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7843
7844 pst->number_of_dependencies = len;
a9342b62 7845 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7846 for (i = 0; i < len; ++i)
f4dc4d17 7847 {
df07e2c7 7848 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7849 gdb_assert (iter->per_cu.is_debug_types);
7850 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7851 iter->type_unit_group = tu_group;
f4dc4d17
DE
7852 }
7853
df07e2c7
AB
7854 delete tu_group->tus;
7855 tu_group->tus = nullptr;
348e048f
DE
7856
7857 return 1;
7858}
7859
7860/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7861 Build partial symbol tables for the .debug_types comp-units. */
7862
7863static void
976ca316 7864build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7865{
976ca316 7866 if (! create_all_type_units (per_objfile))
348e048f
DE
7867 return;
7868
976ca316 7869 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7870}
f4dc4d17 7871
6aa5f3a6
DE
7872/* Traversal function for process_skeletonless_type_unit.
7873 Read a TU in a DWO file and build partial symbols for it. */
7874
7875static int
7876process_skeletonless_type_unit (void **slot, void *info)
7877{
7878 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7879 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7880 struct signatured_type find_entry, *entry;
7881
7882 /* If this TU doesn't exist in the global table, add it and read it in. */
7883
976ca316
SM
7884 if (per_objfile->per_bfd->signatured_types == NULL)
7885 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7886
7887 find_entry.signature = dwo_unit->signature;
976ca316 7888 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7889 &find_entry, INSERT);
6aa5f3a6
DE
7890 /* If we've already seen this type there's nothing to do. What's happening
7891 is we're doing our own version of comdat-folding here. */
7892 if (*slot != NULL)
7893 return 1;
7894
7895 /* This does the job that create_all_type_units would have done for
7896 this TU. */
976ca316
SM
7897 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7898 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7899 *slot = entry;
7900
7901 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7902 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7903 if (!reader.dummy_p)
7904 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7905 reader.comp_unit_die);
6aa5f3a6
DE
7906
7907 return 1;
7908}
7909
7910/* Traversal function for process_skeletonless_type_units. */
7911
7912static int
7913process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7914{
7915 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7916
7917 if (dwo_file->tus != NULL)
b0b6a987
TT
7918 htab_traverse_noresize (dwo_file->tus.get (),
7919 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7920
7921 return 1;
7922}
7923
7924/* Scan all TUs of DWO files, verifying we've processed them.
7925 This is needed in case a TU was emitted without its skeleton.
7926 Note: This can't be done until we know what all the DWO files are. */
7927
7928static void
976ca316 7929process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7930{
7931 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7932 if (get_dwp_file (per_objfile) == NULL
7933 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7934 {
976ca316 7935 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7936 process_dwo_file_for_skeletonless_type_units,
976ca316 7937 per_objfile);
6aa5f3a6 7938 }
348e048f
DE
7939}
7940
ed2dc618 7941/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7942
7943static void
976ca316 7944set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7945{
976ca316 7946 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7947 {
891813be 7948 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7949
36586728
TT
7950 if (pst == NULL)
7951 continue;
7952
b76e467d 7953 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7954 {
7955 /* Set the 'user' field only if it is not already set. */
7956 if (pst->dependencies[j]->user == NULL)
7957 pst->dependencies[j]->user = pst;
7958 }
7959 }
7960}
7961
93311388
DE
7962/* Build the partial symbol table by doing a quick pass through the
7963 .debug_info and .debug_abbrev sections. */
72bf9492 7964
93311388 7965static void
976ca316 7966dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7967{
976ca316 7968 struct objfile *objfile = per_objfile->objfile;
93311388 7969
b4f54984 7970 if (dwarf_read_debug)
45cfd468
DE
7971 {
7972 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7973 objfile_name (objfile));
45cfd468
DE
7974 }
7975
76935768 7976 scoped_restore restore_reading_psyms
976ca316 7977 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 7978 true);
98bfdba5 7979
976ca316 7980 per_objfile->per_bfd->info.read (objfile);
91c24f0a 7981
93311388
DE
7982 /* Any cached compilation units will be linked by the per-objfile
7983 read_in_chain. Make sure to free them when we're done. */
976ca316 7984 free_cached_comp_units freer (per_objfile);
72bf9492 7985
976ca316 7986 build_type_psymtabs (per_objfile);
348e048f 7987
976ca316 7988 create_all_comp_units (per_objfile);
c906108c 7989
60606b2c
TT
7990 /* Create a temporary address map on a temporary obstack. We later
7991 copy this to the final obstack. */
8268c778 7992 auto_obstack temp_obstack;
791afaa2
TT
7993
7994 scoped_restore save_psymtabs_addrmap
d320c2b5 7995 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7996 addrmap_create_mutable (&temp_obstack));
72bf9492 7997
976ca316 7998 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7999 {
8000 if (per_cu->v.psymtab != NULL)
8001 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8002 continue;
976ca316 8003 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8004 language_minimal);
3d5afab3 8005 }
ff013f42 8006
6aa5f3a6 8007 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8008 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8009
8010 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8011 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8012 {
976ca316
SM
8013 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8014 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8015 }
8016
b4f54984 8017 if (dwarf_read_debug)
976ca316 8018 print_tu_stats (per_objfile);
6aa5f3a6 8019
976ca316 8020 set_partial_user (per_objfile);
95554aad 8021
d320c2b5
TT
8022 objfile->partial_symtabs->psymtabs_addrmap
8023 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8024 objfile->partial_symtabs->obstack ());
791afaa2
TT
8025 /* At this point we want to keep the address map. */
8026 save_psymtabs_addrmap.release ();
ff013f42 8027
b4f54984 8028 if (dwarf_read_debug)
45cfd468 8029 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8030 objfile_name (objfile));
ae038cb0
DJ
8031}
8032
dee91e82
DE
8033/* Load the partial DIEs for a secondary CU into memory.
8034 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8035
dee91e82 8036static void
ab432490 8037load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8038 dwarf2_per_objfile *per_objfile,
8039 dwarf2_cu *existing_cu)
dee91e82 8040{
2e671100 8041 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8042
8043 if (!reader.dummy_p)
8044 {
8045 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8046 language_minimal);
8047
8048 /* Check if comp unit has_children.
8049 If so, read the rest of the partial symbols from this comp unit.
8050 If not, there's no more debug_info for this comp unit. */
3e225074 8051 if (reader.comp_unit_die->has_children)
c0ab21c2 8052 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8053
8054 reader.keep ();
c0ab21c2 8055 }
ae038cb0
DJ
8056}
8057
ae038cb0 8058static void
976ca316 8059read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8060 struct dwarf2_section_info *section,
f1902523 8061 struct dwarf2_section_info *abbrev_section,
b76e467d 8062 unsigned int is_dwz)
ae038cb0 8063{
d521ce57 8064 const gdb_byte *info_ptr;
976ca316 8065 struct objfile *objfile = per_objfile->objfile;
be391dca 8066
b4f54984 8067 if (dwarf_read_debug)
bf6af496 8068 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8069 section->get_name (),
8070 section->get_file_name ());
bf6af496 8071
96b79293 8072 section->read (objfile);
ae038cb0 8073
36586728 8074 info_ptr = section->buffer;
6e70227d 8075
36586728 8076 while (info_ptr < section->buffer + section->size)
ae038cb0 8077 {
ae038cb0 8078 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8079
9c541725 8080 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8081
f1902523 8082 comp_unit_head cu_header;
976ca316 8083 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8084 abbrev_section, info_ptr,
8085 rcuh_kind::COMPILE);
ae038cb0
DJ
8086
8087 /* Save the compilation unit for later lookup. */
f1902523 8088 if (cu_header.unit_type != DW_UT_type)
976ca316 8089 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8090 else
8091 {
976ca316 8092 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8093 sig_type->signature = cu_header.signature;
8094 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8095 this_cu = &sig_type->per_cu;
8096 }
8097 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8098 this_cu->sect_off = sect_off;
f1902523 8099 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8100 this_cu->is_dwz = is_dwz;
8a0459fd 8101 this_cu->section = section;
ae038cb0 8102
976ca316 8103 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8104
8105 info_ptr = info_ptr + this_cu->length;
8106 }
36586728
TT
8107}
8108
8109/* Create a list of all compilation units in OBJFILE.
8110 This is only done for -readnow and building partial symtabs. */
8111
8112static void
976ca316 8113create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8114{
976ca316
SM
8115 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8116 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8117 &per_objfile->per_bfd->abbrev, 0);
36586728 8118
976ca316 8119 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8120 if (dwz != NULL)
976ca316 8121 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8122}
8123
5734ee8b 8124/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8125 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8126 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8127 DW_AT_ranges). See the comments of add_partial_subprogram on how
8128 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8129
72bf9492
DJ
8130static void
8131scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8132 CORE_ADDR *highpc, int set_addrmap,
8133 struct dwarf2_cu *cu)
c906108c 8134{
72bf9492 8135 struct partial_die_info *pdi;
c906108c 8136
91c24f0a
DC
8137 /* Now, march along the PDI's, descending into ones which have
8138 interesting children but skipping the children of the other ones,
8139 until we reach the end of the compilation unit. */
c906108c 8140
72bf9492 8141 pdi = first_die;
91c24f0a 8142
72bf9492
DJ
8143 while (pdi != NULL)
8144 {
52356b79 8145 pdi->fixup (cu);
c906108c 8146
f55ee35c 8147 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8148 children, so we need to look at them. Ditto for anonymous
8149 enums. */
933c6fe4 8150
7d00ffec 8151 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8152 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8153 || pdi->tag == DW_TAG_imported_unit
8154 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8155 {
72bf9492 8156 switch (pdi->tag)
c906108c
SS
8157 {
8158 case DW_TAG_subprogram:
b1dc1806 8159 case DW_TAG_inlined_subroutine:
cdc07690 8160 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8161 if (cu->language == language_cplus)
8162 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8163 set_addrmap, cu);
c906108c 8164 break;
72929c62 8165 case DW_TAG_constant:
c906108c
SS
8166 case DW_TAG_variable:
8167 case DW_TAG_typedef:
91c24f0a 8168 case DW_TAG_union_type:
317d2668
TV
8169 if (!pdi->is_declaration
8170 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8171 {
72bf9492 8172 add_partial_symbol (pdi, cu);
63d06c5c
DC
8173 }
8174 break;
c906108c 8175 case DW_TAG_class_type:
680b30c7 8176 case DW_TAG_interface_type:
c906108c 8177 case DW_TAG_structure_type:
72bf9492 8178 if (!pdi->is_declaration)
c906108c 8179 {
72bf9492 8180 add_partial_symbol (pdi, cu);
c906108c 8181 }
b7fee5a3
KS
8182 if ((cu->language == language_rust
8183 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8184 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8185 set_addrmap, cu);
c906108c 8186 break;
91c24f0a 8187 case DW_TAG_enumeration_type:
72bf9492
DJ
8188 if (!pdi->is_declaration)
8189 add_partial_enumeration (pdi, cu);
c906108c
SS
8190 break;
8191 case DW_TAG_base_type:
a02abb62 8192 case DW_TAG_subrange_type:
c906108c 8193 /* File scope base type definitions are added to the partial
c5aa993b 8194 symbol table. */
72bf9492 8195 add_partial_symbol (pdi, cu);
c906108c 8196 break;
d9fa45fe 8197 case DW_TAG_namespace:
cdc07690 8198 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8199 break;
5d7cb8df 8200 case DW_TAG_module:
59c35742
AB
8201 if (!pdi->is_declaration)
8202 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8203 break;
95554aad
TT
8204 case DW_TAG_imported_unit:
8205 {
8206 struct dwarf2_per_cu_data *per_cu;
8207
f4dc4d17
DE
8208 /* For now we don't handle imported units in type units. */
8209 if (cu->per_cu->is_debug_types)
8210 {
8211 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8212 " supported in type units [in module %s]"),
5e22e966 8213 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8214 }
8215
e3b94546 8216 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8217 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8218
8219 /* Go read the partial unit, if needed. */
8220 if (per_cu->v.psymtab == NULL)
ab432490
SM
8221 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8222 cu->language);
95554aad 8223
ae640021 8224 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8225 }
8226 break;
74921315
KS
8227 case DW_TAG_imported_declaration:
8228 add_partial_symbol (pdi, cu);
8229 break;
c906108c
SS
8230 default:
8231 break;
8232 }
8233 }
8234
72bf9492
DJ
8235 /* If the die has a sibling, skip to the sibling. */
8236
8237 pdi = pdi->die_sibling;
8238 }
8239}
8240
8241/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8242
72bf9492 8243 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8244 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8245 Enumerators are an exception; they use the scope of their parent
8246 enumeration type, i.e. the name of the enumeration type is not
8247 prepended to the enumerator.
91c24f0a 8248
72bf9492
DJ
8249 There are two complexities. One is DW_AT_specification; in this
8250 case "parent" means the parent of the target of the specification,
8251 instead of the direct parent of the DIE. The other is compilers
8252 which do not emit DW_TAG_namespace; in this case we try to guess
8253 the fully qualified name of structure types from their members'
8254 linkage names. This must be done using the DIE's children rather
8255 than the children of any DW_AT_specification target. We only need
8256 to do this for structures at the top level, i.e. if the target of
8257 any DW_AT_specification (if any; otherwise the DIE itself) does not
8258 have a parent. */
8259
8260/* Compute the scope prefix associated with PDI's parent, in
8261 compilation unit CU. The result will be allocated on CU's
8262 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8263 field. NULL is returned if no prefix is necessary. */
15d034d0 8264static const char *
72bf9492
DJ
8265partial_die_parent_scope (struct partial_die_info *pdi,
8266 struct dwarf2_cu *cu)
8267{
15d034d0 8268 const char *grandparent_scope;
72bf9492 8269 struct partial_die_info *parent, *real_pdi;
91c24f0a 8270
72bf9492
DJ
8271 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8272 then this means the parent of the specification DIE. */
8273
8274 real_pdi = pdi;
72bf9492 8275 while (real_pdi->has_specification)
fb816e8b 8276 {
122cf0f2
AB
8277 auto res = find_partial_die (real_pdi->spec_offset,
8278 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8279 real_pdi = res.pdi;
8280 cu = res.cu;
8281 }
72bf9492
DJ
8282
8283 parent = real_pdi->die_parent;
8284 if (parent == NULL)
8285 return NULL;
8286
8287 if (parent->scope_set)
8288 return parent->scope;
8289
52356b79 8290 parent->fixup (cu);
72bf9492 8291
10b3939b 8292 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8293
acebe513
UW
8294 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8295 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8296 Work around this problem here. */
8297 if (cu->language == language_cplus
6e70227d 8298 && parent->tag == DW_TAG_namespace
7d00ffec 8299 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8300 && grandparent_scope == NULL)
8301 {
8302 parent->scope = NULL;
8303 parent->scope_set = 1;
8304 return NULL;
8305 }
8306
0a4b0913 8307 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8308 if (pdi->tag == DW_TAG_enumerator)
8309 /* Enumerators should not get the name of the enumeration as a prefix. */
8310 parent->scope = grandparent_scope;
8311 else if (parent->tag == DW_TAG_namespace
f55ee35c 8312 || parent->tag == DW_TAG_module
72bf9492
DJ
8313 || parent->tag == DW_TAG_structure_type
8314 || parent->tag == DW_TAG_class_type
680b30c7 8315 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8316 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8317 || parent->tag == DW_TAG_enumeration_type
8318 || (cu->language == language_fortran
8319 && parent->tag == DW_TAG_subprogram
8320 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8321 {
8322 if (grandparent_scope == NULL)
7d00ffec 8323 parent->scope = parent->name (cu);
72bf9492 8324 else
3e43a32a
MS
8325 parent->scope = typename_concat (&cu->comp_unit_obstack,
8326 grandparent_scope,
7d00ffec 8327 parent->name (cu), 0, cu);
72bf9492 8328 }
72bf9492
DJ
8329 else
8330 {
8331 /* FIXME drow/2004-04-01: What should we be doing with
8332 function-local names? For partial symbols, we should probably be
8333 ignoring them. */
fa9c3fa0
TT
8334 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8335 dwarf_tag_name (parent->tag),
8336 sect_offset_str (pdi->sect_off));
72bf9492 8337 parent->scope = grandparent_scope;
c906108c
SS
8338 }
8339
72bf9492
DJ
8340 parent->scope_set = 1;
8341 return parent->scope;
8342}
8343
8344/* Return the fully scoped name associated with PDI, from compilation unit
8345 CU. The result will be allocated with malloc. */
4568ecf9 8346
43816ebc 8347static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8348partial_die_full_name (struct partial_die_info *pdi,
8349 struct dwarf2_cu *cu)
8350{
15d034d0 8351 const char *parent_scope;
72bf9492 8352
98bfdba5
PA
8353 /* If this is a template instantiation, we can not work out the
8354 template arguments from partial DIEs. So, unfortunately, we have
8355 to go through the full DIEs. At least any work we do building
8356 types here will be reused if full symbols are loaded later. */
8357 if (pdi->has_template_arguments)
8358 {
52356b79 8359 pdi->fixup (cu);
98bfdba5 8360
7d00ffec 8361 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8362 {
8363 struct die_info *die;
8364 struct attribute attr;
8365 struct dwarf2_cu *ref_cu = cu;
8366
b64f50a1 8367 /* DW_FORM_ref_addr is using section offset. */
b4069958 8368 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8369 attr.form = DW_FORM_ref_addr;
9c541725 8370 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8371 die = follow_die_ref (NULL, &attr, &ref_cu);
8372
43816ebc 8373 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8374 }
8375 }
8376
72bf9492
DJ
8377 parent_scope = partial_die_parent_scope (pdi, cu);
8378 if (parent_scope == NULL)
8379 return NULL;
8380 else
43816ebc 8381 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8382 pdi->name (cu),
8383 0, cu));
c906108c
SS
8384}
8385
8386static void
72bf9492 8387add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8388{
976ca316
SM
8389 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8390 struct objfile *objfile = per_objfile->objfile;
08feed99 8391 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8392 CORE_ADDR addr = 0;
15d034d0 8393 const char *actual_name = NULL;
e142c38c
DJ
8394 CORE_ADDR baseaddr;
8395
b3b3bada 8396 baseaddr = objfile->text_section_offset ();
c906108c 8397
43816ebc
TT
8398 gdb::unique_xmalloc_ptr<char> built_actual_name
8399 = partial_die_full_name (pdi, cu);
15d034d0 8400 if (built_actual_name != NULL)
43816ebc 8401 actual_name = built_actual_name.get ();
63d06c5c 8402
72bf9492 8403 if (actual_name == NULL)
7d00ffec 8404 actual_name = pdi->name (cu);
72bf9492 8405
76e288d1
TT
8406 partial_symbol psymbol;
8407 memset (&psymbol, 0, sizeof (psymbol));
8408 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8409 psymbol.ginfo.section = -1;
8410
8411 /* The code below indicates that the psymbol should be installed by
8412 setting this. */
8413 gdb::optional<psymbol_placement> where;
8414
c906108c
SS
8415 switch (pdi->tag)
8416 {
b1dc1806 8417 case DW_TAG_inlined_subroutine:
c906108c 8418 case DW_TAG_subprogram:
79748972
TT
8419 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8420 - baseaddr);
0a4b0913
AB
8421 if (pdi->is_external
8422 || cu->language == language_ada
8423 || (cu->language == language_fortran
8424 && pdi->die_parent != NULL
8425 && pdi->die_parent->tag == DW_TAG_subprogram))
8426 {
8427 /* Normally, only "external" DIEs are part of the global scope.
8428 But in Ada and Fortran, we want to be able to access nested
8429 procedures globally. So all Ada and Fortran subprograms are
8430 stored in the global scope. */
76e288d1 8431 where = psymbol_placement::GLOBAL;
c906108c
SS
8432 }
8433 else
76e288d1
TT
8434 where = psymbol_placement::STATIC;
8435
8436 psymbol.domain = VAR_DOMAIN;
8437 psymbol.aclass = LOC_BLOCK;
8438 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8439 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8440
8441 if (pdi->main_subprogram && actual_name != NULL)
8442 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8443 break;
72929c62 8444 case DW_TAG_constant:
76e288d1
TT
8445 psymbol.domain = VAR_DOMAIN;
8446 psymbol.aclass = LOC_STATIC;
8447 where = (pdi->is_external
8448 ? psymbol_placement::GLOBAL
8449 : psymbol_placement::STATIC);
72929c62 8450 break;
c906108c 8451 case DW_TAG_variable:
95554aad
TT
8452 if (pdi->d.locdesc)
8453 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8454
95554aad 8455 if (pdi->d.locdesc
caac4577 8456 && addr == 0
976ca316 8457 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8458 {
8459 /* A global or static variable may also have been stripped
8460 out by the linker if unused, in which case its address
8461 will be nullified; do not add such variables into partial
8462 symbol table then. */
8463 }
8464 else if (pdi->is_external)
c906108c
SS
8465 {
8466 /* Global Variable.
8467 Don't enter into the minimal symbol tables as there is
8468 a minimal symbol table entry from the ELF symbols already.
8469 Enter into partial symbol table if it has a location
8470 descriptor or a type.
8471 If the location descriptor is missing, new_symbol will create
8472 a LOC_UNRESOLVED symbol, the address of the variable will then
8473 be determined from the minimal symbol table whenever the variable
8474 is referenced.
8475 The address for the partial symbol table entry is not
8476 used by GDB, but it comes in handy for debugging partial symbol
8477 table building. */
8478
95554aad 8479 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8480 {
8481 psymbol.domain = VAR_DOMAIN;
8482 psymbol.aclass = LOC_STATIC;
8483 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8484 psymbol.ginfo.value.address = addr;
8485 where = psymbol_placement::GLOBAL;
8486 }
c906108c
SS
8487 }
8488 else
8489 {
ff908ebf
AW
8490 int has_loc = pdi->d.locdesc != NULL;
8491
8492 /* Static Variable. Skip symbols whose value we cannot know (those
8493 without location descriptors or constant values). */
8494 if (!has_loc && !pdi->has_const_value)
43816ebc 8495 return;
ff908ebf 8496
76e288d1
TT
8497 psymbol.domain = VAR_DOMAIN;
8498 psymbol.aclass = LOC_STATIC;
8499 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8500 if (has_loc)
8501 psymbol.ginfo.value.address = addr;
8502 where = psymbol_placement::STATIC;
c906108c
SS
8503 }
8504 break;
8505 case DW_TAG_typedef:
8506 case DW_TAG_base_type:
a02abb62 8507 case DW_TAG_subrange_type:
76e288d1
TT
8508 psymbol.domain = VAR_DOMAIN;
8509 psymbol.aclass = LOC_TYPEDEF;
8510 where = psymbol_placement::STATIC;
c906108c 8511 break;
74921315 8512 case DW_TAG_imported_declaration:
72bf9492 8513 case DW_TAG_namespace:
76e288d1
TT
8514 psymbol.domain = VAR_DOMAIN;
8515 psymbol.aclass = LOC_TYPEDEF;
8516 where = psymbol_placement::GLOBAL;
72bf9492 8517 break;
530e8392 8518 case DW_TAG_module:
a5fd13a9
BH
8519 /* With Fortran 77 there might be a "BLOCK DATA" module
8520 available without any name. If so, we skip the module as it
8521 doesn't bring any value. */
8522 if (actual_name != nullptr)
76e288d1
TT
8523 {
8524 psymbol.domain = MODULE_DOMAIN;
8525 psymbol.aclass = LOC_TYPEDEF;
8526 where = psymbol_placement::GLOBAL;
8527 }
530e8392 8528 break;
c906108c 8529 case DW_TAG_class_type:
680b30c7 8530 case DW_TAG_interface_type:
c906108c
SS
8531 case DW_TAG_structure_type:
8532 case DW_TAG_union_type:
8533 case DW_TAG_enumeration_type:
fa4028e9
JB
8534 /* Skip external references. The DWARF standard says in the section
8535 about "Structure, Union, and Class Type Entries": "An incomplete
8536 structure, union or class type is represented by a structure,
8537 union or class entry that does not have a byte size attribute
8538 and that has a DW_AT_declaration attribute." */
8539 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8540 return;
fa4028e9 8541
63d06c5c
DC
8542 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8543 static vs. global. */
76e288d1
TT
8544 psymbol.domain = STRUCT_DOMAIN;
8545 psymbol.aclass = LOC_TYPEDEF;
8546 where = (cu->language == language_cplus
8547 ? psymbol_placement::GLOBAL
8548 : psymbol_placement::STATIC);
c906108c
SS
8549 break;
8550 case DW_TAG_enumerator:
76e288d1
TT
8551 psymbol.domain = VAR_DOMAIN;
8552 psymbol.aclass = LOC_CONST;
8553 where = (cu->language == language_cplus
8554 ? psymbol_placement::GLOBAL
8555 : psymbol_placement::STATIC);
c906108c
SS
8556 break;
8557 default:
8558 break;
8559 }
76e288d1
TT
8560
8561 if (where.has_value ())
8562 {
f049a313
TT
8563 if (built_actual_name != nullptr)
8564 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8565 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8566 psymbol.ginfo.set_linkage_name (actual_name);
8567 else
8568 {
8569 psymbol.ginfo.set_demangled_name (actual_name,
8570 &objfile->objfile_obstack);
8571 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8572 }
76e288d1
TT
8573 add_psymbol_to_list (psymbol, *where, objfile);
8574 }
c906108c
SS
8575}
8576
5c4e30ca
DC
8577/* Read a partial die corresponding to a namespace; also, add a symbol
8578 corresponding to that namespace to the symbol table. NAMESPACE is
8579 the name of the enclosing namespace. */
91c24f0a 8580
72bf9492
DJ
8581static void
8582add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8583 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8584 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8585{
72bf9492 8586 /* Add a symbol for the namespace. */
e7c27a73 8587
72bf9492 8588 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8589
8590 /* Now scan partial symbols in that namespace. */
8591
91c24f0a 8592 if (pdi->has_children)
cdc07690 8593 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8594}
8595
5d7cb8df
JK
8596/* Read a partial die corresponding to a Fortran module. */
8597
8598static void
8599add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8600 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8601{
530e8392
KB
8602 /* Add a symbol for the namespace. */
8603
8604 add_partial_symbol (pdi, cu);
8605
f55ee35c 8606 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8607
8608 if (pdi->has_children)
cdc07690 8609 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8610}
8611
b1dc1806
XR
8612/* Read a partial die corresponding to a subprogram or an inlined
8613 subprogram and create a partial symbol for that subprogram.
8614 When the CU language allows it, this routine also defines a partial
8615 symbol for each nested subprogram that this subprogram contains.
8616 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8617 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8618
cdc07690
YQ
8619 PDI may also be a lexical block, in which case we simply search
8620 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8621 Again, this is only performed when the CU language allows this
8622 type of definitions. */
8623
8624static void
8625add_partial_subprogram (struct partial_die_info *pdi,
8626 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8627 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8628{
b1dc1806 8629 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8630 {
8631 if (pdi->has_pc_info)
8632 {
8633 if (pdi->lowpc < *lowpc)
8634 *lowpc = pdi->lowpc;
8635 if (pdi->highpc > *highpc)
8636 *highpc = pdi->highpc;
cdc07690 8637 if (set_addrmap)
5734ee8b 8638 {
5e22e966 8639 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8640 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8641 CORE_ADDR baseaddr;
b926417a
TT
8642 CORE_ADDR this_highpc;
8643 CORE_ADDR this_lowpc;
5734ee8b 8644
b3b3bada 8645 baseaddr = objfile->text_section_offset ();
b926417a
TT
8646 this_lowpc
8647 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8648 pdi->lowpc + baseaddr)
8649 - baseaddr);
8650 this_highpc
8651 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8652 pdi->highpc + baseaddr)
8653 - baseaddr);
d320c2b5 8654 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8655 this_lowpc, this_highpc - 1,
9291a0cd 8656 cu->per_cu->v.psymtab);
5734ee8b 8657 }
481860b3
GB
8658 }
8659
8660 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8661 {
bc30ff58 8662 if (!pdi->is_declaration)
e8d05480
JB
8663 /* Ignore subprogram DIEs that do not have a name, they are
8664 illegal. Do not emit a complaint at this point, we will
8665 do so when we convert this psymtab into a symtab. */
7d00ffec 8666 if (pdi->name (cu))
e8d05480 8667 add_partial_symbol (pdi, cu);
bc30ff58
JB
8668 }
8669 }
6e70227d 8670
bc30ff58
JB
8671 if (! pdi->has_children)
8672 return;
8673
0a4b0913 8674 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8675 {
8676 pdi = pdi->die_child;
8677 while (pdi != NULL)
8678 {
52356b79 8679 pdi->fixup (cu);
bc30ff58 8680 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8681 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8682 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8683 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8684 pdi = pdi->die_sibling;
8685 }
8686 }
8687}
8688
91c24f0a
DC
8689/* Read a partial die corresponding to an enumeration type. */
8690
72bf9492
DJ
8691static void
8692add_partial_enumeration (struct partial_die_info *enum_pdi,
8693 struct dwarf2_cu *cu)
91c24f0a 8694{
72bf9492 8695 struct partial_die_info *pdi;
91c24f0a 8696
7d00ffec 8697 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8698 add_partial_symbol (enum_pdi, cu);
8699
8700 pdi = enum_pdi->die_child;
8701 while (pdi)
91c24f0a 8702 {
7d00ffec 8703 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8704 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8705 else
72bf9492
DJ
8706 add_partial_symbol (pdi, cu);
8707 pdi = pdi->die_sibling;
91c24f0a 8708 }
91c24f0a
DC
8709}
8710
6caca83c
CC
8711/* Return the initial uleb128 in the die at INFO_PTR. */
8712
8713static unsigned int
d521ce57 8714peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8715{
8716 unsigned int bytes_read;
8717
8718 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8719}
8720
685af9cd
TT
8721/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8722 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8723
4bb7a0a7
DJ
8724 Return the corresponding abbrev, or NULL if the number is zero (indicating
8725 an empty DIE). In either case *BYTES_READ will be set to the length of
8726 the initial number. */
8727
8728static struct abbrev_info *
685af9cd
TT
8729peek_die_abbrev (const die_reader_specs &reader,
8730 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8731{
685af9cd 8732 dwarf2_cu *cu = reader.cu;
5e22e966 8733 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8734 unsigned int abbrev_number
8735 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8736
8737 if (abbrev_number == 0)
8738 return NULL;
8739
685af9cd 8740 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8741 if (!abbrev)
8742 {
422b9917 8743 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8744 " at offset %s [in module %s]"),
422b9917 8745 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8746 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8747 }
8748
8749 return abbrev;
8750}
8751
93311388
DE
8752/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8753 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8754 DIE. Any children of the skipped DIEs will also be skipped. */
8755
d521ce57
TT
8756static const gdb_byte *
8757skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8758{
4bb7a0a7
DJ
8759 while (1)
8760 {
685af9cd
TT
8761 unsigned int bytes_read;
8762 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8763
4bb7a0a7
DJ
8764 if (abbrev == NULL)
8765 return info_ptr + bytes_read;
8766 else
dee91e82 8767 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8768 }
8769}
8770
93311388
DE
8771/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8772 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8773 abbrev corresponding to that skipped uleb128 should be passed in
8774 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8775 children. */
8776
d521ce57
TT
8777static const gdb_byte *
8778skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8779 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8780{
8781 unsigned int bytes_read;
8782 struct attribute attr;
dee91e82
DE
8783 bfd *abfd = reader->abfd;
8784 struct dwarf2_cu *cu = reader->cu;
d521ce57 8785 const gdb_byte *buffer = reader->buffer;
f664829e 8786 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8787 unsigned int form, i;
8788
8789 for (i = 0; i < abbrev->num_attrs; i++)
8790 {
8791 /* The only abbrev we care about is DW_AT_sibling. */
8792 if (abbrev->attrs[i].name == DW_AT_sibling)
8793 {
18a8505e
AT
8794 bool ignored;
8795 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8796 &ignored);
4bb7a0a7 8797 if (attr.form == DW_FORM_ref_addr)
b98664d3 8798 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8799 else
b9502d3f 8800 {
0826b30a 8801 sect_offset off = attr.get_ref_die_offset ();
9c541725 8802 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8803
8804 if (sibling_ptr < info_ptr)
b98664d3 8805 complaint (_("DW_AT_sibling points backwards"));
22869d73 8806 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8807 reader->die_section->overflow_complaint ();
b9502d3f
WN
8808 else
8809 return sibling_ptr;
8810 }
4bb7a0a7
DJ
8811 }
8812
8813 /* If it isn't DW_AT_sibling, skip this attribute. */
8814 form = abbrev->attrs[i].form;
8815 skip_attribute:
8816 switch (form)
8817 {
4bb7a0a7 8818 case DW_FORM_ref_addr:
ae411497
TT
8819 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8820 and later it is offset sized. */
8821 if (cu->header.version == 2)
8822 info_ptr += cu->header.addr_size;
8823 else
8824 info_ptr += cu->header.offset_size;
8825 break;
36586728
TT
8826 case DW_FORM_GNU_ref_alt:
8827 info_ptr += cu->header.offset_size;
8828 break;
ae411497 8829 case DW_FORM_addr:
4bb7a0a7
DJ
8830 info_ptr += cu->header.addr_size;
8831 break;
8832 case DW_FORM_data1:
8833 case DW_FORM_ref1:
8834 case DW_FORM_flag:
8fe0f950 8835 case DW_FORM_strx1:
4bb7a0a7
DJ
8836 info_ptr += 1;
8837 break;
2dc7f7b3 8838 case DW_FORM_flag_present:
43988095 8839 case DW_FORM_implicit_const:
2dc7f7b3 8840 break;
4bb7a0a7
DJ
8841 case DW_FORM_data2:
8842 case DW_FORM_ref2:
8fe0f950 8843 case DW_FORM_strx2:
4bb7a0a7
DJ
8844 info_ptr += 2;
8845 break;
8fe0f950
AT
8846 case DW_FORM_strx3:
8847 info_ptr += 3;
8848 break;
4bb7a0a7
DJ
8849 case DW_FORM_data4:
8850 case DW_FORM_ref4:
8fe0f950 8851 case DW_FORM_strx4:
4bb7a0a7
DJ
8852 info_ptr += 4;
8853 break;
8854 case DW_FORM_data8:
8855 case DW_FORM_ref8:
55f1336d 8856 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8857 info_ptr += 8;
8858 break;
0224619f
JK
8859 case DW_FORM_data16:
8860 info_ptr += 16;
8861 break;
4bb7a0a7 8862 case DW_FORM_string:
9b1c24c8 8863 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8864 info_ptr += bytes_read;
8865 break;
2dc7f7b3 8866 case DW_FORM_sec_offset:
4bb7a0a7 8867 case DW_FORM_strp:
36586728 8868 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8869 info_ptr += cu->header.offset_size;
8870 break;
2dc7f7b3 8871 case DW_FORM_exprloc:
4bb7a0a7
DJ
8872 case DW_FORM_block:
8873 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8874 info_ptr += bytes_read;
8875 break;
8876 case DW_FORM_block1:
8877 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8878 break;
8879 case DW_FORM_block2:
8880 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8881 break;
8882 case DW_FORM_block4:
8883 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8884 break;
336d760d 8885 case DW_FORM_addrx:
cf532bd1 8886 case DW_FORM_strx:
4bb7a0a7
DJ
8887 case DW_FORM_sdata:
8888 case DW_FORM_udata:
8889 case DW_FORM_ref_udata:
3019eac3
DE
8890 case DW_FORM_GNU_addr_index:
8891 case DW_FORM_GNU_str_index:
18a8505e 8892 case DW_FORM_rnglistx:
41144253 8893 case DW_FORM_loclistx:
d521ce57 8894 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8895 break;
8896 case DW_FORM_indirect:
8897 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8898 info_ptr += bytes_read;
8899 /* We need to continue parsing from here, so just go back to
8900 the top. */
8901 goto skip_attribute;
8902
8903 default:
3e43a32a
MS
8904 error (_("Dwarf Error: Cannot handle %s "
8905 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8906 dwarf_form_name (form),
8907 bfd_get_filename (abfd));
8908 }
8909 }
8910
8911 if (abbrev->has_children)
dee91e82 8912 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8913 else
8914 return info_ptr;
8915}
8916
93311388 8917/* Locate ORIG_PDI's sibling.
dee91e82 8918 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8919
d521ce57 8920static const gdb_byte *
dee91e82
DE
8921locate_pdi_sibling (const struct die_reader_specs *reader,
8922 struct partial_die_info *orig_pdi,
d521ce57 8923 const gdb_byte *info_ptr)
91c24f0a
DC
8924{
8925 /* Do we know the sibling already? */
72bf9492 8926
91c24f0a
DC
8927 if (orig_pdi->sibling)
8928 return orig_pdi->sibling;
8929
8930 /* Are there any children to deal with? */
8931
8932 if (!orig_pdi->has_children)
8933 return info_ptr;
8934
4bb7a0a7 8935 /* Skip the children the long way. */
91c24f0a 8936
dee91e82 8937 return skip_children (reader, info_ptr);
91c24f0a
DC
8938}
8939
257e7a09 8940/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8941 not NULL. */
c906108c 8942
891813be
TT
8943void
8944dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8945{
976ca316 8946 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8947
976ca316 8948 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8949
077cbab2
TT
8950 /* If this psymtab is constructed from a debug-only objfile, the
8951 has_section_at_zero flag will not necessarily be correct. We
8952 can get the correct value for this flag by looking at the data
8953 associated with the (presumably stripped) associated objfile. */
8954 if (objfile->separate_debug_objfile_backlink)
c906108c 8955 {
976ca316 8956 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8957 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8958
976ca316
SM
8959 per_objfile->per_bfd->has_section_at_zero
8960 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8961 }
98bfdba5 8962
8566b89b 8963 expand_psymtab (objfile);
95554aad 8964
976ca316 8965 process_cu_includes (per_objfile);
c906108c 8966}
9cdd5dbd
DE
8967\f
8968/* Reading in full CUs. */
c906108c 8969
10b3939b
DJ
8970/* Add PER_CU to the queue. */
8971
8972static void
120ce1b5
SM
8973queue_comp_unit (dwarf2_per_cu_data *per_cu,
8974 dwarf2_per_objfile *per_objfile,
95554aad 8975 enum language pretend_language)
10b3939b 8976{
10b3939b 8977 per_cu->queued = 1;
120ce1b5 8978 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8979}
8980
89e63ee4
DE
8981/* If PER_CU is not yet queued, add it to the queue.
8982 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8983 dependency.
0907af0c 8984 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8985 meaning either PER_CU is already queued or it is already loaded.
8986
8987 N.B. There is an invariant here that if a CU is queued then it is loaded.
8988 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8989
8990static int
89e63ee4 8991maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8992 dwarf2_per_cu_data *per_cu,
8993 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8994 enum language pretend_language)
8995{
8996 /* We may arrive here during partial symbol reading, if we need full
8997 DIEs to process an unusual case (e.g. template arguments). Do
8998 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8999 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9000 {
7188ed02
SM
9001 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9002
9003 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9004 return 1;
9005 return 0;
9006 }
9007
9008 /* Mark the dependence relation so that we don't flush PER_CU
9009 too early. */
89e63ee4
DE
9010 if (dependent_cu != NULL)
9011 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9012
9013 /* If it's already on the queue, we have nothing to do. */
9014 if (per_cu->queued)
9015 return 0;
9016
9017 /* If the compilation unit is already loaded, just mark it as
9018 used. */
7188ed02
SM
9019 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9020 if (cu != nullptr)
0907af0c 9021 {
7188ed02 9022 cu->last_used = 0;
0907af0c
DE
9023 return 0;
9024 }
9025
9026 /* Add it to the queue. */
120ce1b5 9027 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9028
9029 return 1;
9030}
9031
10b3939b
DJ
9032/* Process the queue. */
9033
9034static void
976ca316 9035process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9036{
b4f54984 9037 if (dwarf_read_debug)
45cfd468
DE
9038 {
9039 fprintf_unfiltered (gdb_stdlog,
9040 "Expanding one or more symtabs of objfile %s ...\n",
976ca316 9041 objfile_name (per_objfile->objfile));
45cfd468
DE
9042 }
9043
03dd20cc
DJ
9044 /* The queue starts out with one item, but following a DIE reference
9045 may load a new CU, adding it to the end of the queue. */
976ca316 9046 while (!per_objfile->per_bfd->queue.empty ())
10b3939b 9047 {
976ca316 9048 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
7188ed02 9049 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9050
976ca316 9051 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9052 {
976ca316 9053 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9054
7188ed02
SM
9055 /* Skip dummy CUs. */
9056 if (cu != nullptr)
73be47f5 9057 {
7188ed02
SM
9058 unsigned int debug_print_threshold;
9059 char buf[100];
9060
9061 if (per_cu->is_debug_types)
9062 {
9063 struct signatured_type *sig_type =
9064 (struct signatured_type *) per_cu;
9065
9066 sprintf (buf, "TU %s at offset %s",
9067 hex_string (sig_type->signature),
9068 sect_offset_str (per_cu->sect_off));
9069 /* There can be 100s of TUs.
9070 Only print them in verbose mode. */
9071 debug_print_threshold = 2;
9072 }
9073 else
9074 {
9075 sprintf (buf, "CU at offset %s",
9076 sect_offset_str (per_cu->sect_off));
9077 debug_print_threshold = 1;
9078 }
247f5c4f 9079
7188ed02
SM
9080 if (dwarf_read_debug >= debug_print_threshold)
9081 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17 9082
7188ed02
SM
9083 if (per_cu->is_debug_types)
9084 process_full_type_unit (cu, item.pretend_language);
9085 else
9086 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9087
7188ed02
SM
9088 if (dwarf_read_debug >= debug_print_threshold)
9089 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9090 }
f4dc4d17 9091 }
10b3939b 9092
7188ed02 9093 per_cu->queued = 0;
976ca316 9094 per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9095 }
9096
b4f54984 9097 if (dwarf_read_debug)
45cfd468
DE
9098 {
9099 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
976ca316 9100 objfile_name (per_objfile->objfile));
45cfd468 9101 }
10b3939b
DJ
9102}
9103
10b3939b
DJ
9104/* Read in full symbols for PST, and anything it depends on. */
9105
8566b89b
TT
9106void
9107dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9108{
af758d11 9109 gdb_assert (!readin_p (objfile));
95554aad 9110
17ee85fc
TT
9111 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9112 free_cached_comp_units freer (per_objfile);
48993951 9113 expand_dependencies (objfile);
aaa75496 9114
97a1449a 9115 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9116 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9117}
9118
af758d11
SM
9119/* See psympriv.h. */
9120
9121bool
9122dwarf2_psymtab::readin_p (struct objfile *objfile) const
9123{
9124 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9125 return per_objfile->symtab_set_p (per_cu_data);
9126}
9127
9128/* See psympriv.h. */
9129
9130compunit_symtab *
9131dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9132{
9133 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9134 return per_objfile->get_symtab (per_cu_data);
9135}
9136
dee91e82
DE
9137/* Trivial hash function for die_info: the hash value of a DIE
9138 is its offset in .debug_info for this objfile. */
10b3939b 9139
dee91e82
DE
9140static hashval_t
9141die_hash (const void *item)
10b3939b 9142{
9a3c8263 9143 const struct die_info *die = (const struct die_info *) item;
6502dd73 9144
9c541725 9145 return to_underlying (die->sect_off);
dee91e82 9146}
63d06c5c 9147
dee91e82
DE
9148/* Trivial comparison function for die_info structures: two DIEs
9149 are equal if they have the same offset. */
98bfdba5 9150
dee91e82
DE
9151static int
9152die_eq (const void *item_lhs, const void *item_rhs)
9153{
9a3c8263
SM
9154 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9155 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9156
9c541725 9157 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9158}
c906108c 9159
c0ab21c2 9160/* Load the DIEs associated with PER_CU into memory. */
c906108c 9161
dee91e82 9162static void
ab432490
SM
9163load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9164 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9165 bool skip_partial,
9166 enum language pretend_language)
dee91e82 9167{
c0ab21c2
TT
9168 gdb_assert (! this_cu->is_debug_types);
9169
7188ed02
SM
9170 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9171 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9172 if (reader.dummy_p)
9173 return;
9174
9175 struct dwarf2_cu *cu = reader.cu;
9176 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9177
dee91e82
DE
9178 gdb_assert (cu->die_hash == NULL);
9179 cu->die_hash =
9180 htab_create_alloc_ex (cu->header.length / 12,
9181 die_hash,
9182 die_eq,
9183 NULL,
9184 &cu->comp_unit_obstack,
9185 hashtab_obstack_allocate,
9186 dummy_obstack_deallocate);
e142c38c 9187
3e225074 9188 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9189 reader.comp_unit_die->child
9190 = read_die_and_siblings (&reader, reader.info_ptr,
9191 &info_ptr, reader.comp_unit_die);
9192 cu->dies = reader.comp_unit_die;
dee91e82 9193 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9194
9195 /* We try not to read any attributes in this function, because not
9cdd5dbd 9196 all CUs needed for references have been loaded yet, and symbol
10b3939b 9197 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9198 or we won't be able to build types correctly.
9199 Similarly, if we do not read the producer, we can not apply
9200 producer-specific interpretation. */
c0ab21c2 9201 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9202
9203 reader.keep ();
10b3939b
DJ
9204}
9205
3da10d80
KS
9206/* Add a DIE to the delayed physname list. */
9207
9208static void
9209add_to_method_list (struct type *type, int fnfield_index, int index,
9210 const char *name, struct die_info *die,
9211 struct dwarf2_cu *cu)
9212{
9213 struct delayed_method_info mi;
9214 mi.type = type;
9215 mi.fnfield_index = fnfield_index;
9216 mi.index = index;
9217 mi.name = name;
9218 mi.die = die;
c89b44cd 9219 cu->method_list.push_back (mi);
3da10d80
KS
9220}
9221
3693fdb3
PA
9222/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9223 "const" / "volatile". If so, decrements LEN by the length of the
9224 modifier and return true. Otherwise return false. */
9225
9226template<size_t N>
9227static bool
9228check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9229{
9230 size_t mod_len = sizeof (mod) - 1;
9231 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9232 {
9233 len -= mod_len;
9234 return true;
9235 }
9236 return false;
9237}
9238
3da10d80
KS
9239/* Compute the physnames of any methods on the CU's method list.
9240
9241 The computation of method physnames is delayed in order to avoid the
9242 (bad) condition that one of the method's formal parameters is of an as yet
9243 incomplete type. */
9244
9245static void
9246compute_delayed_physnames (struct dwarf2_cu *cu)
9247{
3693fdb3 9248 /* Only C++ delays computing physnames. */
c89b44cd 9249 if (cu->method_list.empty ())
3693fdb3
PA
9250 return;
9251 gdb_assert (cu->language == language_cplus);
9252
52941706 9253 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9254 {
1d06ead6 9255 const char *physname;
3da10d80 9256 struct fn_fieldlist *fn_flp
c89b44cd
TT
9257 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9258 physname = dwarf2_physname (mi.name, mi.die, cu);
9259 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9260 = physname ? physname : "";
3693fdb3
PA
9261
9262 /* Since there's no tag to indicate whether a method is a
9263 const/volatile overload, extract that information out of the
9264 demangled name. */
9265 if (physname != NULL)
9266 {
9267 size_t len = strlen (physname);
9268
9269 while (1)
9270 {
9271 if (physname[len] == ')') /* shortcut */
9272 break;
9273 else if (check_modifier (physname, len, " const"))
c89b44cd 9274 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9275 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9276 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9277 else
9278 break;
9279 }
9280 }
3da10d80 9281 }
c89b44cd
TT
9282
9283 /* The list is no longer needed. */
9284 cu->method_list.clear ();
3da10d80
KS
9285}
9286
a766d390
DE
9287/* Go objects should be embedded in a DW_TAG_module DIE,
9288 and it's not clear if/how imported objects will appear.
9289 To keep Go support simple until that's worked out,
9290 go back through what we've read and create something usable.
9291 We could do this while processing each DIE, and feels kinda cleaner,
9292 but that way is more invasive.
9293 This is to, for example, allow the user to type "p var" or "b main"
9294 without having to specify the package name, and allow lookups
9295 of module.object to work in contexts that use the expression
9296 parser. */
9297
9298static void
9299fixup_go_packaging (struct dwarf2_cu *cu)
9300{
421d1616 9301 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9302 struct pending *list;
9303 int i;
9304
c24bdb02 9305 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9306 list != NULL;
9307 list = list->next)
a766d390
DE
9308 {
9309 for (i = 0; i < list->nsyms; ++i)
9310 {
9311 struct symbol *sym = list->symbol[i];
9312
c1b5c1eb 9313 if (sym->language () == language_go
a766d390
DE
9314 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9315 {
421d1616
TT
9316 gdb::unique_xmalloc_ptr<char> this_package_name
9317 (go_symbol_package_name (sym));
a766d390
DE
9318
9319 if (this_package_name == NULL)
9320 continue;
9321 if (package_name == NULL)
421d1616 9322 package_name = std::move (this_package_name);
a766d390
DE
9323 else
9324 {
5e22e966 9325 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9326 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9327 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9328 (symbol_symtab (sym) != NULL
9329 ? symtab_to_filename_for_display
9330 (symbol_symtab (sym))
e3b94546 9331 : objfile_name (objfile)),
421d1616 9332 this_package_name.get (), package_name.get ());
a766d390
DE
9333 }
9334 }
9335 }
9336 }
9337
9338 if (package_name != NULL)
9339 {
5e22e966 9340 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9341 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9342 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9343 saved_package_name);
a766d390
DE
9344 struct symbol *sym;
9345
8c14c3a3 9346 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9347 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9348 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9349 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9350 e.g., "main" finds the "main" module and not C's main(). */
9351 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9352 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9353 SYMBOL_TYPE (sym) = type;
9354
c24bdb02 9355 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9356 }
9357}
9358
c9317f21
TT
9359/* Allocate a fully-qualified name consisting of the two parts on the
9360 obstack. */
9361
9362static const char *
9363rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9364{
9365 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9366}
9367
9c6a1327
TT
9368/* A helper that allocates a variant part to attach to a Rust enum
9369 type. OBSTACK is where the results should be allocated. TYPE is
9370 the type we're processing. DISCRIMINANT_INDEX is the index of the
9371 discriminant. It must be the index of one of the fields of TYPE.
9372 DEFAULT_INDEX is the index of the default field; or -1 if there is
9373 no default. RANGES is indexed by "effective" field number (the
9374 field index, but omitting the discriminant and default fields) and
9375 must hold the discriminant values used by the variants. Note that
9376 RANGES must have a lifetime at least as long as OBSTACK -- either
9377 already allocated on it, or static. */
c9317f21 9378
9c6a1327
TT
9379static void
9380alloc_rust_variant (struct obstack *obstack, struct type *type,
9381 int discriminant_index, int default_index,
9382 gdb::array_view<discriminant_range> ranges)
9383{
9384 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9385 must be handled by the caller. */
9386 gdb_assert (discriminant_index >= 0
1f704f76 9387 && discriminant_index < type->num_fields ());
c9317f21 9388 gdb_assert (default_index == -1
1f704f76 9389 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9390
9c6a1327 9391 /* We have one variant for each non-discriminant field. */
1f704f76 9392 int n_variants = type->num_fields () - 1;
c9317f21 9393
9c6a1327
TT
9394 variant *variants = new (obstack) variant[n_variants];
9395 int var_idx = 0;
9396 int range_idx = 0;
1f704f76 9397 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9398 {
9399 if (i == discriminant_index)
9400 continue;
c9317f21 9401
9c6a1327
TT
9402 variants[var_idx].first_field = i;
9403 variants[var_idx].last_field = i + 1;
9404
9405 /* The default field does not need a range, but other fields do.
9406 We skipped the discriminant above. */
9407 if (i != default_index)
9408 {
9409 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9410 ++range_idx;
9411 }
c9317f21 9412
9c6a1327
TT
9413 ++var_idx;
9414 }
9415
9416 gdb_assert (range_idx == ranges.size ());
9417 gdb_assert (var_idx == n_variants);
9418
9419 variant_part *part = new (obstack) variant_part;
9420 part->discriminant_index = discriminant_index;
940da03e 9421 part->is_unsigned = TYPE_UNSIGNED (type->field (discriminant_index).type ());
9c6a1327
TT
9422 part->variants = gdb::array_view<variant> (variants, n_variants);
9423
9424 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9425 gdb::array_view<variant_part> *prop_value
9426 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9427
9c6a1327
TT
9428 struct dynamic_prop prop;
9429 prop.kind = PROP_VARIANT_PARTS;
9430 prop.data.variant_parts = prop_value;
9431
5c54719c 9432 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9433}
9434
9435/* Some versions of rustc emitted enums in an unusual way.
9436
9437 Ordinary enums were emitted as unions. The first element of each
9438 structure in the union was named "RUST$ENUM$DISR". This element
9439 held the discriminant.
9440
9441 These versions of Rust also implemented the "non-zero"
9442 optimization. When the enum had two values, and one is empty and
9443 the other holds a pointer that cannot be zero, the pointer is used
9444 as the discriminant, with a zero value meaning the empty variant.
9445 Here, the union's first member is of the form
9446 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9447 where the fieldnos are the indices of the fields that should be
9448 traversed in order to find the field (which may be several fields deep)
9449 and the variantname is the name of the variant of the case when the
9450 field is zero.
9451
9452 This function recognizes whether TYPE is of one of these forms,
9453 and, if so, smashes it to be a variant type. */
9454
9455static void
9456quirk_rust_enum (struct type *type, struct objfile *objfile)
9457{
78134374 9458 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9459
9460 /* We don't need to deal with empty enums. */
1f704f76 9461 if (type->num_fields () == 0)
c9317f21
TT
9462 return;
9463
9464#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9465 if (type->num_fields () == 1
c9317f21
TT
9466 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9467 {
9468 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9469
9470 /* Decode the field name to find the offset of the
9471 discriminant. */
9472 ULONGEST bit_offset = 0;
940da03e 9473 struct type *field_type = type->field (0).type ();
c9317f21
TT
9474 while (name[0] >= '0' && name[0] <= '9')
9475 {
9476 char *tail;
9477 unsigned long index = strtoul (name, &tail, 10);
9478 name = tail;
9479 if (*name != '$'
1f704f76 9480 || index >= field_type->num_fields ()
c9317f21
TT
9481 || (TYPE_FIELD_LOC_KIND (field_type, index)
9482 != FIELD_LOC_KIND_BITPOS))
9483 {
b98664d3 9484 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9485 "[in module %s]"),
9486 TYPE_FIELD_NAME (type, 0),
9487 objfile_name (objfile));
9488 return;
9489 }
9490 ++name;
9491
9492 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9493 field_type = field_type->field (index).type ();
c9317f21
TT
9494 }
9495
9c6a1327
TT
9496 /* Smash this type to be a structure type. We have to do this
9497 because the type has already been recorded. */
67607e24 9498 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9499 type->set_num_fields (3);
9c6a1327 9500 /* Save the field we care about. */
ceacbf6e 9501 struct field saved_field = type->field (0);
3cabb6b0
SM
9502 type->set_fields
9503 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9504
9c6a1327 9505 /* Put the discriminant at index 0. */
5d14b6e5 9506 type->field (0).set_type (field_type);
9c6a1327
TT
9507 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9508 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9509 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9510
9511 /* The order of fields doesn't really matter, so put the real
9512 field at index 1 and the data-less field at index 2. */
ceacbf6e 9513 type->field (1) = saved_field;
9c6a1327 9514 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9515 = rust_last_path_segment (type->field (1).type ()->name ());
9516 type->field (1).type ()->set_name
7d93a1e0 9517 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9518 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9519
9520 const char *dataless_name
7d93a1e0 9521 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9522 name);
9523 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9524 dataless_name);
5d14b6e5 9525 type->field (2).set_type (dataless_type);
c9317f21
TT
9526 /* NAME points into the original discriminant name, which
9527 already has the correct lifetime. */
9c6a1327 9528 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9529 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9530
9c6a1327
TT
9531 /* Indicate that this is a variant type. */
9532 static discriminant_range ranges[1] = { { 0, 0 } };
9533 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9534 }
77c2dba3
TT
9535 /* A union with a single anonymous field is probably an old-style
9536 univariant enum. */
1f704f76 9537 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9538 {
c9317f21
TT
9539 /* Smash this type to be a structure type. We have to do this
9540 because the type has already been recorded. */
67607e24 9541 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9542
940da03e 9543 struct type *field_type = type->field (0).type ();
c9317f21 9544 const char *variant_name
7d93a1e0 9545 = rust_last_path_segment (field_type->name ());
9c6a1327 9546 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9547 field_type->set_name
9548 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9549 type->name (), variant_name));
c9317f21
TT
9550 }
9551 else
9552 {
9553 struct type *disr_type = nullptr;
1f704f76 9554 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9555 {
940da03e 9556 disr_type = type->field (i).type ();
c9317f21 9557
78134374 9558 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9559 {
9560 /* All fields of a true enum will be structs. */
9561 return;
9562 }
1f704f76 9563 else if (disr_type->num_fields () == 0)
c9317f21
TT
9564 {
9565 /* Could be data-less variant, so keep going. */
a037790e 9566 disr_type = nullptr;
c9317f21
TT
9567 }
9568 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9569 "RUST$ENUM$DISR") != 0)
9570 {
9571 /* Not a Rust enum. */
9572 return;
9573 }
9574 else
9575 {
9576 /* Found one. */
9577 break;
9578 }
9579 }
9580
9581 /* If we got here without a discriminant, then it's probably
9582 just a union. */
9583 if (disr_type == nullptr)
9584 return;
9585
9586 /* Smash this type to be a structure type. We have to do this
9587 because the type has already been recorded. */
67607e24 9588 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9589
9c6a1327 9590 /* Make space for the discriminant field. */
ceacbf6e 9591 struct field *disr_field = &disr_type->field (0);
9c6a1327 9592 field *new_fields
1f704f76 9593 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9594 * sizeof (struct field)));
80fc5e77 9595 memcpy (new_fields + 1, type->fields (),
1f704f76 9596 type->num_fields () * sizeof (struct field));
3cabb6b0 9597 type->set_fields (new_fields);
1f704f76 9598 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9599
9600 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9601 type->field (0) = *disr_field;
9c6a1327
TT
9602 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9603 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9604
9605 /* We need a way to find the correct discriminant given a
9606 variant name. For convenience we build a map here. */
b6cdac4b 9607 struct type *enum_type = disr_field->type ();
c9317f21 9608 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9609 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9610 {
9611 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9612 {
9613 const char *name
9614 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9615 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9616 }
9617 }
9618
1f704f76 9619 int n_fields = type->num_fields ();
9c6a1327
TT
9620 /* We don't need a range entry for the discriminant, but we do
9621 need one for every other field, as there is no default
9622 variant. */
9623 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9624 discriminant_range,
9625 n_fields - 1);
c9317f21
TT
9626 /* Skip the discriminant here. */
9627 for (int i = 1; i < n_fields; ++i)
9628 {
9629 /* Find the final word in the name of this variant's type.
9630 That name can be used to look up the correct
9631 discriminant. */
9632 const char *variant_name
940da03e 9633 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9634
9635 auto iter = discriminant_map.find (variant_name);
9636 if (iter != discriminant_map.end ())
9c6a1327
TT
9637 {
9638 ranges[i].low = iter->second;
9639 ranges[i].high = iter->second;
9640 }
c9317f21 9641
bedda9ac 9642 /* Remove the discriminant field, if it exists. */
940da03e 9643 struct type *sub_type = type->field (i).type ();
1f704f76 9644 if (sub_type->num_fields () > 0)
bedda9ac 9645 {
5e33d5f4 9646 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9647 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9648 }
9c6a1327 9649 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9650 sub_type->set_name
9651 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9652 type->name (), variant_name));
c9317f21 9653 }
9c6a1327
TT
9654
9655 /* Indicate that this is a variant type. */
9656 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9657 gdb::array_view<discriminant_range> (ranges,
9658 n_fields - 1));
c9317f21
TT
9659 }
9660}
9661
9662/* Rewrite some Rust unions to be structures with variants parts. */
9663
9664static void
9665rust_union_quirks (struct dwarf2_cu *cu)
9666{
9667 gdb_assert (cu->language == language_rust);
52941706 9668 for (type *type_ : cu->rust_unions)
5e22e966 9669 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9670 /* We don't need this any more. */
9671 cu->rust_unions.clear ();
c9317f21
TT
9672}
9673
8adb8487
TT
9674/* See read.h. */
9675
9676type_unit_group_unshareable *
9677dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9678{
9679 auto iter = this->m_type_units.find (tu_group);
9680 if (iter != this->m_type_units.end ())
9681 return iter->second.get ();
9682
9683 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9684 type_unit_group_unshareable *result = uniq.get ();
9685 this->m_type_units[tu_group] = std::move (uniq);
9686 return result;
9687}
9688
e286671b
TT
9689struct type *
9690dwarf2_per_objfile::get_type_for_signatured_type
9691 (signatured_type *sig_type) const
9692{
9693 auto iter = this->m_type_map.find (sig_type);
9694 if (iter == this->m_type_map.end ())
9695 return nullptr;
9696
9697 return iter->second;
9698}
9699
9700void dwarf2_per_objfile::set_type_for_signatured_type
9701 (signatured_type *sig_type, struct type *type)
9702{
9703 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9704
9705 this->m_type_map[sig_type] = type;
9706}
9707
95554aad
TT
9708/* A helper function for computing the list of all symbol tables
9709 included by PER_CU. */
9710
9711static void
4c39bc03 9712recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9713 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9714 dwarf2_per_cu_data *per_cu,
9715 dwarf2_per_objfile *per_objfile,
43f3e411 9716 struct compunit_symtab *immediate_parent)
95554aad 9717{
af758d11 9718 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9719 if (*slot != NULL)
9720 {
9721 /* This inclusion and its children have been processed. */
9722 return;
9723 }
9724
9725 *slot = per_cu;
af758d11 9726
95554aad 9727 /* Only add a CU if it has a symbol table. */
43182c09 9728 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9729 if (cust != NULL)
ec94af83
DE
9730 {
9731 /* If this is a type unit only add its symbol table if we haven't
9732 seen it yet (type unit per_cu's can share symtabs). */
9733 if (per_cu->is_debug_types)
9734 {
43f3e411 9735 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9736 if (*slot == NULL)
9737 {
43f3e411 9738 *slot = cust;
4c39bc03 9739 result->push_back (cust);
43f3e411
DE
9740 if (cust->user == NULL)
9741 cust->user = immediate_parent;
ec94af83
DE
9742 }
9743 }
9744 else
f9125b6c 9745 {
4c39bc03 9746 result->push_back (cust);
43f3e411
DE
9747 if (cust->user == NULL)
9748 cust->user = immediate_parent;
f9125b6c 9749 }
ec94af83 9750 }
95554aad 9751
ae640021
AB
9752 if (!per_cu->imported_symtabs_empty ())
9753 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9754 {
9755 recursively_compute_inclusions (result, all_children,
43182c09
SM
9756 all_type_symtabs, ptr, per_objfile,
9757 cust);
ae640021 9758 }
95554aad
TT
9759}
9760
43f3e411 9761/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9762 PER_CU. */
9763
9764static void
43182c09
SM
9765compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9766 dwarf2_per_objfile *per_objfile)
95554aad 9767{
f4dc4d17
DE
9768 gdb_assert (! per_cu->is_debug_types);
9769
ae640021 9770 if (!per_cu->imported_symtabs_empty ())
95554aad 9771 {
ae640021 9772 int len;
4c39bc03 9773 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9774 htab_t all_children, all_type_symtabs;
43182c09 9775 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9776
9777 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9778 if (cust == NULL)
95554aad
TT
9779 return;
9780
9781 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9782 NULL, xcalloc, xfree);
ec94af83
DE
9783 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9784 NULL, xcalloc, xfree);
95554aad 9785
ae640021 9786 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9787 {
9788 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9789 all_type_symtabs, ptr, per_objfile,
9790 cust);
ec94af83 9791 }
95554aad 9792
ec94af83 9793 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9794 len = result_symtabs.size ();
43f3e411 9795 cust->includes
f6e649dd 9796 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9797 struct compunit_symtab *, len + 1);
4c39bc03
TT
9798 memcpy (cust->includes, result_symtabs.data (),
9799 len * sizeof (compunit_symtab *));
43f3e411 9800 cust->includes[len] = NULL;
95554aad 9801
95554aad 9802 htab_delete (all_children);
ec94af83 9803 htab_delete (all_type_symtabs);
95554aad
TT
9804 }
9805}
9806
9807/* Compute the 'includes' field for the symtabs of all the CUs we just
9808 read. */
9809
9810static void
976ca316 9811process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9812{
976ca316 9813 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9814 {
9815 if (! iter->is_debug_types)
976ca316 9816 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9817 }
95554aad 9818
976ca316 9819 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9820}
9821
8fc0b21d 9822/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9823 already been loaded into memory. */
9824
9825static void
8fc0b21d 9826process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9827{
976ca316
SM
9828 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9829 struct objfile *objfile = per_objfile->objfile;
08feed99 9830 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9831 CORE_ADDR lowpc, highpc;
43f3e411 9832 struct compunit_symtab *cust;
10b3939b 9833 CORE_ADDR baseaddr;
4359dff1 9834 struct block *static_block;
3e29f34a 9835 CORE_ADDR addr;
10b3939b 9836
b3b3bada 9837 baseaddr = objfile->text_section_offset ();
10b3939b 9838
c89b44cd
TT
9839 /* Clear the list here in case something was left over. */
9840 cu->method_list.clear ();
10b3939b 9841
95554aad
TT
9842 cu->language = pretend_language;
9843 cu->language_defn = language_def (cu->language);
9844
c906108c 9845 /* Do line number decoding in read_file_scope () */
10b3939b 9846 process_die (cu->dies, cu);
c906108c 9847
a766d390
DE
9848 /* For now fudge the Go package. */
9849 if (cu->language == language_go)
9850 fixup_go_packaging (cu);
9851
5f48f8f3 9852 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9853 should be complete, and it should now be safe to compute all of the
9854 physnames. */
9855 compute_delayed_physnames (cu);
3da10d80 9856
c9317f21
TT
9857 if (cu->language == language_rust)
9858 rust_union_quirks (cu);
9859
fae299cd
DC
9860 /* Some compilers don't define a DW_AT_high_pc attribute for the
9861 compilation unit. If the DW_AT_high_pc is missing, synthesize
9862 it, by scanning the DIE's below the compilation unit. */
10b3939b 9863 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9864
3e29f34a 9865 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9866 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9867
9868 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9869 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9870 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9871 addrmap to help ensure it has an accurate map of pc values belonging to
9872 this comp unit. */
9873 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9874
c24bdb02 9875 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9876 SECT_OFF_TEXT (objfile),
9877 0);
c906108c 9878
43f3e411 9879 if (cust != NULL)
c906108c 9880 {
df15bd07 9881 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9882
8be455d7
JK
9883 /* Set symtab language to language from DW_AT_language. If the
9884 compilation is from a C file generated by language preprocessors, do
9885 not set the language if it was already deduced by start_subfile. */
43f3e411 9886 if (!(cu->language == language_c
40e3ad0e 9887 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9888 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9889
9890 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9891 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9892 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9893 there were bugs in prologue debug info, fixed later in GCC-4.5
9894 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9895
9896 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9897 needed, it would be wrong due to missing DW_AT_producer there.
9898
9899 Still one can confuse GDB by using non-standard GCC compilation
9900 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9901 */
ab260dad 9902 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9903 cust->locations_valid = 1;
e0d00bc7
JK
9904
9905 if (gcc_4_minor >= 5)
43f3e411 9906 cust->epilogue_unwind_valid = 1;
96408a79 9907
43f3e411 9908 cust->call_site_htab = cu->call_site_htab;
c906108c 9909 }
9291a0cd 9910
976ca316 9911 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9912
95554aad 9913 /* Push it for inclusion processing later. */
976ca316 9914 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9915
9916 /* Not needed any more. */
c24bdb02 9917 cu->reset_builder ();
f4dc4d17 9918}
45cfd468 9919
8fc0b21d 9920/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9921 already been loaded into memory. */
9922
9923static void
8fc0b21d 9924process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9925 enum language pretend_language)
9926{
976ca316
SM
9927 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9928 struct objfile *objfile = per_objfile->objfile;
43f3e411 9929 struct compunit_symtab *cust;
0186c6a7
DE
9930 struct signatured_type *sig_type;
9931
8fc0b21d
SM
9932 gdb_assert (cu->per_cu->is_debug_types);
9933 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9934
c89b44cd
TT
9935 /* Clear the list here in case something was left over. */
9936 cu->method_list.clear ();
f4dc4d17 9937
f4dc4d17
DE
9938 cu->language = pretend_language;
9939 cu->language_defn = language_def (cu->language);
9940
9941 /* The symbol tables are set up in read_type_unit_scope. */
9942 process_die (cu->dies, cu);
9943
9944 /* For now fudge the Go package. */
9945 if (cu->language == language_go)
9946 fixup_go_packaging (cu);
9947
5f48f8f3 9948 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9949 should be complete, and it should now be safe to compute all of the
9950 physnames. */
9951 compute_delayed_physnames (cu);
f4dc4d17 9952
c9317f21
TT
9953 if (cu->language == language_rust)
9954 rust_union_quirks (cu);
9955
f4dc4d17
DE
9956 /* TUs share symbol tables.
9957 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9958 of it with end_expandable_symtab. Otherwise, complete the addition of
9959 this TU's symbols to the existing symtab. */
8adb8487 9960 type_unit_group_unshareable *tug_unshare =
976ca316 9961 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9962 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9963 {
c24bdb02
KS
9964 buildsym_compunit *builder = cu->get_builder ();
9965 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9966 tug_unshare->compunit_symtab = cust;
f4dc4d17 9967
43f3e411 9968 if (cust != NULL)
f4dc4d17
DE
9969 {
9970 /* Set symtab language to language from DW_AT_language. If the
9971 compilation is from a C file generated by language preprocessors,
9972 do not set the language if it was already deduced by
9973 start_subfile. */
43f3e411
DE
9974 if (!(cu->language == language_c
9975 && COMPUNIT_FILETABS (cust)->language != language_c))
9976 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9977 }
9978 }
9979 else
9980 {
c24bdb02 9981 cu->get_builder ()->augment_type_symtab ();
8adb8487 9982 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9983 }
9984
976ca316 9985 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9986
9987 /* Not needed any more. */
c24bdb02 9988 cu->reset_builder ();
c906108c
SS
9989}
9990
95554aad
TT
9991/* Process an imported unit DIE. */
9992
9993static void
9994process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9995{
9996 struct attribute *attr;
9997
f4dc4d17
DE
9998 /* For now we don't handle imported units in type units. */
9999 if (cu->per_cu->is_debug_types)
10000 {
10001 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10002 " supported in type units [in module %s]"),
5e22e966 10003 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10004 }
10005
95554aad
TT
10006 attr = dwarf2_attr (die, DW_AT_import, cu);
10007 if (attr != NULL)
10008 {
0826b30a 10009 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10010 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10011 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10012 dwarf2_per_cu_data *per_cu
ab432490 10013 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10014
58990295
TV
10015 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10016 into another compilation unit, at root level. Regard this as a hint,
10017 and ignore it. */
10018 if (die->parent && die->parent->parent == NULL
10019 && per_cu->unit_type == DW_UT_compile
10020 && per_cu->lang == language_cplus)
10021 return;
10022
69d751e3 10023 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10024 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 10025 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 10026
ae640021 10027 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10028 }
10029}
10030
4c8aa72d
PA
10031/* RAII object that represents a process_die scope: i.e.,
10032 starts/finishes processing a DIE. */
10033class process_die_scope
adde2bff 10034{
4c8aa72d
PA
10035public:
10036 process_die_scope (die_info *die, dwarf2_cu *cu)
10037 : m_die (die), m_cu (cu)
10038 {
10039 /* We should only be processing DIEs not already in process. */
10040 gdb_assert (!m_die->in_process);
10041 m_die->in_process = true;
10042 }
8c3cb9fa 10043
4c8aa72d
PA
10044 ~process_die_scope ()
10045 {
10046 m_die->in_process = false;
10047
10048 /* If we're done processing the DIE for the CU that owns the line
10049 header, we don't need the line header anymore. */
10050 if (m_cu->line_header_die_owner == m_die)
10051 {
10052 delete m_cu->line_header;
10053 m_cu->line_header = NULL;
10054 m_cu->line_header_die_owner = NULL;
10055 }
10056 }
10057
10058private:
10059 die_info *m_die;
10060 dwarf2_cu *m_cu;
10061};
adde2bff 10062
c906108c
SS
10063/* Process a die and its children. */
10064
10065static void
e7c27a73 10066process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10067{
4c8aa72d 10068 process_die_scope scope (die, cu);
adde2bff 10069
c906108c
SS
10070 switch (die->tag)
10071 {
10072 case DW_TAG_padding:
10073 break;
10074 case DW_TAG_compile_unit:
95554aad 10075 case DW_TAG_partial_unit:
e7c27a73 10076 read_file_scope (die, cu);
c906108c 10077 break;
348e048f
DE
10078 case DW_TAG_type_unit:
10079 read_type_unit_scope (die, cu);
10080 break;
c906108c 10081 case DW_TAG_subprogram:
0a4b0913
AB
10082 /* Nested subprograms in Fortran get a prefix. */
10083 if (cu->language == language_fortran
10084 && die->parent != NULL
10085 && die->parent->tag == DW_TAG_subprogram)
10086 cu->processing_has_namespace_info = true;
10087 /* Fall through. */
c906108c 10088 case DW_TAG_inlined_subroutine:
edb3359d 10089 read_func_scope (die, cu);
c906108c
SS
10090 break;
10091 case DW_TAG_lexical_block:
14898363
L
10092 case DW_TAG_try_block:
10093 case DW_TAG_catch_block:
e7c27a73 10094 read_lexical_block_scope (die, cu);
c906108c 10095 break;
216f72a1 10096 case DW_TAG_call_site:
96408a79
SA
10097 case DW_TAG_GNU_call_site:
10098 read_call_site_scope (die, cu);
10099 break;
c906108c 10100 case DW_TAG_class_type:
680b30c7 10101 case DW_TAG_interface_type:
c906108c
SS
10102 case DW_TAG_structure_type:
10103 case DW_TAG_union_type:
134d01f1 10104 process_structure_scope (die, cu);
c906108c
SS
10105 break;
10106 case DW_TAG_enumeration_type:
134d01f1 10107 process_enumeration_scope (die, cu);
c906108c 10108 break;
134d01f1 10109
f792889a
DJ
10110 /* These dies have a type, but processing them does not create
10111 a symbol or recurse to process the children. Therefore we can
10112 read them on-demand through read_type_die. */
c906108c 10113 case DW_TAG_subroutine_type:
72019c9c 10114 case DW_TAG_set_type:
c906108c 10115 case DW_TAG_array_type:
c906108c 10116 case DW_TAG_pointer_type:
c906108c 10117 case DW_TAG_ptr_to_member_type:
c906108c 10118 case DW_TAG_reference_type:
4297a3f0 10119 case DW_TAG_rvalue_reference_type:
c906108c 10120 case DW_TAG_string_type:
c906108c 10121 break;
134d01f1 10122
c906108c 10123 case DW_TAG_base_type:
a02abb62 10124 case DW_TAG_subrange_type:
cb249c71 10125 case DW_TAG_typedef:
134d01f1
DJ
10126 /* Add a typedef symbol for the type definition, if it has a
10127 DW_AT_name. */
f792889a 10128 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10129 break;
c906108c 10130 case DW_TAG_common_block:
e7c27a73 10131 read_common_block (die, cu);
c906108c
SS
10132 break;
10133 case DW_TAG_common_inclusion:
10134 break;
d9fa45fe 10135 case DW_TAG_namespace:
9068261f 10136 cu->processing_has_namespace_info = true;
e7c27a73 10137 read_namespace (die, cu);
d9fa45fe 10138 break;
5d7cb8df 10139 case DW_TAG_module:
9068261f 10140 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10141 read_module (die, cu);
10142 break;
d9fa45fe 10143 case DW_TAG_imported_declaration:
9068261f 10144 cu->processing_has_namespace_info = true;
74921315
KS
10145 if (read_namespace_alias (die, cu))
10146 break;
86a73007
TT
10147 /* The declaration is not a global namespace alias. */
10148 /* Fall through. */
d9fa45fe 10149 case DW_TAG_imported_module:
9068261f 10150 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10151 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10152 || cu->language != language_fortran))
b98664d3 10153 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10154 dwarf_tag_name (die->tag));
10155 read_import_statement (die, cu);
d9fa45fe 10156 break;
95554aad
TT
10157
10158 case DW_TAG_imported_unit:
10159 process_imported_unit_die (die, cu);
10160 break;
10161
71a3c369
TT
10162 case DW_TAG_variable:
10163 read_variable (die, cu);
10164 break;
10165
c906108c 10166 default:
e7c27a73 10167 new_symbol (die, NULL, cu);
c906108c
SS
10168 break;
10169 }
10170}
ca69b9e6
DE
10171\f
10172/* DWARF name computation. */
c906108c 10173
94af9270
KS
10174/* A helper function for dwarf2_compute_name which determines whether DIE
10175 needs to have the name of the scope prepended to the name listed in the
10176 die. */
10177
10178static int
10179die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10180{
1c809c68
TT
10181 struct attribute *attr;
10182
94af9270
KS
10183 switch (die->tag)
10184 {
10185 case DW_TAG_namespace:
10186 case DW_TAG_typedef:
10187 case DW_TAG_class_type:
10188 case DW_TAG_interface_type:
10189 case DW_TAG_structure_type:
10190 case DW_TAG_union_type:
10191 case DW_TAG_enumeration_type:
10192 case DW_TAG_enumerator:
10193 case DW_TAG_subprogram:
08a76f8a 10194 case DW_TAG_inlined_subroutine:
94af9270 10195 case DW_TAG_member:
74921315 10196 case DW_TAG_imported_declaration:
94af9270
KS
10197 return 1;
10198
10199 case DW_TAG_variable:
c2b0a229 10200 case DW_TAG_constant:
94af9270
KS
10201 /* We only need to prefix "globally" visible variables. These include
10202 any variable marked with DW_AT_external or any variable that
10203 lives in a namespace. [Variables in anonymous namespaces
10204 require prefixing, but they are not DW_AT_external.] */
10205
10206 if (dwarf2_attr (die, DW_AT_specification, cu))
10207 {
10208 struct dwarf2_cu *spec_cu = cu;
9a619af0 10209
94af9270
KS
10210 return die_needs_namespace (die_specification (die, &spec_cu),
10211 spec_cu);
10212 }
10213
1c809c68 10214 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10215 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10216 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10217 return 0;
10218 /* A variable in a lexical block of some kind does not need a
10219 namespace, even though in C++ such variables may be external
10220 and have a mangled name. */
10221 if (die->parent->tag == DW_TAG_lexical_block
10222 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10223 || die->parent->tag == DW_TAG_catch_block
10224 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10225 return 0;
10226 return 1;
94af9270
KS
10227
10228 default:
10229 return 0;
10230 }
10231}
10232
73b9be8b
KS
10233/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10234 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10235 defined for the given DIE. */
10236
10237static struct attribute *
10238dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10239{
10240 struct attribute *attr;
10241
10242 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10243 if (attr == NULL)
10244 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10245
10246 return attr;
10247}
10248
10249/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10250 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10251 defined for the given DIE. */
10252
10253static const char *
10254dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10255{
10256 const char *linkage_name;
10257
10258 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10259 if (linkage_name == NULL)
10260 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10261
787de330
TT
10262 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10263 See https://github.com/rust-lang/rust/issues/32925. */
10264 if (cu->language == language_rust && linkage_name != NULL
10265 && strchr (linkage_name, '{') != NULL)
10266 linkage_name = NULL;
10267
73b9be8b
KS
10268 return linkage_name;
10269}
10270
94af9270 10271/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10272 compute the physname for the object, which include a method's:
9c37b5ae 10273 - formal parameters (C++),
a766d390 10274 - receiver type (Go),
a766d390
DE
10275
10276 The term "physname" is a bit confusing.
10277 For C++, for example, it is the demangled name.
10278 For Go, for example, it's the mangled name.
94af9270 10279
af6b7be1
JB
10280 For Ada, return the DIE's linkage name rather than the fully qualified
10281 name. PHYSNAME is ignored..
10282
5989a64e 10283 The result is allocated on the objfile->per_bfd's obstack and
45940949 10284 canonicalized. */
94af9270
KS
10285
10286static const char *
15d034d0
TT
10287dwarf2_compute_name (const char *name,
10288 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10289 int physname)
10290{
5e22e966 10291 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10292
94af9270
KS
10293 if (name == NULL)
10294 name = dwarf2_name (die, cu);
10295
2ee7123e
DE
10296 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10297 but otherwise compute it by typename_concat inside GDB.
10298 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10299 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10300 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10301 will set the demangled name to the result of dwarf2_full_name, and it is
10302 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10303 if (cu->language == language_ada
10304 || (cu->language == language_fortran && physname))
10305 {
10306 /* For Ada unit, we prefer the linkage name over the name, as
10307 the former contains the exported name, which the user expects
10308 to be able to reference. Ideally, we want the user to be able
10309 to reference this entity using either natural or linkage name,
10310 but we haven't started looking at this enhancement yet. */
73b9be8b 10311 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10312
2ee7123e
DE
10313 if (linkage_name != NULL)
10314 return linkage_name;
f55ee35c
JK
10315 }
10316
94af9270
KS
10317 /* These are the only languages we know how to qualify names in. */
10318 if (name != NULL
9c37b5ae 10319 && (cu->language == language_cplus
c44af4eb
TT
10320 || cu->language == language_fortran || cu->language == language_d
10321 || cu->language == language_rust))
94af9270
KS
10322 {
10323 if (die_needs_namespace (die, cu))
10324 {
0d5cff50 10325 const char *prefix;
34a68019 10326 const char *canonical_name = NULL;
94af9270 10327
d7e74731
PA
10328 string_file buf;
10329
94af9270 10330 prefix = determine_prefix (die, cu);
94af9270
KS
10331 if (*prefix != '\0')
10332 {
43816ebc
TT
10333 gdb::unique_xmalloc_ptr<char> prefixed_name
10334 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10335
43816ebc 10336 buf.puts (prefixed_name.get ());
94af9270
KS
10337 }
10338 else
d7e74731 10339 buf.puts (name);
94af9270 10340
98bfdba5
PA
10341 /* Template parameters may be specified in the DIE's DW_AT_name, or
10342 as children with DW_TAG_template_type_param or
10343 DW_TAG_value_type_param. If the latter, add them to the name
10344 here. If the name already has template parameters, then
10345 skip this step; some versions of GCC emit both, and
10346 it is more efficient to use the pre-computed name.
10347
10348 Something to keep in mind about this process: it is very
10349 unlikely, or in some cases downright impossible, to produce
10350 something that will match the mangled name of a function.
10351 If the definition of the function has the same debug info,
10352 we should be able to match up with it anyway. But fallbacks
10353 using the minimal symbol, for instance to find a method
10354 implemented in a stripped copy of libstdc++, will not work.
10355 If we do not have debug info for the definition, we will have to
10356 match them up some other way.
10357
10358 When we do name matching there is a related problem with function
10359 templates; two instantiated function templates are allowed to
10360 differ only by their return types, which we do not add here. */
10361
10362 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10363 {
10364 struct attribute *attr;
10365 struct die_info *child;
10366 int first = 1;
10367
10368 die->building_fullname = 1;
10369
10370 for (child = die->child; child != NULL; child = child->sibling)
10371 {
10372 struct type *type;
12df843f 10373 LONGEST value;
d521ce57 10374 const gdb_byte *bytes;
98bfdba5
PA
10375 struct dwarf2_locexpr_baton *baton;
10376 struct value *v;
10377
10378 if (child->tag != DW_TAG_template_type_param
10379 && child->tag != DW_TAG_template_value_param)
10380 continue;
10381
10382 if (first)
10383 {
d7e74731 10384 buf.puts ("<");
98bfdba5
PA
10385 first = 0;
10386 }
10387 else
d7e74731 10388 buf.puts (", ");
98bfdba5
PA
10389
10390 attr = dwarf2_attr (child, DW_AT_type, cu);
10391 if (attr == NULL)
10392 {
b98664d3 10393 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10394 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10395 continue;
10396 }
10397 type = die_type (child, cu);
10398
10399 if (child->tag == DW_TAG_template_type_param)
10400 {
c1ec8cea
TT
10401 c_print_type (type, "", &buf, -1, 0, cu->language,
10402 &type_print_raw_options);
98bfdba5
PA
10403 continue;
10404 }
10405
10406 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10407 if (attr == NULL)
10408 {
b98664d3 10409 complaint (_("template parameter missing "
3e43a32a 10410 "DW_AT_const_value"));
d7e74731 10411 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10412 continue;
10413 }
10414
10415 dwarf2_const_value_attr (attr, type, name,
10416 &cu->comp_unit_obstack, cu,
10417 &value, &bytes, &baton);
10418
10419 if (TYPE_NOSIGN (type))
10420 /* GDB prints characters as NUMBER 'CHAR'. If that's
10421 changed, this can use value_print instead. */
d7e74731 10422 c_printchar (value, type, &buf);
98bfdba5
PA
10423 else
10424 {
10425 struct value_print_options opts;
10426
10427 if (baton != NULL)
10428 v = dwarf2_evaluate_loc_desc (type, NULL,
10429 baton->data,
10430 baton->size,
9f47c707
SM
10431 baton->per_cu,
10432 baton->per_objfile);
98bfdba5
PA
10433 else if (bytes != NULL)
10434 {
10435 v = allocate_value (type);
10436 memcpy (value_contents_writeable (v), bytes,
10437 TYPE_LENGTH (type));
10438 }
10439 else
10440 v = value_from_longest (type, value);
10441
3e43a32a
MS
10442 /* Specify decimal so that we do not depend on
10443 the radix. */
98bfdba5
PA
10444 get_formatted_print_options (&opts, 'd');
10445 opts.raw = 1;
d7e74731 10446 value_print (v, &buf, &opts);
98bfdba5 10447 release_value (v);
98bfdba5
PA
10448 }
10449 }
10450
10451 die->building_fullname = 0;
10452
10453 if (!first)
10454 {
10455 /* Close the argument list, with a space if necessary
10456 (nested templates). */
d7e74731
PA
10457 if (!buf.empty () && buf.string ().back () == '>')
10458 buf.puts (" >");
98bfdba5 10459 else
d7e74731 10460 buf.puts (">");
98bfdba5
PA
10461 }
10462 }
10463
9c37b5ae 10464 /* For C++ methods, append formal parameter type
94af9270 10465 information, if PHYSNAME. */
6e70227d 10466
94af9270 10467 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10468 && cu->language == language_cplus)
94af9270
KS
10469 {
10470 struct type *type = read_type_die (die, cu);
10471
d7e74731 10472 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10473 &type_print_raw_options);
94af9270 10474
9c37b5ae 10475 if (cu->language == language_cplus)
94af9270 10476 {
60430eff
DJ
10477 /* Assume that an artificial first parameter is
10478 "this", but do not crash if it is not. RealView
10479 marks unnamed (and thus unused) parameters as
10480 artificial; there is no way to differentiate
10481 the two cases. */
1f704f76 10482 if (type->num_fields () > 0
94af9270 10483 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10484 && type->field (0).type ()->code () == TYPE_CODE_PTR
10485 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10486 buf.puts (" const");
94af9270
KS
10487 }
10488 }
10489
d7e74731 10490 const std::string &intermediate_name = buf.string ();
94af9270
KS
10491
10492 if (cu->language == language_cplus)
34a68019 10493 canonical_name
322a8516 10494 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10495 objfile);
34a68019
TT
10496
10497 /* If we only computed INTERMEDIATE_NAME, or if
10498 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10499 intern it. */
322a8516 10500 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10501 name = objfile->intern (intermediate_name);
34a68019
TT
10502 else
10503 name = canonical_name;
94af9270
KS
10504 }
10505 }
10506
10507 return name;
10508}
10509
0114d602
DJ
10510/* Return the fully qualified name of DIE, based on its DW_AT_name.
10511 If scope qualifiers are appropriate they will be added. The result
34a68019 10512 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10513 not have a name. NAME may either be from a previous call to
10514 dwarf2_name or NULL.
10515
9c37b5ae 10516 The output string will be canonicalized (if C++). */
0114d602
DJ
10517
10518static const char *
15d034d0 10519dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10520{
94af9270
KS
10521 return dwarf2_compute_name (name, die, cu, 0);
10522}
0114d602 10523
94af9270
KS
10524/* Construct a physname for the given DIE in CU. NAME may either be
10525 from a previous call to dwarf2_name or NULL. The result will be
10526 allocated on the objfile_objstack or NULL if the DIE does not have a
10527 name.
0114d602 10528
9c37b5ae 10529 The output string will be canonicalized (if C++). */
0114d602 10530
94af9270 10531static const char *
15d034d0 10532dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10533{
5e22e966 10534 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10535 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10536 int need_copy = 1;
10537
10538 /* In this case dwarf2_compute_name is just a shortcut not building anything
10539 on its own. */
10540 if (!die_needs_namespace (die, cu))
10541 return dwarf2_compute_name (name, die, cu, 1);
10542
906bb4c5
TT
10543 if (cu->language != language_rust)
10544 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10545
10546 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10547 has computed. */
791afaa2 10548 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10549 if (mangled != NULL)
900e11f9 10550 {
900e11f9 10551
59cc4834
JB
10552 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10553 {
10554 /* Do nothing (do not demangle the symbol name). */
10555 }
10556 else if (cu->language == language_go)
a766d390 10557 {
5e2db402
TT
10558 /* This is a lie, but we already lie to the caller new_symbol.
10559 new_symbol assumes we return the mangled name.
a766d390 10560 This just undoes that lie until things are cleaned up. */
a766d390
DE
10561 }
10562 else
10563 {
0eb876f5
JB
10564 /* Use DMGL_RET_DROP for C++ template functions to suppress
10565 their return type. It is easier for GDB users to search
10566 for such functions as `name(params)' than `long name(params)'.
10567 In such case the minimal symbol names do not match the full
10568 symbol names but for template functions there is never a need
10569 to look up their definition from their declaration so
10570 the only disadvantage remains the minimal symbol variant
10571 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10572 demangled.reset (gdb_demangle (mangled,
10573 (DMGL_PARAMS | DMGL_ANSI
10574 | DMGL_RET_DROP)));
a766d390 10575 }
900e11f9 10576 if (demangled)
791afaa2 10577 canon = demangled.get ();
900e11f9
JK
10578 else
10579 {
10580 canon = mangled;
10581 need_copy = 0;
10582 }
10583 }
10584
10585 if (canon == NULL || check_physname)
10586 {
10587 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10588
10589 if (canon != NULL && strcmp (physname, canon) != 0)
10590 {
10591 /* It may not mean a bug in GDB. The compiler could also
10592 compute DW_AT_linkage_name incorrectly. But in such case
10593 GDB would need to be bug-to-bug compatible. */
10594
b98664d3 10595 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10596 "(from linkage <%s>) - DIE at %s [in module %s]"),
10597 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10598 objfile_name (objfile));
900e11f9
JK
10599
10600 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10601 is available here - over computed PHYSNAME. It is safer
10602 against both buggy GDB and buggy compilers. */
10603
10604 retval = canon;
10605 }
10606 else
10607 {
10608 retval = physname;
10609 need_copy = 0;
10610 }
10611 }
10612 else
10613 retval = canon;
10614
10615 if (need_copy)
be1e3d3e 10616 retval = objfile->intern (retval);
900e11f9 10617
900e11f9 10618 return retval;
0114d602
DJ
10619}
10620
74921315
KS
10621/* Inspect DIE in CU for a namespace alias. If one exists, record
10622 a new symbol for it.
10623
10624 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10625
10626static int
10627read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10628{
10629 struct attribute *attr;
10630
10631 /* If the die does not have a name, this is not a namespace
10632 alias. */
10633 attr = dwarf2_attr (die, DW_AT_name, cu);
10634 if (attr != NULL)
10635 {
10636 int num;
10637 struct die_info *d = die;
10638 struct dwarf2_cu *imported_cu = cu;
10639
10640 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10641 keep inspecting DIEs until we hit the underlying import. */
10642#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10643 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10644 {
10645 attr = dwarf2_attr (d, DW_AT_import, cu);
10646 if (attr == NULL)
10647 break;
10648
10649 d = follow_die_ref (d, attr, &imported_cu);
10650 if (d->tag != DW_TAG_imported_declaration)
10651 break;
10652 }
10653
10654 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10655 {
b98664d3 10656 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10657 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10658 return 0;
10659 }
10660
10661 if (attr != NULL)
10662 {
10663 struct type *type;
0826b30a 10664 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10665
aa66c379 10666 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10667 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10668 {
10669 /* This declaration is a global namespace alias. Add
10670 a symbol for it whose type is the aliased namespace. */
10671 new_symbol (die, type, cu);
10672 return 1;
10673 }
10674 }
10675 }
10676
10677 return 0;
10678}
10679
22cee43f 10680/* Return the using directives repository (global or local?) to use in the
804d2729 10681 current context for CU.
22cee43f
PMR
10682
10683 For Ada, imported declarations can materialize renamings, which *may* be
10684 global. However it is impossible (for now?) in DWARF to distinguish
10685 "external" imported declarations and "static" ones. As all imported
10686 declarations seem to be static in all other languages, make them all CU-wide
10687 global only in Ada. */
10688
10689static struct using_direct **
804d2729 10690using_directives (struct dwarf2_cu *cu)
22cee43f 10691{
c24bdb02
KS
10692 if (cu->language == language_ada
10693 && cu->get_builder ()->outermost_context_p ())
10694 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10695 else
c24bdb02 10696 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10697}
10698
27aa8d6a
SW
10699/* Read the import statement specified by the given die and record it. */
10700
10701static void
10702read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10703{
5e22e966 10704 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10705 struct attribute *import_attr;
32019081 10706 struct die_info *imported_die, *child_die;
de4affc9 10707 struct dwarf2_cu *imported_cu;
27aa8d6a 10708 const char *imported_name;
794684b6 10709 const char *imported_name_prefix;
13387711
SW
10710 const char *canonical_name;
10711 const char *import_alias;
10712 const char *imported_declaration = NULL;
794684b6 10713 const char *import_prefix;
eb1e02fd 10714 std::vector<const char *> excludes;
13387711 10715
27aa8d6a
SW
10716 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10717 if (import_attr == NULL)
10718 {
b98664d3 10719 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10720 dwarf_tag_name (die->tag));
10721 return;
10722 }
10723
de4affc9
CC
10724 imported_cu = cu;
10725 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10726 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10727 if (imported_name == NULL)
10728 {
10729 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10730
10731 The import in the following code:
10732 namespace A
10733 {
10734 typedef int B;
10735 }
10736
10737 int main ()
10738 {
10739 using A::B;
10740 B b;
10741 return b;
10742 }
10743
10744 ...
10745 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10746 <52> DW_AT_decl_file : 1
10747 <53> DW_AT_decl_line : 6
10748 <54> DW_AT_import : <0x75>
10749 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10750 <59> DW_AT_name : B
10751 <5b> DW_AT_decl_file : 1
10752 <5c> DW_AT_decl_line : 2
10753 <5d> DW_AT_type : <0x6e>
10754 ...
10755 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10756 <76> DW_AT_byte_size : 4
10757 <77> DW_AT_encoding : 5 (signed)
10758
10759 imports the wrong die ( 0x75 instead of 0x58 ).
10760 This case will be ignored until the gcc bug is fixed. */
10761 return;
10762 }
10763
82856980
SW
10764 /* Figure out the local name after import. */
10765 import_alias = dwarf2_name (die, cu);
27aa8d6a 10766
794684b6
SW
10767 /* Figure out where the statement is being imported to. */
10768 import_prefix = determine_prefix (die, cu);
10769
10770 /* Figure out what the scope of the imported die is and prepend it
10771 to the name of the imported die. */
de4affc9 10772 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10773
f55ee35c
JK
10774 if (imported_die->tag != DW_TAG_namespace
10775 && imported_die->tag != DW_TAG_module)
794684b6 10776 {
13387711
SW
10777 imported_declaration = imported_name;
10778 canonical_name = imported_name_prefix;
794684b6 10779 }
13387711 10780 else if (strlen (imported_name_prefix) > 0)
12aaed36 10781 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10782 imported_name_prefix,
10783 (cu->language == language_d ? "." : "::"),
10784 imported_name, (char *) NULL);
13387711
SW
10785 else
10786 canonical_name = imported_name;
794684b6 10787
32019081
JK
10788 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10789 for (child_die = die->child; child_die && child_die->tag;
436c571c 10790 child_die = child_die->sibling)
32019081
JK
10791 {
10792 /* DWARF-4: A Fortran use statement with a “rename list” may be
10793 represented by an imported module entry with an import attribute
10794 referring to the module and owned entries corresponding to those
10795 entities that are renamed as part of being imported. */
10796
10797 if (child_die->tag != DW_TAG_imported_declaration)
10798 {
b98664d3 10799 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10800 "- DIE at %s [in module %s]"),
10801 sect_offset_str (child_die->sect_off),
10802 objfile_name (objfile));
32019081
JK
10803 continue;
10804 }
10805
10806 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10807 if (import_attr == NULL)
10808 {
b98664d3 10809 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10810 dwarf_tag_name (child_die->tag));
10811 continue;
10812 }
10813
10814 imported_cu = cu;
10815 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10816 &imported_cu);
10817 imported_name = dwarf2_name (imported_die, imported_cu);
10818 if (imported_name == NULL)
10819 {
b98664d3 10820 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10821 "imported name - DIE at %s [in module %s]"),
10822 sect_offset_str (child_die->sect_off),
10823 objfile_name (objfile));
32019081
JK
10824 continue;
10825 }
10826
eb1e02fd 10827 excludes.push_back (imported_name);
32019081
JK
10828
10829 process_die (child_die, cu);
10830 }
10831
804d2729 10832 add_using_directive (using_directives (cu),
22cee43f
PMR
10833 import_prefix,
10834 canonical_name,
10835 import_alias,
10836 imported_declaration,
10837 excludes,
10838 0,
10839 &objfile->objfile_obstack);
27aa8d6a
SW
10840}
10841
5230b05a
WT
10842/* ICC<14 does not output the required DW_AT_declaration on incomplete
10843 types, but gives them a size of zero. Starting with version 14,
10844 ICC is compatible with GCC. */
10845
9068261f 10846static bool
5230b05a
WT
10847producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10848{
10849 if (!cu->checked_producer)
10850 check_producer (cu);
10851
10852 return cu->producer_is_icc_lt_14;
10853}
10854
eb77c9df
AB
10855/* ICC generates a DW_AT_type for C void functions. This was observed on
10856 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10857 which says that void functions should not have a DW_AT_type. */
10858
10859static bool
10860producer_is_icc (struct dwarf2_cu *cu)
10861{
10862 if (!cu->checked_producer)
10863 check_producer (cu);
10864
10865 return cu->producer_is_icc;
10866}
10867
1b80a9fa
JK
10868/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10869 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10870 this, it was first present in GCC release 4.3.0. */
10871
9068261f 10872static bool
1b80a9fa
JK
10873producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10874{
10875 if (!cu->checked_producer)
10876 check_producer (cu);
10877
10878 return cu->producer_is_gcc_lt_4_3;
10879}
10880
d721ba37
PA
10881static file_and_directory
10882find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10883{
d721ba37
PA
10884 file_and_directory res;
10885
9291a0cd
TT
10886 /* Find the filename. Do not use dwarf2_name here, since the filename
10887 is not a source language identifier. */
d721ba37
PA
10888 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10889 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10890
d721ba37
PA
10891 if (res.comp_dir == NULL
10892 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10893 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10894 {
d721ba37
PA
10895 res.comp_dir_storage = ldirname (res.name);
10896 if (!res.comp_dir_storage.empty ())
10897 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10898 }
d721ba37 10899 if (res.comp_dir != NULL)
9291a0cd
TT
10900 {
10901 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10902 directory, get rid of it. */
d721ba37 10903 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10904
d721ba37
PA
10905 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10906 res.comp_dir = cp + 1;
9291a0cd
TT
10907 }
10908
d721ba37
PA
10909 if (res.name == NULL)
10910 res.name = "<unknown>";
10911
10912 return res;
9291a0cd
TT
10913}
10914
f4dc4d17
DE
10915/* Handle DW_AT_stmt_list for a compilation unit.
10916 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10917 COMP_DIR is the compilation directory. LOWPC is passed to
10918 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10919
10920static void
10921handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10922 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10923{
976ca316 10924 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10925 struct attribute *attr;
527f3840
JK
10926 struct line_header line_header_local;
10927 hashval_t line_header_local_hash;
527f3840
JK
10928 void **slot;
10929 int decode_mapping;
2ab95328 10930
f4dc4d17
DE
10931 gdb_assert (! cu->per_cu->is_debug_types);
10932
2ab95328 10933 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10934 if (attr == NULL)
10935 return;
10936
9c541725 10937 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10938
10939 /* The line header hash table is only created if needed (it exists to
10940 prevent redundant reading of the line table for partial_units).
10941 If we're given a partial_unit, we'll need it. If we're given a
10942 compile_unit, then use the line header hash table if it's already
10943 created, but don't create one just yet. */
10944
976ca316 10945 if (per_objfile->line_header_hash == NULL
527f3840 10946 && die->tag == DW_TAG_partial_unit)
2ab95328 10947 {
976ca316 10948 per_objfile->line_header_hash
d15acc42
TT
10949 .reset (htab_create_alloc (127, line_header_hash_voidp,
10950 line_header_eq_voidp,
10951 free_line_header_voidp,
10952 xcalloc, xfree));
527f3840 10953 }
2ab95328 10954
9c541725 10955 line_header_local.sect_off = line_offset;
527f3840
JK
10956 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10957 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10958 if (per_objfile->line_header_hash != NULL)
527f3840 10959 {
976ca316 10960 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10961 &line_header_local,
10962 line_header_local_hash, NO_INSERT);
10963
10964 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10965 is not present in *SLOT (since if there is something in *SLOT then
10966 it will be for a partial_unit). */
10967 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10968 {
527f3840 10969 gdb_assert (*slot != NULL);
9a3c8263 10970 cu->line_header = (struct line_header *) *slot;
527f3840 10971 return;
dee91e82 10972 }
2ab95328 10973 }
527f3840
JK
10974
10975 /* dwarf_decode_line_header does not yet provide sufficient information.
10976 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10977 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10978 if (lh == NULL)
527f3840 10979 return;
4c8aa72d
PA
10980
10981 cu->line_header = lh.release ();
10982 cu->line_header_die_owner = die;
527f3840 10983
976ca316 10984 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10985 slot = NULL;
10986 else
10987 {
976ca316 10988 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10989 &line_header_local,
10990 line_header_local_hash, INSERT);
10991 gdb_assert (slot != NULL);
10992 }
10993 if (slot != NULL && *slot == NULL)
10994 {
10995 /* This newly decoded line number information unit will be owned
10996 by line_header_hash hash table. */
10997 *slot = cu->line_header;
4c8aa72d 10998 cu->line_header_die_owner = NULL;
527f3840
JK
10999 }
11000 else
11001 {
11002 /* We cannot free any current entry in (*slot) as that struct line_header
11003 may be already used by multiple CUs. Create only temporary decoded
11004 line_header for this CU - it may happen at most once for each line
11005 number information unit. And if we're not using line_header_hash
11006 then this is what we want as well. */
11007 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11008 }
11009 decode_mapping = (die->tag != DW_TAG_partial_unit);
11010 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11011 decode_mapping);
fff8551c 11012
2ab95328
TT
11013}
11014
95554aad 11015/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11016
c906108c 11017static void
e7c27a73 11018read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11019{
976ca316
SM
11020 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11021 struct objfile *objfile = per_objfile->objfile;
08feed99 11022 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11023 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11024 CORE_ADDR highpc = ((CORE_ADDR) 0);
11025 struct attribute *attr;
c906108c 11026 struct die_info *child_die;
e142c38c 11027 CORE_ADDR baseaddr;
6e70227d 11028
380618d6 11029 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11030 baseaddr = objfile->text_section_offset ();
c906108c 11031
fae299cd 11032 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11033
11034 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11035 from finish_block. */
2acceee2 11036 if (lowpc == ((CORE_ADDR) -1))
c906108c 11037 lowpc = highpc;
3e29f34a 11038 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11039
d721ba37 11040 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11041
f4b8a18d
KW
11042 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11043 standardised yet. As a workaround for the language detection we fall
11044 back to the DW_AT_producer string. */
11045 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11046 cu->language = language_opencl;
11047
3019eac3
DE
11048 /* Similar hack for Go. */
11049 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11050 set_cu_language (DW_LANG_Go, cu);
11051
c24bdb02 11052 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11053
11054 /* Decode line number information if present. We do this before
11055 processing child DIEs, so that the line header table is available
11056 for DW_AT_decl_file. */
d721ba37 11057 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11058
11059 /* Process all dies in compilation unit. */
11060 if (die->child != NULL)
11061 {
11062 child_die = die->child;
11063 while (child_die && child_die->tag)
11064 {
11065 process_die (child_die, cu);
436c571c 11066 child_die = child_die->sibling;
3019eac3
DE
11067 }
11068 }
11069
11070 /* Decode macro information, if present. Dwarf 2 macro information
11071 refers to information in the line number info statement program
11072 header, so we can only read it if we've read the header
11073 successfully. */
0af92d60
JK
11074 attr = dwarf2_attr (die, DW_AT_macros, cu);
11075 if (attr == NULL)
11076 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11077 if (attr && cu->line_header)
11078 {
11079 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11080 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11081
43f3e411 11082 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11083 }
11084 else
11085 {
11086 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11087 if (attr && cu->line_header)
11088 {
11089 unsigned int macro_offset = DW_UNSND (attr);
11090
43f3e411 11091 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11092 }
11093 }
3019eac3
DE
11094}
11095
c24bdb02
KS
11096void
11097dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11098{
f4dc4d17
DE
11099 struct type_unit_group *tu_group;
11100 int first_time;
3019eac3 11101 struct attribute *attr;
9c541725 11102 unsigned int i;
0186c6a7 11103 struct signatured_type *sig_type;
3019eac3 11104
f4dc4d17 11105 gdb_assert (per_cu->is_debug_types);
0186c6a7 11106 sig_type = (struct signatured_type *) per_cu;
3019eac3 11107
c24bdb02 11108 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11109
f4dc4d17 11110 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11111 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11112 if (sig_type->type_unit_group == NULL)
c24bdb02 11113 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11114 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11115
11116 /* If we've already processed this stmt_list there's no real need to
11117 do it again, we could fake it and just recreate the part we need
11118 (file name,index -> symtab mapping). If data shows this optimization
11119 is useful we can do it then. */
8adb8487
TT
11120 type_unit_group_unshareable *tug_unshare
11121 = per_objfile->get_type_unit_group_unshareable (tu_group);
11122 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11123
11124 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11125 debug info. */
fff8551c 11126 line_header_up lh;
f4dc4d17 11127 if (attr != NULL)
3019eac3 11128 {
9c541725 11129 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11130 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11131 }
11132 if (lh == NULL)
11133 {
11134 if (first_time)
c24bdb02 11135 start_symtab ("", NULL, 0);
f4dc4d17
DE
11136 else
11137 {
8adb8487 11138 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11139 gdb_assert (m_builder == nullptr);
8adb8487 11140 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11141 m_builder.reset (new struct buildsym_compunit
11142 (COMPUNIT_OBJFILE (cust), "",
11143 COMPUNIT_DIRNAME (cust),
11144 compunit_language (cust),
11145 0, cust));
770479f2 11146 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11147 }
f4dc4d17 11148 return;
3019eac3
DE
11149 }
11150
c24bdb02
KS
11151 line_header = lh.release ();
11152 line_header_die_owner = die;
3019eac3 11153
f4dc4d17
DE
11154 if (first_time)
11155 {
c24bdb02 11156 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11157
1fd60fc0
DE
11158 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11159 still initializing it, and our caller (a few levels up)
11160 process_full_type_unit still needs to know if this is the first
11161 time. */
11162
8adb8487 11163 tug_unshare->symtabs
4ac93832
TT
11164 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11165 struct symtab *, line_header->file_names_size ());
3019eac3 11166
7ba99d21
AT
11167 auto &file_names = line_header->file_names ();
11168 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11169 {
7ba99d21 11170 file_entry &fe = file_names[i];
c24bdb02
KS
11171 dwarf2_start_subfile (this, fe.name,
11172 fe.include_dir (line_header));
11173 buildsym_compunit *b = get_builder ();
11174 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11175 {
4c8aa72d
PA
11176 /* NOTE: start_subfile will recognize when it's been
11177 passed a file it has already seen. So we can't
11178 assume there's a simple mapping from
11179 cu->line_header->file_names to subfiles, plus
11180 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11181 b->get_current_subfile ()->symtab
11182 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11183 }
11184
c24bdb02 11185 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11186 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11187 }
11188 }
11189 else
3019eac3 11190 {
c24bdb02 11191 gdb_assert (m_builder == nullptr);
8adb8487 11192 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11193 m_builder.reset (new struct buildsym_compunit
11194 (COMPUNIT_OBJFILE (cust), "",
11195 COMPUNIT_DIRNAME (cust),
11196 compunit_language (cust),
11197 0, cust));
770479f2 11198 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11199
7ba99d21
AT
11200 auto &file_names = line_header->file_names ();
11201 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11202 {
7ba99d21 11203 file_entry &fe = file_names[i];
8adb8487 11204 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11205 }
3019eac3
DE
11206 }
11207
f4dc4d17
DE
11208 /* The main symtab is allocated last. Type units don't have DW_AT_name
11209 so they don't have a "real" (so to speak) symtab anyway.
11210 There is later code that will assign the main symtab to all symbols
11211 that don't have one. We need to handle the case of a symbol with a
11212 missing symtab (DW_AT_decl_file) anyway. */
11213}
3019eac3 11214
f4dc4d17
DE
11215/* Process DW_TAG_type_unit.
11216 For TUs we want to skip the first top level sibling if it's not the
11217 actual type being defined by this TU. In this case the first top
11218 level sibling is there to provide context only. */
3019eac3 11219
f4dc4d17
DE
11220static void
11221read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11222{
11223 struct die_info *child_die;
3019eac3 11224
f4dc4d17
DE
11225 prepare_one_comp_unit (cu, die, language_minimal);
11226
11227 /* Initialize (or reinitialize) the machinery for building symtabs.
11228 We do this before processing child DIEs, so that the line header table
11229 is available for DW_AT_decl_file. */
c24bdb02 11230 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11231
11232 if (die->child != NULL)
11233 {
11234 child_die = die->child;
11235 while (child_die && child_die->tag)
11236 {
11237 process_die (child_die, cu);
436c571c 11238 child_die = child_die->sibling;
f4dc4d17
DE
11239 }
11240 }
3019eac3
DE
11241}
11242\f
80626a55
DE
11243/* DWO/DWP files.
11244
11245 http://gcc.gnu.org/wiki/DebugFission
11246 http://gcc.gnu.org/wiki/DebugFissionDWP
11247
11248 To simplify handling of both DWO files ("object" files with the DWARF info)
11249 and DWP files (a file with the DWOs packaged up into one file), we treat
11250 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11251
11252static hashval_t
11253hash_dwo_file (const void *item)
11254{
9a3c8263 11255 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11256 hashval_t hash;
3019eac3 11257
a2ce51a0
DE
11258 hash = htab_hash_string (dwo_file->dwo_name);
11259 if (dwo_file->comp_dir != NULL)
11260 hash += htab_hash_string (dwo_file->comp_dir);
11261 return hash;
3019eac3
DE
11262}
11263
11264static int
11265eq_dwo_file (const void *item_lhs, const void *item_rhs)
11266{
9a3c8263
SM
11267 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11268 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11269
a2ce51a0
DE
11270 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11271 return 0;
11272 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11273 return lhs->comp_dir == rhs->comp_dir;
11274 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11275}
11276
11277/* Allocate a hash table for DWO files. */
11278
51ac9db5 11279static htab_up
298e9637 11280allocate_dwo_file_hash_table ()
3019eac3 11281{
51ac9db5
SM
11282 auto delete_dwo_file = [] (void *item)
11283 {
11284 struct dwo_file *dwo_file = (struct dwo_file *) item;
11285
11286 delete dwo_file;
11287 };
11288
bc68fb19
TT
11289 return htab_up (htab_create_alloc (41,
11290 hash_dwo_file,
11291 eq_dwo_file,
11292 delete_dwo_file,
11293 xcalloc, xfree));
3019eac3
DE
11294}
11295
80626a55
DE
11296/* Lookup DWO file DWO_NAME. */
11297
11298static void **
976ca316 11299lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11300 const char *dwo_name,
11301 const char *comp_dir)
80626a55
DE
11302{
11303 struct dwo_file find_entry;
11304 void **slot;
11305
976ca316
SM
11306 if (per_objfile->per_bfd->dwo_files == NULL)
11307 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11308
0ac5b59e
DE
11309 find_entry.dwo_name = dwo_name;
11310 find_entry.comp_dir = comp_dir;
976ca316 11311 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11312 INSERT);
80626a55
DE
11313
11314 return slot;
11315}
11316
3019eac3
DE
11317static hashval_t
11318hash_dwo_unit (const void *item)
11319{
9a3c8263 11320 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11321
11322 /* This drops the top 32 bits of the id, but is ok for a hash. */
11323 return dwo_unit->signature;
11324}
11325
11326static int
11327eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11328{
9a3c8263
SM
11329 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11330 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11331
11332 /* The signature is assumed to be unique within the DWO file.
11333 So while object file CU dwo_id's always have the value zero,
11334 that's OK, assuming each object file DWO file has only one CU,
11335 and that's the rule for now. */
11336 return lhs->signature == rhs->signature;
11337}
11338
11339/* Allocate a hash table for DWO CUs,TUs.
11340 There is one of these tables for each of CUs,TUs for each DWO file. */
11341
b0b6a987 11342static htab_up
298e9637 11343allocate_dwo_unit_table ()
3019eac3
DE
11344{
11345 /* Start out with a pretty small number.
11346 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11347 return htab_up (htab_create_alloc (3,
11348 hash_dwo_unit,
11349 eq_dwo_unit,
11350 NULL, xcalloc, xfree));
3019eac3
DE
11351}
11352
19c3d4c9 11353/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11354
11355static void
19c3d4c9
DE
11356create_dwo_cu_reader (const struct die_reader_specs *reader,
11357 const gdb_byte *info_ptr,
11358 struct die_info *comp_unit_die,
c0ab21c2
TT
11359 struct dwo_file *dwo_file,
11360 struct dwo_unit *dwo_unit)
3019eac3
DE
11361{
11362 struct dwarf2_cu *cu = reader->cu;
9c541725 11363 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11364 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11365
a084a2a6
AT
11366 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11367 if (!signature.has_value ())
3019eac3 11368 {
b98664d3 11369 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11370 " its dwo_id [in module %s]"),
9d8780f0 11371 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11372 return;
11373 }
11374
3019eac3 11375 dwo_unit->dwo_file = dwo_file;
a084a2a6 11376 dwo_unit->signature = *signature;
8a0459fd 11377 dwo_unit->section = section;
9c541725 11378 dwo_unit->sect_off = sect_off;
3019eac3
DE
11379 dwo_unit->length = cu->per_cu->length;
11380
b4f54984 11381 if (dwarf_read_debug)
9d8780f0
SM
11382 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11383 sect_offset_str (sect_off),
9c541725 11384 hex_string (dwo_unit->signature));
3019eac3
DE
11385}
11386
33c5cd75 11387/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11388 Note: This function processes DWO files only, not DWP files. */
3019eac3 11389
33c5cd75 11390static void
976ca316 11391create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11392 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11393 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11394{
976ca316
SM
11395 struct objfile *objfile = per_objfile->objfile;
11396 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11397 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11398
96b79293 11399 section.read (objfile);
33c5cd75 11400 info_ptr = section.buffer;
3019eac3
DE
11401
11402 if (info_ptr == NULL)
33c5cd75 11403 return;
3019eac3 11404
b4f54984 11405 if (dwarf_read_debug)
19c3d4c9
DE
11406 {
11407 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11408 section.get_name (),
11409 section.get_file_name ());
19c3d4c9 11410 }
3019eac3 11411
33c5cd75 11412 end_ptr = info_ptr + section.size;
3019eac3
DE
11413 while (info_ptr < end_ptr)
11414 {
11415 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11416 struct dwo_unit read_unit {};
33c5cd75
DB
11417 struct dwo_unit *dwo_unit;
11418 void **slot;
11419 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11420
11421 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11422 per_cu.per_bfd = per_bfd;
3019eac3 11423 per_cu.is_debug_types = 0;
33c5cd75
DB
11424 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11425 per_cu.section = &section;
11426
976ca316 11427 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11428 if (!reader.dummy_p)
11429 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11430 &dwo_file, &read_unit);
33c5cd75
DB
11431 info_ptr += per_cu.length;
11432
11433 // If the unit could not be parsed, skip it.
c0ab21c2 11434 if (read_unit.dwo_file == NULL)
33c5cd75 11435 continue;
3019eac3 11436
33c5cd75 11437 if (cus_htab == NULL)
298e9637 11438 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11439
1859c670 11440 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11441 struct dwo_unit);
c0ab21c2 11442 *dwo_unit = read_unit;
b0b6a987 11443 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11444 gdb_assert (slot != NULL);
11445 if (*slot != NULL)
19c3d4c9 11446 {
33c5cd75
DB
11447 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11448 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11449
b98664d3 11450 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11451 " the entry at offset %s, signature %s"),
11452 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11453 hex_string (dwo_unit->signature));
19c3d4c9 11454 }
33c5cd75 11455 *slot = (void *)dwo_unit;
3019eac3 11456 }
3019eac3
DE
11457}
11458
80626a55
DE
11459/* DWP file .debug_{cu,tu}_index section format:
11460 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11461
d2415c6c
DE
11462 DWP Version 1:
11463
80626a55
DE
11464 Both index sections have the same format, and serve to map a 64-bit
11465 signature to a set of section numbers. Each section begins with a header,
11466 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11467 indexes, and a pool of 32-bit section numbers. The index sections will be
11468 aligned at 8-byte boundaries in the file.
11469
d2415c6c
DE
11470 The index section header consists of:
11471
11472 V, 32 bit version number
11473 -, 32 bits unused
11474 N, 32 bit number of compilation units or type units in the index
11475 M, 32 bit number of slots in the hash table
80626a55 11476
d2415c6c 11477 Numbers are recorded using the byte order of the application binary.
80626a55 11478
d2415c6c
DE
11479 The hash table begins at offset 16 in the section, and consists of an array
11480 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11481 order of the application binary). Unused slots in the hash table are 0.
11482 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11483
d2415c6c
DE
11484 The parallel table begins immediately after the hash table
11485 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11486 array of 32-bit indexes (using the byte order of the application binary),
11487 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11488 table contains a 32-bit index into the pool of section numbers. For unused
11489 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11490
73869dc2
DE
11491 The pool of section numbers begins immediately following the hash table
11492 (at offset 16 + 12 * M from the beginning of the section). The pool of
11493 section numbers consists of an array of 32-bit words (using the byte order
11494 of the application binary). Each item in the array is indexed starting
11495 from 0. The hash table entry provides the index of the first section
11496 number in the set. Additional section numbers in the set follow, and the
11497 set is terminated by a 0 entry (section number 0 is not used in ELF).
11498
11499 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11500 section must be the first entry in the set, and the .debug_abbrev.dwo must
11501 be the second entry. Other members of the set may follow in any order.
11502
11503 ---
11504
11505 DWP Version 2:
11506
11507 DWP Version 2 combines all the .debug_info, etc. sections into one,
11508 and the entries in the index tables are now offsets into these sections.
11509 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11510 section.
11511
11512 Index Section Contents:
11513 Header
11514 Hash Table of Signatures dwp_hash_table.hash_table
11515 Parallel Table of Indices dwp_hash_table.unit_table
11516 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11517 Table of Section Sizes dwp_hash_table.v2.sizes
11518
11519 The index section header consists of:
11520
11521 V, 32 bit version number
11522 L, 32 bit number of columns in the table of section offsets
11523 N, 32 bit number of compilation units or type units in the index
11524 M, 32 bit number of slots in the hash table
11525
11526 Numbers are recorded using the byte order of the application binary.
11527
11528 The hash table has the same format as version 1.
11529 The parallel table of indices has the same format as version 1,
11530 except that the entries are origin-1 indices into the table of sections
11531 offsets and the table of section sizes.
11532
11533 The table of offsets begins immediately following the parallel table
11534 (at offset 16 + 12 * M from the beginning of the section). The table is
11535 a two-dimensional array of 32-bit words (using the byte order of the
11536 application binary), with L columns and N+1 rows, in row-major order.
11537 Each row in the array is indexed starting from 0. The first row provides
11538 a key to the remaining rows: each column in this row provides an identifier
11539 for a debug section, and the offsets in the same column of subsequent rows
11540 refer to that section. The section identifiers are:
11541
11542 DW_SECT_INFO 1 .debug_info.dwo
11543 DW_SECT_TYPES 2 .debug_types.dwo
11544 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11545 DW_SECT_LINE 4 .debug_line.dwo
11546 DW_SECT_LOC 5 .debug_loc.dwo
11547 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11548 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11549 DW_SECT_MACRO 8 .debug_macro.dwo
11550
11551 The offsets provided by the CU and TU index sections are the base offsets
11552 for the contributions made by each CU or TU to the corresponding section
11553 in the package file. Each CU and TU header contains an abbrev_offset
11554 field, used to find the abbreviations table for that CU or TU within the
11555 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11556 be interpreted as relative to the base offset given in the index section.
11557 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11558 should be interpreted as relative to the base offset for .debug_line.dwo,
11559 and offsets into other debug sections obtained from DWARF attributes should
11560 also be interpreted as relative to the corresponding base offset.
11561
11562 The table of sizes begins immediately following the table of offsets.
11563 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11564 with L columns and N rows, in row-major order. Each row in the array is
11565 indexed starting from 1 (row 0 is shared by the two tables).
11566
11567 ---
11568
11569 Hash table lookup is handled the same in version 1 and 2:
11570
11571 We assume that N and M will not exceed 2^32 - 1.
11572 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11573
d2415c6c
DE
11574 Given a 64-bit compilation unit signature or a type signature S, an entry
11575 in the hash table is located as follows:
80626a55 11576
d2415c6c
DE
11577 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11578 the low-order k bits all set to 1.
80626a55 11579
d2415c6c 11580 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11581
d2415c6c
DE
11582 3) If the hash table entry at index H matches the signature, use that
11583 entry. If the hash table entry at index H is unused (all zeroes),
11584 terminate the search: the signature is not present in the table.
80626a55 11585
d2415c6c 11586 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11587
d2415c6c 11588 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11589 to stop at an unused slot or find the match. */
80626a55
DE
11590
11591/* Create a hash table to map DWO IDs to their CU/TU entry in
11592 .debug_{info,types}.dwo in DWP_FILE.
11593 Returns NULL if there isn't one.
11594 Note: This function processes DWP files only, not DWO files. */
11595
11596static struct dwp_hash_table *
976ca316 11597create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11598 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11599{
976ca316 11600 struct objfile *objfile = per_objfile->objfile;
400174b1 11601 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11602 const gdb_byte *index_ptr, *index_end;
80626a55 11603 struct dwarf2_section_info *index;
73869dc2 11604 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11605 struct dwp_hash_table *htab;
11606
11607 if (is_debug_types)
11608 index = &dwp_file->sections.tu_index;
11609 else
11610 index = &dwp_file->sections.cu_index;
11611
96b79293 11612 if (index->empty ())
80626a55 11613 return NULL;
96b79293 11614 index->read (objfile);
80626a55
DE
11615
11616 index_ptr = index->buffer;
11617 index_end = index_ptr + index->size;
11618
11619 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11620 index_ptr += 4;
11621 if (version == 2)
11622 nr_columns = read_4_bytes (dbfd, index_ptr);
11623 else
11624 nr_columns = 0;
11625 index_ptr += 4;
80626a55
DE
11626 nr_units = read_4_bytes (dbfd, index_ptr);
11627 index_ptr += 4;
11628 nr_slots = read_4_bytes (dbfd, index_ptr);
11629 index_ptr += 4;
11630
73869dc2 11631 if (version != 1 && version != 2)
80626a55 11632 {
21aa081e 11633 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11634 " [in module %s]"),
21aa081e 11635 pulongest (version), dwp_file->name);
80626a55
DE
11636 }
11637 if (nr_slots != (nr_slots & -nr_slots))
11638 {
21aa081e 11639 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11640 " is not power of 2 [in module %s]"),
21aa081e 11641 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11642 }
11643
976ca316 11644 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11645 htab->version = version;
11646 htab->nr_columns = nr_columns;
80626a55
DE
11647 htab->nr_units = nr_units;
11648 htab->nr_slots = nr_slots;
11649 htab->hash_table = index_ptr;
11650 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11651
11652 /* Exit early if the table is empty. */
11653 if (nr_slots == 0 || nr_units == 0
11654 || (version == 2 && nr_columns == 0))
11655 {
11656 /* All must be zero. */
11657 if (nr_slots != 0 || nr_units != 0
11658 || (version == 2 && nr_columns != 0))
11659 {
b98664d3 11660 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11661 " all zero [in modules %s]"),
11662 dwp_file->name);
11663 }
11664 return htab;
11665 }
11666
11667 if (version == 1)
11668 {
11669 htab->section_pool.v1.indices =
11670 htab->unit_table + sizeof (uint32_t) * nr_slots;
11671 /* It's harder to decide whether the section is too small in v1.
11672 V1 is deprecated anyway so we punt. */
11673 }
11674 else
11675 {
11676 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11677 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11678 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11679 /* Reverse map for error checking. */
11680 int ids_seen[DW_SECT_MAX + 1];
11681 int i;
11682
11683 if (nr_columns < 2)
11684 {
11685 error (_("Dwarf Error: bad DWP hash table, too few columns"
11686 " in section table [in module %s]"),
11687 dwp_file->name);
11688 }
11689 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11690 {
11691 error (_("Dwarf Error: bad DWP hash table, too many columns"
11692 " in section table [in module %s]"),
11693 dwp_file->name);
11694 }
04fd5eed
GB
11695 memset (ids, 255, sizeof_ids);
11696 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11697 for (i = 0; i < nr_columns; ++i)
11698 {
11699 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11700
11701 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11702 {
11703 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11704 " in section table [in module %s]"),
11705 id, dwp_file->name);
11706 }
11707 if (ids_seen[id] != -1)
11708 {
11709 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11710 " id %d in section table [in module %s]"),
11711 id, dwp_file->name);
11712 }
11713 ids_seen[id] = i;
11714 ids[i] = id;
11715 }
11716 /* Must have exactly one info or types section. */
11717 if (((ids_seen[DW_SECT_INFO] != -1)
11718 + (ids_seen[DW_SECT_TYPES] != -1))
11719 != 1)
11720 {
11721 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11722 " DWO info/types section [in module %s]"),
11723 dwp_file->name);
11724 }
11725 /* Must have an abbrev section. */
11726 if (ids_seen[DW_SECT_ABBREV] == -1)
11727 {
11728 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11729 " section [in module %s]"),
11730 dwp_file->name);
11731 }
11732 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11733 htab->section_pool.v2.sizes =
11734 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11735 * nr_units * nr_columns);
11736 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11737 * nr_units * nr_columns))
11738 > index_end)
11739 {
11740 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11741 " [in module %s]"),
11742 dwp_file->name);
11743 }
11744 }
80626a55
DE
11745
11746 return htab;
11747}
11748
11749/* Update SECTIONS with the data from SECTP.
11750
11751 This function is like the other "locate" section routines that are
11752 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11753 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11754
11755 The result is non-zero for success, or zero if an error was found. */
11756
11757static int
73869dc2
DE
11758locate_v1_virtual_dwo_sections (asection *sectp,
11759 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11760{
11761 const struct dwop_section_names *names = &dwop_section_names;
11762
11763 if (section_is_p (sectp->name, &names->abbrev_dwo))
11764 {
11765 /* There can be only one. */
049412e3 11766 if (sections->abbrev.s.section != NULL)
80626a55 11767 return 0;
049412e3 11768 sections->abbrev.s.section = sectp;
fd361982 11769 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11770 }
11771 else if (section_is_p (sectp->name, &names->info_dwo)
11772 || section_is_p (sectp->name, &names->types_dwo))
11773 {
11774 /* There can be only one. */
049412e3 11775 if (sections->info_or_types.s.section != NULL)
80626a55 11776 return 0;
049412e3 11777 sections->info_or_types.s.section = sectp;
fd361982 11778 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11779 }
11780 else if (section_is_p (sectp->name, &names->line_dwo))
11781 {
11782 /* There can be only one. */
049412e3 11783 if (sections->line.s.section != NULL)
80626a55 11784 return 0;
049412e3 11785 sections->line.s.section = sectp;
fd361982 11786 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11787 }
11788 else if (section_is_p (sectp->name, &names->loc_dwo))
11789 {
11790 /* There can be only one. */
049412e3 11791 if (sections->loc.s.section != NULL)
80626a55 11792 return 0;
049412e3 11793 sections->loc.s.section = sectp;
fd361982 11794 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11795 }
11796 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11797 {
11798 /* There can be only one. */
049412e3 11799 if (sections->macinfo.s.section != NULL)
80626a55 11800 return 0;
049412e3 11801 sections->macinfo.s.section = sectp;
fd361982 11802 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11803 }
11804 else if (section_is_p (sectp->name, &names->macro_dwo))
11805 {
11806 /* There can be only one. */
049412e3 11807 if (sections->macro.s.section != NULL)
80626a55 11808 return 0;
049412e3 11809 sections->macro.s.section = sectp;
fd361982 11810 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11811 }
11812 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11813 {
11814 /* There can be only one. */
049412e3 11815 if (sections->str_offsets.s.section != NULL)
80626a55 11816 return 0;
049412e3 11817 sections->str_offsets.s.section = sectp;
fd361982 11818 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11819 }
11820 else
11821 {
11822 /* No other kind of section is valid. */
11823 return 0;
11824 }
11825
11826 return 1;
11827}
11828
73869dc2
DE
11829/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11830 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11831 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11832 This is for DWP version 1 files. */
80626a55
DE
11833
11834static struct dwo_unit *
976ca316 11835create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11836 struct dwp_file *dwp_file,
73869dc2
DE
11837 uint32_t unit_index,
11838 const char *comp_dir,
11839 ULONGEST signature, int is_debug_types)
80626a55 11840{
73869dc2
DE
11841 const struct dwp_hash_table *dwp_htab =
11842 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11843 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11844 const char *kind = is_debug_types ? "TU" : "CU";
11845 struct dwo_file *dwo_file;
11846 struct dwo_unit *dwo_unit;
73869dc2 11847 struct virtual_v1_dwo_sections sections;
80626a55 11848 void **dwo_file_slot;
80626a55
DE
11849 int i;
11850
73869dc2
DE
11851 gdb_assert (dwp_file->version == 1);
11852
b4f54984 11853 if (dwarf_read_debug)
80626a55 11854 {
73869dc2 11855 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11856 kind,
73869dc2 11857 pulongest (unit_index), hex_string (signature),
80626a55
DE
11858 dwp_file->name);
11859 }
11860
19ac8c2e 11861 /* Fetch the sections of this DWO unit.
80626a55
DE
11862 Put a limit on the number of sections we look for so that bad data
11863 doesn't cause us to loop forever. */
11864
73869dc2 11865#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11866 (1 /* .debug_info or .debug_types */ \
11867 + 1 /* .debug_abbrev */ \
11868 + 1 /* .debug_line */ \
11869 + 1 /* .debug_loc */ \
11870 + 1 /* .debug_str_offsets */ \
19ac8c2e 11871 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11872 + 1 /* trailing zero */)
11873
11874 memset (&sections, 0, sizeof (sections));
80626a55 11875
73869dc2 11876 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11877 {
11878 asection *sectp;
11879 uint32_t section_nr =
11880 read_4_bytes (dbfd,
73869dc2
DE
11881 dwp_htab->section_pool.v1.indices
11882 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11883
11884 if (section_nr == 0)
11885 break;
11886 if (section_nr >= dwp_file->num_sections)
11887 {
11888 error (_("Dwarf Error: bad DWP hash table, section number too large"
11889 " [in module %s]"),
11890 dwp_file->name);
11891 }
11892
11893 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11894 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11895 {
11896 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11897 " [in module %s]"),
11898 dwp_file->name);
11899 }
11900 }
11901
11902 if (i < 2
96b79293
TT
11903 || sections.info_or_types.empty ()
11904 || sections.abbrev.empty ())
80626a55
DE
11905 {
11906 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11907 " [in module %s]"),
11908 dwp_file->name);
11909 }
73869dc2 11910 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11911 {
11912 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11913 " [in module %s]"),
11914 dwp_file->name);
11915 }
11916
11917 /* It's easier for the rest of the code if we fake a struct dwo_file and
11918 have dwo_unit "live" in that. At least for now.
11919
11920 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11921 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11922 file, we can combine them back into a virtual DWO file to save space
11923 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11924 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11925
791afaa2
TT
11926 std::string virtual_dwo_name =
11927 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11928 sections.abbrev.get_id (),
11929 sections.line.get_id (),
11930 sections.loc.get_id (),
11931 sections.str_offsets.get_id ());
80626a55 11932 /* Can we use an existing virtual DWO file? */
976ca316 11933 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11934 comp_dir);
80626a55
DE
11935 /* Create one if necessary. */
11936 if (*dwo_file_slot == NULL)
11937 {
b4f54984 11938 if (dwarf_read_debug)
80626a55
DE
11939 {
11940 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11941 virtual_dwo_name.c_str ());
80626a55 11942 }
51ac9db5 11943 dwo_file = new struct dwo_file;
976ca316 11944 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11945 dwo_file->comp_dir = comp_dir;
80626a55
DE
11946 dwo_file->sections.abbrev = sections.abbrev;
11947 dwo_file->sections.line = sections.line;
11948 dwo_file->sections.loc = sections.loc;
11949 dwo_file->sections.macinfo = sections.macinfo;
11950 dwo_file->sections.macro = sections.macro;
11951 dwo_file->sections.str_offsets = sections.str_offsets;
11952 /* The "str" section is global to the entire DWP file. */
11953 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11954 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11955 there's no need to record it in dwo_file.
11956 Also, we can't simply record type sections in dwo_file because
11957 we record a pointer into the vector in dwo_unit. As we collect more
11958 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11959 for it, invalidating all copies of pointers into the previous
11960 contents. */
80626a55
DE
11961 *dwo_file_slot = dwo_file;
11962 }
11963 else
11964 {
b4f54984 11965 if (dwarf_read_debug)
80626a55
DE
11966 {
11967 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11968 virtual_dwo_name.c_str ());
80626a55 11969 }
9a3c8263 11970 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11971 }
80626a55 11972
976ca316 11973 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11974 dwo_unit->dwo_file = dwo_file;
11975 dwo_unit->signature = signature;
8d749320 11976 dwo_unit->section =
976ca316 11977 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11978 *dwo_unit->section = sections.info_or_types;
57d63ce2 11979 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11980
11981 return dwo_unit;
11982}
11983
73869dc2
DE
11984/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11985 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11986 piece within that section used by a TU/CU, return a virtual section
11987 of just that piece. */
11988
11989static struct dwarf2_section_info
976ca316 11990create_dwp_v2_section (dwarf2_per_objfile *per_objfile,
ed2dc618 11991 struct dwarf2_section_info *section,
73869dc2
DE
11992 bfd_size_type offset, bfd_size_type size)
11993{
11994 struct dwarf2_section_info result;
11995 asection *sectp;
11996
11997 gdb_assert (section != NULL);
11998 gdb_assert (!section->is_virtual);
11999
12000 memset (&result, 0, sizeof (result));
12001 result.s.containing_section = section;
dc4ccb6f 12002 result.is_virtual = true;
73869dc2
DE
12003
12004 if (size == 0)
12005 return result;
12006
96b79293 12007 sectp = section->get_bfd_section ();
73869dc2
DE
12008
12009 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12010 bounds of the real section. This is a pretty-rare event, so just
12011 flag an error (easier) instead of a warning and trying to cope. */
12012 if (sectp == NULL
fd361982 12013 || offset + size > bfd_section_size (sectp))
73869dc2 12014 {
73869dc2
DE
12015 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12016 " in section %s [in module %s]"),
fd361982 12017 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12018 objfile_name (per_objfile->objfile));
73869dc2
DE
12019 }
12020
12021 result.virtual_offset = offset;
12022 result.size = size;
12023 return result;
12024}
12025
12026/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12027 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12028 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12029 This is for DWP version 2 files. */
12030
12031static struct dwo_unit *
976ca316 12032create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12033 struct dwp_file *dwp_file,
73869dc2
DE
12034 uint32_t unit_index,
12035 const char *comp_dir,
12036 ULONGEST signature, int is_debug_types)
12037{
73869dc2
DE
12038 const struct dwp_hash_table *dwp_htab =
12039 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12040 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12041 const char *kind = is_debug_types ? "TU" : "CU";
12042 struct dwo_file *dwo_file;
12043 struct dwo_unit *dwo_unit;
12044 struct virtual_v2_dwo_sections sections;
12045 void **dwo_file_slot;
73869dc2
DE
12046 int i;
12047
12048 gdb_assert (dwp_file->version == 2);
12049
b4f54984 12050 if (dwarf_read_debug)
73869dc2
DE
12051 {
12052 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12053 kind,
12054 pulongest (unit_index), hex_string (signature),
12055 dwp_file->name);
12056 }
12057
12058 /* Fetch the section offsets of this DWO unit. */
12059
12060 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12061
12062 for (i = 0; i < dwp_htab->nr_columns; ++i)
12063 {
12064 uint32_t offset = read_4_bytes (dbfd,
12065 dwp_htab->section_pool.v2.offsets
12066 + (((unit_index - 1) * dwp_htab->nr_columns
12067 + i)
12068 * sizeof (uint32_t)));
12069 uint32_t size = read_4_bytes (dbfd,
12070 dwp_htab->section_pool.v2.sizes
12071 + (((unit_index - 1) * dwp_htab->nr_columns
12072 + i)
12073 * sizeof (uint32_t)));
12074
12075 switch (dwp_htab->section_pool.v2.section_ids[i])
12076 {
12077 case DW_SECT_INFO:
12078 case DW_SECT_TYPES:
12079 sections.info_or_types_offset = offset;
12080 sections.info_or_types_size = size;
12081 break;
12082 case DW_SECT_ABBREV:
12083 sections.abbrev_offset = offset;
12084 sections.abbrev_size = size;
12085 break;
12086 case DW_SECT_LINE:
12087 sections.line_offset = offset;
12088 sections.line_size = size;
12089 break;
12090 case DW_SECT_LOC:
12091 sections.loc_offset = offset;
12092 sections.loc_size = size;
12093 break;
12094 case DW_SECT_STR_OFFSETS:
12095 sections.str_offsets_offset = offset;
12096 sections.str_offsets_size = size;
12097 break;
12098 case DW_SECT_MACINFO:
12099 sections.macinfo_offset = offset;
12100 sections.macinfo_size = size;
12101 break;
12102 case DW_SECT_MACRO:
12103 sections.macro_offset = offset;
12104 sections.macro_size = size;
12105 break;
12106 }
12107 }
12108
12109 /* It's easier for the rest of the code if we fake a struct dwo_file and
12110 have dwo_unit "live" in that. At least for now.
12111
12112 The DWP file can be made up of a random collection of CUs and TUs.
12113 However, for each CU + set of TUs that came from the same original DWO
12114 file, we can combine them back into a virtual DWO file to save space
12115 (fewer struct dwo_file objects to allocate). Remember that for really
12116 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12117
791afaa2
TT
12118 std::string virtual_dwo_name =
12119 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12120 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12121 (long) (sections.line_size ? sections.line_offset : 0),
12122 (long) (sections.loc_size ? sections.loc_offset : 0),
12123 (long) (sections.str_offsets_size
12124 ? sections.str_offsets_offset : 0));
73869dc2 12125 /* Can we use an existing virtual DWO file? */
976ca316 12126 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12127 comp_dir);
73869dc2
DE
12128 /* Create one if necessary. */
12129 if (*dwo_file_slot == NULL)
12130 {
b4f54984 12131 if (dwarf_read_debug)
73869dc2
DE
12132 {
12133 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12134 virtual_dwo_name.c_str ());
73869dc2 12135 }
51ac9db5 12136 dwo_file = new struct dwo_file;
976ca316 12137 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12138 dwo_file->comp_dir = comp_dir;
12139 dwo_file->sections.abbrev =
976ca316 12140 create_dwp_v2_section (per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12141 sections.abbrev_offset, sections.abbrev_size);
12142 dwo_file->sections.line =
976ca316 12143 create_dwp_v2_section (per_objfile, &dwp_file->sections.line,
73869dc2
DE
12144 sections.line_offset, sections.line_size);
12145 dwo_file->sections.loc =
976ca316 12146 create_dwp_v2_section (per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12147 sections.loc_offset, sections.loc_size);
12148 dwo_file->sections.macinfo =
976ca316 12149 create_dwp_v2_section (per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12150 sections.macinfo_offset, sections.macinfo_size);
12151 dwo_file->sections.macro =
976ca316 12152 create_dwp_v2_section (per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12153 sections.macro_offset, sections.macro_size);
12154 dwo_file->sections.str_offsets =
976ca316 12155 create_dwp_v2_section (per_objfile,
ed2dc618 12156 &dwp_file->sections.str_offsets,
73869dc2
DE
12157 sections.str_offsets_offset,
12158 sections.str_offsets_size);
12159 /* The "str" section is global to the entire DWP file. */
12160 dwo_file->sections.str = dwp_file->sections.str;
12161 /* The info or types section is assigned below to dwo_unit,
12162 there's no need to record it in dwo_file.
12163 Also, we can't simply record type sections in dwo_file because
12164 we record a pointer into the vector in dwo_unit. As we collect more
12165 types we'll grow the vector and eventually have to reallocate space
12166 for it, invalidating all copies of pointers into the previous
12167 contents. */
12168 *dwo_file_slot = dwo_file;
12169 }
12170 else
12171 {
b4f54984 12172 if (dwarf_read_debug)
73869dc2
DE
12173 {
12174 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12175 virtual_dwo_name.c_str ());
73869dc2 12176 }
9a3c8263 12177 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12178 }
73869dc2 12179
976ca316 12180 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12181 dwo_unit->dwo_file = dwo_file;
12182 dwo_unit->signature = signature;
8d749320 12183 dwo_unit->section =
976ca316
SM
12184 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12185 *dwo_unit->section = create_dwp_v2_section (per_objfile,
ed2dc618 12186 is_debug_types
73869dc2
DE
12187 ? &dwp_file->sections.types
12188 : &dwp_file->sections.info,
12189 sections.info_or_types_offset,
12190 sections.info_or_types_size);
12191 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12192
12193 return dwo_unit;
12194}
12195
57d63ce2
DE
12196/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12197 Returns NULL if the signature isn't found. */
80626a55
DE
12198
12199static struct dwo_unit *
976ca316 12200lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12201 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12202 ULONGEST signature, int is_debug_types)
80626a55 12203{
57d63ce2
DE
12204 const struct dwp_hash_table *dwp_htab =
12205 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12206 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12207 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12208 uint32_t hash = signature & mask;
12209 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12210 unsigned int i;
12211 void **slot;
870f88f7 12212 struct dwo_unit find_dwo_cu;
80626a55
DE
12213
12214 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12215 find_dwo_cu.signature = signature;
19ac8c2e 12216 slot = htab_find_slot (is_debug_types
48b490f2
TT
12217 ? dwp_file->loaded_tus.get ()
12218 : dwp_file->loaded_cus.get (),
19ac8c2e 12219 &find_dwo_cu, INSERT);
80626a55
DE
12220
12221 if (*slot != NULL)
9a3c8263 12222 return (struct dwo_unit *) *slot;
80626a55
DE
12223
12224 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12225 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12226 {
12227 ULONGEST signature_in_table;
12228
12229 signature_in_table =
57d63ce2 12230 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12231 if (signature_in_table == signature)
12232 {
57d63ce2
DE
12233 uint32_t unit_index =
12234 read_4_bytes (dbfd,
12235 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12236
73869dc2
DE
12237 if (dwp_file->version == 1)
12238 {
976ca316
SM
12239 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12240 unit_index, comp_dir,
12241 signature, is_debug_types);
73869dc2
DE
12242 }
12243 else
12244 {
976ca316
SM
12245 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12246 unit_index, comp_dir,
12247 signature, is_debug_types);
73869dc2 12248 }
9a3c8263 12249 return (struct dwo_unit *) *slot;
80626a55
DE
12250 }
12251 if (signature_in_table == 0)
12252 return NULL;
12253 hash = (hash + hash2) & mask;
12254 }
12255
12256 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12257 " [in module %s]"),
12258 dwp_file->name);
12259}
12260
ab5088bf 12261/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12262 Open the file specified by FILE_NAME and hand it off to BFD for
12263 preliminary analysis. Return a newly initialized bfd *, which
12264 includes a canonicalized copy of FILE_NAME.
80626a55 12265 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12266 SEARCH_CWD is true if the current directory is to be searched.
12267 It will be searched before debug-file-directory.
13aaf454
DE
12268 If successful, the file is added to the bfd include table of the
12269 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12270 If unable to find/open the file, return NULL.
3019eac3
DE
12271 NOTE: This function is derived from symfile_bfd_open. */
12272
192b62ce 12273static gdb_bfd_ref_ptr
976ca316 12274try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12275 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12276{
24b9144d 12277 int desc;
9c02c129
DE
12278 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12279 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12280 to debug_file_directory. */
e0cc99a6 12281 const char *search_path;
9c02c129
DE
12282 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12283
e0cc99a6 12284 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12285 if (search_cwd)
12286 {
12287 if (*debug_file_directory != '\0')
e0cc99a6
TT
12288 {
12289 search_path_holder.reset (concat (".", dirname_separator_string,
12290 debug_file_directory,
12291 (char *) NULL));
12292 search_path = search_path_holder.get ();
12293 }
6ac97d4c 12294 else
e0cc99a6 12295 search_path = ".";
6ac97d4c 12296 }
9c02c129 12297 else
e0cc99a6 12298 search_path = debug_file_directory;
3019eac3 12299
24b9144d 12300 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12301 if (is_dwp)
12302 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12303
12304 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12305 desc = openp (search_path, flags, file_name,
3019eac3
DE
12306 O_RDONLY | O_BINARY, &absolute_name);
12307 if (desc < 0)
12308 return NULL;
12309
e0cc99a6
TT
12310 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12311 gnutarget, desc));
9c02c129
DE
12312 if (sym_bfd == NULL)
12313 return NULL;
192b62ce 12314 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12315
192b62ce
TT
12316 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12317 return NULL;
3019eac3 12318
13aaf454
DE
12319 /* Success. Record the bfd as having been included by the objfile's bfd.
12320 This is important because things like demangled_names_hash lives in the
12321 objfile's per_bfd space and may have references to things like symbol
12322 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12323 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12324
3019eac3
DE
12325 return sym_bfd;
12326}
12327
ab5088bf 12328/* Try to open DWO file FILE_NAME.
3019eac3
DE
12329 COMP_DIR is the DW_AT_comp_dir attribute.
12330 The result is the bfd handle of the file.
12331 If there is a problem finding or opening the file, return NULL.
12332 Upon success, the canonicalized path of the file is stored in the bfd,
12333 same as symfile_bfd_open. */
12334
192b62ce 12335static gdb_bfd_ref_ptr
976ca316 12336open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12337 const char *file_name, const char *comp_dir)
3019eac3 12338{
80626a55 12339 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12340 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12341 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12342
12343 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12344
12345 if (comp_dir != NULL)
12346 {
43816ebc
TT
12347 gdb::unique_xmalloc_ptr<char> path_to_try
12348 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12349
12350 /* NOTE: If comp_dir is a relative path, this will also try the
12351 search path, which seems useful. */
976ca316 12352 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12353 0 /*is_dwp*/,
192b62ce 12354 1 /*search_cwd*/));
3019eac3
DE
12355 if (abfd != NULL)
12356 return abfd;
12357 }
12358
12359 /* That didn't work, try debug-file-directory, which, despite its name,
12360 is a list of paths. */
12361
12362 if (*debug_file_directory == '\0')
12363 return NULL;
12364
976ca316 12365 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12366 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12367}
12368
80626a55
DE
12369/* This function is mapped across the sections and remembers the offset and
12370 size of each of the DWO debugging sections we are interested in. */
12371
12372static void
12373dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12374{
9a3c8263 12375 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12376 const struct dwop_section_names *names = &dwop_section_names;
12377
12378 if (section_is_p (sectp->name, &names->abbrev_dwo))
12379 {
049412e3 12380 dwo_sections->abbrev.s.section = sectp;
fd361982 12381 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12382 }
12383 else if (section_is_p (sectp->name, &names->info_dwo))
12384 {
049412e3 12385 dwo_sections->info.s.section = sectp;
fd361982 12386 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12387 }
12388 else if (section_is_p (sectp->name, &names->line_dwo))
12389 {
049412e3 12390 dwo_sections->line.s.section = sectp;
fd361982 12391 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12392 }
12393 else if (section_is_p (sectp->name, &names->loc_dwo))
12394 {
049412e3 12395 dwo_sections->loc.s.section = sectp;
fd361982 12396 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12397 }
41144253 12398 else if (section_is_p (sectp->name, &names->loclists_dwo))
12399 {
12400 dwo_sections->loclists.s.section = sectp;
12401 dwo_sections->loclists.size = bfd_section_size (sectp);
12402 }
80626a55
DE
12403 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12404 {
049412e3 12405 dwo_sections->macinfo.s.section = sectp;
fd361982 12406 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12407 }
12408 else if (section_is_p (sectp->name, &names->macro_dwo))
12409 {
049412e3 12410 dwo_sections->macro.s.section = sectp;
fd361982 12411 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12412 }
12413 else if (section_is_p (sectp->name, &names->str_dwo))
12414 {
049412e3 12415 dwo_sections->str.s.section = sectp;
fd361982 12416 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12417 }
12418 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12419 {
049412e3 12420 dwo_sections->str_offsets.s.section = sectp;
fd361982 12421 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12422 }
12423 else if (section_is_p (sectp->name, &names->types_dwo))
12424 {
12425 struct dwarf2_section_info type_section;
12426
12427 memset (&type_section, 0, sizeof (type_section));
049412e3 12428 type_section.s.section = sectp;
fd361982 12429 type_section.size = bfd_section_size (sectp);
fd5866f6 12430 dwo_sections->types.push_back (type_section);
80626a55
DE
12431 }
12432}
12433
ab5088bf 12434/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12435 by PER_CU. This is for the non-DWP case.
80626a55 12436 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12437
12438static struct dwo_file *
4ab09049
SM
12439open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12440 const char *comp_dir)
3019eac3 12441{
976ca316 12442 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12443
976ca316 12444 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12445 if (dbfd == NULL)
12446 {
b4f54984 12447 if (dwarf_read_debug)
80626a55
DE
12448 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12449 return NULL;
12450 }
263db9a1 12451
51ac9db5 12452 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12453 dwo_file->dwo_name = dwo_name;
12454 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12455 dwo_file->dbfd = std::move (dbfd);
3019eac3 12456
fb1eb2f9 12457 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12458 &dwo_file->sections);
3019eac3 12459
976ca316
SM
12460 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12461 dwo_file->cus);
3019eac3 12462
976ca316 12463 create_debug_types_hash_table (per_objfile, dwo_file.get (),
ed2dc618 12464 dwo_file->sections.types, dwo_file->tus);
3019eac3 12465
b4f54984 12466 if (dwarf_read_debug)
80626a55
DE
12467 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12468
263db9a1 12469 return dwo_file.release ();
3019eac3
DE
12470}
12471
80626a55 12472/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12473 size of each of the DWP debugging sections common to version 1 and 2 that
12474 we are interested in. */
3019eac3 12475
80626a55 12476static void
73869dc2
DE
12477dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12478 void *dwp_file_ptr)
3019eac3 12479{
9a3c8263 12480 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12481 const struct dwop_section_names *names = &dwop_section_names;
12482 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12483
80626a55 12484 /* Record the ELF section number for later lookup: this is what the
73869dc2 12485 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12486 gdb_assert (elf_section_nr < dwp_file->num_sections);
12487 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12488
80626a55
DE
12489 /* Look for specific sections that we need. */
12490 if (section_is_p (sectp->name, &names->str_dwo))
12491 {
049412e3 12492 dwp_file->sections.str.s.section = sectp;
fd361982 12493 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12494 }
12495 else if (section_is_p (sectp->name, &names->cu_index))
12496 {
049412e3 12497 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12498 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12499 }
12500 else if (section_is_p (sectp->name, &names->tu_index))
12501 {
049412e3 12502 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12503 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12504 }
12505}
3019eac3 12506
73869dc2
DE
12507/* This function is mapped across the sections and remembers the offset and
12508 size of each of the DWP version 2 debugging sections that we are interested
12509 in. This is split into a separate function because we don't know if we
12510 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12511
12512static void
12513dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12514{
9a3c8263 12515 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12516 const struct dwop_section_names *names = &dwop_section_names;
12517 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12518
12519 /* Record the ELF section number for later lookup: this is what the
12520 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12521 gdb_assert (elf_section_nr < dwp_file->num_sections);
12522 dwp_file->elf_sections[elf_section_nr] = sectp;
12523
12524 /* Look for specific sections that we need. */
12525 if (section_is_p (sectp->name, &names->abbrev_dwo))
12526 {
049412e3 12527 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12528 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12529 }
12530 else if (section_is_p (sectp->name, &names->info_dwo))
12531 {
049412e3 12532 dwp_file->sections.info.s.section = sectp;
fd361982 12533 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12534 }
12535 else if (section_is_p (sectp->name, &names->line_dwo))
12536 {
049412e3 12537 dwp_file->sections.line.s.section = sectp;
fd361982 12538 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12539 }
12540 else if (section_is_p (sectp->name, &names->loc_dwo))
12541 {
049412e3 12542 dwp_file->sections.loc.s.section = sectp;
fd361982 12543 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12544 }
12545 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12546 {
049412e3 12547 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12548 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12549 }
12550 else if (section_is_p (sectp->name, &names->macro_dwo))
12551 {
049412e3 12552 dwp_file->sections.macro.s.section = sectp;
fd361982 12553 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12554 }
12555 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12556 {
049412e3 12557 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12558 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12559 }
12560 else if (section_is_p (sectp->name, &names->types_dwo))
12561 {
049412e3 12562 dwp_file->sections.types.s.section = sectp;
fd361982 12563 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12564 }
12565}
12566
80626a55 12567/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12568
80626a55
DE
12569static hashval_t
12570hash_dwp_loaded_cutus (const void *item)
12571{
9a3c8263 12572 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12573
80626a55
DE
12574 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12575 return dwo_unit->signature;
3019eac3
DE
12576}
12577
80626a55 12578/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12579
80626a55
DE
12580static int
12581eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12582{
9a3c8263
SM
12583 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12584 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12585
80626a55
DE
12586 return dua->signature == dub->signature;
12587}
3019eac3 12588
80626a55 12589/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12590
48b490f2 12591static htab_up
298e9637 12592allocate_dwp_loaded_cutus_table ()
80626a55 12593{
48b490f2
TT
12594 return htab_up (htab_create_alloc (3,
12595 hash_dwp_loaded_cutus,
12596 eq_dwp_loaded_cutus,
12597 NULL, xcalloc, xfree));
80626a55 12598}
3019eac3 12599
ab5088bf
DE
12600/* Try to open DWP file FILE_NAME.
12601 The result is the bfd handle of the file.
12602 If there is a problem finding or opening the file, return NULL.
12603 Upon success, the canonicalized path of the file is stored in the bfd,
12604 same as symfile_bfd_open. */
12605
192b62ce 12606static gdb_bfd_ref_ptr
976ca316 12607open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12608{
976ca316 12609 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12610 1 /*is_dwp*/,
192b62ce 12611 1 /*search_cwd*/));
6ac97d4c
DE
12612 if (abfd != NULL)
12613 return abfd;
12614
12615 /* Work around upstream bug 15652.
12616 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12617 [Whether that's a "bug" is debatable, but it is getting in our way.]
12618 We have no real idea where the dwp file is, because gdb's realpath-ing
12619 of the executable's path may have discarded the needed info.
12620 [IWBN if the dwp file name was recorded in the executable, akin to
12621 .gnu_debuglink, but that doesn't exist yet.]
12622 Strip the directory from FILE_NAME and search again. */
12623 if (*debug_file_directory != '\0')
12624 {
12625 /* Don't implicitly search the current directory here.
12626 If the user wants to search "." to handle this case,
12627 it must be added to debug-file-directory. */
976ca316
SM
12628 return try_open_dwop_file (per_objfile, lbasename (file_name),
12629 1 /*is_dwp*/,
6ac97d4c
DE
12630 0 /*search_cwd*/);
12631 }
12632
12633 return NULL;
ab5088bf
DE
12634}
12635
80626a55
DE
12636/* Initialize the use of the DWP file for the current objfile.
12637 By convention the name of the DWP file is ${objfile}.dwp.
12638 The result is NULL if it can't be found. */
a766d390 12639
400174b1 12640static std::unique_ptr<struct dwp_file>
976ca316 12641open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12642{
976ca316 12643 struct objfile *objfile = per_objfile->objfile;
80626a55 12644
82bf32bc
JK
12645 /* Try to find first .dwp for the binary file before any symbolic links
12646 resolving. */
6c447423
DE
12647
12648 /* If the objfile is a debug file, find the name of the real binary
12649 file and get the name of dwp file from there. */
d721ba37 12650 std::string dwp_name;
6c447423
DE
12651 if (objfile->separate_debug_objfile_backlink != NULL)
12652 {
12653 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12654 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12655
d721ba37 12656 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12657 }
12658 else
d721ba37
PA
12659 dwp_name = objfile->original_name;
12660
12661 dwp_name += ".dwp";
80626a55 12662
976ca316 12663 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12664 if (dbfd == NULL
12665 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12666 {
12667 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12668 dwp_name = objfile_name (objfile);
12669 dwp_name += ".dwp";
976ca316 12670 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12671 }
12672
80626a55
DE
12673 if (dbfd == NULL)
12674 {
b4f54984 12675 if (dwarf_read_debug)
d721ba37 12676 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12677 return std::unique_ptr<dwp_file> ();
3019eac3 12678 }
400174b1
TT
12679
12680 const char *name = bfd_get_filename (dbfd.get ());
12681 std::unique_ptr<struct dwp_file> dwp_file
12682 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12683
0a0f4c01 12684 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12685 dwp_file->elf_sections =
976ca316 12686 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12687 dwp_file->num_sections, asection *);
12688
400174b1
TT
12689 bfd_map_over_sections (dwp_file->dbfd.get (),
12690 dwarf2_locate_common_dwp_sections,
12691 dwp_file.get ());
80626a55 12692
976ca316 12693 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12694
976ca316 12695 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12696
73869dc2 12697 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12698 if (dwp_file->cus && dwp_file->tus
12699 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12700 {
12701 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12702 pretty bizarre. We use pulongest here because that's the established
4d65956b 12703 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12704 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12705 " TU version %s [in DWP file %s]"),
12706 pulongest (dwp_file->cus->version),
d721ba37 12707 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12708 }
08302ed2
DE
12709
12710 if (dwp_file->cus)
12711 dwp_file->version = dwp_file->cus->version;
12712 else if (dwp_file->tus)
12713 dwp_file->version = dwp_file->tus->version;
12714 else
12715 dwp_file->version = 2;
73869dc2
DE
12716
12717 if (dwp_file->version == 2)
400174b1
TT
12718 bfd_map_over_sections (dwp_file->dbfd.get (),
12719 dwarf2_locate_v2_dwp_sections,
12720 dwp_file.get ());
73869dc2 12721
298e9637
SM
12722 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12723 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12724
b4f54984 12725 if (dwarf_read_debug)
80626a55
DE
12726 {
12727 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12728 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12729 " %s CUs, %s TUs\n",
12730 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12731 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12732 }
12733
12734 return dwp_file;
3019eac3 12735}
c906108c 12736
ab5088bf
DE
12737/* Wrapper around open_and_init_dwp_file, only open it once. */
12738
12739static struct dwp_file *
976ca316 12740get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12741{
976ca316 12742 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12743 {
976ca316
SM
12744 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12745 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12746 }
976ca316 12747 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12748}
12749
80626a55
DE
12750/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12751 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12752 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12753 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12754 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12755
12756 This is called, for example, when wanting to read a variable with a
12757 complex location. Therefore we don't want to do file i/o for every call.
12758 Therefore we don't want to look for a DWO file on every call.
12759 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12760 then we check if we've already seen DWO_NAME, and only THEN do we check
12761 for a DWO file.
12762
1c658ad5 12763 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12764 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12765
3019eac3 12766static struct dwo_unit *
4ab09049 12767lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12768 ULONGEST signature, int is_debug_types)
3019eac3 12769{
976ca316
SM
12770 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12771 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12772 const char *kind = is_debug_types ? "TU" : "CU";
12773 void **dwo_file_slot;
3019eac3 12774 struct dwo_file *dwo_file;
80626a55 12775 struct dwp_file *dwp_file;
cb1df416 12776
6a506a2d
DE
12777 /* First see if there's a DWP file.
12778 If we have a DWP file but didn't find the DWO inside it, don't
12779 look for the original DWO file. It makes gdb behave differently
12780 depending on whether one is debugging in the build tree. */
cf2c3c16 12781
976ca316 12782 dwp_file = get_dwp_file (per_objfile);
80626a55 12783 if (dwp_file != NULL)
cf2c3c16 12784 {
80626a55
DE
12785 const struct dwp_hash_table *dwp_htab =
12786 is_debug_types ? dwp_file->tus : dwp_file->cus;
12787
12788 if (dwp_htab != NULL)
12789 {
12790 struct dwo_unit *dwo_cutu =
976ca316
SM
12791 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12792 is_debug_types);
80626a55
DE
12793
12794 if (dwo_cutu != NULL)
12795 {
b4f54984 12796 if (dwarf_read_debug)
80626a55
DE
12797 {
12798 fprintf_unfiltered (gdb_stdlog,
12799 "Virtual DWO %s %s found: @%s\n",
12800 kind, hex_string (signature),
12801 host_address_to_string (dwo_cutu));
12802 }
12803 return dwo_cutu;
12804 }
12805 }
12806 }
6a506a2d 12807 else
80626a55 12808 {
6a506a2d 12809 /* No DWP file, look for the DWO file. */
80626a55 12810
976ca316 12811 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12812 if (*dwo_file_slot == NULL)
80626a55 12813 {
6a506a2d 12814 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12815 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12816 }
6a506a2d 12817 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12818 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12819
6a506a2d 12820 if (dwo_file != NULL)
19c3d4c9 12821 {
6a506a2d
DE
12822 struct dwo_unit *dwo_cutu = NULL;
12823
12824 if (is_debug_types && dwo_file->tus)
12825 {
12826 struct dwo_unit find_dwo_cutu;
12827
12828 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12829 find_dwo_cutu.signature = signature;
9a3c8263 12830 dwo_cutu
b0b6a987
TT
12831 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12832 &find_dwo_cutu);
6a506a2d 12833 }
33c5cd75 12834 else if (!is_debug_types && dwo_file->cus)
80626a55 12835 {
33c5cd75
DB
12836 struct dwo_unit find_dwo_cutu;
12837
12838 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12839 find_dwo_cutu.signature = signature;
b0b6a987 12840 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12841 &find_dwo_cutu);
6a506a2d
DE
12842 }
12843
12844 if (dwo_cutu != NULL)
12845 {
b4f54984 12846 if (dwarf_read_debug)
6a506a2d
DE
12847 {
12848 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12849 kind, dwo_name, hex_string (signature),
12850 host_address_to_string (dwo_cutu));
12851 }
12852 return dwo_cutu;
80626a55
DE
12853 }
12854 }
2e276125 12855 }
9cdd5dbd 12856
80626a55
DE
12857 /* We didn't find it. This could mean a dwo_id mismatch, or
12858 someone deleted the DWO/DWP file, or the search path isn't set up
12859 correctly to find the file. */
12860
b4f54984 12861 if (dwarf_read_debug)
80626a55
DE
12862 {
12863 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12864 kind, dwo_name, hex_string (signature));
12865 }
3019eac3 12866
6656a72d
DE
12867 /* This is a warning and not a complaint because it can be caused by
12868 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12869 {
12870 /* Print the name of the DWP file if we looked there, helps the user
12871 better diagnose the problem. */
791afaa2 12872 std::string dwp_text;
43942612
DE
12873
12874 if (dwp_file != NULL)
791afaa2
TT
12875 dwp_text = string_printf (" [in DWP file %s]",
12876 lbasename (dwp_file->name));
43942612 12877
9d8780f0 12878 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12879 " [in module %s]"),
4ab09049
SM
12880 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12881 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12882 }
3019eac3 12883 return NULL;
5fb290d7
DJ
12884}
12885
80626a55
DE
12886/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12887 See lookup_dwo_cutu_unit for details. */
12888
12889static struct dwo_unit *
4ab09049 12890lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12891 ULONGEST signature)
12892{
4ab09049
SM
12893 gdb_assert (!cu->per_cu->is_debug_types);
12894
12895 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12896}
12897
12898/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12899 See lookup_dwo_cutu_unit for details. */
12900
12901static struct dwo_unit *
4ab09049 12902lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12903{
4ab09049
SM
12904 gdb_assert (cu->per_cu->is_debug_types);
12905
12906 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12907
12908 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12909}
12910
89e63ee4
DE
12911/* Traversal function for queue_and_load_all_dwo_tus. */
12912
12913static int
12914queue_and_load_dwo_tu (void **slot, void *info)
12915{
12916 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12917 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12918 ULONGEST signature = dwo_unit->signature;
d460f660 12919 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12920
12921 if (sig_type != NULL)
12922 {
12923 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12924
12925 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12926 a real dependency of PER_CU on SIG_TYPE. That is detected later
12927 while processing PER_CU. */
120ce1b5 12928 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
12929 load_full_type_unit (sig_cu, cu->per_objfile);
12930 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12931 }
12932
12933 return 1;
12934}
12935
1b555f17 12936/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12937 The DWO may have the only definition of the type, though it may not be
12938 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12939 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12940
12941static void
1b555f17 12942queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12943{
12944 struct dwo_unit *dwo_unit;
12945 struct dwo_file *dwo_file;
12946
1b555f17
SM
12947 gdb_assert (cu != nullptr);
12948 gdb_assert (!cu->per_cu->is_debug_types);
12949 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12950
1b555f17 12951 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12952 gdb_assert (dwo_unit != NULL);
12953
12954 dwo_file = dwo_unit->dwo_file;
12955 if (dwo_file->tus != NULL)
1b555f17 12956 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12957}
12958
3019eac3 12959/* Read in various DIEs. */
348e048f 12960
d389af10 12961/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12962 Inherit only the children of the DW_AT_abstract_origin DIE not being
12963 already referenced by DW_AT_abstract_origin from the children of the
12964 current DIE. */
d389af10
JK
12965
12966static void
12967inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12968{
12969 struct die_info *child_die;
791afaa2 12970 sect_offset *offsetp;
d389af10
JK
12971 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12972 struct die_info *origin_die;
12973 /* Iterator of the ORIGIN_DIE children. */
12974 struct die_info *origin_child_die;
d389af10 12975 struct attribute *attr;
cd02d79d
PA
12976 struct dwarf2_cu *origin_cu;
12977 struct pending **origin_previous_list_in_scope;
d389af10
JK
12978
12979 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12980 if (!attr)
12981 return;
12982
cd02d79d
PA
12983 /* Note that following die references may follow to a die in a
12984 different cu. */
12985
12986 origin_cu = cu;
12987 origin_die = follow_die_ref (die, attr, &origin_cu);
12988
12989 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12990 symbols in. */
12991 origin_previous_list_in_scope = origin_cu->list_in_scope;
12992 origin_cu->list_in_scope = cu->list_in_scope;
12993
edb3359d
DJ
12994 if (die->tag != origin_die->tag
12995 && !(die->tag == DW_TAG_inlined_subroutine
12996 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12997 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12998 sect_offset_str (die->sect_off),
12999 sect_offset_str (origin_die->sect_off));
d389af10 13000
791afaa2 13001 std::vector<sect_offset> offsets;
d389af10 13002
3ea89b92
PMR
13003 for (child_die = die->child;
13004 child_die && child_die->tag;
436c571c 13005 child_die = child_die->sibling)
3ea89b92
PMR
13006 {
13007 struct die_info *child_origin_die;
13008 struct dwarf2_cu *child_origin_cu;
13009
13010 /* We are trying to process concrete instance entries:
216f72a1 13011 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13012 it's not relevant to our analysis here. i.e. detecting DIEs that are
13013 present in the abstract instance but not referenced in the concrete
13014 one. */
216f72a1
JK
13015 if (child_die->tag == DW_TAG_call_site
13016 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13017 continue;
13018
c38f313d
DJ
13019 /* For each CHILD_DIE, find the corresponding child of
13020 ORIGIN_DIE. If there is more than one layer of
13021 DW_AT_abstract_origin, follow them all; there shouldn't be,
13022 but GCC versions at least through 4.4 generate this (GCC PR
13023 40573). */
3ea89b92
PMR
13024 child_origin_die = child_die;
13025 child_origin_cu = cu;
c38f313d
DJ
13026 while (1)
13027 {
cd02d79d
PA
13028 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13029 child_origin_cu);
c38f313d
DJ
13030 if (attr == NULL)
13031 break;
cd02d79d
PA
13032 child_origin_die = follow_die_ref (child_origin_die, attr,
13033 &child_origin_cu);
c38f313d
DJ
13034 }
13035
d389af10
JK
13036 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13037 counterpart may exist. */
c38f313d 13038 if (child_origin_die != child_die)
d389af10 13039 {
edb3359d
DJ
13040 if (child_die->tag != child_origin_die->tag
13041 && !(child_die->tag == DW_TAG_inlined_subroutine
13042 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13043 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13044 "different tags"),
9d8780f0
SM
13045 sect_offset_str (child_die->sect_off),
13046 sect_offset_str (child_origin_die->sect_off));
c38f313d 13047 if (child_origin_die->parent != origin_die)
b98664d3 13048 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13049 "different parents"),
9d8780f0
SM
13050 sect_offset_str (child_die->sect_off),
13051 sect_offset_str (child_origin_die->sect_off));
c38f313d 13052 else
791afaa2 13053 offsets.push_back (child_origin_die->sect_off);
d389af10 13054 }
d389af10 13055 }
791afaa2
TT
13056 std::sort (offsets.begin (), offsets.end ());
13057 sect_offset *offsets_end = offsets.data () + offsets.size ();
13058 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13059 if (offsetp[-1] == *offsetp)
b98664d3 13060 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13061 "to DIE %s as their abstract origin"),
13062 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13063
791afaa2 13064 offsetp = offsets.data ();
d389af10
JK
13065 origin_child_die = origin_die->child;
13066 while (origin_child_die && origin_child_die->tag)
13067 {
13068 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13069 while (offsetp < offsets_end
9c541725 13070 && *offsetp < origin_child_die->sect_off)
d389af10 13071 offsetp++;
b64f50a1 13072 if (offsetp >= offsets_end
9c541725 13073 || *offsetp > origin_child_die->sect_off)
d389af10 13074 {
adde2bff
DE
13075 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13076 Check whether we're already processing ORIGIN_CHILD_DIE.
13077 This can happen with mutually referenced abstract_origins.
13078 PR 16581. */
13079 if (!origin_child_die->in_process)
13080 process_die (origin_child_die, origin_cu);
d389af10 13081 }
436c571c 13082 origin_child_die = origin_child_die->sibling;
d389af10 13083 }
cd02d79d 13084 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13085
13086 if (cu != origin_cu)
13087 compute_delayed_physnames (origin_cu);
d389af10
JK
13088}
13089
c906108c 13090static void
e7c27a73 13091read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13092{
5e22e966 13093 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13094 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13095 struct context_stack *newobj;
c906108c
SS
13096 CORE_ADDR lowpc;
13097 CORE_ADDR highpc;
13098 struct die_info *child_die;
edb3359d 13099 struct attribute *attr, *call_line, *call_file;
15d034d0 13100 const char *name;
e142c38c 13101 CORE_ADDR baseaddr;
801e3a5b 13102 struct block *block;
edb3359d 13103 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13104 std::vector<struct symbol *> template_args;
34eaf542 13105 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13106
13107 if (inlined_func)
13108 {
13109 /* If we do not have call site information, we can't show the
13110 caller of this inlined function. That's too confusing, so
13111 only use the scope for local variables. */
13112 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13113 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13114 if (call_line == NULL || call_file == NULL)
13115 {
13116 read_lexical_block_scope (die, cu);
13117 return;
13118 }
13119 }
c906108c 13120
b3b3bada 13121 baseaddr = objfile->text_section_offset ();
e142c38c 13122
94af9270 13123 name = dwarf2_name (die, cu);
c906108c 13124
e8d05480
JB
13125 /* Ignore functions with missing or empty names. These are actually
13126 illegal according to the DWARF standard. */
13127 if (name == NULL)
13128 {
b98664d3 13129 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13130 sect_offset_str (die->sect_off));
e8d05480
JB
13131 return;
13132 }
13133
13134 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13135 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13136 <= PC_BOUNDS_INVALID)
e8d05480 13137 {
ae4d0c03
PM
13138 attr = dwarf2_attr (die, DW_AT_external, cu);
13139 if (!attr || !DW_UNSND (attr))
b98664d3 13140 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13141 "for subprogram DIE at %s"),
13142 sect_offset_str (die->sect_off));
e8d05480
JB
13143 return;
13144 }
c906108c 13145
3e29f34a
MR
13146 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13147 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13148
34eaf542
TT
13149 /* If we have any template arguments, then we must allocate a
13150 different sort of symbol. */
436c571c 13151 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13152 {
13153 if (child_die->tag == DW_TAG_template_type_param
13154 || child_die->tag == DW_TAG_template_value_param)
13155 {
8c14c3a3 13156 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13157 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13158 break;
13159 }
13160 }
13161
c24bdb02 13162 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13163 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13164 (struct symbol *) templ_func);
4c2df51b 13165
81873cc8 13166 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13167 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13168 cu->language);
13169
4cecd739
DJ
13170 /* If there is a location expression for DW_AT_frame_base, record
13171 it. */
e142c38c 13172 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13173 if (attr != nullptr)
fe978cb0 13174 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13175
63e43d3a
PMR
13176 /* If there is a location for the static link, record it. */
13177 newobj->static_link = NULL;
13178 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13179 if (attr != nullptr)
63e43d3a 13180 {
224c3ddb
SM
13181 newobj->static_link
13182 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13183 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13184 cu->addr_type ());
63e43d3a
PMR
13185 }
13186
c24bdb02 13187 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13188
639d11d3 13189 if (die->child != NULL)
c906108c 13190 {
639d11d3 13191 child_die = die->child;
c906108c
SS
13192 while (child_die && child_die->tag)
13193 {
34eaf542
TT
13194 if (child_die->tag == DW_TAG_template_type_param
13195 || child_die->tag == DW_TAG_template_value_param)
13196 {
13197 struct symbol *arg = new_symbol (child_die, NULL, cu);
13198
f1078f66 13199 if (arg != NULL)
2f4732b0 13200 template_args.push_back (arg);
34eaf542
TT
13201 }
13202 else
13203 process_die (child_die, cu);
436c571c 13204 child_die = child_die->sibling;
c906108c
SS
13205 }
13206 }
13207
d389af10
JK
13208 inherit_abstract_dies (die, cu);
13209
4a811a97
UW
13210 /* If we have a DW_AT_specification, we might need to import using
13211 directives from the context of the specification DIE. See the
13212 comment in determine_prefix. */
13213 if (cu->language == language_cplus
13214 && dwarf2_attr (die, DW_AT_specification, cu))
13215 {
13216 struct dwarf2_cu *spec_cu = cu;
13217 struct die_info *spec_die = die_specification (die, &spec_cu);
13218
13219 while (spec_die)
13220 {
13221 child_die = spec_die->child;
13222 while (child_die && child_die->tag)
13223 {
13224 if (child_die->tag == DW_TAG_imported_module)
13225 process_die (child_die, spec_cu);
436c571c 13226 child_die = child_die->sibling;
4a811a97
UW
13227 }
13228
13229 /* In some cases, GCC generates specification DIEs that
13230 themselves contain DW_AT_specification attributes. */
13231 spec_die = die_specification (spec_die, &spec_cu);
13232 }
13233 }
13234
c24bdb02 13235 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13236 /* Make a block for the local symbols within. */
c24bdb02 13237 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13238 cstk.static_link, lowpc, highpc);
801e3a5b 13239
df8a16a1 13240 /* For C++, set the block's scope. */
45280282
IB
13241 if ((cu->language == language_cplus
13242 || cu->language == language_fortran
c44af4eb
TT
13243 || cu->language == language_d
13244 || cu->language == language_rust)
4d4ec4e5 13245 && cu->processing_has_namespace_info)
195a3f6c
TT
13246 block_set_scope (block, determine_prefix (die, cu),
13247 &objfile->objfile_obstack);
df8a16a1 13248
801e3a5b
JB
13249 /* If we have address ranges, record them. */
13250 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13251
a60f3166 13252 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13253
34eaf542 13254 /* Attach template arguments to function. */
2f4732b0 13255 if (!template_args.empty ())
34eaf542
TT
13256 {
13257 gdb_assert (templ_func != NULL);
13258
2f4732b0 13259 templ_func->n_template_arguments = template_args.size ();
34eaf542 13260 templ_func->template_arguments
8d749320
SM
13261 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13262 templ_func->n_template_arguments);
34eaf542 13263 memcpy (templ_func->template_arguments,
2f4732b0 13264 template_args.data (),
34eaf542 13265 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13266
13267 /* Make sure that the symtab is set on the new symbols. Even
13268 though they don't appear in this symtab directly, other parts
13269 of gdb assume that symbols do, and this is reasonably
13270 true. */
8634679f 13271 for (symbol *sym : template_args)
3e1d3d8c 13272 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13273 }
13274
208d8187
JB
13275 /* In C++, we can have functions nested inside functions (e.g., when
13276 a function declares a class that has methods). This means that
13277 when we finish processing a function scope, we may need to go
13278 back to building a containing block's symbol lists. */
c24bdb02
KS
13279 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13280 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13281
921e78cf
JB
13282 /* If we've finished processing a top-level function, subsequent
13283 symbols go in the file symbol list. */
c24bdb02
KS
13284 if (cu->get_builder ()->outermost_context_p ())
13285 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13286}
13287
13288/* Process all the DIES contained within a lexical block scope. Start
13289 a new scope, process the dies, and then close the scope. */
13290
13291static void
e7c27a73 13292read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13293{
5e22e966 13294 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13295 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13296 CORE_ADDR lowpc, highpc;
13297 struct die_info *child_die;
e142c38c
DJ
13298 CORE_ADDR baseaddr;
13299
b3b3bada 13300 baseaddr = objfile->text_section_offset ();
c906108c
SS
13301
13302 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13303 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13304 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13305 be nasty. Might be easier to properly extend generic blocks to
af34e669 13306 describe ranges. */
e385593e
JK
13307 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13308 {
13309 case PC_BOUNDS_NOT_PRESENT:
13310 /* DW_TAG_lexical_block has no attributes, process its children as if
13311 there was no wrapping by that DW_TAG_lexical_block.
13312 GCC does no longer produces such DWARF since GCC r224161. */
13313 for (child_die = die->child;
13314 child_die != NULL && child_die->tag;
436c571c 13315 child_die = child_die->sibling)
4f7bc5ed
TT
13316 {
13317 /* We might already be processing this DIE. This can happen
13318 in an unusual circumstance -- where a subroutine A
13319 appears lexically in another subroutine B, but A actually
13320 inlines B. The recursion is broken here, rather than in
13321 inherit_abstract_dies, because it seems better to simply
13322 drop concrete children here. */
13323 if (!child_die->in_process)
13324 process_die (child_die, cu);
13325 }
e385593e
JK
13326 return;
13327 case PC_BOUNDS_INVALID:
13328 return;
13329 }
3e29f34a
MR
13330 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13331 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13332
c24bdb02 13333 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13334 if (die->child != NULL)
c906108c 13335 {
639d11d3 13336 child_die = die->child;
c906108c
SS
13337 while (child_die && child_die->tag)
13338 {
e7c27a73 13339 process_die (child_die, cu);
436c571c 13340 child_die = child_die->sibling;
c906108c
SS
13341 }
13342 }
3ea89b92 13343 inherit_abstract_dies (die, cu);
c24bdb02 13344 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13345
c24bdb02
KS
13346 if (*cu->get_builder ()->get_local_symbols () != NULL
13347 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13348 {
801e3a5b 13349 struct block *block
c24bdb02 13350 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13351 cstk.start_addr, highpc);
801e3a5b
JB
13352
13353 /* Note that recording ranges after traversing children, as we
13354 do here, means that recording a parent's ranges entails
13355 walking across all its children's ranges as they appear in
13356 the address map, which is quadratic behavior.
13357
13358 It would be nicer to record the parent's ranges before
13359 traversing its children, simply overriding whatever you find
13360 there. But since we don't even decide whether to create a
13361 block until after we've traversed its children, that's hard
13362 to do. */
13363 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13364 }
c24bdb02
KS
13365 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13366 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13367}
13368
216f72a1 13369/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13370
13371static void
13372read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13373{
5e22e966 13374 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13375 struct objfile *objfile = per_objfile->objfile;
08feed99 13376 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13377 CORE_ADDR pc, baseaddr;
13378 struct attribute *attr;
13379 struct call_site *call_site, call_site_local;
13380 void **slot;
13381 int nparams;
13382 struct die_info *child_die;
13383
b3b3bada 13384 baseaddr = objfile->text_section_offset ();
96408a79 13385
216f72a1
JK
13386 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13387 if (attr == NULL)
13388 {
13389 /* This was a pre-DWARF-5 GNU extension alias
13390 for DW_AT_call_return_pc. */
13391 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13392 }
96408a79
SA
13393 if (!attr)
13394 {
b98664d3 13395 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13396 "DIE %s [in module %s]"),
13397 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13398 return;
13399 }
cd6c91b4 13400 pc = attr->value_as_address () + baseaddr;
3e29f34a 13401 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13402
13403 if (cu->call_site_htab == NULL)
13404 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13405 NULL, &objfile->objfile_obstack,
13406 hashtab_obstack_allocate, NULL);
13407 call_site_local.pc = pc;
13408 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13409 if (*slot != NULL)
13410 {
b98664d3 13411 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13412 "DIE %s [in module %s]"),
13413 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13414 objfile_name (objfile));
96408a79
SA
13415 return;
13416 }
13417
13418 /* Count parameters at the caller. */
13419
13420 nparams = 0;
13421 for (child_die = die->child; child_die && child_die->tag;
436c571c 13422 child_die = child_die->sibling)
96408a79 13423 {
216f72a1
JK
13424 if (child_die->tag != DW_TAG_call_site_parameter
13425 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13426 {
b98664d3 13427 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13428 "DW_TAG_call_site child DIE %s [in module %s]"),
13429 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13430 objfile_name (objfile));
96408a79
SA
13431 continue;
13432 }
13433
13434 nparams++;
13435 }
13436
224c3ddb
SM
13437 call_site
13438 = ((struct call_site *)
13439 obstack_alloc (&objfile->objfile_obstack,
13440 sizeof (*call_site)
13441 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13442 *slot = call_site;
13443 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13444 call_site->pc = pc;
13445
216f72a1
JK
13446 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13447 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13448 {
13449 struct die_info *func_die;
13450
13451 /* Skip also over DW_TAG_inlined_subroutine. */
13452 for (func_die = die->parent;
13453 func_die && func_die->tag != DW_TAG_subprogram
13454 && func_die->tag != DW_TAG_subroutine_type;
13455 func_die = func_die->parent);
13456
216f72a1
JK
13457 /* DW_AT_call_all_calls is a superset
13458 of DW_AT_call_all_tail_calls. */
96408a79 13459 if (func_die
216f72a1 13460 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13461 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13462 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13463 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13464 {
13465 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13466 not complete. But keep CALL_SITE for look ups via call_site_htab,
13467 both the initial caller containing the real return address PC and
13468 the final callee containing the current PC of a chain of tail
13469 calls do not need to have the tail call list complete. But any
13470 function candidate for a virtual tail call frame searched via
13471 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13472 determined unambiguously. */
13473 }
13474 else
13475 {
13476 struct type *func_type = NULL;
13477
13478 if (func_die)
13479 func_type = get_die_type (func_die, cu);
13480 if (func_type != NULL)
13481 {
78134374 13482 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13483
13484 /* Enlist this call site to the function. */
13485 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13486 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13487 }
13488 else
b98664d3 13489 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13490 "DIE %s [in module %s]"),
13491 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13492 }
13493 }
13494
216f72a1
JK
13495 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13496 if (attr == NULL)
13497 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13498 if (attr == NULL)
13499 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13500 if (attr == NULL)
216f72a1
JK
13501 {
13502 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13503 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13504 }
96408a79 13505 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13506 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13507 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13508 else if (attr->form_is_block ())
96408a79
SA
13509 {
13510 struct dwarf2_locexpr_baton *dlbaton;
13511
8d749320 13512 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13513 dlbaton->data = DW_BLOCK (attr)->data;
13514 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13515 dlbaton->per_objfile = per_objfile;
96408a79
SA
13516 dlbaton->per_cu = cu->per_cu;
13517
13518 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13519 }
cd6c91b4 13520 else if (attr->form_is_ref ())
96408a79 13521 {
96408a79
SA
13522 struct dwarf2_cu *target_cu = cu;
13523 struct die_info *target_die;
13524
ac9ec31b 13525 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13526 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13527 if (die_is_declaration (target_die, target_cu))
13528 {
7d45c7c3 13529 const char *target_physname;
9112db09
JK
13530
13531 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13532 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13533 if (target_physname == NULL)
9112db09 13534 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13535 if (target_physname == NULL)
b98664d3 13536 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13537 "physname, for referencing DIE %s [in module %s]"),
13538 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13539 else
7d455152 13540 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13541 }
13542 else
13543 {
13544 CORE_ADDR lowpc;
13545
13546 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13547 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13548 <= PC_BOUNDS_INVALID)
b98664d3 13549 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13550 "low pc, for referencing DIE %s [in module %s]"),
13551 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13552 else
3e29f34a
MR
13553 {
13554 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13555 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13556 }
96408a79
SA
13557 }
13558 }
13559 else
b98664d3 13560 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13561 "block nor reference, for DIE %s [in module %s]"),
13562 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13563
13564 call_site->per_cu = cu->per_cu;
9f47c707 13565 call_site->per_objfile = per_objfile;
96408a79
SA
13566
13567 for (child_die = die->child;
13568 child_die && child_die->tag;
436c571c 13569 child_die = child_die->sibling)
96408a79 13570 {
96408a79 13571 struct call_site_parameter *parameter;
1788b2d3 13572 struct attribute *loc, *origin;
96408a79 13573
216f72a1
JK
13574 if (child_die->tag != DW_TAG_call_site_parameter
13575 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13576 {
13577 /* Already printed the complaint above. */
13578 continue;
13579 }
13580
13581 gdb_assert (call_site->parameter_count < nparams);
13582 parameter = &call_site->parameter[call_site->parameter_count];
13583
1788b2d3
JK
13584 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13585 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13586 register is contained in DW_AT_call_value. */
96408a79 13587
24c5c679 13588 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13589 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13590 if (origin == NULL)
13591 {
13592 /* This was a pre-DWARF-5 GNU extension alias
13593 for DW_AT_call_parameter. */
13594 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13595 }
cd6c91b4 13596 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13597 {
1788b2d3 13598 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13599
0826b30a 13600 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13601 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13602 {
13603 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13604 binding can be done only inside one CU. Such referenced DIE
13605 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13606 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13607 "DW_TAG_call_site child DIE %s [in module %s]"),
13608 sect_offset_str (child_die->sect_off),
9c541725 13609 objfile_name (objfile));
d76b7dbc
JK
13610 continue;
13611 }
9c541725
PA
13612 parameter->u.param_cu_off
13613 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13614 }
4fc6c0d5 13615 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13616 {
b98664d3 13617 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13618 "DW_TAG_call_site child DIE %s [in module %s]"),
13619 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13620 continue;
13621 }
24c5c679 13622 else
96408a79 13623 {
24c5c679
JK
13624 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13625 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13626 if (parameter->u.dwarf_reg != -1)
13627 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13628 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13629 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13630 &parameter->u.fb_offset))
13631 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13632 else
13633 {
b98664d3 13634 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13635 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13636 "DW_TAG_call_site child DIE %s "
24c5c679 13637 "[in module %s]"),
9d8780f0 13638 sect_offset_str (child_die->sect_off),
9c541725 13639 objfile_name (objfile));
24c5c679
JK
13640 continue;
13641 }
96408a79
SA
13642 }
13643
216f72a1
JK
13644 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13645 if (attr == NULL)
13646 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13647 if (attr == NULL || !attr->form_is_block ())
96408a79 13648 {
b98664d3 13649 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13650 "DW_TAG_call_site child DIE %s [in module %s]"),
13651 sect_offset_str (child_die->sect_off),
9c541725 13652 objfile_name (objfile));
96408a79
SA
13653 continue;
13654 }
13655 parameter->value = DW_BLOCK (attr)->data;
13656 parameter->value_size = DW_BLOCK (attr)->size;
13657
13658 /* Parameters are not pre-cleared by memset above. */
13659 parameter->data_value = NULL;
13660 parameter->data_value_size = 0;
13661 call_site->parameter_count++;
13662
216f72a1
JK
13663 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13664 if (attr == NULL)
13665 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13666 if (attr != nullptr)
96408a79 13667 {
4fc6c0d5 13668 if (!attr->form_is_block ())
b98664d3 13669 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13670 "DW_TAG_call_site child DIE %s [in module %s]"),
13671 sect_offset_str (child_die->sect_off),
9c541725 13672 objfile_name (objfile));
96408a79
SA
13673 else
13674 {
13675 parameter->data_value = DW_BLOCK (attr)->data;
13676 parameter->data_value_size = DW_BLOCK (attr)->size;
13677 }
13678 }
13679 }
13680}
13681
71a3c369
TT
13682/* Helper function for read_variable. If DIE represents a virtual
13683 table, then return the type of the concrete object that is
13684 associated with the virtual table. Otherwise, return NULL. */
13685
13686static struct type *
13687rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13688{
13689 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13690 if (attr == NULL)
13691 return NULL;
13692
13693 /* Find the type DIE. */
13694 struct die_info *type_die = NULL;
13695 struct dwarf2_cu *type_cu = cu;
13696
cd6c91b4 13697 if (attr->form_is_ref ())
71a3c369
TT
13698 type_die = follow_die_ref (die, attr, &type_cu);
13699 if (type_die == NULL)
13700 return NULL;
13701
13702 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13703 return NULL;
13704 return die_containing_type (type_die, type_cu);
13705}
13706
13707/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13708
13709static void
13710read_variable (struct die_info *die, struct dwarf2_cu *cu)
13711{
13712 struct rust_vtable_symbol *storage = NULL;
13713
13714 if (cu->language == language_rust)
13715 {
13716 struct type *containing_type = rust_containing_type (die, cu);
13717
13718 if (containing_type != NULL)
13719 {
5e22e966 13720 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13721
8c14c3a3 13722 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13723 storage->concrete_type = containing_type;
cf724bc9 13724 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13725 }
13726 }
13727
e4a62c65
TV
13728 struct symbol *res = new_symbol (die, NULL, cu, storage);
13729 struct attribute *abstract_origin
13730 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13731 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13732 if (res == NULL && loc && abstract_origin)
13733 {
13734 /* We have a variable without a name, but with a location and an abstract
13735 origin. This may be a concrete instance of an abstract variable
13736 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13737 later. */
13738 struct dwarf2_cu *origin_cu = cu;
13739 struct die_info *origin_die
13740 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13741 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13742 per_objfile->per_bfd->abstract_to_concrete
13743 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13744 }
71a3c369
TT
13745}
13746
43988095
JK
13747/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13748 reading .debug_rnglists.
13749 Callback's type should be:
13750 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13751 Return true if the attributes are present and valid, otherwise,
13752 return false. */
13753
13754template <typename Callback>
13755static bool
13756dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13757 Callback &&callback)
13758{
976ca316
SM
13759 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13760 struct objfile *objfile = per_objfile->objfile;
43988095 13761 bfd *obfd = objfile->obfd;
43988095 13762 /* Base address selection entry. */
2b24b6e4 13763 gdb::optional<CORE_ADDR> base;
43988095 13764 const gdb_byte *buffer;
43988095
JK
13765 CORE_ADDR baseaddr;
13766 bool overflow = false;
13767
43988095
JK
13768 base = cu->base_address;
13769
976ca316
SM
13770 per_objfile->per_bfd->rnglists.read (objfile);
13771 if (offset >= per_objfile->per_bfd->rnglists.size)
43988095 13772 {
b98664d3 13773 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13774 offset);
13775 return false;
13776 }
976ca316 13777 buffer = per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13778
b3b3bada 13779 baseaddr = objfile->text_section_offset ();
43988095
JK
13780
13781 while (1)
13782 {
7814882a
JK
13783 /* Initialize it due to a false compiler warning. */
13784 CORE_ADDR range_beginning = 0, range_end = 0;
976ca316
SM
13785 const gdb_byte *buf_end = (per_objfile->per_bfd->rnglists.buffer
13786 + per_objfile->per_bfd->rnglists.size);
43988095
JK
13787 unsigned int bytes_read;
13788
13789 if (buffer == buf_end)
13790 {
13791 overflow = true;
13792 break;
13793 }
13794 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13795 switch (rlet)
13796 {
13797 case DW_RLE_end_of_list:
13798 break;
13799 case DW_RLE_base_address:
13800 if (buffer + cu->header.addr_size > buf_end)
13801 {
13802 overflow = true;
13803 break;
13804 }
c8a7a66f 13805 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13806 buffer += bytes_read;
13807 break;
13808 case DW_RLE_start_length:
13809 if (buffer + cu->header.addr_size > buf_end)
13810 {
13811 overflow = true;
13812 break;
13813 }
c8a7a66f
TT
13814 range_beginning = cu->header.read_address (obfd, buffer,
13815 &bytes_read);
43988095
JK
13816 buffer += bytes_read;
13817 range_end = (range_beginning
13818 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13819 buffer += bytes_read;
13820 if (buffer > buf_end)
13821 {
13822 overflow = true;
13823 break;
13824 }
13825 break;
13826 case DW_RLE_offset_pair:
13827 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13828 buffer += bytes_read;
13829 if (buffer > buf_end)
13830 {
13831 overflow = true;
13832 break;
13833 }
13834 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13835 buffer += bytes_read;
13836 if (buffer > buf_end)
13837 {
13838 overflow = true;
13839 break;
13840 }
13841 break;
13842 case DW_RLE_start_end:
13843 if (buffer + 2 * cu->header.addr_size > buf_end)
13844 {
13845 overflow = true;
13846 break;
13847 }
c8a7a66f
TT
13848 range_beginning = cu->header.read_address (obfd, buffer,
13849 &bytes_read);
43988095 13850 buffer += bytes_read;
c8a7a66f 13851 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13852 buffer += bytes_read;
13853 break;
13854 default:
b98664d3 13855 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13856 return false;
13857 }
13858 if (rlet == DW_RLE_end_of_list || overflow)
13859 break;
13860 if (rlet == DW_RLE_base_address)
13861 continue;
13862
2b24b6e4 13863 if (!base.has_value ())
43988095
JK
13864 {
13865 /* We have no valid base address for the ranges
13866 data. */
b98664d3 13867 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13868 return false;
13869 }
13870
13871 if (range_beginning > range_end)
13872 {
13873 /* Inverted range entries are invalid. */
b98664d3 13874 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13875 return false;
13876 }
13877
13878 /* Empty range entries have no effect. */
13879 if (range_beginning == range_end)
13880 continue;
13881
2b24b6e4
TT
13882 range_beginning += *base;
13883 range_end += *base;
43988095
JK
13884
13885 /* A not-uncommon case of bad debug info.
13886 Don't pollute the addrmap with bad data. */
13887 if (range_beginning + baseaddr == 0
976ca316 13888 && !per_objfile->per_bfd->has_section_at_zero)
43988095 13889 {
b98664d3 13890 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13891 " [in module %s]"), objfile_name (objfile));
13892 continue;
13893 }
13894
13895 callback (range_beginning, range_end);
13896 }
13897
13898 if (overflow)
13899 {
b98664d3 13900 complaint (_("Offset %d is not terminated "
43988095
JK
13901 "for DW_AT_ranges attribute"),
13902 offset);
13903 return false;
13904 }
13905
13906 return true;
13907}
13908
13909/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13910 Callback's type should be:
13911 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13912 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13913
43988095 13914template <typename Callback>
43039443 13915static int
5f46c5a5 13916dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13917 Callback &&callback)
43039443 13918{
5e22e966
SM
13919 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13920 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13921 struct comp_unit_head *cu_header = &cu->header;
13922 bfd *obfd = objfile->obfd;
13923 unsigned int addr_size = cu_header->addr_size;
13924 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13925 /* Base address selection entry. */
2b24b6e4 13926 gdb::optional<CORE_ADDR> base;
43039443 13927 unsigned int dummy;
d521ce57 13928 const gdb_byte *buffer;
ff013f42 13929 CORE_ADDR baseaddr;
43039443 13930
43988095
JK
13931 if (cu_header->version >= 5)
13932 return dwarf2_rnglists_process (offset, cu, callback);
13933
d00adf39 13934 base = cu->base_address;
43039443 13935
5e22e966
SM
13936 per_objfile->per_bfd->ranges.read (objfile);
13937 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13938 {
b98664d3 13939 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13940 offset);
13941 return 0;
13942 }
5e22e966 13943 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13944
b3b3bada 13945 baseaddr = objfile->text_section_offset ();
ff013f42 13946
43039443
JK
13947 while (1)
13948 {
13949 CORE_ADDR range_beginning, range_end;
13950
c8a7a66f 13951 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13952 buffer += addr_size;
c8a7a66f 13953 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13954 buffer += addr_size;
13955 offset += 2 * addr_size;
13956
13957 /* An end of list marker is a pair of zero addresses. */
13958 if (range_beginning == 0 && range_end == 0)
13959 /* Found the end of list entry. */
13960 break;
13961
13962 /* Each base address selection entry is a pair of 2 values.
13963 The first is the largest possible address, the second is
13964 the base address. Check for a base address here. */
13965 if ((range_beginning & mask) == mask)
13966 {
28d2bfb9
AB
13967 /* If we found the largest possible address, then we already
13968 have the base address in range_end. */
13969 base = range_end;
43039443
JK
13970 continue;
13971 }
13972
2b24b6e4 13973 if (!base.has_value ())
43039443
JK
13974 {
13975 /* We have no valid base address for the ranges
13976 data. */
b98664d3 13977 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13978 return 0;
13979 }
13980
9277c30c
UW
13981 if (range_beginning > range_end)
13982 {
13983 /* Inverted range entries are invalid. */
b98664d3 13984 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13985 return 0;
13986 }
13987
13988 /* Empty range entries have no effect. */
13989 if (range_beginning == range_end)
13990 continue;
13991
2b24b6e4
TT
13992 range_beginning += *base;
13993 range_end += *base;
43039443 13994
01093045
DE
13995 /* A not-uncommon case of bad debug info.
13996 Don't pollute the addrmap with bad data. */
13997 if (range_beginning + baseaddr == 0
5e22e966 13998 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13999 {
b98664d3 14000 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14001 " [in module %s]"), objfile_name (objfile));
01093045
DE
14002 continue;
14003 }
14004
5f46c5a5
JK
14005 callback (range_beginning, range_end);
14006 }
14007
14008 return 1;
14009}
14010
14011/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14012 Return 1 if the attributes are present and valid, otherwise, return 0.
14013 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14014
14015static int
14016dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14017 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14018 dwarf2_psymtab *ranges_pst)
5f46c5a5 14019{
5e22e966 14020 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14021 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14022 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14023 int low_set = 0;
14024 CORE_ADDR low = 0;
14025 CORE_ADDR high = 0;
14026 int retval;
14027
14028 retval = dwarf2_ranges_process (offset, cu,
14029 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14030 {
9277c30c 14031 if (ranges_pst != NULL)
3e29f34a
MR
14032 {
14033 CORE_ADDR lowpc;
14034 CORE_ADDR highpc;
14035
79748972
TT
14036 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14037 range_beginning + baseaddr)
14038 - baseaddr);
14039 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14040 range_end + baseaddr)
14041 - baseaddr);
d320c2b5
TT
14042 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14043 lowpc, highpc - 1, ranges_pst);
3e29f34a 14044 }
ff013f42 14045
43039443
JK
14046 /* FIXME: This is recording everything as a low-high
14047 segment of consecutive addresses. We should have a
14048 data structure for discontiguous block ranges
14049 instead. */
14050 if (! low_set)
14051 {
14052 low = range_beginning;
14053 high = range_end;
14054 low_set = 1;
14055 }
14056 else
14057 {
14058 if (range_beginning < low)
14059 low = range_beginning;
14060 if (range_end > high)
14061 high = range_end;
14062 }
5f46c5a5
JK
14063 });
14064 if (!retval)
14065 return 0;
43039443
JK
14066
14067 if (! low_set)
14068 /* If the first entry is an end-of-list marker, the range
14069 describes an empty scope, i.e. no instructions. */
14070 return 0;
14071
14072 if (low_return)
14073 *low_return = low;
14074 if (high_return)
14075 *high_return = high;
14076 return 1;
14077}
14078
3a2b436a
JK
14079/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14080 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14081 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14082
3a2b436a 14083static enum pc_bounds_kind
af34e669 14084dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14085 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14086 dwarf2_psymtab *pst)
c906108c 14087{
976ca316 14088 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14089 struct attribute *attr;
91da1414 14090 struct attribute *attr_high;
af34e669
DJ
14091 CORE_ADDR low = 0;
14092 CORE_ADDR high = 0;
e385593e 14093 enum pc_bounds_kind ret;
c906108c 14094
91da1414
MW
14095 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14096 if (attr_high)
af34e669 14097 {
e142c38c 14098 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14099 if (attr != nullptr)
91da1414 14100 {
cd6c91b4
TT
14101 low = attr->value_as_address ();
14102 high = attr_high->value_as_address ();
14103 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14104 high += low;
91da1414 14105 }
af34e669
DJ
14106 else
14107 /* Found high w/o low attribute. */
e385593e 14108 return PC_BOUNDS_INVALID;
af34e669
DJ
14109
14110 /* Found consecutive range of addresses. */
3a2b436a 14111 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14112 }
c906108c 14113 else
af34e669 14114 {
e142c38c 14115 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14116 if (attr != NULL)
14117 {
18a8505e 14118 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14119 We take advantage of the fact that DW_AT_ranges does not appear
14120 in DW_TAG_compile_unit of DWO files. */
14121 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14122 unsigned int ranges_offset = (DW_UNSND (attr)
14123 + (need_ranges_base
14124 ? cu->ranges_base
14125 : 0));
2e3cf129 14126
af34e669 14127 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14128 .debug_ranges section. */
2e3cf129 14129 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14130 return PC_BOUNDS_INVALID;
43039443 14131 /* Found discontinuous range of addresses. */
3a2b436a 14132 ret = PC_BOUNDS_RANGES;
af34e669 14133 }
e385593e
JK
14134 else
14135 return PC_BOUNDS_NOT_PRESENT;
af34e669 14136 }
c906108c 14137
48fbe735 14138 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14139 if (high <= low)
e385593e 14140 return PC_BOUNDS_INVALID;
c906108c
SS
14141
14142 /* When using the GNU linker, .gnu.linkonce. sections are used to
14143 eliminate duplicate copies of functions and vtables and such.
14144 The linker will arbitrarily choose one and discard the others.
14145 The AT_*_pc values for such functions refer to local labels in
14146 these sections. If the section from that file was discarded, the
14147 labels are not in the output, so the relocs get a value of 0.
14148 If this is a discarded function, mark the pc bounds as invalid,
14149 so that GDB will ignore it. */
976ca316 14150 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14151 return PC_BOUNDS_INVALID;
c906108c
SS
14152
14153 *lowpc = low;
96408a79
SA
14154 if (highpc)
14155 *highpc = high;
af34e669 14156 return ret;
c906108c
SS
14157}
14158
b084d499
JB
14159/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14160 its low and high PC addresses. Do nothing if these addresses could not
14161 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14162 and HIGHPC to the high address if greater than HIGHPC. */
14163
14164static void
14165dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14166 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14167 struct dwarf2_cu *cu)
14168{
14169 CORE_ADDR low, high;
14170 struct die_info *child = die->child;
14171
e385593e 14172 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14173 {
325fac50
PA
14174 *lowpc = std::min (*lowpc, low);
14175 *highpc = std::max (*highpc, high);
b084d499
JB
14176 }
14177
14178 /* If the language does not allow nested subprograms (either inside
14179 subprograms or lexical blocks), we're done. */
14180 if (cu->language != language_ada)
14181 return;
6e70227d 14182
b084d499
JB
14183 /* Check all the children of the given DIE. If it contains nested
14184 subprograms, then check their pc bounds. Likewise, we need to
14185 check lexical blocks as well, as they may also contain subprogram
14186 definitions. */
14187 while (child && child->tag)
14188 {
14189 if (child->tag == DW_TAG_subprogram
14190 || child->tag == DW_TAG_lexical_block)
14191 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14192 child = child->sibling;
b084d499
JB
14193 }
14194}
14195
fae299cd
DC
14196/* Get the low and high pc's represented by the scope DIE, and store
14197 them in *LOWPC and *HIGHPC. If the correct values can't be
14198 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14199
14200static void
14201get_scope_pc_bounds (struct die_info *die,
14202 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14203 struct dwarf2_cu *cu)
14204{
14205 CORE_ADDR best_low = (CORE_ADDR) -1;
14206 CORE_ADDR best_high = (CORE_ADDR) 0;
14207 CORE_ADDR current_low, current_high;
14208
3a2b436a 14209 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14210 >= PC_BOUNDS_RANGES)
fae299cd
DC
14211 {
14212 best_low = current_low;
14213 best_high = current_high;
14214 }
14215 else
14216 {
14217 struct die_info *child = die->child;
14218
14219 while (child && child->tag)
14220 {
14221 switch (child->tag) {
14222 case DW_TAG_subprogram:
b084d499 14223 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14224 break;
14225 case DW_TAG_namespace:
f55ee35c 14226 case DW_TAG_module:
fae299cd
DC
14227 /* FIXME: carlton/2004-01-16: Should we do this for
14228 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14229 that current GCC's always emit the DIEs corresponding
14230 to definitions of methods of classes as children of a
14231 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14232 the DIEs giving the declarations, which could be
14233 anywhere). But I don't see any reason why the
14234 standards says that they have to be there. */
14235 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14236
14237 if (current_low != ((CORE_ADDR) -1))
14238 {
325fac50
PA
14239 best_low = std::min (best_low, current_low);
14240 best_high = std::max (best_high, current_high);
fae299cd
DC
14241 }
14242 break;
14243 default:
0963b4bd 14244 /* Ignore. */
fae299cd
DC
14245 break;
14246 }
14247
436c571c 14248 child = child->sibling;
fae299cd
DC
14249 }
14250 }
14251
14252 *lowpc = best_low;
14253 *highpc = best_high;
14254}
14255
801e3a5b
JB
14256/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14257 in DIE. */
380bca97 14258
801e3a5b
JB
14259static void
14260dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14261 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14262{
5e22e966 14263 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14264 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14265 struct attribute *attr;
91da1414 14266 struct attribute *attr_high;
801e3a5b 14267
91da1414
MW
14268 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14269 if (attr_high)
801e3a5b 14270 {
801e3a5b 14271 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14272 if (attr != nullptr)
801e3a5b 14273 {
cd6c91b4
TT
14274 CORE_ADDR low = attr->value_as_address ();
14275 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14276
cd6c91b4 14277 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14278 high += low;
9a619af0 14279
3e29f34a
MR
14280 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14281 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14282 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14283 }
14284 }
14285
14286 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14287 if (attr != nullptr)
801e3a5b 14288 {
18a8505e 14289 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14290 We take advantage of the fact that DW_AT_ranges does not appear
14291 in DW_TAG_compile_unit of DWO files. */
14292 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14293
14294 /* The value of the DW_AT_ranges attribute is the offset of the
14295 address range list in the .debug_ranges section. */
ab435259
DE
14296 unsigned long offset = (DW_UNSND (attr)
14297 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14298
2d5f09ec 14299 std::vector<blockrange> blockvec;
5f46c5a5
JK
14300 dwarf2_ranges_process (offset, cu,
14301 [&] (CORE_ADDR start, CORE_ADDR end)
14302 {
58fdfd2c
JK
14303 start += baseaddr;
14304 end += baseaddr;
5f46c5a5
JK
14305 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14306 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14307 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14308 blockvec.emplace_back (start, end);
5f46c5a5 14309 });
2d5f09ec
KB
14310
14311 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14312 }
14313}
14314
685b1105
JK
14315/* Check whether the producer field indicates either of GCC < 4.6, or the
14316 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14317
685b1105
JK
14318static void
14319check_producer (struct dwarf2_cu *cu)
60d5a603 14320{
38360086 14321 int major, minor;
60d5a603
JK
14322
14323 if (cu->producer == NULL)
14324 {
14325 /* For unknown compilers expect their behavior is DWARF version
14326 compliant.
14327
14328 GCC started to support .debug_types sections by -gdwarf-4 since
14329 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14330 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14331 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14332 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14333 }
b1ffba5a 14334 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14335 {
38360086
MW
14336 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14337 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14338 }
5230b05a 14339 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14340 {
14341 cu->producer_is_icc = true;
14342 cu->producer_is_icc_lt_14 = major < 14;
14343 }
c258c396
JD
14344 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14345 cu->producer_is_codewarrior = true;
685b1105
JK
14346 else
14347 {
14348 /* For other non-GCC compilers, expect their behavior is DWARF version
14349 compliant. */
60d5a603
JK
14350 }
14351
9068261f 14352 cu->checked_producer = true;
685b1105 14353}
ba919b58 14354
685b1105
JK
14355/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14356 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14357 during 4.6.0 experimental. */
14358
9068261f 14359static bool
685b1105
JK
14360producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14361{
14362 if (!cu->checked_producer)
14363 check_producer (cu);
14364
14365 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14366}
14367
c258c396
JD
14368
14369/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14370 with incorrect is_stmt attributes. */
14371
14372static bool
14373producer_is_codewarrior (struct dwarf2_cu *cu)
14374{
14375 if (!cu->checked_producer)
14376 check_producer (cu);
14377
14378 return cu->producer_is_codewarrior;
14379}
14380
405feb71 14381/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14382 DW_AT_accessibility. */
14383
14384static enum dwarf_access_attribute
14385dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14386{
14387 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14388 {
14389 /* The default DWARF 2 accessibility for members is public, the default
14390 accessibility for inheritance is private. */
14391
14392 if (die->tag != DW_TAG_inheritance)
14393 return DW_ACCESS_public;
14394 else
14395 return DW_ACCESS_private;
14396 }
14397 else
14398 {
14399 /* DWARF 3+ defines the default accessibility a different way. The same
14400 rules apply now for DW_TAG_inheritance as for the members and it only
14401 depends on the container kind. */
14402
14403 if (die->parent->tag == DW_TAG_class_type)
14404 return DW_ACCESS_private;
14405 else
14406 return DW_ACCESS_public;
14407 }
14408}
14409
74ac6d43
TT
14410/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14411 offset. If the attribute was not found return 0, otherwise return
14412 1. If it was found but could not properly be handled, set *OFFSET
14413 to 0. */
14414
14415static int
14416handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14417 LONGEST *offset)
14418{
14419 struct attribute *attr;
14420
14421 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14422 if (attr != NULL)
14423 {
14424 *offset = 0;
14425
14426 /* Note that we do not check for a section offset first here.
14427 This is because DW_AT_data_member_location is new in DWARF 4,
14428 so if we see it, we can assume that a constant form is really
14429 a constant and not a section offset. */
cd6c91b4 14430 if (attr->form_is_constant ())
0826b30a 14431 *offset = attr->constant_value (0);
cd6c91b4 14432 else if (attr->form_is_section_offset ())
74ac6d43 14433 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14434 else if (attr->form_is_block ())
74ac6d43
TT
14435 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14436 else
14437 dwarf2_complex_location_expr_complaint ();
14438
14439 return 1;
14440 }
14441
14442 return 0;
14443}
14444
7d79de9a
TT
14445/* Look for DW_AT_data_member_location and store the results in FIELD. */
14446
14447static void
14448handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14449 struct field *field)
14450{
14451 struct attribute *attr;
14452
14453 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14454 if (attr != NULL)
14455 {
14456 if (attr->form_is_constant ())
14457 {
14458 LONGEST offset = attr->constant_value (0);
14459 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14460 }
14461 else if (attr->form_is_section_offset ())
14462 dwarf2_complex_location_expr_complaint ();
14463 else if (attr->form_is_block ())
14464 {
14465 bool handled;
14466 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14467 if (handled)
14468 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14469 else
14470 {
5e22e966
SM
14471 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14472 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14473 struct dwarf2_locexpr_baton *dlbaton
14474 = XOBNEW (&objfile->objfile_obstack,
14475 struct dwarf2_locexpr_baton);
14476 dlbaton->data = DW_BLOCK (attr)->data;
14477 dlbaton->size = DW_BLOCK (attr)->size;
14478 /* When using this baton, we want to compute the address
14479 of the field, not the value. This is why
14480 is_reference is set to false here. */
14481 dlbaton->is_reference = false;
5e22e966 14482 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14483 dlbaton->per_cu = cu->per_cu;
14484
14485 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14486 }
14487 }
14488 else
14489 dwarf2_complex_location_expr_complaint ();
14490 }
14491}
14492
c906108c
SS
14493/* Add an aggregate field to the field list. */
14494
14495static void
107d2387 14496dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14497 struct dwarf2_cu *cu)
6e70227d 14498{
5e22e966 14499 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14500 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14501 struct nextfield *new_field;
14502 struct attribute *attr;
14503 struct field *fp;
15d034d0 14504 const char *fieldname = "";
c906108c 14505
7d0ccb61
DJ
14506 if (die->tag == DW_TAG_inheritance)
14507 {
be2daae6
TT
14508 fip->baseclasses.emplace_back ();
14509 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14510 }
14511 else
14512 {
be2daae6
TT
14513 fip->fields.emplace_back ();
14514 new_field = &fip->fields.back ();
7d0ccb61 14515 }
be2daae6 14516
9c6a1327
TT
14517 new_field->offset = die->sect_off;
14518
e142c38c 14519 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14520 if (attr != nullptr)
c906108c 14521 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14522 else
14523 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14524 if (new_field->accessibility != DW_ACCESS_public)
14525 fip->non_public_fields = 1;
60d5a603 14526
e142c38c 14527 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14528 if (attr != nullptr)
c906108c 14529 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14530 else
14531 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14532
14533 fp = &new_field->field;
a9a9bd0f 14534
e142c38c 14535 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14536 {
a9a9bd0f 14537 /* Data member other than a C++ static data member. */
6e70227d 14538
c906108c 14539 /* Get type of field. */
5d14b6e5 14540 fp->set_type (die_type (die, cu));
c906108c 14541
d6a843b5 14542 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14543
c906108c 14544 /* Get bit size of field (zero if none). */
e142c38c 14545 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14546 if (attr != nullptr)
c906108c
SS
14547 {
14548 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14549 }
14550 else
14551 {
14552 FIELD_BITSIZE (*fp) = 0;
14553 }
14554
14555 /* Get bit offset of field. */
7d79de9a 14556 handle_data_member_location (die, cu, fp);
e142c38c 14557 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14558 if (attr != nullptr)
c906108c 14559 {
d5a22e77 14560 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14561 {
14562 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14563 additional bit offset from the MSB of the containing
14564 anonymous object to the MSB of the field. We don't
14565 have to do anything special since we don't need to
14566 know the size of the anonymous object. */
f41f5e61 14567 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14568 }
14569 else
14570 {
14571 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14572 MSB of the anonymous object, subtract off the number of
14573 bits from the MSB of the field to the MSB of the
14574 object, and then subtract off the number of bits of
14575 the field itself. The result is the bit offset of
14576 the LSB of the field. */
c906108c
SS
14577 int anonymous_size;
14578 int bit_offset = DW_UNSND (attr);
14579
e142c38c 14580 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14581 if (attr != nullptr)
c906108c
SS
14582 {
14583 /* The size of the anonymous object containing
14584 the bit field is explicit, so use the
14585 indicated size (in bytes). */
14586 anonymous_size = DW_UNSND (attr);
14587 }
14588 else
14589 {
14590 /* The size of the anonymous object containing
14591 the bit field must be inferred from the type
14592 attribute of the data member containing the
14593 bit field. */
5d14b6e5 14594 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14595 }
f41f5e61
PA
14596 SET_FIELD_BITPOS (*fp,
14597 (FIELD_BITPOS (*fp)
14598 + anonymous_size * bits_per_byte
14599 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14600 }
14601 }
da5b30da
AA
14602 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14603 if (attr != NULL)
14604 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14605 + attr->constant_value (0)));
c906108c
SS
14606
14607 /* Get name of field. */
39cbfefa
DJ
14608 fieldname = dwarf2_name (die, cu);
14609 if (fieldname == NULL)
14610 fieldname = "";
d8151005
DJ
14611
14612 /* The name is already allocated along with this objfile, so we don't
14613 need to duplicate it for the type. */
14614 fp->name = fieldname;
c906108c
SS
14615
14616 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14617 pointer or virtual base class pointer) to private. */
e142c38c 14618 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14619 {
d48cc9dd 14620 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14621 new_field->accessibility = DW_ACCESS_private;
14622 fip->non_public_fields = 1;
14623 }
14624 }
a9a9bd0f 14625 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14626 {
a9a9bd0f
DC
14627 /* C++ static member. */
14628
14629 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14630 is a declaration, but all versions of G++ as of this writing
14631 (so through at least 3.2.1) incorrectly generate
14632 DW_TAG_variable tags. */
6e70227d 14633
ff355380 14634 const char *physname;
c906108c 14635
a9a9bd0f 14636 /* Get name of field. */
39cbfefa
DJ
14637 fieldname = dwarf2_name (die, cu);
14638 if (fieldname == NULL)
c906108c
SS
14639 return;
14640
254e6b9e 14641 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14642 if (attr
14643 /* Only create a symbol if this is an external value.
14644 new_symbol checks this and puts the value in the global symbol
14645 table, which we want. If it is not external, new_symbol
14646 will try to put the value in cu->list_in_scope which is wrong. */
14647 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14648 {
14649 /* A static const member, not much different than an enum as far as
14650 we're concerned, except that we can support more types. */
14651 new_symbol (die, NULL, cu);
14652 }
14653
2df3850c 14654 /* Get physical name. */
ff355380 14655 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14656
d8151005
DJ
14657 /* The name is already allocated along with this objfile, so we don't
14658 need to duplicate it for the type. */
14659 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14660 fp->set_type (die_type (die, cu));
d8151005 14661 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14662 }
14663 else if (die->tag == DW_TAG_inheritance)
14664 {
74ac6d43 14665 /* C++ base class field. */
7d79de9a 14666 handle_data_member_location (die, cu, fp);
c906108c 14667 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14668 fp->set_type (die_type (die, cu));
14669 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14670 }
2ddeaf8a
TT
14671 else
14672 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14673}
14674
883fd55a
KS
14675/* Can the type given by DIE define another type? */
14676
14677static bool
14678type_can_define_types (const struct die_info *die)
14679{
14680 switch (die->tag)
14681 {
14682 case DW_TAG_typedef:
14683 case DW_TAG_class_type:
14684 case DW_TAG_structure_type:
14685 case DW_TAG_union_type:
14686 case DW_TAG_enumeration_type:
14687 return true;
14688
14689 default:
14690 return false;
14691 }
14692}
14693
14694/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14695
14696static void
883fd55a
KS
14697dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14698 struct dwarf2_cu *cu)
6e70227d 14699{
be2daae6
TT
14700 struct decl_field fp;
14701 memset (&fp, 0, sizeof (fp));
98751a41 14702
883fd55a 14703 gdb_assert (type_can_define_types (die));
98751a41 14704
883fd55a 14705 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14706 fp.name = dwarf2_name (die, cu);
14707 fp.type = read_type_die (die, cu);
98751a41 14708
c191a687
KS
14709 /* Save accessibility. */
14710 enum dwarf_access_attribute accessibility;
14711 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14712 if (attr != NULL)
14713 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14714 else
14715 accessibility = dwarf2_default_access_attribute (die, cu);
14716 switch (accessibility)
14717 {
14718 case DW_ACCESS_public:
14719 /* The assumed value if neither private nor protected. */
14720 break;
14721 case DW_ACCESS_private:
be2daae6 14722 fp.is_private = 1;
c191a687
KS
14723 break;
14724 case DW_ACCESS_protected:
be2daae6 14725 fp.is_protected = 1;
c191a687
KS
14726 break;
14727 default:
b98664d3 14728 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14729 }
14730
883fd55a 14731 if (die->tag == DW_TAG_typedef)
be2daae6 14732 fip->typedef_field_list.push_back (fp);
883fd55a 14733 else
be2daae6 14734 fip->nested_types_list.push_back (fp);
98751a41
JK
14735}
14736
9c6a1327
TT
14737/* A convenience typedef that's used when finding the discriminant
14738 field for a variant part. */
1b95cdb7
SM
14739typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14740 offset_map_type;
9c6a1327
TT
14741
14742/* Compute the discriminant range for a given variant. OBSTACK is
14743 where the results will be stored. VARIANT is the variant to
14744 process. IS_UNSIGNED indicates whether the discriminant is signed
14745 or unsigned. */
14746
14747static const gdb::array_view<discriminant_range>
14748convert_variant_range (struct obstack *obstack, const variant_field &variant,
14749 bool is_unsigned)
14750{
14751 std::vector<discriminant_range> ranges;
14752
14753 if (variant.default_branch)
14754 return {};
14755
14756 if (variant.discr_list_data == nullptr)
14757 {
14758 discriminant_range r
14759 = {variant.discriminant_value, variant.discriminant_value};
14760 ranges.push_back (r);
14761 }
14762 else
14763 {
14764 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14765 variant.discr_list_data->size);
14766 while (!data.empty ())
14767 {
14768 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14769 {
14770 complaint (_("invalid discriminant marker: %d"), data[0]);
14771 break;
14772 }
14773 bool is_range = data[0] == DW_DSC_range;
14774 data = data.slice (1);
14775
14776 ULONGEST low, high;
14777 unsigned int bytes_read;
14778
14779 if (data.empty ())
14780 {
14781 complaint (_("DW_AT_discr_list missing low value"));
14782 break;
14783 }
14784 if (is_unsigned)
14785 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14786 else
14787 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14788 &bytes_read);
14789 data = data.slice (bytes_read);
14790
14791 if (is_range)
14792 {
14793 if (data.empty ())
14794 {
14795 complaint (_("DW_AT_discr_list missing high value"));
14796 break;
14797 }
14798 if (is_unsigned)
14799 high = read_unsigned_leb128 (nullptr, data.data (),
14800 &bytes_read);
14801 else
14802 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14803 &bytes_read);
14804 data = data.slice (bytes_read);
14805 }
14806 else
14807 high = low;
14808
14809 ranges.push_back ({ low, high });
14810 }
14811 }
14812
14813 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14814 ranges.size ());
14815 std::copy (ranges.begin (), ranges.end (), result);
14816 return gdb::array_view<discriminant_range> (result, ranges.size ());
14817}
14818
14819static const gdb::array_view<variant_part> create_variant_parts
14820 (struct obstack *obstack,
14821 const offset_map_type &offset_map,
14822 struct field_info *fi,
14823 const std::vector<variant_part_builder> &variant_parts);
14824
14825/* Fill in a "struct variant" for a given variant field. RESULT is
14826 the variant to fill in. OBSTACK is where any needed allocations
14827 will be done. OFFSET_MAP holds the mapping from section offsets to
14828 fields for the type. FI describes the fields of the type we're
14829 processing. FIELD is the variant field we're converting. */
14830
14831static void
14832create_one_variant (variant &result, struct obstack *obstack,
14833 const offset_map_type &offset_map,
14834 struct field_info *fi, const variant_field &field)
14835{
14836 result.discriminants = convert_variant_range (obstack, field, false);
14837 result.first_field = field.first_field + fi->baseclasses.size ();
14838 result.last_field = field.last_field + fi->baseclasses.size ();
14839 result.parts = create_variant_parts (obstack, offset_map, fi,
14840 field.variant_parts);
14841}
14842
14843/* Fill in a "struct variant_part" for a given variant part. RESULT
14844 is the variant part to fill in. OBSTACK is where any needed
14845 allocations will be done. OFFSET_MAP holds the mapping from
14846 section offsets to fields for the type. FI describes the fields of
14847 the type we're processing. BUILDER is the variant part to be
14848 converted. */
14849
14850static void
14851create_one_variant_part (variant_part &result,
14852 struct obstack *obstack,
14853 const offset_map_type &offset_map,
14854 struct field_info *fi,
14855 const variant_part_builder &builder)
14856{
14857 auto iter = offset_map.find (builder.discriminant_offset);
14858 if (iter == offset_map.end ())
14859 {
14860 result.discriminant_index = -1;
14861 /* Doesn't matter. */
14862 result.is_unsigned = false;
14863 }
14864 else
14865 {
14866 result.discriminant_index = iter->second;
14867 result.is_unsigned
b6cdac4b 14868 = TYPE_UNSIGNED (fi->fields[result.discriminant_index].field.type ());
9c6a1327
TT
14869 }
14870
14871 size_t n = builder.variants.size ();
14872 variant *output = new (obstack) variant[n];
14873 for (size_t i = 0; i < n; ++i)
14874 create_one_variant (output[i], obstack, offset_map, fi,
14875 builder.variants[i]);
14876
14877 result.variants = gdb::array_view<variant> (output, n);
14878}
14879
14880/* Create a vector of variant parts that can be attached to a type.
14881 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14882 holds the mapping from section offsets to fields for the type. FI
14883 describes the fields of the type we're processing. VARIANT_PARTS
14884 is the vector to convert. */
14885
14886static const gdb::array_view<variant_part>
14887create_variant_parts (struct obstack *obstack,
14888 const offset_map_type &offset_map,
14889 struct field_info *fi,
14890 const std::vector<variant_part_builder> &variant_parts)
14891{
14892 if (variant_parts.empty ())
14893 return {};
14894
14895 size_t n = variant_parts.size ();
14896 variant_part *result = new (obstack) variant_part[n];
14897 for (size_t i = 0; i < n; ++i)
14898 create_one_variant_part (result[i], obstack, offset_map, fi,
14899 variant_parts[i]);
14900
14901 return gdb::array_view<variant_part> (result, n);
14902}
14903
14904/* Compute the variant part vector for FIP, attaching it to TYPE when
14905 done. */
14906
14907static void
14908add_variant_property (struct field_info *fip, struct type *type,
14909 struct dwarf2_cu *cu)
14910{
14911 /* Map section offsets of fields to their field index. Note the
14912 field index here does not take the number of baseclasses into
14913 account. */
14914 offset_map_type offset_map;
14915 for (int i = 0; i < fip->fields.size (); ++i)
14916 offset_map[fip->fields[i].offset] = i;
14917
5e22e966 14918 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14919 gdb::array_view<variant_part> parts
14920 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14921 fip->variant_parts);
14922
14923 struct dynamic_prop prop;
14924 prop.kind = PROP_VARIANT_PARTS;
14925 prop.data.variant_parts
14926 = ((gdb::array_view<variant_part> *)
14927 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14928
5c54719c 14929 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14930}
14931
c906108c
SS
14932/* Create the vector of fields, and attach it to the type. */
14933
14934static void
fba45db2 14935dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14936 struct dwarf2_cu *cu)
c906108c 14937{
317f7127 14938 int nfields = fip->nfields ();
c906108c
SS
14939
14940 /* Record the field count, allocate space for the array of fields,
14941 and create blank accessibility bitfields if necessary. */
5e33d5f4 14942 type->set_num_fields (nfields);
3cabb6b0
SM
14943 type->set_fields
14944 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14945
b4ba55a1 14946 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14947 {
14948 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14949
14950 TYPE_FIELD_PRIVATE_BITS (type) =
14951 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14952 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14953
14954 TYPE_FIELD_PROTECTED_BITS (type) =
14955 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14956 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14957
774b6a14
TT
14958 TYPE_FIELD_IGNORE_BITS (type) =
14959 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14960 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14961 }
14962
14963 /* If the type has baseclasses, allocate and clear a bit vector for
14964 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14965 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14966 {
be2daae6 14967 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14968 unsigned char *pointer;
c906108c
SS
14969
14970 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14971 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14972 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14973 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14974 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14975 }
14976
9c6a1327
TT
14977 if (!fip->variant_parts.empty ())
14978 add_variant_property (fip, type, cu);
2ddeaf8a 14979
be2daae6
TT
14980 /* Copy the saved-up fields into the field vector. */
14981 for (int i = 0; i < nfields; ++i)
c906108c 14982 {
be2daae6
TT
14983 struct nextfield &field
14984 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14985 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14986
ceacbf6e 14987 type->field (i) = field.field;
be2daae6 14988 switch (field.accessibility)
c906108c 14989 {
c5aa993b 14990 case DW_ACCESS_private:
b4ba55a1 14991 if (cu->language != language_ada)
be2daae6 14992 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14993 break;
c906108c 14994
c5aa993b 14995 case DW_ACCESS_protected:
b4ba55a1 14996 if (cu->language != language_ada)
be2daae6 14997 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14998 break;
c906108c 14999
c5aa993b
JM
15000 case DW_ACCESS_public:
15001 break;
c906108c 15002
c5aa993b
JM
15003 default:
15004 /* Unknown accessibility. Complain and treat it as public. */
15005 {
b98664d3 15006 complaint (_("unsupported accessibility %d"),
be2daae6 15007 field.accessibility);
c5aa993b
JM
15008 }
15009 break;
c906108c 15010 }
be2daae6 15011 if (i < fip->baseclasses.size ())
c906108c 15012 {
be2daae6 15013 switch (field.virtuality)
c906108c 15014 {
c5aa993b
JM
15015 case DW_VIRTUALITY_virtual:
15016 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15017 if (cu->language == language_ada)
a73c6dcd 15018 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15019 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15020 break;
c906108c
SS
15021 }
15022 }
c906108c
SS
15023 }
15024}
15025
7d27a96d
TT
15026/* Return true if this member function is a constructor, false
15027 otherwise. */
15028
15029static int
15030dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15031{
15032 const char *fieldname;
fe978cb0 15033 const char *type_name;
7d27a96d
TT
15034 int len;
15035
15036 if (die->parent == NULL)
15037 return 0;
15038
15039 if (die->parent->tag != DW_TAG_structure_type
15040 && die->parent->tag != DW_TAG_union_type
15041 && die->parent->tag != DW_TAG_class_type)
15042 return 0;
15043
15044 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15045 type_name = dwarf2_name (die->parent, cu);
15046 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15047 return 0;
15048
15049 len = strlen (fieldname);
fe978cb0
PA
15050 return (strncmp (fieldname, type_name, len) == 0
15051 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15052}
15053
e35000a7
TBA
15054/* Check if the given VALUE is a recognized enum
15055 dwarf_defaulted_attribute constant according to DWARF5 spec,
15056 Table 7.24. */
15057
15058static bool
15059is_valid_DW_AT_defaulted (ULONGEST value)
15060{
15061 switch (value)
15062 {
15063 case DW_DEFAULTED_no:
15064 case DW_DEFAULTED_in_class:
15065 case DW_DEFAULTED_out_of_class:
15066 return true;
15067 }
15068
3142e908 15069 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15070 return false;
15071}
15072
c906108c
SS
15073/* Add a member function to the proper fieldlist. */
15074
15075static void
107d2387 15076dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15077 struct type *type, struct dwarf2_cu *cu)
c906108c 15078{
5e22e966 15079 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15080 struct attribute *attr;
c906108c 15081 int i;
be2daae6 15082 struct fnfieldlist *flp = nullptr;
c906108c 15083 struct fn_field *fnp;
15d034d0 15084 const char *fieldname;
f792889a 15085 struct type *this_type;
60d5a603 15086 enum dwarf_access_attribute accessibility;
c906108c 15087
b4ba55a1 15088 if (cu->language == language_ada)
a73c6dcd 15089 error (_("unexpected member function in Ada type"));
b4ba55a1 15090
2df3850c 15091 /* Get name of member function. */
39cbfefa
DJ
15092 fieldname = dwarf2_name (die, cu);
15093 if (fieldname == NULL)
2df3850c 15094 return;
c906108c 15095
c906108c 15096 /* Look up member function name in fieldlist. */
be2daae6 15097 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15098 {
27bfe10e 15099 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15100 {
15101 flp = &fip->fnfieldlists[i];
15102 break;
15103 }
c906108c
SS
15104 }
15105
be2daae6
TT
15106 /* Create a new fnfieldlist if necessary. */
15107 if (flp == nullptr)
c906108c 15108 {
be2daae6
TT
15109 fip->fnfieldlists.emplace_back ();
15110 flp = &fip->fnfieldlists.back ();
c906108c 15111 flp->name = fieldname;
be2daae6 15112 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15113 }
15114
be2daae6
TT
15115 /* Create a new member function field and add it to the vector of
15116 fnfieldlists. */
15117 flp->fnfields.emplace_back ();
15118 fnp = &flp->fnfields.back ();
3da10d80
KS
15119
15120 /* Delay processing of the physname until later. */
9c37b5ae 15121 if (cu->language == language_cplus)
be2daae6
TT
15122 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15123 die, cu);
3da10d80
KS
15124 else
15125 {
1d06ead6 15126 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15127 fnp->physname = physname ? physname : "";
15128 }
15129
c906108c 15130 fnp->type = alloc_type (objfile);
f792889a 15131 this_type = read_type_die (die, cu);
78134374 15132 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15133 {
1f704f76 15134 int nparams = this_type->num_fields ();
c906108c 15135
f792889a 15136 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15137 of the method itself (TYPE_CODE_METHOD). */
15138 smash_to_method_type (fnp->type, type,
f792889a 15139 TYPE_TARGET_TYPE (this_type),
80fc5e77 15140 this_type->fields (),
1f704f76 15141 this_type->num_fields (),
f792889a 15142 TYPE_VARARGS (this_type));
c906108c
SS
15143
15144 /* Handle static member functions.
c5aa993b 15145 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15146 member functions. G++ helps GDB by marking the first
15147 parameter for non-static member functions (which is the this
15148 pointer) as artificial. We obtain this information from
15149 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15150 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15151 fnp->voffset = VOFFSET_STATIC;
15152 }
15153 else
b98664d3 15154 complaint (_("member function type missing for '%s'"),
3da10d80 15155 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15156
15157 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15158 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15159 fnp->fcontext = die_containing_type (die, cu);
c906108c 15160
3e43a32a
MS
15161 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15162 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15163
15164 /* Get accessibility. */
e142c38c 15165 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15166 if (attr != nullptr)
aead7601 15167 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15168 else
15169 accessibility = dwarf2_default_access_attribute (die, cu);
15170 switch (accessibility)
c906108c 15171 {
60d5a603
JK
15172 case DW_ACCESS_private:
15173 fnp->is_private = 1;
15174 break;
15175 case DW_ACCESS_protected:
15176 fnp->is_protected = 1;
15177 break;
c906108c
SS
15178 }
15179
b02dede2 15180 /* Check for artificial methods. */
e142c38c 15181 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15182 if (attr && DW_UNSND (attr) != 0)
15183 fnp->is_artificial = 1;
15184
e35000a7
TBA
15185 /* Check for defaulted methods. */
15186 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15187 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15188 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15189
15190 /* Check for deleted methods. */
15191 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15192 if (attr != nullptr && DW_UNSND (attr) != 0)
15193 fnp->is_deleted = 1;
15194
7d27a96d
TT
15195 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15196
0d564a31 15197 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15198 function. For older versions of GCC, this is an offset in the
15199 appropriate virtual table, as specified by DW_AT_containing_type.
15200 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15201 to the object address. */
15202
e142c38c 15203 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15204 if (attr != nullptr)
8e19ed76 15205 {
4fc6c0d5 15206 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15207 {
aec5aa8b
TT
15208 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15209 {
15210 /* Old-style GCC. */
15211 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15212 }
15213 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15214 || (DW_BLOCK (attr)->size > 1
15215 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15216 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15217 {
aec5aa8b
TT
15218 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15219 if ((fnp->voffset % cu->header.addr_size) != 0)
15220 dwarf2_complex_location_expr_complaint ();
15221 else
15222 fnp->voffset /= cu->header.addr_size;
15223 fnp->voffset += 2;
15224 }
15225 else
15226 dwarf2_complex_location_expr_complaint ();
15227
15228 if (!fnp->fcontext)
7e993ebf
KS
15229 {
15230 /* If there is no `this' field and no DW_AT_containing_type,
15231 we cannot actually find a base class context for the
15232 vtable! */
1f704f76 15233 if (this_type->num_fields () == 0
7e993ebf
KS
15234 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15235 {
b98664d3 15236 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15237 "function \"%s\" (offset %s)"),
15238 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15239 }
15240 else
15241 {
15242 fnp->fcontext
940da03e 15243 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15244 }
15245 }
aec5aa8b 15246 }
cd6c91b4 15247 else if (attr->form_is_section_offset ())
8e19ed76 15248 {
4d3c2250 15249 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15250 }
15251 else
15252 {
4d3c2250
KB
15253 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15254 fieldname);
8e19ed76 15255 }
0d564a31 15256 }
d48cc9dd
DJ
15257 else
15258 {
15259 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15260 if (attr && DW_UNSND (attr))
15261 {
15262 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15263 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15264 "but the vtable offset is not specified"),
9d8780f0 15265 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15266 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15267 TYPE_CPLUS_DYNAMIC (type) = 1;
15268 }
15269 }
c906108c
SS
15270}
15271
15272/* Create the vector of member function fields, and attach it to the type. */
15273
15274static void
fba45db2 15275dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15276 struct dwarf2_cu *cu)
c906108c 15277{
b4ba55a1 15278 if (cu->language == language_ada)
a73c6dcd 15279 error (_("unexpected member functions in Ada type"));
b4ba55a1 15280
c906108c
SS
15281 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15282 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15283 TYPE_ALLOC (type,
15284 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15285
be2daae6 15286 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15287 {
be2daae6 15288 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15289 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15290
be2daae6
TT
15291 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15292 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15293 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15294 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15295
15296 for (int k = 0; k < nf.fnfields.size (); ++k)
15297 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15298 }
15299
be2daae6 15300 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15301}
15302
1168df01
JB
15303/* Returns non-zero if NAME is the name of a vtable member in CU's
15304 language, zero otherwise. */
15305static int
15306is_vtable_name (const char *name, struct dwarf2_cu *cu)
15307{
15308 static const char vptr[] = "_vptr";
15309
9c37b5ae
TT
15310 /* Look for the C++ form of the vtable. */
15311 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15312 return 1;
15313
15314 return 0;
15315}
15316
c0dd20ea 15317/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15318 functions, with the ABI-specified layout. If TYPE describes
15319 such a structure, smash it into a member function type.
61049d3b
DJ
15320
15321 GCC shouldn't do this; it should just output pointer to member DIEs.
15322 This is GCC PR debug/28767. */
c0dd20ea 15323
0b92b5bb
TT
15324static void
15325quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15326{
09e2d7c7 15327 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15328
15329 /* Check for a structure with no name and two children. */
1f704f76 15330 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15331 return;
c0dd20ea
DJ
15332
15333 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15334 if (TYPE_FIELD_NAME (type, 0) == NULL
15335 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15336 || TYPE_FIELD_NAME (type, 1) == NULL
15337 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15338 return;
c0dd20ea
DJ
15339
15340 /* Find the type of the method. */
940da03e 15341 pfn_type = type->field (0).type ();
c0dd20ea 15342 if (pfn_type == NULL
78134374
SM
15343 || pfn_type->code () != TYPE_CODE_PTR
15344 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15345 return;
c0dd20ea
DJ
15346
15347 /* Look for the "this" argument. */
15348 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15349 if (pfn_type->num_fields () == 0
940da03e
SM
15350 /* || pfn_type->field (0).type () == NULL */
15351 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15352 return;
c0dd20ea 15353
940da03e 15354 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15355 new_type = alloc_type (objfile);
09e2d7c7 15356 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15357 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15358 TYPE_VARARGS (pfn_type));
0b92b5bb 15359 smash_to_methodptr_type (type, new_type);
c0dd20ea 15360}
1168df01 15361
2b4424c3
TT
15362/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15363 appropriate error checking and issuing complaints if there is a
15364 problem. */
15365
15366static ULONGEST
15367get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15368{
15369 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15370
15371 if (attr == nullptr)
15372 return 0;
15373
cd6c91b4 15374 if (!attr->form_is_constant ())
2b4424c3 15375 {
b98664d3 15376 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15377 " - DIE at %s [in module %s]"),
15378 sect_offset_str (die->sect_off),
5e22e966 15379 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15380 return 0;
15381 }
15382
15383 ULONGEST align;
15384 if (attr->form == DW_FORM_sdata)
15385 {
15386 LONGEST val = DW_SND (attr);
15387 if (val < 0)
15388 {
b98664d3 15389 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15390 " - DIE at %s [in module %s]"),
15391 sect_offset_str (die->sect_off),
5e22e966 15392 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15393 return 0;
15394 }
15395 align = val;
15396 }
15397 else
15398 align = DW_UNSND (attr);
15399
15400 if (align == 0)
15401 {
b98664d3 15402 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15403 " - DIE at %s [in module %s]"),
15404 sect_offset_str (die->sect_off),
5e22e966 15405 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15406 return 0;
15407 }
15408 if ((align & (align - 1)) != 0)
15409 {
b98664d3 15410 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15411 " - DIE at %s [in module %s]"),
15412 sect_offset_str (die->sect_off),
5e22e966 15413 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15414 return 0;
15415 }
15416
15417 return align;
15418}
15419
15420/* If the DIE has a DW_AT_alignment attribute, use its value to set
15421 the alignment for TYPE. */
15422
15423static void
15424maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15425 struct type *type)
15426{
15427 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15428 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15429 " - DIE at %s [in module %s]"),
15430 sect_offset_str (die->sect_off),
5e22e966 15431 objfile_name (cu->per_objfile->objfile));
2b4424c3 15432}
685b1105 15433
e35000a7
TBA
15434/* Check if the given VALUE is a valid enum dwarf_calling_convention
15435 constant for a type, according to DWARF5 spec, Table 5.5. */
15436
15437static bool
15438is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15439{
15440 switch (value)
15441 {
15442 case DW_CC_normal:
15443 case DW_CC_pass_by_reference:
15444 case DW_CC_pass_by_value:
15445 return true;
15446
15447 default:
15448 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15449 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15450 return false;
15451 }
15452}
15453
d0922fcf
TBA
15454/* Check if the given VALUE is a valid enum dwarf_calling_convention
15455 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15456 also according to GNU-specific values (see include/dwarf2.h). */
15457
15458static bool
15459is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15460{
15461 switch (value)
15462 {
15463 case DW_CC_normal:
15464 case DW_CC_program:
15465 case DW_CC_nocall:
15466 return true;
15467
15468 case DW_CC_GNU_renesas_sh:
15469 case DW_CC_GNU_borland_fastcall_i386:
15470 case DW_CC_GDB_IBM_OpenCL:
15471 return true;
15472
15473 default:
15474 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15475 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15476 return false;
15477 }
15478}
15479
c906108c 15480/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15481 (definition) to create a type for the structure or union. Fill in
15482 the type's name and general properties; the members will not be
83655187
DE
15483 processed until process_structure_scope. A symbol table entry for
15484 the type will also not be done until process_structure_scope (assuming
15485 the type has a name).
c906108c 15486
c767944b
DJ
15487 NOTE: we need to call these functions regardless of whether or not the
15488 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15489 structure or union. This gets the type entered into our set of
83655187 15490 user defined types. */
c906108c 15491
f792889a 15492static struct type *
134d01f1 15493read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15494{
5e22e966 15495 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15496 struct type *type;
15497 struct attribute *attr;
15d034d0 15498 const char *name;
c906108c 15499
348e048f
DE
15500 /* If the definition of this type lives in .debug_types, read that type.
15501 Don't follow DW_AT_specification though, that will take us back up
15502 the chain and we want to go down. */
052c8bb8 15503 attr = die->attr (DW_AT_signature);
435d3d88 15504 if (attr != nullptr)
348e048f 15505 {
ac9ec31b 15506 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15507
ac9ec31b 15508 /* The type's CU may not be the same as CU.
02142a6c 15509 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15510 return set_die_type (die, type, cu);
15511 }
15512
c0dd20ea 15513 type = alloc_type (objfile);
c906108c 15514 INIT_CPLUS_SPECIFIC (type);
93311388 15515
39cbfefa
DJ
15516 name = dwarf2_name (die, cu);
15517 if (name != NULL)
c906108c 15518 {
987504bb 15519 if (cu->language == language_cplus
c44af4eb
TT
15520 || cu->language == language_d
15521 || cu->language == language_rust)
63d06c5c 15522 {
15d034d0 15523 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15524
15525 /* dwarf2_full_name might have already finished building the DIE's
15526 type. If so, there is no need to continue. */
15527 if (get_die_type (die, cu) != NULL)
15528 return get_die_type (die, cu);
15529
d0e39ea2 15530 type->set_name (full_name);
63d06c5c
DC
15531 }
15532 else
15533 {
d8151005
DJ
15534 /* The name is already allocated along with this objfile, so
15535 we don't need to duplicate it for the type. */
d0e39ea2 15536 type->set_name (name);
63d06c5c 15537 }
c906108c
SS
15538 }
15539
15540 if (die->tag == DW_TAG_structure_type)
15541 {
67607e24 15542 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15543 }
15544 else if (die->tag == DW_TAG_union_type)
15545 {
67607e24 15546 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15547 }
15548 else
15549 {
67607e24 15550 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15551 }
15552
0cc2414c
TT
15553 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15554 TYPE_DECLARED_CLASS (type) = 1;
15555
e35000a7
TBA
15556 /* Store the calling convention in the type if it's available in
15557 the die. Otherwise the calling convention remains set to
15558 the default value DW_CC_normal. */
15559 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15560 if (attr != nullptr
15561 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15562 {
15563 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15564 TYPE_CPLUS_CALLING_CONVENTION (type)
15565 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15566 }
15567
e142c38c 15568 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15569 if (attr != nullptr)
c906108c 15570 {
cd6c91b4 15571 if (attr->form_is_constant ())
155bfbd3
JB
15572 TYPE_LENGTH (type) = DW_UNSND (attr);
15573 else
15574 {
f8e89861 15575 struct dynamic_prop prop;
293e7e51 15576 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15577 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15578 TYPE_LENGTH (type) = 0;
15579 }
c906108c
SS
15580 }
15581 else
15582 {
15583 TYPE_LENGTH (type) = 0;
15584 }
15585
2b4424c3
TT
15586 maybe_set_alignment (cu, die, type);
15587
5230b05a 15588 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15589 {
5230b05a
WT
15590 /* ICC<14 does not output the required DW_AT_declaration on
15591 incomplete types, but gives them a size of zero. */
422b1cb0 15592 TYPE_STUB (type) = 1;
685b1105
JK
15593 }
15594 else
15595 TYPE_STUB_SUPPORTED (type) = 1;
15596
dc718098 15597 if (die_is_declaration (die, cu))
876cecd0 15598 TYPE_STUB (type) = 1;
a6c727b2
DJ
15599 else if (attr == NULL && die->child == NULL
15600 && producer_is_realview (cu->producer))
15601 /* RealView does not output the required DW_AT_declaration
15602 on incomplete types. */
15603 TYPE_STUB (type) = 1;
dc718098 15604
c906108c
SS
15605 /* We need to add the type field to the die immediately so we don't
15606 infinitely recurse when dealing with pointers to the structure
0963b4bd 15607 type within the structure itself. */
1c379e20 15608 set_die_type (die, type, cu);
c906108c 15609
7e314c57
JK
15610 /* set_die_type should be already done. */
15611 set_descriptive_type (type, die, cu);
15612
c767944b
DJ
15613 return type;
15614}
15615
9c6a1327
TT
15616static void handle_struct_member_die
15617 (struct die_info *child_die,
15618 struct type *type,
15619 struct field_info *fi,
15620 std::vector<struct symbol *> *template_args,
15621 struct dwarf2_cu *cu);
15622
15623/* A helper for handle_struct_member_die that handles
15624 DW_TAG_variant_part. */
15625
15626static void
15627handle_variant_part (struct die_info *die, struct type *type,
15628 struct field_info *fi,
15629 std::vector<struct symbol *> *template_args,
15630 struct dwarf2_cu *cu)
15631{
15632 variant_part_builder *new_part;
15633 if (fi->current_variant_part == nullptr)
15634 {
15635 fi->variant_parts.emplace_back ();
15636 new_part = &fi->variant_parts.back ();
15637 }
15638 else if (!fi->current_variant_part->processing_variant)
15639 {
15640 complaint (_("nested DW_TAG_variant_part seen "
15641 "- DIE at %s [in module %s]"),
15642 sect_offset_str (die->sect_off),
5e22e966 15643 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15644 return;
15645 }
15646 else
15647 {
15648 variant_field &current = fi->current_variant_part->variants.back ();
15649 current.variant_parts.emplace_back ();
15650 new_part = &current.variant_parts.back ();
15651 }
15652
15653 /* When we recurse, we want callees to add to this new variant
15654 part. */
15655 scoped_restore save_current_variant_part
15656 = make_scoped_restore (&fi->current_variant_part, new_part);
15657
15658 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15659 if (discr == NULL)
15660 {
15661 /* It's a univariant form, an extension we support. */
15662 }
15663 else if (discr->form_is_ref ())
15664 {
15665 struct dwarf2_cu *target_cu = cu;
15666 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15667
15668 new_part->discriminant_offset = target_die->sect_off;
15669 }
15670 else
15671 {
15672 complaint (_("DW_AT_discr does not have DIE reference form"
15673 " - DIE at %s [in module %s]"),
15674 sect_offset_str (die->sect_off),
5e22e966 15675 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15676 }
15677
15678 for (die_info *child_die = die->child;
15679 child_die != NULL;
15680 child_die = child_die->sibling)
15681 handle_struct_member_die (child_die, type, fi, template_args, cu);
15682}
15683
15684/* A helper for handle_struct_member_die that handles
15685 DW_TAG_variant. */
15686
15687static void
15688handle_variant (struct die_info *die, struct type *type,
15689 struct field_info *fi,
15690 std::vector<struct symbol *> *template_args,
15691 struct dwarf2_cu *cu)
15692{
15693 if (fi->current_variant_part == nullptr)
15694 {
15695 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15696 "- DIE at %s [in module %s]"),
15697 sect_offset_str (die->sect_off),
5e22e966 15698 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15699 return;
15700 }
15701 if (fi->current_variant_part->processing_variant)
15702 {
15703 complaint (_("nested DW_TAG_variant seen "
15704 "- DIE at %s [in module %s]"),
15705 sect_offset_str (die->sect_off),
5e22e966 15706 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15707 return;
15708 }
15709
15710 scoped_restore save_processing_variant
15711 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15712 true);
15713
15714 fi->current_variant_part->variants.emplace_back ();
15715 variant_field &variant = fi->current_variant_part->variants.back ();
15716 variant.first_field = fi->fields.size ();
15717
15718 /* In a variant we want to get the discriminant and also add a
15719 field for our sole member child. */
15720 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15721 if (discr == nullptr)
15722 {
15723 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15724 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15725 variant.default_branch = true;
15726 else
15727 variant.discr_list_data = DW_BLOCK (discr);
15728 }
15729 else
15730 variant.discriminant_value = DW_UNSND (discr);
15731
15732 for (die_info *variant_child = die->child;
15733 variant_child != NULL;
15734 variant_child = variant_child->sibling)
15735 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15736
15737 variant.last_field = fi->fields.size ();
15738}
15739
2ddeaf8a
TT
15740/* A helper for process_structure_scope that handles a single member
15741 DIE. */
15742
15743static void
15744handle_struct_member_die (struct die_info *child_die, struct type *type,
15745 struct field_info *fi,
15746 std::vector<struct symbol *> *template_args,
15747 struct dwarf2_cu *cu)
15748{
15749 if (child_die->tag == DW_TAG_member
9c6a1327 15750 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15751 {
15752 /* NOTE: carlton/2002-11-05: A C++ static data member
15753 should be a DW_TAG_member that is a declaration, but
15754 all versions of G++ as of this writing (so through at
15755 least 3.2.1) incorrectly generate DW_TAG_variable
15756 tags for them instead. */
15757 dwarf2_add_field (fi, child_die, cu);
15758 }
15759 else if (child_die->tag == DW_TAG_subprogram)
15760 {
15761 /* Rust doesn't have member functions in the C++ sense.
15762 However, it does emit ordinary functions as children
15763 of a struct DIE. */
15764 if (cu->language == language_rust)
15765 read_func_scope (child_die, cu);
15766 else
15767 {
15768 /* C++ member function. */
15769 dwarf2_add_member_fn (fi, child_die, type, cu);
15770 }
15771 }
15772 else if (child_die->tag == DW_TAG_inheritance)
15773 {
15774 /* C++ base class field. */
15775 dwarf2_add_field (fi, child_die, cu);
15776 }
15777 else if (type_can_define_types (child_die))
15778 dwarf2_add_type_defn (fi, child_die, cu);
15779 else if (child_die->tag == DW_TAG_template_type_param
15780 || child_die->tag == DW_TAG_template_value_param)
15781 {
15782 struct symbol *arg = new_symbol (child_die, NULL, cu);
15783
15784 if (arg != NULL)
15785 template_args->push_back (arg);
15786 }
9c6a1327
TT
15787 else if (child_die->tag == DW_TAG_variant_part)
15788 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15789 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15790 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15791}
15792
c767944b
DJ
15793/* Finish creating a structure or union type, including filling in
15794 its members and creating a symbol for it. */
15795
15796static void
15797process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15798{
5e22e966 15799 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15800 struct die_info *child_die;
c767944b
DJ
15801 struct type *type;
15802
15803 type = get_die_type (die, cu);
15804 if (type == NULL)
15805 type = read_structure_type (die, cu);
15806
3e1d3d8c 15807 bool has_template_parameters = false;
e142c38c 15808 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15809 {
15810 struct field_info fi;
2f4732b0 15811 std::vector<struct symbol *> template_args;
c906108c 15812
639d11d3 15813 child_die = die->child;
c906108c
SS
15814
15815 while (child_die && child_die->tag)
15816 {
2ddeaf8a 15817 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15818 child_die = child_die->sibling;
c906108c
SS
15819 }
15820
34eaf542 15821 /* Attach template arguments to type. */
2f4732b0 15822 if (!template_args.empty ())
34eaf542 15823 {
3e1d3d8c 15824 has_template_parameters = true;
34eaf542 15825 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15826 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15827 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15828 = XOBNEWVEC (&objfile->objfile_obstack,
15829 struct symbol *,
15830 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15831 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15832 template_args.data (),
34eaf542
TT
15833 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15834 * sizeof (struct symbol *)));
34eaf542
TT
15835 }
15836
c906108c 15837 /* Attach fields and member functions to the type. */
317f7127 15838 if (fi.nfields () > 0)
e7c27a73 15839 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15840 if (!fi.fnfieldlists.empty ())
c906108c 15841 {
e7c27a73 15842 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15843
c5aa993b 15844 /* Get the type which refers to the base class (possibly this
c906108c 15845 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15846 class from the DW_AT_containing_type attribute. This use of
15847 DW_AT_containing_type is a GNU extension. */
c906108c 15848
e142c38c 15849 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15850 {
e7c27a73 15851 struct type *t = die_containing_type (die, cu);
c906108c 15852
ae6ae975 15853 set_type_vptr_basetype (type, t);
c906108c
SS
15854 if (type == t)
15855 {
c906108c
SS
15856 int i;
15857
15858 /* Our own class provides vtbl ptr. */
1f704f76 15859 for (i = t->num_fields () - 1;
c906108c
SS
15860 i >= TYPE_N_BASECLASSES (t);
15861 --i)
15862 {
0d5cff50 15863 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15864
1168df01 15865 if (is_vtable_name (fieldname, cu))
c906108c 15866 {
ae6ae975 15867 set_type_vptr_fieldno (type, i);
c906108c
SS
15868 break;
15869 }
15870 }
15871
15872 /* Complain if virtual function table field not found. */
15873 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15874 complaint (_("virtual function table pointer "
3e43a32a 15875 "not found when defining class '%s'"),
7d93a1e0 15876 type->name () ? type->name () : "");
c906108c
SS
15877 }
15878 else
15879 {
ae6ae975 15880 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15881 }
15882 }
f6235d4c 15883 else if (cu->producer
61012eef 15884 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15885 {
15886 /* The IBM XLC compiler does not provide direct indication
15887 of the containing type, but the vtable pointer is
15888 always named __vfp. */
15889
15890 int i;
15891
1f704f76 15892 for (i = type->num_fields () - 1;
f6235d4c
EZ
15893 i >= TYPE_N_BASECLASSES (type);
15894 --i)
15895 {
15896 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15897 {
ae6ae975
DE
15898 set_type_vptr_fieldno (type, i);
15899 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15900 break;
15901 }
15902 }
15903 }
c906108c 15904 }
98751a41
JK
15905
15906 /* Copy fi.typedef_field_list linked list elements content into the
15907 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15908 if (!fi.typedef_field_list.empty ())
98751a41 15909 {
be2daae6 15910 int count = fi.typedef_field_list.size ();
98751a41 15911
a0d7a4ff 15912 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15913 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15914 = ((struct decl_field *)
be2daae6
TT
15915 TYPE_ALLOC (type,
15916 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15917 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15918
be2daae6
TT
15919 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15920 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15921 }
c767944b 15922
883fd55a
KS
15923 /* Copy fi.nested_types_list linked list elements content into the
15924 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15925 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15926 {
be2daae6 15927 int count = fi.nested_types_list.size ();
883fd55a
KS
15928
15929 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15930 TYPE_NESTED_TYPES_ARRAY (type)
15931 = ((struct decl_field *)
be2daae6
TT
15932 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15933 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15934
be2daae6
TT
15935 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15936 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15937 }
c906108c 15938 }
63d06c5c 15939
bb5ed363 15940 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15941 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15942 cu->rust_unions.push_back (type);
0b92b5bb 15943
90aeadfc
DC
15944 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15945 snapshots) has been known to create a die giving a declaration
15946 for a class that has, as a child, a die giving a definition for a
15947 nested class. So we have to process our children even if the
15948 current die is a declaration. Normally, of course, a declaration
15949 won't have any children at all. */
134d01f1 15950
ca040673
DE
15951 child_die = die->child;
15952
90aeadfc
DC
15953 while (child_die != NULL && child_die->tag)
15954 {
15955 if (child_die->tag == DW_TAG_member
15956 || child_die->tag == DW_TAG_variable
34eaf542
TT
15957 || child_die->tag == DW_TAG_inheritance
15958 || child_die->tag == DW_TAG_template_value_param
15959 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15960 {
90aeadfc 15961 /* Do nothing. */
134d01f1 15962 }
90aeadfc
DC
15963 else
15964 process_die (child_die, cu);
134d01f1 15965
436c571c 15966 child_die = child_die->sibling;
134d01f1
DJ
15967 }
15968
fa4028e9
JB
15969 /* Do not consider external references. According to the DWARF standard,
15970 these DIEs are identified by the fact that they have no byte_size
15971 attribute, and a declaration attribute. */
15972 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15973 || !die_is_declaration (die, cu)
15974 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15975 {
15976 struct symbol *sym = new_symbol (die, type, cu);
15977
15978 if (has_template_parameters)
15979 {
a776957c
TT
15980 struct symtab *symtab;
15981 if (sym != nullptr)
15982 symtab = symbol_symtab (sym);
15983 else if (cu->line_header != nullptr)
15984 {
15985 /* Any related symtab will do. */
15986 symtab
7ba99d21 15987 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15988 }
15989 else
15990 {
15991 symtab = nullptr;
15992 complaint (_("could not find suitable "
15993 "symtab for template parameter"
15994 " - DIE at %s [in module %s]"),
15995 sect_offset_str (die->sect_off),
15996 objfile_name (objfile));
15997 }
15998
15999 if (symtab != nullptr)
16000 {
16001 /* Make sure that the symtab is set on the new symbols.
16002 Even though they don't appear in this symtab directly,
16003 other parts of gdb assume that symbols do, and this is
16004 reasonably true. */
16005 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16006 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16007 }
3e1d3d8c
TT
16008 }
16009 }
134d01f1
DJ
16010}
16011
ed6acedd
TT
16012/* Assuming DIE is an enumeration type, and TYPE is its associated
16013 type, update TYPE using some information only available in DIE's
16014 children. In particular, the fields are computed. */
55426c9d
JB
16015
16016static void
16017update_enumeration_type_from_children (struct die_info *die,
16018 struct type *type,
16019 struct dwarf2_cu *cu)
16020{
60f7655a 16021 struct die_info *child_die;
55426c9d
JB
16022 int unsigned_enum = 1;
16023 int flag_enum = 1;
55426c9d 16024
8268c778 16025 auto_obstack obstack;
ed6acedd 16026 std::vector<struct field> fields;
55426c9d 16027
60f7655a
DE
16028 for (child_die = die->child;
16029 child_die != NULL && child_die->tag;
436c571c 16030 child_die = child_die->sibling)
55426c9d
JB
16031 {
16032 struct attribute *attr;
16033 LONGEST value;
16034 const gdb_byte *bytes;
16035 struct dwarf2_locexpr_baton *baton;
16036 const char *name;
60f7655a 16037
55426c9d
JB
16038 if (child_die->tag != DW_TAG_enumerator)
16039 continue;
16040
16041 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16042 if (attr == NULL)
16043 continue;
16044
16045 name = dwarf2_name (child_die, cu);
16046 if (name == NULL)
16047 name = "<anonymous enumerator>";
16048
16049 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16050 &value, &bytes, &baton);
16051 if (value < 0)
16052 {
16053 unsigned_enum = 0;
16054 flag_enum = 0;
16055 }
55426c9d 16056 else
edd45eb0
SM
16057 {
16058 if (count_one_bits_ll (value) >= 2)
16059 flag_enum = 0;
edd45eb0 16060 }
55426c9d 16061
ed6acedd
TT
16062 fields.emplace_back ();
16063 struct field &field = fields.back ();
16064 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16065 SET_FIELD_ENUMVAL (field, value);
16066 }
16067
16068 if (!fields.empty ())
16069 {
5e33d5f4 16070 type->set_num_fields (fields.size ());
3cabb6b0
SM
16071 type->set_fields
16072 ((struct field *)
16073 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16074 memcpy (type->fields (), fields.data (),
ed6acedd 16075 sizeof (struct field) * fields.size ());
55426c9d
JB
16076 }
16077
16078 if (unsigned_enum)
16079 TYPE_UNSIGNED (type) = 1;
16080 if (flag_enum)
16081 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16082}
16083
134d01f1
DJ
16084/* Given a DW_AT_enumeration_type die, set its type. We do not
16085 complete the type's fields yet, or create any symbols. */
c906108c 16086
f792889a 16087static struct type *
134d01f1 16088read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16089{
5e22e966 16090 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16091 struct type *type;
c906108c 16092 struct attribute *attr;
0114d602 16093 const char *name;
134d01f1 16094
348e048f
DE
16095 /* If the definition of this type lives in .debug_types, read that type.
16096 Don't follow DW_AT_specification though, that will take us back up
16097 the chain and we want to go down. */
052c8bb8 16098 attr = die->attr (DW_AT_signature);
435d3d88 16099 if (attr != nullptr)
348e048f 16100 {
ac9ec31b 16101 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16102
ac9ec31b 16103 /* The type's CU may not be the same as CU.
02142a6c 16104 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16105 return set_die_type (die, type, cu);
16106 }
16107
c906108c
SS
16108 type = alloc_type (objfile);
16109
67607e24 16110 type->set_code (TYPE_CODE_ENUM);
94af9270 16111 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16112 if (name != NULL)
d0e39ea2 16113 type->set_name (name);
c906108c 16114
0626fc76
TT
16115 attr = dwarf2_attr (die, DW_AT_type, cu);
16116 if (attr != NULL)
16117 {
16118 struct type *underlying_type = die_type (die, cu);
16119
16120 TYPE_TARGET_TYPE (type) = underlying_type;
16121 }
16122
e142c38c 16123 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16124 if (attr != nullptr)
c906108c
SS
16125 {
16126 TYPE_LENGTH (type) = DW_UNSND (attr);
16127 }
16128 else
16129 {
16130 TYPE_LENGTH (type) = 0;
16131 }
16132
2b4424c3
TT
16133 maybe_set_alignment (cu, die, type);
16134
137033e9
JB
16135 /* The enumeration DIE can be incomplete. In Ada, any type can be
16136 declared as private in the package spec, and then defined only
16137 inside the package body. Such types are known as Taft Amendment
16138 Types. When another package uses such a type, an incomplete DIE
16139 may be generated by the compiler. */
02eb380e 16140 if (die_is_declaration (die, cu))
876cecd0 16141 TYPE_STUB (type) = 1;
02eb380e 16142
0626fc76
TT
16143 /* If this type has an underlying type that is not a stub, then we
16144 may use its attributes. We always use the "unsigned" attribute
16145 in this situation, because ordinarily we guess whether the type
16146 is unsigned -- but the guess can be wrong and the underlying type
16147 can tell us the reality. However, we defer to a local size
16148 attribute if one exists, because this lets the compiler override
16149 the underlying type if needed. */
16150 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16151 {
9e7c9a03
HD
16152 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16153 underlying_type = check_typedef (underlying_type);
16154 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16155 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16156 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16157 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16158 && TYPE_RAW_ALIGN (underlying_type) != 0)
16159 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16160 }
16161
3d567982
TT
16162 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16163
ed6acedd
TT
16164 set_die_type (die, type, cu);
16165
16166 /* Finish the creation of this type by using the enum's children.
16167 Note that, as usual, this must come after set_die_type to avoid
16168 infinite recursion when trying to compute the names of the
16169 enumerators. */
16170 update_enumeration_type_from_children (die, type, cu);
16171
16172 return type;
134d01f1
DJ
16173}
16174
16175/* Given a pointer to a die which begins an enumeration, process all
16176 the dies that define the members of the enumeration, and create the
16177 symbol for the enumeration type.
16178
16179 NOTE: We reverse the order of the element list. */
16180
16181static void
16182process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16183{
f792889a 16184 struct type *this_type;
134d01f1 16185
f792889a
DJ
16186 this_type = get_die_type (die, cu);
16187 if (this_type == NULL)
16188 this_type = read_enumeration_type (die, cu);
9dc481d3 16189
639d11d3 16190 if (die->child != NULL)
c906108c 16191 {
9dc481d3 16192 struct die_info *child_die;
15d034d0 16193 const char *name;
9dc481d3 16194
639d11d3 16195 child_die = die->child;
c906108c
SS
16196 while (child_die && child_die->tag)
16197 {
16198 if (child_die->tag != DW_TAG_enumerator)
16199 {
e7c27a73 16200 process_die (child_die, cu);
c906108c
SS
16201 }
16202 else
16203 {
39cbfefa
DJ
16204 name = dwarf2_name (child_die, cu);
16205 if (name)
ed6acedd 16206 new_symbol (child_die, this_type, cu);
c906108c
SS
16207 }
16208
436c571c 16209 child_die = child_die->sibling;
c906108c 16210 }
c906108c 16211 }
134d01f1 16212
6c83ed52
TT
16213 /* If we are reading an enum from a .debug_types unit, and the enum
16214 is a declaration, and the enum is not the signatured type in the
16215 unit, then we do not want to add a symbol for it. Adding a
16216 symbol would in some cases obscure the true definition of the
16217 enum, giving users an incomplete type when the definition is
16218 actually available. Note that we do not want to do this for all
16219 enums which are just declarations, because C++0x allows forward
16220 enum declarations. */
3019eac3 16221 if (cu->per_cu->is_debug_types
6c83ed52
TT
16222 && die_is_declaration (die, cu))
16223 {
52dc124a 16224 struct signatured_type *sig_type;
6c83ed52 16225
c0f78cd4 16226 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16227 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16228 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16229 return;
16230 }
16231
f792889a 16232 new_symbol (die, this_type, cu);
c906108c
SS
16233}
16234
16235/* Extract all information from a DW_TAG_array_type DIE and put it in
16236 the DIE's type field. For now, this only handles one dimensional
16237 arrays. */
16238
f792889a 16239static struct type *
e7c27a73 16240read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16241{
5e22e966 16242 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16243 struct die_info *child_die;
7e314c57 16244 struct type *type;
c906108c 16245 struct type *element_type, *range_type, *index_type;
c906108c 16246 struct attribute *attr;
15d034d0 16247 const char *name;
a405673c 16248 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16249 unsigned int bit_stride = 0;
c906108c 16250
e7c27a73 16251 element_type = die_type (die, cu);
c906108c 16252
7e314c57
JK
16253 /* The die_type call above may have already set the type for this DIE. */
16254 type = get_die_type (die, cu);
16255 if (type)
16256 return type;
16257
dc53a7ad
JB
16258 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16259 if (attr != NULL)
a405673c
JB
16260 {
16261 int stride_ok;
293e7e51 16262 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16263
16264 byte_stride_prop
16265 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16266 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16267 prop_type);
a405673c
JB
16268 if (!stride_ok)
16269 {
b98664d3 16270 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16271 " - DIE at %s [in module %s]"),
16272 sect_offset_str (die->sect_off),
5e22e966 16273 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16274 /* Ignore this attribute. We will likely not be able to print
16275 arrays of this type correctly, but there is little we can do
16276 to help if we cannot read the attribute's value. */
16277 byte_stride_prop = NULL;
16278 }
16279 }
dc53a7ad
JB
16280
16281 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16282 if (attr != NULL)
16283 bit_stride = DW_UNSND (attr);
16284
c906108c
SS
16285 /* Irix 6.2 native cc creates array types without children for
16286 arrays with unspecified length. */
639d11d3 16287 if (die->child == NULL)
c906108c 16288 {
46bf5051 16289 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16290 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16291 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16292 byte_stride_prop, bit_stride);
f792889a 16293 return set_die_type (die, type, cu);
c906108c
SS
16294 }
16295
791afaa2 16296 std::vector<struct type *> range_types;
639d11d3 16297 child_die = die->child;
c906108c
SS
16298 while (child_die && child_die->tag)
16299 {
16300 if (child_die->tag == DW_TAG_subrange_type)
16301 {
f792889a 16302 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16303
f792889a 16304 if (child_type != NULL)
a02abb62 16305 {
0963b4bd
MS
16306 /* The range type was succesfully read. Save it for the
16307 array type creation. */
791afaa2 16308 range_types.push_back (child_type);
a02abb62 16309 }
c906108c 16310 }
436c571c 16311 child_die = child_die->sibling;
c906108c
SS
16312 }
16313
16314 /* Dwarf2 dimensions are output from left to right, create the
16315 necessary array types in backwards order. */
7ca2d3a3 16316
c906108c 16317 type = element_type;
7ca2d3a3
DL
16318
16319 if (read_array_order (die, cu) == DW_ORD_col_major)
16320 {
16321 int i = 0;
9a619af0 16322
791afaa2 16323 while (i < range_types.size ())
dc53a7ad 16324 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16325 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16326 }
16327 else
16328 {
791afaa2 16329 size_t ndim = range_types.size ();
7ca2d3a3 16330 while (ndim-- > 0)
dc53a7ad 16331 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16332 byte_stride_prop, bit_stride);
7ca2d3a3 16333 }
c906108c 16334
f5f8a009
EZ
16335 /* Understand Dwarf2 support for vector types (like they occur on
16336 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16337 array type. This is not part of the Dwarf2/3 standard yet, but a
16338 custom vendor extension. The main difference between a regular
16339 array and the vector variant is that vectors are passed by value
16340 to functions. */
e142c38c 16341 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16342 if (attr != nullptr)
ea37ba09 16343 make_vector_type (type);
f5f8a009 16344
dbc98a8b
KW
16345 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16346 implementation may choose to implement triple vectors using this
16347 attribute. */
16348 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16349 if (attr != nullptr)
dbc98a8b
KW
16350 {
16351 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16352 TYPE_LENGTH (type) = DW_UNSND (attr);
16353 else
b98664d3 16354 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16355 "than the total size of elements"));
dbc98a8b
KW
16356 }
16357
39cbfefa
DJ
16358 name = dwarf2_name (die, cu);
16359 if (name)
d0e39ea2 16360 type->set_name (name);
6e70227d 16361
2b4424c3
TT
16362 maybe_set_alignment (cu, die, type);
16363
0963b4bd 16364 /* Install the type in the die. */
7e314c57
JK
16365 set_die_type (die, type, cu);
16366
16367 /* set_die_type should be already done. */
b4ba55a1
JB
16368 set_descriptive_type (type, die, cu);
16369
7e314c57 16370 return type;
c906108c
SS
16371}
16372
7ca2d3a3 16373static enum dwarf_array_dim_ordering
6e70227d 16374read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16375{
16376 struct attribute *attr;
16377
16378 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16379
435d3d88 16380 if (attr != nullptr)
aead7601 16381 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16382
0963b4bd
MS
16383 /* GNU F77 is a special case, as at 08/2004 array type info is the
16384 opposite order to the dwarf2 specification, but data is still
16385 laid out as per normal fortran.
7ca2d3a3 16386
0963b4bd
MS
16387 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16388 version checking. */
7ca2d3a3 16389
905e0470
PM
16390 if (cu->language == language_fortran
16391 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16392 {
16393 return DW_ORD_row_major;
16394 }
16395
6e70227d 16396 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16397 {
16398 case array_column_major:
16399 return DW_ORD_col_major;
16400 case array_row_major:
16401 default:
16402 return DW_ORD_row_major;
16403 };
16404}
16405
72019c9c 16406/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16407 the DIE's type field. */
72019c9c 16408
f792889a 16409static struct type *
72019c9c
GM
16410read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16411{
7e314c57
JK
16412 struct type *domain_type, *set_type;
16413 struct attribute *attr;
f792889a 16414
7e314c57
JK
16415 domain_type = die_type (die, cu);
16416
16417 /* The die_type call above may have already set the type for this DIE. */
16418 set_type = get_die_type (die, cu);
16419 if (set_type)
16420 return set_type;
16421
16422 set_type = create_set_type (NULL, domain_type);
16423
16424 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16425 if (attr != nullptr)
d09039dd 16426 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16427
2b4424c3
TT
16428 maybe_set_alignment (cu, die, set_type);
16429
f792889a 16430 return set_die_type (die, set_type, cu);
72019c9c 16431}
7ca2d3a3 16432
0971de02
TT
16433/* A helper for read_common_block that creates a locexpr baton.
16434 SYM is the symbol which we are marking as computed.
16435 COMMON_DIE is the DIE for the common block.
16436 COMMON_LOC is the location expression attribute for the common
16437 block itself.
16438 MEMBER_LOC is the location expression attribute for the particular
16439 member of the common block that we are processing.
16440 CU is the CU from which the above come. */
16441
16442static void
16443mark_common_block_symbol_computed (struct symbol *sym,
16444 struct die_info *common_die,
16445 struct attribute *common_loc,
16446 struct attribute *member_loc,
16447 struct dwarf2_cu *cu)
16448{
5e22e966 16449 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16450 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16451 struct dwarf2_locexpr_baton *baton;
16452 gdb_byte *ptr;
16453 unsigned int cu_off;
08feed99 16454 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16455 LONGEST offset = 0;
16456
16457 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16458 gdb_assert (common_loc->form_is_block ());
16459 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16460 || member_loc->form_is_constant ());
0971de02 16461
8d749320 16462 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16463 baton->per_objfile = per_objfile;
0971de02
TT
16464 baton->per_cu = cu->per_cu;
16465 gdb_assert (baton->per_cu);
16466
16467 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16468
cd6c91b4 16469 if (member_loc->form_is_constant ())
0971de02 16470 {
0826b30a 16471 offset = member_loc->constant_value (0);
0971de02
TT
16472 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16473 }
16474 else
16475 baton->size += DW_BLOCK (member_loc)->size;
16476
224c3ddb 16477 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16478 baton->data = ptr;
16479
16480 *ptr++ = DW_OP_call4;
9c541725 16481 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16482 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16483 ptr += 4;
16484
cd6c91b4 16485 if (member_loc->form_is_constant ())
0971de02
TT
16486 {
16487 *ptr++ = DW_OP_addr;
16488 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16489 ptr += cu->header.addr_size;
16490 }
16491 else
16492 {
16493 /* We have to copy the data here, because DW_OP_call4 will only
16494 use a DW_AT_location attribute. */
16495 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16496 ptr += DW_BLOCK (member_loc)->size;
16497 }
16498
16499 *ptr++ = DW_OP_plus;
16500 gdb_assert (ptr - baton->data == baton->size);
16501
0971de02 16502 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16503 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16504}
16505
4357ac6c
TT
16506/* Create appropriate locally-scoped variables for all the
16507 DW_TAG_common_block entries. Also create a struct common_block
16508 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16509 is used to separate the common blocks name namespace from regular
4357ac6c 16510 variable names. */
c906108c
SS
16511
16512static void
e7c27a73 16513read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16514{
0971de02
TT
16515 struct attribute *attr;
16516
16517 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16518 if (attr != nullptr)
0971de02
TT
16519 {
16520 /* Support the .debug_loc offsets. */
4fc6c0d5 16521 if (attr->form_is_block ())
0971de02
TT
16522 {
16523 /* Ok. */
16524 }
cd6c91b4 16525 else if (attr->form_is_section_offset ())
0971de02
TT
16526 {
16527 dwarf2_complex_location_expr_complaint ();
16528 attr = NULL;
16529 }
16530 else
16531 {
16532 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16533 "common block member");
16534 attr = NULL;
16535 }
16536 }
16537
639d11d3 16538 if (die->child != NULL)
c906108c 16539 {
5e22e966 16540 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16541 struct die_info *child_die;
16542 size_t n_entries = 0, size;
16543 struct common_block *common_block;
16544 struct symbol *sym;
74ac6d43 16545
4357ac6c
TT
16546 for (child_die = die->child;
16547 child_die && child_die->tag;
436c571c 16548 child_die = child_die->sibling)
4357ac6c
TT
16549 ++n_entries;
16550
16551 size = (sizeof (struct common_block)
16552 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16553 common_block
16554 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16555 size);
4357ac6c
TT
16556 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16557 common_block->n_entries = 0;
16558
16559 for (child_die = die->child;
16560 child_die && child_die->tag;
436c571c 16561 child_die = child_die->sibling)
4357ac6c
TT
16562 {
16563 /* Create the symbol in the DW_TAG_common_block block in the current
16564 symbol scope. */
e7c27a73 16565 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16566 if (sym != NULL)
16567 {
16568 struct attribute *member_loc;
16569
16570 common_block->contents[common_block->n_entries++] = sym;
16571
16572 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16573 cu);
16574 if (member_loc)
16575 {
16576 /* GDB has handled this for a long time, but it is
16577 not specified by DWARF. It seems to have been
16578 emitted by gfortran at least as recently as:
16579 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16580 complaint (_("Variable in common block has "
0971de02 16581 "DW_AT_data_member_location "
9d8780f0
SM
16582 "- DIE at %s [in module %s]"),
16583 sect_offset_str (child_die->sect_off),
518817b3 16584 objfile_name (objfile));
0971de02 16585
cd6c91b4 16586 if (member_loc->form_is_section_offset ())
0971de02 16587 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16588 else if (member_loc->form_is_constant ()
4fc6c0d5 16589 || member_loc->form_is_block ())
0971de02 16590 {
435d3d88 16591 if (attr != nullptr)
0971de02
TT
16592 mark_common_block_symbol_computed (sym, die, attr,
16593 member_loc, cu);
16594 }
16595 else
16596 dwarf2_complex_location_expr_complaint ();
16597 }
16598 }
c906108c 16599 }
4357ac6c
TT
16600
16601 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16602 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16603 }
16604}
16605
0114d602 16606/* Create a type for a C++ namespace. */
d9fa45fe 16607
0114d602
DJ
16608static struct type *
16609read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16610{
5e22e966 16611 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16612 const char *previous_prefix, *name;
9219021c 16613 int is_anonymous;
0114d602
DJ
16614 struct type *type;
16615
16616 /* For extensions, reuse the type of the original namespace. */
16617 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16618 {
16619 struct die_info *ext_die;
16620 struct dwarf2_cu *ext_cu = cu;
9a619af0 16621
0114d602
DJ
16622 ext_die = dwarf2_extension (die, &ext_cu);
16623 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16624
16625 /* EXT_CU may not be the same as CU.
02142a6c 16626 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16627 return set_die_type (die, type, cu);
16628 }
9219021c 16629
e142c38c 16630 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16631
16632 /* Now build the name of the current namespace. */
16633
0114d602
DJ
16634 previous_prefix = determine_prefix (die, cu);
16635 if (previous_prefix[0] != '\0')
16636 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16637 previous_prefix, name, 0, cu);
0114d602
DJ
16638
16639 /* Create the type. */
19f392bc 16640 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16641
60531b24 16642 return set_die_type (die, type, cu);
0114d602
DJ
16643}
16644
22cee43f 16645/* Read a namespace scope. */
0114d602
DJ
16646
16647static void
16648read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16649{
5e22e966 16650 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16651 int is_anonymous;
9219021c 16652
5c4e30ca
DC
16653 /* Add a symbol associated to this if we haven't seen the namespace
16654 before. Also, add a using directive if it's an anonymous
16655 namespace. */
9219021c 16656
f2f0e013 16657 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16658 {
16659 struct type *type;
16660
0114d602 16661 type = read_type_die (die, cu);
e7c27a73 16662 new_symbol (die, type, cu);
5c4e30ca 16663
e8e80198 16664 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16665 if (is_anonymous)
0114d602
DJ
16666 {
16667 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16668
eb1e02fd 16669 std::vector<const char *> excludes;
804d2729 16670 add_using_directive (using_directives (cu),
7d93a1e0 16671 previous_prefix, type->name (), NULL,
eb1e02fd 16672 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16673 }
5c4e30ca 16674 }
9219021c 16675
639d11d3 16676 if (die->child != NULL)
d9fa45fe 16677 {
639d11d3 16678 struct die_info *child_die = die->child;
6e70227d 16679
d9fa45fe
DC
16680 while (child_die && child_die->tag)
16681 {
e7c27a73 16682 process_die (child_die, cu);
436c571c 16683 child_die = child_die->sibling;
d9fa45fe
DC
16684 }
16685 }
38d518c9
EZ
16686}
16687
f55ee35c
JK
16688/* Read a Fortran module as type. This DIE can be only a declaration used for
16689 imported module. Still we need that type as local Fortran "use ... only"
16690 declaration imports depend on the created type in determine_prefix. */
16691
16692static struct type *
16693read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16694{
5e22e966 16695 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16696 const char *module_name;
f55ee35c
JK
16697 struct type *type;
16698
16699 module_name = dwarf2_name (die, cu);
19f392bc 16700 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16701
f55ee35c
JK
16702 return set_die_type (die, type, cu);
16703}
16704
5d7cb8df
JK
16705/* Read a Fortran module. */
16706
16707static void
16708read_module (struct die_info *die, struct dwarf2_cu *cu)
16709{
16710 struct die_info *child_die = die->child;
530e8392
KB
16711 struct type *type;
16712
16713 type = read_type_die (die, cu);
16714 new_symbol (die, type, cu);
5d7cb8df 16715
5d7cb8df
JK
16716 while (child_die && child_die->tag)
16717 {
16718 process_die (child_die, cu);
436c571c 16719 child_die = child_die->sibling;
5d7cb8df
JK
16720 }
16721}
16722
38d518c9
EZ
16723/* Return the name of the namespace represented by DIE. Set
16724 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16725 namespace. */
16726
16727static const char *
e142c38c 16728namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16729{
16730 struct die_info *current_die;
16731 const char *name = NULL;
16732
16733 /* Loop through the extensions until we find a name. */
16734
16735 for (current_die = die;
16736 current_die != NULL;
f2f0e013 16737 current_die = dwarf2_extension (die, &cu))
38d518c9 16738 {
96553a0c
DE
16739 /* We don't use dwarf2_name here so that we can detect the absence
16740 of a name -> anonymous namespace. */
7d45c7c3 16741 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16742
38d518c9
EZ
16743 if (name != NULL)
16744 break;
16745 }
16746
16747 /* Is it an anonymous namespace? */
16748
16749 *is_anonymous = (name == NULL);
16750 if (*is_anonymous)
2b1dbab0 16751 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16752
16753 return name;
d9fa45fe
DC
16754}
16755
c906108c
SS
16756/* Extract all information from a DW_TAG_pointer_type DIE and add to
16757 the user defined type vector. */
16758
f792889a 16759static struct type *
e7c27a73 16760read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16761{
5e22e966 16762 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16763 struct comp_unit_head *cu_header = &cu->header;
c906108c 16764 struct type *type;
8b2dbe47
KB
16765 struct attribute *attr_byte_size;
16766 struct attribute *attr_address_class;
16767 int byte_size, addr_class;
7e314c57
JK
16768 struct type *target_type;
16769
16770 target_type = die_type (die, cu);
c906108c 16771
7e314c57
JK
16772 /* The die_type call above may have already set the type for this DIE. */
16773 type = get_die_type (die, cu);
16774 if (type)
16775 return type;
16776
16777 type = lookup_pointer_type (target_type);
8b2dbe47 16778
e142c38c 16779 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16780 if (attr_byte_size)
16781 byte_size = DW_UNSND (attr_byte_size);
c906108c 16782 else
8b2dbe47
KB
16783 byte_size = cu_header->addr_size;
16784
e142c38c 16785 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16786 if (attr_address_class)
16787 addr_class = DW_UNSND (attr_address_class);
16788 else
16789 addr_class = DW_ADDR_none;
16790
2b4424c3
TT
16791 ULONGEST alignment = get_alignment (cu, die);
16792
16793 /* If the pointer size, alignment, or address class is different
16794 than the default, create a type variant marked as such and set
16795 the length accordingly. */
16796 if (TYPE_LENGTH (type) != byte_size
16797 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16798 && alignment != TYPE_RAW_ALIGN (type))
16799 || addr_class != DW_ADDR_none)
c906108c 16800 {
5e2b427d 16801 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16802 {
16803 int type_flags;
16804
849957d9 16805 type_flags = gdbarch_address_class_type_flags
5e2b427d 16806 (gdbarch, byte_size, addr_class);
876cecd0
TT
16807 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16808 == 0);
8b2dbe47
KB
16809 type = make_type_with_address_space (type, type_flags);
16810 }
16811 else if (TYPE_LENGTH (type) != byte_size)
16812 {
b98664d3 16813 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16814 }
2b4424c3
TT
16815 else if (TYPE_RAW_ALIGN (type) != alignment)
16816 {
b98664d3 16817 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16818 " - DIE at %s [in module %s]"),
16819 sect_offset_str (die->sect_off),
5e22e966 16820 objfile_name (cu->per_objfile->objfile));
2b4424c3 16821 }
6e70227d 16822 else
9a619af0
MS
16823 {
16824 /* Should we also complain about unhandled address classes? */
16825 }
c906108c 16826 }
8b2dbe47
KB
16827
16828 TYPE_LENGTH (type) = byte_size;
2b4424c3 16829 set_type_align (type, alignment);
f792889a 16830 return set_die_type (die, type, cu);
c906108c
SS
16831}
16832
16833/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16834 the user defined type vector. */
16835
f792889a 16836static struct type *
e7c27a73 16837read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16838{
16839 struct type *type;
16840 struct type *to_type;
16841 struct type *domain;
16842
e7c27a73
DJ
16843 to_type = die_type (die, cu);
16844 domain = die_containing_type (die, cu);
0d5de010 16845
7e314c57
JK
16846 /* The calls above may have already set the type for this DIE. */
16847 type = get_die_type (die, cu);
16848 if (type)
16849 return type;
16850
78134374 16851 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16852 type = lookup_methodptr_type (to_type);
78134374 16853 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16854 {
5e22e966 16855 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16856
16857 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16858 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16859 TYPE_VARARGS (to_type));
16860 type = lookup_methodptr_type (new_type);
16861 }
0d5de010
DJ
16862 else
16863 type = lookup_memberptr_type (to_type, domain);
c906108c 16864
f792889a 16865 return set_die_type (die, type, cu);
c906108c
SS
16866}
16867
4297a3f0 16868/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16869 the user defined type vector. */
16870
f792889a 16871static struct type *
4297a3f0
AV
16872read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16873 enum type_code refcode)
c906108c 16874{
e7c27a73 16875 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16876 struct type *type, *target_type;
c906108c
SS
16877 struct attribute *attr;
16878
4297a3f0
AV
16879 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16880
7e314c57
JK
16881 target_type = die_type (die, cu);
16882
16883 /* The die_type call above may have already set the type for this DIE. */
16884 type = get_die_type (die, cu);
16885 if (type)
16886 return type;
16887
4297a3f0 16888 type = lookup_reference_type (target_type, refcode);
e142c38c 16889 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16890 if (attr != nullptr)
c906108c
SS
16891 {
16892 TYPE_LENGTH (type) = DW_UNSND (attr);
16893 }
16894 else
16895 {
107d2387 16896 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16897 }
2b4424c3 16898 maybe_set_alignment (cu, die, type);
f792889a 16899 return set_die_type (die, type, cu);
c906108c
SS
16900}
16901
cf363f18
MW
16902/* Add the given cv-qualifiers to the element type of the array. GCC
16903 outputs DWARF type qualifiers that apply to an array, not the
16904 element type. But GDB relies on the array element type to carry
16905 the cv-qualifiers. This mimics section 6.7.3 of the C99
16906 specification. */
16907
16908static struct type *
16909add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16910 struct type *base_type, int cnst, int voltl)
16911{
16912 struct type *el_type, *inner_array;
16913
16914 base_type = copy_type (base_type);
16915 inner_array = base_type;
16916
78134374 16917 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16918 {
16919 TYPE_TARGET_TYPE (inner_array) =
16920 copy_type (TYPE_TARGET_TYPE (inner_array));
16921 inner_array = TYPE_TARGET_TYPE (inner_array);
16922 }
16923
16924 el_type = TYPE_TARGET_TYPE (inner_array);
16925 cnst |= TYPE_CONST (el_type);
16926 voltl |= TYPE_VOLATILE (el_type);
16927 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16928
16929 return set_die_type (die, base_type, cu);
16930}
16931
f792889a 16932static struct type *
e7c27a73 16933read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16934{
f792889a 16935 struct type *base_type, *cv_type;
c906108c 16936
e7c27a73 16937 base_type = die_type (die, cu);
7e314c57
JK
16938
16939 /* The die_type call above may have already set the type for this DIE. */
16940 cv_type = get_die_type (die, cu);
16941 if (cv_type)
16942 return cv_type;
16943
2f608a3a
KW
16944 /* In case the const qualifier is applied to an array type, the element type
16945 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16946 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16947 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16948
f792889a
DJ
16949 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16950 return set_die_type (die, cv_type, cu);
c906108c
SS
16951}
16952
f792889a 16953static struct type *
e7c27a73 16954read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16955{
f792889a 16956 struct type *base_type, *cv_type;
c906108c 16957
e7c27a73 16958 base_type = die_type (die, cu);
7e314c57
JK
16959
16960 /* The die_type call above may have already set the type for this DIE. */
16961 cv_type = get_die_type (die, cu);
16962 if (cv_type)
16963 return cv_type;
16964
cf363f18
MW
16965 /* In case the volatile qualifier is applied to an array type, the
16966 element type is so qualified, not the array type (section 6.7.3
16967 of C99). */
78134374 16968 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16969 return add_array_cv_type (die, cu, base_type, 0, 1);
16970
f792889a
DJ
16971 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16972 return set_die_type (die, cv_type, cu);
c906108c
SS
16973}
16974
06d66ee9
TT
16975/* Handle DW_TAG_restrict_type. */
16976
16977static struct type *
16978read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16979{
16980 struct type *base_type, *cv_type;
16981
16982 base_type = die_type (die, cu);
16983
16984 /* The die_type call above may have already set the type for this DIE. */
16985 cv_type = get_die_type (die, cu);
16986 if (cv_type)
16987 return cv_type;
16988
16989 cv_type = make_restrict_type (base_type);
16990 return set_die_type (die, cv_type, cu);
16991}
16992
a2c2acaf
MW
16993/* Handle DW_TAG_atomic_type. */
16994
16995static struct type *
16996read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16997{
16998 struct type *base_type, *cv_type;
16999
17000 base_type = die_type (die, cu);
17001
17002 /* The die_type call above may have already set the type for this DIE. */
17003 cv_type = get_die_type (die, cu);
17004 if (cv_type)
17005 return cv_type;
17006
17007 cv_type = make_atomic_type (base_type);
17008 return set_die_type (die, cv_type, cu);
17009}
17010
c906108c
SS
17011/* Extract all information from a DW_TAG_string_type DIE and add to
17012 the user defined type vector. It isn't really a user defined type,
17013 but it behaves like one, with other DIE's using an AT_user_def_type
17014 attribute to reference it. */
17015
f792889a 17016static struct type *
e7c27a73 17017read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17018{
5e22e966 17019 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17020 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17021 struct type *type, *range_type, *index_type, *char_type;
17022 struct attribute *attr;
216a7e6b
AB
17023 struct dynamic_prop prop;
17024 bool length_is_constant = true;
17025 LONGEST length;
17026
17027 /* There are a couple of places where bit sizes might be made use of
17028 when parsing a DW_TAG_string_type, however, no producer that we know
17029 of make use of these. Handling bit sizes that are a multiple of the
17030 byte size is easy enough, but what about other bit sizes? Lets deal
17031 with that problem when we have to. Warn about these attributes being
17032 unsupported, then parse the type and ignore them like we always
17033 have. */
17034 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17035 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17036 {
17037 static bool warning_printed = false;
17038 if (!warning_printed)
17039 {
17040 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17041 "currently supported on DW_TAG_string_type."));
17042 warning_printed = true;
17043 }
17044 }
c906108c 17045
e142c38c 17046 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17047 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17048 {
17049 /* The string length describes the location at which the length of
17050 the string can be found. The size of the length field can be
17051 specified with one of the attributes below. */
17052 struct type *prop_type;
17053 struct attribute *len
17054 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17055 if (len == nullptr)
17056 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17057 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17058 {
17059 /* Pass 0 as the default as we know this attribute is constant
17060 and the default value will not be returned. */
0826b30a 17061 LONGEST sz = len->constant_value (0);
293e7e51 17062 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17063 }
17064 else
17065 {
17066 /* If the size is not specified then we assume it is the size of
17067 an address on this target. */
293e7e51 17068 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17069 }
17070
17071 /* Convert the attribute into a dynamic property. */
17072 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17073 length = 1;
17074 else
17075 length_is_constant = false;
17076 }
17077 else if (attr != nullptr)
17078 {
17079 /* This DW_AT_string_length just contains the length with no
17080 indirection. There's no need to create a dynamic property in this
17081 case. Pass 0 for the default value as we know it will not be
17082 returned in this case. */
0826b30a 17083 length = attr->constant_value (0);
216a7e6b
AB
17084 }
17085 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17086 {
216a7e6b 17087 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17088 length = attr->constant_value (1);
c906108c
SS
17089 }
17090 else
17091 {
216a7e6b
AB
17092 /* Use 1 as a fallback length if we have nothing else. */
17093 length = 1;
c906108c 17094 }
6ccb9162 17095
46bf5051 17096 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17097 if (length_is_constant)
17098 range_type = create_static_range_type (NULL, index_type, 1, length);
17099 else
17100 {
17101 struct dynamic_prop low_bound;
17102
17103 low_bound.kind = PROP_CONST;
17104 low_bound.data.const_val = 1;
17105 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17106 }
3b7538c0
UW
17107 char_type = language_string_char_type (cu->language_defn, gdbarch);
17108 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17109
f792889a 17110 return set_die_type (die, type, cu);
c906108c
SS
17111}
17112
4d804846
JB
17113/* Assuming that DIE corresponds to a function, returns nonzero
17114 if the function is prototyped. */
17115
17116static int
17117prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17118{
17119 struct attribute *attr;
17120
17121 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17122 if (attr && (DW_UNSND (attr) != 0))
17123 return 1;
17124
17125 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17126 is only meaningful for C, but the concept also extends to other
4d804846
JB
17127 languages that allow unprototyped functions (Eg: Objective C).
17128 For all other languages, assume that functions are always
17129 prototyped. */
17130 if (cu->language != language_c
17131 && cu->language != language_objc
17132 && cu->language != language_opencl)
17133 return 1;
17134
17135 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17136 prototyped and unprototyped functions; default to prototyped,
17137 since that is more common in modern code (and RealView warns
17138 about unprototyped functions). */
17139 if (producer_is_realview (cu->producer))
17140 return 1;
17141
17142 return 0;
17143}
17144
c906108c
SS
17145/* Handle DIES due to C code like:
17146
17147 struct foo
c5aa993b
JM
17148 {
17149 int (*funcp)(int a, long l);
17150 int b;
17151 };
c906108c 17152
0963b4bd 17153 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17154
f792889a 17155static struct type *
e7c27a73 17156read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17157{
5e22e966 17158 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17159 struct type *type; /* Type that this function returns. */
17160 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17161 struct attribute *attr;
17162
e7c27a73 17163 type = die_type (die, cu);
7e314c57
JK
17164
17165 /* The die_type call above may have already set the type for this DIE. */
17166 ftype = get_die_type (die, cu);
17167 if (ftype)
17168 return ftype;
17169
0c8b41f1 17170 ftype = lookup_function_type (type);
c906108c 17171
4d804846 17172 if (prototyped_function_p (die, cu))
a6c727b2 17173 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17174
c055b101
CV
17175 /* Store the calling convention in the type if it's available in
17176 the subroutine die. Otherwise set the calling convention to
17177 the default value DW_CC_normal. */
17178 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17179 if (attr != nullptr
17180 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17181 TYPE_CALLING_CONVENTION (ftype)
17182 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17183 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17184 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17185 else
17186 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17187
743649fd
MW
17188 /* Record whether the function returns normally to its caller or not
17189 if the DWARF producer set that information. */
17190 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17191 if (attr && (DW_UNSND (attr) != 0))
17192 TYPE_NO_RETURN (ftype) = 1;
17193
76c10ea2
GM
17194 /* We need to add the subroutine type to the die immediately so
17195 we don't infinitely recurse when dealing with parameters
0963b4bd 17196 declared as the same subroutine type. */
76c10ea2 17197 set_die_type (die, ftype, cu);
6e70227d 17198
639d11d3 17199 if (die->child != NULL)
c906108c 17200 {
bb5ed363 17201 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17202 struct die_info *child_die;
8072405b 17203 int nparams, iparams;
c906108c
SS
17204
17205 /* Count the number of parameters.
17206 FIXME: GDB currently ignores vararg functions, but knows about
17207 vararg member functions. */
8072405b 17208 nparams = 0;
639d11d3 17209 child_die = die->child;
c906108c
SS
17210 while (child_die && child_die->tag)
17211 {
17212 if (child_die->tag == DW_TAG_formal_parameter)
17213 nparams++;
17214 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17215 TYPE_VARARGS (ftype) = 1;
436c571c 17216 child_die = child_die->sibling;
c906108c
SS
17217 }
17218
17219 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17220 ftype->set_num_fields (nparams);
3cabb6b0
SM
17221 ftype->set_fields
17222 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17223
8072405b
JK
17224 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17225 even if we error out during the parameters reading below. */
17226 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17227 ftype->field (iparams).set_type (void_type);
8072405b
JK
17228
17229 iparams = 0;
639d11d3 17230 child_die = die->child;
c906108c
SS
17231 while (child_die && child_die->tag)
17232 {
17233 if (child_die->tag == DW_TAG_formal_parameter)
17234 {
3ce3b1ba
PA
17235 struct type *arg_type;
17236
17237 /* DWARF version 2 has no clean way to discern C++
17238 static and non-static member functions. G++ helps
17239 GDB by marking the first parameter for non-static
17240 member functions (which is the this pointer) as
17241 artificial. We pass this information to
17242 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17243
17244 DWARF version 3 added DW_AT_object_pointer, which GCC
17245 4.5 does not yet generate. */
e142c38c 17246 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17247 if (attr != nullptr)
c906108c
SS
17248 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17249 else
9c37b5ae 17250 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17251 arg_type = die_type (child_die, cu);
17252
17253 /* RealView does not mark THIS as const, which the testsuite
17254 expects. GCC marks THIS as const in method definitions,
17255 but not in the class specifications (GCC PR 43053). */
17256 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17257 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17258 {
17259 int is_this = 0;
17260 struct dwarf2_cu *arg_cu = cu;
17261 const char *name = dwarf2_name (child_die, cu);
17262
17263 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17264 if (attr != nullptr)
3ce3b1ba
PA
17265 {
17266 /* If the compiler emits this, use it. */
17267 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17268 is_this = 1;
17269 }
17270 else if (name && strcmp (name, "this") == 0)
17271 /* Function definitions will have the argument names. */
17272 is_this = 1;
17273 else if (name == NULL && iparams == 0)
17274 /* Declarations may not have the names, so like
17275 elsewhere in GDB, assume an artificial first
17276 argument is "this". */
17277 is_this = 1;
17278
17279 if (is_this)
17280 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17281 arg_type, 0);
17282 }
17283
5d14b6e5 17284 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17285 iparams++;
17286 }
436c571c 17287 child_die = child_die->sibling;
c906108c
SS
17288 }
17289 }
17290
76c10ea2 17291 return ftype;
c906108c
SS
17292}
17293
f792889a 17294static struct type *
e7c27a73 17295read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17296{
5e22e966 17297 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17298 const char *name = NULL;
3c8e0968 17299 struct type *this_type, *target_type;
c906108c 17300
94af9270 17301 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17302 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17303 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17304 set_die_type (die, this_type, cu);
3c8e0968
DE
17305 target_type = die_type (die, cu);
17306 if (target_type != this_type)
17307 TYPE_TARGET_TYPE (this_type) = target_type;
17308 else
17309 {
17310 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17311 spec and cause infinite loops in GDB. */
b98664d3 17312 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17313 "- DIE at %s [in module %s]"),
17314 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17315 TYPE_TARGET_TYPE (this_type) = NULL;
17316 }
e4003a34
TV
17317 if (name == NULL)
17318 {
17319 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17320 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17321 Handle these by just returning the target type, rather than
17322 constructing an anonymous typedef type and trying to handle this
17323 elsewhere. */
17324 set_die_type (die, target_type, cu);
17325 return target_type;
17326 }
f792889a 17327 return this_type;
c906108c
SS
17328}
17329
9b790ce7
UW
17330/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17331 (which may be different from NAME) to the architecture back-end to allow
17332 it to guess the correct format if necessary. */
17333
17334static struct type *
17335dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17336 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17337{
08feed99 17338 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17339 const struct floatformat **format;
17340 struct type *type;
17341
17342 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17343 if (format)
103a685e 17344 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17345 else
77b7c781 17346 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17347
17348 return type;
17349}
17350
eb77c9df
AB
17351/* Allocate an integer type of size BITS and name NAME. */
17352
17353static struct type *
17354dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17355 int bits, int unsigned_p, const char *name)
17356{
17357 struct type *type;
17358
17359 /* Versions of Intel's C Compiler generate an integer type called "void"
17360 instead of using DW_TAG_unspecified_type. This has been seen on
17361 at least versions 14, 17, and 18. */
35ee2dc2
AB
17362 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17363 && strcmp (name, "void") == 0)
eb77c9df
AB
17364 type = objfile_type (objfile)->builtin_void;
17365 else
17366 type = init_integer_type (objfile, bits, unsigned_p, name);
17367
17368 return type;
17369}
17370
8bdc1658
AB
17371/* Initialise and return a floating point type of size BITS suitable for
17372 use as a component of a complex number. The NAME_HINT is passed through
17373 when initialising the floating point type and is the name of the complex
17374 type.
17375
17376 As DWARF doesn't currently provide an explicit name for the components
17377 of a complex number, but it can be helpful to have these components
17378 named, we try to select a suitable name based on the size of the
17379 component. */
17380static struct type *
17381dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17382 struct objfile *objfile,
103a685e
TT
17383 int bits, const char *name_hint,
17384 enum bfd_endian byte_order)
8bdc1658 17385{
08feed99 17386 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17387 struct type *tt = nullptr;
17388
35add35e
AB
17389 /* Try to find a suitable floating point builtin type of size BITS.
17390 We're going to use the name of this type as the name for the complex
17391 target type that we are about to create. */
1db455a7 17392 switch (cu->language)
8bdc1658 17393 {
1db455a7
AB
17394 case language_fortran:
17395 switch (bits)
17396 {
17397 case 32:
17398 tt = builtin_f_type (gdbarch)->builtin_real;
17399 break;
17400 case 64:
17401 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17402 break;
17403 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17404 case 128:
17405 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17406 break;
17407 }
8bdc1658 17408 break;
1db455a7
AB
17409 default:
17410 switch (bits)
17411 {
17412 case 32:
17413 tt = builtin_type (gdbarch)->builtin_float;
17414 break;
17415 case 64:
17416 tt = builtin_type (gdbarch)->builtin_double;
17417 break;
17418 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17419 case 128:
17420 tt = builtin_type (gdbarch)->builtin_long_double;
17421 break;
17422 }
8bdc1658
AB
17423 break;
17424 }
17425
35add35e
AB
17426 /* If the type we found doesn't match the size we were looking for, then
17427 pretend we didn't find a type at all, the complex target type we
17428 create will then be nameless. */
a12e5744 17429 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17430 tt = nullptr;
17431
7d93a1e0 17432 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17433 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17434}
17435
c906108c
SS
17436/* Find a representation of a given base type and install
17437 it in the TYPE field of the die. */
17438
f792889a 17439static struct type *
e7c27a73 17440read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17441{
5e22e966 17442 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17443 struct type *type;
17444 struct attribute *attr;
19f392bc 17445 int encoding = 0, bits = 0;
15d034d0 17446 const char *name;
34877895 17447 gdbarch *arch;
c906108c 17448
e142c38c 17449 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17450 if (attr != nullptr)
34877895 17451 encoding = DW_UNSND (attr);
e142c38c 17452 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17453 if (attr != nullptr)
34877895 17454 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17455 name = dwarf2_name (die, cu);
6ccb9162 17456 if (!name)
34877895 17457 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17458
08feed99 17459 arch = objfile->arch ();
103a685e
TT
17460 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17461
34877895
PJ
17462 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17463 if (attr)
103a685e
TT
17464 {
17465 int endianity = DW_UNSND (attr);
17466
17467 switch (endianity)
17468 {
17469 case DW_END_big:
17470 byte_order = BFD_ENDIAN_BIG;
17471 break;
17472 case DW_END_little:
17473 byte_order = BFD_ENDIAN_LITTLE;
17474 break;
17475 default:
17476 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17477 break;
17478 }
17479 }
6ccb9162
UW
17480
17481 switch (encoding)
c906108c 17482 {
6ccb9162
UW
17483 case DW_ATE_address:
17484 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17485 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17486 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17487 break;
17488 case DW_ATE_boolean:
19f392bc 17489 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17490 break;
17491 case DW_ATE_complex_float:
103a685e
TT
17492 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17493 byte_order);
78134374 17494 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17495 {
17496 if (name == nullptr)
17497 {
17498 struct obstack *obstack
5e22e966 17499 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17500 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17501 nullptr);
17502 }
17503 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17504 }
17505 else
17506 type = init_complex_type (name, type);
6ccb9162
UW
17507 break;
17508 case DW_ATE_decimal_float:
19f392bc 17509 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17510 break;
17511 case DW_ATE_float:
103a685e 17512 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17513 break;
17514 case DW_ATE_signed:
eb77c9df 17515 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17516 break;
17517 case DW_ATE_unsigned:
3b2b8fea
TT
17518 if (cu->language == language_fortran
17519 && name
61012eef 17520 && startswith (name, "character("))
19f392bc
UW
17521 type = init_character_type (objfile, bits, 1, name);
17522 else
eb77c9df 17523 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17524 break;
17525 case DW_ATE_signed_char:
6e70227d 17526 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17527 || cu->language == language_pascal
17528 || cu->language == language_fortran)
19f392bc
UW
17529 type = init_character_type (objfile, bits, 0, name);
17530 else
eb77c9df 17531 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17532 break;
17533 case DW_ATE_unsigned_char:
868a0084 17534 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17535 || cu->language == language_pascal
c44af4eb
TT
17536 || cu->language == language_fortran
17537 || cu->language == language_rust)
19f392bc
UW
17538 type = init_character_type (objfile, bits, 1, name);
17539 else
eb77c9df 17540 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17541 break;
75079b2b 17542 case DW_ATE_UTF:
53e710ac 17543 {
53e710ac
PA
17544 if (bits == 16)
17545 type = builtin_type (arch)->builtin_char16;
17546 else if (bits == 32)
17547 type = builtin_type (arch)->builtin_char32;
17548 else
17549 {
b98664d3 17550 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17551 bits);
eb77c9df 17552 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17553 }
17554 return set_die_type (die, type, cu);
17555 }
75079b2b
TT
17556 break;
17557
6ccb9162 17558 default:
b98664d3 17559 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17560 dwarf_type_encoding_name (encoding));
77b7c781 17561 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17562 break;
c906108c 17563 }
6ccb9162 17564
0114d602 17565 if (name && strcmp (name, "char") == 0)
876cecd0 17566 TYPE_NOSIGN (type) = 1;
0114d602 17567
2b4424c3
TT
17568 maybe_set_alignment (cu, die, type);
17569
103a685e 17570 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17571
f792889a 17572 return set_die_type (die, type, cu);
c906108c
SS
17573}
17574
80180f79
SA
17575/* Parse dwarf attribute if it's a block, reference or constant and put the
17576 resulting value of the attribute into struct bound_prop.
17577 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17578
17579static int
17580attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17581 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17582 struct type *default_type)
80180f79
SA
17583{
17584 struct dwarf2_property_baton *baton;
5e22e966 17585 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17586 struct objfile *objfile = per_objfile->objfile;
17587 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17588
9a49df9d
AB
17589 gdb_assert (default_type != NULL);
17590
80180f79
SA
17591 if (attr == NULL || prop == NULL)
17592 return 0;
17593
4fc6c0d5 17594 if (attr->form_is_block ())
80180f79 17595 {
8d749320 17596 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17597 baton->property_type = default_type;
80180f79 17598 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17599 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17600 baton->locexpr.size = DW_BLOCK (attr)->size;
17601 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17602 switch (attr->name)
17603 {
17604 case DW_AT_string_length:
17605 baton->locexpr.is_reference = true;
17606 break;
17607 default:
17608 baton->locexpr.is_reference = false;
17609 break;
17610 }
80180f79
SA
17611 prop->data.baton = baton;
17612 prop->kind = PROP_LOCEXPR;
17613 gdb_assert (prop->data.baton != NULL);
17614 }
cd6c91b4 17615 else if (attr->form_is_ref ())
80180f79
SA
17616 {
17617 struct dwarf2_cu *target_cu = cu;
17618 struct die_info *target_die;
17619 struct attribute *target_attr;
17620
17621 target_die = follow_die_ref (die, attr, &target_cu);
17622 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17623 if (target_attr == NULL)
17624 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17625 target_cu);
80180f79
SA
17626 if (target_attr == NULL)
17627 return 0;
17628
df25ebbd 17629 switch (target_attr->name)
80180f79 17630 {
df25ebbd 17631 case DW_AT_location:
cd6c91b4 17632 if (target_attr->form_is_section_offset ())
df25ebbd 17633 {
8d749320 17634 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17635 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17636 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17637 prop->data.baton = baton;
17638 prop->kind = PROP_LOCLIST;
17639 gdb_assert (prop->data.baton != NULL);
17640 }
4fc6c0d5 17641 else if (target_attr->form_is_block ())
df25ebbd 17642 {
8d749320 17643 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17644 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17645 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17646 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17647 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17648 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17649 baton->locexpr.is_reference = true;
df25ebbd
JB
17650 prop->data.baton = baton;
17651 prop->kind = PROP_LOCEXPR;
17652 gdb_assert (prop->data.baton != NULL);
17653 }
17654 else
17655 {
17656 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17657 "dynamic property");
17658 return 0;
17659 }
17660 break;
17661 case DW_AT_data_member_location:
17662 {
17663 LONGEST offset;
17664
17665 if (!handle_data_member_location (target_die, target_cu,
17666 &offset))
17667 return 0;
17668
8d749320 17669 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17670 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17671 target_cu);
df25ebbd
JB
17672 baton->offset_info.offset = offset;
17673 baton->offset_info.type = die_type (target_die, target_cu);
17674 prop->data.baton = baton;
17675 prop->kind = PROP_ADDR_OFFSET;
17676 break;
17677 }
80180f79
SA
17678 }
17679 }
cd6c91b4 17680 else if (attr->form_is_constant ())
80180f79 17681 {
0826b30a 17682 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17683 prop->kind = PROP_CONST;
17684 }
17685 else
17686 {
17687 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17688 dwarf2_name (die, cu));
17689 return 0;
17690 }
17691
17692 return 1;
17693}
17694
09ba997f 17695/* See read.h. */
9a49df9d 17696
09ba997f 17697struct type *
293e7e51 17698dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17699{
9a49df9d
AB
17700 struct type *int_type;
17701
17702 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17703#define TRY_TYPE(F) \
17704 int_type = (unsigned_p \
17705 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17706 : objfile_type (objfile)->builtin_ ## F); \
17707 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17708 return int_type
17709
17710 TRY_TYPE (char);
17711 TRY_TYPE (short);
17712 TRY_TYPE (int);
17713 TRY_TYPE (long);
17714 TRY_TYPE (long_long);
17715
17716#undef TRY_TYPE
17717
17718 gdb_assert_not_reached ("unable to find suitable integer type");
17719}
17720
09ba997f 17721/* See read.h. */
11a8b164 17722
09ba997f 17723struct type *
293e7e51 17724dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17725{
293e7e51
SM
17726 int addr_size = this->per_cu->addr_size ();
17727 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17728}
17729
b86352cf
AB
17730/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17731 present (which is valid) then compute the default type based on the
17732 compilation units address size. */
17733
17734static struct type *
17735read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17736{
17737 struct type *index_type = die_type (die, cu);
17738
17739 /* Dwarf-2 specifications explicitly allows to create subrange types
17740 without specifying a base type.
17741 In that case, the base type must be set to the type of
17742 the lower bound, upper bound or count, in that order, if any of these
17743 three attributes references an object that has a type.
17744 If no base type is found, the Dwarf-2 specifications say that
17745 a signed integer type of size equal to the size of an address should
17746 be used.
17747 For the following C code: `extern char gdb_int [];'
17748 GCC produces an empty range DIE.
17749 FIXME: muller/2010-05-28: Possible references to object for low bound,
17750 high bound or count are not yet handled by this code. */
78134374 17751 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17752 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17753
17754 return index_type;
17755}
17756
a02abb62
JB
17757/* Read the given DW_AT_subrange DIE. */
17758
f792889a 17759static struct type *
a02abb62
JB
17760read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17761{
4c9ad8c2 17762 struct type *base_type, *orig_base_type;
a02abb62
JB
17763 struct type *range_type;
17764 struct attribute *attr;
729efb13 17765 struct dynamic_prop low, high;
4fae6e18 17766 int low_default_is_valid;
c451ebe5 17767 int high_bound_is_count = 0;
15d034d0 17768 const char *name;
d359392f 17769 ULONGEST negative_mask;
e77813c8 17770
b86352cf
AB
17771 orig_base_type = read_subrange_index_type (die, cu);
17772
4c9ad8c2
TT
17773 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17774 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17775 creating the range type, but we use the result of check_typedef
17776 when examining properties of the type. */
17777 base_type = check_typedef (orig_base_type);
a02abb62 17778
7e314c57
JK
17779 /* The die_type call above may have already set the type for this DIE. */
17780 range_type = get_die_type (die, cu);
17781 if (range_type)
17782 return range_type;
17783
729efb13
SA
17784 low.kind = PROP_CONST;
17785 high.kind = PROP_CONST;
17786 high.data.const_val = 0;
17787
4fae6e18
JK
17788 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17789 omitting DW_AT_lower_bound. */
17790 switch (cu->language)
6e70227d 17791 {
4fae6e18
JK
17792 case language_c:
17793 case language_cplus:
729efb13 17794 low.data.const_val = 0;
4fae6e18
JK
17795 low_default_is_valid = 1;
17796 break;
17797 case language_fortran:
729efb13 17798 low.data.const_val = 1;
4fae6e18
JK
17799 low_default_is_valid = 1;
17800 break;
17801 case language_d:
4fae6e18 17802 case language_objc:
c44af4eb 17803 case language_rust:
729efb13 17804 low.data.const_val = 0;
4fae6e18
JK
17805 low_default_is_valid = (cu->header.version >= 4);
17806 break;
17807 case language_ada:
17808 case language_m2:
17809 case language_pascal:
729efb13 17810 low.data.const_val = 1;
4fae6e18
JK
17811 low_default_is_valid = (cu->header.version >= 4);
17812 break;
17813 default:
729efb13 17814 low.data.const_val = 0;
4fae6e18
JK
17815 low_default_is_valid = 0;
17816 break;
a02abb62
JB
17817 }
17818
e142c38c 17819 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17820 if (attr != nullptr)
9a49df9d 17821 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17822 else if (!low_default_is_valid)
b98664d3 17823 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17824 "- DIE at %s [in module %s]"),
17825 sect_offset_str (die->sect_off),
5e22e966 17826 objfile_name (cu->per_objfile->objfile));
a02abb62 17827
506f5c41
TV
17828 struct attribute *attr_ub, *attr_count;
17829 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17830 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17831 {
506f5c41 17832 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17833 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17834 {
c451ebe5
SA
17835 /* If bounds are constant do the final calculation here. */
17836 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17837 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17838 else
17839 high_bound_is_count = 1;
c2ff108b 17840 }
506f5c41
TV
17841 else
17842 {
17843 if (attr_ub != NULL)
17844 complaint (_("Unresolved DW_AT_upper_bound "
17845 "- DIE at %s [in module %s]"),
17846 sect_offset_str (die->sect_off),
5e22e966 17847 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17848 if (attr_count != NULL)
17849 complaint (_("Unresolved DW_AT_count "
17850 "- DIE at %s [in module %s]"),
17851 sect_offset_str (die->sect_off),
5e22e966 17852 objfile_name (cu->per_objfile->objfile));
506f5c41 17853 }
e77813c8 17854 }
a02abb62 17855
4e962e74
TT
17856 LONGEST bias = 0;
17857 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17858 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17859 bias = bias_attr->constant_value (0);
4e962e74 17860
dbb9c2b1
JB
17861 /* Normally, the DWARF producers are expected to use a signed
17862 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17863 But this is unfortunately not always the case, as witnessed
17864 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17865 is used instead. To work around that ambiguity, we treat
17866 the bounds as signed, and thus sign-extend their values, when
17867 the base type is signed. */
6e70227d 17868 negative_mask =
d359392f 17869 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17870 if (low.kind == PROP_CONST
17871 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17872 low.data.const_val |= negative_mask;
17873 if (high.kind == PROP_CONST
17874 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17875 high.data.const_val |= negative_mask;
43bbcdc2 17876
5bbd8269
AB
17877 /* Check for bit and byte strides. */
17878 struct dynamic_prop byte_stride_prop;
17879 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17880 if (attr_byte_stride != nullptr)
17881 {
293e7e51 17882 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17883 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17884 prop_type);
17885 }
17886
17887 struct dynamic_prop bit_stride_prop;
17888 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17889 if (attr_bit_stride != nullptr)
17890 {
17891 /* It only makes sense to have either a bit or byte stride. */
17892 if (attr_byte_stride != nullptr)
17893 {
17894 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17895 "- DIE at %s [in module %s]"),
17896 sect_offset_str (die->sect_off),
5e22e966 17897 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17898 attr_bit_stride = nullptr;
17899 }
17900 else
17901 {
293e7e51 17902 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17903 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17904 prop_type);
17905 }
17906 }
17907
17908 if (attr_byte_stride != nullptr
17909 || attr_bit_stride != nullptr)
17910 {
17911 bool byte_stride_p = (attr_byte_stride != nullptr);
17912 struct dynamic_prop *stride
17913 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17914
17915 range_type
17916 = create_range_type_with_stride (NULL, orig_base_type, &low,
17917 &high, bias, stride, byte_stride_p);
17918 }
17919 else
17920 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17921
c451ebe5
SA
17922 if (high_bound_is_count)
17923 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17924
c2ff108b
JK
17925 /* Ada expects an empty array on no boundary attributes. */
17926 if (attr == NULL && cu->language != language_ada)
729efb13 17927 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17928
39cbfefa
DJ
17929 name = dwarf2_name (die, cu);
17930 if (name)
d0e39ea2 17931 range_type->set_name (name);
6e70227d 17932
e142c38c 17933 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17934 if (attr != nullptr)
a02abb62
JB
17935 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17936
2b4424c3
TT
17937 maybe_set_alignment (cu, die, range_type);
17938
7e314c57
JK
17939 set_die_type (die, range_type, cu);
17940
17941 /* set_die_type should be already done. */
b4ba55a1
JB
17942 set_descriptive_type (range_type, die, cu);
17943
7e314c57 17944 return range_type;
a02abb62 17945}
6e70227d 17946
f792889a 17947static struct type *
81a17f79
JB
17948read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17949{
17950 struct type *type;
81a17f79 17951
5e22e966 17952 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17953 type->set_name (dwarf2_name (die, cu));
81a17f79 17954
74a2f8ff 17955 /* In Ada, an unspecified type is typically used when the description
85102364 17956 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17957 such a type, we treat it as a stub, and try to resolve it later on,
17958 when needed. */
17959 if (cu->language == language_ada)
17960 TYPE_STUB (type) = 1;
17961
f792889a 17962 return set_die_type (die, type, cu);
81a17f79 17963}
a02abb62 17964
639d11d3
DC
17965/* Read a single die and all its descendents. Set the die's sibling
17966 field to NULL; set other fields in the die correctly, and set all
17967 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17968 location of the info_ptr after reading all of those dies. PARENT
17969 is the parent of the die in question. */
17970
17971static struct die_info *
dee91e82 17972read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17973 const gdb_byte *info_ptr,
17974 const gdb_byte **new_info_ptr,
dee91e82 17975 struct die_info *parent)
639d11d3
DC
17976{
17977 struct die_info *die;
d521ce57 17978 const gdb_byte *cur_ptr;
639d11d3 17979
3e225074 17980 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17981 if (die == NULL)
17982 {
17983 *new_info_ptr = cur_ptr;
17984 return NULL;
17985 }
93311388 17986 store_in_ref_table (die, reader->cu);
639d11d3 17987
3e225074 17988 if (die->has_children)
bf6af496 17989 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17990 else
17991 {
17992 die->child = NULL;
17993 *new_info_ptr = cur_ptr;
17994 }
17995
17996 die->sibling = NULL;
17997 die->parent = parent;
17998 return die;
17999}
18000
18001/* Read a die, all of its descendents, and all of its siblings; set
18002 all of the fields of all of the dies correctly. Arguments are as
18003 in read_die_and_children. */
18004
18005static struct die_info *
bf6af496 18006read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18007 const gdb_byte *info_ptr,
18008 const gdb_byte **new_info_ptr,
bf6af496 18009 struct die_info *parent)
639d11d3
DC
18010{
18011 struct die_info *first_die, *last_sibling;
d521ce57 18012 const gdb_byte *cur_ptr;
639d11d3 18013
c906108c 18014 cur_ptr = info_ptr;
639d11d3
DC
18015 first_die = last_sibling = NULL;
18016
18017 while (1)
c906108c 18018 {
639d11d3 18019 struct die_info *die
dee91e82 18020 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18021
1d325ec1 18022 if (die == NULL)
c906108c 18023 {
639d11d3
DC
18024 *new_info_ptr = cur_ptr;
18025 return first_die;
c906108c 18026 }
1d325ec1
DJ
18027
18028 if (!first_die)
18029 first_die = die;
c906108c 18030 else
1d325ec1
DJ
18031 last_sibling->sibling = die;
18032
18033 last_sibling = die;
c906108c 18034 }
c906108c
SS
18035}
18036
bf6af496
DE
18037/* Read a die, all of its descendents, and all of its siblings; set
18038 all of the fields of all of the dies correctly. Arguments are as
18039 in read_die_and_children.
18040 This the main entry point for reading a DIE and all its children. */
18041
18042static struct die_info *
18043read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18044 const gdb_byte *info_ptr,
18045 const gdb_byte **new_info_ptr,
bf6af496
DE
18046 struct die_info *parent)
18047{
18048 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18049 new_info_ptr, parent);
18050
b4f54984 18051 if (dwarf_die_debug)
bf6af496
DE
18052 {
18053 fprintf_unfiltered (gdb_stdlog,
18054 "Read die from %s@0x%x of %s:\n",
96b79293 18055 reader->die_section->get_name (),
bf6af496
DE
18056 (unsigned) (info_ptr - reader->die_section->buffer),
18057 bfd_get_filename (reader->abfd));
b4f54984 18058 dump_die (die, dwarf_die_debug);
bf6af496
DE
18059 }
18060
18061 return die;
18062}
18063
3019eac3
DE
18064/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18065 attributes.
18066 The caller is responsible for filling in the extra attributes
18067 and updating (*DIEP)->num_attrs.
18068 Set DIEP to point to a newly allocated die with its information,
3e225074 18069 except for its child, sibling, and parent fields. */
93311388 18070
d521ce57 18071static const gdb_byte *
3019eac3 18072read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18073 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18074 int num_extra_attrs)
93311388 18075{
b64f50a1 18076 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18077 struct abbrev_info *abbrev;
18078 struct die_info *die;
18079 struct dwarf2_cu *cu = reader->cu;
18080 bfd *abfd = reader->abfd;
18081
9c541725 18082 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18083 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18084 info_ptr += bytes_read;
18085 if (!abbrev_number)
18086 {
18087 *diep = NULL;
93311388
DE
18088 return info_ptr;
18089 }
18090
685af9cd 18091 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18092 if (!abbrev)
348e048f
DE
18093 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18094 abbrev_number,
18095 bfd_get_filename (abfd));
18096
3019eac3 18097 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18098 die->sect_off = sect_off;
93311388
DE
18099 die->tag = abbrev->tag;
18100 die->abbrev = abbrev_number;
3e225074 18101 die->has_children = abbrev->has_children;
93311388 18102
3019eac3
DE
18103 /* Make the result usable.
18104 The caller needs to update num_attrs after adding the extra
18105 attributes. */
93311388
DE
18106 die->num_attrs = abbrev->num_attrs;
18107
18a8505e 18108 std::vector<int> indexes_that_need_reprocess;
93311388 18109 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18110 {
18111 bool need_reprocess;
18112 info_ptr =
18113 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18114 info_ptr, &need_reprocess);
18115 if (need_reprocess)
18116 indexes_that_need_reprocess.push_back (i);
18117 }
18118
052c8bb8 18119 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18120 if (attr != nullptr)
18121 cu->str_offsets_base = DW_UNSND (attr);
93311388 18122
41144253 18123 attr = die->attr (DW_AT_loclists_base);
18124 if (attr != nullptr)
18125 cu->loclist_base = DW_UNSND (attr);
18126
a39fdb41 18127 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18128 if (maybe_addr_base.has_value ())
18129 cu->addr_base = *maybe_addr_base;
18130 for (int index : indexes_that_need_reprocess)
18131 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18132 *diep = die;
93311388
DE
18133 return info_ptr;
18134}
18135
3019eac3
DE
18136/* Read a die and all its attributes.
18137 Set DIEP to point to a newly allocated die with its information,
3e225074 18138 except for its child, sibling, and parent fields. */
3019eac3 18139
d521ce57 18140static const gdb_byte *
3019eac3 18141read_full_die (const struct die_reader_specs *reader,
3e225074 18142 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18143{
d521ce57 18144 const gdb_byte *result;
bf6af496 18145
3e225074 18146 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18147
b4f54984 18148 if (dwarf_die_debug)
bf6af496
DE
18149 {
18150 fprintf_unfiltered (gdb_stdlog,
18151 "Read die from %s@0x%x of %s:\n",
96b79293 18152 reader->die_section->get_name (),
bf6af496
DE
18153 (unsigned) (info_ptr - reader->die_section->buffer),
18154 bfd_get_filename (reader->abfd));
b4f54984 18155 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18156 }
18157
18158 return result;
3019eac3 18159}
433df2d4 18160\f
c906108c 18161
72bf9492
DJ
18162/* Returns nonzero if TAG represents a type that we might generate a partial
18163 symbol for. */
18164
18165static int
18166is_type_tag_for_partial (int tag)
18167{
18168 switch (tag)
18169 {
18170#if 0
18171 /* Some types that would be reasonable to generate partial symbols for,
18172 that we don't at present. */
18173 case DW_TAG_array_type:
18174 case DW_TAG_file_type:
18175 case DW_TAG_ptr_to_member_type:
18176 case DW_TAG_set_type:
18177 case DW_TAG_string_type:
18178 case DW_TAG_subroutine_type:
18179#endif
18180 case DW_TAG_base_type:
18181 case DW_TAG_class_type:
680b30c7 18182 case DW_TAG_interface_type:
72bf9492
DJ
18183 case DW_TAG_enumeration_type:
18184 case DW_TAG_structure_type:
18185 case DW_TAG_subrange_type:
18186 case DW_TAG_typedef:
18187 case DW_TAG_union_type:
18188 return 1;
18189 default:
18190 return 0;
18191 }
18192}
18193
18194/* Load all DIEs that are interesting for partial symbols into memory. */
18195
18196static struct partial_die_info *
dee91e82 18197load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18198 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18199{
dee91e82 18200 struct dwarf2_cu *cu = reader->cu;
5e22e966 18201 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18202 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18203 unsigned int bytes_read;
5afb4e99 18204 unsigned int load_all = 0;
72bf9492
DJ
18205 int nesting_level = 1;
18206
18207 parent_die = NULL;
18208 last_die = NULL;
18209
7adf1e79
DE
18210 gdb_assert (cu->per_cu != NULL);
18211 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18212 load_all = 1;
18213
72bf9492
DJ
18214 cu->partial_dies
18215 = htab_create_alloc_ex (cu->header.length / 12,
18216 partial_die_hash,
18217 partial_die_eq,
18218 NULL,
18219 &cu->comp_unit_obstack,
18220 hashtab_obstack_allocate,
18221 dummy_obstack_deallocate);
18222
72bf9492
DJ
18223 while (1)
18224 {
685af9cd 18225 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18226
18227 /* A NULL abbrev means the end of a series of children. */
18228 if (abbrev == NULL)
18229 {
18230 if (--nesting_level == 0)
cd9983dd
YQ
18231 return first_die;
18232
72bf9492
DJ
18233 info_ptr += bytes_read;
18234 last_die = parent_die;
18235 parent_die = parent_die->die_parent;
18236 continue;
18237 }
18238
98bfdba5
PA
18239 /* Check for template arguments. We never save these; if
18240 they're seen, we just mark the parent, and go on our way. */
18241 if (parent_die != NULL
18242 && cu->language == language_cplus
18243 && (abbrev->tag == DW_TAG_template_type_param
18244 || abbrev->tag == DW_TAG_template_value_param))
18245 {
18246 parent_die->has_template_arguments = 1;
18247
18248 if (!load_all)
18249 {
18250 /* We don't need a partial DIE for the template argument. */
dee91e82 18251 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18252 continue;
18253 }
18254 }
18255
0d99eb77 18256 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18257 Skip their other children. */
18258 if (!load_all
18259 && cu->language == language_cplus
18260 && parent_die != NULL
f9b5d5ea
TV
18261 && parent_die->tag == DW_TAG_subprogram
18262 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 18263 {
dee91e82 18264 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18265 continue;
18266 }
18267
5afb4e99
DJ
18268 /* Check whether this DIE is interesting enough to save. Normally
18269 we would not be interested in members here, but there may be
18270 later variables referencing them via DW_AT_specification (for
18271 static members). */
18272 if (!load_all
18273 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18274 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18275 && abbrev->tag != DW_TAG_enumerator
18276 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18277 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18278 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18279 && abbrev->tag != DW_TAG_variable
5afb4e99 18280 && abbrev->tag != DW_TAG_namespace
f55ee35c 18281 && abbrev->tag != DW_TAG_module
95554aad 18282 && abbrev->tag != DW_TAG_member
74921315
KS
18283 && abbrev->tag != DW_TAG_imported_unit
18284 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18285 {
18286 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18287 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18288 continue;
18289 }
18290
6f06d47b
YQ
18291 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18292 abbrev);
cd9983dd 18293
48fbe735 18294 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18295
18296 /* This two-pass algorithm for processing partial symbols has a
18297 high cost in cache pressure. Thus, handle some simple cases
18298 here which cover the majority of C partial symbols. DIEs
18299 which neither have specification tags in them, nor could have
18300 specification tags elsewhere pointing at them, can simply be
18301 processed and discarded.
18302
18303 This segment is also optional; scan_partial_symbols and
18304 add_partial_symbol will handle these DIEs if we chain
18305 them in normally. When compilers which do not emit large
18306 quantities of duplicate debug information are more common,
18307 this code can probably be removed. */
18308
18309 /* Any complete simple types at the top level (pretty much all
18310 of them, for a language without namespaces), can be processed
18311 directly. */
18312 if (parent_die == NULL
cd9983dd
YQ
18313 && pdi.has_specification == 0
18314 && pdi.is_declaration == 0
18315 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18316 || pdi.tag == DW_TAG_base_type
18317 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18318 {
7d00ffec 18319 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
18320 add_partial_symbol (&pdi, cu);
18321
cd9983dd 18322 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18323 continue;
18324 }
18325
d8228535
JK
18326 /* The exception for DW_TAG_typedef with has_children above is
18327 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18328 type_name_or_error will error on such types later.
d8228535
JK
18329
18330 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18331 it could not find the child DIEs referenced later, this is checked
18332 above. In correct DWARF DW_TAG_typedef should have no children. */
18333
cd9983dd 18334 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18335 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18336 "- DIE at %s [in module %s]"),
cd9983dd 18337 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18338
72bf9492
DJ
18339 /* If we're at the second level, and we're an enumerator, and
18340 our parent has no specification (meaning possibly lives in a
18341 namespace elsewhere), then we can add the partial symbol now
18342 instead of queueing it. */
cd9983dd 18343 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18344 && parent_die != NULL
18345 && parent_die->die_parent == NULL
18346 && parent_die->tag == DW_TAG_enumeration_type
18347 && parent_die->has_specification == 0)
18348 {
7d00ffec 18349 if (pdi.raw_name == NULL)
b98664d3 18350 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18351 else if (building_psymtab)
f0fbb768 18352 add_partial_symbol (&pdi, cu);
72bf9492 18353
cd9983dd 18354 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18355 continue;
18356 }
18357
cd9983dd 18358 struct partial_die_info *part_die
6f06d47b 18359 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18360
72bf9492
DJ
18361 /* We'll save this DIE so link it in. */
18362 part_die->die_parent = parent_die;
18363 part_die->die_sibling = NULL;
18364 part_die->die_child = NULL;
18365
18366 if (last_die && last_die == parent_die)
18367 last_die->die_child = part_die;
18368 else if (last_die)
18369 last_die->die_sibling = part_die;
18370
18371 last_die = part_die;
18372
18373 if (first_die == NULL)
18374 first_die = part_die;
18375
18376 /* Maybe add the DIE to the hash table. Not all DIEs that we
18377 find interesting need to be in the hash table, because we
18378 also have the parent/sibling/child chains; only those that we
18379 might refer to by offset later during partial symbol reading.
18380
18381 For now this means things that might have be the target of a
18382 DW_AT_specification, DW_AT_abstract_origin, or
18383 DW_AT_extension. DW_AT_extension will refer only to
18384 namespaces; DW_AT_abstract_origin refers to functions (and
18385 many things under the function DIE, but we do not recurse
18386 into function DIEs during partial symbol reading) and
18387 possibly variables as well; DW_AT_specification refers to
18388 declarations. Declarations ought to have the DW_AT_declaration
18389 flag. It happens that GCC forgets to put it in sometimes, but
18390 only for functions, not for types.
18391
18392 Adding more things than necessary to the hash table is harmless
18393 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18394 wasted time in find_partial_die, when we reread the compilation
18395 unit with load_all_dies set. */
72bf9492 18396
5afb4e99 18397 if (load_all
72929c62 18398 || abbrev->tag == DW_TAG_constant
5afb4e99 18399 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18400 || abbrev->tag == DW_TAG_variable
18401 || abbrev->tag == DW_TAG_namespace
18402 || part_die->is_declaration)
18403 {
18404 void **slot;
18405
18406 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18407 to_underlying (part_die->sect_off),
18408 INSERT);
72bf9492
DJ
18409 *slot = part_die;
18410 }
18411
72bf9492 18412 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18413 we have no reason to follow the children of structures; for other
98bfdba5
PA
18414 languages we have to, so that we can get at method physnames
18415 to infer fully qualified class names, for DW_AT_specification,
18416 and for C++ template arguments. For C++, we also look one level
18417 inside functions to find template arguments (if the name of the
18418 function does not already contain the template arguments).
bc30ff58 18419
0a4b0913
AB
18420 For Ada and Fortran, we need to scan the children of subprograms
18421 and lexical blocks as well because these languages allow the
18422 definition of nested entities that could be interesting for the
18423 debugger, such as nested subprograms for instance. */
72bf9492 18424 if (last_die->has_children
5afb4e99
DJ
18425 && (load_all
18426 || last_die->tag == DW_TAG_namespace
f55ee35c 18427 || last_die->tag == DW_TAG_module
72bf9492 18428 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18429 || (cu->language == language_cplus
18430 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
18431 && (last_die->raw_name == NULL
18432 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
18433 || (cu->language != language_c
18434 && (last_die->tag == DW_TAG_class_type
680b30c7 18435 || last_die->tag == DW_TAG_interface_type
72bf9492 18436 || last_die->tag == DW_TAG_structure_type
bc30ff58 18437 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18438 || ((cu->language == language_ada
18439 || cu->language == language_fortran)
bc30ff58
JB
18440 && (last_die->tag == DW_TAG_subprogram
18441 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18442 {
18443 nesting_level++;
18444 parent_die = last_die;
18445 continue;
18446 }
18447
18448 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18449 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18450
18451 /* Back to the top, do it again. */
18452 }
18453}
18454
6f06d47b
YQ
18455partial_die_info::partial_die_info (sect_offset sect_off_,
18456 struct abbrev_info *abbrev)
18457 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18458{
18459}
18460
7d00ffec
TT
18461/* See class definition. */
18462
18463const char *
18464partial_die_info::name (dwarf2_cu *cu)
18465{
18466 if (!canonical_name && raw_name != nullptr)
18467 {
18468 struct objfile *objfile = cu->per_objfile->objfile;
18469 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18470 canonical_name = 1;
18471 }
18472
18473 return raw_name;
18474}
18475
35cc7ed7
YQ
18476/* Read a minimal amount of information into the minimal die structure.
18477 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18478
48fbe735
YQ
18479const gdb_byte *
18480partial_die_info::read (const struct die_reader_specs *reader,
18481 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18482{
dee91e82 18483 struct dwarf2_cu *cu = reader->cu;
976ca316 18484 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 18485 unsigned int i;
c5aa993b 18486 int has_low_pc_attr = 0;
c906108c 18487 int has_high_pc_attr = 0;
91da1414 18488 int high_pc_relative = 0;
c906108c 18489
fd0a254f 18490 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18491 {
e7da7f8f 18492 attribute attr;
18a8505e 18493 bool need_reprocess;
e7da7f8f 18494 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18495 info_ptr, &need_reprocess);
18496 /* String and address offsets that need to do the reprocessing have
18497 already been read at this point, so there is no need to wait until
18498 the loop terminates to do the reprocessing. */
18499 if (need_reprocess)
e7da7f8f 18500 read_attribute_reprocess (reader, &attr);
c906108c 18501 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18502 partial symbol table. */
c906108c
SS
18503 switch (attr.name)
18504 {
18505 case DW_AT_name:
48fbe735 18506 switch (tag)
71c25dea
TT
18507 {
18508 case DW_TAG_compile_unit:
95554aad 18509 case DW_TAG_partial_unit:
348e048f 18510 case DW_TAG_type_unit:
71c25dea
TT
18511 /* Compilation units have a DW_AT_name that is a filename, not
18512 a source language identifier. */
18513 case DW_TAG_enumeration_type:
18514 case DW_TAG_enumerator:
18515 /* These tags always have simple identifiers already; no need
18516 to canonicalize them. */
7d00ffec
TT
18517 canonical_name = 1;
18518 raw_name = DW_STRING (&attr);
71c25dea
TT
18519 break;
18520 default:
7d00ffec
TT
18521 canonical_name = 0;
18522 raw_name = DW_STRING (&attr);
71c25dea
TT
18523 break;
18524 }
c906108c 18525 break;
31ef98ae 18526 case DW_AT_linkage_name:
c906108c 18527 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18528 /* Note that both forms of linkage name might appear. We
18529 assume they will be the same, and we only store the last
18530 one we see. */
e61108c9 18531 linkage_name = attr.value_as_string ();
787de330
TT
18532 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18533 See https://github.com/rust-lang/rust/issues/32925. */
18534 if (cu->language == language_rust && linkage_name != NULL
18535 && strchr (linkage_name, '{') != NULL)
18536 linkage_name = NULL;
c906108c
SS
18537 break;
18538 case DW_AT_low_pc:
18539 has_low_pc_attr = 1;
cd6c91b4 18540 lowpc = attr.value_as_address ();
c906108c
SS
18541 break;
18542 case DW_AT_high_pc:
18543 has_high_pc_attr = 1;
cd6c91b4
TT
18544 highpc = attr.value_as_address ();
18545 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18546 high_pc_relative = 1;
c906108c
SS
18547 break;
18548 case DW_AT_location:
0963b4bd 18549 /* Support the .debug_loc offsets. */
4fc6c0d5 18550 if (attr.form_is_block ())
8e19ed76 18551 {
48fbe735 18552 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18553 }
cd6c91b4 18554 else if (attr.form_is_section_offset ())
8e19ed76 18555 {
4d3c2250 18556 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18557 }
18558 else
18559 {
4d3c2250
KB
18560 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18561 "partial symbol information");
8e19ed76 18562 }
c906108c 18563 break;
c906108c 18564 case DW_AT_external:
48fbe735 18565 is_external = DW_UNSND (&attr);
c906108c
SS
18566 break;
18567 case DW_AT_declaration:
48fbe735 18568 is_declaration = DW_UNSND (&attr);
c906108c
SS
18569 break;
18570 case DW_AT_type:
48fbe735 18571 has_type = 1;
c906108c
SS
18572 break;
18573 case DW_AT_abstract_origin:
18574 case DW_AT_specification:
72bf9492 18575 case DW_AT_extension:
48fbe735 18576 has_specification = 1;
0826b30a 18577 spec_offset = attr.get_ref_die_offset ();
48fbe735 18578 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18579 || cu->per_cu->is_dwz);
c906108c
SS
18580 break;
18581 case DW_AT_sibling:
18582 /* Ignore absolute siblings, they might point outside of
18583 the current compile unit. */
18584 if (attr.form == DW_FORM_ref_addr)
b98664d3 18585 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18586 else
b9502d3f 18587 {
48fbe735 18588 const gdb_byte *buffer = reader->buffer;
0826b30a 18589 sect_offset off = attr.get_ref_die_offset ();
9c541725 18590 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18591
18592 if (sibling_ptr < info_ptr)
b98664d3 18593 complaint (_("DW_AT_sibling points backwards"));
22869d73 18594 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18595 reader->die_section->overflow_complaint ();
b9502d3f 18596 else
48fbe735 18597 sibling = sibling_ptr;
b9502d3f 18598 }
c906108c 18599 break;
fa4028e9 18600 case DW_AT_byte_size:
48fbe735 18601 has_byte_size = 1;
fa4028e9 18602 break;
ff908ebf 18603 case DW_AT_const_value:
48fbe735 18604 has_const_value = 1;
ff908ebf 18605 break;
68511cec
CES
18606 case DW_AT_calling_convention:
18607 /* DWARF doesn't provide a way to identify a program's source-level
18608 entry point. DW_AT_calling_convention attributes are only meant
18609 to describe functions' calling conventions.
18610
18611 However, because it's a necessary piece of information in
0c1b455e
TT
18612 Fortran, and before DWARF 4 DW_CC_program was the only
18613 piece of debugging information whose definition refers to
18614 a 'main program' at all, several compilers marked Fortran
18615 main programs with DW_CC_program --- even when those
18616 functions use the standard calling conventions.
18617
18618 Although DWARF now specifies a way to provide this
18619 information, we support this practice for backward
18620 compatibility. */
68511cec 18621 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18622 && cu->language == language_fortran)
48fbe735 18623 main_subprogram = 1;
68511cec 18624 break;
481860b3
GB
18625 case DW_AT_inline:
18626 if (DW_UNSND (&attr) == DW_INL_inlined
18627 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18628 may_be_inlined = 1;
481860b3 18629 break;
95554aad
TT
18630
18631 case DW_AT_import:
48fbe735 18632 if (tag == DW_TAG_imported_unit)
36586728 18633 {
0826b30a 18634 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18635 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18636 || cu->per_cu->is_dwz);
18637 }
95554aad
TT
18638 break;
18639
0c1b455e 18640 case DW_AT_main_subprogram:
48fbe735 18641 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18642 break;
18643
05caa1d2
TT
18644 case DW_AT_ranges:
18645 {
18646 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18647 but that requires a full DIE, so instead we just
18648 reimplement it. */
18649 int need_ranges_base = tag != DW_TAG_compile_unit;
18650 unsigned int ranges_offset = (DW_UNSND (&attr)
18651 + (need_ranges_base
18652 ? cu->ranges_base
18653 : 0));
18654
18655 /* Value of the DW_AT_ranges attribute is the offset in the
18656 .debug_ranges section. */
18657 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18658 nullptr))
18659 has_pc_info = 1;
18660 }
18661 break;
18662
c906108c
SS
18663 default:
18664 break;
18665 }
18666 }
18667
10d06d82
TT
18668 /* For Ada, if both the name and the linkage name appear, we prefer
18669 the latter. This lets "catch exception" work better, regardless
18670 of the order in which the name and linkage name were emitted.
18671 Really, though, this is just a workaround for the fact that gdb
18672 doesn't store both the name and the linkage name. */
18673 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 18674 raw_name = linkage_name;
10d06d82 18675
91da1414 18676 if (high_pc_relative)
48fbe735 18677 highpc += lowpc;
91da1414 18678
9373cf26
JK
18679 if (has_low_pc_attr && has_high_pc_attr)
18680 {
18681 /* When using the GNU linker, .gnu.linkonce. sections are used to
18682 eliminate duplicate copies of functions and vtables and such.
18683 The linker will arbitrarily choose one and discard the others.
18684 The AT_*_pc values for such functions refer to local labels in
18685 these sections. If the section from that file was discarded, the
18686 labels are not in the output, so the relocs get a value of 0.
18687 If this is a discarded function, mark the pc bounds as invalid,
18688 so that GDB will ignore it. */
976ca316 18689 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 18690 {
976ca316 18691 struct objfile *objfile = per_objfile->objfile;
08feed99 18692 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18693
b98664d3 18694 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18695 "for DIE at %s [in module %s]"),
48fbe735
YQ
18696 paddress (gdbarch, lowpc),
18697 sect_offset_str (sect_off),
9d8780f0 18698 objfile_name (objfile));
9373cf26
JK
18699 }
18700 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18701 else if (lowpc >= highpc)
9373cf26 18702 {
976ca316 18703 struct objfile *objfile = per_objfile->objfile;
08feed99 18704 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18705
b98664d3 18706 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18707 "for DIE at %s [in module %s]"),
48fbe735
YQ
18708 paddress (gdbarch, lowpc),
18709 paddress (gdbarch, highpc),
18710 sect_offset_str (sect_off),
9c541725 18711 objfile_name (objfile));
9373cf26
JK
18712 }
18713 else
48fbe735 18714 has_pc_info = 1;
9373cf26 18715 }
85cbf3d3 18716
c906108c
SS
18717 return info_ptr;
18718}
18719
72bf9492
DJ
18720/* Find a cached partial DIE at OFFSET in CU. */
18721
d590ff25
YQ
18722struct partial_die_info *
18723dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18724{
18725 struct partial_die_info *lookup_die = NULL;
6f06d47b 18726 struct partial_die_info part_die (sect_off);
72bf9492 18727
9a3c8263 18728 lookup_die = ((struct partial_die_info *)
d590ff25 18729 htab_find_with_hash (partial_dies, &part_die,
9c541725 18730 to_underlying (sect_off)));
72bf9492 18731
72bf9492
DJ
18732 return lookup_die;
18733}
18734
348e048f
DE
18735/* Find a partial DIE at OFFSET, which may or may not be in CU,
18736 except in the case of .debug_types DIEs which do not reference
18737 outside their CU (they do however referencing other types via
55f1336d 18738 DW_FORM_ref_sig8). */
72bf9492 18739
122cf0f2 18740static const struct cu_partial_die_info
9c541725 18741find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18742{
976ca316
SM
18743 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18744 struct objfile *objfile = per_objfile->objfile;
5afb4e99 18745 struct partial_die_info *pd = NULL;
72bf9492 18746
36586728 18747 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18748 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18749 {
d590ff25 18750 pd = cu->find_partial_die (sect_off);
5afb4e99 18751 if (pd != NULL)
fb816e8b 18752 return { cu, pd };
0d99eb77
DE
18753 /* We missed recording what we needed.
18754 Load all dies and try again. */
5afb4e99 18755 }
0d99eb77
DE
18756 else
18757 {
18758 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18759 if (cu->per_cu->is_debug_types)
0d99eb77 18760 {
9d8780f0
SM
18761 error (_("Dwarf Error: Type Unit at offset %s contains"
18762 " external reference to offset %s [in module %s].\n"),
18763 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18764 bfd_get_filename (objfile->obfd));
18765 }
7188ed02
SM
18766 dwarf2_per_cu_data *per_cu
18767 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 18768 per_objfile);
72bf9492 18769
976ca316 18770 cu = per_objfile->get_cu (per_cu);
7188ed02 18771 if (cu == NULL || cu->partial_dies == NULL)
976ca316 18772 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 18773
976ca316 18774 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
18775
18776 cu->last_used = 0;
18777 pd = cu->find_partial_die (sect_off);
0d99eb77 18778 }
5afb4e99 18779
dee91e82
DE
18780 /* If we didn't find it, and not all dies have been loaded,
18781 load them all and try again. */
18782
7188ed02 18783 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 18784 {
7188ed02 18785 cu->per_cu->load_all_dies = 1;
fd820528
DE
18786
18787 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18788 THIS_CU->cu may already be in use. So we can't just free it and
18789 replace its DIEs with the ones we read in. Instead, we leave those
18790 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18791 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18792 set. */
976ca316 18793 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 18794
7188ed02 18795 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18796 }
18797
18798 if (pd == NULL)
18799 internal_error (__FILE__, __LINE__,
9d8780f0 18800 _("could not find partial DIE %s "
3e43a32a 18801 "in cache [from module %s]\n"),
9d8780f0 18802 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 18803 return { cu, pd };
72bf9492
DJ
18804}
18805
abc72ce4
DE
18806/* See if we can figure out if the class lives in a namespace. We do
18807 this by looking for a member function; its demangled name will
18808 contain namespace info, if there is any. */
18809
18810static void
18811guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18812 struct dwarf2_cu *cu)
18813{
18814 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18815 what template types look like, because the demangler
18816 frequently doesn't give the same name as the debug info. We
18817 could fix this by only using the demangled name to get the
18818 prefix (but see comment in read_structure_type). */
18819
18820 struct partial_die_info *real_pdi;
18821 struct partial_die_info *child_pdi;
18822
18823 /* If this DIE (this DIE's specification, if any) has a parent, then
18824 we should not do this. We'll prepend the parent's fully qualified
18825 name when we create the partial symbol. */
18826
18827 real_pdi = struct_pdi;
18828 while (real_pdi->has_specification)
fb816e8b 18829 {
122cf0f2
AB
18830 auto res = find_partial_die (real_pdi->spec_offset,
18831 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18832 real_pdi = res.pdi;
18833 cu = res.cu;
18834 }
abc72ce4
DE
18835
18836 if (real_pdi->die_parent != NULL)
18837 return;
18838
18839 for (child_pdi = struct_pdi->die_child;
18840 child_pdi != NULL;
18841 child_pdi = child_pdi->die_sibling)
18842 {
18843 if (child_pdi->tag == DW_TAG_subprogram
18844 && child_pdi->linkage_name != NULL)
18845 {
43816ebc
TT
18846 gdb::unique_xmalloc_ptr<char> actual_class_name
18847 (language_class_name_from_physname (cu->language_defn,
18848 child_pdi->linkage_name));
abc72ce4
DE
18849 if (actual_class_name != NULL)
18850 {
5e22e966 18851 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
18852 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
18853 struct_pdi->canonical_name = 1;
abc72ce4
DE
18854 }
18855 break;
18856 }
18857 }
18858}
18859
25c11aca
TV
18860/* Return true if a DIE with TAG may have the DW_AT_const_value
18861 attribute. */
18862
18863static bool
18864can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18865{
18866 switch (tag)
18867 {
18868 case DW_TAG_constant:
18869 case DW_TAG_enumerator:
18870 case DW_TAG_formal_parameter:
18871 case DW_TAG_template_value_param:
18872 case DW_TAG_variable:
18873 return true;
18874 }
18875
18876 return false;
18877}
18878
52356b79
YQ
18879void
18880partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18881{
abc72ce4
DE
18882 /* Once we've fixed up a die, there's no point in doing so again.
18883 This also avoids a memory leak if we were to call
18884 guess_partial_die_structure_name multiple times. */
52356b79 18885 if (fixup_called)
abc72ce4
DE
18886 return;
18887
72bf9492
DJ
18888 /* If we found a reference attribute and the DIE has no name, try
18889 to find a name in the referred to DIE. */
18890
7d00ffec 18891 if (raw_name == NULL && has_specification)
72bf9492
DJ
18892 {
18893 struct partial_die_info *spec_die;
72bf9492 18894
122cf0f2 18895 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18896 spec_die = res.pdi;
18897 cu = res.cu;
72bf9492 18898
52356b79 18899 spec_die->fixup (cu);
72bf9492 18900
7d00ffec 18901 if (spec_die->raw_name)
72bf9492 18902 {
7d00ffec
TT
18903 raw_name = spec_die->raw_name;
18904 canonical_name = spec_die->canonical_name;
72bf9492
DJ
18905
18906 /* Copy DW_AT_external attribute if it is set. */
18907 if (spec_die->is_external)
52356b79 18908 is_external = spec_die->is_external;
72bf9492
DJ
18909 }
18910 }
18911
25c11aca
TV
18912 if (!has_const_value && has_specification
18913 && can_have_DW_AT_const_value_p (tag))
18914 {
18915 struct partial_die_info *spec_die;
18916
18917 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18918 spec_die = res.pdi;
18919 cu = res.cu;
18920
18921 spec_die->fixup (cu);
18922
18923 if (spec_die->has_const_value)
18924 {
18925 /* Copy DW_AT_const_value attribute if it is set. */
18926 has_const_value = spec_die->has_const_value;
18927 }
18928 }
18929
72bf9492 18930 /* Set default names for some unnamed DIEs. */
72bf9492 18931
7d00ffec
TT
18932 if (raw_name == NULL && tag == DW_TAG_namespace)
18933 {
18934 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
18935 canonical_name = 1;
18936 }
72bf9492 18937
abc72ce4
DE
18938 /* If there is no parent die to provide a namespace, and there are
18939 children, see if we can determine the namespace from their linkage
122d1940 18940 name. */
abc72ce4 18941 if (cu->language == language_cplus
5e22e966 18942 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18943 && die_parent == NULL
18944 && has_children
18945 && (tag == DW_TAG_class_type
18946 || tag == DW_TAG_structure_type
18947 || tag == DW_TAG_union_type))
18948 guess_partial_die_structure_name (this, cu);
abc72ce4 18949
53832f31
TT
18950 /* GCC might emit a nameless struct or union that has a linkage
18951 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 18952 if (raw_name == NULL
52356b79
YQ
18953 && (tag == DW_TAG_class_type
18954 || tag == DW_TAG_interface_type
18955 || tag == DW_TAG_structure_type
18956 || tag == DW_TAG_union_type)
18957 && linkage_name != NULL)
53832f31 18958 {
43816ebc
TT
18959 gdb::unique_xmalloc_ptr<char> demangled
18960 (gdb_demangle (linkage_name, DMGL_TYPES));
18961 if (demangled != nullptr)
53832f31 18962 {
96408a79
SA
18963 const char *base;
18964
18965 /* Strip any leading namespaces/classes, keep only the base name.
18966 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18967 base = strrchr (demangled.get (), ':');
18968 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18969 base++;
18970 else
43816ebc 18971 base = demangled.get ();
96408a79 18972
5e22e966 18973 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
18974 raw_name = objfile->intern (base);
18975 canonical_name = 1;
53832f31
TT
18976 }
18977 }
18978
52356b79 18979 fixup_called = 1;
72bf9492
DJ
18980}
18981
41144253 18982/* Read the .debug_loclists header contents from the given SECTION in the
18983 HEADER. */
18984static void
18985read_loclist_header (struct loclist_header *header,
18986 struct dwarf2_section_info *section)
18987{
18988 unsigned int bytes_read;
18989 bfd *abfd = section->get_bfd_owner ();
18990 const gdb_byte *info_ptr = section->buffer;
18991 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18992 info_ptr += bytes_read;
18993 header->version = read_2_bytes (abfd, info_ptr);
18994 info_ptr += 2;
18995 header->addr_size = read_1_byte (abfd, info_ptr);
18996 info_ptr += 1;
18997 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18998 info_ptr += 1;
18999 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19000}
19001
19002/* Return the DW_AT_loclists_base value for the CU. */
19003static ULONGEST
19004lookup_loclist_base (struct dwarf2_cu *cu)
19005{
19006 /* For the .dwo unit, the loclist_base points to the first offset following
19007 the header. The header consists of the following entities-
19008 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19009 bit format)
19010 2. version (2 bytes)
19011 3. address size (1 byte)
19012 4. segment selector size (1 byte)
19013 5. offset entry count (4 bytes)
19014 These sizes are derived as per the DWARFv5 standard. */
19015 if (cu->dwo_unit != nullptr)
19016 {
19017 if (cu->header.initial_length_size == 4)
19018 return LOCLIST_HEADER_SIZE32;
19019 return LOCLIST_HEADER_SIZE64;
19020 }
19021 return cu->loclist_base;
19022}
19023
19024/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19025 array of offsets in the .debug_loclists section. */
19026static CORE_ADDR
19027read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19028{
976ca316
SM
19029 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19030 struct objfile *objfile = per_objfile->objfile;
41144253 19031 bfd *abfd = objfile->obfd;
19032 ULONGEST loclist_base = lookup_loclist_base (cu);
19033 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19034
19035 section->read (objfile);
19036 if (section->buffer == NULL)
19037 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19038 "section [in module %s]"), objfile_name (objfile));
19039 struct loclist_header header;
19040 read_loclist_header (&header, section);
19041 if (loclist_index >= header.offset_entry_count)
19042 complaint (_("DW_FORM_loclistx pointing outside of "
19043 ".debug_loclists offset array [in module %s]"),
19044 objfile_name (objfile));
19045 if (loclist_base + loclist_index * cu->header.offset_size
19046 >= section->size)
19047 complaint (_("DW_FORM_loclistx pointing outside of "
19048 ".debug_loclists section [in module %s]"),
19049 objfile_name (objfile));
19050 const gdb_byte *info_ptr
19051 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19052
19053 if (cu->header.offset_size == 4)
19054 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19055 else
19056 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19057}
19058
18a8505e
AT
19059/* Process the attributes that had to be skipped in the first round. These
19060 attributes are the ones that need str_offsets_base or addr_base attributes.
19061 They could not have been processed in the first round, because at the time
19062 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19063static void
19064read_attribute_reprocess (const struct die_reader_specs *reader,
19065 struct attribute *attr)
18a8505e
AT
19066{
19067 struct dwarf2_cu *cu = reader->cu;
19068 switch (attr->form)
19069 {
19070 case DW_FORM_addrx:
19071 case DW_FORM_GNU_addr_index:
19072 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19073 break;
41144253 19074 case DW_FORM_loclistx:
19075 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19076 break;
18a8505e
AT
19077 case DW_FORM_strx:
19078 case DW_FORM_strx1:
19079 case DW_FORM_strx2:
19080 case DW_FORM_strx3:
19081 case DW_FORM_strx4:
19082 case DW_FORM_GNU_str_index:
19083 {
19084 unsigned int str_index = DW_UNSND (attr);
19085 if (reader->dwo_file != NULL)
19086 {
19087 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19088 DW_STRING_IS_CANONICAL (attr) = 0;
19089 }
19090 else
19091 {
19092 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19093 DW_STRING_IS_CANONICAL (attr) = 0;
19094 }
19095 break;
19096 }
19097 default:
19098 gdb_assert_not_reached (_("Unexpected DWARF form."));
19099 }
19100}
19101
a8329558 19102/* Read an attribute value described by an attribute form. */
c906108c 19103
d521ce57 19104static const gdb_byte *
dee91e82
DE
19105read_attribute_value (const struct die_reader_specs *reader,
19106 struct attribute *attr, unsigned form,
18a8505e
AT
19107 LONGEST implicit_const, const gdb_byte *info_ptr,
19108 bool *need_reprocess)
c906108c 19109{
dee91e82 19110 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19111 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19112 struct objfile *objfile = per_objfile->objfile;
dee91e82 19113 bfd *abfd = reader->abfd;
e7c27a73 19114 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19115 unsigned int bytes_read;
19116 struct dwarf_block *blk;
18a8505e 19117 *need_reprocess = false;
c906108c 19118
aead7601 19119 attr->form = (enum dwarf_form) form;
a8329558 19120 switch (form)
c906108c 19121 {
c906108c 19122 case DW_FORM_ref_addr:
ae411497 19123 if (cu->header.version == 2)
c8a7a66f
TT
19124 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19125 &bytes_read);
ae411497 19126 else
8266302d
TT
19127 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19128 &bytes_read);
ae411497
TT
19129 info_ptr += bytes_read;
19130 break;
36586728 19131 case DW_FORM_GNU_ref_alt:
8266302d 19132 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19133 info_ptr += bytes_read;
19134 break;
ae411497 19135 case DW_FORM_addr:
08feed99
TT
19136 {
19137 struct gdbarch *gdbarch = objfile->arch ();
19138 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19139 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19140 info_ptr += bytes_read;
19141 }
c906108c
SS
19142 break;
19143 case DW_FORM_block2:
7b5a2f43 19144 blk = dwarf_alloc_block (cu);
c906108c
SS
19145 blk->size = read_2_bytes (abfd, info_ptr);
19146 info_ptr += 2;
19147 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19148 info_ptr += blk->size;
19149 DW_BLOCK (attr) = blk;
19150 break;
19151 case DW_FORM_block4:
7b5a2f43 19152 blk = dwarf_alloc_block (cu);
c906108c
SS
19153 blk->size = read_4_bytes (abfd, info_ptr);
19154 info_ptr += 4;
19155 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19156 info_ptr += blk->size;
19157 DW_BLOCK (attr) = blk;
19158 break;
19159 case DW_FORM_data2:
19160 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19161 info_ptr += 2;
19162 break;
19163 case DW_FORM_data4:
19164 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19165 info_ptr += 4;
19166 break;
19167 case DW_FORM_data8:
19168 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19169 info_ptr += 8;
19170 break;
0224619f
JK
19171 case DW_FORM_data16:
19172 blk = dwarf_alloc_block (cu);
19173 blk->size = 16;
19174 blk->data = read_n_bytes (abfd, info_ptr, 16);
19175 info_ptr += 16;
19176 DW_BLOCK (attr) = blk;
19177 break;
2dc7f7b3 19178 case DW_FORM_sec_offset:
8266302d 19179 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19180 info_ptr += bytes_read;
19181 break;
41144253 19182 case DW_FORM_loclistx:
19183 {
19184 *need_reprocess = true;
19185 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19186 info_ptr += bytes_read;
19187 }
19188 break;
c906108c 19189 case DW_FORM_string:
9b1c24c8 19190 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19191 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19192 info_ptr += bytes_read;
19193 break;
4bdf3d34 19194 case DW_FORM_strp:
36586728
TT
19195 if (!cu->per_cu->is_dwz)
19196 {
976ca316 19197 DW_STRING (attr) = read_indirect_string (per_objfile,
ed2dc618 19198 abfd, info_ptr, cu_header,
36586728
TT
19199 &bytes_read);
19200 DW_STRING_IS_CANONICAL (attr) = 0;
19201 info_ptr += bytes_read;
19202 break;
19203 }
19204 /* FALLTHROUGH */
43988095
JK
19205 case DW_FORM_line_strp:
19206 if (!cu->per_cu->is_dwz)
19207 {
976ca316
SM
19208 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19209 &bytes_read);
43988095
JK
19210 DW_STRING_IS_CANONICAL (attr) = 0;
19211 info_ptr += bytes_read;
19212 break;
19213 }
19214 /* FALLTHROUGH */
36586728
TT
19215 case DW_FORM_GNU_strp_alt:
19216 {
976ca316 19217 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
19218 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19219 &bytes_read);
36586728 19220
0314b390 19221 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19222 DW_STRING_IS_CANONICAL (attr) = 0;
19223 info_ptr += bytes_read;
19224 }
4bdf3d34 19225 break;
2dc7f7b3 19226 case DW_FORM_exprloc:
c906108c 19227 case DW_FORM_block:
7b5a2f43 19228 blk = dwarf_alloc_block (cu);
c906108c
SS
19229 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19230 info_ptr += bytes_read;
19231 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19232 info_ptr += blk->size;
19233 DW_BLOCK (attr) = blk;
19234 break;
19235 case DW_FORM_block1:
7b5a2f43 19236 blk = dwarf_alloc_block (cu);
c906108c
SS
19237 blk->size = read_1_byte (abfd, info_ptr);
19238 info_ptr += 1;
19239 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19240 info_ptr += blk->size;
19241 DW_BLOCK (attr) = blk;
19242 break;
19243 case DW_FORM_data1:
19244 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19245 info_ptr += 1;
19246 break;
19247 case DW_FORM_flag:
19248 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19249 info_ptr += 1;
19250 break;
2dc7f7b3
TT
19251 case DW_FORM_flag_present:
19252 DW_UNSND (attr) = 1;
19253 break;
c906108c
SS
19254 case DW_FORM_sdata:
19255 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19256 info_ptr += bytes_read;
19257 break;
19258 case DW_FORM_udata:
18a8505e 19259 case DW_FORM_rnglistx:
c906108c
SS
19260 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19261 info_ptr += bytes_read;
19262 break;
19263 case DW_FORM_ref1:
9c541725 19264 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19265 + read_1_byte (abfd, info_ptr));
c906108c
SS
19266 info_ptr += 1;
19267 break;
19268 case DW_FORM_ref2:
9c541725 19269 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19270 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19271 info_ptr += 2;
19272 break;
19273 case DW_FORM_ref4:
9c541725 19274 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19275 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19276 info_ptr += 4;
19277 break;
613e1657 19278 case DW_FORM_ref8:
9c541725 19279 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19280 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19281 info_ptr += 8;
19282 break;
55f1336d 19283 case DW_FORM_ref_sig8:
ac9ec31b 19284 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19285 info_ptr += 8;
19286 break;
c906108c 19287 case DW_FORM_ref_udata:
9c541725 19288 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19289 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19290 info_ptr += bytes_read;
19291 break;
c906108c 19292 case DW_FORM_indirect:
a8329558
KW
19293 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19294 info_ptr += bytes_read;
43988095
JK
19295 if (form == DW_FORM_implicit_const)
19296 {
19297 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19298 info_ptr += bytes_read;
19299 }
19300 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19301 info_ptr, need_reprocess);
43988095
JK
19302 break;
19303 case DW_FORM_implicit_const:
19304 DW_SND (attr) = implicit_const;
a8329558 19305 break;
336d760d 19306 case DW_FORM_addrx:
3019eac3 19307 case DW_FORM_GNU_addr_index:
18a8505e
AT
19308 *need_reprocess = true;
19309 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19310 info_ptr += bytes_read;
19311 break;
cf532bd1 19312 case DW_FORM_strx:
15f18d14
AT
19313 case DW_FORM_strx1:
19314 case DW_FORM_strx2:
19315 case DW_FORM_strx3:
19316 case DW_FORM_strx4:
3019eac3 19317 case DW_FORM_GNU_str_index:
3019eac3 19318 {
15f18d14
AT
19319 ULONGEST str_index;
19320 if (form == DW_FORM_strx1)
19321 {
19322 str_index = read_1_byte (abfd, info_ptr);
19323 info_ptr += 1;
19324 }
19325 else if (form == DW_FORM_strx2)
19326 {
19327 str_index = read_2_bytes (abfd, info_ptr);
19328 info_ptr += 2;
19329 }
19330 else if (form == DW_FORM_strx3)
19331 {
19332 str_index = read_3_bytes (abfd, info_ptr);
19333 info_ptr += 3;
19334 }
19335 else if (form == DW_FORM_strx4)
19336 {
19337 str_index = read_4_bytes (abfd, info_ptr);
19338 info_ptr += 4;
19339 }
19340 else
19341 {
19342 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19343 info_ptr += bytes_read;
19344 }
18a8505e
AT
19345 *need_reprocess = true;
19346 DW_UNSND (attr) = str_index;
19347 }
3019eac3 19348 break;
c906108c 19349 default:
8a3fe4f8 19350 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19351 dwarf_form_name (form),
19352 bfd_get_filename (abfd));
c906108c 19353 }
28e94949 19354
36586728 19355 /* Super hack. */
cd6c91b4 19356 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19357 attr->form = DW_FORM_GNU_ref_alt;
19358
28e94949
JB
19359 /* We have seen instances where the compiler tried to emit a byte
19360 size attribute of -1 which ended up being encoded as an unsigned
19361 0xffffffff. Although 0xffffffff is technically a valid size value,
19362 an object of this size seems pretty unlikely so we can relatively
19363 safely treat these cases as if the size attribute was invalid and
19364 treat them as zero by default. */
19365 if (attr->name == DW_AT_byte_size
19366 && form == DW_FORM_data4
19367 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19368 {
19369 complaint
b98664d3 19370 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19371 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19372 DW_UNSND (attr) = 0;
19373 }
28e94949 19374
c906108c
SS
19375 return info_ptr;
19376}
19377
a8329558
KW
19378/* Read an attribute described by an abbreviated attribute. */
19379
d521ce57 19380static const gdb_byte *
dee91e82
DE
19381read_attribute (const struct die_reader_specs *reader,
19382 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19383 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19384{
19385 attr->name = abbrev->name;
43988095 19386 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19387 abbrev->implicit_const, info_ptr,
19388 need_reprocess);
a8329558
KW
19389}
19390
43988095
JK
19391/* Return pointer to string at .debug_str offset STR_OFFSET. */
19392
19393static const char *
976ca316 19394read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 19395 LONGEST str_offset)
43988095 19396{
976ca316
SM
19397 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19398 str_offset, "DW_FORM_strp");
c906108c
SS
19399}
19400
43988095
JK
19401/* Return pointer to string at .debug_str offset as read from BUF.
19402 BUF is assumed to be in a compilation unit described by CU_HEADER.
19403 Return *BYTES_READ_PTR count of bytes read from BUF. */
19404
d521ce57 19405static const char *
976ca316 19406read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 19407 const gdb_byte *buf,
cf2c3c16
TT
19408 const struct comp_unit_head *cu_header,
19409 unsigned int *bytes_read_ptr)
19410{
8266302d 19411 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19412
976ca316 19413 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
19414}
19415
86c0bb4c 19416/* See read.h. */
43988095 19417
86c0bb4c
TT
19418const char *
19419dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19420 const struct comp_unit_head *cu_header,
19421 unsigned int *bytes_read_ptr)
43988095 19422{
86c0bb4c 19423 bfd *abfd = objfile->obfd;
8266302d 19424 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19425
5989a64e 19426 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19427}
19428
3019eac3 19429/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19430 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19431 ADDR_SIZE is the size of addresses from the CU header. */
19432
19433static CORE_ADDR
976ca316
SM
19434read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19435 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 19436{
976ca316 19437 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
19438 bfd *abfd = objfile->obfd;
19439 const gdb_byte *info_ptr;
18a8505e 19440 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19441
976ca316
SM
19442 per_objfile->per_bfd->addr.read (objfile);
19443 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19444 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19445 objfile_name (objfile));
18a8505e 19446 if (addr_base_or_zero + addr_index * addr_size
976ca316 19447 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
19448 error (_("DW_FORM_addr_index pointing outside of "
19449 ".debug_addr section [in module %s]"),
4262abfb 19450 objfile_name (objfile));
976ca316
SM
19451 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19452 + addr_index * addr_size);
3019eac3
DE
19453 if (addr_size == 4)
19454 return bfd_get_32 (abfd, info_ptr);
19455 else
19456 return bfd_get_64 (abfd, info_ptr);
19457}
19458
19459/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19460
19461static CORE_ADDR
19462read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19463{
5e22e966 19464 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19465 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19466}
19467
19468/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19469
19470static CORE_ADDR
d521ce57 19471read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19472 unsigned int *bytes_read)
19473{
5e22e966 19474 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19475 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19476
19477 return read_addr_index (cu, addr_index);
19478}
19479
450a1bfc 19480/* See read.h. */
3019eac3
DE
19481
19482CORE_ADDR
82ca3f51 19483dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 19484 dwarf2_per_objfile *per_objfile,
82ca3f51 19485 unsigned int addr_index)
3019eac3 19486{
976ca316 19487 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 19488 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19489 int addr_size;
19490
3019eac3
DE
19491 /* We need addr_base and addr_size.
19492 If we don't have PER_CU->cu, we have to get it.
19493 Nasty, but the alternative is storing the needed info in PER_CU,
19494 which at this point doesn't seem justified: it's not clear how frequently
19495 it would get used and it would increase the size of every PER_CU.
19496 Entry points like dwarf2_per_cu_addr_size do a similar thing
19497 so we're not in uncharted territory here.
19498 Alas we need to be a bit more complicated as addr_base is contained
19499 in the DIE.
19500
19501 We don't need to read the entire CU(/TU).
19502 We just need the header and top level die.
a1b64ce1 19503
3019eac3 19504 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19505 For now we skip this optimization. */
3019eac3
DE
19506
19507 if (cu != NULL)
19508 {
19509 addr_base = cu->addr_base;
19510 addr_size = cu->header.addr_size;
19511 }
19512 else
19513 {
976ca316 19514 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
19515 addr_base = reader.cu->addr_base;
19516 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19517 }
19518
976ca316 19519 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
19520}
19521
18a8505e
AT
19522/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19523 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19524 DWO file. */
3019eac3 19525
d521ce57 19526static const char *
18a8505e
AT
19527read_str_index (struct dwarf2_cu *cu,
19528 struct dwarf2_section_info *str_section,
19529 struct dwarf2_section_info *str_offsets_section,
19530 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19531{
976ca316
SM
19532 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19533 struct objfile *objfile = per_objfile->objfile;
c5164cbc 19534 const char *objf_name = objfile_name (objfile);
3019eac3 19535 bfd *abfd = objfile->obfd;
d521ce57 19536 const gdb_byte *info_ptr;
3019eac3 19537 ULONGEST str_offset;
cf532bd1 19538 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19539
96b79293
TT
19540 str_section->read (objfile);
19541 str_offsets_section->read (objfile);
73869dc2 19542 if (str_section->buffer == NULL)
18a8505e 19543 error (_("%s used without %s section"
9d8780f0 19544 " in CU at offset %s [in module %s]"),
96b79293 19545 form_name, str_section->get_name (),
18a8505e 19546 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19547 if (str_offsets_section->buffer == NULL)
18a8505e 19548 error (_("%s used without %s section"
9d8780f0 19549 " in CU at offset %s [in module %s]"),
96b79293 19550 form_name, str_section->get_name (),
18a8505e 19551 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19552 info_ptr = (str_offsets_section->buffer
18a8505e 19553 + str_offsets_base
3019eac3
DE
19554 + str_index * cu->header.offset_size);
19555 if (cu->header.offset_size == 4)
19556 str_offset = bfd_get_32 (abfd, info_ptr);
19557 else
19558 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19559 if (str_offset >= str_section->size)
57d63ce2 19560 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19561 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19562 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19563 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19564}
19565
18a8505e
AT
19566/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19567
19568static const char *
19569read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19570{
19571 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19572 ? reader->cu->header.addr_size : 0;
19573 return read_str_index (reader->cu,
19574 &reader->dwo_file->sections.str,
19575 &reader->dwo_file->sections.str_offsets,
19576 str_offsets_base, str_index);
19577}
19578
19579/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19580
19581static const char *
19582read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19583{
5e22e966 19584 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19585 const char *objf_name = objfile_name (objfile);
19586 static const char form_name[] = "DW_FORM_GNU_str_index";
19587 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19588
19589 if (!cu->str_offsets_base.has_value ())
19590 error (_("%s used in Fission stub without %s"
19591 " in CU at offset 0x%lx [in module %s]"),
19592 form_name, str_offsets_attr_name,
19593 (long) cu->header.offset_size, objf_name);
19594
19595 return read_str_index (cu,
5e22e966
SM
19596 &cu->per_objfile->per_bfd->str,
19597 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19598 *cu->str_offsets_base, str_index);
19599}
19600
3019eac3
DE
19601/* Return the length of an LEB128 number in BUF. */
19602
19603static int
19604leb128_size (const gdb_byte *buf)
19605{
19606 const gdb_byte *begin = buf;
19607 gdb_byte byte;
19608
19609 while (1)
19610 {
19611 byte = *buf++;
19612 if ((byte & 128) == 0)
19613 return buf - begin;
19614 }
19615}
19616
c906108c 19617static void
e142c38c 19618set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19619{
19620 switch (lang)
19621 {
19622 case DW_LANG_C89:
76bee0cc 19623 case DW_LANG_C99:
0cfd832f 19624 case DW_LANG_C11:
c906108c 19625 case DW_LANG_C:
d1be3247 19626 case DW_LANG_UPC:
e142c38c 19627 cu->language = language_c;
c906108c 19628 break;
9c37b5ae 19629 case DW_LANG_Java:
c906108c 19630 case DW_LANG_C_plus_plus:
0cfd832f
MW
19631 case DW_LANG_C_plus_plus_11:
19632 case DW_LANG_C_plus_plus_14:
e142c38c 19633 cu->language = language_cplus;
c906108c 19634 break;
6aecb9c2
JB
19635 case DW_LANG_D:
19636 cu->language = language_d;
19637 break;
c906108c
SS
19638 case DW_LANG_Fortran77:
19639 case DW_LANG_Fortran90:
b21b22e0 19640 case DW_LANG_Fortran95:
f7de9aab
MW
19641 case DW_LANG_Fortran03:
19642 case DW_LANG_Fortran08:
e142c38c 19643 cu->language = language_fortran;
c906108c 19644 break;
a766d390
DE
19645 case DW_LANG_Go:
19646 cu->language = language_go;
19647 break;
c906108c 19648 case DW_LANG_Mips_Assembler:
e142c38c 19649 cu->language = language_asm;
c906108c
SS
19650 break;
19651 case DW_LANG_Ada83:
8aaf0b47 19652 case DW_LANG_Ada95:
bc5f45f8
JB
19653 cu->language = language_ada;
19654 break;
72019c9c
GM
19655 case DW_LANG_Modula2:
19656 cu->language = language_m2;
19657 break;
fe8e67fd
PM
19658 case DW_LANG_Pascal83:
19659 cu->language = language_pascal;
19660 break;
22566fbd
DJ
19661 case DW_LANG_ObjC:
19662 cu->language = language_objc;
19663 break;
c44af4eb
TT
19664 case DW_LANG_Rust:
19665 case DW_LANG_Rust_old:
19666 cu->language = language_rust;
19667 break;
c906108c
SS
19668 case DW_LANG_Cobol74:
19669 case DW_LANG_Cobol85:
c906108c 19670 default:
e142c38c 19671 cu->language = language_minimal;
c906108c
SS
19672 break;
19673 }
e142c38c 19674 cu->language_defn = language_def (cu->language);
c906108c
SS
19675}
19676
19677/* Return the named attribute or NULL if not there. */
19678
19679static struct attribute *
e142c38c 19680dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19681{
a48e046c 19682 for (;;)
c906108c 19683 {
a48e046c
TT
19684 unsigned int i;
19685 struct attribute *spec = NULL;
19686
19687 for (i = 0; i < die->num_attrs; ++i)
19688 {
19689 if (die->attrs[i].name == name)
19690 return &die->attrs[i];
19691 if (die->attrs[i].name == DW_AT_specification
19692 || die->attrs[i].name == DW_AT_abstract_origin)
19693 spec = &die->attrs[i];
19694 }
19695
19696 if (!spec)
19697 break;
c906108c 19698
f2f0e013 19699 die = follow_die_ref (die, spec, &cu);
f2f0e013 19700 }
c5aa993b 19701
c906108c
SS
19702 return NULL;
19703}
19704
7d45c7c3
KB
19705/* Return the string associated with a string-typed attribute, or NULL if it
19706 is either not found or is of an incorrect type. */
19707
19708static const char *
19709dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19710{
19711 struct attribute *attr;
19712 const char *str = NULL;
19713
19714 attr = dwarf2_attr (die, name, cu);
19715
19716 if (attr != NULL)
19717 {
e61108c9
TT
19718 str = attr->value_as_string ();
19719 if (str == nullptr)
b98664d3 19720 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19721 "DIE at %s in module %s"),
19722 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19723 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19724 }
19725
19726 return str;
19727}
19728
a084a2a6 19729/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19730 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19731static const char *
19732dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19733{
19734 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19735 if (dwo_name == nullptr)
19736 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19737 return dwo_name;
19738}
19739
05cf31d1
JB
19740/* Return non-zero iff the attribute NAME is defined for the given DIE,
19741 and holds a non-zero value. This function should only be used for
2dc7f7b3 19742 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19743
19744static int
19745dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19746{
19747 struct attribute *attr = dwarf2_attr (die, name, cu);
19748
19749 return (attr && DW_UNSND (attr));
19750}
19751
3ca72b44 19752static int
e142c38c 19753die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19754{
05cf31d1
JB
19755 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19756 which value is non-zero. However, we have to be careful with
19757 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19758 (via dwarf2_flag_true_p) follows this attribute. So we may
19759 end up accidently finding a declaration attribute that belongs
19760 to a different DIE referenced by the specification attribute,
19761 even though the given DIE does not have a declaration attribute. */
19762 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19763 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19764}
19765
63d06c5c 19766/* Return the die giving the specification for DIE, if there is
f2f0e013 19767 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19768 containing the return value on output. If there is no
19769 specification, but there is an abstract origin, that is
19770 returned. */
63d06c5c
DC
19771
19772static struct die_info *
f2f0e013 19773die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19774{
f2f0e013
DJ
19775 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19776 *spec_cu);
63d06c5c 19777
edb3359d
DJ
19778 if (spec_attr == NULL)
19779 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19780
63d06c5c
DC
19781 if (spec_attr == NULL)
19782 return NULL;
19783 else
f2f0e013 19784 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19785}
c906108c 19786
527f3840
JK
19787/* Stub for free_line_header to match void * callback types. */
19788
19789static void
19790free_line_header_voidp (void *arg)
19791{
9a3c8263 19792 struct line_header *lh = (struct line_header *) arg;
527f3840 19793
fff8551c 19794 delete lh;
527f3840
JK
19795}
19796
83769d0b 19797/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19798
19799static struct dwarf2_section_info *
19800get_debug_line_section (struct dwarf2_cu *cu)
19801{
19802 struct dwarf2_section_info *section;
976ca316 19803 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
19804
19805 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19806 DWO file. */
19807 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19808 section = &cu->dwo_unit->dwo_file->sections.line;
19809 else if (cu->per_cu->is_dwz)
19810 {
976ca316 19811 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
19812
19813 section = &dwz->line;
19814 }
19815 else
976ca316 19816 section = &per_objfile->per_bfd->line;
36586728
TT
19817
19818 return section;
19819}
19820
debd256d 19821/* Read the statement program header starting at OFFSET in
3019eac3 19822 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19823 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19824 Returns NULL if there is a problem reading the header, e.g., if it
19825 has a version we don't understand.
debd256d
JB
19826
19827 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19828 the returned object point into the dwarf line section buffer,
19829 and must not be freed. */
ae2de4f8 19830
fff8551c 19831static line_header_up
9c541725 19832dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19833{
3019eac3 19834 struct dwarf2_section_info *section;
976ca316 19835 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 19836
36586728 19837 section = get_debug_line_section (cu);
976ca316 19838 section->read (per_objfile->objfile);
3019eac3 19839 if (section->buffer == NULL)
debd256d 19840 {
3019eac3 19841 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19842 complaint (_("missing .debug_line.dwo section"));
3019eac3 19843 else
b98664d3 19844 complaint (_("missing .debug_line section"));
debd256d
JB
19845 return 0;
19846 }
19847
0df7ad3a 19848 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 19849 per_objfile, section, &cu->header);
debd256d 19850}
c906108c 19851
c6da4cef 19852/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19853 Return the file name of the psymtab for the given file_entry.
c6da4cef 19854 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19855 If space for the result is malloc'd, *NAME_HOLDER will be set.
19856 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19857
d521ce57 19858static const char *
7ba99d21 19859psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19860 const dwarf2_psymtab *pst,
c89b44cd
TT
19861 const char *comp_dir,
19862 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19863{
d521ce57
TT
19864 const char *include_name = fe.name;
19865 const char *include_name_to_compare = include_name;
72b9f47f 19866 const char *pst_filename;
c6da4cef
DE
19867 int file_is_pst;
19868
8c43009f 19869 const char *dir_name = fe.include_dir (lh);
c6da4cef 19870
c89b44cd 19871 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19872 if (!IS_ABSOLUTE_PATH (include_name)
19873 && (dir_name != NULL || comp_dir != NULL))
19874 {
19875 /* Avoid creating a duplicate psymtab for PST.
19876 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19877 Before we do the comparison, however, we need to account
19878 for DIR_NAME and COMP_DIR.
19879 First prepend dir_name (if non-NULL). If we still don't
19880 have an absolute path prepend comp_dir (if non-NULL).
19881 However, the directory we record in the include-file's
19882 psymtab does not contain COMP_DIR (to match the
19883 corresponding symtab(s)).
19884
19885 Example:
19886
19887 bash$ cd /tmp
19888 bash$ gcc -g ./hello.c
19889 include_name = "hello.c"
19890 dir_name = "."
19891 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19892 DW_AT_name = "./hello.c"
19893
19894 */
c6da4cef
DE
19895
19896 if (dir_name != NULL)
19897 {
c89b44cd
TT
19898 name_holder->reset (concat (dir_name, SLASH_STRING,
19899 include_name, (char *) NULL));
19900 include_name = name_holder->get ();
c6da4cef 19901 include_name_to_compare = include_name;
c6da4cef
DE
19902 }
19903 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19904 {
c89b44cd
TT
19905 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19906 include_name, (char *) NULL));
19907 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19908 }
19909 }
19910
19911 pst_filename = pst->filename;
c89b44cd 19912 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19913 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19914 {
c89b44cd
TT
19915 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19916 pst_filename, (char *) NULL));
19917 pst_filename = copied_name.get ();
c6da4cef
DE
19918 }
19919
1e3fad37 19920 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19921
c6da4cef
DE
19922 if (file_is_pst)
19923 return NULL;
19924 return include_name;
19925}
19926
d9b3de22
DE
19927/* State machine to track the state of the line number program. */
19928
6f77053d 19929class lnp_state_machine
d9b3de22 19930{
6f77053d
PA
19931public:
19932 /* Initialize a machine state for the start of a line number
19933 program. */
804d2729
TT
19934 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19935 bool record_lines_p);
6f77053d 19936
8c43009f
PA
19937 file_entry *current_file ()
19938 {
19939 /* lh->file_names is 0-based, but the file name numbers in the
19940 statement program are 1-based. */
6f77053d
PA
19941 return m_line_header->file_name_at (m_file);
19942 }
19943
19944 /* Record the line in the state machine. END_SEQUENCE is true if
19945 we're processing the end of a sequence. */
19946 void record_line (bool end_sequence);
19947
7ab6656f
OJ
19948 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19949 nop-out rest of the lines in this sequence. */
6f77053d
PA
19950 void check_line_address (struct dwarf2_cu *cu,
19951 const gdb_byte *line_ptr,
7ab6656f 19952 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19953
19954 void handle_set_discriminator (unsigned int discriminator)
19955 {
19956 m_discriminator = discriminator;
19957 m_line_has_non_zero_discriminator |= discriminator != 0;
19958 }
19959
19960 /* Handle DW_LNE_set_address. */
19961 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19962 {
19963 m_op_index = 0;
19964 address += baseaddr;
19965 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19966 }
19967
19968 /* Handle DW_LNS_advance_pc. */
19969 void handle_advance_pc (CORE_ADDR adjust);
19970
19971 /* Handle a special opcode. */
19972 void handle_special_opcode (unsigned char op_code);
19973
19974 /* Handle DW_LNS_advance_line. */
19975 void handle_advance_line (int line_delta)
19976 {
19977 advance_line (line_delta);
19978 }
19979
19980 /* Handle DW_LNS_set_file. */
19981 void handle_set_file (file_name_index file);
19982
19983 /* Handle DW_LNS_negate_stmt. */
19984 void handle_negate_stmt ()
19985 {
19986 m_is_stmt = !m_is_stmt;
19987 }
19988
19989 /* Handle DW_LNS_const_add_pc. */
19990 void handle_const_add_pc ();
19991
19992 /* Handle DW_LNS_fixed_advance_pc. */
19993 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19994 {
19995 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19996 m_op_index = 0;
19997 }
19998
19999 /* Handle DW_LNS_copy. */
20000 void handle_copy ()
20001 {
20002 record_line (false);
20003 m_discriminator = 0;
20004 }
20005
20006 /* Handle DW_LNE_end_sequence. */
20007 void handle_end_sequence ()
20008 {
804d2729 20009 m_currently_recording_lines = true;
6f77053d
PA
20010 }
20011
20012private:
20013 /* Advance the line by LINE_DELTA. */
20014 void advance_line (int line_delta)
20015 {
20016 m_line += line_delta;
20017
20018 if (line_delta != 0)
20019 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20020 }
20021
804d2729
TT
20022 struct dwarf2_cu *m_cu;
20023
6f77053d
PA
20024 gdbarch *m_gdbarch;
20025
20026 /* True if we're recording lines.
20027 Otherwise we're building partial symtabs and are just interested in
20028 finding include files mentioned by the line number program. */
20029 bool m_record_lines_p;
20030
8c43009f 20031 /* The line number header. */
6f77053d 20032 line_header *m_line_header;
8c43009f 20033
6f77053d
PA
20034 /* These are part of the standard DWARF line number state machine,
20035 and initialized according to the DWARF spec. */
d9b3de22 20036
6f77053d 20037 unsigned char m_op_index = 0;
7ba99d21
AT
20038 /* The line table index of the current file. */
20039 file_name_index m_file = 1;
6f77053d
PA
20040 unsigned int m_line = 1;
20041
20042 /* These are initialized in the constructor. */
20043
20044 CORE_ADDR m_address;
20045 bool m_is_stmt;
20046 unsigned int m_discriminator;
d9b3de22
DE
20047
20048 /* Additional bits of state we need to track. */
20049
20050 /* The last file that we called dwarf2_start_subfile for.
20051 This is only used for TLLs. */
6f77053d 20052 unsigned int m_last_file = 0;
d9b3de22 20053 /* The last file a line number was recorded for. */
6f77053d 20054 struct subfile *m_last_subfile = NULL;
d9b3de22 20055
1313c56e
AB
20056 /* The address of the last line entry. */
20057 CORE_ADDR m_last_address;
20058
20059 /* Set to true when a previous line at the same address (using
20060 m_last_address) had m_is_stmt true. This is reset to false when a
20061 line entry at a new address (m_address different to m_last_address) is
20062 processed. */
20063 bool m_stmt_at_address = false;
20064
804d2729
TT
20065 /* When true, record the lines we decode. */
20066 bool m_currently_recording_lines = false;
d9b3de22
DE
20067
20068 /* The last line number that was recorded, used to coalesce
20069 consecutive entries for the same line. This can happen, for
20070 example, when discriminators are present. PR 17276. */
6f77053d
PA
20071 unsigned int m_last_line = 0;
20072 bool m_line_has_non_zero_discriminator = false;
8c43009f 20073};
d9b3de22 20074
6f77053d
PA
20075void
20076lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20077{
20078 CORE_ADDR addr_adj = (((m_op_index + adjust)
20079 / m_line_header->maximum_ops_per_instruction)
20080 * m_line_header->minimum_instruction_length);
20081 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20082 m_op_index = ((m_op_index + adjust)
20083 % m_line_header->maximum_ops_per_instruction);
20084}
d9b3de22 20085
6f77053d
PA
20086void
20087lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20088{
6f77053d 20089 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20090 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20091 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20092 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20093 / m_line_header->maximum_ops_per_instruction)
20094 * m_line_header->minimum_instruction_length);
20095 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20096 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20097 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20098
258bf0ee 20099 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20100 advance_line (line_delta);
20101 record_line (false);
20102 m_discriminator = 0;
20103}
d9b3de22 20104
6f77053d
PA
20105void
20106lnp_state_machine::handle_set_file (file_name_index file)
20107{
20108 m_file = file;
20109
20110 const file_entry *fe = current_file ();
20111 if (fe == NULL)
20112 dwarf2_debug_line_missing_file_complaint ();
20113 else if (m_record_lines_p)
20114 {
20115 const char *dir = fe->include_dir (m_line_header);
20116
c24bdb02 20117 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20118 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20119 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20120 }
20121}
20122
20123void
20124lnp_state_machine::handle_const_add_pc ()
20125{
20126 CORE_ADDR adjust
20127 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20128
20129 CORE_ADDR addr_adj
20130 = (((m_op_index + adjust)
20131 / m_line_header->maximum_ops_per_instruction)
20132 * m_line_header->minimum_instruction_length);
20133
20134 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20135 m_op_index = ((m_op_index + adjust)
20136 % m_line_header->maximum_ops_per_instruction);
20137}
d9b3de22 20138
a05a36a5
DE
20139/* Return non-zero if we should add LINE to the line number table.
20140 LINE is the line to add, LAST_LINE is the last line that was added,
20141 LAST_SUBFILE is the subfile for LAST_LINE.
20142 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20143 had a non-zero discriminator.
20144
20145 We have to be careful in the presence of discriminators.
20146 E.g., for this line:
20147
20148 for (i = 0; i < 100000; i++);
20149
20150 clang can emit four line number entries for that one line,
20151 each with a different discriminator.
20152 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20153
20154 However, we want gdb to coalesce all four entries into one.
20155 Otherwise the user could stepi into the middle of the line and
20156 gdb would get confused about whether the pc really was in the
20157 middle of the line.
20158
20159 Things are further complicated by the fact that two consecutive
20160 line number entries for the same line is a heuristic used by gcc
20161 to denote the end of the prologue. So we can't just discard duplicate
20162 entries, we have to be selective about it. The heuristic we use is
20163 that we only collapse consecutive entries for the same line if at least
20164 one of those entries has a non-zero discriminator. PR 17276.
20165
20166 Note: Addresses in the line number state machine can never go backwards
20167 within one sequence, thus this coalescing is ok. */
20168
20169static int
804d2729
TT
20170dwarf_record_line_p (struct dwarf2_cu *cu,
20171 unsigned int line, unsigned int last_line,
a05a36a5
DE
20172 int line_has_non_zero_discriminator,
20173 struct subfile *last_subfile)
20174{
c24bdb02 20175 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20176 return 1;
20177 if (line != last_line)
20178 return 1;
20179 /* Same line for the same file that we've seen already.
20180 As a last check, for pr 17276, only record the line if the line
20181 has never had a non-zero discriminator. */
20182 if (!line_has_non_zero_discriminator)
20183 return 1;
20184 return 0;
20185}
20186
804d2729
TT
20187/* Use the CU's builder to record line number LINE beginning at
20188 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20189
20190static void
d9b3de22 20191dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20192 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20193 struct dwarf2_cu *cu)
252a6764
DE
20194{
20195 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20196
27e0867f
DE
20197 if (dwarf_line_debug)
20198 {
20199 fprintf_unfiltered (gdb_stdlog,
20200 "Recording line %u, file %s, address %s\n",
20201 line, lbasename (subfile->name),
20202 paddress (gdbarch, address));
20203 }
20204
804d2729 20205 if (cu != nullptr)
8c95582d 20206 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20207}
20208
20209/* Subroutine of dwarf_decode_lines_1 to simplify it.
20210 Mark the end of a set of line number records.
d9b3de22 20211 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20212 If SUBFILE is NULL the request is ignored. */
20213
20214static void
20215dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20216 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20217{
27e0867f
DE
20218 if (subfile == NULL)
20219 return;
20220
20221 if (dwarf_line_debug)
20222 {
20223 fprintf_unfiltered (gdb_stdlog,
20224 "Finishing current line, file %s, address %s\n",
20225 lbasename (subfile->name),
20226 paddress (gdbarch, address));
20227 }
20228
8c95582d 20229 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20230}
20231
6f77053d
PA
20232void
20233lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20234{
d9b3de22
DE
20235 if (dwarf_line_debug)
20236 {
20237 fprintf_unfiltered (gdb_stdlog,
20238 "Processing actual line %u: file %u,"
94a72be7 20239 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20240 m_line, m_file,
6f77053d 20241 paddress (m_gdbarch, m_address),
94a72be7
AB
20242 m_is_stmt, m_discriminator,
20243 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20244 }
20245
6f77053d 20246 file_entry *fe = current_file ();
8c43009f
PA
20247
20248 if (fe == NULL)
d9b3de22
DE
20249 dwarf2_debug_line_missing_file_complaint ();
20250 /* For now we ignore lines not starting on an instruction boundary.
20251 But not when processing end_sequence for compatibility with the
20252 previous version of the code. */
6f77053d 20253 else if (m_op_index == 0 || end_sequence)
d9b3de22 20254 {
8c43009f 20255 fe->included_p = 1;
8c95582d 20256 if (m_record_lines_p)
d9b3de22 20257 {
1313c56e
AB
20258 /* When we switch files we insert an end maker in the first file,
20259 switch to the second file and add a new line entry. The
20260 problem is that the end marker inserted in the first file will
20261 discard any previous line entries at the same address. If the
20262 line entries in the first file are marked as is-stmt, while
20263 the new line in the second file is non-stmt, then this means
20264 the end marker will discard is-stmt lines so we can have a
20265 non-stmt line. This means that there are less addresses at
20266 which the user can insert a breakpoint.
20267
20268 To improve this we track the last address in m_last_address,
20269 and whether we have seen an is-stmt at this address. Then
20270 when switching files, if we have seen a stmt at the current
20271 address, and we are switching to create a non-stmt line, then
20272 discard the new line. */
20273 bool file_changed
20274 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20275 bool ignore_this_line
20276 = (file_changed && !end_sequence && m_last_address == m_address
20277 && !m_is_stmt && m_stmt_at_address);
20278
20279 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 20280 {
804d2729
TT
20281 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20282 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20283 }
20284
1313c56e 20285 if (!end_sequence && !ignore_this_line)
d9b3de22 20286 {
8c95582d
AB
20287 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20288
804d2729 20289 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20290 m_line_has_non_zero_discriminator,
20291 m_last_subfile))
d9b3de22 20292 {
c24bdb02 20293 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20294 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20295 builder->get_current_subfile (),
8c95582d 20296 m_line, m_address, is_stmt,
804d2729 20297 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20298 }
c24bdb02 20299 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20300 m_last_line = m_line;
d9b3de22
DE
20301 }
20302 }
20303 }
1313c56e
AB
20304
20305 /* Track whether we have seen any m_is_stmt true at m_address in case we
20306 have multiple line table entries all at m_address. */
20307 if (m_last_address != m_address)
20308 {
20309 m_stmt_at_address = false;
20310 m_last_address = m_address;
20311 }
20312 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
20313}
20314
804d2729
TT
20315lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20316 line_header *lh, bool record_lines_p)
d9b3de22 20317{
804d2729 20318 m_cu = cu;
6f77053d
PA
20319 m_gdbarch = arch;
20320 m_record_lines_p = record_lines_p;
20321 m_line_header = lh;
d9b3de22 20322
804d2729 20323 m_currently_recording_lines = true;
d9b3de22 20324
d9b3de22
DE
20325 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20326 was a line entry for it so that the backend has a chance to adjust it
20327 and also record it in case it needs it. This is currently used by MIPS
20328 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20329 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20330 m_is_stmt = lh->default_is_stmt;
20331 m_discriminator = 0;
1313c56e
AB
20332
20333 m_last_address = m_address;
20334 m_stmt_at_address = false;
252a6764
DE
20335}
20336
6f77053d
PA
20337void
20338lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20339 const gdb_byte *line_ptr,
7ab6656f 20340 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20341{
7ab6656f
OJ
20342 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20343 the pc range of the CU. However, we restrict the test to only ADDRESS
20344 values of zero to preserve GDB's previous behaviour which is to handle
20345 the specific case of a function being GC'd by the linker. */
924c2928 20346
7ab6656f 20347 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20348 {
20349 /* This line table is for a function which has been
20350 GCd by the linker. Ignore it. PR gdb/12528 */
20351
5e22e966 20352 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20353 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20354
b98664d3 20355 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20356 line_offset, objfile_name (objfile));
804d2729
TT
20357 m_currently_recording_lines = false;
20358 /* Note: m_currently_recording_lines is left as false until we see
20359 DW_LNE_end_sequence. */
924c2928
DE
20360 }
20361}
20362
f3f5162e 20363/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20364 Process the line number information in LH.
20365 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20366 program in order to set included_p for every referenced header. */
debd256d 20367
c906108c 20368static void
43f3e411
DE
20369dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20370 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20371{
d521ce57
TT
20372 const gdb_byte *line_ptr, *extended_end;
20373 const gdb_byte *line_end;
a8c50c1f 20374 unsigned int bytes_read, extended_len;
699ca60a 20375 unsigned char op_code, extended_op;
e142c38c 20376 CORE_ADDR baseaddr;
5e22e966 20377 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20378 bfd *abfd = objfile->obfd;
08feed99 20379 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20380 /* True if we're recording line info (as opposed to building partial
20381 symtabs and just interested in finding include files mentioned by
20382 the line number program). */
20383 bool record_lines_p = !decode_for_pst_p;
e142c38c 20384
b3b3bada 20385 baseaddr = objfile->text_section_offset ();
c906108c 20386
debd256d
JB
20387 line_ptr = lh->statement_program_start;
20388 line_end = lh->statement_program_end;
c906108c
SS
20389
20390 /* Read the statement sequences until there's nothing left. */
20391 while (line_ptr < line_end)
20392 {
6f77053d
PA
20393 /* The DWARF line number program state machine. Reset the state
20394 machine at the start of each sequence. */
804d2729 20395 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20396 bool end_sequence = false;
d9b3de22 20397
8c43009f 20398 if (record_lines_p)
c906108c 20399 {
8c43009f
PA
20400 /* Start a subfile for the current file of the state
20401 machine. */
20402 const file_entry *fe = state_machine.current_file ();
20403
20404 if (fe != NULL)
804d2729 20405 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20406 }
20407
a738430d 20408 /* Decode the table. */
d9b3de22 20409 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20410 {
20411 op_code = read_1_byte (abfd, line_ptr);
20412 line_ptr += 1;
9aa1fe7e 20413
debd256d 20414 if (op_code >= lh->opcode_base)
6e70227d 20415 {
8e07a239 20416 /* Special opcode. */
6f77053d 20417 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20418 }
20419 else switch (op_code)
c906108c
SS
20420 {
20421 case DW_LNS_extended_op:
3e43a32a
MS
20422 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20423 &bytes_read);
473b7be6 20424 line_ptr += bytes_read;
a8c50c1f 20425 extended_end = line_ptr + extended_len;
c906108c
SS
20426 extended_op = read_1_byte (abfd, line_ptr);
20427 line_ptr += 1;
20428 switch (extended_op)
20429 {
20430 case DW_LNE_end_sequence:
6f77053d
PA
20431 state_machine.handle_end_sequence ();
20432 end_sequence = true;
c906108c
SS
20433 break;
20434 case DW_LNE_set_address:
d9b3de22
DE
20435 {
20436 CORE_ADDR address
c8a7a66f 20437 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20438 line_ptr += bytes_read;
6f77053d
PA
20439
20440 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20441 lowpc - baseaddr, address);
6f77053d 20442 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20443 }
c906108c
SS
20444 break;
20445 case DW_LNE_define_file:
debd256d 20446 {
d521ce57 20447 const char *cur_file;
ecfb656c
PA
20448 unsigned int mod_time, length;
20449 dir_index dindex;
6e70227d 20450
3e43a32a
MS
20451 cur_file = read_direct_string (abfd, line_ptr,
20452 &bytes_read);
debd256d 20453 line_ptr += bytes_read;
ecfb656c 20454 dindex = (dir_index)
debd256d
JB
20455 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20456 line_ptr += bytes_read;
20457 mod_time =
20458 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20459 line_ptr += bytes_read;
20460 length =
20461 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20462 line_ptr += bytes_read;
ecfb656c 20463 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20464 }
c906108c 20465 break;
d0c6ba3d 20466 case DW_LNE_set_discriminator:
6f77053d
PA
20467 {
20468 /* The discriminator is not interesting to the
20469 debugger; just ignore it. We still need to
20470 check its value though:
20471 if there are consecutive entries for the same
20472 (non-prologue) line we want to coalesce them.
20473 PR 17276. */
20474 unsigned int discr
20475 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20476 line_ptr += bytes_read;
20477
20478 state_machine.handle_set_discriminator (discr);
20479 }
d0c6ba3d 20480 break;
c906108c 20481 default:
b98664d3 20482 complaint (_("mangled .debug_line section"));
debd256d 20483 return;
c906108c 20484 }
a8c50c1f
DJ
20485 /* Make sure that we parsed the extended op correctly. If e.g.
20486 we expected a different address size than the producer used,
20487 we may have read the wrong number of bytes. */
20488 if (line_ptr != extended_end)
20489 {
b98664d3 20490 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20491 return;
20492 }
c906108c
SS
20493 break;
20494 case DW_LNS_copy:
6f77053d 20495 state_machine.handle_copy ();
c906108c
SS
20496 break;
20497 case DW_LNS_advance_pc:
2dc7f7b3
TT
20498 {
20499 CORE_ADDR adjust
20500 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20501 line_ptr += bytes_read;
6f77053d
PA
20502
20503 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20504 }
c906108c
SS
20505 break;
20506 case DW_LNS_advance_line:
a05a36a5
DE
20507 {
20508 int line_delta
20509 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20510 line_ptr += bytes_read;
6f77053d
PA
20511
20512 state_machine.handle_advance_line (line_delta);
a05a36a5 20513 }
c906108c
SS
20514 break;
20515 case DW_LNS_set_file:
d9b3de22 20516 {
6f77053d 20517 file_name_index file
ecfb656c
PA
20518 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20519 &bytes_read);
d9b3de22 20520 line_ptr += bytes_read;
8c43009f 20521
6f77053d 20522 state_machine.handle_set_file (file);
d9b3de22 20523 }
c906108c
SS
20524 break;
20525 case DW_LNS_set_column:
0ad93d4f 20526 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20527 line_ptr += bytes_read;
20528 break;
20529 case DW_LNS_negate_stmt:
6f77053d 20530 state_machine.handle_negate_stmt ();
c906108c
SS
20531 break;
20532 case DW_LNS_set_basic_block:
c906108c 20533 break;
c2c6d25f
JM
20534 /* Add to the address register of the state machine the
20535 address increment value corresponding to special opcode
a738430d
MK
20536 255. I.e., this value is scaled by the minimum
20537 instruction length since special opcode 255 would have
b021a221 20538 scaled the increment. */
c906108c 20539 case DW_LNS_const_add_pc:
6f77053d 20540 state_machine.handle_const_add_pc ();
c906108c
SS
20541 break;
20542 case DW_LNS_fixed_advance_pc:
3e29f34a 20543 {
6f77053d 20544 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20545 line_ptr += 2;
6f77053d
PA
20546
20547 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20548 }
c906108c 20549 break;
9aa1fe7e 20550 default:
a738430d
MK
20551 {
20552 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20553 int i;
a738430d 20554
debd256d 20555 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20556 {
20557 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20558 line_ptr += bytes_read;
20559 }
20560 }
c906108c
SS
20561 }
20562 }
d9b3de22
DE
20563
20564 if (!end_sequence)
20565 dwarf2_debug_line_missing_end_sequence_complaint ();
20566
20567 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20568 in which case we still finish recording the last line). */
6f77053d 20569 state_machine.record_line (true);
c906108c 20570 }
f3f5162e
DE
20571}
20572
20573/* Decode the Line Number Program (LNP) for the given line_header
20574 structure and CU. The actual information extracted and the type
20575 of structures created from the LNP depends on the value of PST.
20576
20577 1. If PST is NULL, then this procedure uses the data from the program
20578 to create all necessary symbol tables, and their linetables.
20579
20580 2. If PST is not NULL, this procedure reads the program to determine
20581 the list of files included by the unit represented by PST, and
20582 builds all the associated partial symbol tables.
20583
20584 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20585 It is used for relative paths in the line table.
20586 NOTE: When processing partial symtabs (pst != NULL),
20587 comp_dir == pst->dirname.
20588
20589 NOTE: It is important that psymtabs have the same file name (via strcmp)
20590 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20591 symtab we don't use it in the name of the psymtabs we create.
20592 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20593 A good testcase for this is mb-inline.exp.
20594
527f3840
JK
20595 LOWPC is the lowest address in CU (or 0 if not known).
20596
20597 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20598 for its PC<->lines mapping information. Otherwise only the filename
20599 table is read in. */
f3f5162e
DE
20600
20601static void
20602dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20603 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20604 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20605{
5e22e966 20606 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20607 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20608
527f3840
JK
20609 if (decode_mapping)
20610 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20611
20612 if (decode_for_pst_p)
20613 {
aaa75496
JB
20614 /* Now that we're done scanning the Line Header Program, we can
20615 create the psymtab of each included file. */
7ba99d21
AT
20616 for (auto &file_entry : lh->file_names ())
20617 if (file_entry.included_p == 1)
aaa75496 20618 {
c89b44cd 20619 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20620 const char *include_name =
7ba99d21
AT
20621 psymtab_include_file_name (lh, file_entry, pst,
20622 comp_dir, &name_holder);
c6da4cef 20623 if (include_name != NULL)
aaa75496
JB
20624 dwarf2_create_include_psymtab (include_name, pst, objfile);
20625 }
20626 }
cb1df416
DJ
20627 else
20628 {
20629 /* Make sure a symtab is created for every file, even files
20630 which contain only variables (i.e. no code with associated
20631 line numbers). */
c24bdb02
KS
20632 buildsym_compunit *builder = cu->get_builder ();
20633 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20634
7ba99d21 20635 for (auto &fe : lh->file_names ())
cb1df416 20636 {
804d2729 20637 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20638 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20639 {
c24bdb02 20640 builder->get_current_subfile ()->symtab
804d2729 20641 = allocate_symtab (cust,
c24bdb02 20642 builder->get_current_subfile ()->name);
43f3e411 20643 }
c24bdb02 20644 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20645 }
20646 }
c906108c
SS
20647}
20648
20649/* Start a subfile for DWARF. FILENAME is the name of the file and
20650 DIRNAME the name of the source directory which contains FILENAME
4d663531 20651 or NULL if not known.
c906108c
SS
20652 This routine tries to keep line numbers from identical absolute and
20653 relative file names in a common subfile.
20654
20655 Using the `list' example from the GDB testsuite, which resides in
20656 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20657 of /srcdir/list0.c yields the following debugging information for list0.c:
20658
c5aa993b 20659 DW_AT_name: /srcdir/list0.c
4d663531 20660 DW_AT_comp_dir: /compdir
357e46e7 20661 files.files[0].name: list0.h
c5aa993b 20662 files.files[0].dir: /srcdir
357e46e7 20663 files.files[1].name: list0.c
c5aa993b 20664 files.files[1].dir: /srcdir
c906108c
SS
20665
20666 The line number information for list0.c has to end up in a single
4f1520fb
FR
20667 subfile, so that `break /srcdir/list0.c:1' works as expected.
20668 start_subfile will ensure that this happens provided that we pass the
20669 concatenation of files.files[1].dir and files.files[1].name as the
20670 subfile's name. */
c906108c
SS
20671
20672static void
804d2729
TT
20673dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20674 const char *dirname)
c906108c 20675{
43816ebc 20676 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20677
4d663531 20678 /* In order not to lose the line information directory,
4f1520fb
FR
20679 we concatenate it to the filename when it makes sense.
20680 Note that the Dwarf3 standard says (speaking of filenames in line
20681 information): ``The directory index is ignored for file names
20682 that represent full path names''. Thus ignoring dirname in the
20683 `else' branch below isn't an issue. */
c906108c 20684
d5166ae1 20685 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20686 {
43816ebc
TT
20687 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20688 filename = copy.get ();
d521ce57 20689 }
c906108c 20690
c24bdb02 20691 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20692}
20693
804d2729
TT
20694/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20695 buildsym_compunit constructor. */
f4dc4d17 20696
c24bdb02
KS
20697struct compunit_symtab *
20698dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20699 CORE_ADDR low_pc)
f4dc4d17 20700{
c24bdb02 20701 gdb_assert (m_builder == nullptr);
43f3e411 20702
c24bdb02 20703 m_builder.reset (new struct buildsym_compunit
f6e649dd 20704 (this->per_objfile->objfile,
c24bdb02 20705 name, comp_dir, language, low_pc));
93b8bea4 20706
c24bdb02 20707 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20708
c24bdb02
KS
20709 get_builder ()->record_debugformat ("DWARF 2");
20710 get_builder ()->record_producer (producer);
f4dc4d17 20711
c24bdb02 20712 processing_has_namespace_info = false;
43f3e411 20713
c24bdb02 20714 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20715}
20716
4c2df51b
DJ
20717static void
20718var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20719 struct dwarf2_cu *cu)
4c2df51b 20720{
5e22e966 20721 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20722 struct comp_unit_head *cu_header = &cu->header;
20723
4c2df51b
DJ
20724 /* NOTE drow/2003-01-30: There used to be a comment and some special
20725 code here to turn a symbol with DW_AT_external and a
20726 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20727 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20728 with some versions of binutils) where shared libraries could have
20729 relocations against symbols in their debug information - the
20730 minimal symbol would have the right address, but the debug info
20731 would not. It's no longer necessary, because we will explicitly
20732 apply relocations when we read in the debug information now. */
20733
20734 /* A DW_AT_location attribute with no contents indicates that a
20735 variable has been optimized away. */
4fc6c0d5 20736 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20737 {
f1e6e072 20738 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20739 return;
20740 }
20741
20742 /* Handle one degenerate form of location expression specially, to
20743 preserve GDB's previous behavior when section offsets are
336d760d
AT
20744 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20745 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20746
4fc6c0d5 20747 if (attr->form_is_block ()
3019eac3
DE
20748 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20749 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20750 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20751 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20752 && (DW_BLOCK (attr)->size
20753 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20754 {
891d2f0b 20755 unsigned int dummy;
4c2df51b 20756
3019eac3 20757 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20758 SET_SYMBOL_VALUE_ADDRESS
20759 (sym, cu->header.read_address (objfile->obfd,
20760 DW_BLOCK (attr)->data + 1,
20761 &dummy));
3019eac3 20762 else
38583298
TT
20763 SET_SYMBOL_VALUE_ADDRESS
20764 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20765 &dummy));
f1e6e072 20766 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20767 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20768 SET_SYMBOL_VALUE_ADDRESS
20769 (sym,
20770 SYMBOL_VALUE_ADDRESS (sym)
20771 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20772 return;
20773 }
20774
20775 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20776 expression evaluator, and use LOC_COMPUTED only when necessary
20777 (i.e. when the value of a register or memory location is
20778 referenced, or a thread-local block, etc.). Then again, it might
20779 not be worthwhile. I'm assuming that it isn't unless performance
20780 or memory numbers show me otherwise. */
20781
f1e6e072 20782 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20783
f1e6e072 20784 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20785 cu->has_loclist = true;
4c2df51b
DJ
20786}
20787
c906108c
SS
20788/* Given a pointer to a DWARF information entry, figure out if we need
20789 to make a symbol table entry for it, and if so, create a new entry
20790 and return a pointer to it.
20791 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20792 used the passed type.
20793 If SPACE is not NULL, use it to hold the new symbol. If it is
20794 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20795
20796static struct symbol *
5e2db402
TT
20797new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20798 struct symbol *space)
c906108c 20799{
976ca316
SM
20800 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20801 struct objfile *objfile = per_objfile->objfile;
08feed99 20802 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20803 struct symbol *sym = NULL;
15d034d0 20804 const char *name;
c906108c
SS
20805 struct attribute *attr = NULL;
20806 struct attribute *attr2 = NULL;
e142c38c 20807 CORE_ADDR baseaddr;
e37fd15a
SW
20808 struct pending **list_to_add = NULL;
20809
edb3359d 20810 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20811
b3b3bada 20812 baseaddr = objfile->text_section_offset ();
c906108c 20813
94af9270 20814 name = dwarf2_name (die, cu);
c906108c
SS
20815 if (name)
20816 {
34eaf542 20817 int suppress_add = 0;
94af9270 20818
34eaf542
TT
20819 if (space)
20820 sym = space;
20821 else
8c14c3a3 20822 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20823 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20824
20825 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20826 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20827 /* Fortran does not have mangling standard and the mangling does differ
20828 between gfortran, iFort etc. */
bcfe6157
TT
20829 const char *physname
20830 = (cu->language == language_fortran
20831 ? dwarf2_full_name (name, die, cu)
20832 : dwarf2_physname (name, die, cu));
20833 const char *linkagename = dw2_linkage_name (die, cu);
20834
20835 if (linkagename == nullptr || cu->language == language_ada)
20836 sym->set_linkage_name (physname);
20837 else
20838 {
20839 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20840 sym->set_linkage_name (linkagename);
20841 }
f55ee35c 20842
c906108c 20843 /* Default assumptions.
c5aa993b 20844 Use the passed type or decode it from the die. */
176620f1 20845 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20846 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20847 if (type != NULL)
20848 SYMBOL_TYPE (sym) = type;
20849 else
e7c27a73 20850 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20851 attr = dwarf2_attr (die,
20852 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20853 cu);
435d3d88 20854 if (attr != nullptr)
c906108c
SS
20855 {
20856 SYMBOL_LINE (sym) = DW_UNSND (attr);
20857 }
cb1df416 20858
edb3359d
DJ
20859 attr = dwarf2_attr (die,
20860 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20861 cu);
435d3d88 20862 if (attr != nullptr)
cb1df416 20863 {
ecfb656c 20864 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20865 struct file_entry *fe;
9a619af0 20866
ecfb656c
PA
20867 if (cu->line_header != NULL)
20868 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20869 else
20870 fe = NULL;
20871
20872 if (fe == NULL)
b98664d3 20873 complaint (_("file index out of range"));
8c43009f
PA
20874 else
20875 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20876 }
20877
c906108c
SS
20878 switch (die->tag)
20879 {
20880 case DW_TAG_label:
e142c38c 20881 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20882 if (attr != nullptr)
3e29f34a
MR
20883 {
20884 CORE_ADDR addr;
20885
cd6c91b4 20886 addr = attr->value_as_address ();
3e29f34a 20887 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20888 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20889 }
0f5238ed
TT
20890 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20891 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20892 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20893 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20894 break;
20895 case DW_TAG_subprogram:
20896 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20897 finish_block. */
f1e6e072 20898 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20899 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20900 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20901 || cu->language == language_ada
20902 || cu->language == language_fortran)
c906108c 20903 {
2cfa0c8d 20904 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20905 Ada and Fortran subprograms, whether marked external or
20906 not, are always stored as a global symbol, because we want
20907 to be able to access them globally. For instance, we want
20908 to be able to break on a nested subprogram without having
20909 to specify the context. */
c24bdb02 20910 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20911 }
20912 else
20913 {
e37fd15a 20914 list_to_add = cu->list_in_scope;
c906108c
SS
20915 }
20916 break;
edb3359d
DJ
20917 case DW_TAG_inlined_subroutine:
20918 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20919 finish_block. */
f1e6e072 20920 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20921 SYMBOL_INLINED (sym) = 1;
481860b3 20922 list_to_add = cu->list_in_scope;
edb3359d 20923 break;
34eaf542
TT
20924 case DW_TAG_template_value_param:
20925 suppress_add = 1;
20926 /* Fall through. */
72929c62 20927 case DW_TAG_constant:
c906108c 20928 case DW_TAG_variable:
254e6b9e 20929 case DW_TAG_member:
0963b4bd
MS
20930 /* Compilation with minimal debug info may result in
20931 variables with missing type entries. Change the
20932 misleading `void' type to something sensible. */
78134374 20933 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20934 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20935
e142c38c 20936 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20937 /* In the case of DW_TAG_member, we should only be called for
20938 static const members. */
20939 if (die->tag == DW_TAG_member)
20940 {
3863f96c
DE
20941 /* dwarf2_add_field uses die_is_declaration,
20942 so we do the same. */
254e6b9e
DE
20943 gdb_assert (die_is_declaration (die, cu));
20944 gdb_assert (attr);
20945 }
435d3d88 20946 if (attr != nullptr)
c906108c 20947 {
e7c27a73 20948 dwarf2_const_value (attr, sym, cu);
e142c38c 20949 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20950 if (!suppress_add)
34eaf542
TT
20951 {
20952 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20953 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20954 else
e37fd15a 20955 list_to_add = cu->list_in_scope;
34eaf542 20956 }
c906108c
SS
20957 break;
20958 }
e142c38c 20959 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20960 if (attr != nullptr)
c906108c 20961 {
e7c27a73 20962 var_decode_location (attr, sym, cu);
e142c38c 20963 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20964
20965 /* Fortran explicitly imports any global symbols to the local
20966 scope by DW_TAG_common_block. */
20967 if (cu->language == language_fortran && die->parent
20968 && die->parent->tag == DW_TAG_common_block)
20969 attr2 = NULL;
20970
caac4577
JG
20971 if (SYMBOL_CLASS (sym) == LOC_STATIC
20972 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 20973 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20974 {
20975 /* When a static variable is eliminated by the linker,
20976 the corresponding debug information is not stripped
20977 out, but the variable address is set to null;
20978 do not add such variables into symbol table. */
20979 }
20980 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20981 {
4b610737
TT
20982 if (SYMBOL_CLASS (sym) == LOC_STATIC
20983 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 20984 && per_objfile->per_bfd->can_copy)
4b610737
TT
20985 {
20986 /* A global static variable might be subject to
20987 copy relocation. We first check for a local
20988 minsym, though, because maybe the symbol was
20989 marked hidden, in which case this would not
20990 apply. */
20991 bound_minimal_symbol found
20992 = (lookup_minimal_symbol_linkage
987012b8 20993 (sym->linkage_name (), objfile));
4b610737
TT
20994 if (found.minsym != nullptr)
20995 sym->maybe_copied = 1;
20996 }
f55ee35c 20997
1c809c68
TT
20998 /* A variable with DW_AT_external is never static,
20999 but it may be block-scoped. */
804d2729 21000 list_to_add
c24bdb02
KS
21001 = ((cu->list_in_scope
21002 == cu->get_builder ()->get_file_symbols ())
21003 ? cu->get_builder ()->get_global_symbols ()
804d2729 21004 : cu->list_in_scope);
1c809c68 21005 }
c906108c 21006 else
e37fd15a 21007 list_to_add = cu->list_in_scope;
c906108c
SS
21008 }
21009 else
21010 {
21011 /* We do not know the address of this symbol.
c5aa993b
JM
21012 If it is an external symbol and we have type information
21013 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21014 The address of the variable will then be determined from
21015 the minimal symbol table whenever the variable is
21016 referenced. */
e142c38c 21017 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21018
21019 /* Fortran explicitly imports any global symbols to the local
21020 scope by DW_TAG_common_block. */
21021 if (cu->language == language_fortran && die->parent
21022 && die->parent->tag == DW_TAG_common_block)
21023 {
21024 /* SYMBOL_CLASS doesn't matter here because
21025 read_common_block is going to reset it. */
21026 if (!suppress_add)
21027 list_to_add = cu->list_in_scope;
21028 }
21029 else if (attr2 && (DW_UNSND (attr2) != 0)
21030 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21031 {
0fe7935b
DJ
21032 /* A variable with DW_AT_external is never static, but it
21033 may be block-scoped. */
804d2729 21034 list_to_add
c24bdb02
KS
21035 = ((cu->list_in_scope
21036 == cu->get_builder ()->get_file_symbols ())
21037 ? cu->get_builder ()->get_global_symbols ()
804d2729 21038 : cu->list_in_scope);
0fe7935b 21039
f1e6e072 21040 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21041 }
442ddf59
JK
21042 else if (!die_is_declaration (die, cu))
21043 {
21044 /* Use the default LOC_OPTIMIZED_OUT class. */
21045 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21046 if (!suppress_add)
21047 list_to_add = cu->list_in_scope;
442ddf59 21048 }
c906108c
SS
21049 }
21050 break;
21051 case DW_TAG_formal_parameter:
a60f3166
TT
21052 {
21053 /* If we are inside a function, mark this as an argument. If
21054 not, we might be looking at an argument to an inlined function
21055 when we do not have enough information to show inlined frames;
21056 pretend it's a local variable in that case so that the user can
21057 still see it. */
804d2729 21058 struct context_stack *curr
c24bdb02 21059 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21060 if (curr != nullptr && curr->name != nullptr)
21061 SYMBOL_IS_ARGUMENT (sym) = 1;
21062 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21063 if (attr != nullptr)
a60f3166
TT
21064 {
21065 var_decode_location (attr, sym, cu);
21066 }
21067 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21068 if (attr != nullptr)
a60f3166
TT
21069 {
21070 dwarf2_const_value (attr, sym, cu);
21071 }
f346a30d 21072
a60f3166
TT
21073 list_to_add = cu->list_in_scope;
21074 }
c906108c
SS
21075 break;
21076 case DW_TAG_unspecified_parameters:
21077 /* From varargs functions; gdb doesn't seem to have any
21078 interest in this information, so just ignore it for now.
21079 (FIXME?) */
21080 break;
34eaf542
TT
21081 case DW_TAG_template_type_param:
21082 suppress_add = 1;
21083 /* Fall through. */
c906108c 21084 case DW_TAG_class_type:
680b30c7 21085 case DW_TAG_interface_type:
c906108c
SS
21086 case DW_TAG_structure_type:
21087 case DW_TAG_union_type:
72019c9c 21088 case DW_TAG_set_type:
c906108c 21089 case DW_TAG_enumeration_type:
f1e6e072 21090 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21091 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21092
63d06c5c 21093 {
9c37b5ae 21094 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21095 really ever be static objects: otherwise, if you try
21096 to, say, break of a class's method and you're in a file
21097 which doesn't mention that class, it won't work unless
21098 the check for all static symbols in lookup_symbol_aux
21099 saves you. See the OtherFileClass tests in
21100 gdb.c++/namespace.exp. */
21101
e37fd15a 21102 if (!suppress_add)
34eaf542 21103 {
c24bdb02 21104 buildsym_compunit *builder = cu->get_builder ();
804d2729 21105 list_to_add
c24bdb02 21106 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21107 && cu->language == language_cplus
c24bdb02 21108 ? builder->get_global_symbols ()
804d2729 21109 : cu->list_in_scope);
63d06c5c 21110
64382290 21111 /* The semantics of C++ state that "struct foo {
9c37b5ae 21112 ... }" also defines a typedef for "foo". */
64382290 21113 if (cu->language == language_cplus
45280282 21114 || cu->language == language_ada
c44af4eb
TT
21115 || cu->language == language_d
21116 || cu->language == language_rust)
64382290
TT
21117 {
21118 /* The symbol's name is already allocated along
21119 with this objfile, so we don't need to
21120 duplicate it for the type. */
7d93a1e0 21121 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21122 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21123 }
63d06c5c
DC
21124 }
21125 }
c906108c
SS
21126 break;
21127 case DW_TAG_typedef:
f1e6e072 21128 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21129 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21130 list_to_add = cu->list_in_scope;
63d06c5c 21131 break;
c906108c 21132 case DW_TAG_base_type:
a02abb62 21133 case DW_TAG_subrange_type:
f1e6e072 21134 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21135 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21136 list_to_add = cu->list_in_scope;
c906108c
SS
21137 break;
21138 case DW_TAG_enumerator:
e142c38c 21139 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21140 if (attr != nullptr)
c906108c 21141 {
e7c27a73 21142 dwarf2_const_value (attr, sym, cu);
c906108c 21143 }
63d06c5c
DC
21144 {
21145 /* NOTE: carlton/2003-11-10: See comment above in the
21146 DW_TAG_class_type, etc. block. */
21147
804d2729 21148 list_to_add
c24bdb02 21149 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21150 && cu->language == language_cplus
c24bdb02 21151 ? cu->get_builder ()->get_global_symbols ()
804d2729 21152 : cu->list_in_scope);
63d06c5c 21153 }
c906108c 21154 break;
74921315 21155 case DW_TAG_imported_declaration:
5c4e30ca 21156 case DW_TAG_namespace:
f1e6e072 21157 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21158 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21159 break;
530e8392
KB
21160 case DW_TAG_module:
21161 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21162 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21163 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21164 break;
4357ac6c 21165 case DW_TAG_common_block:
f1e6e072 21166 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21167 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21168 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21169 break;
c906108c
SS
21170 default:
21171 /* Not a tag we recognize. Hopefully we aren't processing
21172 trash data, but since we must specifically ignore things
21173 we don't recognize, there is nothing else we should do at
0963b4bd 21174 this point. */
b98664d3 21175 complaint (_("unsupported tag: '%s'"),
4d3c2250 21176 dwarf_tag_name (die->tag));
c906108c
SS
21177 break;
21178 }
df8a16a1 21179
e37fd15a
SW
21180 if (suppress_add)
21181 {
21182 sym->hash_next = objfile->template_symbols;
21183 objfile->template_symbols = sym;
21184 list_to_add = NULL;
21185 }
21186
21187 if (list_to_add != NULL)
d3cb6808 21188 add_symbol_to_list (sym, list_to_add);
e37fd15a 21189
df8a16a1
DJ
21190 /* For the benefit of old versions of GCC, check for anonymous
21191 namespaces based on the demangled name. */
4d4ec4e5 21192 if (!cu->processing_has_namespace_info
94af9270 21193 && cu->language == language_cplus)
c24bdb02 21194 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21195 }
21196 return (sym);
21197}
21198
98bfdba5
PA
21199/* Given an attr with a DW_FORM_dataN value in host byte order,
21200 zero-extend it as appropriate for the symbol's type. The DWARF
21201 standard (v4) is not entirely clear about the meaning of using
21202 DW_FORM_dataN for a constant with a signed type, where the type is
21203 wider than the data. The conclusion of a discussion on the DWARF
21204 list was that this is unspecified. We choose to always zero-extend
21205 because that is the interpretation long in use by GCC. */
c906108c 21206
98bfdba5 21207static gdb_byte *
ff39bb5e 21208dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21209 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21210{
5e22e966 21211 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21212 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21213 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21214 LONGEST l = DW_UNSND (attr);
21215
21216 if (bits < sizeof (*value) * 8)
21217 {
21218 l &= ((LONGEST) 1 << bits) - 1;
21219 *value = l;
21220 }
21221 else if (bits == sizeof (*value) * 8)
21222 *value = l;
21223 else
21224 {
224c3ddb 21225 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21226 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21227 return bytes;
21228 }
21229
21230 return NULL;
21231}
21232
21233/* Read a constant value from an attribute. Either set *VALUE, or if
21234 the value does not fit in *VALUE, set *BYTES - either already
21235 allocated on the objfile obstack, or newly allocated on OBSTACK,
21236 or, set *BATON, if we translated the constant to a location
21237 expression. */
21238
21239static void
ff39bb5e 21240dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21241 const char *name, struct obstack *obstack,
21242 struct dwarf2_cu *cu,
d521ce57 21243 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21244 struct dwarf2_locexpr_baton **baton)
21245{
5e22e966 21246 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21247 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21248 struct comp_unit_head *cu_header = &cu->header;
c906108c 21249 struct dwarf_block *blk;
98bfdba5
PA
21250 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21251 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21252
21253 *value = 0;
21254 *bytes = NULL;
21255 *baton = NULL;
c906108c
SS
21256
21257 switch (attr->form)
21258 {
21259 case DW_FORM_addr:
336d760d 21260 case DW_FORM_addrx:
3019eac3 21261 case DW_FORM_GNU_addr_index:
ac56253d 21262 {
ac56253d
TT
21263 gdb_byte *data;
21264
98bfdba5
PA
21265 if (TYPE_LENGTH (type) != cu_header->addr_size)
21266 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21267 cu_header->addr_size,
98bfdba5 21268 TYPE_LENGTH (type));
ac56253d
TT
21269 /* Symbols of this form are reasonably rare, so we just
21270 piggyback on the existing location code rather than writing
21271 a new implementation of symbol_computed_ops. */
8d749320 21272 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21273 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21274 (*baton)->per_cu = cu->per_cu;
21275 gdb_assert ((*baton)->per_cu);
ac56253d 21276
98bfdba5 21277 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21278 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21279 (*baton)->data = data;
ac56253d
TT
21280
21281 data[0] = DW_OP_addr;
21282 store_unsigned_integer (&data[1], cu_header->addr_size,
21283 byte_order, DW_ADDR (attr));
21284 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21285 }
c906108c 21286 break;
4ac36638 21287 case DW_FORM_string:
93b5768b 21288 case DW_FORM_strp:
cf532bd1 21289 case DW_FORM_strx:
3019eac3 21290 case DW_FORM_GNU_str_index:
36586728 21291 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21292 /* DW_STRING is already allocated on the objfile obstack, point
21293 directly to it. */
d521ce57 21294 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21295 break;
c906108c
SS
21296 case DW_FORM_block1:
21297 case DW_FORM_block2:
21298 case DW_FORM_block4:
21299 case DW_FORM_block:
2dc7f7b3 21300 case DW_FORM_exprloc:
0224619f 21301 case DW_FORM_data16:
c906108c 21302 blk = DW_BLOCK (attr);
98bfdba5
PA
21303 if (TYPE_LENGTH (type) != blk->size)
21304 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21305 TYPE_LENGTH (type));
21306 *bytes = blk->data;
c906108c 21307 break;
2df3850c
JM
21308
21309 /* The DW_AT_const_value attributes are supposed to carry the
21310 symbol's value "represented as it would be on the target
21311 architecture." By the time we get here, it's already been
21312 converted to host endianness, so we just need to sign- or
21313 zero-extend it as appropriate. */
21314 case DW_FORM_data1:
3aef2284 21315 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21316 break;
c906108c 21317 case DW_FORM_data2:
3aef2284 21318 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21319 break;
c906108c 21320 case DW_FORM_data4:
3aef2284 21321 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21322 break;
c906108c 21323 case DW_FORM_data8:
3aef2284 21324 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21325 break;
21326
c906108c 21327 case DW_FORM_sdata:
663c44ac 21328 case DW_FORM_implicit_const:
98bfdba5 21329 *value = DW_SND (attr);
2df3850c
JM
21330 break;
21331
c906108c 21332 case DW_FORM_udata:
98bfdba5 21333 *value = DW_UNSND (attr);
c906108c 21334 break;
2df3850c 21335
c906108c 21336 default:
b98664d3 21337 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21338 dwarf_form_name (attr->form));
98bfdba5 21339 *value = 0;
c906108c
SS
21340 break;
21341 }
21342}
21343
2df3850c 21344
98bfdba5
PA
21345/* Copy constant value from an attribute to a symbol. */
21346
2df3850c 21347static void
ff39bb5e 21348dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21349 struct dwarf2_cu *cu)
2df3850c 21350{
5e22e966 21351 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21352 LONGEST value;
d521ce57 21353 const gdb_byte *bytes;
98bfdba5 21354 struct dwarf2_locexpr_baton *baton;
2df3850c 21355
98bfdba5 21356 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21357 sym->print_name (),
98bfdba5
PA
21358 &objfile->objfile_obstack, cu,
21359 &value, &bytes, &baton);
2df3850c 21360
98bfdba5
PA
21361 if (baton != NULL)
21362 {
98bfdba5 21363 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21364 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21365 }
21366 else if (bytes != NULL)
21367 {
21368 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21369 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21370 }
21371 else
21372 {
21373 SYMBOL_VALUE (sym) = value;
f1e6e072 21374 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21375 }
2df3850c
JM
21376}
21377
c906108c
SS
21378/* Return the type of the die in question using its DW_AT_type attribute. */
21379
21380static struct type *
e7c27a73 21381die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21382{
c906108c 21383 struct attribute *type_attr;
c906108c 21384
e142c38c 21385 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21386 if (!type_attr)
21387 {
5e22e966 21388 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21389 /* A missing DW_AT_type represents a void type. */
518817b3 21390 return objfile_type (objfile)->builtin_void;
c906108c 21391 }
348e048f 21392
673bfd45 21393 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21394}
21395
b4ba55a1
JB
21396/* True iff CU's producer generates GNAT Ada auxiliary information
21397 that allows to find parallel types through that information instead
21398 of having to do expensive parallel lookups by type name. */
21399
21400static int
21401need_gnat_info (struct dwarf2_cu *cu)
21402{
de4cb04a
JB
21403 /* Assume that the Ada compiler was GNAT, which always produces
21404 the auxiliary information. */
21405 return (cu->language == language_ada);
b4ba55a1
JB
21406}
21407
b4ba55a1
JB
21408/* Return the auxiliary type of the die in question using its
21409 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21410 attribute is not present. */
21411
21412static struct type *
21413die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21414{
b4ba55a1 21415 struct attribute *type_attr;
b4ba55a1
JB
21416
21417 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21418 if (!type_attr)
21419 return NULL;
21420
673bfd45 21421 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21422}
21423
21424/* If DIE has a descriptive_type attribute, then set the TYPE's
21425 descriptive type accordingly. */
21426
21427static void
21428set_descriptive_type (struct type *type, struct die_info *die,
21429 struct dwarf2_cu *cu)
21430{
21431 struct type *descriptive_type = die_descriptive_type (die, cu);
21432
21433 if (descriptive_type)
21434 {
21435 ALLOCATE_GNAT_AUX_TYPE (type);
21436 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21437 }
21438}
21439
c906108c
SS
21440/* Return the containing type of the die in question using its
21441 DW_AT_containing_type attribute. */
21442
21443static struct type *
e7c27a73 21444die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21445{
c906108c 21446 struct attribute *type_attr;
5e22e966 21447 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21448
e142c38c 21449 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21450 if (!type_attr)
21451 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21452 "[in module %s]"), objfile_name (objfile));
33ac96f0 21453
673bfd45 21454 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21455}
21456
ac9ec31b
DE
21457/* Return an error marker type to use for the ill formed type in DIE/CU. */
21458
21459static struct type *
21460build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21461{
976ca316
SM
21462 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21463 struct objfile *objfile = per_objfile->objfile;
528e1572 21464 char *saved;
ac9ec31b 21465
528e1572
SM
21466 std::string message
21467 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21468 objfile_name (objfile),
21469 sect_offset_str (cu->header.sect_off),
21470 sect_offset_str (die->sect_off));
efba19b0 21471 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21472
19f392bc 21473 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21474}
21475
673bfd45 21476/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21477 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21478 DW_AT_containing_type.
673bfd45
DE
21479 If there is no type substitute an error marker. */
21480
c906108c 21481static struct type *
ff39bb5e 21482lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21483 struct dwarf2_cu *cu)
c906108c 21484{
976ca316
SM
21485 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21486 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
21487 struct type *this_type;
21488
ac9ec31b
DE
21489 gdb_assert (attr->name == DW_AT_type
21490 || attr->name == DW_AT_GNAT_descriptive_type
21491 || attr->name == DW_AT_containing_type);
21492
673bfd45
DE
21493 /* First see if we have it cached. */
21494
36586728
TT
21495 if (attr->form == DW_FORM_GNU_ref_alt)
21496 {
21497 struct dwarf2_per_cu_data *per_cu;
0826b30a 21498 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21499
976ca316
SM
21500 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
21501 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 21502 }
cd6c91b4 21503 else if (attr->form_is_ref ())
673bfd45 21504 {
0826b30a 21505 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21506
976ca316 21507 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 21508 }
55f1336d 21509 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21510 {
ac9ec31b 21511 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21512
ac9ec31b 21513 return get_signatured_type (die, signature, cu);
673bfd45
DE
21514 }
21515 else
21516 {
b98664d3 21517 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21518 " at %s [in module %s]"),
21519 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21520 objfile_name (objfile));
ac9ec31b 21521 return build_error_marker_type (cu, die);
673bfd45
DE
21522 }
21523
21524 /* If not cached we need to read it in. */
21525
21526 if (this_type == NULL)
21527 {
ac9ec31b 21528 struct die_info *type_die = NULL;
673bfd45
DE
21529 struct dwarf2_cu *type_cu = cu;
21530
cd6c91b4 21531 if (attr->form_is_ref ())
ac9ec31b
DE
21532 type_die = follow_die_ref (die, attr, &type_cu);
21533 if (type_die == NULL)
21534 return build_error_marker_type (cu, die);
21535 /* If we find the type now, it's probably because the type came
3019eac3
DE
21536 from an inter-CU reference and the type's CU got expanded before
21537 ours. */
ac9ec31b 21538 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21539 }
21540
21541 /* If we still don't have a type use an error marker. */
21542
21543 if (this_type == NULL)
ac9ec31b 21544 return build_error_marker_type (cu, die);
673bfd45 21545
f792889a 21546 return this_type;
c906108c
SS
21547}
21548
673bfd45
DE
21549/* Return the type in DIE, CU.
21550 Returns NULL for invalid types.
21551
02142a6c 21552 This first does a lookup in die_type_hash,
673bfd45
DE
21553 and only reads the die in if necessary.
21554
21555 NOTE: This can be called when reading in partial or full symbols. */
21556
f792889a 21557static struct type *
e7c27a73 21558read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21559{
f792889a
DJ
21560 struct type *this_type;
21561
21562 this_type = get_die_type (die, cu);
21563 if (this_type)
21564 return this_type;
21565
673bfd45
DE
21566 return read_type_die_1 (die, cu);
21567}
21568
21569/* Read the type in DIE, CU.
21570 Returns NULL for invalid types. */
21571
21572static struct type *
21573read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21574{
21575 struct type *this_type = NULL;
21576
c906108c
SS
21577 switch (die->tag)
21578 {
21579 case DW_TAG_class_type:
680b30c7 21580 case DW_TAG_interface_type:
c906108c
SS
21581 case DW_TAG_structure_type:
21582 case DW_TAG_union_type:
f792889a 21583 this_type = read_structure_type (die, cu);
c906108c
SS
21584 break;
21585 case DW_TAG_enumeration_type:
f792889a 21586 this_type = read_enumeration_type (die, cu);
c906108c
SS
21587 break;
21588 case DW_TAG_subprogram:
21589 case DW_TAG_subroutine_type:
edb3359d 21590 case DW_TAG_inlined_subroutine:
f792889a 21591 this_type = read_subroutine_type (die, cu);
c906108c
SS
21592 break;
21593 case DW_TAG_array_type:
f792889a 21594 this_type = read_array_type (die, cu);
c906108c 21595 break;
72019c9c 21596 case DW_TAG_set_type:
f792889a 21597 this_type = read_set_type (die, cu);
72019c9c 21598 break;
c906108c 21599 case DW_TAG_pointer_type:
f792889a 21600 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21601 break;
21602 case DW_TAG_ptr_to_member_type:
f792889a 21603 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21604 break;
21605 case DW_TAG_reference_type:
4297a3f0
AV
21606 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21607 break;
21608 case DW_TAG_rvalue_reference_type:
21609 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21610 break;
21611 case DW_TAG_const_type:
f792889a 21612 this_type = read_tag_const_type (die, cu);
c906108c
SS
21613 break;
21614 case DW_TAG_volatile_type:
f792889a 21615 this_type = read_tag_volatile_type (die, cu);
c906108c 21616 break;
06d66ee9
TT
21617 case DW_TAG_restrict_type:
21618 this_type = read_tag_restrict_type (die, cu);
21619 break;
c906108c 21620 case DW_TAG_string_type:
f792889a 21621 this_type = read_tag_string_type (die, cu);
c906108c
SS
21622 break;
21623 case DW_TAG_typedef:
f792889a 21624 this_type = read_typedef (die, cu);
c906108c 21625 break;
a02abb62 21626 case DW_TAG_subrange_type:
f792889a 21627 this_type = read_subrange_type (die, cu);
a02abb62 21628 break;
c906108c 21629 case DW_TAG_base_type:
f792889a 21630 this_type = read_base_type (die, cu);
c906108c 21631 break;
81a17f79 21632 case DW_TAG_unspecified_type:
f792889a 21633 this_type = read_unspecified_type (die, cu);
81a17f79 21634 break;
0114d602
DJ
21635 case DW_TAG_namespace:
21636 this_type = read_namespace_type (die, cu);
21637 break;
f55ee35c
JK
21638 case DW_TAG_module:
21639 this_type = read_module_type (die, cu);
21640 break;
a2c2acaf
MW
21641 case DW_TAG_atomic_type:
21642 this_type = read_tag_atomic_type (die, cu);
21643 break;
c906108c 21644 default:
b98664d3 21645 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21646 dwarf_tag_name (die->tag));
c906108c
SS
21647 break;
21648 }
63d06c5c 21649
f792889a 21650 return this_type;
63d06c5c
DC
21651}
21652
abc72ce4
DE
21653/* See if we can figure out if the class lives in a namespace. We do
21654 this by looking for a member function; its demangled name will
21655 contain namespace info, if there is any.
21656 Return the computed name or NULL.
21657 Space for the result is allocated on the objfile's obstack.
21658 This is the full-die version of guess_partial_die_structure_name.
21659 In this case we know DIE has no useful parent. */
21660
43816ebc 21661static const char *
abc72ce4
DE
21662guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21663{
21664 struct die_info *spec_die;
21665 struct dwarf2_cu *spec_cu;
21666 struct die_info *child;
5e22e966 21667 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21668
21669 spec_cu = cu;
21670 spec_die = die_specification (die, &spec_cu);
21671 if (spec_die != NULL)
21672 {
21673 die = spec_die;
21674 cu = spec_cu;
21675 }
21676
21677 for (child = die->child;
21678 child != NULL;
21679 child = child->sibling)
21680 {
21681 if (child->tag == DW_TAG_subprogram)
21682 {
73b9be8b 21683 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21684
7d45c7c3 21685 if (linkage_name != NULL)
abc72ce4 21686 {
43816ebc
TT
21687 gdb::unique_xmalloc_ptr<char> actual_name
21688 (language_class_name_from_physname (cu->language_defn,
21689 linkage_name));
21690 const char *name = NULL;
abc72ce4
DE
21691
21692 if (actual_name != NULL)
21693 {
15d034d0 21694 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21695
21696 if (die_name != NULL
43816ebc 21697 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21698 {
21699 /* Strip off the class name from the full name.
21700 We want the prefix. */
21701 int die_name_len = strlen (die_name);
43816ebc
TT
21702 int actual_name_len = strlen (actual_name.get ());
21703 const char *ptr = actual_name.get ();
abc72ce4
DE
21704
21705 /* Test for '::' as a sanity check. */
21706 if (actual_name_len > die_name_len + 2
43816ebc 21707 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21708 name = obstack_strndup (
e3b94546 21709 &objfile->per_bfd->storage_obstack,
43816ebc 21710 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21711 }
21712 }
abc72ce4
DE
21713 return name;
21714 }
21715 }
21716 }
21717
21718 return NULL;
21719}
21720
96408a79
SA
21721/* GCC might emit a nameless typedef that has a linkage name. Determine the
21722 prefix part in such case. See
21723 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21724
a121b7c1 21725static const char *
96408a79
SA
21726anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21727{
21728 struct attribute *attr;
e6a959d6 21729 const char *base;
96408a79
SA
21730
21731 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21732 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21733 return NULL;
21734
7d45c7c3 21735 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21736 return NULL;
21737
73b9be8b 21738 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21739 if (attr == NULL || DW_STRING (attr) == NULL)
21740 return NULL;
21741
21742 /* dwarf2_name had to be already called. */
21743 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21744
21745 /* Strip the base name, keep any leading namespaces/classes. */
21746 base = strrchr (DW_STRING (attr), ':');
21747 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21748 return "";
21749
5e22e966 21750 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21751 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21752 DW_STRING (attr),
21753 &base[-1] - DW_STRING (attr));
96408a79
SA
21754}
21755
fdde2d81 21756/* Return the name of the namespace/class that DIE is defined within,
0114d602 21757 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21758
0114d602
DJ
21759 For example, if we're within the method foo() in the following
21760 code:
21761
21762 namespace N {
21763 class C {
21764 void foo () {
21765 }
21766 };
21767 }
21768
21769 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21770
0d5cff50 21771static const char *
e142c38c 21772determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21773{
976ca316 21774 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
21775 struct die_info *parent, *spec_die;
21776 struct dwarf2_cu *spec_cu;
21777 struct type *parent_type;
a121b7c1 21778 const char *retval;
63d06c5c 21779
9c37b5ae 21780 if (cu->language != language_cplus
c44af4eb
TT
21781 && cu->language != language_fortran && cu->language != language_d
21782 && cu->language != language_rust)
0114d602
DJ
21783 return "";
21784
96408a79
SA
21785 retval = anonymous_struct_prefix (die, cu);
21786 if (retval)
21787 return retval;
21788
0114d602
DJ
21789 /* We have to be careful in the presence of DW_AT_specification.
21790 For example, with GCC 3.4, given the code
21791
21792 namespace N {
21793 void foo() {
21794 // Definition of N::foo.
21795 }
21796 }
21797
21798 then we'll have a tree of DIEs like this:
21799
21800 1: DW_TAG_compile_unit
21801 2: DW_TAG_namespace // N
21802 3: DW_TAG_subprogram // declaration of N::foo
21803 4: DW_TAG_subprogram // definition of N::foo
21804 DW_AT_specification // refers to die #3
21805
21806 Thus, when processing die #4, we have to pretend that we're in
21807 the context of its DW_AT_specification, namely the contex of die
21808 #3. */
21809 spec_cu = cu;
21810 spec_die = die_specification (die, &spec_cu);
21811 if (spec_die == NULL)
21812 parent = die->parent;
21813 else
63d06c5c 21814 {
0114d602
DJ
21815 parent = spec_die->parent;
21816 cu = spec_cu;
63d06c5c 21817 }
0114d602
DJ
21818
21819 if (parent == NULL)
21820 return "";
98bfdba5
PA
21821 else if (parent->building_fullname)
21822 {
21823 const char *name;
21824 const char *parent_name;
21825
21826 /* It has been seen on RealView 2.2 built binaries,
21827 DW_TAG_template_type_param types actually _defined_ as
21828 children of the parent class:
21829
21830 enum E {};
21831 template class <class Enum> Class{};
21832 Class<enum E> class_e;
21833
21834 1: DW_TAG_class_type (Class)
21835 2: DW_TAG_enumeration_type (E)
21836 3: DW_TAG_enumerator (enum1:0)
21837 3: DW_TAG_enumerator (enum2:1)
21838 ...
21839 2: DW_TAG_template_type_param
21840 DW_AT_type DW_FORM_ref_udata (E)
21841
21842 Besides being broken debug info, it can put GDB into an
21843 infinite loop. Consider:
21844
21845 When we're building the full name for Class<E>, we'll start
21846 at Class, and go look over its template type parameters,
21847 finding E. We'll then try to build the full name of E, and
21848 reach here. We're now trying to build the full name of E,
21849 and look over the parent DIE for containing scope. In the
21850 broken case, if we followed the parent DIE of E, we'd again
21851 find Class, and once again go look at its template type
21852 arguments, etc., etc. Simply don't consider such parent die
21853 as source-level parent of this die (it can't be, the language
21854 doesn't allow it), and break the loop here. */
21855 name = dwarf2_name (die, cu);
21856 parent_name = dwarf2_name (parent, cu);
b98664d3 21857 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21858 name ? name : "<unknown>",
21859 parent_name ? parent_name : "<unknown>");
21860 return "";
21861 }
63d06c5c 21862 else
0114d602
DJ
21863 switch (parent->tag)
21864 {
63d06c5c 21865 case DW_TAG_namespace:
0114d602 21866 parent_type = read_type_die (parent, cu);
acebe513
UW
21867 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21868 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21869 Work around this problem here. */
21870 if (cu->language == language_cplus
7d93a1e0 21871 && strcmp (parent_type->name (), "::") == 0)
acebe513 21872 return "";
0114d602 21873 /* We give a name to even anonymous namespaces. */
7d93a1e0 21874 return parent_type->name ();
63d06c5c 21875 case DW_TAG_class_type:
680b30c7 21876 case DW_TAG_interface_type:
63d06c5c 21877 case DW_TAG_structure_type:
0114d602 21878 case DW_TAG_union_type:
f55ee35c 21879 case DW_TAG_module:
0114d602 21880 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21881 if (parent_type->name () != NULL)
21882 return parent_type->name ();
0114d602
DJ
21883 else
21884 /* An anonymous structure is only allowed non-static data
21885 members; no typedefs, no member functions, et cetera.
21886 So it does not need a prefix. */
21887 return "";
abc72ce4 21888 case DW_TAG_compile_unit:
95554aad 21889 case DW_TAG_partial_unit:
abc72ce4
DE
21890 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21891 if (cu->language == language_cplus
976ca316 21892 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21893 && die->child != NULL
21894 && (die->tag == DW_TAG_class_type
21895 || die->tag == DW_TAG_structure_type
21896 || die->tag == DW_TAG_union_type))
21897 {
43816ebc 21898 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21899 if (name != NULL)
21900 return name;
21901 }
21902 return "";
0a4b0913
AB
21903 case DW_TAG_subprogram:
21904 /* Nested subroutines in Fortran get a prefix with the name
21905 of the parent's subroutine. */
21906 if (cu->language == language_fortran)
21907 {
21908 if ((die->tag == DW_TAG_subprogram)
21909 && (dwarf2_name (parent, cu) != NULL))
21910 return dwarf2_name (parent, cu);
21911 }
21912 return determine_prefix (parent, cu);
3d567982
TT
21913 case DW_TAG_enumeration_type:
21914 parent_type = read_type_die (parent, cu);
21915 if (TYPE_DECLARED_CLASS (parent_type))
21916 {
7d93a1e0
SM
21917 if (parent_type->name () != NULL)
21918 return parent_type->name ();
3d567982
TT
21919 return "";
21920 }
21921 /* Fall through. */
63d06c5c 21922 default:
8176b9b8 21923 return determine_prefix (parent, cu);
63d06c5c 21924 }
63d06c5c
DC
21925}
21926
3e43a32a
MS
21927/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21928 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21929 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21930 an obconcat, otherwise allocate storage for the result. The CU argument is
21931 used to determine the language and hence, the appropriate separator. */
987504bb 21932
f55ee35c 21933#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21934
21935static char *
f55ee35c
JK
21936typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21937 int physname, struct dwarf2_cu *cu)
63d06c5c 21938{
f55ee35c 21939 const char *lead = "";
5c315b68 21940 const char *sep;
63d06c5c 21941
3e43a32a
MS
21942 if (suffix == NULL || suffix[0] == '\0'
21943 || prefix == NULL || prefix[0] == '\0')
987504bb 21944 sep = "";
45280282
IB
21945 else if (cu->language == language_d)
21946 {
21947 /* For D, the 'main' function could be defined in any module, but it
21948 should never be prefixed. */
21949 if (strcmp (suffix, "D main") == 0)
21950 {
21951 prefix = "";
21952 sep = "";
21953 }
21954 else
21955 sep = ".";
21956 }
f55ee35c
JK
21957 else if (cu->language == language_fortran && physname)
21958 {
21959 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21960 DW_AT_MIPS_linkage_name is preferred and used instead. */
21961
21962 lead = "__";
21963 sep = "_MOD_";
21964 }
987504bb
JJ
21965 else
21966 sep = "::";
63d06c5c 21967
6dd47d34
DE
21968 if (prefix == NULL)
21969 prefix = "";
21970 if (suffix == NULL)
21971 suffix = "";
21972
987504bb
JJ
21973 if (obs == NULL)
21974 {
3e43a32a 21975 char *retval
224c3ddb
SM
21976 = ((char *)
21977 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21978
f55ee35c
JK
21979 strcpy (retval, lead);
21980 strcat (retval, prefix);
6dd47d34
DE
21981 strcat (retval, sep);
21982 strcat (retval, suffix);
63d06c5c
DC
21983 return retval;
21984 }
987504bb
JJ
21985 else
21986 {
21987 /* We have an obstack. */
f55ee35c 21988 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21989 }
63d06c5c
DC
21990}
21991
71c25dea
TT
21992/* Get name of a die, return NULL if not found. */
21993
15d034d0
TT
21994static const char *
21995dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21996 struct objfile *objfile)
71c25dea
TT
21997{
21998 if (name && cu->language == language_cplus)
21999 {
596dc4ad
TT
22000 gdb::unique_xmalloc_ptr<char> canon_name
22001 = cp_canonicalize_string (name);
71c25dea 22002
596dc4ad
TT
22003 if (canon_name != nullptr)
22004 name = objfile->intern (canon_name.get ());
71c25dea
TT
22005 }
22006
22007 return name;
c906108c
SS
22008}
22009
96553a0c
DE
22010/* Get name of a die, return NULL if not found.
22011 Anonymous namespaces are converted to their magic string. */
9219021c 22012
15d034d0 22013static const char *
e142c38c 22014dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22015{
22016 struct attribute *attr;
5e22e966 22017 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22018
e142c38c 22019 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22020 if ((!attr || !DW_STRING (attr))
96553a0c 22021 && die->tag != DW_TAG_namespace
53832f31
TT
22022 && die->tag != DW_TAG_class_type
22023 && die->tag != DW_TAG_interface_type
22024 && die->tag != DW_TAG_structure_type
22025 && die->tag != DW_TAG_union_type)
71c25dea
TT
22026 return NULL;
22027
22028 switch (die->tag)
22029 {
22030 case DW_TAG_compile_unit:
95554aad 22031 case DW_TAG_partial_unit:
71c25dea
TT
22032 /* Compilation units have a DW_AT_name that is a filename, not
22033 a source language identifier. */
22034 case DW_TAG_enumeration_type:
22035 case DW_TAG_enumerator:
22036 /* These tags always have simple identifiers already; no need
22037 to canonicalize them. */
22038 return DW_STRING (attr);
907af001 22039
96553a0c
DE
22040 case DW_TAG_namespace:
22041 if (attr != NULL && DW_STRING (attr) != NULL)
22042 return DW_STRING (attr);
22043 return CP_ANONYMOUS_NAMESPACE_STR;
22044
907af001
UW
22045 case DW_TAG_class_type:
22046 case DW_TAG_interface_type:
22047 case DW_TAG_structure_type:
22048 case DW_TAG_union_type:
22049 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22050 structures or unions. These were of the form "._%d" in GCC 4.1,
22051 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22052 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22053 if (attr && DW_STRING (attr)
61012eef
GB
22054 && (startswith (DW_STRING (attr), "._")
22055 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22056 return NULL;
53832f31
TT
22057
22058 /* GCC might emit a nameless typedef that has a linkage name. See
22059 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22060 if (!attr || DW_STRING (attr) == NULL)
22061 {
73b9be8b 22062 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22063 if (attr == NULL || DW_STRING (attr) == NULL)
22064 return NULL;
22065
df5c6c50
JK
22066 /* Avoid demangling DW_STRING (attr) the second time on a second
22067 call for the same DIE. */
22068 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 22069 {
43816ebc
TT
22070 gdb::unique_xmalloc_ptr<char> demangled
22071 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
22072 if (demangled == nullptr)
22073 return nullptr;
43816ebc 22074
be1e3d3e 22075 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 22076 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 22077 }
67430cd0
TT
22078
22079 /* Strip any leading namespaces/classes, keep only the base name.
22080 DW_AT_name for named DIEs does not contain the prefixes. */
22081 const char *base = strrchr (DW_STRING (attr), ':');
22082 if (base && base > DW_STRING (attr) && base[-1] == ':')
22083 return &base[1];
22084 else
22085 return DW_STRING (attr);
53832f31 22086 }
907af001
UW
22087 break;
22088
71c25dea 22089 default:
907af001
UW
22090 break;
22091 }
22092
22093 if (!DW_STRING_IS_CANONICAL (attr))
22094 {
be1e3d3e
TT
22095 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22096 objfile);
907af001 22097 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22098 }
907af001 22099 return DW_STRING (attr);
9219021c
DC
22100}
22101
22102/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22103 is none. *EXT_CU is the CU containing DIE on input, and the CU
22104 containing the return value on output. */
9219021c
DC
22105
22106static struct die_info *
f2f0e013 22107dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22108{
22109 struct attribute *attr;
9219021c 22110
f2f0e013 22111 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22112 if (attr == NULL)
22113 return NULL;
22114
f2f0e013 22115 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22116}
22117
f9aca02d 22118static void
d97bc12b 22119dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22120{
22121 unsigned int i;
22122
d97bc12b 22123 print_spaces (indent, f);
9d8780f0 22124 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22125 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22126 sect_offset_str (die->sect_off));
d97bc12b
DE
22127
22128 if (die->parent != NULL)
22129 {
22130 print_spaces (indent, f);
9d8780f0
SM
22131 fprintf_unfiltered (f, " parent at offset: %s\n",
22132 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22133 }
22134
22135 print_spaces (indent, f);
22136 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22137 dwarf_bool_name (die->child != NULL));
c906108c 22138
d97bc12b
DE
22139 print_spaces (indent, f);
22140 fprintf_unfiltered (f, " attributes:\n");
22141
c906108c
SS
22142 for (i = 0; i < die->num_attrs; ++i)
22143 {
d97bc12b
DE
22144 print_spaces (indent, f);
22145 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22146 dwarf_attr_name (die->attrs[i].name),
22147 dwarf_form_name (die->attrs[i].form));
d97bc12b 22148
c906108c
SS
22149 switch (die->attrs[i].form)
22150 {
c906108c 22151 case DW_FORM_addr:
336d760d 22152 case DW_FORM_addrx:
3019eac3 22153 case DW_FORM_GNU_addr_index:
d97bc12b 22154 fprintf_unfiltered (f, "address: ");
5af949e3 22155 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22156 break;
22157 case DW_FORM_block2:
22158 case DW_FORM_block4:
22159 case DW_FORM_block:
22160 case DW_FORM_block1:
56eb65bd
SP
22161 fprintf_unfiltered (f, "block: size %s",
22162 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22163 break;
2dc7f7b3 22164 case DW_FORM_exprloc:
56eb65bd
SP
22165 fprintf_unfiltered (f, "expression: size %s",
22166 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22167 break;
0224619f
JK
22168 case DW_FORM_data16:
22169 fprintf_unfiltered (f, "constant of 16 bytes");
22170 break;
4568ecf9
DE
22171 case DW_FORM_ref_addr:
22172 fprintf_unfiltered (f, "ref address: ");
22173 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22174 break;
36586728
TT
22175 case DW_FORM_GNU_ref_alt:
22176 fprintf_unfiltered (f, "alt ref address: ");
22177 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22178 break;
10b3939b
DJ
22179 case DW_FORM_ref1:
22180 case DW_FORM_ref2:
22181 case DW_FORM_ref4:
4568ecf9
DE
22182 case DW_FORM_ref8:
22183 case DW_FORM_ref_udata:
d97bc12b 22184 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22185 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22186 break;
c906108c
SS
22187 case DW_FORM_data1:
22188 case DW_FORM_data2:
22189 case DW_FORM_data4:
ce5d95e1 22190 case DW_FORM_data8:
c906108c
SS
22191 case DW_FORM_udata:
22192 case DW_FORM_sdata:
43bbcdc2
PH
22193 fprintf_unfiltered (f, "constant: %s",
22194 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22195 break;
2dc7f7b3
TT
22196 case DW_FORM_sec_offset:
22197 fprintf_unfiltered (f, "section offset: %s",
22198 pulongest (DW_UNSND (&die->attrs[i])));
22199 break;
55f1336d 22200 case DW_FORM_ref_sig8:
ac9ec31b
DE
22201 fprintf_unfiltered (f, "signature: %s",
22202 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22203 break;
c906108c 22204 case DW_FORM_string:
4bdf3d34 22205 case DW_FORM_strp:
43988095 22206 case DW_FORM_line_strp:
cf532bd1 22207 case DW_FORM_strx:
3019eac3 22208 case DW_FORM_GNU_str_index:
36586728 22209 case DW_FORM_GNU_strp_alt:
8285870a 22210 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22211 DW_STRING (&die->attrs[i])
8285870a
JK
22212 ? DW_STRING (&die->attrs[i]) : "",
22213 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22214 break;
22215 case DW_FORM_flag:
22216 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22217 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22218 else
d97bc12b 22219 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22220 break;
2dc7f7b3
TT
22221 case DW_FORM_flag_present:
22222 fprintf_unfiltered (f, "flag: TRUE");
22223 break;
a8329558 22224 case DW_FORM_indirect:
0963b4bd
MS
22225 /* The reader will have reduced the indirect form to
22226 the "base form" so this form should not occur. */
5f48f8f3 22227 fprintf_unfiltered (f,
3e43a32a 22228 "unexpected attribute form: DW_FORM_indirect");
a8329558 22229 break;
663c44ac
JK
22230 case DW_FORM_implicit_const:
22231 fprintf_unfiltered (f, "constant: %s",
22232 plongest (DW_SND (&die->attrs[i])));
22233 break;
c906108c 22234 default:
d97bc12b 22235 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22236 die->attrs[i].form);
d97bc12b 22237 break;
c906108c 22238 }
d97bc12b 22239 fprintf_unfiltered (f, "\n");
c906108c
SS
22240 }
22241}
22242
f9aca02d 22243static void
d97bc12b 22244dump_die_for_error (struct die_info *die)
c906108c 22245{
d97bc12b
DE
22246 dump_die_shallow (gdb_stderr, 0, die);
22247}
22248
22249static void
22250dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22251{
22252 int indent = level * 4;
22253
22254 gdb_assert (die != NULL);
22255
22256 if (level >= max_level)
22257 return;
22258
22259 dump_die_shallow (f, indent, die);
22260
22261 if (die->child != NULL)
c906108c 22262 {
d97bc12b
DE
22263 print_spaces (indent, f);
22264 fprintf_unfiltered (f, " Children:");
22265 if (level + 1 < max_level)
22266 {
22267 fprintf_unfiltered (f, "\n");
22268 dump_die_1 (f, level + 1, max_level, die->child);
22269 }
22270 else
22271 {
3e43a32a
MS
22272 fprintf_unfiltered (f,
22273 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22274 }
22275 }
22276
22277 if (die->sibling != NULL && level > 0)
22278 {
22279 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22280 }
22281}
22282
d97bc12b
DE
22283/* This is called from the pdie macro in gdbinit.in.
22284 It's not static so gcc will keep a copy callable from gdb. */
22285
22286void
22287dump_die (struct die_info *die, int max_level)
22288{
22289 dump_die_1 (gdb_stdlog, 0, max_level, die);
22290}
22291
f9aca02d 22292static void
51545339 22293store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22294{
51545339 22295 void **slot;
c906108c 22296
9c541725
PA
22297 slot = htab_find_slot_with_hash (cu->die_hash, die,
22298 to_underlying (die->sect_off),
b64f50a1 22299 INSERT);
51545339
DJ
22300
22301 *slot = die;
c906108c
SS
22302}
22303
348e048f
DE
22304/* Follow reference or signature attribute ATTR of SRC_DIE.
22305 On entry *REF_CU is the CU of SRC_DIE.
22306 On exit *REF_CU is the CU of the result. */
22307
22308static struct die_info *
ff39bb5e 22309follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22310 struct dwarf2_cu **ref_cu)
22311{
22312 struct die_info *die;
22313
cd6c91b4 22314 if (attr->form_is_ref ())
348e048f 22315 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22316 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22317 die = follow_die_sig (src_die, attr, ref_cu);
22318 else
22319 {
22320 dump_die_for_error (src_die);
22321 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22322 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22323 }
22324
22325 return die;
03dd20cc
DJ
22326}
22327
5c631832 22328/* Follow reference OFFSET.
673bfd45
DE
22329 On entry *REF_CU is the CU of the source die referencing OFFSET.
22330 On exit *REF_CU is the CU of the result.
22331 Returns NULL if OFFSET is invalid. */
f504f079 22332
f9aca02d 22333static struct die_info *
9c541725 22334follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22335 struct dwarf2_cu **ref_cu)
c906108c 22336{
10b3939b 22337 struct die_info temp_die;
f2f0e013 22338 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 22339 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 22340
348e048f
DE
22341 gdb_assert (cu->per_cu != NULL);
22342
98bfdba5
PA
22343 target_cu = cu;
22344
3019eac3 22345 if (cu->per_cu->is_debug_types)
348e048f
DE
22346 {
22347 /* .debug_types CUs cannot reference anything outside their CU.
22348 If they need to, they have to reference a signatured type via
55f1336d 22349 DW_FORM_ref_sig8. */
4057dfde 22350 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22351 return NULL;
348e048f 22352 }
36586728 22353 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22354 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22355 {
22356 struct dwarf2_per_cu_data *per_cu;
9a619af0 22357
9c541725 22358 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 22359 per_objfile);
03dd20cc
DJ
22360
22361 /* If necessary, add it to the queue and load its DIEs. */
976ca316
SM
22362 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22363 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
03dd20cc 22364
976ca316 22365 target_cu = per_objfile->get_cu (per_cu);
10b3939b 22366 }
98bfdba5
PA
22367 else if (cu->dies == NULL)
22368 {
22369 /* We're loading full DIEs during partial symbol reading. */
976ca316
SM
22370 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22371 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
98bfdba5 22372 }
c906108c 22373
f2f0e013 22374 *ref_cu = target_cu;
9c541725 22375 temp_die.sect_off = sect_off;
c24bdb02
KS
22376
22377 if (target_cu != cu)
22378 target_cu->ancestor = cu;
22379
9a3c8263 22380 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22381 &temp_die,
22382 to_underlying (sect_off));
5c631832 22383}
10b3939b 22384
5c631832
JK
22385/* Follow reference attribute ATTR of SRC_DIE.
22386 On entry *REF_CU is the CU of SRC_DIE.
22387 On exit *REF_CU is the CU of the result. */
22388
22389static struct die_info *
ff39bb5e 22390follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22391 struct dwarf2_cu **ref_cu)
22392{
0826b30a 22393 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22394 struct dwarf2_cu *cu = *ref_cu;
22395 struct die_info *die;
22396
9c541725 22397 die = follow_die_offset (sect_off,
36586728
TT
22398 (attr->form == DW_FORM_GNU_ref_alt
22399 || cu->per_cu->is_dwz),
22400 ref_cu);
5c631832 22401 if (!die)
9d8780f0
SM
22402 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22403 "at %s [in module %s]"),
22404 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22405 objfile_name (cu->per_objfile->objfile));
348e048f 22406
5c631832
JK
22407 return die;
22408}
22409
d4c9a4f8 22410/* See read.h. */
5c631832
JK
22411
22412struct dwarf2_locexpr_baton
9c541725 22413dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22414 dwarf2_per_cu_data *per_cu,
976ca316 22415 dwarf2_per_objfile *per_objfile,
8b9737bf 22416 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22417 void *baton, bool resolve_abstract_p)
5c631832 22418{
5c631832
JK
22419 struct die_info *die;
22420 struct attribute *attr;
22421 struct dwarf2_locexpr_baton retval;
976ca316 22422 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 22423
976ca316 22424 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 22425 if (cu == nullptr)
976ca316 22426 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
22427
22428 if (cu == nullptr)
cc12ce38
DE
22429 {
22430 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22431 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22432 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22433 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22434 }
918dd910 22435
9c541725 22436 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22437 if (!die)
9d8780f0
SM
22438 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22439 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22440
22441 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22442 if (!attr && resolve_abstract_p
976ca316
SM
22443 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22444 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22445 {
22446 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22447 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22448 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22449
3360b6e7 22450 for (const auto &cand_off
976ca316 22451 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22452 {
3360b6e7
TV
22453 struct dwarf2_cu *cand_cu = cu;
22454 struct die_info *cand
22455 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22456 if (!cand
22457 || !cand->parent
e4a62c65
TV
22458 || cand->parent->tag != DW_TAG_subprogram)
22459 continue;
22460
22461 CORE_ADDR pc_low, pc_high;
22462 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22463 if (pc_low == ((CORE_ADDR) -1))
22464 continue;
22465 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22466 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22467 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22468 continue;
22469
22470 die = cand;
22471 attr = dwarf2_attr (die, DW_AT_location, cu);
22472 break;
22473 }
22474 }
22475
5c631832
JK
22476 if (!attr)
22477 {
e103e986
JK
22478 /* DWARF: "If there is no such attribute, then there is no effect.".
22479 DATA is ignored if SIZE is 0. */
5c631832 22480
e103e986 22481 retval.data = NULL;
5c631832
JK
22482 retval.size = 0;
22483 }
cd6c91b4 22484 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22485 {
22486 struct dwarf2_loclist_baton loclist_baton;
22487 CORE_ADDR pc = (*get_frame_pc) (baton);
22488 size_t size;
22489
22490 fill_in_loclist_baton (cu, &loclist_baton, attr);
22491
22492 retval.data = dwarf2_find_location_expression (&loclist_baton,
22493 &size, pc);
22494 retval.size = size;
22495 }
5c631832
JK
22496 else
22497 {
4fc6c0d5 22498 if (!attr->form_is_block ())
9d8780f0 22499 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22500 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22501 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22502
22503 retval.data = DW_BLOCK (attr)->data;
22504 retval.size = DW_BLOCK (attr)->size;
22505 }
976ca316 22506 retval.per_objfile = per_objfile;
5c631832 22507 retval.per_cu = cu->per_cu;
918dd910 22508
976ca316 22509 per_objfile->age_comp_units ();
918dd910 22510
5c631832 22511 return retval;
348e048f
DE
22512}
22513
d4c9a4f8 22514/* See read.h. */
8b9737bf
TT
22515
22516struct dwarf2_locexpr_baton
22517dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22518 dwarf2_per_cu_data *per_cu,
14095eb3 22519 dwarf2_per_objfile *per_objfile,
8b9737bf
TT
22520 CORE_ADDR (*get_frame_pc) (void *baton),
22521 void *baton)
22522{
9c541725 22523 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22524
14095eb3
SM
22525 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22526 get_frame_pc, baton);
8b9737bf
TT
22527}
22528
b6807d98
TT
22529/* Write a constant of a given type as target-ordered bytes into
22530 OBSTACK. */
22531
22532static const gdb_byte *
22533write_constant_as_bytes (struct obstack *obstack,
22534 enum bfd_endian byte_order,
22535 struct type *type,
22536 ULONGEST value,
22537 LONGEST *len)
22538{
22539 gdb_byte *result;
22540
22541 *len = TYPE_LENGTH (type);
224c3ddb 22542 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22543 store_unsigned_integer (result, *len, byte_order, value);
22544
22545 return result;
22546}
22547
d4c9a4f8 22548/* See read.h. */
b6807d98
TT
22549
22550const gdb_byte *
9c541725 22551dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 22552 dwarf2_per_cu_data *per_cu,
14095eb3 22553 dwarf2_per_objfile *per_objfile,
d4c9a4f8 22554 obstack *obstack,
b6807d98
TT
22555 LONGEST *len)
22556{
b6807d98
TT
22557 struct die_info *die;
22558 struct attribute *attr;
22559 const gdb_byte *result = NULL;
22560 struct type *type;
22561 LONGEST value;
22562 enum bfd_endian byte_order;
14095eb3 22563 struct objfile *objfile = per_objfile->objfile;
b6807d98 22564
7188ed02 22565 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22566 if (cu == nullptr)
22567 cu = load_cu (per_cu, per_objfile, false);
22568
22569 if (cu == nullptr)
cc12ce38
DE
22570 {
22571 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22572 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22573 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22574 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22575 }
b6807d98 22576
9c541725 22577 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22578 if (!die)
9d8780f0
SM
22579 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22580 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22581
22582 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22583 if (attr == NULL)
22584 return NULL;
22585
e3b94546 22586 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22587 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22588
22589 switch (attr->form)
22590 {
22591 case DW_FORM_addr:
336d760d 22592 case DW_FORM_addrx:
b6807d98
TT
22593 case DW_FORM_GNU_addr_index:
22594 {
22595 gdb_byte *tem;
22596
22597 *len = cu->header.addr_size;
224c3ddb 22598 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22599 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22600 result = tem;
22601 }
22602 break;
22603 case DW_FORM_string:
22604 case DW_FORM_strp:
cf532bd1 22605 case DW_FORM_strx:
b6807d98
TT
22606 case DW_FORM_GNU_str_index:
22607 case DW_FORM_GNU_strp_alt:
22608 /* DW_STRING is already allocated on the objfile obstack, point
22609 directly to it. */
22610 result = (const gdb_byte *) DW_STRING (attr);
22611 *len = strlen (DW_STRING (attr));
22612 break;
22613 case DW_FORM_block1:
22614 case DW_FORM_block2:
22615 case DW_FORM_block4:
22616 case DW_FORM_block:
22617 case DW_FORM_exprloc:
0224619f 22618 case DW_FORM_data16:
b6807d98
TT
22619 result = DW_BLOCK (attr)->data;
22620 *len = DW_BLOCK (attr)->size;
22621 break;
22622
22623 /* The DW_AT_const_value attributes are supposed to carry the
22624 symbol's value "represented as it would be on the target
22625 architecture." By the time we get here, it's already been
22626 converted to host endianness, so we just need to sign- or
22627 zero-extend it as appropriate. */
22628 case DW_FORM_data1:
22629 type = die_type (die, cu);
22630 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22631 if (result == NULL)
22632 result = write_constant_as_bytes (obstack, byte_order,
22633 type, value, len);
22634 break;
22635 case DW_FORM_data2:
22636 type = die_type (die, cu);
22637 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22638 if (result == NULL)
22639 result = write_constant_as_bytes (obstack, byte_order,
22640 type, value, len);
22641 break;
22642 case DW_FORM_data4:
22643 type = die_type (die, cu);
22644 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22645 if (result == NULL)
22646 result = write_constant_as_bytes (obstack, byte_order,
22647 type, value, len);
22648 break;
22649 case DW_FORM_data8:
22650 type = die_type (die, cu);
22651 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22652 if (result == NULL)
22653 result = write_constant_as_bytes (obstack, byte_order,
22654 type, value, len);
22655 break;
22656
22657 case DW_FORM_sdata:
663c44ac 22658 case DW_FORM_implicit_const:
b6807d98
TT
22659 type = die_type (die, cu);
22660 result = write_constant_as_bytes (obstack, byte_order,
22661 type, DW_SND (attr), len);
22662 break;
22663
22664 case DW_FORM_udata:
22665 type = die_type (die, cu);
22666 result = write_constant_as_bytes (obstack, byte_order,
22667 type, DW_UNSND (attr), len);
22668 break;
22669
22670 default:
b98664d3 22671 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22672 dwarf_form_name (attr->form));
22673 break;
22674 }
22675
22676 return result;
22677}
22678
d4c9a4f8 22679/* See read.h. */
7942e96e
AA
22680
22681struct type *
9c541725 22682dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
22683 dwarf2_per_cu_data *per_cu,
22684 dwarf2_per_objfile *per_objfile)
7942e96e 22685{
7942e96e
AA
22686 struct die_info *die;
22687
7188ed02 22688 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22689 if (cu == nullptr)
22690 cu = load_cu (per_cu, per_objfile, false);
22691
22692 if (cu == nullptr)
22693 return nullptr;
7942e96e 22694
9c541725 22695 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22696 if (!die)
22697 return NULL;
22698
22699 return die_type (die, cu);
22700}
22701
8cb5117c 22702/* See read.h. */
8a9b8146
TT
22703
22704struct type *
b64f50a1 22705dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
22706 dwarf2_per_cu_data *per_cu,
22707 dwarf2_per_objfile *per_objfile)
8a9b8146 22708{
9c541725 22709 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 22710 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
22711}
22712
ac9ec31b 22713/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22714 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22715 On exit *REF_CU is the CU of the result.
22716 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22717
22718static struct die_info *
ac9ec31b
DE
22719follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22720 struct dwarf2_cu **ref_cu)
348e048f 22721{
348e048f 22722 struct die_info temp_die;
c24bdb02 22723 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 22724 struct die_info *die;
976ca316 22725 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 22726
348e048f 22727
ac9ec31b
DE
22728 /* While it might be nice to assert sig_type->type == NULL here,
22729 we can get here for DW_AT_imported_declaration where we need
22730 the DIE not the type. */
348e048f
DE
22731
22732 /* If necessary, add it to the queue and load its DIEs. */
22733
976ca316 22734 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
120ce1b5 22735 language_minimal))
976ca316 22736 read_signatured_type (sig_type, per_objfile);
348e048f 22737
976ca316 22738 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 22739 gdb_assert (sig_cu != NULL);
9c541725
PA
22740 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22741 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22742 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22743 to_underlying (temp_die.sect_off));
348e048f
DE
22744 if (die)
22745 {
796a7ff8
DE
22746 /* For .gdb_index version 7 keep track of included TUs.
22747 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
22748 if (per_objfile->per_bfd->index_table != NULL
22749 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22750 {
ae640021 22751 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22752 }
22753
348e048f 22754 *ref_cu = sig_cu;
c24bdb02
KS
22755 if (sig_cu != cu)
22756 sig_cu->ancestor = cu;
22757
348e048f
DE
22758 return die;
22759 }
22760
ac9ec31b
DE
22761 return NULL;
22762}
22763
22764/* Follow signatured type referenced by ATTR in SRC_DIE.
22765 On entry *REF_CU is the CU of SRC_DIE.
22766 On exit *REF_CU is the CU of the result.
22767 The result is the DIE of the type.
22768 If the referenced type cannot be found an error is thrown. */
22769
22770static struct die_info *
ff39bb5e 22771follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22772 struct dwarf2_cu **ref_cu)
22773{
22774 ULONGEST signature = DW_SIGNATURE (attr);
22775 struct signatured_type *sig_type;
22776 struct die_info *die;
22777
22778 gdb_assert (attr->form == DW_FORM_ref_sig8);
22779
a2ce51a0 22780 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22781 /* sig_type will be NULL if the signatured type is missing from
22782 the debug info. */
22783 if (sig_type == NULL)
22784 {
22785 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22786 " from DIE at %s [in module %s]"),
22787 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22788 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22789 }
22790
22791 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22792 if (die == NULL)
22793 {
22794 dump_die_for_error (src_die);
22795 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22796 " from DIE at %s [in module %s]"),
22797 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22798 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22799 }
22800
22801 return die;
22802}
22803
22804/* Get the type specified by SIGNATURE referenced in DIE/CU,
22805 reading in and processing the type unit if necessary. */
22806
22807static struct type *
22808get_signatured_type (struct die_info *die, ULONGEST signature,
22809 struct dwarf2_cu *cu)
22810{
976ca316 22811 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
22812 struct signatured_type *sig_type;
22813 struct dwarf2_cu *type_cu;
22814 struct die_info *type_die;
22815 struct type *type;
22816
a2ce51a0 22817 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22818 /* sig_type will be NULL if the signatured type is missing from
22819 the debug info. */
22820 if (sig_type == NULL)
22821 {
b98664d3 22822 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22823 " from DIE at %s [in module %s]"),
22824 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22825 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22826 return build_error_marker_type (cu, die);
22827 }
22828
22829 /* If we already know the type we're done. */
976ca316 22830 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
22831 if (type != nullptr)
22832 return type;
ac9ec31b
DE
22833
22834 type_cu = cu;
22835 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22836 if (type_die != NULL)
22837 {
22838 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22839 is created. This is important, for example, because for c++ classes
22840 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22841 type = read_type_die (type_die, type_cu);
22842 if (type == NULL)
22843 {
b98664d3 22844 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22845 " referenced from DIE at %s [in module %s]"),
22846 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22847 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22848 type = build_error_marker_type (cu, die);
22849 }
22850 }
22851 else
22852 {
b98664d3 22853 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22854 " from DIE at %s [in module %s]"),
22855 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22856 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22857 type = build_error_marker_type (cu, die);
22858 }
e286671b 22859
976ca316 22860 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
22861
22862 return type;
22863}
22864
22865/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22866 reading in and processing the type unit if necessary. */
22867
22868static struct type *
ff39bb5e 22869get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22870 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22871{
22872 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22873 if (attr->form_is_ref ())
ac9ec31b
DE
22874 {
22875 struct dwarf2_cu *type_cu = cu;
22876 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22877
22878 return read_type_die (type_die, type_cu);
22879 }
22880 else if (attr->form == DW_FORM_ref_sig8)
22881 {
22882 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22883 }
22884 else
22885 {
976ca316 22886 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 22887
b98664d3 22888 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22889 " at %s [in module %s]"),
22890 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 22891 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22892 return build_error_marker_type (cu, die);
22893 }
348e048f
DE
22894}
22895
e5fe5e75 22896/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22897
22898static void
ab432490
SM
22899load_full_type_unit (dwarf2_per_cu_data *per_cu,
22900 dwarf2_per_objfile *per_objfile)
348e048f 22901{
52dc124a 22902 struct signatured_type *sig_type;
348e048f 22903
f4dc4d17 22904 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22905 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22906
6721b2ec
DE
22907 /* We have the per_cu, but we need the signatured_type.
22908 Fortunately this is an easy translation. */
22909 gdb_assert (per_cu->is_debug_types);
22910 sig_type = (struct signatured_type *) per_cu;
348e048f 22911
7188ed02 22912 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22913
ab432490 22914 read_signatured_type (sig_type, per_objfile);
348e048f 22915
7188ed02 22916 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
22917}
22918
3019eac3
DE
22919/* Read in a signatured type and build its CU and DIEs.
22920 If the type is a stub for the real type in a DWO file,
22921 read in the real type from the DWO file as well. */
dee91e82
DE
22922
22923static void
ab432490
SM
22924read_signatured_type (signatured_type *sig_type,
22925 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22926{
22927 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22928
3019eac3 22929 gdb_assert (per_cu->is_debug_types);
7188ed02 22930 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22931
2e671100 22932 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
22933
22934 if (!reader.dummy_p)
22935 {
22936 struct dwarf2_cu *cu = reader.cu;
22937 const gdb_byte *info_ptr = reader.info_ptr;
22938
22939 gdb_assert (cu->die_hash == NULL);
22940 cu->die_hash =
22941 htab_create_alloc_ex (cu->header.length / 12,
22942 die_hash,
22943 die_eq,
22944 NULL,
22945 &cu->comp_unit_obstack,
22946 hashtab_obstack_allocate,
22947 dummy_obstack_deallocate);
22948
3e225074 22949 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22950 reader.comp_unit_die->child
22951 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22952 reader.comp_unit_die);
22953 cu->dies = reader.comp_unit_die;
22954 /* comp_unit_die is not stored in die_hash, no need. */
22955
22956 /* We try not to read any attributes in this function, because
22957 not all CUs needed for references have been loaded yet, and
22958 symbol table processing isn't initialized. But we have to
22959 set the CU language, or we won't be able to build types
22960 correctly. Similarly, if we do not read the producer, we can
22961 not apply producer-specific interpretation. */
22962 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22963
22964 reader.keep ();
c0ab21c2
TT
22965 }
22966
7ee85ab1 22967 sig_type->per_cu.tu_read = 1;
c906108c
SS
22968}
22969
c906108c
SS
22970/* Decode simple location descriptions.
22971 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22972 the location and return the value. If COMPUTED is non-null, it is
22973 set to true to indicate that decoding was successful, and false
22974 otherwise. If COMPUTED is null, then this function may emit a
22975 complaint. */
c906108c
SS
22976
22977static CORE_ADDR
7d79de9a 22978decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22979{
5e22e966 22980 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22981 size_t i;
22982 size_t size = blk->size;
d521ce57 22983 const gdb_byte *data = blk->data;
21ae7a4d
JK
22984 CORE_ADDR stack[64];
22985 int stacki;
22986 unsigned int bytes_read, unsnd;
22987 gdb_byte op;
c906108c 22988
7d79de9a
TT
22989 if (computed != nullptr)
22990 *computed = false;
22991
21ae7a4d
JK
22992 i = 0;
22993 stacki = 0;
22994 stack[stacki] = 0;
22995 stack[++stacki] = 0;
22996
22997 while (i < size)
22998 {
22999 op = data[i++];
23000 switch (op)
23001 {
23002 case DW_OP_lit0:
23003 case DW_OP_lit1:
23004 case DW_OP_lit2:
23005 case DW_OP_lit3:
23006 case DW_OP_lit4:
23007 case DW_OP_lit5:
23008 case DW_OP_lit6:
23009 case DW_OP_lit7:
23010 case DW_OP_lit8:
23011 case DW_OP_lit9:
23012 case DW_OP_lit10:
23013 case DW_OP_lit11:
23014 case DW_OP_lit12:
23015 case DW_OP_lit13:
23016 case DW_OP_lit14:
23017 case DW_OP_lit15:
23018 case DW_OP_lit16:
23019 case DW_OP_lit17:
23020 case DW_OP_lit18:
23021 case DW_OP_lit19:
23022 case DW_OP_lit20:
23023 case DW_OP_lit21:
23024 case DW_OP_lit22:
23025 case DW_OP_lit23:
23026 case DW_OP_lit24:
23027 case DW_OP_lit25:
23028 case DW_OP_lit26:
23029 case DW_OP_lit27:
23030 case DW_OP_lit28:
23031 case DW_OP_lit29:
23032 case DW_OP_lit30:
23033 case DW_OP_lit31:
23034 stack[++stacki] = op - DW_OP_lit0;
23035 break;
f1bea926 23036
21ae7a4d
JK
23037 case DW_OP_reg0:
23038 case DW_OP_reg1:
23039 case DW_OP_reg2:
23040 case DW_OP_reg3:
23041 case DW_OP_reg4:
23042 case DW_OP_reg5:
23043 case DW_OP_reg6:
23044 case DW_OP_reg7:
23045 case DW_OP_reg8:
23046 case DW_OP_reg9:
23047 case DW_OP_reg10:
23048 case DW_OP_reg11:
23049 case DW_OP_reg12:
23050 case DW_OP_reg13:
23051 case DW_OP_reg14:
23052 case DW_OP_reg15:
23053 case DW_OP_reg16:
23054 case DW_OP_reg17:
23055 case DW_OP_reg18:
23056 case DW_OP_reg19:
23057 case DW_OP_reg20:
23058 case DW_OP_reg21:
23059 case DW_OP_reg22:
23060 case DW_OP_reg23:
23061 case DW_OP_reg24:
23062 case DW_OP_reg25:
23063 case DW_OP_reg26:
23064 case DW_OP_reg27:
23065 case DW_OP_reg28:
23066 case DW_OP_reg29:
23067 case DW_OP_reg30:
23068 case DW_OP_reg31:
23069 stack[++stacki] = op - DW_OP_reg0;
23070 if (i < size)
7d79de9a
TT
23071 {
23072 if (computed == nullptr)
23073 dwarf2_complex_location_expr_complaint ();
23074 else
23075 return 0;
23076 }
21ae7a4d 23077 break;
c906108c 23078
21ae7a4d
JK
23079 case DW_OP_regx:
23080 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23081 i += bytes_read;
23082 stack[++stacki] = unsnd;
23083 if (i < size)
7d79de9a
TT
23084 {
23085 if (computed == nullptr)
23086 dwarf2_complex_location_expr_complaint ();
23087 else
23088 return 0;
23089 }
21ae7a4d 23090 break;
c906108c 23091
21ae7a4d 23092 case DW_OP_addr:
c8a7a66f
TT
23093 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23094 &bytes_read);
21ae7a4d
JK
23095 i += bytes_read;
23096 break;
d53d4ac5 23097
21ae7a4d
JK
23098 case DW_OP_const1u:
23099 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23100 i += 1;
23101 break;
23102
23103 case DW_OP_const1s:
23104 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23105 i += 1;
23106 break;
23107
23108 case DW_OP_const2u:
23109 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23110 i += 2;
23111 break;
23112
23113 case DW_OP_const2s:
23114 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23115 i += 2;
23116 break;
d53d4ac5 23117
21ae7a4d
JK
23118 case DW_OP_const4u:
23119 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23120 i += 4;
23121 break;
23122
23123 case DW_OP_const4s:
23124 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23125 i += 4;
23126 break;
23127
585861ea
JK
23128 case DW_OP_const8u:
23129 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23130 i += 8;
23131 break;
23132
21ae7a4d
JK
23133 case DW_OP_constu:
23134 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23135 &bytes_read);
23136 i += bytes_read;
23137 break;
23138
23139 case DW_OP_consts:
23140 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23141 i += bytes_read;
23142 break;
23143
23144 case DW_OP_dup:
23145 stack[stacki + 1] = stack[stacki];
23146 stacki++;
23147 break;
23148
23149 case DW_OP_plus:
23150 stack[stacki - 1] += stack[stacki];
23151 stacki--;
23152 break;
23153
23154 case DW_OP_plus_uconst:
23155 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23156 &bytes_read);
23157 i += bytes_read;
23158 break;
23159
23160 case DW_OP_minus:
23161 stack[stacki - 1] -= stack[stacki];
23162 stacki--;
23163 break;
23164
23165 case DW_OP_deref:
23166 /* If we're not the last op, then we definitely can't encode
23167 this using GDB's address_class enum. This is valid for partial
23168 global symbols, although the variable's address will be bogus
23169 in the psymtab. */
23170 if (i < size)
7d79de9a
TT
23171 {
23172 if (computed == nullptr)
23173 dwarf2_complex_location_expr_complaint ();
23174 else
23175 return 0;
23176 }
21ae7a4d
JK
23177 break;
23178
23179 case DW_OP_GNU_push_tls_address:
4aa4e28b 23180 case DW_OP_form_tls_address:
21ae7a4d
JK
23181 /* The top of the stack has the offset from the beginning
23182 of the thread control block at which the variable is located. */
23183 /* Nothing should follow this operator, so the top of stack would
23184 be returned. */
23185 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23186 address will be bogus in the psymtab. Make it always at least
23187 non-zero to not look as a variable garbage collected by linker
23188 which have DW_OP_addr 0. */
21ae7a4d 23189 if (i < size)
7d79de9a
TT
23190 {
23191 if (computed == nullptr)
23192 dwarf2_complex_location_expr_complaint ();
23193 else
23194 return 0;
23195 }
585861ea 23196 stack[stacki]++;
21ae7a4d
JK
23197 break;
23198
23199 case DW_OP_GNU_uninit:
7d79de9a
TT
23200 if (computed != nullptr)
23201 return 0;
21ae7a4d
JK
23202 break;
23203
336d760d 23204 case DW_OP_addrx:
3019eac3 23205 case DW_OP_GNU_addr_index:
49f6c839 23206 case DW_OP_GNU_const_index:
3019eac3
DE
23207 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23208 &bytes_read);
23209 i += bytes_read;
23210 break;
23211
21ae7a4d 23212 default:
7d79de9a
TT
23213 if (computed == nullptr)
23214 {
23215 const char *name = get_DW_OP_name (op);
21ae7a4d 23216
7d79de9a
TT
23217 if (name)
23218 complaint (_("unsupported stack op: '%s'"),
23219 name);
23220 else
23221 complaint (_("unsupported stack op: '%02x'"),
23222 op);
23223 }
21ae7a4d
JK
23224
23225 return (stack[stacki]);
d53d4ac5 23226 }
3c6e0cb3 23227
21ae7a4d
JK
23228 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23229 outside of the allocated space. Also enforce minimum>0. */
23230 if (stacki >= ARRAY_SIZE (stack) - 1)
23231 {
7d79de9a
TT
23232 if (computed == nullptr)
23233 complaint (_("location description stack overflow"));
21ae7a4d
JK
23234 return 0;
23235 }
23236
23237 if (stacki <= 0)
23238 {
7d79de9a
TT
23239 if (computed == nullptr)
23240 complaint (_("location description stack underflow"));
21ae7a4d
JK
23241 return 0;
23242 }
23243 }
7d79de9a
TT
23244
23245 if (computed != nullptr)
23246 *computed = true;
21ae7a4d 23247 return (stack[stacki]);
c906108c
SS
23248}
23249
23250/* memory allocation interface */
23251
c906108c 23252static struct dwarf_block *
7b5a2f43 23253dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23254{
8d749320 23255 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23256}
23257
c906108c 23258static struct die_info *
b60c80d6 23259dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23260{
23261 struct die_info *die;
b60c80d6
DJ
23262 size_t size = sizeof (struct die_info);
23263
23264 if (num_attrs > 1)
23265 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23266
b60c80d6 23267 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23268 memset (die, 0, sizeof (struct die_info));
23269 return (die);
23270}
2e276125
JB
23271
23272\f
a036ba48 23273
c90ec28a 23274/* Macro support. */
cf2c3c16 23275
9eac9650
TT
23276/* An overload of dwarf_decode_macros that finds the correct section
23277 and ensures it is read in before calling the other overload. */
23278
23279static void
23280dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23281 int section_is_gnu)
23282{
976ca316
SM
23283 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23284 struct objfile *objfile = per_objfile->objfile;
5a0e026f 23285 const struct line_header *lh = cu->line_header;
9eac9650
TT
23286 unsigned int offset_size = cu->header.offset_size;
23287 struct dwarf2_section_info *section;
23288 const char *section_name;
23289
23290 if (cu->dwo_unit != nullptr)
23291 {
23292 if (section_is_gnu)
23293 {
23294 section = &cu->dwo_unit->dwo_file->sections.macro;
23295 section_name = ".debug_macro.dwo";
23296 }
23297 else
23298 {
23299 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23300 section_name = ".debug_macinfo.dwo";
23301 }
23302 }
23303 else
23304 {
23305 if (section_is_gnu)
23306 {
976ca316 23307 section = &per_objfile->per_bfd->macro;
9eac9650
TT
23308 section_name = ".debug_macro";
23309 }
23310 else
23311 {
976ca316 23312 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
23313 section_name = ".debug_macinfo";
23314 }
23315 }
23316
23317 section->read (objfile);
23318 if (section->buffer == nullptr)
23319 {
23320 complaint (_("missing %s section"), section_name);
23321 return;
23322 }
23323
23324 buildsym_compunit *builder = cu->get_builder ();
23325
976ca316 23326 dwarf_decode_macros (per_objfile, builder, section, lh,
9eac9650
TT
23327 offset_size, offset, section_is_gnu);
23328}
23329
3019eac3
DE
23330/* Return the .debug_loc section to use for CU.
23331 For DWO files use .debug_loc.dwo. */
23332
23333static struct dwarf2_section_info *
23334cu_debug_loc_section (struct dwarf2_cu *cu)
23335{
976ca316 23336 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23337
3019eac3 23338 if (cu->dwo_unit)
43988095
JK
23339 {
23340 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23341
43988095
JK
23342 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23343 }
976ca316
SM
23344 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23345 : &per_objfile->per_bfd->loc);
3019eac3
DE
23346}
23347
8cf6f0b1
TT
23348/* A helper function that fills in a dwarf2_loclist_baton. */
23349
23350static void
23351fill_in_loclist_baton (struct dwarf2_cu *cu,
23352 struct dwarf2_loclist_baton *baton,
ff39bb5e 23353 const struct attribute *attr)
8cf6f0b1 23354{
976ca316 23355 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
23356 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23357
976ca316 23358 section->read (per_objfile->objfile);
8cf6f0b1 23359
976ca316 23360 baton->per_objfile = per_objfile;
8cf6f0b1
TT
23361 baton->per_cu = cu->per_cu;
23362 gdb_assert (baton->per_cu);
23363 /* We don't know how long the location list is, but make sure we
23364 don't run off the edge of the section. */
3019eac3
DE
23365 baton->size = section->size - DW_UNSND (attr);
23366 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23367 if (cu->base_address.has_value ())
23368 baton->base_address = *cu->base_address;
23369 else
23370 baton->base_address = 0;
f664829e 23371 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23372}
23373
4c2df51b 23374static void
ff39bb5e 23375dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23376 struct dwarf2_cu *cu, int is_block)
4c2df51b 23377{
976ca316
SM
23378 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23379 struct objfile *objfile = per_objfile->objfile;
3019eac3 23380 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23381
cd6c91b4 23382 if (attr->form_is_section_offset ()
3019eac3 23383 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23384 the section. If so, fall through to the complaint in the
23385 other branch. */
2c7d5afc 23386 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23387 {
0d53c4c4 23388 struct dwarf2_loclist_baton *baton;
4c2df51b 23389
8d749320 23390 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23391
8cf6f0b1 23392 fill_in_loclist_baton (cu, baton, attr);
be391dca 23393
2b24b6e4 23394 if (!cu->base_address.has_value ())
b98664d3 23395 complaint (_("Location list used without "
3e43a32a 23396 "specifying the CU base address."));
4c2df51b 23397
f1e6e072
TT
23398 SYMBOL_ACLASS_INDEX (sym) = (is_block
23399 ? dwarf2_loclist_block_index
23400 : dwarf2_loclist_index);
0d53c4c4
DJ
23401 SYMBOL_LOCATION_BATON (sym) = baton;
23402 }
23403 else
23404 {
23405 struct dwarf2_locexpr_baton *baton;
23406
8d749320 23407 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 23408 baton->per_objfile = per_objfile;
ae0d2f24
UW
23409 baton->per_cu = cu->per_cu;
23410 gdb_assert (baton->per_cu);
0d53c4c4 23411
4fc6c0d5 23412 if (attr->form_is_block ())
0d53c4c4
DJ
23413 {
23414 /* Note that we're just copying the block's data pointer
23415 here, not the actual data. We're still pointing into the
6502dd73
DJ
23416 info_buffer for SYM's objfile; right now we never release
23417 that buffer, but when we do clean up properly this may
23418 need to change. */
0d53c4c4
DJ
23419 baton->size = DW_BLOCK (attr)->size;
23420 baton->data = DW_BLOCK (attr)->data;
23421 }
23422 else
23423 {
23424 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23425 sym->natural_name ());
0d53c4c4 23426 baton->size = 0;
0d53c4c4 23427 }
6e70227d 23428
f1e6e072
TT
23429 SYMBOL_ACLASS_INDEX (sym) = (is_block
23430 ? dwarf2_locexpr_block_index
23431 : dwarf2_locexpr_index);
0d53c4c4
DJ
23432 SYMBOL_LOCATION_BATON (sym) = baton;
23433 }
4c2df51b 23434}
6502dd73 23435
2e6a9f79 23436/* See read.h. */
96408a79 23437
2e6a9f79
SM
23438const comp_unit_head *
23439dwarf2_per_cu_data::get_header () const
96408a79 23440{
2e6a9f79
SM
23441 if (!m_header_read_in)
23442 {
23443 const gdb_byte *info_ptr
23444 = this->section->buffer + to_underlying (this->sect_off);
96408a79 23445
2e6a9f79 23446 memset (&m_header, 0, sizeof (m_header));
96408a79 23447
2e6a9f79
SM
23448 read_comp_unit_head (&m_header, info_ptr, this->section,
23449 rcuh_kind::COMPILE);
23450 }
96408a79 23451
2e6a9f79 23452 return &m_header;
96408a79
SA
23453}
23454
09ba997f 23455/* See read.h. */
ae0d2f24 23456
98714339 23457int
09ba997f 23458dwarf2_per_cu_data::addr_size () const
ae0d2f24 23459{
2e6a9f79 23460 return this->get_header ()->addr_size;
ae0d2f24
UW
23461}
23462
09ba997f 23463/* See read.h. */
9eae7c52
TT
23464
23465int
09ba997f 23466dwarf2_per_cu_data::offset_size () const
9eae7c52 23467{
2e6a9f79 23468 return this->get_header ()->offset_size;
96408a79
SA
23469}
23470
09ba997f 23471/* See read.h. */
96408a79
SA
23472
23473int
09ba997f 23474dwarf2_per_cu_data::ref_addr_size () const
96408a79 23475{
2e6a9f79 23476 const comp_unit_head *header = this->get_header ();
96408a79 23477
2e6a9f79
SM
23478 if (header->version == 2)
23479 return header->addr_size;
96408a79 23480 else
2e6a9f79 23481 return header->offset_size;
181cebd4
JK
23482}
23483
09ba997f 23484/* See read.h. */
9aa1f1e3 23485
09ba997f 23486struct type *
293e7e51 23487dwarf2_cu::addr_type () const
9a49df9d 23488{
293e7e51 23489 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23490 struct type *void_type = objfile_type (objfile)->builtin_void;
23491 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23492 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23493
23494 if (TYPE_LENGTH (addr_type) == addr_size)
23495 return addr_type;
23496
09ba997f 23497 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23498 return addr_type;
23499}
23500
22b6cd70
TT
23501/* A helper function for dwarf2_find_containing_comp_unit that returns
23502 the index of the result, and that searches a vector. It will
23503 return a result even if the offset in question does not actually
23504 occur in any CU. This is separate so that it can be unit
23505 tested. */
ae038cb0 23506
22b6cd70
TT
23507static int
23508dwarf2_find_containing_comp_unit
23509 (sect_offset sect_off,
23510 unsigned int offset_in_dwz,
23511 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23512{
ae038cb0
DJ
23513 int low, high;
23514
ae038cb0 23515 low = 0;
22b6cd70 23516 high = all_comp_units.size () - 1;
ae038cb0
DJ
23517 while (high > low)
23518 {
36586728 23519 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23520 int mid = low + (high - low) / 2;
9a619af0 23521
22b6cd70 23522 mid_cu = all_comp_units[mid];
36586728 23523 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23524 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23525 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23526 high = mid;
23527 else
23528 low = mid + 1;
23529 }
23530 gdb_assert (low == high);
22b6cd70
TT
23531 return low;
23532}
23533
23534/* Locate the .debug_info compilation unit from CU's objfile which contains
23535 the DIE at OFFSET. Raises an error on failure. */
23536
23537static struct dwarf2_per_cu_data *
23538dwarf2_find_containing_comp_unit (sect_offset sect_off,
23539 unsigned int offset_in_dwz,
976ca316 23540 dwarf2_per_objfile *per_objfile)
22b6cd70 23541{
976ca316
SM
23542 int low = dwarf2_find_containing_comp_unit
23543 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
23544 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23545
45b8ae0c 23546 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23547 {
36586728 23548 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23549 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23550 "offset %s [in module %s]"),
23551 sect_offset_str (sect_off),
976ca316 23552 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 23553
976ca316 23554 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23555 <= sect_off);
976ca316 23556 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23557 }
23558 else
23559 {
976ca316 23560 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23561 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23562 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23563 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23564 return this_cu;
23565 }
23566}
23567
22b6cd70
TT
23568#if GDB_SELF_TEST
23569
23570namespace selftests {
23571namespace find_containing_comp_unit {
23572
23573static void
23574run_test ()
23575{
23576 struct dwarf2_per_cu_data one {};
23577 struct dwarf2_per_cu_data two {};
23578 struct dwarf2_per_cu_data three {};
23579 struct dwarf2_per_cu_data four {};
23580
23581 one.length = 5;
23582 two.sect_off = sect_offset (one.length);
23583 two.length = 7;
23584
23585 three.length = 5;
23586 three.is_dwz = 1;
23587 four.sect_off = sect_offset (three.length);
23588 four.length = 7;
23589 four.is_dwz = 1;
23590
23591 std::vector<dwarf2_per_cu_data *> units;
23592 units.push_back (&one);
23593 units.push_back (&two);
23594 units.push_back (&three);
23595 units.push_back (&four);
23596
23597 int result;
23598
23599 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23600 SELF_CHECK (units[result] == &one);
23601 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23602 SELF_CHECK (units[result] == &one);
23603 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23604 SELF_CHECK (units[result] == &two);
23605
23606 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23607 SELF_CHECK (units[result] == &three);
23608 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23609 SELF_CHECK (units[result] == &three);
23610 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23611 SELF_CHECK (units[result] == &four);
23612}
23613
23614}
23615}
23616
23617#endif /* GDB_SELF_TEST */
23618
9e021579 23619/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23620
9e021579
SM
23621dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23622 dwarf2_per_objfile *per_objfile)
23623 : per_cu (per_cu),
23624 per_objfile (per_objfile),
9068261f
AB
23625 mark (false),
23626 has_loclist (false),
23627 checked_producer (false),
23628 producer_is_gxx_lt_4_6 (false),
23629 producer_is_gcc_lt_4_3 (false),
eb77c9df 23630 producer_is_icc (false),
9068261f 23631 producer_is_icc_lt_14 (false),
c258c396 23632 producer_is_codewarrior (false),
9068261f 23633 processing_has_namespace_info (false)
93311388 23634{
9816fde3
JK
23635}
23636
23637/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23638
23639static void
95554aad
TT
23640prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23641 enum language pretend_language)
9816fde3
JK
23642{
23643 struct attribute *attr;
23644
23645 /* Set the language we're debugging. */
23646 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23647 if (attr != nullptr)
9816fde3
JK
23648 set_cu_language (DW_UNSND (attr), cu);
23649 else
9cded63f 23650 {
95554aad 23651 cu->language = pretend_language;
9cded63f
TT
23652 cu->language_defn = language_def (cu->language);
23653 }
dee91e82 23654
7d45c7c3 23655 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23656}
23657
7188ed02 23658/* See read.h. */
ae038cb0 23659
7188ed02
SM
23660dwarf2_cu *
23661dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23662{
7188ed02
SM
23663 auto it = m_dwarf2_cus.find (per_cu);
23664 if (it == m_dwarf2_cus.end ())
23665 return nullptr;
ae038cb0 23666
7188ed02
SM
23667 return it->second;
23668}
23669
23670/* See read.h. */
23671
23672void
23673dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23674{
23675 gdb_assert (this->get_cu (per_cu) == nullptr);
23676
23677 m_dwarf2_cus[per_cu] = cu;
23678}
23679
23680/* See read.h. */
23681
23682void
23683dwarf2_per_objfile::age_comp_units ()
23684{
23685 /* Start by clearing all marks. */
23686 for (auto pair : m_dwarf2_cus)
23687 pair.second->mark = false;
23688
23689 /* Traverse all CUs, mark them and their dependencies if used recently
23690 enough. */
23691 for (auto pair : m_dwarf2_cus)
ae038cb0 23692 {
7188ed02
SM
23693 dwarf2_cu *cu = pair.second;
23694
23695 cu->last_used++;
23696 if (cu->last_used <= dwarf_max_cache_age)
23697 dwarf2_mark (cu);
ae038cb0
DJ
23698 }
23699
7188ed02
SM
23700 /* Delete all CUs still not marked. */
23701 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 23702 {
7188ed02 23703 dwarf2_cu *cu = it->second;
ae038cb0 23704
7188ed02 23705 if (!cu->mark)
ae038cb0 23706 {
7188ed02
SM
23707 delete cu;
23708 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
23709 }
23710 else
7188ed02 23711 it++;
ae038cb0
DJ
23712 }
23713}
23714
7188ed02 23715/* See read.h. */
ae038cb0 23716
7188ed02
SM
23717void
23718dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23719{
7188ed02
SM
23720 auto it = m_dwarf2_cus.find (per_cu);
23721 if (it == m_dwarf2_cus.end ())
23722 return;
ae038cb0 23723
7188ed02 23724 delete it->second;
ae038cb0 23725
7188ed02
SM
23726 m_dwarf2_cus.erase (it);
23727}
ae038cb0 23728
7188ed02
SM
23729dwarf2_per_objfile::~dwarf2_per_objfile ()
23730{
23731 remove_all_cus ();
ae038cb0
DJ
23732}
23733
dee91e82
DE
23734/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23735 We store these in a hash table separate from the DIEs, and preserve them
23736 when the DIEs are flushed out of cache.
23737
23738 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23739 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23740 or the type may come from a DWO file. Furthermore, while it's more logical
23741 to use per_cu->section+offset, with Fission the section with the data is in
23742 the DWO file but we don't know that section at the point we need it.
23743 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23744 because we can enter the lookup routine, get_die_type_at_offset, from
23745 outside this file, and thus won't necessarily have PER_CU->cu.
23746 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23747
dee91e82 23748struct dwarf2_per_cu_offset_and_type
1c379e20 23749{
dee91e82 23750 const struct dwarf2_per_cu_data *per_cu;
9c541725 23751 sect_offset sect_off;
1c379e20
DJ
23752 struct type *type;
23753};
23754
dee91e82 23755/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23756
23757static hashval_t
dee91e82 23758per_cu_offset_and_type_hash (const void *item)
1c379e20 23759{
9a3c8263
SM
23760 const struct dwarf2_per_cu_offset_and_type *ofs
23761 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23762
9c541725 23763 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23764}
23765
dee91e82 23766/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23767
23768static int
dee91e82 23769per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23770{
9a3c8263
SM
23771 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23772 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23773 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23774 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23775
dee91e82 23776 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23777 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23778}
23779
23780/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23781 table if necessary. For convenience, return TYPE.
23782
23783 The DIEs reading must have careful ordering to:
85102364 23784 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23785 reading current DIE.
23786 * Not trying to dereference contents of still incompletely read in types
23787 while reading in other DIEs.
23788 * Enable referencing still incompletely read in types just by a pointer to
23789 the type without accessing its fields.
23790
23791 Therefore caller should follow these rules:
23792 * Try to fetch any prerequisite types we may need to build this DIE type
23793 before building the type and calling set_die_type.
e71ec853 23794 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23795 possible before fetching more types to complete the current type.
23796 * Make the type as complete as possible before fetching more types. */
1c379e20 23797
f792889a 23798static struct type *
1c379e20
DJ
23799set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23800{
976ca316 23801 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 23802 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 23803 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
23804 struct attribute *attr;
23805 struct dynamic_prop prop;
1c379e20 23806
b4ba55a1
JB
23807 /* For Ada types, make sure that the gnat-specific data is always
23808 initialized (if not already set). There are a few types where
23809 we should not be doing so, because the type-specific area is
23810 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23811 where the type-specific area is used to store the floatformat).
23812 But this is not a problem, because the gnat-specific information
23813 is actually not needed for these types. */
23814 if (need_gnat_info (cu)
78134374
SM
23815 && type->code () != TYPE_CODE_FUNC
23816 && type->code () != TYPE_CODE_FLT
23817 && type->code () != TYPE_CODE_METHODPTR
23818 && type->code () != TYPE_CODE_MEMBERPTR
23819 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23820 && !HAVE_GNAT_AUX_INFO (type))
23821 INIT_GNAT_SPECIFIC (type);
23822
3f2f83dd
KB
23823 /* Read DW_AT_allocated and set in type. */
23824 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23825 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23826 {
293e7e51 23827 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23828 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23829 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23830 }
23831 else if (attr != NULL)
23832 {
b98664d3 23833 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23834 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23835 sect_offset_str (die->sect_off));
3f2f83dd
KB
23836 }
23837
23838 /* Read DW_AT_associated and set in type. */
23839 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23840 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23841 {
293e7e51 23842 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23843 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23844 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23845 }
23846 else if (attr != NULL)
23847 {
b98664d3 23848 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23849 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23850 sect_offset_str (die->sect_off));
3f2f83dd
KB
23851 }
23852
3cdcd0ce
JB
23853 /* Read DW_AT_data_location and set in type. */
23854 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23855 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23856 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23857
976ca316
SM
23858 if (per_objfile->die_type_hash == NULL)
23859 per_objfile->die_type_hash
0335378b
TT
23860 = htab_up (htab_create_alloc (127,
23861 per_cu_offset_and_type_hash,
23862 per_cu_offset_and_type_eq,
23863 NULL, xcalloc, xfree));
1c379e20 23864
dee91e82 23865 ofs.per_cu = cu->per_cu;
9c541725 23866 ofs.sect_off = die->sect_off;
1c379e20 23867 ofs.type = type;
dee91e82 23868 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 23869 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23870 if (*slot)
b98664d3 23871 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23872 sect_offset_str (die->sect_off));
8d749320
SM
23873 *slot = XOBNEW (&objfile->objfile_obstack,
23874 struct dwarf2_per_cu_offset_and_type);
1c379e20 23875 **slot = ofs;
f792889a 23876 return type;
1c379e20
DJ
23877}
23878
9c541725 23879/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23880 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23881
23882static struct type *
9c541725 23883get_die_type_at_offset (sect_offset sect_off,
aa66c379 23884 dwarf2_per_cu_data *per_cu,
976ca316 23885 dwarf2_per_objfile *per_objfile)
1c379e20 23886{
dee91e82 23887 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23888
976ca316 23889 if (per_objfile->die_type_hash == NULL)
f792889a 23890 return NULL;
1c379e20 23891
dee91e82 23892 ofs.per_cu = per_cu;
9c541725 23893 ofs.sect_off = sect_off;
9a3c8263 23894 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 23895 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23896 if (slot)
23897 return slot->type;
23898 else
23899 return NULL;
23900}
23901
02142a6c 23902/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23903 or return NULL if DIE does not have a saved type. */
23904
23905static struct type *
23906get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23907{
aa66c379 23908 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
23909}
23910
10b3939b
DJ
23911/* Add a dependence relationship from CU to REF_PER_CU. */
23912
23913static void
23914dwarf2_add_dependence (struct dwarf2_cu *cu,
23915 struct dwarf2_per_cu_data *ref_per_cu)
23916{
23917 void **slot;
23918
23919 if (cu->dependencies == NULL)
23920 cu->dependencies
23921 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23922 NULL, &cu->comp_unit_obstack,
23923 hashtab_obstack_allocate,
23924 dummy_obstack_deallocate);
23925
23926 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23927 if (*slot == NULL)
23928 *slot = ref_per_cu;
23929}
1c379e20 23930
f504f079
DE
23931/* Subroutine of dwarf2_mark to pass to htab_traverse.
23932 Set the mark field in every compilation unit in the
7188ed02
SM
23933 cache that we must keep because we are keeping CU.
23934
23935 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 23936
10b3939b
DJ
23937static int
23938dwarf2_mark_helper (void **slot, void *data)
23939{
7188ed02
SM
23940 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23941 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23942 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
23943
23944 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23945 reading of the chain. As such dependencies remain valid it is not much
23946 useful to track and undo them during QUIT cleanups. */
7188ed02 23947 if (cu == nullptr)
d07ed419
JK
23948 return 1;
23949
7188ed02 23950 if (cu->mark)
10b3939b 23951 return 1;
10b3939b 23952
7188ed02
SM
23953 cu->mark = true;
23954
23955 if (cu->dependencies != nullptr)
23956 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
23957
23958 return 1;
23959}
23960
f504f079
DE
23961/* Set the mark field in CU and in every other compilation unit in the
23962 cache that we must keep because we are keeping CU. */
23963
ae038cb0
DJ
23964static void
23965dwarf2_mark (struct dwarf2_cu *cu)
23966{
23967 if (cu->mark)
23968 return;
7188ed02 23969
9068261f 23970 cu->mark = true;
ae038cb0 23971
7188ed02
SM
23972 if (cu->dependencies != nullptr)
23973 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
23974}
23975
72bf9492
DJ
23976/* Trivial hash function for partial_die_info: the hash value of a DIE
23977 is its offset in .debug_info for this objfile. */
23978
23979static hashval_t
23980partial_die_hash (const void *item)
23981{
9a3c8263
SM
23982 const struct partial_die_info *part_die
23983 = (const struct partial_die_info *) item;
9a619af0 23984
9c541725 23985 return to_underlying (part_die->sect_off);
72bf9492
DJ
23986}
23987
23988/* Trivial comparison function for partial_die_info structures: two DIEs
23989 are equal if they have the same offset. */
23990
23991static int
23992partial_die_eq (const void *item_lhs, const void *item_rhs)
23993{
9a3c8263
SM
23994 const struct partial_die_info *part_die_lhs
23995 = (const struct partial_die_info *) item_lhs;
23996 const struct partial_die_info *part_die_rhs
23997 = (const struct partial_die_info *) item_rhs;
9a619af0 23998
9c541725 23999 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24000}
24001
3c3bb058
AB
24002struct cmd_list_element *set_dwarf_cmdlist;
24003struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 24004
9291a0cd 24005static void
cd4fb1b2
SM
24006show_check_physname (struct ui_file *file, int from_tty,
24007 struct cmd_list_element *c, const char *value)
9291a0cd 24008{
cd4fb1b2
SM
24009 fprintf_filtered (file,
24010 _("Whether to check \"physname\" is %s.\n"),
24011 value);
9291a0cd
TT
24012}
24013
6c265988 24014void _initialize_dwarf2_read ();
cd4fb1b2 24015void
6c265988 24016_initialize_dwarf2_read ()
9291a0cd 24017{
0743fc83 24018 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24019Set DWARF specific variables.\n\
590042fc 24020Configure DWARF variables such as the cache size."),
0743fc83
TT
24021 &set_dwarf_cmdlist, "maintenance set dwarf ",
24022 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24023
0743fc83 24024 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24025Show DWARF specific variables.\n\
24026Show DWARF variables such as the cache size."),
0743fc83
TT
24027 &show_dwarf_cmdlist, "maintenance show dwarf ",
24028 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24029
cd4fb1b2
SM
24030 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24031 &dwarf_max_cache_age, _("\
24032Set the upper bound on the age of cached DWARF compilation units."), _("\
24033Show the upper bound on the age of cached DWARF compilation units."), _("\
24034A higher limit means that cached compilation units will be stored\n\
24035in memory longer, and more total memory will be used. Zero disables\n\
24036caching, which can slow down startup."),
24037 NULL,
24038 show_dwarf_max_cache_age,
24039 &set_dwarf_cmdlist,
24040 &show_dwarf_cmdlist);
156942c7 24041
cd4fb1b2
SM
24042 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24043Set debugging of the DWARF reader."), _("\
24044Show debugging of the DWARF reader."), _("\
24045When enabled (non-zero), debugging messages are printed during DWARF\n\
24046reading and symtab expansion. A value of 1 (one) provides basic\n\
24047information. A value greater than 1 provides more verbose information."),
24048 NULL,
24049 NULL,
24050 &setdebuglist, &showdebuglist);
9291a0cd 24051
cd4fb1b2
SM
24052 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24053Set debugging of the DWARF DIE reader."), _("\
24054Show debugging of the DWARF DIE reader."), _("\
24055When enabled (non-zero), DIEs are dumped after they are read in.\n\
24056The value is the maximum depth to print."),
24057 NULL,
24058 NULL,
24059 &setdebuglist, &showdebuglist);
9291a0cd 24060
cd4fb1b2
SM
24061 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24062Set debugging of the dwarf line reader."), _("\
24063Show debugging of the dwarf line reader."), _("\
24064When enabled (non-zero), line number entries are dumped as they are read in.\n\
24065A value of 1 (one) provides basic information.\n\
24066A value greater than 1 provides more verbose information."),
24067 NULL,
24068 NULL,
24069 &setdebuglist, &showdebuglist);
437afbb8 24070
cd4fb1b2
SM
24071 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24072Set cross-checking of \"physname\" code against demangler."), _("\
24073Show cross-checking of \"physname\" code against demangler."), _("\
24074When enabled, GDB's internal \"physname\" code is checked against\n\
24075the demangler."),
24076 NULL, show_check_physname,
24077 &setdebuglist, &showdebuglist);
900e11f9 24078
e615022a
DE
24079 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24080 no_class, &use_deprecated_index_sections, _("\
24081Set whether to use deprecated gdb_index sections."), _("\
24082Show whether to use deprecated gdb_index sections."), _("\
24083When enabled, deprecated .gdb_index sections are used anyway.\n\
24084Normally they are ignored either because of a missing feature or\n\
24085performance issue.\n\
24086Warning: This option must be enabled before gdb reads the file."),
24087 NULL,
24088 NULL,
24089 &setlist, &showlist);
24090
f1e6e072
TT
24091 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24092 &dwarf2_locexpr_funcs);
24093 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24094 &dwarf2_loclist_funcs);
24095
24096 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24097 &dwarf2_block_frame_base_locexpr_funcs);
24098 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24099 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24100
24101#if GDB_SELF_TEST
24102 selftests::register_test ("dw2_expand_symtabs_matching",
24103 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24104 selftests::register_test ("dwarf2_find_containing_comp_unit",
24105 selftests::find_containing_comp_unit::run_test);
c62446b1 24106#endif
6502dd73 24107}
This page took 9.304568 seconds and 4 git commands to generate.