PR26188, buff overflow in coff_find_nearest_line_with_names
[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
e5f3ece2 3439/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3440
9291a0cd 3441static void
e5f3ece2
TV
3442dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3443 dwarf2_per_objfile *per_objfile,
3444 gdb::optional<block_enum> block_index,
3445 domain_enum domain)
da51c347 3446{
976ca316 3447 iter->per_objfile = per_objfile;
da51c347
DE
3448 iter->block_index = block_index;
3449 iter->domain = domain;
3450 iter->next = 0;
8943b874 3451 iter->global_seen = 0;
e5f3ece2
TV
3452 iter->vec = NULL;
3453 iter->length = 0;
3454}
da51c347 3455
e5f3ece2
TV
3456/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3457
3458static void
3459dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3460 dwarf2_per_objfile *per_objfile,
3461 gdb::optional<block_enum> block_index,
3462 domain_enum domain,
3463 const char *name)
3464{
3465 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3466
e5f3ece2 3467 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3468 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3469 if (index == NULL)
3470 return;
3471
3472 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3473 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3474}
3475
3476/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3477
3478static void
3479dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3480 dwarf2_per_objfile *per_objfile,
3481 gdb::optional<block_enum> block_index,
3482 domain_enum domain, offset_type namei)
3483{
3484 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3485
3486 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3487 /* index is NULL if OBJF_READNOW. */
3488 if (index == NULL)
3489 return;
3490
3491 gdb_assert (!index->symbol_name_slot_invalid (namei));
3492 const auto &bucket = index->symbol_table[namei];
3493
3494 iter->vec = (offset_type *) (index->constant_pool
3495 + MAYBE_SWAP (bucket.vec));
3496 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3497}
3498
3499/* Return the next matching CU or NULL if there are no more. */
3500
3501static struct dwarf2_per_cu_data *
3502dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3503{
976ca316 3504 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3505
da51c347
DE
3506 for ( ; iter->next < iter->length; ++iter->next)
3507 {
3508 offset_type cu_index_and_attrs =
3509 MAYBE_SWAP (iter->vec[iter->next + 1]);
3510 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3511 gdb_index_symbol_kind symbol_kind =
3512 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3513 /* Only check the symbol attributes if they're present.
3514 Indices prior to version 7 don't record them,
3515 and indices >= 7 may elide them for certain symbols
3516 (gold does this). */
3517 int attrs_valid =
976ca316 3518 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3519 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3520
3190f0c6 3521 /* Don't crash on bad data. */
976ca316
SM
3522 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3523 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3524 {
b98664d3 3525 complaint (_(".gdb_index entry has bad CU index"
976ca316 3526 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3527 continue;
3528 }
3529
976ca316 3530 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3531
da51c347 3532 /* Skip if already read in. */
976ca316 3533 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3534 continue;
3535
8943b874
DE
3536 /* Check static vs global. */
3537 if (attrs_valid)
3538 {
2b79f376
SM
3539 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3540
3541 if (iter->block_index.has_value ())
3542 {
3543 bool want_static = *iter->block_index == STATIC_BLOCK;
3544
3545 if (is_static != want_static)
3546 continue;
3547 }
3548
8943b874 3549 /* Work around gold/15646. */
f030440d
TV
3550 if (!is_static
3551 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3552 {
3553 if (iter->global_seen)
3554 continue;
3555
3556 iter->global_seen = 1;
3557 }
8943b874 3558 }
da51c347
DE
3559
3560 /* Only check the symbol's kind if it has one. */
3561 if (attrs_valid)
3562 {
3563 switch (iter->domain)
3564 {
3565 case VAR_DOMAIN:
3566 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3567 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3568 /* Some types are also in VAR_DOMAIN. */
3569 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3570 continue;
3571 break;
3572 case STRUCT_DOMAIN:
3573 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3574 continue;
3575 break;
3576 case LABEL_DOMAIN:
3577 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3578 continue;
3579 break;
59c35742
AB
3580 case MODULE_DOMAIN:
3581 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3582 continue;
3583 break;
da51c347
DE
3584 default:
3585 break;
3586 }
3587 }
3588
3589 ++iter->next;
3590 return per_cu;
3591 }
3592
3593 return NULL;
3594}
3595
43f3e411 3596static struct compunit_symtab *
c7f839cb 3597dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3598 const char *name, domain_enum domain)
9291a0cd 3599{
43f3e411 3600 struct compunit_symtab *stab_best = NULL;
976ca316 3601 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3602
b5ec771e
PA
3603 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3604
ed2dc618
SM
3605 struct dw2_symtab_iterator iter;
3606 struct dwarf2_per_cu_data *per_cu;
da51c347 3607
976ca316 3608 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3609
ed2dc618
SM
3610 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3611 {
3612 struct symbol *sym, *with_opaque = NULL;
97a1449a 3613 struct compunit_symtab *stab
976ca316 3614 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3615 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3616 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3617
ed2dc618
SM
3618 sym = block_find_symbol (block, name, domain,
3619 block_find_non_opaque_type_preferred,
3620 &with_opaque);
b2e2f908 3621
ed2dc618
SM
3622 /* Some caution must be observed with overloaded functions
3623 and methods, since the index will not contain any overload
3624 information (but NAME might contain it). */
da51c347 3625
ed2dc618
SM
3626 if (sym != NULL
3627 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3628 return stab;
3629 if (with_opaque != NULL
3630 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3631 stab_best = stab;
da51c347 3632
ed2dc618 3633 /* Keep looking through other CUs. */
9291a0cd 3634 }
9291a0cd 3635
da51c347 3636 return stab_best;
9291a0cd
TT
3637}
3638
3639static void
3640dw2_print_stats (struct objfile *objfile)
3641{
976ca316
SM
3642 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3643 int total = (per_objfile->per_bfd->all_comp_units.size ()
3644 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3645 int count = 0;
9291a0cd 3646
ed2dc618 3647 for (int i = 0; i < total; ++i)
9291a0cd 3648 {
976ca316 3649 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3650
976ca316 3651 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3652 ++count;
3653 }
e4a48d9d 3654 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3655 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3656}
3657
779bd270
DE
3658/* This dumps minimal information about the index.
3659 It is called via "mt print objfiles".
3660 One use is to verify .gdb_index has been loaded by the
3661 gdb.dwarf2/gdb-index.exp testcase. */
3662
9291a0cd
TT
3663static void
3664dw2_dump (struct objfile *objfile)
3665{
976ca316 3666 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3667
976ca316 3668 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3669 printf_filtered (".gdb_index:");
976ca316 3670 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3671 {
3672 printf_filtered (" version %d\n",
976ca316 3673 per_objfile->per_bfd->index_table->version);
779bd270
DE
3674 }
3675 else
3676 printf_filtered (" faked for \"readnow\"\n");
3677 printf_filtered ("\n");
9291a0cd
TT
3678}
3679
9291a0cd
TT
3680static void
3681dw2_expand_symtabs_for_function (struct objfile *objfile,
3682 const char *func_name)
3683{
976ca316 3684 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3685
ed2dc618
SM
3686 struct dw2_symtab_iterator iter;
3687 struct dwarf2_per_cu_data *per_cu;
da51c347 3688
976ca316 3689 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3690
ed2dc618 3691 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3692 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3693
9291a0cd
TT
3694}
3695
3696static void
3697dw2_expand_all_symtabs (struct objfile *objfile)
3698{
976ca316
SM
3699 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3700 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3701 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3702
ed2dc618 3703 for (int i = 0; i < total_units; ++i)
9291a0cd 3704 {
976ca316 3705 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3706
58f0c718
TT
3707 /* We don't want to directly expand a partial CU, because if we
3708 read it with the wrong language, then assertion failures can
3709 be triggered later on. See PR symtab/23010. So, tell
3710 dw2_instantiate_symtab to skip partial CUs -- any important
3711 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3712 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3713 }
3714}
3715
3716static void
652a8996
JK
3717dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3718 const char *fullname)
9291a0cd 3719{
976ca316 3720 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3721
3722 /* We don't need to consider type units here.
3723 This is only called for examining code, e.g. expand_line_sal.
3724 There can be an order of magnitude (or more) more type units
3725 than comp units, and we avoid them if we can. */
3726
976ca316 3727 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3728 {
3d7bb9d9 3729 /* We only need to look at symtabs not already expanded. */
976ca316 3730 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3731 continue;
3732
976ca316 3733 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3734 if (file_data == NULL)
9291a0cd
TT
3735 continue;
3736
b76e467d 3737 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3738 {
652a8996
JK
3739 const char *this_fullname = file_data->file_names[j];
3740
3741 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3742 {
976ca316 3743 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3744 break;
3745 }
3746 }
3747 }
3748}
3749
9a0bacfb
TV
3750static void
3751dw2_expand_symtabs_matching_symbol
3752 (mapped_index_base &index,
3753 const lookup_name_info &lookup_name_in,
3754 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3755 enum search_domain kind,
fcf23d5b
SM
3756 gdb::function_view<bool (offset_type)> match_callback,
3757 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3758
3759static void
3760dw2_expand_symtabs_matching_one
97a1449a
SM
3761 (dwarf2_per_cu_data *per_cu,
3762 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3763 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3764 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3765
9291a0cd 3766static void
199b4314
TT
3767dw2_map_matching_symbols
3768 (struct objfile *objfile,
b054970d 3769 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3770 int global,
3771 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3772 symbol_compare_ftype *ordered_compare)
9291a0cd 3773{
1aa98955 3774 /* Used for Ada. */
976ca316 3775 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3776
9a0bacfb
TV
3777 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3778
976ca316 3779 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3780 {
976ca316 3781 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3782
9a0bacfb
TV
3783 const char *match_name = name.ada ().lookup_name ().c_str ();
3784 auto matcher = [&] (const char *symname)
3785 {
3786 if (ordered_compare == nullptr)
3787 return true;
3788 return ordered_compare (symname, match_name) == 0;
3789 };
3790
3791 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3792 [&] (offset_type namei)
3793 {
3794 struct dw2_symtab_iterator iter;
3795 struct dwarf2_per_cu_data *per_cu;
3796
976ca316 3797 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3798 namei);
9a0bacfb 3799 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3800 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3801 nullptr);
9a0bacfb 3802 return true;
976ca316 3803 }, per_objfile);
9a0bacfb
TV
3804 }
3805 else
3806 {
3807 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3808 proceed assuming all symtabs have been read in. */
3809 }
1aa98955
TV
3810
3811 for (compunit_symtab *cust : objfile->compunits ())
3812 {
3813 const struct block *block;
3814
3815 if (cust == NULL)
3816 continue;
3817 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3818 if (!iterate_over_symbols_terminated (block, name,
3819 domain, callback))
3820 return;
3821 }
9291a0cd
TT
3822}
3823
e1ef7d7a
PA
3824/* Starting from a search name, return the string that finds the upper
3825 bound of all strings that start with SEARCH_NAME in a sorted name
3826 list. Returns the empty string to indicate that the upper bound is
3827 the end of the list. */
3828
3829static std::string
3830make_sort_after_prefix_name (const char *search_name)
3831{
3832 /* When looking to complete "func", we find the upper bound of all
3833 symbols that start with "func" by looking for where we'd insert
3834 the closest string that would follow "func" in lexicographical
3835 order. Usually, that's "func"-with-last-character-incremented,
3836 i.e. "fund". Mind non-ASCII characters, though. Usually those
3837 will be UTF-8 multi-byte sequences, but we can't be certain.
3838 Especially mind the 0xff character, which is a valid character in
3839 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3840 rule out compilers allowing it in identifiers. Note that
3841 conveniently, strcmp/strcasecmp are specified to compare
3842 characters interpreted as unsigned char. So what we do is treat
3843 the whole string as a base 256 number composed of a sequence of
3844 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3845 to 0, and carries 1 to the following more-significant position.
3846 If the very first character in SEARCH_NAME ends up incremented
3847 and carries/overflows, then the upper bound is the end of the
3848 list. The string after the empty string is also the empty
3849 string.
3850
3851 Some examples of this operation:
3852
3853 SEARCH_NAME => "+1" RESULT
3854
3855 "abc" => "abd"
3856 "ab\xff" => "ac"
3857 "\xff" "a" "\xff" => "\xff" "b"
3858 "\xff" => ""
3859 "\xff\xff" => ""
3860 "" => ""
3861
3862 Then, with these symbols for example:
3863
3864 func
3865 func1
3866 fund
3867
3868 completing "func" looks for symbols between "func" and
3869 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3870 which finds "func" and "func1", but not "fund".
3871
3872 And with:
3873
3874 funcÿ (Latin1 'ÿ' [0xff])
3875 funcÿ1
3876 fund
3877
3878 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3879 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3880
3881 And with:
3882
3883 ÿÿ (Latin1 'ÿ' [0xff])
3884 ÿÿ1
3885
3886 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3887 the end of the list.
3888 */
3889 std::string after = search_name;
3890 while (!after.empty () && (unsigned char) after.back () == 0xff)
3891 after.pop_back ();
3892 if (!after.empty ())
3893 after.back () = (unsigned char) after.back () + 1;
3894 return after;
3895}
3896
5c58de74 3897/* See declaration. */
61d96d7e 3898
5c58de74
PA
3899std::pair<std::vector<name_component>::const_iterator,
3900 std::vector<name_component>::const_iterator>
44ed8f3e 3901mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3902 (const lookup_name_info &lookup_name_without_params, language lang,
3903 dwarf2_per_objfile *per_objfile) const
3f563c84 3904{
5c58de74
PA
3905 auto *name_cmp
3906 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3907
3b00ef10 3908 const char *lang_name
e0802d59 3909 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3910
3f563c84
PA
3911 /* Comparison function object for lower_bound that matches against a
3912 given symbol name. */
3913 auto lookup_compare_lower = [&] (const name_component &elem,
3914 const char *name)
3915 {
fcf23d5b 3916 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3917 const char *elem_name = elem_qualified + elem.name_offset;
3918 return name_cmp (elem_name, name) < 0;
3919 };
3920
3921 /* Comparison function object for upper_bound that matches against a
3922 given symbol name. */
3923 auto lookup_compare_upper = [&] (const char *name,
3924 const name_component &elem)
3925 {
fcf23d5b 3926 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3927 const char *elem_name = elem_qualified + elem.name_offset;
3928 return name_cmp (name, elem_name) < 0;
3929 };
3930
5c58de74
PA
3931 auto begin = this->name_components.begin ();
3932 auto end = this->name_components.end ();
3f563c84
PA
3933
3934 /* Find the lower bound. */
3935 auto lower = [&] ()
3936 {
3b00ef10 3937 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3938 return begin;
3939 else
3b00ef10 3940 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3941 } ();
3942
3943 /* Find the upper bound. */
3944 auto upper = [&] ()
3945 {
5c58de74 3946 if (lookup_name_without_params.completion_mode ())
3f563c84 3947 {
e1ef7d7a
PA
3948 /* In completion mode, we want UPPER to point past all
3949 symbols names that have the same prefix. I.e., with
3950 these symbols, and completing "func":
3951
3952 function << lower bound
3953 function1
3954 other_function << upper bound
3955
3956 We find the upper bound by looking for the insertion
3957 point of "func"-with-last-character-incremented,
3958 i.e. "fund". */
3b00ef10 3959 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3960 if (after.empty ())
3f563c84 3961 return end;
e6b2f5ef
PA
3962 return std::lower_bound (lower, end, after.c_str (),
3963 lookup_compare_lower);
3f563c84
PA
3964 }
3965 else
3b00ef10 3966 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3967 } ();
3968
5c58de74
PA
3969 return {lower, upper};
3970}
3971
3972/* See declaration. */
3973
3974void
fcf23d5b 3975mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3976{
3977 if (!this->name_components.empty ())
3978 return;
3979
3980 this->name_components_casing = case_sensitivity;
3981 auto *name_cmp
3982 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3983
3984 /* The code below only knows how to break apart components of C++
3985 symbol names (and other languages that use '::' as
3b00ef10 3986 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3987 auto count = this->symbol_name_count ();
3988 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3989 {
44ed8f3e 3990 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3991 continue;
3992
fcf23d5b 3993 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3994
3995 /* Add each name component to the name component table. */
3996 unsigned int previous_len = 0;
3b00ef10
TT
3997
3998 if (strstr (name, "::") != nullptr)
3999 {
4000 for (unsigned int current_len = cp_find_first_component (name);
4001 name[current_len] != '\0';
4002 current_len += cp_find_first_component (name + current_len))
4003 {
4004 gdb_assert (name[current_len] == ':');
4005 this->name_components.push_back ({previous_len, idx});
4006 /* Skip the '::'. */
4007 current_len += 2;
4008 previous_len = current_len;
4009 }
4010 }
4011 else
5c58de74 4012 {
3b00ef10
TT
4013 /* Handle the Ada encoded (aka mangled) form here. */
4014 for (const char *iter = strstr (name, "__");
4015 iter != nullptr;
4016 iter = strstr (iter, "__"))
4017 {
4018 this->name_components.push_back ({previous_len, idx});
4019 iter += 2;
4020 previous_len = iter - name;
4021 }
5c58de74 4022 }
3b00ef10 4023
5c58de74
PA
4024 this->name_components.push_back ({previous_len, idx});
4025 }
4026
4027 /* Sort name_components elements by name. */
4028 auto name_comp_compare = [&] (const name_component &left,
4029 const name_component &right)
4030 {
fcf23d5b
SM
4031 const char *left_qualified
4032 = this->symbol_name_at (left.idx, per_objfile);
4033 const char *right_qualified
4034 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4035
4036 const char *left_name = left_qualified + left.name_offset;
4037 const char *right_name = right_qualified + right.name_offset;
4038
4039 return name_cmp (left_name, right_name) < 0;
4040 };
4041
4042 std::sort (this->name_components.begin (),
4043 this->name_components.end (),
4044 name_comp_compare);
4045}
4046
4047/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4048 mapped_index_base instead of the containing objfile. This is split
4049 to a separate function in order to be able to unit test the
4050 name_components matching using a mock mapped_index_base. For each
5c58de74 4051 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4052 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4053
4054static void
4055dw2_expand_symtabs_matching_symbol
44ed8f3e 4056 (mapped_index_base &index,
5c58de74
PA
4057 const lookup_name_info &lookup_name_in,
4058 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4059 enum search_domain kind,
fcf23d5b
SM
4060 gdb::function_view<bool (offset_type)> match_callback,
4061 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4062{
4063 lookup_name_info lookup_name_without_params
4064 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4065
4066 /* Build the symbol name component sorted vector, if we haven't
4067 yet. */
fcf23d5b 4068 index.build_name_components (per_objfile);
5c58de74 4069
3f563c84
PA
4070 /* The same symbol may appear more than once in the range though.
4071 E.g., if we're looking for symbols that complete "w", and we have
4072 a symbol named "w1::w2", we'll find the two name components for
4073 that same symbol in the range. To be sure we only call the
4074 callback once per symbol, we first collect the symbol name
4075 indexes that matched in a temporary vector and ignore
4076 duplicates. */
4077 std::vector<offset_type> matches;
3f563c84 4078
3b00ef10
TT
4079 struct name_and_matcher
4080 {
4081 symbol_name_matcher_ftype *matcher;
ecc6c606 4082 const char *name;
3b00ef10
TT
4083
4084 bool operator== (const name_and_matcher &other) const
3f563c84 4085 {
ecc6c606 4086 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4087 }
4088 };
4089
4090 /* A vector holding all the different symbol name matchers, for all
4091 languages. */
4092 std::vector<name_and_matcher> matchers;
4093
4094 for (int i = 0; i < nr_languages; i++)
4095 {
4096 enum language lang_e = (enum language) i;
4097
4098 const language_defn *lang = language_def (lang_e);
4099 symbol_name_matcher_ftype *name_matcher
c9debfb9 4100 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4101
3b00ef10
TT
4102 name_and_matcher key {
4103 name_matcher,
4104 lookup_name_without_params.language_lookup_name (lang_e)
4105 };
4106
4107 /* Don't insert the same comparison routine more than once.
4108 Note that we do this linear walk. This is not a problem in
4109 practice because the number of supported languages is
4110 low. */
4111 if (std::find (matchers.begin (), matchers.end (), key)
4112 != matchers.end ())
9291a0cd 4113 continue;
3b00ef10
TT
4114 matchers.push_back (std::move (key));
4115
4116 auto bounds
4117 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4118 lang_e, per_objfile);
3b00ef10
TT
4119
4120 /* Now for each symbol name in range, check to see if we have a name
4121 match, and if so, call the MATCH_CALLBACK callback. */
4122
4123 for (; bounds.first != bounds.second; ++bounds.first)
4124 {
fcf23d5b
SM
4125 const char *qualified
4126 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4127
4128 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4129 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4130 continue;
9291a0cd 4131
3b00ef10
TT
4132 matches.push_back (bounds.first->idx);
4133 }
3f563c84
PA
4134 }
4135
4136 std::sort (matches.begin (), matches.end ());
4137
4138 /* Finally call the callback, once per match. */
4139 ULONGEST prev = -1;
4140 for (offset_type idx : matches)
4141 {
4142 if (prev != idx)
4143 {
3b00ef10
TT
4144 if (!match_callback (idx))
4145 break;
3f563c84
PA
4146 prev = idx;
4147 }
4148 }
4149
4150 /* Above we use a type wider than idx's for 'prev', since 0 and
4151 (offset_type)-1 are both possible values. */
4152 static_assert (sizeof (prev) > sizeof (offset_type), "");
4153}
4154
c62446b1
PA
4155#if GDB_SELF_TEST
4156
4157namespace selftests { namespace dw2_expand_symtabs_matching {
4158
a3c5fafd
PA
4159/* A mock .gdb_index/.debug_names-like name index table, enough to
4160 exercise dw2_expand_symtabs_matching_symbol, which works with the
4161 mapped_index_base interface. Builds an index from the symbol list
4162 passed as parameter to the constructor. */
4163class mock_mapped_index : public mapped_index_base
c62446b1
PA
4164{
4165public:
a3c5fafd
PA
4166 mock_mapped_index (gdb::array_view<const char *> symbols)
4167 : m_symbol_table (symbols)
c62446b1
PA
4168 {}
4169
a3c5fafd 4170 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4171
a3c5fafd 4172 /* Return the number of names in the symbol table. */
632e107b 4173 size_t symbol_name_count () const override
c62446b1 4174 {
a3c5fafd 4175 return m_symbol_table.size ();
c62446b1
PA
4176 }
4177
a3c5fafd 4178 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4179 const char *symbol_name_at
4180 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4181 {
4182 return m_symbol_table[idx];
4183 }
c62446b1 4184
a3c5fafd
PA
4185private:
4186 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4187};
4188
4189/* Convenience function that converts a NULL pointer to a "<null>"
4190 string, to pass to print routines. */
4191
4192static const char *
4193string_or_null (const char *str)
4194{
4195 return str != NULL ? str : "<null>";
4196}
4197
4198/* Check if a lookup_name_info built from
4199 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4200 index. EXPECTED_LIST is the list of expected matches, in expected
4201 matching order. If no match expected, then an empty list is
4202 specified. Returns true on success. On failure prints a warning
4203 indicating the file:line that failed, and returns false. */
4204
4205static bool
4206check_match (const char *file, int line,
4207 mock_mapped_index &mock_index,
4208 const char *name, symbol_name_match_type match_type,
4209 bool completion_mode,
fcf23d5b
SM
4210 std::initializer_list<const char *> expected_list,
4211 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4212{
4213 lookup_name_info lookup_name (name, match_type, completion_mode);
4214
4215 bool matched = true;
4216
4217 auto mismatch = [&] (const char *expected_str,
4218 const char *got)
4219 {
4220 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4221 "expected=\"%s\", got=\"%s\"\n"),
4222 file, line,
4223 (match_type == symbol_name_match_type::FULL
4224 ? "FULL" : "WILD"),
4225 name, string_or_null (expected_str), string_or_null (got));
4226 matched = false;
4227 };
4228
4229 auto expected_it = expected_list.begin ();
4230 auto expected_end = expected_list.end ();
4231
a3c5fafd 4232 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4233 NULL, ALL_DOMAIN,
4234 [&] (offset_type idx)
4235 {
fcf23d5b 4236 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4237 const char *expected_str
4238 = expected_it == expected_end ? NULL : *expected_it++;
4239
4240 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4241 mismatch (expected_str, matched_name);
3b00ef10 4242 return true;
fcf23d5b 4243 }, per_objfile);
c62446b1
PA
4244
4245 const char *expected_str
4246 = expected_it == expected_end ? NULL : *expected_it++;
4247 if (expected_str != NULL)
4248 mismatch (expected_str, NULL);
4249
4250 return matched;
4251}
4252
4253/* The symbols added to the mock mapped_index for testing (in
4254 canonical form). */
4255static const char *test_symbols[] = {
4256 "function",
4257 "std::bar",
4258 "std::zfunction",
4259 "std::zfunction2",
4260 "w1::w2",
4261 "ns::foo<char*>",
4262 "ns::foo<int>",
4263 "ns::foo<long>",
a20714ff
PA
4264 "ns2::tmpl<int>::foo2",
4265 "(anonymous namespace)::A::B::C",
c62446b1 4266
e1ef7d7a
PA
4267 /* These are used to check that the increment-last-char in the
4268 matching algorithm for completion doesn't match "t1_fund" when
4269 completing "t1_func". */
4270 "t1_func",
4271 "t1_func1",
4272 "t1_fund",
4273 "t1_fund1",
4274
4275 /* A UTF-8 name with multi-byte sequences to make sure that
4276 cp-name-parser understands this as a single identifier ("função"
4277 is "function" in PT). */
4278 u8"u8função",
4279
4280 /* \377 (0xff) is Latin1 'ÿ'. */
4281 "yfunc\377",
4282
4283 /* \377 (0xff) is Latin1 'ÿ'. */
4284 "\377",
4285 "\377\377123",
4286
c62446b1
PA
4287 /* A name with all sorts of complications. Starts with "z" to make
4288 it easier for the completion tests below. */
4289#define Z_SYM_NAME \
4290 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4291 "::tuple<(anonymous namespace)::ui*, " \
4292 "std::default_delete<(anonymous namespace)::ui>, void>"
4293
4294 Z_SYM_NAME
4295};
4296
a3c5fafd
PA
4297/* Returns true if the mapped_index_base::find_name_component_bounds
4298 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4299 in completion mode. */
5c58de74
PA
4300
4301static bool
a3c5fafd 4302check_find_bounds_finds (mapped_index_base &index,
5c58de74 4303 const char *search_name,
fcf23d5b
SM
4304 gdb::array_view<const char *> expected_syms,
4305 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4306{
4307 lookup_name_info lookup_name (search_name,
4308 symbol_name_match_type::FULL, true);
4309
3b00ef10 4310 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4311 language_cplus,
4312 per_objfile);
5c58de74
PA
4313
4314 size_t distance = std::distance (bounds.first, bounds.second);
4315 if (distance != expected_syms.size ())
4316 return false;
4317
4318 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4319 {
4320 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4321 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4322 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4323 return false;
4324 }
4325
4326 return true;
4327}
4328
4329/* Test the lower-level mapped_index::find_name_component_bounds
4330 method. */
4331
c62446b1 4332static void
5c58de74
PA
4333test_mapped_index_find_name_component_bounds ()
4334{
4335 mock_mapped_index mock_index (test_symbols);
4336
fcf23d5b 4337 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4338
4339 /* Test the lower-level mapped_index::find_name_component_bounds
4340 method in completion mode. */
4341 {
4342 static const char *expected_syms[] = {
4343 "t1_func",
4344 "t1_func1",
5c58de74
PA
4345 };
4346
fcf23d5b
SM
4347 SELF_CHECK (check_find_bounds_finds
4348 (mock_index, "t1_func", expected_syms,
4349 NULL /* per_objfile */));
5c58de74
PA
4350 }
4351
4352 /* Check that the increment-last-char in the name matching algorithm
4353 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4354 {
4355 static const char *expected_syms1[] = {
4356 "\377",
4357 "\377\377123",
4358 };
fcf23d5b
SM
4359 SELF_CHECK (check_find_bounds_finds
4360 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4361
4362 static const char *expected_syms2[] = {
4363 "\377\377123",
4364 };
fcf23d5b
SM
4365 SELF_CHECK (check_find_bounds_finds
4366 (mock_index, "\377\377", expected_syms2,
4367 NULL /* per_objfile */));
5c58de74
PA
4368 }
4369}
4370
4371/* Test dw2_expand_symtabs_matching_symbol. */
4372
4373static void
4374test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4375{
4376 mock_mapped_index mock_index (test_symbols);
4377
4378 /* We let all tests run until the end even if some fails, for debug
4379 convenience. */
4380 bool any_mismatch = false;
4381
4382 /* Create the expected symbols list (an initializer_list). Needed
4383 because lists have commas, and we need to pass them to CHECK,
4384 which is a macro. */
4385#define EXPECT(...) { __VA_ARGS__ }
4386
4387 /* Wrapper for check_match that passes down the current
4388 __FILE__/__LINE__. */
4389#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4390 any_mismatch |= !check_match (__FILE__, __LINE__, \
4391 mock_index, \
4392 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4393 EXPECTED_LIST, NULL)
c62446b1
PA
4394
4395 /* Identity checks. */
4396 for (const char *sym : test_symbols)
4397 {
4398 /* Should be able to match all existing symbols. */
4399 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4400 EXPECT (sym));
4401
4402 /* Should be able to match all existing symbols with
4403 parameters. */
4404 std::string with_params = std::string (sym) + "(int)";
4405 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4406 EXPECT (sym));
4407
4408 /* Should be able to match all existing symbols with
4409 parameters and qualifiers. */
4410 with_params = std::string (sym) + " ( int ) const";
4411 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4412 EXPECT (sym));
4413
4414 /* This should really find sym, but cp-name-parser.y doesn't
4415 know about lvalue/rvalue qualifiers yet. */
4416 with_params = std::string (sym) + " ( int ) &&";
4417 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4418 {});
4419 }
4420
e1ef7d7a
PA
4421 /* Check that the name matching algorithm for completion doesn't get
4422 confused with Latin1 'ÿ' / 0xff. */
4423 {
4424 static const char str[] = "\377";
4425 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4426 EXPECT ("\377", "\377\377123"));
4427 }
4428
4429 /* Check that the increment-last-char in the matching algorithm for
4430 completion doesn't match "t1_fund" when completing "t1_func". */
4431 {
4432 static const char str[] = "t1_func";
4433 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4434 EXPECT ("t1_func", "t1_func1"));
4435 }
4436
c62446b1
PA
4437 /* Check that completion mode works at each prefix of the expected
4438 symbol name. */
4439 {
4440 static const char str[] = "function(int)";
4441 size_t len = strlen (str);
4442 std::string lookup;
4443
4444 for (size_t i = 1; i < len; i++)
4445 {
4446 lookup.assign (str, i);
4447 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4448 EXPECT ("function"));
4449 }
4450 }
4451
4452 /* While "w" is a prefix of both components, the match function
4453 should still only be called once. */
4454 {
4455 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4456 EXPECT ("w1::w2"));
a20714ff
PA
4457 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4458 EXPECT ("w1::w2"));
c62446b1
PA
4459 }
4460
4461 /* Same, with a "complicated" symbol. */
4462 {
4463 static const char str[] = Z_SYM_NAME;
4464 size_t len = strlen (str);
4465 std::string lookup;
4466
4467 for (size_t i = 1; i < len; i++)
4468 {
4469 lookup.assign (str, i);
4470 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4471 EXPECT (Z_SYM_NAME));
4472 }
4473 }
4474
4475 /* In FULL mode, an incomplete symbol doesn't match. */
4476 {
4477 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4478 {});
4479 }
4480
4481 /* A complete symbol with parameters matches any overload, since the
4482 index has no overload info. */
4483 {
4484 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4485 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4486 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4487 EXPECT ("std::zfunction", "std::zfunction2"));
4488 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4489 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4490 }
4491
4492 /* Check that whitespace is ignored appropriately. A symbol with a
4493 template argument list. */
4494 {
4495 static const char expected[] = "ns::foo<int>";
4496 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4497 EXPECT (expected));
a20714ff
PA
4498 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4499 EXPECT (expected));
c62446b1
PA
4500 }
4501
4502 /* Check that whitespace is ignored appropriately. A symbol with a
4503 template argument list that includes a pointer. */
4504 {
4505 static const char expected[] = "ns::foo<char*>";
4506 /* Try both completion and non-completion modes. */
4507 static const bool completion_mode[2] = {false, true};
4508 for (size_t i = 0; i < 2; i++)
4509 {
4510 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4511 completion_mode[i], EXPECT (expected));
a20714ff
PA
4512 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4513 completion_mode[i], EXPECT (expected));
c62446b1
PA
4514
4515 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4516 completion_mode[i], EXPECT (expected));
a20714ff
PA
4517 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4518 completion_mode[i], EXPECT (expected));
c62446b1
PA
4519 }
4520 }
4521
4522 {
4523 /* Check method qualifiers are ignored. */
4524 static const char expected[] = "ns::foo<char*>";
4525 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4526 symbol_name_match_type::FULL, true, EXPECT (expected));
4527 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4528 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4529 CHECK_MATCH ("foo < char * > ( int ) const",
4530 symbol_name_match_type::WILD, true, EXPECT (expected));
4531 CHECK_MATCH ("foo < char * > ( int ) &&",
4532 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4533 }
4534
4535 /* Test lookup names that don't match anything. */
4536 {
a20714ff
PA
4537 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4538 {});
4539
c62446b1
PA
4540 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4541 {});
4542 }
4543
a20714ff
PA
4544 /* Some wild matching tests, exercising "(anonymous namespace)",
4545 which should not be confused with a parameter list. */
4546 {
4547 static const char *syms[] = {
4548 "A::B::C",
4549 "B::C",
4550 "C",
4551 "A :: B :: C ( int )",
4552 "B :: C ( int )",
4553 "C ( int )",
4554 };
4555
4556 for (const char *s : syms)
4557 {
4558 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4559 EXPECT ("(anonymous namespace)::A::B::C"));
4560 }
4561 }
4562
4563 {
4564 static const char expected[] = "ns2::tmpl<int>::foo2";
4565 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4566 EXPECT (expected));
4567 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4568 EXPECT (expected));
4569 }
4570
c62446b1
PA
4571 SELF_CHECK (!any_mismatch);
4572
4573#undef EXPECT
4574#undef CHECK_MATCH
4575}
4576
5c58de74
PA
4577static void
4578run_test ()
4579{
4580 test_mapped_index_find_name_component_bounds ();
4581 test_dw2_expand_symtabs_matching_symbol ();
4582}
4583
c62446b1
PA
4584}} // namespace selftests::dw2_expand_symtabs_matching
4585
4586#endif /* GDB_SELF_TEST */
4587
4b514bc8
JK
4588/* If FILE_MATCHER is NULL or if PER_CU has
4589 dwarf2_per_cu_quick_data::MARK set (see
4590 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4591 EXPANSION_NOTIFY on it. */
4592
4593static void
4594dw2_expand_symtabs_matching_one
97a1449a
SM
4595 (dwarf2_per_cu_data *per_cu,
4596 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4597 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4598 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4599{
4600 if (file_matcher == NULL || per_cu->v.quick->mark)
4601 {
af758d11 4602 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4603
97a1449a
SM
4604 compunit_symtab *symtab
4605 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4606 gdb_assert (symtab != nullptr);
4b514bc8 4607
af758d11
SM
4608 if (expansion_notify != NULL && symtab_was_null)
4609 expansion_notify (symtab);
4b514bc8
JK
4610 }
4611}
4612
3f563c84
PA
4613/* Helper for dw2_expand_matching symtabs. Called on each symbol
4614 matched, to expand corresponding CUs that were marked. IDX is the
4615 index of the symbol name that matched. */
4616
4617static void
4618dw2_expand_marked_cus
976ca316 4619 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4620 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4621 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4622 search_domain kind)
4623{
3f563c84
PA
4624 offset_type *vec, vec_len, vec_idx;
4625 bool global_seen = false;
976ca316 4626 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4627
61920122 4628 vec = (offset_type *) (index.constant_pool
f00a2de2 4629 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4630 vec_len = MAYBE_SWAP (vec[0]);
4631 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4632 {
61920122
PA
4633 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4634 /* This value is only valid for index versions >= 7. */
4635 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4636 gdb_index_symbol_kind symbol_kind =
4637 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4638 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4639 /* Only check the symbol attributes if they're present.
4640 Indices prior to version 7 don't record them,
4641 and indices >= 7 may elide them for certain symbols
4642 (gold does this). */
4643 int attrs_valid =
4644 (index.version >= 7
4645 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4646
4647 /* Work around gold/15646. */
f030440d
TV
4648 if (attrs_valid
4649 && !is_static
4650 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4651 {
f030440d 4652 if (global_seen)
61920122 4653 continue;
f030440d
TV
4654
4655 global_seen = true;
61920122 4656 }
3190f0c6 4657
61920122
PA
4658 /* Only check the symbol's kind if it has one. */
4659 if (attrs_valid)
4660 {
4661 switch (kind)
8943b874 4662 {
61920122
PA
4663 case VARIABLES_DOMAIN:
4664 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4665 continue;
4666 break;
4667 case FUNCTIONS_DOMAIN:
4668 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4669 continue;
61920122
PA
4670 break;
4671 case TYPES_DOMAIN:
4672 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4673 continue;
4674 break;
59c35742
AB
4675 case MODULES_DOMAIN:
4676 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4677 continue;
4678 break;
61920122
PA
4679 default:
4680 break;
8943b874 4681 }
61920122 4682 }
8943b874 4683
61920122 4684 /* Don't crash on bad data. */
976ca316
SM
4685 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4686 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4687 {
b98664d3 4688 complaint (_(".gdb_index entry has bad CU index"
976ca316 4689 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4690 continue;
4691 }
4692
976ca316
SM
4693 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4694 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4695 expansion_notify);
61920122
PA
4696 }
4697}
4698
4b514bc8
JK
4699/* If FILE_MATCHER is non-NULL, set all the
4700 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4701 that match FILE_MATCHER. */
4702
61920122 4703static void
4b514bc8 4704dw_expand_symtabs_matching_file_matcher
976ca316 4705 (dwarf2_per_objfile *per_objfile,
ed2dc618 4706 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4707{
4b514bc8 4708 if (file_matcher == NULL)
61920122
PA
4709 return;
4710
4b514bc8
JK
4711 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4712 htab_eq_pointer,
4713 NULL, xcalloc, xfree));
4714 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4715 htab_eq_pointer,
4716 NULL, xcalloc, xfree));
61920122 4717
4b514bc8
JK
4718 /* The rule is CUs specify all the files, including those used by
4719 any TU, so there's no need to scan TUs here. */
61920122 4720
976ca316 4721 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4722 {
927aa2e7
JK
4723 QUIT;
4724
4725 per_cu->v.quick->mark = 0;
4726
4727 /* We only need to look at symtabs not already expanded. */
976ca316 4728 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4729 continue;
4730
976ca316 4731 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4732 if (file_data == NULL)
4733 continue;
4734
4735 if (htab_find (visited_not_found.get (), file_data) != NULL)
4736 continue;
4737 else if (htab_find (visited_found.get (), file_data) != NULL)
4738 {
4739 per_cu->v.quick->mark = 1;
4740 continue;
4741 }
4742
b76e467d 4743 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4744 {
4745 const char *this_real_name;
4746
4747 if (file_matcher (file_data->file_names[j], false))
4748 {
4749 per_cu->v.quick->mark = 1;
4750 break;
4751 }
4752
4753 /* Before we invoke realpath, which can get expensive when many
4754 files are involved, do a quick comparison of the basenames. */
4755 if (!basenames_may_differ
4756 && !file_matcher (lbasename (file_data->file_names[j]),
4757 true))
4758 continue;
4759
976ca316 4760 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4761 if (file_matcher (this_real_name, false))
4762 {
4763 per_cu->v.quick->mark = 1;
4764 break;
4765 }
4766 }
4767
b76e467d
SM
4768 void **slot = htab_find_slot (per_cu->v.quick->mark
4769 ? visited_found.get ()
4770 : visited_not_found.get (),
4771 file_data, INSERT);
927aa2e7
JK
4772 *slot = file_data;
4773 }
4774}
4775
4776static void
4777dw2_expand_symtabs_matching
4778 (struct objfile *objfile,
4779 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4780 const lookup_name_info *lookup_name,
927aa2e7
JK
4781 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4782 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4783 enum search_domain kind)
4784{
976ca316 4785 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4786
4787 /* index_table is NULL if OBJF_READNOW. */
976ca316 4788 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4789 return;
4790
976ca316 4791 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4792
c1a66c06
TV
4793 if (symbol_matcher == NULL && lookup_name == NULL)
4794 {
976ca316 4795 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4796 {
4797 QUIT;
4798
976ca316 4799 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4800 file_matcher, expansion_notify);
c1a66c06
TV
4801 }
4802 return;
4803 }
4804
976ca316 4805 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4806
c1a66c06 4807 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4808 symbol_matcher,
4809 kind, [&] (offset_type idx)
4810 {
976ca316
SM
4811 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4812 kind);
3b00ef10 4813 return true;
976ca316 4814 }, per_objfile);
927aa2e7
JK
4815}
4816
4817/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4818 symtab. */
4819
4820static struct compunit_symtab *
4821recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4822 CORE_ADDR pc)
4823{
4824 int i;
4825
4826 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4827 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4828 return cust;
4829
4830 if (cust->includes == NULL)
4831 return NULL;
4832
4833 for (i = 0; cust->includes[i]; ++i)
4834 {
4835 struct compunit_symtab *s = cust->includes[i];
4836
4837 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4838 if (s != NULL)
4839 return s;
4840 }
4841
4842 return NULL;
4843}
4844
4845static struct compunit_symtab *
4846dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4847 struct bound_minimal_symbol msymbol,
4848 CORE_ADDR pc,
4849 struct obj_section *section,
4850 int warn_if_readin)
4851{
4852 struct dwarf2_per_cu_data *data;
4853 struct compunit_symtab *result;
4854
d320c2b5 4855 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4856 return NULL;
4857
b3b3bada 4858 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4859 data = (struct dwarf2_per_cu_data *) addrmap_find
4860 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4861 if (!data)
4862 return NULL;
4863
af758d11
SM
4864 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4865 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4866 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4867 paddress (objfile->arch (), pc));
927aa2e7 4868
97a1449a
SM
4869 result = recursively_find_pc_sect_compunit_symtab
4870 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4871
927aa2e7
JK
4872 gdb_assert (result != NULL);
4873 return result;
4874}
4875
4876static void
4877dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4878 void *data, int need_fullname)
4879{
976ca316 4880 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4881
976ca316 4882 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4883 {
976ca316 4884 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4885
4886 htab_up visited (htab_create_alloc (10,
4887 htab_hash_pointer, htab_eq_pointer,
4888 NULL, xcalloc, xfree));
4889
4890 /* The rule is CUs specify all the files, including those used
4891 by any TU, so there's no need to scan TUs here. We can
4892 ignore file names coming from already-expanded CUs. */
4893
976ca316 4894 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4895 {
976ca316 4896 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4897 {
4898 void **slot = htab_find_slot (visited.get (),
4899 per_cu->v.quick->file_names,
4900 INSERT);
4901
4902 *slot = per_cu->v.quick->file_names;
4903 }
4904 }
4905
976ca316 4906 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4907 {
927aa2e7 4908 /* We only need to look at symtabs not already expanded. */
976ca316 4909 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4910 continue;
4911
ab432490 4912 quick_file_names *file_data
976ca316 4913 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4914 if (file_data == NULL)
4915 continue;
4916
b76e467d 4917 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4918 if (*slot)
4919 {
4920 /* Already visited. */
4921 continue;
4922 }
4923 *slot = file_data;
4924
4925 for (int j = 0; j < file_data->num_file_names; ++j)
4926 {
4927 const char *filename = file_data->file_names[j];
976ca316 4928 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4929 }
4930 }
4931 }
4932
976ca316 4933 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4934 {
4935 gdb::unique_xmalloc_ptr<char> this_real_name;
4936
4937 if (need_fullname)
4938 this_real_name = gdb_realpath (filename);
4939 (*fun) (filename, this_real_name.get (), data);
4940 });
4941}
4942
4943static int
4944dw2_has_symbols (struct objfile *objfile)
4945{
4946 return 1;
4947}
4948
4949const struct quick_symbol_functions dwarf2_gdb_index_functions =
4950{
4951 dw2_has_symbols,
4952 dw2_find_last_source_symtab,
4953 dw2_forget_cached_source_info,
4954 dw2_map_symtabs_matching_filename,
4955 dw2_lookup_symbol,
d3214198 4956 NULL,
927aa2e7
JK
4957 dw2_print_stats,
4958 dw2_dump,
927aa2e7
JK
4959 dw2_expand_symtabs_for_function,
4960 dw2_expand_all_symtabs,
4961 dw2_expand_symtabs_with_fullname,
4962 dw2_map_matching_symbols,
4963 dw2_expand_symtabs_matching,
4964 dw2_find_pc_sect_compunit_symtab,
4965 NULL,
4966 dw2_map_symbol_filenames
4967};
4968
4969/* DWARF-5 debug_names reader. */
4970
4971/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4972static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4973
4974/* A helper function that reads the .debug_names section in SECTION
4975 and fills in MAP. FILENAME is the name of the file containing the
4976 section; it is used for error reporting.
4977
4978 Returns true if all went well, false otherwise. */
4979
4980static bool
4981read_debug_names_from_section (struct objfile *objfile,
4982 const char *filename,
4983 struct dwarf2_section_info *section,
4984 mapped_debug_names &map)
4985{
96b79293 4986 if (section->empty ())
927aa2e7
JK
4987 return false;
4988
4989 /* Older elfutils strip versions could keep the section in the main
4990 executable while splitting it for the separate debug info file. */
96b79293 4991 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4992 return false;
4993
96b79293 4994 section->read (objfile);
927aa2e7 4995
08feed99 4996 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4997
4998 const gdb_byte *addr = section->buffer;
4999
96b79293 5000 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5001
5002 unsigned int bytes_read;
5003 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5004 addr += bytes_read;
5005
5006 map.dwarf5_is_dwarf64 = bytes_read != 4;
5007 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5008 if (bytes_read + length != section->size)
5009 {
5010 /* There may be multiple per-CU indices. */
5011 warning (_("Section .debug_names in %s length %s does not match "
5012 "section length %s, ignoring .debug_names."),
5013 filename, plongest (bytes_read + length),
5014 pulongest (section->size));
5015 return false;
5016 }
5017
5018 /* The version number. */
5019 uint16_t version = read_2_bytes (abfd, addr);
5020 addr += 2;
5021 if (version != 5)
5022 {
5023 warning (_("Section .debug_names in %s has unsupported version %d, "
5024 "ignoring .debug_names."),
5025 filename, version);
5026 return false;
5027 }
5028
5029 /* Padding. */
5030 uint16_t padding = read_2_bytes (abfd, addr);
5031 addr += 2;
5032 if (padding != 0)
5033 {
5034 warning (_("Section .debug_names in %s has unsupported padding %d, "
5035 "ignoring .debug_names."),
5036 filename, padding);
5037 return false;
5038 }
5039
5040 /* comp_unit_count - The number of CUs in the CU list. */
5041 map.cu_count = read_4_bytes (abfd, addr);
5042 addr += 4;
5043
5044 /* local_type_unit_count - The number of TUs in the local TU
5045 list. */
5046 map.tu_count = read_4_bytes (abfd, addr);
5047 addr += 4;
5048
5049 /* foreign_type_unit_count - The number of TUs in the foreign TU
5050 list. */
5051 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5052 addr += 4;
5053 if (foreign_tu_count != 0)
5054 {
5055 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5056 "ignoring .debug_names."),
5057 filename, static_cast<unsigned long> (foreign_tu_count));
5058 return false;
5059 }
5060
5061 /* bucket_count - The number of hash buckets in the hash lookup
5062 table. */
5063 map.bucket_count = read_4_bytes (abfd, addr);
5064 addr += 4;
5065
5066 /* name_count - The number of unique names in the index. */
5067 map.name_count = read_4_bytes (abfd, addr);
5068 addr += 4;
5069
5070 /* abbrev_table_size - The size in bytes of the abbreviations
5071 table. */
5072 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5073 addr += 4;
5074
5075 /* augmentation_string_size - The size in bytes of the augmentation
5076 string. This value is rounded up to a multiple of 4. */
5077 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5078 addr += 4;
5079 map.augmentation_is_gdb = ((augmentation_string_size
5080 == sizeof (dwarf5_augmentation))
5081 && memcmp (addr, dwarf5_augmentation,
5082 sizeof (dwarf5_augmentation)) == 0);
5083 augmentation_string_size += (-augmentation_string_size) & 3;
5084 addr += augmentation_string_size;
5085
5086 /* List of CUs */
5087 map.cu_table_reordered = addr;
5088 addr += map.cu_count * map.offset_size;
5089
5090 /* List of Local TUs */
5091 map.tu_table_reordered = addr;
5092 addr += map.tu_count * map.offset_size;
5093
5094 /* Hash Lookup Table */
5095 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5096 addr += map.bucket_count * 4;
5097 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5098 addr += map.name_count * 4;
5099
5100 /* Name Table */
5101 map.name_table_string_offs_reordered = addr;
5102 addr += map.name_count * map.offset_size;
5103 map.name_table_entry_offs_reordered = addr;
5104 addr += map.name_count * map.offset_size;
5105
5106 const gdb_byte *abbrev_table_start = addr;
5107 for (;;)
5108 {
927aa2e7
JK
5109 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5110 addr += bytes_read;
5111 if (index_num == 0)
5112 break;
5113
5114 const auto insertpair
5115 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5116 if (!insertpair.second)
5117 {
5118 warning (_("Section .debug_names in %s has duplicate index %s, "
5119 "ignoring .debug_names."),
5120 filename, pulongest (index_num));
5121 return false;
5122 }
5123 mapped_debug_names::index_val &indexval = insertpair.first->second;
5124 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5125 addr += bytes_read;
5126
5127 for (;;)
5128 {
5129 mapped_debug_names::index_val::attr attr;
5130 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5131 addr += bytes_read;
5132 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5133 addr += bytes_read;
5134 if (attr.form == DW_FORM_implicit_const)
5135 {
5136 attr.implicit_const = read_signed_leb128 (abfd, addr,
5137 &bytes_read);
5138 addr += bytes_read;
5139 }
5140 if (attr.dw_idx == 0 && attr.form == 0)
5141 break;
5142 indexval.attr_vec.push_back (std::move (attr));
5143 }
5144 }
5145 if (addr != abbrev_table_start + abbrev_table_size)
5146 {
5147 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5148 "of size %s vs. written as %u, ignoring .debug_names."),
5149 filename, plongest (addr - abbrev_table_start),
5150 abbrev_table_size);
927aa2e7
JK
5151 return false;
5152 }
5153 map.entry_pool = addr;
5154
5155 return true;
5156}
5157
5158/* A helper for create_cus_from_debug_names that handles the MAP's CU
5159 list. */
5160
5161static void
168c9250 5162create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5163 const mapped_debug_names &map,
5164 dwarf2_section_info &section,
b76e467d 5165 bool is_dwz)
927aa2e7 5166{
3ee6bb11
TV
5167 if (!map.augmentation_is_gdb)
5168 {
5169 for (uint32_t i = 0; i < map.cu_count; ++i)
5170 {
5171 sect_offset sect_off
5172 = (sect_offset) (extract_unsigned_integer
5173 (map.cu_table_reordered + i * map.offset_size,
5174 map.offset_size,
5175 map.dwarf5_byte_order));
5176 /* We don't know the length of the CU, because the CU list in a
5177 .debug_names index can be incomplete, so we can't use the start of
5178 the next CU as end of this CU. We create the CUs here with length 0,
5179 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5180 dwarf2_per_cu_data *per_cu
168c9250
SM
5181 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5182 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5183 }
5184 }
5185
927aa2e7
JK
5186 sect_offset sect_off_prev;
5187 for (uint32_t i = 0; i <= map.cu_count; ++i)
5188 {
5189 sect_offset sect_off_next;
5190 if (i < map.cu_count)
5191 {
5192 sect_off_next
5193 = (sect_offset) (extract_unsigned_integer
5194 (map.cu_table_reordered + i * map.offset_size,
5195 map.offset_size,
5196 map.dwarf5_byte_order));
5197 }
5198 else
5199 sect_off_next = (sect_offset) section.size;
5200 if (i >= 1)
5201 {
5202 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5203 dwarf2_per_cu_data *per_cu
168c9250 5204 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5205 sect_off_prev, length);
168c9250 5206 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5207 }
5208 sect_off_prev = sect_off_next;
5209 }
5210}
5211
5212/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5213 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5214
5215static void
168c9250 5216create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5217 const mapped_debug_names &map,
5218 const mapped_debug_names &dwz_map)
5219{
168c9250
SM
5220 gdb_assert (per_bfd->all_comp_units.empty ());
5221 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5222
168c9250 5223 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5224 false /* is_dwz */);
927aa2e7
JK
5225
5226 if (dwz_map.cu_count == 0)
5227 return;
5228
168c9250
SM
5229 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5230 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5231 true /* is_dwz */);
927aa2e7
JK
5232}
5233
5234/* Read .debug_names. If everything went ok, initialize the "quick"
5235 elements of all the CUs and return true. Otherwise, return false. */
5236
5237static bool
976ca316 5238dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5239{
fcf23d5b
SM
5240 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5241 mapped_debug_names dwz_map;
976ca316 5242 struct objfile *objfile = per_objfile->objfile;
f8c41851 5243 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5244
5245 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5246 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5247 return false;
5248
5249 /* Don't use the index if it's empty. */
22ca247e 5250 if (map->name_count == 0)
927aa2e7
JK
5251 return false;
5252
5253 /* If there is a .dwz file, read it so we can get its CU list as
5254 well. */
f8c41851 5255 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5256 if (dwz != NULL)
5257 {
5258 if (!read_debug_names_from_section (objfile,
00f93c44 5259 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5260 &dwz->debug_names, dwz_map))
5261 {
5262 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5263 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5264 return false;
5265 }
5266 }
5267
f8c41851 5268 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5269
22ca247e 5270 if (map->tu_count != 0)
927aa2e7
JK
5271 {
5272 /* We can only handle a single .debug_types when we have an
5273 index. */
f8c41851 5274 if (per_bfd->types.size () != 1)
927aa2e7
JK
5275 return false;
5276
f8c41851 5277 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5278
5279 create_signatured_type_table_from_debug_names
f8c41851 5280 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5281 }
5282
f8c41851 5283 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5284
f8c41851
SM
5285 per_bfd->debug_names_table = std::move (map);
5286 per_bfd->using_index = 1;
5287 per_bfd->quick_file_names_table =
976ca316 5288 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5289
f8c41851
SM
5290 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5291 objfiles using the same BFD. */
5292 gdb_assert (per_bfd->partial_symtabs == nullptr);
5293 per_bfd->partial_symtabs = objfile->partial_symtabs;
5294
927aa2e7
JK
5295 return true;
5296}
5297
927aa2e7
JK
5298/* Type used to manage iterating over all CUs looking for a symbol for
5299 .debug_names. */
5300
5301class dw2_debug_names_iterator
5302{
5303public:
927aa2e7 5304 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5305 gdb::optional<block_enum> block_index,
5306 domain_enum domain,
fcf23d5b 5307 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5308 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5309 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5310 m_per_objfile (per_objfile)
927aa2e7
JK
5311 {}
5312
5313 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5314 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5315 : m_map (map),
5316 m_search (search),
fcf23d5b
SM
5317 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5318 m_per_objfile (per_objfile)
927aa2e7
JK
5319 {}
5320
3b00ef10
TT
5321 dw2_debug_names_iterator (const mapped_debug_names &map,
5322 block_enum block_index, domain_enum domain,
fcf23d5b 5323 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5324 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5325 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5326 m_per_objfile (per_objfile)
3b00ef10
TT
5327 {}
5328
927aa2e7
JK
5329 /* Return the next matching CU or NULL if there are no more. */
5330 dwarf2_per_cu_data *next ();
5331
5332private:
5333 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5334 const char *name,
5335 dwarf2_per_objfile *per_objfile);
927aa2e7 5336 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5337 uint32_t namei,
5338 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5339
5340 /* The internalized form of .debug_names. */
5341 const mapped_debug_names &m_map;
5342
2b79f376
SM
5343 /* If set, only look for symbols that match that block. Valid values are
5344 GLOBAL_BLOCK and STATIC_BLOCK. */
5345 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5346
5347 /* The kind of symbol we're looking for. */
5348 const domain_enum m_domain = UNDEF_DOMAIN;
5349 const search_domain m_search = ALL_DOMAIN;
5350
5351 /* The list of CUs from the index entry of the symbol, or NULL if
5352 not found. */
5353 const gdb_byte *m_addr;
fcf23d5b
SM
5354
5355 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5356};
5357
5358const char *
fcf23d5b 5359mapped_debug_names::namei_to_name
976ca316 5360 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5361{
5362 const ULONGEST namei_string_offs
5363 = extract_unsigned_integer ((name_table_string_offs_reordered
5364 + namei * offset_size),
5365 offset_size,
5366 dwarf5_byte_order);
976ca316 5367 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5368}
5369
5370/* Find a slot in .debug_names for the object named NAME. If NAME is
5371 found, return pointer to its pool data. If NAME cannot be found,
5372 return NULL. */
5373
5374const gdb_byte *
5375dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5376 (const mapped_debug_names &map, const char *name,
5377 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5378{
5379 int (*cmp) (const char *, const char *);
5380
54ee4252 5381 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5382 if (current_language->la_language == language_cplus
5383 || current_language->la_language == language_fortran
5384 || current_language->la_language == language_d)
5385 {
5386 /* NAME is already canonical. Drop any qualifiers as
5387 .debug_names does not contain any. */
5388
5389 if (strchr (name, '(') != NULL)
5390 {
54ee4252 5391 without_params = cp_remove_params (name);
927aa2e7 5392 if (without_params != NULL)
54ee4252 5393 name = without_params.get ();
927aa2e7
JK
5394 }
5395 }
5396
5397 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5398
5399 const uint32_t full_hash = dwarf5_djb_hash (name);
5400 uint32_t namei
5401 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5402 (map.bucket_table_reordered
5403 + (full_hash % map.bucket_count)), 4,
5404 map.dwarf5_byte_order);
5405 if (namei == 0)
5406 return NULL;
5407 --namei;
5408 if (namei >= map.name_count)
5409 {
b98664d3 5410 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5411 "[in module %s]"),
5412 namei, map.name_count,
fcf23d5b 5413 objfile_name (per_objfile->objfile));
927aa2e7
JK
5414 return NULL;
5415 }
5416
5417 for (;;)
5418 {
5419 const uint32_t namei_full_hash
5420 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5421 (map.hash_table_reordered + namei), 4,
5422 map.dwarf5_byte_order);
5423 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5424 return NULL;
5425
5426 if (full_hash == namei_full_hash)
5427 {
fcf23d5b 5428 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5429
5430#if 0 /* An expensive sanity check. */
5431 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5432 {
b98664d3 5433 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5434 "[in module %s]"),
5435 namei, objfile_name (dwarf2_per_objfile->objfile));
5436 return NULL;
5437 }
5438#endif
5439
5440 if (cmp (namei_string, name) == 0)
5441 {
5442 const ULONGEST namei_entry_offs
5443 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5444 + namei * map.offset_size),
5445 map.offset_size, map.dwarf5_byte_order);
5446 return map.entry_pool + namei_entry_offs;
5447 }
5448 }
5449
5450 ++namei;
5451 if (namei >= map.name_count)
5452 return NULL;
5453 }
5454}
5455
5456const gdb_byte *
5457dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5458 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5459{
5460 if (namei >= map.name_count)
5461 {
b98664d3 5462 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5463 "[in module %s]"),
5464 namei, map.name_count,
fcf23d5b 5465 objfile_name (per_objfile->objfile));
927aa2e7
JK
5466 return NULL;
5467 }
5468
5469 const ULONGEST namei_entry_offs
5470 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5471 + namei * map.offset_size),
5472 map.offset_size, map.dwarf5_byte_order);
5473 return map.entry_pool + namei_entry_offs;
5474}
5475
5476/* See dw2_debug_names_iterator. */
5477
5478dwarf2_per_cu_data *
5479dw2_debug_names_iterator::next ()
5480{
5481 if (m_addr == NULL)
5482 return NULL;
5483
fcf23d5b
SM
5484 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5485 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5486 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5487
5488 again:
5489
5490 unsigned int bytes_read;
5491 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5492 m_addr += bytes_read;
5493 if (abbrev == 0)
5494 return NULL;
5495
5496 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5497 if (indexval_it == m_map.abbrev_map.cend ())
5498 {
b98664d3 5499 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5500 "[in module %s]"),
ed2dc618 5501 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5502 return NULL;
5503 }
5504 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5505 enum class symbol_linkage {
5506 unknown,
5507 static_,
5508 extern_,
23c13d42 5509 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5510 dwarf2_per_cu_data *per_cu = NULL;
5511 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5512 {
5513 ULONGEST ull;
5514 switch (attr.form)
5515 {
5516 case DW_FORM_implicit_const:
5517 ull = attr.implicit_const;
5518 break;
5519 case DW_FORM_flag_present:
5520 ull = 1;
5521 break;
5522 case DW_FORM_udata:
5523 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5524 m_addr += bytes_read;
5525 break;
6dc55ce9 5526 case DW_FORM_ref4:
5527 ull = read_4_bytes (abfd, m_addr);
5528 m_addr += 4;
5529 break;
5530 case DW_FORM_ref8:
5531 ull = read_8_bytes (abfd, m_addr);
5532 m_addr += 8;
5533 break;
5534 case DW_FORM_ref_sig8:
5535 ull = read_8_bytes (abfd, m_addr);
5536 m_addr += 8;
5537 break;
927aa2e7 5538 default:
b98664d3 5539 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5540 dwarf_form_name (attr.form),
ed2dc618 5541 objfile_name (objfile));
927aa2e7
JK
5542 return NULL;
5543 }
5544 switch (attr.dw_idx)
5545 {
5546 case DW_IDX_compile_unit:
5547 /* Don't crash on bad data. */
fcf23d5b 5548 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5549 {
b98664d3 5550 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5551 " [in module %s]"),
5552 pulongest (ull),
fcf23d5b 5553 objfile_name (objfile));
927aa2e7
JK
5554 continue;
5555 }
fcf23d5b 5556 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5557 break;
8af5c486
JK
5558 case DW_IDX_type_unit:
5559 /* Don't crash on bad data. */
fcf23d5b 5560 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5561 {
b98664d3 5562 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5563 " [in module %s]"),
5564 pulongest (ull),
fcf23d5b 5565 objfile_name (objfile));
8af5c486
JK
5566 continue;
5567 }
fcf23d5b 5568 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5569 break;
6dc55ce9 5570 case DW_IDX_die_offset:
5571 /* In a per-CU index (as opposed to a per-module index), index
5572 entries without CU attribute implicitly refer to the single CU. */
5573 if (per_cu == NULL)
fcf23d5b 5574 per_cu = per_bfd->get_cu (0);
6dc55ce9 5575 break;
927aa2e7
JK
5576 case DW_IDX_GNU_internal:
5577 if (!m_map.augmentation_is_gdb)
5578 break;
23c13d42 5579 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5580 break;
5581 case DW_IDX_GNU_external:
5582 if (!m_map.augmentation_is_gdb)
5583 break;
23c13d42 5584 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5585 break;
5586 }
5587 }
5588
5589 /* Skip if already read in. */
fcf23d5b 5590 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5591 goto again;
5592
5593 /* Check static vs global. */
23c13d42 5594 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5595 {
2b79f376 5596 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5597 const bool symbol_is_static =
5598 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5599 if (want_static != symbol_is_static)
2b79f376 5600 goto again;
927aa2e7
JK
5601 }
5602
5603 /* Match dw2_symtab_iter_next, symbol_kind
5604 and debug_names::psymbol_tag. */
5605 switch (m_domain)
5606 {
5607 case VAR_DOMAIN:
5608 switch (indexval.dwarf_tag)
5609 {
5610 case DW_TAG_variable:
5611 case DW_TAG_subprogram:
5612 /* Some types are also in VAR_DOMAIN. */
5613 case DW_TAG_typedef:
5614 case DW_TAG_structure_type:
5615 break;
5616 default:
5617 goto again;
5618 }
5619 break;
5620 case STRUCT_DOMAIN:
5621 switch (indexval.dwarf_tag)
5622 {
5623 case DW_TAG_typedef:
5624 case DW_TAG_structure_type:
5625 break;
5626 default:
5627 goto again;
5628 }
5629 break;
5630 case LABEL_DOMAIN:
5631 switch (indexval.dwarf_tag)
5632 {
5633 case 0:
5634 case DW_TAG_variable:
5635 break;
5636 default:
5637 goto again;
5638 }
5639 break;
59c35742
AB
5640 case MODULE_DOMAIN:
5641 switch (indexval.dwarf_tag)
5642 {
5643 case DW_TAG_module:
5644 break;
5645 default:
5646 goto again;
5647 }
5648 break;
927aa2e7
JK
5649 default:
5650 break;
5651 }
5652
5653 /* Match dw2_expand_symtabs_matching, symbol_kind and
5654 debug_names::psymbol_tag. */
5655 switch (m_search)
4b514bc8 5656 {
927aa2e7
JK
5657 case VARIABLES_DOMAIN:
5658 switch (indexval.dwarf_tag)
4b514bc8 5659 {
927aa2e7
JK
5660 case DW_TAG_variable:
5661 break;
5662 default:
5663 goto again;
4b514bc8 5664 }
927aa2e7
JK
5665 break;
5666 case FUNCTIONS_DOMAIN:
5667 switch (indexval.dwarf_tag)
4b514bc8 5668 {
927aa2e7
JK
5669 case DW_TAG_subprogram:
5670 break;
5671 default:
5672 goto again;
4b514bc8 5673 }
927aa2e7
JK
5674 break;
5675 case TYPES_DOMAIN:
5676 switch (indexval.dwarf_tag)
5677 {
5678 case DW_TAG_typedef:
5679 case DW_TAG_structure_type:
5680 break;
5681 default:
5682 goto again;
5683 }
5684 break;
59c35742
AB
5685 case MODULES_DOMAIN:
5686 switch (indexval.dwarf_tag)
5687 {
5688 case DW_TAG_module:
5689 break;
5690 default:
5691 goto again;
5692 }
927aa2e7
JK
5693 default:
5694 break;
4b514bc8 5695 }
927aa2e7
JK
5696
5697 return per_cu;
4b514bc8 5698}
61920122 5699
927aa2e7 5700static struct compunit_symtab *
c7f839cb 5701dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5702 const char *name, domain_enum domain)
4b514bc8 5703{
976ca316 5704 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5705
976ca316 5706 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5707 if (!mapp)
61920122 5708 {
927aa2e7
JK
5709 /* index is NULL if OBJF_READNOW. */
5710 return NULL;
5711 }
5712 const auto &map = *mapp;
9291a0cd 5713
976ca316 5714 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5715
927aa2e7
JK
5716 struct compunit_symtab *stab_best = NULL;
5717 struct dwarf2_per_cu_data *per_cu;
5718 while ((per_cu = iter.next ()) != NULL)
5719 {
5720 struct symbol *sym, *with_opaque = NULL;
97a1449a 5721 compunit_symtab *stab
976ca316 5722 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5723 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5724 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5725
927aa2e7
JK
5726 sym = block_find_symbol (block, name, domain,
5727 block_find_non_opaque_type_preferred,
5728 &with_opaque);
9703b513 5729
927aa2e7
JK
5730 /* Some caution must be observed with overloaded functions and
5731 methods, since the index will not contain any overload
5732 information (but NAME might contain it). */
a3ec0bb1 5733
927aa2e7 5734 if (sym != NULL
987012b8 5735 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5736 return stab;
5737 if (with_opaque != NULL
987012b8 5738 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5739 stab_best = stab;
9703b513 5740
927aa2e7 5741 /* Keep looking through other CUs. */
9703b513
TT
5742 }
5743
927aa2e7 5744 return stab_best;
9703b513
TT
5745}
5746
927aa2e7
JK
5747/* This dumps minimal information about .debug_names. It is called
5748 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5749 uses this to verify that .debug_names has been loaded. */
9291a0cd 5750
927aa2e7
JK
5751static void
5752dw2_debug_names_dump (struct objfile *objfile)
5753{
976ca316 5754 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5755
976ca316 5756 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5757 printf_filtered (".debug_names:");
976ca316 5758 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5759 printf_filtered (" exists\n");
5760 else
5761 printf_filtered (" faked for \"readnow\"\n");
5762 printf_filtered ("\n");
9291a0cd
TT
5763}
5764
9291a0cd 5765static void
927aa2e7
JK
5766dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5767 const char *func_name)
9291a0cd 5768{
976ca316 5769 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5770
976ca316
SM
5771 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5772 if (per_objfile->per_bfd->debug_names_table)
24c79950 5773 {
976ca316 5774 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5775
fcf23d5b 5776 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5777 per_objfile);
24c79950 5778
927aa2e7
JK
5779 struct dwarf2_per_cu_data *per_cu;
5780 while ((per_cu = iter.next ()) != NULL)
976ca316 5781 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5782 }
5783}
24c79950 5784
3b00ef10
TT
5785static void
5786dw2_debug_names_map_matching_symbols
5787 (struct objfile *objfile,
5788 const lookup_name_info &name, domain_enum domain,
5789 int global,
5790 gdb::function_view<symbol_found_callback_ftype> callback,
5791 symbol_compare_ftype *ordered_compare)
5792{
976ca316 5793 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5794
5795 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5796 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5797 return;
5798
976ca316 5799 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5800 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5801
5802 const char *match_name = name.ada ().lookup_name ().c_str ();
5803 auto matcher = [&] (const char *symname)
5804 {
5805 if (ordered_compare == nullptr)
5806 return true;
5807 return ordered_compare (symname, match_name) == 0;
5808 };
5809
5810 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5811 [&] (offset_type namei)
5812 {
5813 /* The name was matched, now expand corresponding CUs that were
5814 marked. */
fcf23d5b 5815 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5816 per_objfile);
3b00ef10
TT
5817
5818 struct dwarf2_per_cu_data *per_cu;
5819 while ((per_cu = iter.next ()) != NULL)
976ca316 5820 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5821 nullptr);
3b00ef10 5822 return true;
976ca316 5823 }, per_objfile);
3b00ef10
TT
5824
5825 /* It's a shame we couldn't do this inside the
5826 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5827 that have already been expanded. Instead, this loop matches what
5828 the psymtab code does. */
976ca316 5829 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5830 {
976ca316 5831 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5832 if (symtab != nullptr)
3b00ef10
TT
5833 {
5834 const struct block *block
af758d11 5835 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5836 if (!iterate_over_symbols_terminated (block, name,
5837 domain, callback))
5838 break;
5839 }
5840 }
5841}
5842
927aa2e7
JK
5843static void
5844dw2_debug_names_expand_symtabs_matching
5845 (struct objfile *objfile,
5846 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5847 const lookup_name_info *lookup_name,
927aa2e7
JK
5848 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5849 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5850 enum search_domain kind)
5851{
976ca316 5852 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5853
927aa2e7 5854 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5855 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5856 return;
9291a0cd 5857
976ca316 5858 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5859
c1a66c06
TV
5860 if (symbol_matcher == NULL && lookup_name == NULL)
5861 {
976ca316 5862 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5863 {
5864 QUIT;
5865
976ca316
SM
5866 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5867 expansion_notify);
c1a66c06
TV
5868 }
5869 return;
5870 }
5871
976ca316 5872 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5873
c1a66c06 5874 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5875 symbol_matcher,
5876 kind, [&] (offset_type namei)
927aa2e7 5877 {
927aa2e7
JK
5878 /* The name was matched, now expand corresponding CUs that were
5879 marked. */
976ca316 5880 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5881
927aa2e7
JK
5882 struct dwarf2_per_cu_data *per_cu;
5883 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5884 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5885 expansion_notify);
3b00ef10 5886 return true;
976ca316 5887 }, per_objfile);
9291a0cd
TT
5888}
5889
927aa2e7 5890const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5891{
5892 dw2_has_symbols,
5893 dw2_find_last_source_symtab,
5894 dw2_forget_cached_source_info,
f8eba3c6 5895 dw2_map_symtabs_matching_filename,
927aa2e7 5896 dw2_debug_names_lookup_symbol,
d3214198 5897 NULL,
9291a0cd 5898 dw2_print_stats,
927aa2e7 5899 dw2_debug_names_dump,
927aa2e7 5900 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5901 dw2_expand_all_symtabs,
652a8996 5902 dw2_expand_symtabs_with_fullname,
3b00ef10 5903 dw2_debug_names_map_matching_symbols,
927aa2e7 5904 dw2_debug_names_expand_symtabs_matching,
43f3e411 5905 dw2_find_pc_sect_compunit_symtab,
71a3c369 5906 NULL,
9291a0cd
TT
5907 dw2_map_symbol_filenames
5908};
5909
4485a1c1 5910/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5911 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5912
5913template <typename T>
5914static gdb::array_view<const gdb_byte>
5915get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5916{
5917 dwarf2_section_info *section = &section_owner->gdb_index;
5918
96b79293 5919 if (section->empty ())
4485a1c1
SM
5920 return {};
5921
5922 /* Older elfutils strip versions could keep the section in the main
5923 executable while splitting it for the separate debug info file. */
96b79293 5924 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5925 return {};
5926
96b79293 5927 section->read (obj);
4485a1c1 5928
8bebfcda
PA
5929 /* dwarf2_section_info::size is a bfd_size_type, while
5930 gdb::array_view works with size_t. On 32-bit hosts, with
5931 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5932 is 32-bit. So we need an explicit narrowing conversion here.
5933 This is fine, because it's impossible to allocate or mmap an
5934 array/buffer larger than what size_t can represent. */
5935 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5936}
5937
87d6a7aa
SM
5938/* Lookup the index cache for the contents of the index associated to
5939 DWARF2_OBJ. */
5940
5941static gdb::array_view<const gdb_byte>
5989a64e 5942get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5943{
5944 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5945 if (build_id == nullptr)
5946 return {};
5947
5948 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5949 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5950}
5951
5952/* Same as the above, but for DWZ. */
5953
5954static gdb::array_view<const gdb_byte>
5955get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5956{
5957 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5958 if (build_id == nullptr)
5959 return {};
5960
5961 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5962}
5963
3c0aa29a 5964/* See symfile.h. */
9291a0cd 5965
3c0aa29a
PA
5966bool
5967dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5968{
976ca316
SM
5969 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5970 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5971
9291a0cd
TT
5972 /* If we're about to read full symbols, don't bother with the
5973 indices. In this case we also don't care if some other debug
5974 format is making psymtabs, because they are all about to be
5975 expanded anyway. */
5976 if ((objfile->flags & OBJF_READNOW))
5977 {
17ee85fc
TT
5978 /* When using READNOW, the using_index flag (set below) indicates that
5979 PER_BFD was already initialized, when we loaded some other objfile. */
5980 if (per_bfd->using_index)
5981 {
5982 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 5983 per_objfile->resize_symtabs ();
17ee85fc
TT
5984 return true;
5985 }
5986
5987 per_bfd->using_index = 1;
976ca316
SM
5988 create_all_comp_units (per_objfile);
5989 create_all_type_units (per_objfile);
17ee85fc
TT
5990 per_bfd->quick_file_names_table
5991 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5992 per_objfile->resize_symtabs ();
9291a0cd 5993
17ee85fc
TT
5994 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5995 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 5996 {
17ee85fc 5997 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 5998
17ee85fc 5999 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6000 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6001 }
6002
6003 /* Return 1 so that gdb sees the "quick" functions. However,
6004 these functions will be no-ops because we will have expanded
6005 all symtabs. */
3c0aa29a
PA
6006 *index_kind = dw_index_kind::GDB_INDEX;
6007 return true;
9291a0cd
TT
6008 }
6009
17ee85fc
TT
6010 /* Was a debug names index already read when we processed an objfile sharing
6011 PER_BFD? */
6012 if (per_bfd->debug_names_table != nullptr)
6013 {
6014 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 6015 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6016 per_objfile->resize_symtabs ();
17ee85fc
TT
6017 return true;
6018 }
6019
6020 /* Was a GDB index already read when we processed an objfile sharing
6021 PER_BFD? */
6022 if (per_bfd->index_table != nullptr)
6023 {
6024 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 6025 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6026 per_objfile->resize_symtabs ();
17ee85fc
TT
6027 return true;
6028 }
6029
efb763a5
SM
6030 /* There might already be partial symtabs built for this BFD. This happens
6031 when loading the same binary twice with the index-cache enabled. If so,
6032 don't try to read an index. The objfile / per_objfile initialization will
6033 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6034 code path. */
6035 if (per_bfd->partial_symtabs != nullptr)
6036 return false;
6037
976ca316 6038 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
6039 {
6040 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6041 per_objfile->resize_symtabs ();
3c0aa29a
PA
6042 return true;
6043 }
927aa2e7 6044
976ca316 6045 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6046 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6047 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6048 {
6049 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6050 per_objfile->resize_symtabs ();
3c0aa29a
PA
6051 return true;
6052 }
9291a0cd 6053
87d6a7aa 6054 /* ... otherwise, try to find the index in the index cache. */
976ca316 6055 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6056 get_gdb_index_contents_from_cache,
6057 get_gdb_index_contents_from_cache_dwz))
6058 {
6059 global_index_cache.hit ();
6060 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6061 per_objfile->resize_symtabs ();
87d6a7aa
SM
6062 return true;
6063 }
6064
6065 global_index_cache.miss ();
3c0aa29a 6066 return false;
9291a0cd
TT
6067}
6068
6069\f
6070
dce234bc
PP
6071/* Build a partial symbol table. */
6072
6073void
f29dff0a 6074dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6075{
976ca316
SM
6076 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6077 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6078
6079 if (per_bfd->partial_symtabs != nullptr)
6080 {
6081 /* Partial symbols were already read, so now we can simply
6082 attach them. */
6083 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6084 per_objfile->resize_symtabs ();
17ee85fc
TT
6085 return;
6086 }
c9bf0622 6087
6eee24ce 6088 init_psymbol_list (objfile, 1024);
c906108c 6089
a70b8144 6090 try
c9bf0622
TT
6091 {
6092 /* This isn't really ideal: all the data we allocate on the
6093 objfile's obstack is still uselessly kept around. However,
6094 freeing it seems unsafe. */
906768f9 6095 psymtab_discarder psymtabs (objfile);
976ca316 6096 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6097 psymtabs.keep ();
87d6a7aa 6098
976ca316 6099 per_objfile->resize_symtabs ();
af758d11 6100
87d6a7aa 6101 /* (maybe) store an index in the cache. */
976ca316 6102 global_index_cache.store (per_objfile);
c9bf0622 6103 }
230d2906 6104 catch (const gdb_exception_error &except)
492d29ea
PA
6105 {
6106 exception_print (gdb_stderr, except);
6107 }
17ee85fc
TT
6108
6109 /* Finish by setting the local reference to partial symtabs, so that
6110 we don't try to read them again if reading another objfile with the same
6111 BFD. If we can't in fact share, this won't make a difference anyway as
6112 the dwarf2_per_bfd object won't be shared. */
6113 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6114}
c906108c 6115
3b80fe9b
DE
6116/* Find the base address of the compilation unit for range lists and
6117 location lists. It will normally be specified by DW_AT_low_pc.
6118 In DWARF-3 draft 4, the base address could be overridden by
6119 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6120 compilation units with discontinuous ranges. */
6121
6122static void
6123dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6124{
6125 struct attribute *attr;
6126
2b24b6e4 6127 cu->base_address.reset ();
3b80fe9b
DE
6128
6129 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6130 if (attr != nullptr)
2b24b6e4 6131 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6132 else
6133 {
6134 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6135 if (attr != nullptr)
2b24b6e4 6136 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6137 }
6138}
6139
36586728
TT
6140/* Helper function that returns the proper abbrev section for
6141 THIS_CU. */
6142
6143static struct dwarf2_section_info *
6144get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6145{
6146 struct dwarf2_section_info *abbrev;
c3699833 6147 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6148
6149 if (this_cu->is_dwz)
c3699833 6150 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6151 else
c3699833 6152 abbrev = &per_bfd->abbrev;
36586728
TT
6153
6154 return abbrev;
6155}
6156
f4dc4d17
DE
6157/* Fetch the abbreviation table offset from a comp or type unit header. */
6158
6159static sect_offset
976ca316 6160read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6161 struct dwarf2_section_info *section,
9c541725 6162 sect_offset sect_off)
f4dc4d17 6163{
96b79293 6164 bfd *abfd = section->get_bfd_owner ();
d521ce57 6165 const gdb_byte *info_ptr;
ac298888 6166 unsigned int initial_length_size, offset_size;
43988095 6167 uint16_t version;
f4dc4d17 6168
976ca316 6169 section->read (per_objfile->objfile);
9c541725 6170 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6171 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6172 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6173 info_ptr += initial_length_size;
6174
6175 version = read_2_bytes (abfd, info_ptr);
6176 info_ptr += 2;
6177 if (version >= 5)
6178 {
6179 /* Skip unit type and address size. */
6180 info_ptr += 2;
6181 }
6182
24aa364d 6183 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6184}
6185
b83470bf
TT
6186/* A partial symtab that is used only for include files. */
6187struct dwarf2_include_psymtab : public partial_symtab
6188{
6189 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6190 : partial_symtab (filename, objfile)
6191 {
6192 }
6193
6194 void read_symtab (struct objfile *objfile) override
6195 {
194d088f
TV
6196 /* It's an include file, no symbols to read for it.
6197 Everything is in the includer symtab. */
6198
6199 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6200 expansion of the includer psymtab. We use the dependencies[0] field to
6201 model the includer. But if we go the regular route of calling
6202 expand_psymtab here, and having expand_psymtab call expand_dependencies
6203 to expand the includer, we'll only use expand_psymtab on the includer
6204 (making it a non-toplevel psymtab), while if we expand the includer via
6205 another path, we'll use read_symtab (making it a toplevel psymtab).
6206 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6207 psymtab, and trigger read_symtab on the includer here directly. */
6208 includer ()->read_symtab (objfile);
b83470bf
TT
6209 }
6210
6211 void expand_psymtab (struct objfile *objfile) override
6212 {
194d088f
TV
6213 /* This is not called by read_symtab, and should not be called by any
6214 expand_dependencies. */
6215 gdb_assert (false);
b83470bf
TT
6216 }
6217
5717c425 6218 bool readin_p (struct objfile *objfile) const override
b83470bf 6219 {
5717c425 6220 return includer ()->readin_p (objfile);
b83470bf
TT
6221 }
6222
5717c425 6223 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6224 {
6225 return nullptr;
6226 }
6227
6228private:
194d088f
TV
6229 partial_symtab *includer () const
6230 {
6231 /* An include psymtab has exactly one dependency: the psymtab that
6232 includes it. */
6233 gdb_assert (this->number_of_dependencies == 1);
6234 return this->dependencies[0];
6235 }
b83470bf
TT
6236};
6237
aaa75496
JB
6238/* Allocate a new partial symtab for file named NAME and mark this new
6239 partial symtab as being an include of PST. */
6240
6241static void
891813be 6242dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6243 struct objfile *objfile)
6244{
b83470bf 6245 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6246
fbd9ab74 6247 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6248 subpst->dirname = pst->dirname;
fbd9ab74 6249
a9342b62 6250 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6251 subpst->dependencies[0] = pst;
6252 subpst->number_of_dependencies = 1;
aaa75496
JB
6253}
6254
6255/* Read the Line Number Program data and extract the list of files
6256 included by the source file represented by PST. Build an include
d85a05f0 6257 partial symtab for each of these included files. */
aaa75496
JB
6258
6259static void
6260dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6261 struct die_info *die,
891813be 6262 dwarf2_psymtab *pst)
aaa75496 6263{
fff8551c 6264 line_header_up lh;
d85a05f0 6265 struct attribute *attr;
aaa75496 6266
d85a05f0 6267 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6268 if (attr != nullptr)
9c541725 6269 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6270 if (lh == NULL)
6271 return; /* No linetable, so no includes. */
6272
79748972
TT
6273 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6274 that we pass in the raw text_low here; that is ok because we're
6275 only decoding the line table to make include partial symtabs, and
6276 so the addresses aren't really used. */
4ae976d1 6277 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6278 pst->raw_text_low (), 1);
aaa75496
JB
6279}
6280
348e048f 6281static hashval_t
52dc124a 6282hash_signatured_type (const void *item)
348e048f 6283{
9a3c8263
SM
6284 const struct signatured_type *sig_type
6285 = (const struct signatured_type *) item;
9a619af0 6286
348e048f 6287 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6288 return sig_type->signature;
348e048f
DE
6289}
6290
6291static int
52dc124a 6292eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6293{
9a3c8263
SM
6294 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6295 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6296
348e048f
DE
6297 return lhs->signature == rhs->signature;
6298}
6299
1fd400ff
TT
6300/* Allocate a hash table for signatured types. */
6301
b0b6a987 6302static htab_up
298e9637 6303allocate_signatured_type_table ()
1fd400ff 6304{
b0b6a987
TT
6305 return htab_up (htab_create_alloc (41,
6306 hash_signatured_type,
6307 eq_signatured_type,
6308 NULL, xcalloc, xfree));
1fd400ff
TT
6309}
6310
d467dd73 6311/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6312
6313static int
d467dd73 6314add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6315{
9a3c8263 6316 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6317 std::vector<signatured_type *> *all_type_units
6318 = (std::vector<signatured_type *> *) datum;
1fd400ff 6319
b2bdb8cf 6320 all_type_units->push_back (sigt);
1fd400ff
TT
6321
6322 return 1;
6323}
6324
78d4d2c5 6325/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6326 and fill them into TYPES_HTAB. It will process only type units,
6327 therefore DW_UT_type. */
c88ee1f0 6328
78d4d2c5 6329static void
976ca316 6330create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6331 struct dwo_file *dwo_file,
b0b6a987 6332 dwarf2_section_info *section, htab_up &types_htab,
43988095 6333 rcuh_kind section_kind)
348e048f 6334{
976ca316 6335 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6336 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6337 bfd *abfd;
6338 const gdb_byte *info_ptr, *end_ptr;
348e048f 6339
4bdcc0c1
DE
6340 abbrev_section = (dwo_file != NULL
6341 ? &dwo_file->sections.abbrev
976ca316 6342 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6343
b4f54984 6344 if (dwarf_read_debug)
43988095 6345 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6346 section->get_name (),
6347 abbrev_section->get_file_name ());
09406207 6348
96b79293 6349 section->read (objfile);
78d4d2c5 6350 info_ptr = section->buffer;
348e048f 6351
78d4d2c5
JK
6352 if (info_ptr == NULL)
6353 return;
348e048f 6354
78d4d2c5
JK
6355 /* We can't set abfd until now because the section may be empty or
6356 not present, in which case the bfd is unknown. */
96b79293 6357 abfd = section->get_bfd_owner ();
348e048f 6358
c0ab21c2
TT
6359 /* We don't use cutu_reader here because we don't need to read
6360 any dies: the signature is in the header. */
3019eac3 6361
78d4d2c5
JK
6362 end_ptr = info_ptr + section->size;
6363 while (info_ptr < end_ptr)
6364 {
78d4d2c5
JK
6365 struct signatured_type *sig_type;
6366 struct dwo_unit *dwo_tu;
6367 void **slot;
6368 const gdb_byte *ptr = info_ptr;
6369 struct comp_unit_head header;
6370 unsigned int length;
8b70b953 6371
9c541725 6372 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6373
a49dd8dd
JK
6374 /* Initialize it due to a false compiler warning. */
6375 header.signature = -1;
9c541725 6376 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6377
78d4d2c5
JK
6378 /* We need to read the type's signature in order to build the hash
6379 table, but we don't need anything else just yet. */
348e048f 6380
976ca316 6381 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6382 abbrev_section, ptr, section_kind);
348e048f 6383
4057dfde 6384 length = header.get_length ();
6caca83c 6385
78d4d2c5
JK
6386 /* Skip dummy type units. */
6387 if (ptr >= info_ptr + length
43988095
JK
6388 || peek_abbrev_code (abfd, ptr) == 0
6389 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6390 {
6391 info_ptr += length;
6392 continue;
6393 }
dee91e82 6394
78d4d2c5
JK
6395 if (types_htab == NULL)
6396 {
6397 if (dwo_file)
298e9637 6398 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6399 else
298e9637 6400 types_htab = allocate_signatured_type_table ();
78d4d2c5 6401 }
8b70b953 6402
78d4d2c5
JK
6403 if (dwo_file)
6404 {
6405 sig_type = NULL;
976ca316 6406 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6407 dwo_tu->dwo_file = dwo_file;
43988095 6408 dwo_tu->signature = header.signature;
9c541725 6409 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6410 dwo_tu->section = section;
9c541725 6411 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6412 dwo_tu->length = length;
6413 }
6414 else
6415 {
6416 /* N.B.: type_offset is not usable if this type uses a DWO file.
6417 The real type_offset is in the DWO file. */
6418 dwo_tu = NULL;
976ca316 6419 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6420 sig_type->signature = header.signature;
9c541725 6421 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6422 sig_type->per_cu.is_debug_types = 1;
6423 sig_type->per_cu.section = section;
9c541725 6424 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6425 sig_type->per_cu.length = length;
6426 }
6427
b0b6a987 6428 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6429 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6430 INSERT);
6431 gdb_assert (slot != NULL);
6432 if (*slot != NULL)
6433 {
9c541725 6434 sect_offset dup_sect_off;
0349ea22 6435
3019eac3
DE
6436 if (dwo_file)
6437 {
78d4d2c5
JK
6438 const struct dwo_unit *dup_tu
6439 = (const struct dwo_unit *) *slot;
6440
9c541725 6441 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6442 }
6443 else
6444 {
78d4d2c5
JK
6445 const struct signatured_type *dup_tu
6446 = (const struct signatured_type *) *slot;
6447
9c541725 6448 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6449 }
8b70b953 6450
b98664d3 6451 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6452 " the entry at offset %s, signature %s"),
6453 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6454 hex_string (header.signature));
78d4d2c5
JK
6455 }
6456 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6457
78d4d2c5 6458 if (dwarf_read_debug > 1)
9d8780f0
SM
6459 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6460 sect_offset_str (sect_off),
43988095 6461 hex_string (header.signature));
3019eac3 6462
78d4d2c5
JK
6463 info_ptr += length;
6464 }
6465}
3019eac3 6466
78d4d2c5
JK
6467/* Create the hash table of all entries in the .debug_types
6468 (or .debug_types.dwo) section(s).
6469 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6470 otherwise it is NULL.
b3c8eb43 6471
78d4d2c5 6472 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6473
78d4d2c5 6474 Note: This function processes DWO files only, not DWP files. */
348e048f 6475
78d4d2c5 6476static void
976ca316 6477create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6478 struct dwo_file *dwo_file,
fd5866f6 6479 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6480 htab_up &types_htab)
78d4d2c5 6481{
fd5866f6 6482 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6483 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6484 rcuh_kind::TYPE);
3019eac3
DE
6485}
6486
6487/* Create the hash table of all entries in the .debug_types section,
6488 and initialize all_type_units.
6489 The result is zero if there is an error (e.g. missing .debug_types section),
6490 otherwise non-zero. */
6491
6492static int
976ca316 6493create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6494{
b0b6a987 6495 htab_up types_htab;
3019eac3 6496
976ca316
SM
6497 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6498 types_htab, rcuh_kind::COMPILE);
6499 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6500 types_htab);
3019eac3
DE
6501 if (types_htab == NULL)
6502 {
976ca316 6503 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6504 return 0;
6505 }
6506
976ca316 6507 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6508
976ca316
SM
6509 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6510 per_objfile->per_bfd->all_type_units.reserve
6511 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6512
976ca316 6513 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6514 add_signatured_type_cu_to_table,
976ca316 6515 &per_objfile->per_bfd->all_type_units);
1fd400ff 6516
348e048f
DE
6517 return 1;
6518}
6519
5989a64e 6520/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6521 If SLOT is non-NULL, it is the entry to use in the hash table.
6522 Otherwise we find one. */
6523
6524static struct signatured_type *
976ca316 6525add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6526{
976ca316
SM
6527 if (per_objfile->per_bfd->all_type_units.size ()
6528 == per_objfile->per_bfd->all_type_units.capacity ())
6529 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6530
976ca316 6531 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6532
976ca316 6533 per_objfile->resize_symtabs ();
af758d11 6534
976ca316 6535 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6536 sig_type->signature = sig;
6537 sig_type->per_cu.is_debug_types = 1;
976ca316 6538 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6539 {
6540 sig_type->per_cu.v.quick =
976ca316 6541 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6542 struct dwarf2_per_cu_quick_data);
6543 }
6544
6545 if (slot == NULL)
6546 {
976ca316 6547 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6548 sig_type, INSERT);
6549 }
6550 gdb_assert (*slot == NULL);
6551 *slot = sig_type;
6552 /* The rest of sig_type must be filled in by the caller. */
6553 return sig_type;
6554}
6555
a2ce51a0
DE
6556/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6557 Fill in SIG_ENTRY with DWO_ENTRY. */
6558
6559static void
976ca316 6560fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6561 struct signatured_type *sig_entry,
6562 struct dwo_unit *dwo_entry)
6563{
976ca316 6564 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6565
7ee85ab1 6566 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6567 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6568 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6569 if (per_bfd->using_index)
6aa5f3a6
DE
6570 {
6571 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6572 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6573 }
6574 else
6575 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6576 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6577 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6578 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6579 gdb_assert (sig_entry->dwo_unit == NULL);
6580
6581 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6582 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6583 sig_entry->per_cu.length = dwo_entry->length;
6584 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6585 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6586 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6587 sig_entry->dwo_unit = dwo_entry;
6588}
6589
6590/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6591 If we haven't read the TU yet, create the signatured_type data structure
6592 for a TU to be read in directly from a DWO file, bypassing the stub.
6593 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6594 using .gdb_index, then when reading a CU we want to stay in the DWO file
6595 containing that CU. Otherwise we could end up reading several other DWO
6596 files (due to comdat folding) to process the transitive closure of all the
6597 mentioned TUs, and that can be slow. The current DWO file will have every
6598 type signature that it needs.
a2ce51a0
DE
6599 We only do this for .gdb_index because in the psymtab case we already have
6600 to read all the DWOs to build the type unit groups. */
6601
6602static struct signatured_type *
6603lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6604{
976ca316 6605 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6606 struct dwo_file *dwo_file;
6607 struct dwo_unit find_dwo_entry, *dwo_entry;
6608 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6609 void **slot;
a2ce51a0 6610
976ca316 6611 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6612
6aa5f3a6
DE
6613 /* If TU skeletons have been removed then we may not have read in any
6614 TUs yet. */
976ca316
SM
6615 if (per_objfile->per_bfd->signatured_types == NULL)
6616 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6617
6618 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6619 Use the global signatured_types array to do our own comdat-folding
6620 of types. If this is the first time we're reading this TU, and
6621 the TU has an entry in .gdb_index, replace the recorded data from
6622 .gdb_index with this TU. */
a2ce51a0 6623
a2ce51a0 6624 find_sig_entry.signature = sig;
976ca316 6625 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6626 &find_sig_entry, INSERT);
9a3c8263 6627 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6628
6629 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6630 read. Don't reassign the global entry to point to this DWO if that's
6631 the case. Also note that if the TU is already being read, it may not
6632 have come from a DWO, the program may be a mix of Fission-compiled
6633 code and non-Fission-compiled code. */
6634
6635 /* Have we already tried to read this TU?
6636 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6637 needn't exist in the global table yet). */
6638 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6639 return sig_entry;
6640
6aa5f3a6
DE
6641 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6642 dwo_unit of the TU itself. */
6643 dwo_file = cu->dwo_unit->dwo_file;
6644
a2ce51a0
DE
6645 /* Ok, this is the first time we're reading this TU. */
6646 if (dwo_file->tus == NULL)
6647 return NULL;
6648 find_dwo_entry.signature = sig;
b0b6a987
TT
6649 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6650 &find_dwo_entry);
a2ce51a0
DE
6651 if (dwo_entry == NULL)
6652 return NULL;
6653
6aa5f3a6
DE
6654 /* If the global table doesn't have an entry for this TU, add one. */
6655 if (sig_entry == NULL)
976ca316 6656 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6657
976ca316 6658 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6659 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6660 return sig_entry;
6661}
6662
a2ce51a0
DE
6663/* Subroutine of lookup_signatured_type.
6664 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6665 then try the DWP file. If the TU stub (skeleton) has been removed then
6666 it won't be in .gdb_index. */
a2ce51a0
DE
6667
6668static struct signatured_type *
6669lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6670{
976ca316
SM
6671 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6672 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6673 struct dwo_unit *dwo_entry;
6674 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6675 void **slot;
a2ce51a0 6676
976ca316 6677 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6678 gdb_assert (dwp_file != NULL);
6679
6aa5f3a6
DE
6680 /* If TU skeletons have been removed then we may not have read in any
6681 TUs yet. */
976ca316
SM
6682 if (per_objfile->per_bfd->signatured_types == NULL)
6683 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6684
6aa5f3a6 6685 find_sig_entry.signature = sig;
976ca316 6686 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6687 &find_sig_entry, INSERT);
9a3c8263 6688 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6689
6690 /* Have we already tried to read this TU?
6691 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6692 needn't exist in the global table yet). */
6693 if (sig_entry != NULL)
6694 return sig_entry;
6695
a2ce51a0
DE
6696 if (dwp_file->tus == NULL)
6697 return NULL;
976ca316
SM
6698 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6699 1 /* is_debug_types */);
a2ce51a0
DE
6700 if (dwo_entry == NULL)
6701 return NULL;
6702
976ca316
SM
6703 sig_entry = add_type_unit (per_objfile, sig, slot);
6704 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6705
a2ce51a0
DE
6706 return sig_entry;
6707}
6708
380bca97 6709/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6710 Returns NULL if signature SIG is not present in the table.
6711 It is up to the caller to complain about this. */
348e048f
DE
6712
6713static struct signatured_type *
a2ce51a0 6714lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6715{
976ca316 6716 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6717
976ca316 6718 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6719 {
6720 /* We're in a DWO/DWP file, and we're using .gdb_index.
6721 These cases require special processing. */
976ca316 6722 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6723 return lookup_dwo_signatured_type (cu, sig);
6724 else
6725 return lookup_dwp_signatured_type (cu, sig);
6726 }
6727 else
6728 {
6729 struct signatured_type find_entry, *entry;
348e048f 6730
976ca316 6731 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6732 return NULL;
6733 find_entry.signature = sig;
9a3c8263 6734 entry = ((struct signatured_type *)
976ca316 6735 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6736 &find_entry));
a2ce51a0
DE
6737 return entry;
6738 }
348e048f 6739}
18a8505e 6740
42e7ad6c 6741/* Low level DIE reading support. */
348e048f 6742
d85a05f0
DJ
6743/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6744
6745static void
6746init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6747 struct dwarf2_cu *cu,
3019eac3 6748 struct dwarf2_section_info *section,
685af9cd
TT
6749 struct dwo_file *dwo_file,
6750 struct abbrev_table *abbrev_table)
d85a05f0 6751{
fceca515 6752 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6753 reader->abfd = section->get_bfd_owner ();
d85a05f0 6754 reader->cu = cu;
3019eac3 6755 reader->dwo_file = dwo_file;
dee91e82
DE
6756 reader->die_section = section;
6757 reader->buffer = section->buffer;
f664829e 6758 reader->buffer_end = section->buffer + section->size;
685af9cd 6759 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6760}
6761
c0ab21c2 6762/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6763 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6764 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6765 already.
6766
6767 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6768 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6769 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6770 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6771 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6772 STUB_COMP_DIR may be non-NULL.
3e225074 6773 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6774 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6775 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6776 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6777 kept around for at least as long as *RESULT_READER.
6778
b0c7bfa9
DE
6779 The result is non-zero if a valid (non-dummy) DIE was found. */
6780
6781static int
4ab09049 6782read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6783 struct dwo_unit *dwo_unit,
b0c7bfa9 6784 struct die_info *stub_comp_unit_die,
a2ce51a0 6785 const char *stub_comp_dir,
b0c7bfa9 6786 struct die_reader_specs *result_reader,
d521ce57 6787 const gdb_byte **result_info_ptr,
b0c7bfa9 6788 struct die_info **result_comp_unit_die,
685af9cd 6789 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6790{
976ca316 6791 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6792 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6793 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6794 bfd *abfd;
d521ce57 6795 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6796 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6797 int i,num_extra_attrs;
6798 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6799 struct die_info *comp_unit_die;
6800
b0aeadb3
DE
6801 /* At most one of these may be provided. */
6802 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6803
b0c7bfa9
DE
6804 /* These attributes aren't processed until later:
6805 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6806 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6807 referenced later. However, these attributes are found in the stub
6808 which we won't have later. In order to not impose this complication
6809 on the rest of the code, we read them here and copy them to the
6810 DWO CU/TU die. */
b0c7bfa9
DE
6811
6812 stmt_list = NULL;
6813 low_pc = NULL;
6814 high_pc = NULL;
6815 ranges = NULL;
6816 comp_dir = NULL;
6817
6818 if (stub_comp_unit_die != NULL)
6819 {
6820 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6821 DWO file. */
4ab09049 6822 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6823 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6824 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6825 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6826 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6827 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6828
a39fdb41 6829 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6830
18a8505e
AT
6831 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6832 here (if needed). We need the value before we can process
6833 DW_AT_ranges. */
a39fdb41 6834 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6835 }
a2ce51a0
DE
6836 else if (stub_comp_dir != NULL)
6837 {
6838 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6839 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6840 comp_dir->name = DW_AT_comp_dir;
6841 comp_dir->form = DW_FORM_string;
6842 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6843 DW_STRING (comp_dir) = stub_comp_dir;
6844 }
b0c7bfa9
DE
6845
6846 /* Set up for reading the DWO CU/TU. */
6847 cu->dwo_unit = dwo_unit;
685af9cd 6848 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6849 section->read (objfile);
6850 abfd = section->get_bfd_owner ();
9c541725
PA
6851 begin_info_ptr = info_ptr = (section->buffer
6852 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6853 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6854
4ab09049 6855 if (per_cu->is_debug_types)
b0c7bfa9 6856 {
4ab09049 6857 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6858
976ca316
SM
6859 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6860 section, dwo_abbrev_section,
43988095 6861 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6862 /* This is not an assert because it can be caused by bad debug info. */
43988095 6863 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6864 {
6865 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6866 " TU at offset %s [in module %s]"),
a2ce51a0 6867 hex_string (sig_type->signature),
43988095 6868 hex_string (cu->header.signature),
9d8780f0 6869 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6870 bfd_get_filename (abfd));
6871 }
9c541725 6872 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6873 /* For DWOs coming from DWP files, we don't know the CU length
6874 nor the type's offset in the TU until now. */
4057dfde 6875 dwo_unit->length = cu->header.get_length ();
9c541725 6876 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6877
6878 /* Establish the type offset that can be used to lookup the type.
6879 For DWO files, we don't know it until now. */
9c541725
PA
6880 sig_type->type_offset_in_section
6881 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6882 }
6883 else
6884 {
976ca316
SM
6885 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6886 section, dwo_abbrev_section,
43988095 6887 info_ptr, rcuh_kind::COMPILE);
9c541725 6888 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6889 /* For DWOs coming from DWP files, we don't know the CU length
6890 until now. */
4057dfde 6891 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6892 }
6893
685af9cd 6894 *result_dwo_abbrev_table
86de1d91
TT
6895 = abbrev_table::read (objfile, dwo_abbrev_section,
6896 cu->header.abbrev_sect_off);
685af9cd
TT
6897 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6898 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6899
6900 /* Read in the die, but leave space to copy over the attributes
6901 from the stub. This has the benefit of simplifying the rest of
6902 the code - all the work to maintain the illusion of a single
6903 DW_TAG_{compile,type}_unit DIE is done here. */
6904 num_extra_attrs = ((stmt_list != NULL)
6905 + (low_pc != NULL)
6906 + (high_pc != NULL)
6907 + (ranges != NULL)
6908 + (comp_dir != NULL));
6909 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6910 num_extra_attrs);
b0c7bfa9
DE
6911
6912 /* Copy over the attributes from the stub to the DIE we just read in. */
6913 comp_unit_die = *result_comp_unit_die;
6914 i = comp_unit_die->num_attrs;
6915 if (stmt_list != NULL)
6916 comp_unit_die->attrs[i++] = *stmt_list;
6917 if (low_pc != NULL)
6918 comp_unit_die->attrs[i++] = *low_pc;
6919 if (high_pc != NULL)
6920 comp_unit_die->attrs[i++] = *high_pc;
6921 if (ranges != NULL)
6922 comp_unit_die->attrs[i++] = *ranges;
6923 if (comp_dir != NULL)
6924 comp_unit_die->attrs[i++] = *comp_dir;
6925 comp_unit_die->num_attrs += num_extra_attrs;
6926
b4f54984 6927 if (dwarf_die_debug)
bf6af496
DE
6928 {
6929 fprintf_unfiltered (gdb_stdlog,
6930 "Read die from %s@0x%x of %s:\n",
96b79293 6931 section->get_name (),
bf6af496
DE
6932 (unsigned) (begin_info_ptr - section->buffer),
6933 bfd_get_filename (abfd));
b4f54984 6934 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6935 }
6936
b0c7bfa9
DE
6937 /* Skip dummy compilation units. */
6938 if (info_ptr >= begin_info_ptr + dwo_unit->length
6939 || peek_abbrev_code (abfd, info_ptr) == 0)
6940 return 0;
6941
6942 *result_info_ptr = info_ptr;
6943 return 1;
6944}
6945
a084a2a6
AT
6946/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6947 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6948 signature is part of the header. */
6949static gdb::optional<ULONGEST>
6950lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6951{
6952 if (cu->header.version >= 5)
6953 return cu->header.signature;
6954 struct attribute *attr;
6955 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6956 if (attr == nullptr)
6957 return gdb::optional<ULONGEST> ();
6958 return DW_UNSND (attr);
6959}
6960
c0ab21c2 6961/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6962 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6963 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6964
6965static struct dwo_unit *
4ab09049 6966lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6967{
4ab09049 6968 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6969 struct dwo_unit *dwo_unit;
c0ab21c2 6970 const char *comp_dir;
b0c7bfa9 6971
a2ce51a0
DE
6972 gdb_assert (cu != NULL);
6973
b0c7bfa9 6974 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6975 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6976 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6977
4ab09049
SM
6978 if (per_cu->is_debug_types)
6979 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6980 else
6981 {
a084a2a6 6982 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6983
a084a2a6 6984 if (!signature.has_value ())
b0c7bfa9
DE
6985 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6986 " [in module %s]"),
4ab09049
SM
6987 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6988
6989 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6990 }
6991
b0c7bfa9
DE
6992 return dwo_unit;
6993}
6994
c0ab21c2 6995/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6996 See it for a description of the parameters.
fcd3b13d 6997 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6998
c0ab21c2 6999void
9e021579
SM
7000cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7001 dwarf2_per_objfile *per_objfile,
2e671100 7002 dwarf2_cu *existing_cu)
a2ce51a0 7003{
a2ce51a0 7004 struct signatured_type *sig_type;
a2ce51a0
DE
7005
7006 /* Verify we can do the following downcast, and that we have the
7007 data we need. */
7008 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7009 sig_type = (struct signatured_type *) this_cu;
7010 gdb_assert (sig_type->dwo_unit != NULL);
7011
2e671100
SM
7012 dwarf2_cu *cu;
7013
7014 if (existing_cu != nullptr)
6aa5f3a6 7015 {
2e671100
SM
7016 cu = existing_cu;
7017 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7018 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7019 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7020 }
7021 else
7022 {
7188ed02
SM
7023 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7024 in per_objfile yet. */
7025 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7026 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7027 cu = m_new_cu.get ();
6aa5f3a6
DE
7028 }
7029
7030 /* A future optimization, if needed, would be to use an existing
7031 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7032 could share abbrev tables. */
a2ce51a0 7033
2e671100 7034 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7035 NULL /* stub_comp_unit_die */,
7036 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7037 this, &info_ptr,
3e225074 7038 &comp_unit_die,
c0ab21c2 7039 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7040 {
7041 /* Dummy die. */
c0ab21c2 7042 dummy_p = true;
a2ce51a0 7043 }
a2ce51a0
DE
7044}
7045
fd820528 7046/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7047 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7048
f4dc4d17
DE
7049 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7050 Otherwise the table specified in the comp unit header is read in and used.
7051 This is an optimization for when we already have the abbrev table.
7052
2e671100
SM
7053 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7054 allocated. */
aaa75496 7055
ab432490 7056cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7057 dwarf2_per_objfile *per_objfile,
c0ab21c2 7058 struct abbrev_table *abbrev_table,
2e671100 7059 dwarf2_cu *existing_cu,
c0ab21c2
TT
7060 bool skip_partial)
7061 : die_reader_specs {},
6751ebae 7062 m_this_cu (this_cu)
c906108c 7063{
976ca316 7064 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7065 struct dwarf2_section_info *section = this_cu->section;
96b79293 7066 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7067 const gdb_byte *begin_info_ptr;
dee91e82 7068 struct signatured_type *sig_type = NULL;
4bdcc0c1 7069 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7070 /* Non-zero if CU currently points to a DWO file and we need to
7071 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7072 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7073 int rereading_dwo_cu = 0;
c906108c 7074
b4f54984 7075 if (dwarf_die_debug)
9d8780f0 7076 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7077 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7078 sect_offset_str (this_cu->sect_off));
09406207 7079
a2ce51a0
DE
7080 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7081 file (instead of going through the stub), short-circuit all of this. */
7082 if (this_cu->reading_dwo_directly)
7083 {
7084 /* Narrow down the scope of possibilities to have to understand. */
7085 gdb_assert (this_cu->is_debug_types);
7086 gdb_assert (abbrev_table == NULL);
976ca316 7087 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7088 return;
7089 }
7090
dee91e82 7091 /* This is cheap if the section is already read in. */
96b79293 7092 section->read (objfile);
dee91e82 7093
9c541725 7094 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7095
7096 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7097
2e671100
SM
7098 dwarf2_cu *cu;
7099
7100 if (existing_cu != nullptr)
dee91e82 7101 {
2e671100 7102 cu = existing_cu;
42e7ad6c
DE
7103 /* If this CU is from a DWO file we need to start over, we need to
7104 refetch the attributes from the skeleton CU.
7105 This could be optimized by retrieving those attributes from when we
7106 were here the first time: the previous comp_unit_die was stored in
7107 comp_unit_obstack. But there's no data yet that we need this
7108 optimization. */
7109 if (cu->dwo_unit != NULL)
7110 rereading_dwo_cu = 1;
dee91e82
DE
7111 }
7112 else
7113 {
7188ed02
SM
7114 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7115 in per_objfile yet. */
976ca316
SM
7116 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7117 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7118 cu = m_new_cu.get ();
42e7ad6c 7119 }
dee91e82 7120
b0c7bfa9 7121 /* Get the header. */
9c541725 7122 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7123 {
7124 /* We already have the header, there's no need to read it in again. */
9c541725 7125 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7126 }
7127 else
7128 {
3019eac3 7129 if (this_cu->is_debug_types)
dee91e82 7130 {
976ca316
SM
7131 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7132 section, abbrev_section,
7133 info_ptr, rcuh_kind::TYPE);
dee91e82 7134
42e7ad6c
DE
7135 /* Since per_cu is the first member of struct signatured_type,
7136 we can go from a pointer to one to a pointer to the other. */
7137 sig_type = (struct signatured_type *) this_cu;
43988095 7138 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7139 gdb_assert (sig_type->type_offset_in_tu
7140 == cu->header.type_cu_offset_in_tu);
7141 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7142
42e7ad6c
DE
7143 /* LENGTH has not been set yet for type units if we're
7144 using .gdb_index. */
4057dfde 7145 this_cu->length = cu->header.get_length ();
3019eac3
DE
7146
7147 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7148 sig_type->type_offset_in_section =
7149 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7150
7151 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7152 }
7153 else
7154 {
976ca316
SM
7155 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7156 section, abbrev_section,
43988095
JK
7157 info_ptr,
7158 rcuh_kind::COMPILE);
dee91e82 7159
9c541725 7160 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7161 if (this_cu->length == 0)
7162 this_cu->length = cu->header.get_length ();
7163 else
7164 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7165 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7166 }
7167 }
10b3939b 7168
6caca83c 7169 /* Skip dummy compilation units. */
dee91e82 7170 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7171 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7172 {
7173 dummy_p = true;
7174 return;
7175 }
6caca83c 7176
433df2d4
DE
7177 /* If we don't have them yet, read the abbrevs for this compilation unit.
7178 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7179 done. */
f4dc4d17 7180 if (abbrev_table != NULL)
685af9cd
TT
7181 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7182 else
f4dc4d17 7183 {
c0ab21c2 7184 m_abbrev_table_holder
86de1d91
TT
7185 = abbrev_table::read (objfile, abbrev_section,
7186 cu->header.abbrev_sect_off);
c0ab21c2 7187 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7188 }
af703f96 7189
dee91e82 7190 /* Read the top level CU/TU die. */
c0ab21c2 7191 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7192 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7193
58f0c718 7194 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7195 {
7196 dummy_p = true;
7197 return;
7198 }
58f0c718 7199
b0c7bfa9 7200 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7201 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7202 table from the DWO file and pass the ownership over to us. It will be
7203 referenced from READER, so we must make sure to free it after we're done
7204 with READER.
7205
b0c7bfa9
DE
7206 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7207 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7208 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7209 if (dwo_name != nullptr)
3019eac3 7210 {
3019eac3 7211 struct dwo_unit *dwo_unit;
b0c7bfa9 7212 struct die_info *dwo_comp_unit_die;
3019eac3 7213
3e225074 7214 if (comp_unit_die->has_children)
6a506a2d 7215 {
b98664d3 7216 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7217 " has children (offset %s) [in module %s]"),
7218 sect_offset_str (this_cu->sect_off),
7219 bfd_get_filename (abfd));
6a506a2d 7220 }
4ab09049 7221 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7222 if (dwo_unit != NULL)
3019eac3 7223 {
4ab09049 7224 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7225 comp_unit_die, NULL,
c0ab21c2 7226 this, &info_ptr,
3e225074 7227 &dwo_comp_unit_die,
c0ab21c2 7228 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7229 {
7230 /* Dummy die. */
c0ab21c2 7231 dummy_p = true;
6a506a2d
DE
7232 return;
7233 }
7234 comp_unit_die = dwo_comp_unit_die;
7235 }
7236 else
7237 {
7238 /* Yikes, we couldn't find the rest of the DIE, we only have
7239 the stub. A complaint has already been logged. There's
7240 not much more we can do except pass on the stub DIE to
7241 die_reader_func. We don't want to throw an error on bad
7242 debug info. */
3019eac3
DE
7243 }
7244 }
c0ab21c2 7245}
3019eac3 7246
6751ebae
TT
7247void
7248cutu_reader::keep ()
c0ab21c2 7249{
b0c7bfa9 7250 /* Done, clean up. */
6751ebae
TT
7251 gdb_assert (!dummy_p);
7252 if (m_new_cu != NULL)
348e048f 7253 {
7188ed02
SM
7254 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7255 now. */
7256 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7257 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7258 }
dee91e82
DE
7259}
7260
18a8505e
AT
7261/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7262 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7263 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7264
7265 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7266 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7267
7268 We fill in THIS_CU->length.
7269
dee91e82 7270 THIS_CU->cu is always freed when done.
3019eac3 7271 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7272 to care whether it refers to the "main" CU or the DWO CU.
7273
7274 When parent_cu is passed, it is used to provide a default value for
7275 str_offsets_base and addr_base from the parent. */
dee91e82 7276
ab432490 7277cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7278 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7279 struct dwarf2_cu *parent_cu,
7280 struct dwo_file *dwo_file)
7281 : die_reader_specs {},
7282 m_this_cu (this_cu)
dee91e82 7283{
976ca316 7284 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7285 struct dwarf2_section_info *section = this_cu->section;
96b79293 7286 bfd *abfd = section->get_bfd_owner ();
33e80786 7287 struct dwarf2_section_info *abbrev_section;
d521ce57 7288 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7289
b4f54984 7290 if (dwarf_die_debug)
9d8780f0 7291 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7292 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7293 sect_offset_str (this_cu->sect_off));
09406207 7294
976ca316 7295 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7296
33e80786
DE
7297 abbrev_section = (dwo_file != NULL
7298 ? &dwo_file->sections.abbrev
7299 : get_abbrev_section_for_cu (this_cu));
7300
dee91e82 7301 /* This is cheap if the section is already read in. */
96b79293 7302 section->read (objfile);
dee91e82 7303
976ca316 7304 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7305
9c541725 7306 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7307 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7308 section, abbrev_section, info_ptr,
43988095
JK
7309 (this_cu->is_debug_types
7310 ? rcuh_kind::TYPE
7311 : rcuh_kind::COMPILE));
dee91e82 7312
18a8505e
AT
7313 if (parent_cu != nullptr)
7314 {
c0ab21c2
TT
7315 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7316 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7317 }
4057dfde 7318 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7319
7320 /* Skip dummy compilation units. */
7321 if (info_ptr >= begin_info_ptr + this_cu->length
7322 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7323 {
7324 dummy_p = true;
7325 return;
7326 }
72bf9492 7327
c0ab21c2 7328 m_abbrev_table_holder
86de1d91
TT
7329 = abbrev_table::read (objfile, abbrev_section,
7330 m_new_cu->header.abbrev_sect_off);
dee91e82 7331
c0ab21c2
TT
7332 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7333 m_abbrev_table_holder.get ());
3e225074 7334 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7335}
7336
0018ea6f
DE
7337\f
7338/* Type Unit Groups.
dee91e82 7339
0018ea6f
DE
7340 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7341 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7342 so that all types coming from the same compilation (.o file) are grouped
7343 together. A future step could be to put the types in the same symtab as
7344 the CU the types ultimately came from. */
ff013f42 7345
f4dc4d17
DE
7346static hashval_t
7347hash_type_unit_group (const void *item)
7348{
9a3c8263
SM
7349 const struct type_unit_group *tu_group
7350 = (const struct type_unit_group *) item;
f4dc4d17 7351
094b34ac 7352 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7353}
348e048f
DE
7354
7355static int
f4dc4d17 7356eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7357{
9a3c8263
SM
7358 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7359 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7360
094b34ac 7361 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7362}
348e048f 7363
f4dc4d17
DE
7364/* Allocate a hash table for type unit groups. */
7365
eaa5fa8b 7366static htab_up
298e9637 7367allocate_type_unit_groups_table ()
f4dc4d17 7368{
eaa5fa8b
TT
7369 return htab_up (htab_create_alloc (3,
7370 hash_type_unit_group,
7371 eq_type_unit_group,
7372 NULL, xcalloc, xfree));
f4dc4d17 7373}
dee91e82 7374
f4dc4d17
DE
7375/* Type units that don't have DW_AT_stmt_list are grouped into their own
7376 partial symtabs. We combine several TUs per psymtab to not let the size
7377 of any one psymtab grow too big. */
7378#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7379#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7380
094b34ac 7381/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7382 Create the type_unit_group object used to hold one or more TUs. */
7383
7384static struct type_unit_group *
094b34ac 7385create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7386{
976ca316
SM
7387 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7388 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7389 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7390 struct type_unit_group *tu_group;
f4dc4d17 7391
976ca316 7392 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7393 per_cu = &tu_group->per_cu;
1859c670 7394 per_cu->per_bfd = per_bfd;
f4dc4d17 7395
1859c670 7396 if (per_bfd->using_index)
094b34ac 7397 {
1859c670 7398 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7399 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7400 }
7401 else
7402 {
9c541725 7403 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7404 dwarf2_psymtab *pst;
528e1572 7405 std::string name;
094b34ac
DE
7406
7407 /* Give the symtab a useful name for debug purposes. */
7408 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7409 name = string_printf ("<type_units_%d>",
7410 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7411 else
528e1572 7412 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7413
976ca316 7414 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7415 pst->anonymous = true;
094b34ac 7416 }
f4dc4d17 7417
094b34ac 7418 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7419 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7420
7421 return tu_group;
7422}
7423
094b34ac
DE
7424/* Look up the type_unit_group for type unit CU, and create it if necessary.
7425 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7426
7427static struct type_unit_group *
ff39bb5e 7428get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7429{
976ca316
SM
7430 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7431 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7432 struct type_unit_group *tu_group;
7433 void **slot;
7434 unsigned int line_offset;
7435 struct type_unit_group type_unit_group_for_lookup;
7436
976ca316
SM
7437 if (per_objfile->per_bfd->type_unit_groups == NULL)
7438 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7439
7440 /* Do we need to create a new group, or can we use an existing one? */
7441
7442 if (stmt_list)
7443 {
7444 line_offset = DW_UNSND (stmt_list);
7445 ++tu_stats->nr_symtab_sharers;
7446 }
7447 else
7448 {
7449 /* Ugh, no stmt_list. Rare, but we have to handle it.
7450 We can do various things here like create one group per TU or
7451 spread them over multiple groups to split up the expansion work.
7452 To avoid worst case scenarios (too many groups or too large groups)
7453 we, umm, group them in bunches. */
7454 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7455 | (tu_stats->nr_stmt_less_type_units
7456 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7457 ++tu_stats->nr_stmt_less_type_units;
7458 }
7459
094b34ac 7460 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7461 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7462 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7463 &type_unit_group_for_lookup, INSERT);
7464 if (*slot != NULL)
7465 {
9a3c8263 7466 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7467 gdb_assert (tu_group != NULL);
7468 }
7469 else
7470 {
9c541725 7471 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7472 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7473 *slot = tu_group;
7474 ++tu_stats->nr_symtabs;
7475 }
7476
7477 return tu_group;
7478}
0018ea6f
DE
7479\f
7480/* Partial symbol tables. */
7481
7482/* Create a psymtab named NAME and assign it to PER_CU.
7483
7484 The caller must fill in the following details:
7485 dirname, textlow, texthigh. */
7486
891813be 7487static dwarf2_psymtab *
7aa104c4
SM
7488create_partial_symtab (dwarf2_per_cu_data *per_cu,
7489 dwarf2_per_objfile *per_objfile,
7490 const char *name)
0018ea6f 7491{
7aa104c4 7492 struct objfile *objfile = per_objfile->objfile;
891813be 7493 dwarf2_psymtab *pst;
0018ea6f 7494
9f4e76a4 7495 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7496
6d94535f 7497 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7498
7499 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7500 per_cu->v.psymtab = pst;
7501
7502 return pst;
7503}
7504
c0ab21c2 7505/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7506
7507static void
7508process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7509 const gdb_byte *info_ptr,
0018ea6f 7510 struct die_info *comp_unit_die,
c0ab21c2 7511 enum language pretend_language)
0018ea6f
DE
7512{
7513 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7514 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7515 struct objfile *objfile = per_objfile->objfile;
08feed99 7516 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7517 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7518 CORE_ADDR baseaddr;
7519 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7520 dwarf2_psymtab *pst;
3a2b436a 7521 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7522 const char *filename;
0018ea6f 7523
0018ea6f
DE
7524 gdb_assert (! per_cu->is_debug_types);
7525
c0ab21c2 7526 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7527
0018ea6f 7528 /* Allocate a new partial symbol table structure. */
2e927613
TV
7529 gdb::unique_xmalloc_ptr<char> debug_filename;
7530 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7531 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7532 if (filename == NULL)
0018ea6f 7533 filename = "";
2e927613
TV
7534 else if (strcmp (filename, artificial) == 0)
7535 {
7536 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7537 sect_offset_str (per_cu->sect_off),
7538 (char *) NULL));
2e927613
TV
7539 filename = debug_filename.get ();
7540 }
0018ea6f 7541
7aa104c4 7542 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7543
7544 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7545 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7546
b3b3bada 7547 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7548
7549 dwarf2_find_base_address (comp_unit_die, cu);
7550
7551 /* Possibly set the default values of LOWPC and HIGHPC from
7552 `DW_AT_ranges'. */
3a2b436a
JK
7553 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7554 &best_highpc, cu, pst);
7555 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7556 {
7557 CORE_ADDR low
7558 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7559 - baseaddr);
7560 CORE_ADDR high
7561 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7562 - baseaddr - 1);
7563 /* Store the contiguous range if it is not empty; it can be
7564 empty for CUs with no code. */
d320c2b5
TT
7565 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7566 low, high, pst);
79748972 7567 }
0018ea6f
DE
7568
7569 /* Check if comp unit has_children.
7570 If so, read the rest of the partial symbols from this comp unit.
7571 If not, there's no more debug_info for this comp unit. */
3e225074 7572 if (comp_unit_die->has_children)
0018ea6f
DE
7573 {
7574 struct partial_die_info *first_die;
7575 CORE_ADDR lowpc, highpc;
7576
7577 lowpc = ((CORE_ADDR) -1);
7578 highpc = ((CORE_ADDR) 0);
7579
7580 first_die = load_partial_dies (reader, info_ptr, 1);
7581
7582 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7583 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7584
7585 /* If we didn't find a lowpc, set it to highpc to avoid
7586 complaints from `maint check'. */
7587 if (lowpc == ((CORE_ADDR) -1))
7588 lowpc = highpc;
7589
7590 /* If the compilation unit didn't have an explicit address range,
7591 then use the information extracted from its child dies. */
e385593e 7592 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7593 {
7594 best_lowpc = lowpc;
7595 best_highpc = highpc;
7596 }
7597 }
4ae976d1 7598 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7599 best_lowpc + baseaddr)
7600 - baseaddr);
4ae976d1 7601 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7602 best_highpc + baseaddr)
7603 - baseaddr);
0018ea6f 7604
8763cede 7605 end_psymtab_common (objfile, pst);
0018ea6f 7606
ae640021 7607 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7608 {
7609 int i;
ae640021 7610 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7611
7612 /* Fill in 'dependencies' here; we fill in 'users' in a
7613 post-pass. */
7614 pst->number_of_dependencies = len;
a9342b62
TT
7615 pst->dependencies
7616 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7617 for (i = 0; i < len; ++i)
7618 {
7619 pst->dependencies[i]
7620 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7621 }
0018ea6f 7622
ae640021 7623 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7624 }
7625
7626 /* Get the list of files included in the current compilation unit,
7627 and build a psymtab for each of them. */
7628 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7629
b4f54984 7630 if (dwarf_read_debug)
b926417a
TT
7631 fprintf_unfiltered (gdb_stdlog,
7632 "Psymtab for %s unit @%s: %s - %s"
7633 ", %d global, %d static syms\n",
7634 per_cu->is_debug_types ? "type" : "comp",
7635 sect_offset_str (per_cu->sect_off),
7636 paddress (gdbarch, pst->text_low (objfile)),
7637 paddress (gdbarch, pst->text_high (objfile)),
7638 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7639}
7640
7641/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7642 Process compilation unit THIS_CU for a psymtab. */
7643
7644static void
ab432490
SM
7645process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7646 dwarf2_per_objfile *per_objfile,
135f5437 7647 bool want_partial_unit,
b93601f3 7648 enum language pretend_language)
0018ea6f
DE
7649{
7650 /* If this compilation unit was already read in, free the
7651 cached copy in order to read it in again. This is
7652 necessary because we skipped some symbols when we first
7653 read in the compilation unit (see load_partial_dies).
7654 This problem could be avoided, but the benefit is unclear. */
7188ed02 7655 per_objfile->remove_cu (this_cu);
0018ea6f 7656
2e671100 7657 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7658
58990295
TV
7659 switch (reader.comp_unit_die->tag)
7660 {
7661 case DW_TAG_compile_unit:
7662 this_cu->unit_type = DW_UT_compile;
7663 break;
7664 case DW_TAG_partial_unit:
7665 this_cu->unit_type = DW_UT_partial;
7666 break;
7667 default:
7668 abort ();
7669 }
7670
c0ab21c2 7671 if (reader.dummy_p)
f1902523 7672 {
c0ab21c2 7673 /* Nothing. */
f1902523 7674 }
c0ab21c2 7675 else if (this_cu->is_debug_types)
3e225074
TT
7676 build_type_psymtabs_reader (&reader, reader.info_ptr,
7677 reader.comp_unit_die);
135f5437
TT
7678 else if (want_partial_unit
7679 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7680 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7681 reader.comp_unit_die,
c0ab21c2 7682 pretend_language);
0018ea6f 7683
7188ed02 7684 this_cu->lang = reader.cu->language;
58990295 7685
0018ea6f 7686 /* Age out any secondary CUs. */
7188ed02 7687 per_objfile->age_comp_units ();
0018ea6f 7688}
f4dc4d17
DE
7689
7690/* Reader function for build_type_psymtabs. */
7691
7692static void
7693build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7694 const gdb_byte *info_ptr,
3e225074 7695 struct die_info *type_unit_die)
f4dc4d17 7696{
976ca316
SM
7697 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7698 struct objfile *objfile = per_objfile->objfile;
f4dc4d17
DE
7699 struct dwarf2_cu *cu = reader->cu;
7700 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7701 struct signatured_type *sig_type;
f4dc4d17
DE
7702 struct type_unit_group *tu_group;
7703 struct attribute *attr;
7704 struct partial_die_info *first_die;
7705 CORE_ADDR lowpc, highpc;
891813be 7706 dwarf2_psymtab *pst;
f4dc4d17 7707
0186c6a7
DE
7708 gdb_assert (per_cu->is_debug_types);
7709 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7710
3e225074 7711 if (! type_unit_die->has_children)
f4dc4d17
DE
7712 return;
7713
052c8bb8 7714 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7715 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7716
df07e2c7 7717 if (tu_group->tus == nullptr)
a8b3b8e9 7718 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7719 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7720
7721 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7722 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7723 pst->anonymous = true;
f4dc4d17
DE
7724
7725 first_die = load_partial_dies (reader, info_ptr, 1);
7726
7727 lowpc = (CORE_ADDR) -1;
7728 highpc = (CORE_ADDR) 0;
7729 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7730
8763cede 7731 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7732}
7733
73051182
DE
7734/* Struct used to sort TUs by their abbreviation table offset. */
7735
7736struct tu_abbrev_offset
7737{
b2bdb8cf
SM
7738 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7739 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7740 {}
7741
7742 signatured_type *sig_type;
73051182
DE
7743 sect_offset abbrev_offset;
7744};
7745
484cf504 7746/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7747
484cf504
TT
7748static bool
7749sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7750 const struct tu_abbrev_offset &b)
73051182 7751{
484cf504 7752 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7753}
7754
7755/* Efficiently read all the type units.
7756 This does the bulk of the work for build_type_psymtabs.
7757
7758 The efficiency is because we sort TUs by the abbrev table they use and
7759 only read each abbrev table once. In one program there are 200K TUs
7760 sharing 8K abbrev tables.
7761
7762 The main purpose of this function is to support building the
5989a64e 7763 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7764 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7765 can collapse the search space by grouping them by stmt_list.
7766 The savings can be significant, in the same program from above the 200K TUs
7767 share 8K stmt_list tables.
7768
7769 FUNC is expected to call get_type_unit_group, which will create the
7770 struct type_unit_group if necessary and add it to
5989a64e 7771 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7772
7773static void
976ca316 7774build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7775{
976ca316 7776 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7777 abbrev_table_up abbrev_table;
73051182 7778 sect_offset abbrev_offset;
73051182
DE
7779
7780 /* It's up to the caller to not call us multiple times. */
976ca316 7781 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7782
976ca316 7783 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7784 return;
7785
7786 /* TUs typically share abbrev tables, and there can be way more TUs than
7787 abbrev tables. Sort by abbrev table to reduce the number of times we
7788 read each abbrev table in.
7789 Alternatives are to punt or to maintain a cache of abbrev tables.
7790 This is simpler and efficient enough for now.
7791
7792 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7793 symtab to use). Typically TUs with the same abbrev offset have the same
7794 stmt_list value too so in practice this should work well.
7795
7796 The basic algorithm here is:
7797
7798 sort TUs by abbrev table
7799 for each TU with same abbrev table:
7800 read abbrev table if first user
7801 read TU top level DIE
7802 [IWBN if DWO skeletons had DW_AT_stmt_list]
7803 call FUNC */
7804
b4f54984 7805 if (dwarf_read_debug)
73051182
DE
7806 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7807
7808 /* Sort in a separate table to maintain the order of all_type_units
7809 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7810 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7811 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7812
976ca316 7813 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7814 sorted_by_abbrev.emplace_back
976ca316 7815 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7816 sig_type->per_cu.sect_off));
73051182 7817
484cf504
TT
7818 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7819 sort_tu_by_abbrev_offset);
73051182 7820
9c541725 7821 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7822
b2bdb8cf 7823 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7824 {
73051182
DE
7825 /* Switch to the next abbrev table if necessary. */
7826 if (abbrev_table == NULL
b2bdb8cf 7827 || tu.abbrev_offset != abbrev_offset)
73051182 7828 {
b2bdb8cf 7829 abbrev_offset = tu.abbrev_offset;
73051182 7830 abbrev_table =
976ca316
SM
7831 abbrev_table::read (per_objfile->objfile,
7832 &per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7833 ++tu_stats->nr_uniq_abbrev_tables;
7834 }
7835
976ca316 7836 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7837 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7838 if (!reader.dummy_p)
7839 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7840 reader.comp_unit_die);
73051182 7841 }
6aa5f3a6 7842}
73051182 7843
6aa5f3a6
DE
7844/* Print collected type unit statistics. */
7845
7846static void
976ca316 7847print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7848{
976ca316 7849 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7850
7851 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7852 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
976ca316 7853 per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7854 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7855 tu_stats->nr_uniq_abbrev_tables);
7856 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7857 tu_stats->nr_symtabs);
7858 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7859 tu_stats->nr_symtab_sharers);
7860 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7861 tu_stats->nr_stmt_less_type_units);
7862 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7863 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7864}
7865
f4dc4d17
DE
7866/* Traversal function for build_type_psymtabs. */
7867
7868static int
7869build_type_psymtab_dependencies (void **slot, void *info)
7870{
976ca316
SM
7871 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7872 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 7873 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7874 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7875 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7876 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7877 int i;
7878
7879 gdb_assert (len > 0);
197400e8 7880 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7881
7882 pst->number_of_dependencies = len;
a9342b62 7883 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7884 for (i = 0; i < len; ++i)
f4dc4d17 7885 {
df07e2c7 7886 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7887 gdb_assert (iter->per_cu.is_debug_types);
7888 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7889 iter->type_unit_group = tu_group;
f4dc4d17
DE
7890 }
7891
df07e2c7
AB
7892 delete tu_group->tus;
7893 tu_group->tus = nullptr;
348e048f
DE
7894
7895 return 1;
7896}
7897
7898/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7899 Build partial symbol tables for the .debug_types comp-units. */
7900
7901static void
976ca316 7902build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7903{
976ca316 7904 if (! create_all_type_units (per_objfile))
348e048f
DE
7905 return;
7906
976ca316 7907 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7908}
f4dc4d17 7909
6aa5f3a6
DE
7910/* Traversal function for process_skeletonless_type_unit.
7911 Read a TU in a DWO file and build partial symbols for it. */
7912
7913static int
7914process_skeletonless_type_unit (void **slot, void *info)
7915{
7916 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7917 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7918 struct signatured_type find_entry, *entry;
7919
7920 /* If this TU doesn't exist in the global table, add it and read it in. */
7921
976ca316
SM
7922 if (per_objfile->per_bfd->signatured_types == NULL)
7923 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7924
7925 find_entry.signature = dwo_unit->signature;
976ca316 7926 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7927 &find_entry, INSERT);
6aa5f3a6
DE
7928 /* If we've already seen this type there's nothing to do. What's happening
7929 is we're doing our own version of comdat-folding here. */
7930 if (*slot != NULL)
7931 return 1;
7932
7933 /* This does the job that create_all_type_units would have done for
7934 this TU. */
976ca316
SM
7935 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7936 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7937 *slot = entry;
7938
7939 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7940 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7941 if (!reader.dummy_p)
7942 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7943 reader.comp_unit_die);
6aa5f3a6
DE
7944
7945 return 1;
7946}
7947
7948/* Traversal function for process_skeletonless_type_units. */
7949
7950static int
7951process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7952{
7953 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7954
7955 if (dwo_file->tus != NULL)
b0b6a987
TT
7956 htab_traverse_noresize (dwo_file->tus.get (),
7957 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7958
7959 return 1;
7960}
7961
7962/* Scan all TUs of DWO files, verifying we've processed them.
7963 This is needed in case a TU was emitted without its skeleton.
7964 Note: This can't be done until we know what all the DWO files are. */
7965
7966static void
976ca316 7967process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7968{
7969 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7970 if (get_dwp_file (per_objfile) == NULL
7971 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7972 {
976ca316 7973 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7974 process_dwo_file_for_skeletonless_type_units,
976ca316 7975 per_objfile);
6aa5f3a6 7976 }
348e048f
DE
7977}
7978
ed2dc618 7979/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7980
7981static void
976ca316 7982set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7983{
976ca316 7984 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7985 {
891813be 7986 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7987
36586728
TT
7988 if (pst == NULL)
7989 continue;
7990
b76e467d 7991 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7992 {
7993 /* Set the 'user' field only if it is not already set. */
7994 if (pst->dependencies[j]->user == NULL)
7995 pst->dependencies[j]->user = pst;
7996 }
7997 }
7998}
7999
93311388
DE
8000/* Build the partial symbol table by doing a quick pass through the
8001 .debug_info and .debug_abbrev sections. */
72bf9492 8002
93311388 8003static void
976ca316 8004dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8005{
976ca316 8006 struct objfile *objfile = per_objfile->objfile;
93311388 8007
b4f54984 8008 if (dwarf_read_debug)
45cfd468
DE
8009 {
8010 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8011 objfile_name (objfile));
45cfd468
DE
8012 }
8013
76935768 8014 scoped_restore restore_reading_psyms
976ca316 8015 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 8016 true);
98bfdba5 8017
976ca316 8018 per_objfile->per_bfd->info.read (objfile);
91c24f0a 8019
93311388
DE
8020 /* Any cached compilation units will be linked by the per-objfile
8021 read_in_chain. Make sure to free them when we're done. */
976ca316 8022 free_cached_comp_units freer (per_objfile);
72bf9492 8023
976ca316 8024 build_type_psymtabs (per_objfile);
348e048f 8025
976ca316 8026 create_all_comp_units (per_objfile);
c906108c 8027
60606b2c
TT
8028 /* Create a temporary address map on a temporary obstack. We later
8029 copy this to the final obstack. */
8268c778 8030 auto_obstack temp_obstack;
791afaa2
TT
8031
8032 scoped_restore save_psymtabs_addrmap
d320c2b5 8033 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8034 addrmap_create_mutable (&temp_obstack));
72bf9492 8035
976ca316 8036 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8037 {
8038 if (per_cu->v.psymtab != NULL)
8039 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8040 continue;
976ca316 8041 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8042 language_minimal);
3d5afab3 8043 }
ff013f42 8044
6aa5f3a6 8045 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8046 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8047
8048 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8049 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8050 {
976ca316
SM
8051 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8052 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8053 }
8054
b4f54984 8055 if (dwarf_read_debug)
976ca316 8056 print_tu_stats (per_objfile);
6aa5f3a6 8057
976ca316 8058 set_partial_user (per_objfile);
95554aad 8059
d320c2b5
TT
8060 objfile->partial_symtabs->psymtabs_addrmap
8061 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8062 objfile->partial_symtabs->obstack ());
791afaa2
TT
8063 /* At this point we want to keep the address map. */
8064 save_psymtabs_addrmap.release ();
ff013f42 8065
b4f54984 8066 if (dwarf_read_debug)
45cfd468 8067 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8068 objfile_name (objfile));
ae038cb0
DJ
8069}
8070
dee91e82
DE
8071/* Load the partial DIEs for a secondary CU into memory.
8072 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8073
dee91e82 8074static void
ab432490 8075load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8076 dwarf2_per_objfile *per_objfile,
8077 dwarf2_cu *existing_cu)
dee91e82 8078{
2e671100 8079 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8080
8081 if (!reader.dummy_p)
8082 {
8083 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8084 language_minimal);
8085
8086 /* Check if comp unit has_children.
8087 If so, read the rest of the partial symbols from this comp unit.
8088 If not, there's no more debug_info for this comp unit. */
3e225074 8089 if (reader.comp_unit_die->has_children)
c0ab21c2 8090 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8091
8092 reader.keep ();
c0ab21c2 8093 }
ae038cb0
DJ
8094}
8095
ae038cb0 8096static void
976ca316 8097read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8098 struct dwarf2_section_info *section,
f1902523 8099 struct dwarf2_section_info *abbrev_section,
b76e467d 8100 unsigned int is_dwz)
ae038cb0 8101{
d521ce57 8102 const gdb_byte *info_ptr;
976ca316 8103 struct objfile *objfile = per_objfile->objfile;
be391dca 8104
b4f54984 8105 if (dwarf_read_debug)
bf6af496 8106 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8107 section->get_name (),
8108 section->get_file_name ());
bf6af496 8109
96b79293 8110 section->read (objfile);
ae038cb0 8111
36586728 8112 info_ptr = section->buffer;
6e70227d 8113
36586728 8114 while (info_ptr < section->buffer + section->size)
ae038cb0 8115 {
ae038cb0 8116 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8117
9c541725 8118 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8119
f1902523 8120 comp_unit_head cu_header;
976ca316 8121 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8122 abbrev_section, info_ptr,
8123 rcuh_kind::COMPILE);
ae038cb0
DJ
8124
8125 /* Save the compilation unit for later lookup. */
f1902523 8126 if (cu_header.unit_type != DW_UT_type)
976ca316 8127 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8128 else
8129 {
976ca316 8130 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8131 sig_type->signature = cu_header.signature;
8132 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8133 this_cu = &sig_type->per_cu;
8134 }
8135 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8136 this_cu->sect_off = sect_off;
f1902523 8137 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8138 this_cu->is_dwz = is_dwz;
8a0459fd 8139 this_cu->section = section;
ae038cb0 8140
976ca316 8141 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8142
8143 info_ptr = info_ptr + this_cu->length;
8144 }
36586728
TT
8145}
8146
8147/* Create a list of all compilation units in OBJFILE.
8148 This is only done for -readnow and building partial symtabs. */
8149
8150static void
976ca316 8151create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8152{
976ca316
SM
8153 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8154 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8155 &per_objfile->per_bfd->abbrev, 0);
36586728 8156
976ca316 8157 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8158 if (dwz != NULL)
976ca316 8159 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8160}
8161
5734ee8b 8162/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8163 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8164 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8165 DW_AT_ranges). See the comments of add_partial_subprogram on how
8166 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8167
72bf9492
DJ
8168static void
8169scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8170 CORE_ADDR *highpc, int set_addrmap,
8171 struct dwarf2_cu *cu)
c906108c 8172{
72bf9492 8173 struct partial_die_info *pdi;
c906108c 8174
91c24f0a
DC
8175 /* Now, march along the PDI's, descending into ones which have
8176 interesting children but skipping the children of the other ones,
8177 until we reach the end of the compilation unit. */
c906108c 8178
72bf9492 8179 pdi = first_die;
91c24f0a 8180
72bf9492
DJ
8181 while (pdi != NULL)
8182 {
52356b79 8183 pdi->fixup (cu);
c906108c 8184
f55ee35c 8185 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8186 children, so we need to look at them. Ditto for anonymous
8187 enums. */
933c6fe4 8188
7d00ffec 8189 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8190 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8191 || pdi->tag == DW_TAG_imported_unit
8192 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8193 {
72bf9492 8194 switch (pdi->tag)
c906108c
SS
8195 {
8196 case DW_TAG_subprogram:
b1dc1806 8197 case DW_TAG_inlined_subroutine:
cdc07690 8198 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8199 if (cu->language == language_cplus)
8200 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8201 set_addrmap, cu);
c906108c 8202 break;
72929c62 8203 case DW_TAG_constant:
c906108c
SS
8204 case DW_TAG_variable:
8205 case DW_TAG_typedef:
91c24f0a 8206 case DW_TAG_union_type:
317d2668
TV
8207 if (!pdi->is_declaration
8208 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8209 {
72bf9492 8210 add_partial_symbol (pdi, cu);
63d06c5c
DC
8211 }
8212 break;
c906108c 8213 case DW_TAG_class_type:
680b30c7 8214 case DW_TAG_interface_type:
c906108c 8215 case DW_TAG_structure_type:
72bf9492 8216 if (!pdi->is_declaration)
c906108c 8217 {
72bf9492 8218 add_partial_symbol (pdi, cu);
c906108c 8219 }
b7fee5a3
KS
8220 if ((cu->language == language_rust
8221 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8222 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8223 set_addrmap, cu);
c906108c 8224 break;
91c24f0a 8225 case DW_TAG_enumeration_type:
72bf9492
DJ
8226 if (!pdi->is_declaration)
8227 add_partial_enumeration (pdi, cu);
c906108c
SS
8228 break;
8229 case DW_TAG_base_type:
a02abb62 8230 case DW_TAG_subrange_type:
c906108c 8231 /* File scope base type definitions are added to the partial
c5aa993b 8232 symbol table. */
72bf9492 8233 add_partial_symbol (pdi, cu);
c906108c 8234 break;
d9fa45fe 8235 case DW_TAG_namespace:
cdc07690 8236 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8237 break;
5d7cb8df 8238 case DW_TAG_module:
59c35742
AB
8239 if (!pdi->is_declaration)
8240 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8241 break;
95554aad
TT
8242 case DW_TAG_imported_unit:
8243 {
8244 struct dwarf2_per_cu_data *per_cu;
8245
f4dc4d17
DE
8246 /* For now we don't handle imported units in type units. */
8247 if (cu->per_cu->is_debug_types)
8248 {
8249 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8250 " supported in type units [in module %s]"),
5e22e966 8251 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8252 }
8253
e3b94546 8254 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8255 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8256
8257 /* Go read the partial unit, if needed. */
8258 if (per_cu->v.psymtab == NULL)
ab432490
SM
8259 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8260 cu->language);
95554aad 8261
ae640021 8262 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8263 }
8264 break;
74921315
KS
8265 case DW_TAG_imported_declaration:
8266 add_partial_symbol (pdi, cu);
8267 break;
c906108c
SS
8268 default:
8269 break;
8270 }
8271 }
8272
72bf9492
DJ
8273 /* If the die has a sibling, skip to the sibling. */
8274
8275 pdi = pdi->die_sibling;
8276 }
8277}
8278
8279/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8280
72bf9492 8281 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8282 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8283 Enumerators are an exception; they use the scope of their parent
8284 enumeration type, i.e. the name of the enumeration type is not
8285 prepended to the enumerator.
91c24f0a 8286
72bf9492
DJ
8287 There are two complexities. One is DW_AT_specification; in this
8288 case "parent" means the parent of the target of the specification,
8289 instead of the direct parent of the DIE. The other is compilers
8290 which do not emit DW_TAG_namespace; in this case we try to guess
8291 the fully qualified name of structure types from their members'
8292 linkage names. This must be done using the DIE's children rather
8293 than the children of any DW_AT_specification target. We only need
8294 to do this for structures at the top level, i.e. if the target of
8295 any DW_AT_specification (if any; otherwise the DIE itself) does not
8296 have a parent. */
8297
8298/* Compute the scope prefix associated with PDI's parent, in
8299 compilation unit CU. The result will be allocated on CU's
8300 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8301 field. NULL is returned if no prefix is necessary. */
15d034d0 8302static const char *
72bf9492
DJ
8303partial_die_parent_scope (struct partial_die_info *pdi,
8304 struct dwarf2_cu *cu)
8305{
15d034d0 8306 const char *grandparent_scope;
72bf9492 8307 struct partial_die_info *parent, *real_pdi;
91c24f0a 8308
72bf9492
DJ
8309 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8310 then this means the parent of the specification DIE. */
8311
8312 real_pdi = pdi;
72bf9492 8313 while (real_pdi->has_specification)
fb816e8b 8314 {
122cf0f2
AB
8315 auto res = find_partial_die (real_pdi->spec_offset,
8316 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8317 real_pdi = res.pdi;
8318 cu = res.cu;
8319 }
72bf9492
DJ
8320
8321 parent = real_pdi->die_parent;
8322 if (parent == NULL)
8323 return NULL;
8324
8325 if (parent->scope_set)
8326 return parent->scope;
8327
52356b79 8328 parent->fixup (cu);
72bf9492 8329
10b3939b 8330 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8331
acebe513
UW
8332 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8333 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8334 Work around this problem here. */
8335 if (cu->language == language_cplus
6e70227d 8336 && parent->tag == DW_TAG_namespace
7d00ffec 8337 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8338 && grandparent_scope == NULL)
8339 {
8340 parent->scope = NULL;
8341 parent->scope_set = 1;
8342 return NULL;
8343 }
8344
0a4b0913 8345 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8346 if (pdi->tag == DW_TAG_enumerator)
8347 /* Enumerators should not get the name of the enumeration as a prefix. */
8348 parent->scope = grandparent_scope;
8349 else if (parent->tag == DW_TAG_namespace
f55ee35c 8350 || parent->tag == DW_TAG_module
72bf9492
DJ
8351 || parent->tag == DW_TAG_structure_type
8352 || parent->tag == DW_TAG_class_type
680b30c7 8353 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8354 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8355 || parent->tag == DW_TAG_enumeration_type
8356 || (cu->language == language_fortran
8357 && parent->tag == DW_TAG_subprogram
8358 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8359 {
8360 if (grandparent_scope == NULL)
7d00ffec 8361 parent->scope = parent->name (cu);
72bf9492 8362 else
3e43a32a
MS
8363 parent->scope = typename_concat (&cu->comp_unit_obstack,
8364 grandparent_scope,
7d00ffec 8365 parent->name (cu), 0, cu);
72bf9492 8366 }
72bf9492
DJ
8367 else
8368 {
8369 /* FIXME drow/2004-04-01: What should we be doing with
8370 function-local names? For partial symbols, we should probably be
8371 ignoring them. */
fa9c3fa0
TT
8372 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8373 dwarf_tag_name (parent->tag),
8374 sect_offset_str (pdi->sect_off));
72bf9492 8375 parent->scope = grandparent_scope;
c906108c
SS
8376 }
8377
72bf9492
DJ
8378 parent->scope_set = 1;
8379 return parent->scope;
8380}
8381
8382/* Return the fully scoped name associated with PDI, from compilation unit
8383 CU. The result will be allocated with malloc. */
4568ecf9 8384
43816ebc 8385static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8386partial_die_full_name (struct partial_die_info *pdi,
8387 struct dwarf2_cu *cu)
8388{
15d034d0 8389 const char *parent_scope;
72bf9492 8390
98bfdba5
PA
8391 /* If this is a template instantiation, we can not work out the
8392 template arguments from partial DIEs. So, unfortunately, we have
8393 to go through the full DIEs. At least any work we do building
8394 types here will be reused if full symbols are loaded later. */
8395 if (pdi->has_template_arguments)
8396 {
52356b79 8397 pdi->fixup (cu);
98bfdba5 8398
7d00ffec 8399 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8400 {
8401 struct die_info *die;
8402 struct attribute attr;
8403 struct dwarf2_cu *ref_cu = cu;
8404
b64f50a1 8405 /* DW_FORM_ref_addr is using section offset. */
b4069958 8406 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8407 attr.form = DW_FORM_ref_addr;
9c541725 8408 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8409 die = follow_die_ref (NULL, &attr, &ref_cu);
8410
43816ebc 8411 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8412 }
8413 }
8414
72bf9492
DJ
8415 parent_scope = partial_die_parent_scope (pdi, cu);
8416 if (parent_scope == NULL)
8417 return NULL;
8418 else
43816ebc 8419 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8420 pdi->name (cu),
8421 0, cu));
c906108c
SS
8422}
8423
8424static void
72bf9492 8425add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8426{
976ca316
SM
8427 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8428 struct objfile *objfile = per_objfile->objfile;
08feed99 8429 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8430 CORE_ADDR addr = 0;
15d034d0 8431 const char *actual_name = NULL;
e142c38c
DJ
8432 CORE_ADDR baseaddr;
8433
b3b3bada 8434 baseaddr = objfile->text_section_offset ();
c906108c 8435
43816ebc
TT
8436 gdb::unique_xmalloc_ptr<char> built_actual_name
8437 = partial_die_full_name (pdi, cu);
15d034d0 8438 if (built_actual_name != NULL)
43816ebc 8439 actual_name = built_actual_name.get ();
63d06c5c 8440
72bf9492 8441 if (actual_name == NULL)
7d00ffec 8442 actual_name = pdi->name (cu);
72bf9492 8443
76e288d1
TT
8444 partial_symbol psymbol;
8445 memset (&psymbol, 0, sizeof (psymbol));
8446 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8447 psymbol.ginfo.section = -1;
8448
8449 /* The code below indicates that the psymbol should be installed by
8450 setting this. */
8451 gdb::optional<psymbol_placement> where;
8452
c906108c
SS
8453 switch (pdi->tag)
8454 {
b1dc1806 8455 case DW_TAG_inlined_subroutine:
c906108c 8456 case DW_TAG_subprogram:
79748972
TT
8457 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8458 - baseaddr);
0a4b0913
AB
8459 if (pdi->is_external
8460 || cu->language == language_ada
8461 || (cu->language == language_fortran
8462 && pdi->die_parent != NULL
8463 && pdi->die_parent->tag == DW_TAG_subprogram))
8464 {
8465 /* Normally, only "external" DIEs are part of the global scope.
8466 But in Ada and Fortran, we want to be able to access nested
8467 procedures globally. So all Ada and Fortran subprograms are
8468 stored in the global scope. */
76e288d1 8469 where = psymbol_placement::GLOBAL;
c906108c
SS
8470 }
8471 else
76e288d1
TT
8472 where = psymbol_placement::STATIC;
8473
8474 psymbol.domain = VAR_DOMAIN;
8475 psymbol.aclass = LOC_BLOCK;
8476 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8477 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8478
8479 if (pdi->main_subprogram && actual_name != NULL)
8480 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8481 break;
72929c62 8482 case DW_TAG_constant:
76e288d1
TT
8483 psymbol.domain = VAR_DOMAIN;
8484 psymbol.aclass = LOC_STATIC;
8485 where = (pdi->is_external
8486 ? psymbol_placement::GLOBAL
8487 : psymbol_placement::STATIC);
72929c62 8488 break;
c906108c 8489 case DW_TAG_variable:
95554aad
TT
8490 if (pdi->d.locdesc)
8491 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8492
95554aad 8493 if (pdi->d.locdesc
caac4577 8494 && addr == 0
976ca316 8495 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8496 {
8497 /* A global or static variable may also have been stripped
8498 out by the linker if unused, in which case its address
8499 will be nullified; do not add such variables into partial
8500 symbol table then. */
8501 }
8502 else if (pdi->is_external)
c906108c
SS
8503 {
8504 /* Global Variable.
8505 Don't enter into the minimal symbol tables as there is
8506 a minimal symbol table entry from the ELF symbols already.
8507 Enter into partial symbol table if it has a location
8508 descriptor or a type.
8509 If the location descriptor is missing, new_symbol will create
8510 a LOC_UNRESOLVED symbol, the address of the variable will then
8511 be determined from the minimal symbol table whenever the variable
8512 is referenced.
8513 The address for the partial symbol table entry is not
8514 used by GDB, but it comes in handy for debugging partial symbol
8515 table building. */
8516
95554aad 8517 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8518 {
8519 psymbol.domain = VAR_DOMAIN;
8520 psymbol.aclass = LOC_STATIC;
8521 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8522 psymbol.ginfo.value.address = addr;
8523 where = psymbol_placement::GLOBAL;
8524 }
c906108c
SS
8525 }
8526 else
8527 {
ff908ebf
AW
8528 int has_loc = pdi->d.locdesc != NULL;
8529
8530 /* Static Variable. Skip symbols whose value we cannot know (those
8531 without location descriptors or constant values). */
8532 if (!has_loc && !pdi->has_const_value)
43816ebc 8533 return;
ff908ebf 8534
76e288d1
TT
8535 psymbol.domain = VAR_DOMAIN;
8536 psymbol.aclass = LOC_STATIC;
8537 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8538 if (has_loc)
8539 psymbol.ginfo.value.address = addr;
8540 where = psymbol_placement::STATIC;
c906108c
SS
8541 }
8542 break;
8543 case DW_TAG_typedef:
8544 case DW_TAG_base_type:
a02abb62 8545 case DW_TAG_subrange_type:
76e288d1
TT
8546 psymbol.domain = VAR_DOMAIN;
8547 psymbol.aclass = LOC_TYPEDEF;
8548 where = psymbol_placement::STATIC;
c906108c 8549 break;
74921315 8550 case DW_TAG_imported_declaration:
72bf9492 8551 case DW_TAG_namespace:
76e288d1
TT
8552 psymbol.domain = VAR_DOMAIN;
8553 psymbol.aclass = LOC_TYPEDEF;
8554 where = psymbol_placement::GLOBAL;
72bf9492 8555 break;
530e8392 8556 case DW_TAG_module:
a5fd13a9
BH
8557 /* With Fortran 77 there might be a "BLOCK DATA" module
8558 available without any name. If so, we skip the module as it
8559 doesn't bring any value. */
8560 if (actual_name != nullptr)
76e288d1
TT
8561 {
8562 psymbol.domain = MODULE_DOMAIN;
8563 psymbol.aclass = LOC_TYPEDEF;
8564 where = psymbol_placement::GLOBAL;
8565 }
530e8392 8566 break;
c906108c 8567 case DW_TAG_class_type:
680b30c7 8568 case DW_TAG_interface_type:
c906108c
SS
8569 case DW_TAG_structure_type:
8570 case DW_TAG_union_type:
8571 case DW_TAG_enumeration_type:
fa4028e9
JB
8572 /* Skip external references. The DWARF standard says in the section
8573 about "Structure, Union, and Class Type Entries": "An incomplete
8574 structure, union or class type is represented by a structure,
8575 union or class entry that does not have a byte size attribute
8576 and that has a DW_AT_declaration attribute." */
8577 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8578 return;
fa4028e9 8579
63d06c5c
DC
8580 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8581 static vs. global. */
76e288d1
TT
8582 psymbol.domain = STRUCT_DOMAIN;
8583 psymbol.aclass = LOC_TYPEDEF;
8584 where = (cu->language == language_cplus
8585 ? psymbol_placement::GLOBAL
8586 : psymbol_placement::STATIC);
c906108c
SS
8587 break;
8588 case DW_TAG_enumerator:
76e288d1
TT
8589 psymbol.domain = VAR_DOMAIN;
8590 psymbol.aclass = LOC_CONST;
8591 where = (cu->language == language_cplus
8592 ? psymbol_placement::GLOBAL
8593 : psymbol_placement::STATIC);
c906108c
SS
8594 break;
8595 default:
8596 break;
8597 }
76e288d1
TT
8598
8599 if (where.has_value ())
8600 {
f049a313
TT
8601 if (built_actual_name != nullptr)
8602 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8603 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8604 psymbol.ginfo.set_linkage_name (actual_name);
8605 else
8606 {
8607 psymbol.ginfo.set_demangled_name (actual_name,
8608 &objfile->objfile_obstack);
8609 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8610 }
76e288d1
TT
8611 add_psymbol_to_list (psymbol, *where, objfile);
8612 }
c906108c
SS
8613}
8614
5c4e30ca
DC
8615/* Read a partial die corresponding to a namespace; also, add a symbol
8616 corresponding to that namespace to the symbol table. NAMESPACE is
8617 the name of the enclosing namespace. */
91c24f0a 8618
72bf9492
DJ
8619static void
8620add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8621 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8622 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8623{
72bf9492 8624 /* Add a symbol for the namespace. */
e7c27a73 8625
72bf9492 8626 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8627
8628 /* Now scan partial symbols in that namespace. */
8629
91c24f0a 8630 if (pdi->has_children)
cdc07690 8631 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8632}
8633
5d7cb8df
JK
8634/* Read a partial die corresponding to a Fortran module. */
8635
8636static void
8637add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8638 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8639{
530e8392
KB
8640 /* Add a symbol for the namespace. */
8641
8642 add_partial_symbol (pdi, cu);
8643
f55ee35c 8644 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8645
8646 if (pdi->has_children)
cdc07690 8647 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8648}
8649
b1dc1806
XR
8650/* Read a partial die corresponding to a subprogram or an inlined
8651 subprogram and create a partial symbol for that subprogram.
8652 When the CU language allows it, this routine also defines a partial
8653 symbol for each nested subprogram that this subprogram contains.
8654 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8655 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8656
cdc07690
YQ
8657 PDI may also be a lexical block, in which case we simply search
8658 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8659 Again, this is only performed when the CU language allows this
8660 type of definitions. */
8661
8662static void
8663add_partial_subprogram (struct partial_die_info *pdi,
8664 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8665 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8666{
b1dc1806 8667 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8668 {
8669 if (pdi->has_pc_info)
8670 {
8671 if (pdi->lowpc < *lowpc)
8672 *lowpc = pdi->lowpc;
8673 if (pdi->highpc > *highpc)
8674 *highpc = pdi->highpc;
cdc07690 8675 if (set_addrmap)
5734ee8b 8676 {
5e22e966 8677 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8678 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8679 CORE_ADDR baseaddr;
b926417a
TT
8680 CORE_ADDR this_highpc;
8681 CORE_ADDR this_lowpc;
5734ee8b 8682
b3b3bada 8683 baseaddr = objfile->text_section_offset ();
b926417a
TT
8684 this_lowpc
8685 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8686 pdi->lowpc + baseaddr)
8687 - baseaddr);
8688 this_highpc
8689 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8690 pdi->highpc + baseaddr)
8691 - baseaddr);
d320c2b5 8692 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8693 this_lowpc, this_highpc - 1,
9291a0cd 8694 cu->per_cu->v.psymtab);
5734ee8b 8695 }
481860b3
GB
8696 }
8697
8698 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8699 {
bc30ff58 8700 if (!pdi->is_declaration)
e8d05480
JB
8701 /* Ignore subprogram DIEs that do not have a name, they are
8702 illegal. Do not emit a complaint at this point, we will
8703 do so when we convert this psymtab into a symtab. */
7d00ffec 8704 if (pdi->name (cu))
e8d05480 8705 add_partial_symbol (pdi, cu);
bc30ff58
JB
8706 }
8707 }
6e70227d 8708
bc30ff58
JB
8709 if (! pdi->has_children)
8710 return;
8711
0a4b0913 8712 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8713 {
8714 pdi = pdi->die_child;
8715 while (pdi != NULL)
8716 {
52356b79 8717 pdi->fixup (cu);
bc30ff58 8718 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8719 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8720 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8721 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8722 pdi = pdi->die_sibling;
8723 }
8724 }
8725}
8726
91c24f0a
DC
8727/* Read a partial die corresponding to an enumeration type. */
8728
72bf9492
DJ
8729static void
8730add_partial_enumeration (struct partial_die_info *enum_pdi,
8731 struct dwarf2_cu *cu)
91c24f0a 8732{
72bf9492 8733 struct partial_die_info *pdi;
91c24f0a 8734
7d00ffec 8735 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8736 add_partial_symbol (enum_pdi, cu);
8737
8738 pdi = enum_pdi->die_child;
8739 while (pdi)
91c24f0a 8740 {
7d00ffec 8741 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8742 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8743 else
72bf9492
DJ
8744 add_partial_symbol (pdi, cu);
8745 pdi = pdi->die_sibling;
91c24f0a 8746 }
91c24f0a
DC
8747}
8748
6caca83c
CC
8749/* Return the initial uleb128 in the die at INFO_PTR. */
8750
8751static unsigned int
d521ce57 8752peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8753{
8754 unsigned int bytes_read;
8755
8756 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8757}
8758
685af9cd
TT
8759/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8760 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8761
4bb7a0a7
DJ
8762 Return the corresponding abbrev, or NULL if the number is zero (indicating
8763 an empty DIE). In either case *BYTES_READ will be set to the length of
8764 the initial number. */
8765
8766static struct abbrev_info *
685af9cd
TT
8767peek_die_abbrev (const die_reader_specs &reader,
8768 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8769{
685af9cd 8770 dwarf2_cu *cu = reader.cu;
5e22e966 8771 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8772 unsigned int abbrev_number
8773 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8774
8775 if (abbrev_number == 0)
8776 return NULL;
8777
685af9cd 8778 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8779 if (!abbrev)
8780 {
422b9917 8781 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8782 " at offset %s [in module %s]"),
422b9917 8783 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8784 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8785 }
8786
8787 return abbrev;
8788}
8789
93311388
DE
8790/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8791 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8792 DIE. Any children of the skipped DIEs will also be skipped. */
8793
d521ce57
TT
8794static const gdb_byte *
8795skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8796{
4bb7a0a7
DJ
8797 while (1)
8798 {
685af9cd
TT
8799 unsigned int bytes_read;
8800 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8801
4bb7a0a7
DJ
8802 if (abbrev == NULL)
8803 return info_ptr + bytes_read;
8804 else
dee91e82 8805 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8806 }
8807}
8808
93311388
DE
8809/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8810 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8811 abbrev corresponding to that skipped uleb128 should be passed in
8812 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8813 children. */
8814
d521ce57
TT
8815static const gdb_byte *
8816skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8817 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8818{
8819 unsigned int bytes_read;
8820 struct attribute attr;
dee91e82
DE
8821 bfd *abfd = reader->abfd;
8822 struct dwarf2_cu *cu = reader->cu;
d521ce57 8823 const gdb_byte *buffer = reader->buffer;
f664829e 8824 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8825 unsigned int form, i;
8826
8827 for (i = 0; i < abbrev->num_attrs; i++)
8828 {
8829 /* The only abbrev we care about is DW_AT_sibling. */
8830 if (abbrev->attrs[i].name == DW_AT_sibling)
8831 {
18a8505e
AT
8832 bool ignored;
8833 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8834 &ignored);
4bb7a0a7 8835 if (attr.form == DW_FORM_ref_addr)
b98664d3 8836 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8837 else
b9502d3f 8838 {
0826b30a 8839 sect_offset off = attr.get_ref_die_offset ();
9c541725 8840 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8841
8842 if (sibling_ptr < info_ptr)
b98664d3 8843 complaint (_("DW_AT_sibling points backwards"));
22869d73 8844 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8845 reader->die_section->overflow_complaint ();
b9502d3f
WN
8846 else
8847 return sibling_ptr;
8848 }
4bb7a0a7
DJ
8849 }
8850
8851 /* If it isn't DW_AT_sibling, skip this attribute. */
8852 form = abbrev->attrs[i].form;
8853 skip_attribute:
8854 switch (form)
8855 {
4bb7a0a7 8856 case DW_FORM_ref_addr:
ae411497
TT
8857 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8858 and later it is offset sized. */
8859 if (cu->header.version == 2)
8860 info_ptr += cu->header.addr_size;
8861 else
8862 info_ptr += cu->header.offset_size;
8863 break;
36586728
TT
8864 case DW_FORM_GNU_ref_alt:
8865 info_ptr += cu->header.offset_size;
8866 break;
ae411497 8867 case DW_FORM_addr:
4bb7a0a7
DJ
8868 info_ptr += cu->header.addr_size;
8869 break;
8870 case DW_FORM_data1:
8871 case DW_FORM_ref1:
8872 case DW_FORM_flag:
8fe0f950 8873 case DW_FORM_strx1:
4bb7a0a7
DJ
8874 info_ptr += 1;
8875 break;
2dc7f7b3 8876 case DW_FORM_flag_present:
43988095 8877 case DW_FORM_implicit_const:
2dc7f7b3 8878 break;
4bb7a0a7
DJ
8879 case DW_FORM_data2:
8880 case DW_FORM_ref2:
8fe0f950 8881 case DW_FORM_strx2:
4bb7a0a7
DJ
8882 info_ptr += 2;
8883 break;
8fe0f950
AT
8884 case DW_FORM_strx3:
8885 info_ptr += 3;
8886 break;
4bb7a0a7
DJ
8887 case DW_FORM_data4:
8888 case DW_FORM_ref4:
8fe0f950 8889 case DW_FORM_strx4:
4bb7a0a7
DJ
8890 info_ptr += 4;
8891 break;
8892 case DW_FORM_data8:
8893 case DW_FORM_ref8:
55f1336d 8894 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8895 info_ptr += 8;
8896 break;
0224619f
JK
8897 case DW_FORM_data16:
8898 info_ptr += 16;
8899 break;
4bb7a0a7 8900 case DW_FORM_string:
9b1c24c8 8901 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8902 info_ptr += bytes_read;
8903 break;
2dc7f7b3 8904 case DW_FORM_sec_offset:
4bb7a0a7 8905 case DW_FORM_strp:
36586728 8906 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8907 info_ptr += cu->header.offset_size;
8908 break;
2dc7f7b3 8909 case DW_FORM_exprloc:
4bb7a0a7
DJ
8910 case DW_FORM_block:
8911 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8912 info_ptr += bytes_read;
8913 break;
8914 case DW_FORM_block1:
8915 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8916 break;
8917 case DW_FORM_block2:
8918 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8919 break;
8920 case DW_FORM_block4:
8921 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8922 break;
336d760d 8923 case DW_FORM_addrx:
cf532bd1 8924 case DW_FORM_strx:
4bb7a0a7
DJ
8925 case DW_FORM_sdata:
8926 case DW_FORM_udata:
8927 case DW_FORM_ref_udata:
3019eac3
DE
8928 case DW_FORM_GNU_addr_index:
8929 case DW_FORM_GNU_str_index:
18a8505e 8930 case DW_FORM_rnglistx:
41144253 8931 case DW_FORM_loclistx:
d521ce57 8932 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8933 break;
8934 case DW_FORM_indirect:
8935 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8936 info_ptr += bytes_read;
8937 /* We need to continue parsing from here, so just go back to
8938 the top. */
8939 goto skip_attribute;
8940
8941 default:
3e43a32a
MS
8942 error (_("Dwarf Error: Cannot handle %s "
8943 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8944 dwarf_form_name (form),
8945 bfd_get_filename (abfd));
8946 }
8947 }
8948
8949 if (abbrev->has_children)
dee91e82 8950 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8951 else
8952 return info_ptr;
8953}
8954
93311388 8955/* Locate ORIG_PDI's sibling.
dee91e82 8956 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8957
d521ce57 8958static const gdb_byte *
dee91e82
DE
8959locate_pdi_sibling (const struct die_reader_specs *reader,
8960 struct partial_die_info *orig_pdi,
d521ce57 8961 const gdb_byte *info_ptr)
91c24f0a
DC
8962{
8963 /* Do we know the sibling already? */
72bf9492 8964
91c24f0a
DC
8965 if (orig_pdi->sibling)
8966 return orig_pdi->sibling;
8967
8968 /* Are there any children to deal with? */
8969
8970 if (!orig_pdi->has_children)
8971 return info_ptr;
8972
4bb7a0a7 8973 /* Skip the children the long way. */
91c24f0a 8974
dee91e82 8975 return skip_children (reader, info_ptr);
91c24f0a
DC
8976}
8977
257e7a09 8978/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8979 not NULL. */
c906108c 8980
891813be
TT
8981void
8982dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8983{
976ca316 8984 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8985
976ca316 8986 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8987
077cbab2
TT
8988 /* If this psymtab is constructed from a debug-only objfile, the
8989 has_section_at_zero flag will not necessarily be correct. We
8990 can get the correct value for this flag by looking at the data
8991 associated with the (presumably stripped) associated objfile. */
8992 if (objfile->separate_debug_objfile_backlink)
c906108c 8993 {
976ca316 8994 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8995 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8996
976ca316
SM
8997 per_objfile->per_bfd->has_section_at_zero
8998 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8999 }
98bfdba5 9000
8566b89b 9001 expand_psymtab (objfile);
95554aad 9002
976ca316 9003 process_cu_includes (per_objfile);
c906108c 9004}
9cdd5dbd
DE
9005\f
9006/* Reading in full CUs. */
c906108c 9007
10b3939b
DJ
9008/* Add PER_CU to the queue. */
9009
9010static void
120ce1b5
SM
9011queue_comp_unit (dwarf2_per_cu_data *per_cu,
9012 dwarf2_per_objfile *per_objfile,
95554aad 9013 enum language pretend_language)
10b3939b 9014{
10b3939b 9015 per_cu->queued = 1;
120ce1b5 9016 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9017}
9018
89e63ee4
DE
9019/* If PER_CU is not yet queued, add it to the queue.
9020 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9021 dependency.
0907af0c 9022 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9023 meaning either PER_CU is already queued or it is already loaded.
9024
9025 N.B. There is an invariant here that if a CU is queued then it is loaded.
9026 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9027
9028static int
89e63ee4 9029maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9030 dwarf2_per_cu_data *per_cu,
9031 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9032 enum language pretend_language)
9033{
9034 /* We may arrive here during partial symbol reading, if we need full
9035 DIEs to process an unusual case (e.g. template arguments). Do
9036 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9037 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9038 {
7188ed02
SM
9039 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9040
9041 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9042 return 1;
9043 return 0;
9044 }
9045
9046 /* Mark the dependence relation so that we don't flush PER_CU
9047 too early. */
89e63ee4
DE
9048 if (dependent_cu != NULL)
9049 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9050
9051 /* If it's already on the queue, we have nothing to do. */
9052 if (per_cu->queued)
9053 return 0;
9054
9055 /* If the compilation unit is already loaded, just mark it as
9056 used. */
7188ed02
SM
9057 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9058 if (cu != nullptr)
0907af0c 9059 {
7188ed02 9060 cu->last_used = 0;
0907af0c
DE
9061 return 0;
9062 }
9063
9064 /* Add it to the queue. */
120ce1b5 9065 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9066
9067 return 1;
9068}
9069
10b3939b
DJ
9070/* Process the queue. */
9071
9072static void
976ca316 9073process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9074{
b4f54984 9075 if (dwarf_read_debug)
45cfd468
DE
9076 {
9077 fprintf_unfiltered (gdb_stdlog,
9078 "Expanding one or more symtabs of objfile %s ...\n",
976ca316 9079 objfile_name (per_objfile->objfile));
45cfd468
DE
9080 }
9081
03dd20cc
DJ
9082 /* The queue starts out with one item, but following a DIE reference
9083 may load a new CU, adding it to the end of the queue. */
976ca316 9084 while (!per_objfile->per_bfd->queue.empty ())
10b3939b 9085 {
976ca316 9086 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
7188ed02 9087 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9088
976ca316 9089 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9090 {
976ca316 9091 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9092
7188ed02
SM
9093 /* Skip dummy CUs. */
9094 if (cu != nullptr)
73be47f5 9095 {
7188ed02
SM
9096 unsigned int debug_print_threshold;
9097 char buf[100];
9098
9099 if (per_cu->is_debug_types)
9100 {
9101 struct signatured_type *sig_type =
9102 (struct signatured_type *) per_cu;
9103
9104 sprintf (buf, "TU %s at offset %s",
9105 hex_string (sig_type->signature),
9106 sect_offset_str (per_cu->sect_off));
9107 /* There can be 100s of TUs.
9108 Only print them in verbose mode. */
9109 debug_print_threshold = 2;
9110 }
9111 else
9112 {
9113 sprintf (buf, "CU at offset %s",
9114 sect_offset_str (per_cu->sect_off));
9115 debug_print_threshold = 1;
9116 }
247f5c4f 9117
7188ed02
SM
9118 if (dwarf_read_debug >= debug_print_threshold)
9119 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17 9120
7188ed02
SM
9121 if (per_cu->is_debug_types)
9122 process_full_type_unit (cu, item.pretend_language);
9123 else
9124 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9125
7188ed02
SM
9126 if (dwarf_read_debug >= debug_print_threshold)
9127 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9128 }
f4dc4d17 9129 }
10b3939b 9130
7188ed02 9131 per_cu->queued = 0;
976ca316 9132 per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9133 }
9134
b4f54984 9135 if (dwarf_read_debug)
45cfd468
DE
9136 {
9137 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
976ca316 9138 objfile_name (per_objfile->objfile));
45cfd468 9139 }
10b3939b
DJ
9140}
9141
10b3939b
DJ
9142/* Read in full symbols for PST, and anything it depends on. */
9143
8566b89b
TT
9144void
9145dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9146{
af758d11 9147 gdb_assert (!readin_p (objfile));
95554aad 9148
17ee85fc
TT
9149 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9150 free_cached_comp_units freer (per_objfile);
48993951 9151 expand_dependencies (objfile);
aaa75496 9152
97a1449a 9153 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9154 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9155}
9156
af758d11
SM
9157/* See psympriv.h. */
9158
9159bool
9160dwarf2_psymtab::readin_p (struct objfile *objfile) const
9161{
9162 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9163 return per_objfile->symtab_set_p (per_cu_data);
9164}
9165
9166/* See psympriv.h. */
9167
9168compunit_symtab *
9169dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9170{
9171 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9172 return per_objfile->get_symtab (per_cu_data);
9173}
9174
dee91e82
DE
9175/* Trivial hash function for die_info: the hash value of a DIE
9176 is its offset in .debug_info for this objfile. */
10b3939b 9177
dee91e82
DE
9178static hashval_t
9179die_hash (const void *item)
10b3939b 9180{
9a3c8263 9181 const struct die_info *die = (const struct die_info *) item;
6502dd73 9182
9c541725 9183 return to_underlying (die->sect_off);
dee91e82 9184}
63d06c5c 9185
dee91e82
DE
9186/* Trivial comparison function for die_info structures: two DIEs
9187 are equal if they have the same offset. */
98bfdba5 9188
dee91e82
DE
9189static int
9190die_eq (const void *item_lhs, const void *item_rhs)
9191{
9a3c8263
SM
9192 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9193 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9194
9c541725 9195 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9196}
c906108c 9197
c0ab21c2 9198/* Load the DIEs associated with PER_CU into memory. */
c906108c 9199
dee91e82 9200static void
ab432490
SM
9201load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9202 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9203 bool skip_partial,
9204 enum language pretend_language)
dee91e82 9205{
c0ab21c2
TT
9206 gdb_assert (! this_cu->is_debug_types);
9207
7188ed02
SM
9208 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9209 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9210 if (reader.dummy_p)
9211 return;
9212
9213 struct dwarf2_cu *cu = reader.cu;
9214 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9215
dee91e82
DE
9216 gdb_assert (cu->die_hash == NULL);
9217 cu->die_hash =
9218 htab_create_alloc_ex (cu->header.length / 12,
9219 die_hash,
9220 die_eq,
9221 NULL,
9222 &cu->comp_unit_obstack,
9223 hashtab_obstack_allocate,
9224 dummy_obstack_deallocate);
e142c38c 9225
3e225074 9226 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9227 reader.comp_unit_die->child
9228 = read_die_and_siblings (&reader, reader.info_ptr,
9229 &info_ptr, reader.comp_unit_die);
9230 cu->dies = reader.comp_unit_die;
dee91e82 9231 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9232
9233 /* We try not to read any attributes in this function, because not
9cdd5dbd 9234 all CUs needed for references have been loaded yet, and symbol
10b3939b 9235 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9236 or we won't be able to build types correctly.
9237 Similarly, if we do not read the producer, we can not apply
9238 producer-specific interpretation. */
c0ab21c2 9239 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9240
9241 reader.keep ();
10b3939b
DJ
9242}
9243
3da10d80
KS
9244/* Add a DIE to the delayed physname list. */
9245
9246static void
9247add_to_method_list (struct type *type, int fnfield_index, int index,
9248 const char *name, struct die_info *die,
9249 struct dwarf2_cu *cu)
9250{
9251 struct delayed_method_info mi;
9252 mi.type = type;
9253 mi.fnfield_index = fnfield_index;
9254 mi.index = index;
9255 mi.name = name;
9256 mi.die = die;
c89b44cd 9257 cu->method_list.push_back (mi);
3da10d80
KS
9258}
9259
3693fdb3
PA
9260/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9261 "const" / "volatile". If so, decrements LEN by the length of the
9262 modifier and return true. Otherwise return false. */
9263
9264template<size_t N>
9265static bool
9266check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9267{
9268 size_t mod_len = sizeof (mod) - 1;
9269 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9270 {
9271 len -= mod_len;
9272 return true;
9273 }
9274 return false;
9275}
9276
3da10d80
KS
9277/* Compute the physnames of any methods on the CU's method list.
9278
9279 The computation of method physnames is delayed in order to avoid the
9280 (bad) condition that one of the method's formal parameters is of an as yet
9281 incomplete type. */
9282
9283static void
9284compute_delayed_physnames (struct dwarf2_cu *cu)
9285{
3693fdb3 9286 /* Only C++ delays computing physnames. */
c89b44cd 9287 if (cu->method_list.empty ())
3693fdb3
PA
9288 return;
9289 gdb_assert (cu->language == language_cplus);
9290
52941706 9291 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9292 {
1d06ead6 9293 const char *physname;
3da10d80 9294 struct fn_fieldlist *fn_flp
c89b44cd
TT
9295 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9296 physname = dwarf2_physname (mi.name, mi.die, cu);
9297 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9298 = physname ? physname : "";
3693fdb3
PA
9299
9300 /* Since there's no tag to indicate whether a method is a
9301 const/volatile overload, extract that information out of the
9302 demangled name. */
9303 if (physname != NULL)
9304 {
9305 size_t len = strlen (physname);
9306
9307 while (1)
9308 {
9309 if (physname[len] == ')') /* shortcut */
9310 break;
9311 else if (check_modifier (physname, len, " const"))
c89b44cd 9312 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9313 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9314 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9315 else
9316 break;
9317 }
9318 }
3da10d80 9319 }
c89b44cd
TT
9320
9321 /* The list is no longer needed. */
9322 cu->method_list.clear ();
3da10d80
KS
9323}
9324
a766d390
DE
9325/* Go objects should be embedded in a DW_TAG_module DIE,
9326 and it's not clear if/how imported objects will appear.
9327 To keep Go support simple until that's worked out,
9328 go back through what we've read and create something usable.
9329 We could do this while processing each DIE, and feels kinda cleaner,
9330 but that way is more invasive.
9331 This is to, for example, allow the user to type "p var" or "b main"
9332 without having to specify the package name, and allow lookups
9333 of module.object to work in contexts that use the expression
9334 parser. */
9335
9336static void
9337fixup_go_packaging (struct dwarf2_cu *cu)
9338{
421d1616 9339 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9340 struct pending *list;
9341 int i;
9342
c24bdb02 9343 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9344 list != NULL;
9345 list = list->next)
a766d390
DE
9346 {
9347 for (i = 0; i < list->nsyms; ++i)
9348 {
9349 struct symbol *sym = list->symbol[i];
9350
c1b5c1eb 9351 if (sym->language () == language_go
a766d390
DE
9352 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9353 {
421d1616
TT
9354 gdb::unique_xmalloc_ptr<char> this_package_name
9355 (go_symbol_package_name (sym));
a766d390
DE
9356
9357 if (this_package_name == NULL)
9358 continue;
9359 if (package_name == NULL)
421d1616 9360 package_name = std::move (this_package_name);
a766d390
DE
9361 else
9362 {
5e22e966 9363 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9364 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9365 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9366 (symbol_symtab (sym) != NULL
9367 ? symtab_to_filename_for_display
9368 (symbol_symtab (sym))
e3b94546 9369 : objfile_name (objfile)),
421d1616 9370 this_package_name.get (), package_name.get ());
a766d390
DE
9371 }
9372 }
9373 }
9374 }
9375
9376 if (package_name != NULL)
9377 {
5e22e966 9378 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9379 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9380 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9381 saved_package_name);
a766d390
DE
9382 struct symbol *sym;
9383
8c14c3a3 9384 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9385 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9386 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9387 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9388 e.g., "main" finds the "main" module and not C's main(). */
9389 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9390 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9391 SYMBOL_TYPE (sym) = type;
9392
c24bdb02 9393 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9394 }
9395}
9396
c9317f21
TT
9397/* Allocate a fully-qualified name consisting of the two parts on the
9398 obstack. */
9399
9400static const char *
9401rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9402{
9403 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9404}
9405
9c6a1327
TT
9406/* A helper that allocates a variant part to attach to a Rust enum
9407 type. OBSTACK is where the results should be allocated. TYPE is
9408 the type we're processing. DISCRIMINANT_INDEX is the index of the
9409 discriminant. It must be the index of one of the fields of TYPE.
9410 DEFAULT_INDEX is the index of the default field; or -1 if there is
9411 no default. RANGES is indexed by "effective" field number (the
9412 field index, but omitting the discriminant and default fields) and
9413 must hold the discriminant values used by the variants. Note that
9414 RANGES must have a lifetime at least as long as OBSTACK -- either
9415 already allocated on it, or static. */
c9317f21 9416
9c6a1327
TT
9417static void
9418alloc_rust_variant (struct obstack *obstack, struct type *type,
9419 int discriminant_index, int default_index,
9420 gdb::array_view<discriminant_range> ranges)
9421{
9422 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9423 must be handled by the caller. */
9424 gdb_assert (discriminant_index >= 0
1f704f76 9425 && discriminant_index < type->num_fields ());
c9317f21 9426 gdb_assert (default_index == -1
1f704f76 9427 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9428
9c6a1327 9429 /* We have one variant for each non-discriminant field. */
1f704f76 9430 int n_variants = type->num_fields () - 1;
c9317f21 9431
9c6a1327
TT
9432 variant *variants = new (obstack) variant[n_variants];
9433 int var_idx = 0;
9434 int range_idx = 0;
1f704f76 9435 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9436 {
9437 if (i == discriminant_index)
9438 continue;
c9317f21 9439
9c6a1327
TT
9440 variants[var_idx].first_field = i;
9441 variants[var_idx].last_field = i + 1;
9442
9443 /* The default field does not need a range, but other fields do.
9444 We skipped the discriminant above. */
9445 if (i != default_index)
9446 {
9447 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9448 ++range_idx;
9449 }
c9317f21 9450
9c6a1327
TT
9451 ++var_idx;
9452 }
9453
9454 gdb_assert (range_idx == ranges.size ());
9455 gdb_assert (var_idx == n_variants);
9456
9457 variant_part *part = new (obstack) variant_part;
9458 part->discriminant_index = discriminant_index;
940da03e 9459 part->is_unsigned = TYPE_UNSIGNED (type->field (discriminant_index).type ());
9c6a1327
TT
9460 part->variants = gdb::array_view<variant> (variants, n_variants);
9461
9462 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9463 gdb::array_view<variant_part> *prop_value
9464 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9465
9c6a1327
TT
9466 struct dynamic_prop prop;
9467 prop.kind = PROP_VARIANT_PARTS;
9468 prop.data.variant_parts = prop_value;
9469
5c54719c 9470 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9471}
9472
9473/* Some versions of rustc emitted enums in an unusual way.
9474
9475 Ordinary enums were emitted as unions. The first element of each
9476 structure in the union was named "RUST$ENUM$DISR". This element
9477 held the discriminant.
9478
9479 These versions of Rust also implemented the "non-zero"
9480 optimization. When the enum had two values, and one is empty and
9481 the other holds a pointer that cannot be zero, the pointer is used
9482 as the discriminant, with a zero value meaning the empty variant.
9483 Here, the union's first member is of the form
9484 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9485 where the fieldnos are the indices of the fields that should be
9486 traversed in order to find the field (which may be several fields deep)
9487 and the variantname is the name of the variant of the case when the
9488 field is zero.
9489
9490 This function recognizes whether TYPE is of one of these forms,
9491 and, if so, smashes it to be a variant type. */
9492
9493static void
9494quirk_rust_enum (struct type *type, struct objfile *objfile)
9495{
78134374 9496 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9497
9498 /* We don't need to deal with empty enums. */
1f704f76 9499 if (type->num_fields () == 0)
c9317f21
TT
9500 return;
9501
9502#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9503 if (type->num_fields () == 1
c9317f21
TT
9504 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9505 {
9506 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9507
9508 /* Decode the field name to find the offset of the
9509 discriminant. */
9510 ULONGEST bit_offset = 0;
940da03e 9511 struct type *field_type = type->field (0).type ();
c9317f21
TT
9512 while (name[0] >= '0' && name[0] <= '9')
9513 {
9514 char *tail;
9515 unsigned long index = strtoul (name, &tail, 10);
9516 name = tail;
9517 if (*name != '$'
1f704f76 9518 || index >= field_type->num_fields ()
c9317f21
TT
9519 || (TYPE_FIELD_LOC_KIND (field_type, index)
9520 != FIELD_LOC_KIND_BITPOS))
9521 {
b98664d3 9522 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9523 "[in module %s]"),
9524 TYPE_FIELD_NAME (type, 0),
9525 objfile_name (objfile));
9526 return;
9527 }
9528 ++name;
9529
9530 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9531 field_type = field_type->field (index).type ();
c9317f21
TT
9532 }
9533
9c6a1327
TT
9534 /* Smash this type to be a structure type. We have to do this
9535 because the type has already been recorded. */
67607e24 9536 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9537 type->set_num_fields (3);
9c6a1327 9538 /* Save the field we care about. */
ceacbf6e 9539 struct field saved_field = type->field (0);
3cabb6b0
SM
9540 type->set_fields
9541 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9542
9c6a1327 9543 /* Put the discriminant at index 0. */
5d14b6e5 9544 type->field (0).set_type (field_type);
9c6a1327
TT
9545 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9546 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9547 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9548
9549 /* The order of fields doesn't really matter, so put the real
9550 field at index 1 and the data-less field at index 2. */
ceacbf6e 9551 type->field (1) = saved_field;
9c6a1327 9552 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9553 = rust_last_path_segment (type->field (1).type ()->name ());
9554 type->field (1).type ()->set_name
7d93a1e0 9555 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9556 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9557
9558 const char *dataless_name
7d93a1e0 9559 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9560 name);
9561 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9562 dataless_name);
5d14b6e5 9563 type->field (2).set_type (dataless_type);
c9317f21
TT
9564 /* NAME points into the original discriminant name, which
9565 already has the correct lifetime. */
9c6a1327 9566 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9567 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9568
9c6a1327
TT
9569 /* Indicate that this is a variant type. */
9570 static discriminant_range ranges[1] = { { 0, 0 } };
9571 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9572 }
77c2dba3
TT
9573 /* A union with a single anonymous field is probably an old-style
9574 univariant enum. */
1f704f76 9575 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9576 {
c9317f21
TT
9577 /* Smash this type to be a structure type. We have to do this
9578 because the type has already been recorded. */
67607e24 9579 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9580
940da03e 9581 struct type *field_type = type->field (0).type ();
c9317f21 9582 const char *variant_name
7d93a1e0 9583 = rust_last_path_segment (field_type->name ());
9c6a1327 9584 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9585 field_type->set_name
9586 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9587 type->name (), variant_name));
c9317f21
TT
9588 }
9589 else
9590 {
9591 struct type *disr_type = nullptr;
1f704f76 9592 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9593 {
940da03e 9594 disr_type = type->field (i).type ();
c9317f21 9595
78134374 9596 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9597 {
9598 /* All fields of a true enum will be structs. */
9599 return;
9600 }
1f704f76 9601 else if (disr_type->num_fields () == 0)
c9317f21
TT
9602 {
9603 /* Could be data-less variant, so keep going. */
a037790e 9604 disr_type = nullptr;
c9317f21
TT
9605 }
9606 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9607 "RUST$ENUM$DISR") != 0)
9608 {
9609 /* Not a Rust enum. */
9610 return;
9611 }
9612 else
9613 {
9614 /* Found one. */
9615 break;
9616 }
9617 }
9618
9619 /* If we got here without a discriminant, then it's probably
9620 just a union. */
9621 if (disr_type == nullptr)
9622 return;
9623
9624 /* Smash this type to be a structure type. We have to do this
9625 because the type has already been recorded. */
67607e24 9626 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9627
9c6a1327 9628 /* Make space for the discriminant field. */
ceacbf6e 9629 struct field *disr_field = &disr_type->field (0);
9c6a1327 9630 field *new_fields
1f704f76 9631 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9632 * sizeof (struct field)));
80fc5e77 9633 memcpy (new_fields + 1, type->fields (),
1f704f76 9634 type->num_fields () * sizeof (struct field));
3cabb6b0 9635 type->set_fields (new_fields);
1f704f76 9636 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9637
9638 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9639 type->field (0) = *disr_field;
9c6a1327
TT
9640 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9641 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9642
9643 /* We need a way to find the correct discriminant given a
9644 variant name. For convenience we build a map here. */
b6cdac4b 9645 struct type *enum_type = disr_field->type ();
c9317f21 9646 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9647 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9648 {
9649 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9650 {
9651 const char *name
9652 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9653 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9654 }
9655 }
9656
1f704f76 9657 int n_fields = type->num_fields ();
9c6a1327
TT
9658 /* We don't need a range entry for the discriminant, but we do
9659 need one for every other field, as there is no default
9660 variant. */
9661 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9662 discriminant_range,
9663 n_fields - 1);
c9317f21
TT
9664 /* Skip the discriminant here. */
9665 for (int i = 1; i < n_fields; ++i)
9666 {
9667 /* Find the final word in the name of this variant's type.
9668 That name can be used to look up the correct
9669 discriminant. */
9670 const char *variant_name
940da03e 9671 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9672
9673 auto iter = discriminant_map.find (variant_name);
9674 if (iter != discriminant_map.end ())
9c6a1327
TT
9675 {
9676 ranges[i].low = iter->second;
9677 ranges[i].high = iter->second;
9678 }
c9317f21 9679
bedda9ac 9680 /* Remove the discriminant field, if it exists. */
940da03e 9681 struct type *sub_type = type->field (i).type ();
1f704f76 9682 if (sub_type->num_fields () > 0)
bedda9ac 9683 {
5e33d5f4 9684 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9685 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9686 }
9c6a1327 9687 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9688 sub_type->set_name
9689 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9690 type->name (), variant_name));
c9317f21 9691 }
9c6a1327
TT
9692
9693 /* Indicate that this is a variant type. */
a1520ad8 9694 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9695 gdb::array_view<discriminant_range> (ranges,
9696 n_fields - 1));
c9317f21
TT
9697 }
9698}
9699
9700/* Rewrite some Rust unions to be structures with variants parts. */
9701
9702static void
9703rust_union_quirks (struct dwarf2_cu *cu)
9704{
9705 gdb_assert (cu->language == language_rust);
52941706 9706 for (type *type_ : cu->rust_unions)
5e22e966 9707 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9708 /* We don't need this any more. */
9709 cu->rust_unions.clear ();
c9317f21
TT
9710}
9711
8adb8487
TT
9712/* See read.h. */
9713
9714type_unit_group_unshareable *
9715dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9716{
9717 auto iter = this->m_type_units.find (tu_group);
9718 if (iter != this->m_type_units.end ())
9719 return iter->second.get ();
9720
9721 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9722 type_unit_group_unshareable *result = uniq.get ();
9723 this->m_type_units[tu_group] = std::move (uniq);
9724 return result;
9725}
9726
e286671b
TT
9727struct type *
9728dwarf2_per_objfile::get_type_for_signatured_type
9729 (signatured_type *sig_type) const
9730{
9731 auto iter = this->m_type_map.find (sig_type);
9732 if (iter == this->m_type_map.end ())
9733 return nullptr;
9734
9735 return iter->second;
9736}
9737
9738void dwarf2_per_objfile::set_type_for_signatured_type
9739 (signatured_type *sig_type, struct type *type)
9740{
9741 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9742
9743 this->m_type_map[sig_type] = type;
9744}
9745
95554aad
TT
9746/* A helper function for computing the list of all symbol tables
9747 included by PER_CU. */
9748
9749static void
4c39bc03 9750recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9751 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9752 dwarf2_per_cu_data *per_cu,
9753 dwarf2_per_objfile *per_objfile,
43f3e411 9754 struct compunit_symtab *immediate_parent)
95554aad 9755{
af758d11 9756 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9757 if (*slot != NULL)
9758 {
9759 /* This inclusion and its children have been processed. */
9760 return;
9761 }
9762
9763 *slot = per_cu;
af758d11 9764
95554aad 9765 /* Only add a CU if it has a symbol table. */
43182c09 9766 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9767 if (cust != NULL)
ec94af83
DE
9768 {
9769 /* If this is a type unit only add its symbol table if we haven't
9770 seen it yet (type unit per_cu's can share symtabs). */
9771 if (per_cu->is_debug_types)
9772 {
43f3e411 9773 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9774 if (*slot == NULL)
9775 {
43f3e411 9776 *slot = cust;
4c39bc03 9777 result->push_back (cust);
43f3e411
DE
9778 if (cust->user == NULL)
9779 cust->user = immediate_parent;
ec94af83
DE
9780 }
9781 }
9782 else
f9125b6c 9783 {
4c39bc03 9784 result->push_back (cust);
43f3e411
DE
9785 if (cust->user == NULL)
9786 cust->user = immediate_parent;
f9125b6c 9787 }
ec94af83 9788 }
95554aad 9789
ae640021
AB
9790 if (!per_cu->imported_symtabs_empty ())
9791 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9792 {
9793 recursively_compute_inclusions (result, all_children,
43182c09
SM
9794 all_type_symtabs, ptr, per_objfile,
9795 cust);
ae640021 9796 }
95554aad
TT
9797}
9798
43f3e411 9799/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9800 PER_CU. */
9801
9802static void
43182c09
SM
9803compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9804 dwarf2_per_objfile *per_objfile)
95554aad 9805{
f4dc4d17
DE
9806 gdb_assert (! per_cu->is_debug_types);
9807
ae640021 9808 if (!per_cu->imported_symtabs_empty ())
95554aad 9809 {
ae640021 9810 int len;
4c39bc03 9811 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9812 htab_t all_children, all_type_symtabs;
43182c09 9813 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9814
9815 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9816 if (cust == NULL)
95554aad
TT
9817 return;
9818
9819 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9820 NULL, xcalloc, xfree);
ec94af83
DE
9821 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9822 NULL, xcalloc, xfree);
95554aad 9823
ae640021 9824 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9825 {
9826 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9827 all_type_symtabs, ptr, per_objfile,
9828 cust);
ec94af83 9829 }
95554aad 9830
ec94af83 9831 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9832 len = result_symtabs.size ();
43f3e411 9833 cust->includes
f6e649dd 9834 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9835 struct compunit_symtab *, len + 1);
4c39bc03
TT
9836 memcpy (cust->includes, result_symtabs.data (),
9837 len * sizeof (compunit_symtab *));
43f3e411 9838 cust->includes[len] = NULL;
95554aad 9839
95554aad 9840 htab_delete (all_children);
ec94af83 9841 htab_delete (all_type_symtabs);
95554aad
TT
9842 }
9843}
9844
9845/* Compute the 'includes' field for the symtabs of all the CUs we just
9846 read. */
9847
9848static void
976ca316 9849process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9850{
976ca316 9851 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9852 {
9853 if (! iter->is_debug_types)
976ca316 9854 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9855 }
95554aad 9856
976ca316 9857 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9858}
9859
8fc0b21d 9860/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9861 already been loaded into memory. */
9862
9863static void
8fc0b21d 9864process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9865{
976ca316
SM
9866 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9867 struct objfile *objfile = per_objfile->objfile;
08feed99 9868 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9869 CORE_ADDR lowpc, highpc;
43f3e411 9870 struct compunit_symtab *cust;
10b3939b 9871 CORE_ADDR baseaddr;
4359dff1 9872 struct block *static_block;
3e29f34a 9873 CORE_ADDR addr;
10b3939b 9874
b3b3bada 9875 baseaddr = objfile->text_section_offset ();
10b3939b 9876
c89b44cd
TT
9877 /* Clear the list here in case something was left over. */
9878 cu->method_list.clear ();
10b3939b 9879
95554aad
TT
9880 cu->language = pretend_language;
9881 cu->language_defn = language_def (cu->language);
9882
c906108c 9883 /* Do line number decoding in read_file_scope () */
10b3939b 9884 process_die (cu->dies, cu);
c906108c 9885
a766d390
DE
9886 /* For now fudge the Go package. */
9887 if (cu->language == language_go)
9888 fixup_go_packaging (cu);
9889
5f48f8f3 9890 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9891 should be complete, and it should now be safe to compute all of the
9892 physnames. */
9893 compute_delayed_physnames (cu);
3da10d80 9894
c9317f21
TT
9895 if (cu->language == language_rust)
9896 rust_union_quirks (cu);
9897
fae299cd
DC
9898 /* Some compilers don't define a DW_AT_high_pc attribute for the
9899 compilation unit. If the DW_AT_high_pc is missing, synthesize
9900 it, by scanning the DIE's below the compilation unit. */
10b3939b 9901 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9902
3e29f34a 9903 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9904 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9905
9906 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9907 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9908 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9909 addrmap to help ensure it has an accurate map of pc values belonging to
9910 this comp unit. */
9911 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9912
c24bdb02 9913 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9914 SECT_OFF_TEXT (objfile),
9915 0);
c906108c 9916
43f3e411 9917 if (cust != NULL)
c906108c 9918 {
df15bd07 9919 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9920
8be455d7
JK
9921 /* Set symtab language to language from DW_AT_language. If the
9922 compilation is from a C file generated by language preprocessors, do
9923 not set the language if it was already deduced by start_subfile. */
43f3e411 9924 if (!(cu->language == language_c
40e3ad0e 9925 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9926 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9927
9928 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9929 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9930 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9931 there were bugs in prologue debug info, fixed later in GCC-4.5
9932 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9933
9934 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9935 needed, it would be wrong due to missing DW_AT_producer there.
9936
9937 Still one can confuse GDB by using non-standard GCC compilation
9938 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9939 */
ab260dad 9940 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9941 cust->locations_valid = 1;
e0d00bc7
JK
9942
9943 if (gcc_4_minor >= 5)
43f3e411 9944 cust->epilogue_unwind_valid = 1;
96408a79 9945
43f3e411 9946 cust->call_site_htab = cu->call_site_htab;
c906108c 9947 }
9291a0cd 9948
976ca316 9949 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9950
95554aad 9951 /* Push it for inclusion processing later. */
976ca316 9952 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9953
9954 /* Not needed any more. */
c24bdb02 9955 cu->reset_builder ();
f4dc4d17 9956}
45cfd468 9957
8fc0b21d 9958/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9959 already been loaded into memory. */
9960
9961static void
8fc0b21d 9962process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9963 enum language pretend_language)
9964{
976ca316
SM
9965 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9966 struct objfile *objfile = per_objfile->objfile;
43f3e411 9967 struct compunit_symtab *cust;
0186c6a7
DE
9968 struct signatured_type *sig_type;
9969
8fc0b21d
SM
9970 gdb_assert (cu->per_cu->is_debug_types);
9971 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9972
c89b44cd
TT
9973 /* Clear the list here in case something was left over. */
9974 cu->method_list.clear ();
f4dc4d17 9975
f4dc4d17
DE
9976 cu->language = pretend_language;
9977 cu->language_defn = language_def (cu->language);
9978
9979 /* The symbol tables are set up in read_type_unit_scope. */
9980 process_die (cu->dies, cu);
9981
9982 /* For now fudge the Go package. */
9983 if (cu->language == language_go)
9984 fixup_go_packaging (cu);
9985
5f48f8f3 9986 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9987 should be complete, and it should now be safe to compute all of the
9988 physnames. */
9989 compute_delayed_physnames (cu);
f4dc4d17 9990
c9317f21
TT
9991 if (cu->language == language_rust)
9992 rust_union_quirks (cu);
9993
f4dc4d17
DE
9994 /* TUs share symbol tables.
9995 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9996 of it with end_expandable_symtab. Otherwise, complete the addition of
9997 this TU's symbols to the existing symtab. */
8adb8487 9998 type_unit_group_unshareable *tug_unshare =
976ca316 9999 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 10000 if (tug_unshare->compunit_symtab == NULL)
45cfd468 10001 {
c24bdb02
KS
10002 buildsym_compunit *builder = cu->get_builder ();
10003 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 10004 tug_unshare->compunit_symtab = cust;
f4dc4d17 10005
43f3e411 10006 if (cust != NULL)
f4dc4d17
DE
10007 {
10008 /* Set symtab language to language from DW_AT_language. If the
10009 compilation is from a C file generated by language preprocessors,
10010 do not set the language if it was already deduced by
10011 start_subfile. */
43f3e411
DE
10012 if (!(cu->language == language_c
10013 && COMPUNIT_FILETABS (cust)->language != language_c))
10014 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10015 }
10016 }
10017 else
10018 {
c24bdb02 10019 cu->get_builder ()->augment_type_symtab ();
8adb8487 10020 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10021 }
10022
976ca316 10023 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10024
10025 /* Not needed any more. */
c24bdb02 10026 cu->reset_builder ();
c906108c
SS
10027}
10028
95554aad
TT
10029/* Process an imported unit DIE. */
10030
10031static void
10032process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10033{
10034 struct attribute *attr;
10035
f4dc4d17
DE
10036 /* For now we don't handle imported units in type units. */
10037 if (cu->per_cu->is_debug_types)
10038 {
10039 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10040 " supported in type units [in module %s]"),
5e22e966 10041 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10042 }
10043
95554aad
TT
10044 attr = dwarf2_attr (die, DW_AT_import, cu);
10045 if (attr != NULL)
10046 {
0826b30a 10047 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10048 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10049 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10050 dwarf2_per_cu_data *per_cu
ab432490 10051 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10052
58990295
TV
10053 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10054 into another compilation unit, at root level. Regard this as a hint,
10055 and ignore it. */
10056 if (die->parent && die->parent->parent == NULL
10057 && per_cu->unit_type == DW_UT_compile
10058 && per_cu->lang == language_cplus)
10059 return;
10060
69d751e3 10061 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10062 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 10063 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 10064
ae640021 10065 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10066 }
10067}
10068
4c8aa72d
PA
10069/* RAII object that represents a process_die scope: i.e.,
10070 starts/finishes processing a DIE. */
10071class process_die_scope
adde2bff 10072{
4c8aa72d
PA
10073public:
10074 process_die_scope (die_info *die, dwarf2_cu *cu)
10075 : m_die (die), m_cu (cu)
10076 {
10077 /* We should only be processing DIEs not already in process. */
10078 gdb_assert (!m_die->in_process);
10079 m_die->in_process = true;
10080 }
8c3cb9fa 10081
4c8aa72d
PA
10082 ~process_die_scope ()
10083 {
10084 m_die->in_process = false;
10085
10086 /* If we're done processing the DIE for the CU that owns the line
10087 header, we don't need the line header anymore. */
10088 if (m_cu->line_header_die_owner == m_die)
10089 {
10090 delete m_cu->line_header;
10091 m_cu->line_header = NULL;
10092 m_cu->line_header_die_owner = NULL;
10093 }
10094 }
10095
10096private:
10097 die_info *m_die;
10098 dwarf2_cu *m_cu;
10099};
adde2bff 10100
c906108c
SS
10101/* Process a die and its children. */
10102
10103static void
e7c27a73 10104process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10105{
4c8aa72d 10106 process_die_scope scope (die, cu);
adde2bff 10107
c906108c
SS
10108 switch (die->tag)
10109 {
10110 case DW_TAG_padding:
10111 break;
10112 case DW_TAG_compile_unit:
95554aad 10113 case DW_TAG_partial_unit:
e7c27a73 10114 read_file_scope (die, cu);
c906108c 10115 break;
348e048f
DE
10116 case DW_TAG_type_unit:
10117 read_type_unit_scope (die, cu);
10118 break;
c906108c 10119 case DW_TAG_subprogram:
0a4b0913
AB
10120 /* Nested subprograms in Fortran get a prefix. */
10121 if (cu->language == language_fortran
10122 && die->parent != NULL
10123 && die->parent->tag == DW_TAG_subprogram)
10124 cu->processing_has_namespace_info = true;
10125 /* Fall through. */
c906108c 10126 case DW_TAG_inlined_subroutine:
edb3359d 10127 read_func_scope (die, cu);
c906108c
SS
10128 break;
10129 case DW_TAG_lexical_block:
14898363
L
10130 case DW_TAG_try_block:
10131 case DW_TAG_catch_block:
e7c27a73 10132 read_lexical_block_scope (die, cu);
c906108c 10133 break;
216f72a1 10134 case DW_TAG_call_site:
96408a79
SA
10135 case DW_TAG_GNU_call_site:
10136 read_call_site_scope (die, cu);
10137 break;
c906108c 10138 case DW_TAG_class_type:
680b30c7 10139 case DW_TAG_interface_type:
c906108c
SS
10140 case DW_TAG_structure_type:
10141 case DW_TAG_union_type:
134d01f1 10142 process_structure_scope (die, cu);
c906108c
SS
10143 break;
10144 case DW_TAG_enumeration_type:
134d01f1 10145 process_enumeration_scope (die, cu);
c906108c 10146 break;
134d01f1 10147
f792889a
DJ
10148 /* These dies have a type, but processing them does not create
10149 a symbol or recurse to process the children. Therefore we can
10150 read them on-demand through read_type_die. */
c906108c 10151 case DW_TAG_subroutine_type:
72019c9c 10152 case DW_TAG_set_type:
c906108c 10153 case DW_TAG_array_type:
c906108c 10154 case DW_TAG_pointer_type:
c906108c 10155 case DW_TAG_ptr_to_member_type:
c906108c 10156 case DW_TAG_reference_type:
4297a3f0 10157 case DW_TAG_rvalue_reference_type:
c906108c 10158 case DW_TAG_string_type:
c906108c 10159 break;
134d01f1 10160
c906108c 10161 case DW_TAG_base_type:
a02abb62 10162 case DW_TAG_subrange_type:
cb249c71 10163 case DW_TAG_typedef:
134d01f1
DJ
10164 /* Add a typedef symbol for the type definition, if it has a
10165 DW_AT_name. */
f792889a 10166 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10167 break;
c906108c 10168 case DW_TAG_common_block:
e7c27a73 10169 read_common_block (die, cu);
c906108c
SS
10170 break;
10171 case DW_TAG_common_inclusion:
10172 break;
d9fa45fe 10173 case DW_TAG_namespace:
9068261f 10174 cu->processing_has_namespace_info = true;
e7c27a73 10175 read_namespace (die, cu);
d9fa45fe 10176 break;
5d7cb8df 10177 case DW_TAG_module:
9068261f 10178 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10179 read_module (die, cu);
10180 break;
d9fa45fe 10181 case DW_TAG_imported_declaration:
9068261f 10182 cu->processing_has_namespace_info = true;
74921315
KS
10183 if (read_namespace_alias (die, cu))
10184 break;
86a73007
TT
10185 /* The declaration is not a global namespace alias. */
10186 /* Fall through. */
d9fa45fe 10187 case DW_TAG_imported_module:
9068261f 10188 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10189 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10190 || cu->language != language_fortran))
b98664d3 10191 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10192 dwarf_tag_name (die->tag));
10193 read_import_statement (die, cu);
d9fa45fe 10194 break;
95554aad
TT
10195
10196 case DW_TAG_imported_unit:
10197 process_imported_unit_die (die, cu);
10198 break;
10199
71a3c369
TT
10200 case DW_TAG_variable:
10201 read_variable (die, cu);
10202 break;
10203
c906108c 10204 default:
e7c27a73 10205 new_symbol (die, NULL, cu);
c906108c
SS
10206 break;
10207 }
10208}
ca69b9e6
DE
10209\f
10210/* DWARF name computation. */
c906108c 10211
94af9270
KS
10212/* A helper function for dwarf2_compute_name which determines whether DIE
10213 needs to have the name of the scope prepended to the name listed in the
10214 die. */
10215
10216static int
10217die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10218{
1c809c68
TT
10219 struct attribute *attr;
10220
94af9270
KS
10221 switch (die->tag)
10222 {
10223 case DW_TAG_namespace:
10224 case DW_TAG_typedef:
10225 case DW_TAG_class_type:
10226 case DW_TAG_interface_type:
10227 case DW_TAG_structure_type:
10228 case DW_TAG_union_type:
10229 case DW_TAG_enumeration_type:
10230 case DW_TAG_enumerator:
10231 case DW_TAG_subprogram:
08a76f8a 10232 case DW_TAG_inlined_subroutine:
94af9270 10233 case DW_TAG_member:
74921315 10234 case DW_TAG_imported_declaration:
94af9270
KS
10235 return 1;
10236
10237 case DW_TAG_variable:
c2b0a229 10238 case DW_TAG_constant:
94af9270
KS
10239 /* We only need to prefix "globally" visible variables. These include
10240 any variable marked with DW_AT_external or any variable that
10241 lives in a namespace. [Variables in anonymous namespaces
10242 require prefixing, but they are not DW_AT_external.] */
10243
10244 if (dwarf2_attr (die, DW_AT_specification, cu))
10245 {
10246 struct dwarf2_cu *spec_cu = cu;
9a619af0 10247
94af9270
KS
10248 return die_needs_namespace (die_specification (die, &spec_cu),
10249 spec_cu);
10250 }
10251
1c809c68 10252 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10253 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10254 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10255 return 0;
10256 /* A variable in a lexical block of some kind does not need a
10257 namespace, even though in C++ such variables may be external
10258 and have a mangled name. */
10259 if (die->parent->tag == DW_TAG_lexical_block
10260 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10261 || die->parent->tag == DW_TAG_catch_block
10262 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10263 return 0;
10264 return 1;
94af9270
KS
10265
10266 default:
10267 return 0;
10268 }
10269}
10270
73b9be8b
KS
10271/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10272 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10273 defined for the given DIE. */
10274
10275static struct attribute *
10276dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10277{
10278 struct attribute *attr;
10279
10280 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10281 if (attr == NULL)
10282 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10283
10284 return attr;
10285}
10286
10287/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10288 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10289 defined for the given DIE. */
10290
10291static const char *
10292dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10293{
10294 const char *linkage_name;
10295
10296 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10297 if (linkage_name == NULL)
10298 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10299
787de330
TT
10300 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10301 See https://github.com/rust-lang/rust/issues/32925. */
10302 if (cu->language == language_rust && linkage_name != NULL
10303 && strchr (linkage_name, '{') != NULL)
10304 linkage_name = NULL;
10305
73b9be8b
KS
10306 return linkage_name;
10307}
10308
94af9270 10309/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10310 compute the physname for the object, which include a method's:
9c37b5ae 10311 - formal parameters (C++),
a766d390 10312 - receiver type (Go),
a766d390
DE
10313
10314 The term "physname" is a bit confusing.
10315 For C++, for example, it is the demangled name.
10316 For Go, for example, it's the mangled name.
94af9270 10317
af6b7be1
JB
10318 For Ada, return the DIE's linkage name rather than the fully qualified
10319 name. PHYSNAME is ignored..
10320
5989a64e 10321 The result is allocated on the objfile->per_bfd's obstack and
45940949 10322 canonicalized. */
94af9270
KS
10323
10324static const char *
15d034d0
TT
10325dwarf2_compute_name (const char *name,
10326 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10327 int physname)
10328{
5e22e966 10329 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10330
94af9270
KS
10331 if (name == NULL)
10332 name = dwarf2_name (die, cu);
10333
2ee7123e
DE
10334 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10335 but otherwise compute it by typename_concat inside GDB.
10336 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10337 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10338 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10339 will set the demangled name to the result of dwarf2_full_name, and it is
10340 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10341 if (cu->language == language_ada
10342 || (cu->language == language_fortran && physname))
10343 {
10344 /* For Ada unit, we prefer the linkage name over the name, as
10345 the former contains the exported name, which the user expects
10346 to be able to reference. Ideally, we want the user to be able
10347 to reference this entity using either natural or linkage name,
10348 but we haven't started looking at this enhancement yet. */
73b9be8b 10349 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10350
2ee7123e
DE
10351 if (linkage_name != NULL)
10352 return linkage_name;
f55ee35c
JK
10353 }
10354
94af9270
KS
10355 /* These are the only languages we know how to qualify names in. */
10356 if (name != NULL
9c37b5ae 10357 && (cu->language == language_cplus
c44af4eb
TT
10358 || cu->language == language_fortran || cu->language == language_d
10359 || cu->language == language_rust))
94af9270
KS
10360 {
10361 if (die_needs_namespace (die, cu))
10362 {
0d5cff50 10363 const char *prefix;
34a68019 10364 const char *canonical_name = NULL;
94af9270 10365
d7e74731
PA
10366 string_file buf;
10367
94af9270 10368 prefix = determine_prefix (die, cu);
94af9270
KS
10369 if (*prefix != '\0')
10370 {
43816ebc
TT
10371 gdb::unique_xmalloc_ptr<char> prefixed_name
10372 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10373
43816ebc 10374 buf.puts (prefixed_name.get ());
94af9270
KS
10375 }
10376 else
d7e74731 10377 buf.puts (name);
94af9270 10378
98bfdba5
PA
10379 /* Template parameters may be specified in the DIE's DW_AT_name, or
10380 as children with DW_TAG_template_type_param or
10381 DW_TAG_value_type_param. If the latter, add them to the name
10382 here. If the name already has template parameters, then
10383 skip this step; some versions of GCC emit both, and
10384 it is more efficient to use the pre-computed name.
10385
10386 Something to keep in mind about this process: it is very
10387 unlikely, or in some cases downright impossible, to produce
10388 something that will match the mangled name of a function.
10389 If the definition of the function has the same debug info,
10390 we should be able to match up with it anyway. But fallbacks
10391 using the minimal symbol, for instance to find a method
10392 implemented in a stripped copy of libstdc++, will not work.
10393 If we do not have debug info for the definition, we will have to
10394 match them up some other way.
10395
10396 When we do name matching there is a related problem with function
10397 templates; two instantiated function templates are allowed to
10398 differ only by their return types, which we do not add here. */
10399
10400 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10401 {
10402 struct attribute *attr;
10403 struct die_info *child;
10404 int first = 1;
10405
10406 die->building_fullname = 1;
10407
10408 for (child = die->child; child != NULL; child = child->sibling)
10409 {
10410 struct type *type;
12df843f 10411 LONGEST value;
d521ce57 10412 const gdb_byte *bytes;
98bfdba5
PA
10413 struct dwarf2_locexpr_baton *baton;
10414 struct value *v;
10415
10416 if (child->tag != DW_TAG_template_type_param
10417 && child->tag != DW_TAG_template_value_param)
10418 continue;
10419
10420 if (first)
10421 {
d7e74731 10422 buf.puts ("<");
98bfdba5
PA
10423 first = 0;
10424 }
10425 else
d7e74731 10426 buf.puts (", ");
98bfdba5
PA
10427
10428 attr = dwarf2_attr (child, DW_AT_type, cu);
10429 if (attr == NULL)
10430 {
b98664d3 10431 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10432 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10433 continue;
10434 }
10435 type = die_type (child, cu);
10436
10437 if (child->tag == DW_TAG_template_type_param)
10438 {
c1ec8cea
TT
10439 c_print_type (type, "", &buf, -1, 0, cu->language,
10440 &type_print_raw_options);
98bfdba5
PA
10441 continue;
10442 }
10443
10444 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10445 if (attr == NULL)
10446 {
b98664d3 10447 complaint (_("template parameter missing "
3e43a32a 10448 "DW_AT_const_value"));
d7e74731 10449 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10450 continue;
10451 }
10452
10453 dwarf2_const_value_attr (attr, type, name,
10454 &cu->comp_unit_obstack, cu,
10455 &value, &bytes, &baton);
10456
10457 if (TYPE_NOSIGN (type))
10458 /* GDB prints characters as NUMBER 'CHAR'. If that's
10459 changed, this can use value_print instead. */
d7e74731 10460 c_printchar (value, type, &buf);
98bfdba5
PA
10461 else
10462 {
10463 struct value_print_options opts;
10464
10465 if (baton != NULL)
10466 v = dwarf2_evaluate_loc_desc (type, NULL,
10467 baton->data,
10468 baton->size,
9f47c707
SM
10469 baton->per_cu,
10470 baton->per_objfile);
98bfdba5
PA
10471 else if (bytes != NULL)
10472 {
10473 v = allocate_value (type);
10474 memcpy (value_contents_writeable (v), bytes,
10475 TYPE_LENGTH (type));
10476 }
10477 else
10478 v = value_from_longest (type, value);
10479
3e43a32a
MS
10480 /* Specify decimal so that we do not depend on
10481 the radix. */
98bfdba5
PA
10482 get_formatted_print_options (&opts, 'd');
10483 opts.raw = 1;
d7e74731 10484 value_print (v, &buf, &opts);
98bfdba5 10485 release_value (v);
98bfdba5
PA
10486 }
10487 }
10488
10489 die->building_fullname = 0;
10490
10491 if (!first)
10492 {
10493 /* Close the argument list, with a space if necessary
10494 (nested templates). */
d7e74731
PA
10495 if (!buf.empty () && buf.string ().back () == '>')
10496 buf.puts (" >");
98bfdba5 10497 else
d7e74731 10498 buf.puts (">");
98bfdba5
PA
10499 }
10500 }
10501
9c37b5ae 10502 /* For C++ methods, append formal parameter type
94af9270 10503 information, if PHYSNAME. */
6e70227d 10504
94af9270 10505 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10506 && cu->language == language_cplus)
94af9270
KS
10507 {
10508 struct type *type = read_type_die (die, cu);
10509
d7e74731 10510 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10511 &type_print_raw_options);
94af9270 10512
9c37b5ae 10513 if (cu->language == language_cplus)
94af9270 10514 {
60430eff
DJ
10515 /* Assume that an artificial first parameter is
10516 "this", but do not crash if it is not. RealView
10517 marks unnamed (and thus unused) parameters as
10518 artificial; there is no way to differentiate
10519 the two cases. */
1f704f76 10520 if (type->num_fields () > 0
94af9270 10521 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10522 && type->field (0).type ()->code () == TYPE_CODE_PTR
10523 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10524 buf.puts (" const");
94af9270
KS
10525 }
10526 }
10527
d7e74731 10528 const std::string &intermediate_name = buf.string ();
94af9270
KS
10529
10530 if (cu->language == language_cplus)
34a68019 10531 canonical_name
322a8516 10532 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10533 objfile);
34a68019
TT
10534
10535 /* If we only computed INTERMEDIATE_NAME, or if
10536 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10537 intern it. */
322a8516 10538 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10539 name = objfile->intern (intermediate_name);
34a68019
TT
10540 else
10541 name = canonical_name;
94af9270
KS
10542 }
10543 }
10544
10545 return name;
10546}
10547
0114d602
DJ
10548/* Return the fully qualified name of DIE, based on its DW_AT_name.
10549 If scope qualifiers are appropriate they will be added. The result
34a68019 10550 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10551 not have a name. NAME may either be from a previous call to
10552 dwarf2_name or NULL.
10553
9c37b5ae 10554 The output string will be canonicalized (if C++). */
0114d602
DJ
10555
10556static const char *
15d034d0 10557dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10558{
94af9270
KS
10559 return dwarf2_compute_name (name, die, cu, 0);
10560}
0114d602 10561
94af9270
KS
10562/* Construct a physname for the given DIE in CU. NAME may either be
10563 from a previous call to dwarf2_name or NULL. The result will be
10564 allocated on the objfile_objstack or NULL if the DIE does not have a
10565 name.
0114d602 10566
9c37b5ae 10567 The output string will be canonicalized (if C++). */
0114d602 10568
94af9270 10569static const char *
15d034d0 10570dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10571{
5e22e966 10572 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10573 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10574 int need_copy = 1;
10575
10576 /* In this case dwarf2_compute_name is just a shortcut not building anything
10577 on its own. */
10578 if (!die_needs_namespace (die, cu))
10579 return dwarf2_compute_name (name, die, cu, 1);
10580
906bb4c5
TT
10581 if (cu->language != language_rust)
10582 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10583
10584 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10585 has computed. */
791afaa2 10586 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10587 if (mangled != NULL)
900e11f9 10588 {
900e11f9 10589
59cc4834
JB
10590 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10591 {
10592 /* Do nothing (do not demangle the symbol name). */
10593 }
10594 else if (cu->language == language_go)
a766d390 10595 {
5e2db402
TT
10596 /* This is a lie, but we already lie to the caller new_symbol.
10597 new_symbol assumes we return the mangled name.
a766d390 10598 This just undoes that lie until things are cleaned up. */
a766d390
DE
10599 }
10600 else
10601 {
0eb876f5
JB
10602 /* Use DMGL_RET_DROP for C++ template functions to suppress
10603 their return type. It is easier for GDB users to search
10604 for such functions as `name(params)' than `long name(params)'.
10605 In such case the minimal symbol names do not match the full
10606 symbol names but for template functions there is never a need
10607 to look up their definition from their declaration so
10608 the only disadvantage remains the minimal symbol variant
10609 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10610 demangled.reset (gdb_demangle (mangled,
10611 (DMGL_PARAMS | DMGL_ANSI
10612 | DMGL_RET_DROP)));
a766d390 10613 }
900e11f9 10614 if (demangled)
791afaa2 10615 canon = demangled.get ();
900e11f9
JK
10616 else
10617 {
10618 canon = mangled;
10619 need_copy = 0;
10620 }
10621 }
10622
10623 if (canon == NULL || check_physname)
10624 {
10625 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10626
10627 if (canon != NULL && strcmp (physname, canon) != 0)
10628 {
10629 /* It may not mean a bug in GDB. The compiler could also
10630 compute DW_AT_linkage_name incorrectly. But in such case
10631 GDB would need to be bug-to-bug compatible. */
10632
b98664d3 10633 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10634 "(from linkage <%s>) - DIE at %s [in module %s]"),
10635 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10636 objfile_name (objfile));
900e11f9
JK
10637
10638 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10639 is available here - over computed PHYSNAME. It is safer
10640 against both buggy GDB and buggy compilers. */
10641
10642 retval = canon;
10643 }
10644 else
10645 {
10646 retval = physname;
10647 need_copy = 0;
10648 }
10649 }
10650 else
10651 retval = canon;
10652
10653 if (need_copy)
be1e3d3e 10654 retval = objfile->intern (retval);
900e11f9 10655
900e11f9 10656 return retval;
0114d602
DJ
10657}
10658
74921315
KS
10659/* Inspect DIE in CU for a namespace alias. If one exists, record
10660 a new symbol for it.
10661
10662 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10663
10664static int
10665read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10666{
10667 struct attribute *attr;
10668
10669 /* If the die does not have a name, this is not a namespace
10670 alias. */
10671 attr = dwarf2_attr (die, DW_AT_name, cu);
10672 if (attr != NULL)
10673 {
10674 int num;
10675 struct die_info *d = die;
10676 struct dwarf2_cu *imported_cu = cu;
10677
10678 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10679 keep inspecting DIEs until we hit the underlying import. */
10680#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10681 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10682 {
10683 attr = dwarf2_attr (d, DW_AT_import, cu);
10684 if (attr == NULL)
10685 break;
10686
10687 d = follow_die_ref (d, attr, &imported_cu);
10688 if (d->tag != DW_TAG_imported_declaration)
10689 break;
10690 }
10691
10692 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10693 {
b98664d3 10694 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10695 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10696 return 0;
10697 }
10698
10699 if (attr != NULL)
10700 {
10701 struct type *type;
0826b30a 10702 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10703
aa66c379 10704 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10705 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10706 {
10707 /* This declaration is a global namespace alias. Add
10708 a symbol for it whose type is the aliased namespace. */
10709 new_symbol (die, type, cu);
10710 return 1;
10711 }
10712 }
10713 }
10714
10715 return 0;
10716}
10717
22cee43f 10718/* Return the using directives repository (global or local?) to use in the
804d2729 10719 current context for CU.
22cee43f
PMR
10720
10721 For Ada, imported declarations can materialize renamings, which *may* be
10722 global. However it is impossible (for now?) in DWARF to distinguish
10723 "external" imported declarations and "static" ones. As all imported
10724 declarations seem to be static in all other languages, make them all CU-wide
10725 global only in Ada. */
10726
10727static struct using_direct **
804d2729 10728using_directives (struct dwarf2_cu *cu)
22cee43f 10729{
c24bdb02
KS
10730 if (cu->language == language_ada
10731 && cu->get_builder ()->outermost_context_p ())
10732 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10733 else
c24bdb02 10734 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10735}
10736
27aa8d6a
SW
10737/* Read the import statement specified by the given die and record it. */
10738
10739static void
10740read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10741{
5e22e966 10742 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10743 struct attribute *import_attr;
32019081 10744 struct die_info *imported_die, *child_die;
de4affc9 10745 struct dwarf2_cu *imported_cu;
27aa8d6a 10746 const char *imported_name;
794684b6 10747 const char *imported_name_prefix;
13387711
SW
10748 const char *canonical_name;
10749 const char *import_alias;
10750 const char *imported_declaration = NULL;
794684b6 10751 const char *import_prefix;
eb1e02fd 10752 std::vector<const char *> excludes;
13387711 10753
27aa8d6a
SW
10754 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10755 if (import_attr == NULL)
10756 {
b98664d3 10757 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10758 dwarf_tag_name (die->tag));
10759 return;
10760 }
10761
de4affc9
CC
10762 imported_cu = cu;
10763 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10764 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10765 if (imported_name == NULL)
10766 {
10767 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10768
10769 The import in the following code:
10770 namespace A
10771 {
10772 typedef int B;
10773 }
10774
10775 int main ()
10776 {
10777 using A::B;
10778 B b;
10779 return b;
10780 }
10781
10782 ...
10783 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10784 <52> DW_AT_decl_file : 1
10785 <53> DW_AT_decl_line : 6
10786 <54> DW_AT_import : <0x75>
10787 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10788 <59> DW_AT_name : B
10789 <5b> DW_AT_decl_file : 1
10790 <5c> DW_AT_decl_line : 2
10791 <5d> DW_AT_type : <0x6e>
10792 ...
10793 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10794 <76> DW_AT_byte_size : 4
10795 <77> DW_AT_encoding : 5 (signed)
10796
10797 imports the wrong die ( 0x75 instead of 0x58 ).
10798 This case will be ignored until the gcc bug is fixed. */
10799 return;
10800 }
10801
82856980
SW
10802 /* Figure out the local name after import. */
10803 import_alias = dwarf2_name (die, cu);
27aa8d6a 10804
794684b6
SW
10805 /* Figure out where the statement is being imported to. */
10806 import_prefix = determine_prefix (die, cu);
10807
10808 /* Figure out what the scope of the imported die is and prepend it
10809 to the name of the imported die. */
de4affc9 10810 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10811
f55ee35c
JK
10812 if (imported_die->tag != DW_TAG_namespace
10813 && imported_die->tag != DW_TAG_module)
794684b6 10814 {
13387711
SW
10815 imported_declaration = imported_name;
10816 canonical_name = imported_name_prefix;
794684b6 10817 }
13387711 10818 else if (strlen (imported_name_prefix) > 0)
12aaed36 10819 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10820 imported_name_prefix,
10821 (cu->language == language_d ? "." : "::"),
10822 imported_name, (char *) NULL);
13387711
SW
10823 else
10824 canonical_name = imported_name;
794684b6 10825
32019081
JK
10826 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10827 for (child_die = die->child; child_die && child_die->tag;
436c571c 10828 child_die = child_die->sibling)
32019081
JK
10829 {
10830 /* DWARF-4: A Fortran use statement with a “rename list” may be
10831 represented by an imported module entry with an import attribute
10832 referring to the module and owned entries corresponding to those
10833 entities that are renamed as part of being imported. */
10834
10835 if (child_die->tag != DW_TAG_imported_declaration)
10836 {
b98664d3 10837 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10838 "- DIE at %s [in module %s]"),
10839 sect_offset_str (child_die->sect_off),
10840 objfile_name (objfile));
32019081
JK
10841 continue;
10842 }
10843
10844 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10845 if (import_attr == NULL)
10846 {
b98664d3 10847 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10848 dwarf_tag_name (child_die->tag));
10849 continue;
10850 }
10851
10852 imported_cu = cu;
10853 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10854 &imported_cu);
10855 imported_name = dwarf2_name (imported_die, imported_cu);
10856 if (imported_name == NULL)
10857 {
b98664d3 10858 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10859 "imported name - DIE at %s [in module %s]"),
10860 sect_offset_str (child_die->sect_off),
10861 objfile_name (objfile));
32019081
JK
10862 continue;
10863 }
10864
eb1e02fd 10865 excludes.push_back (imported_name);
32019081
JK
10866
10867 process_die (child_die, cu);
10868 }
10869
804d2729 10870 add_using_directive (using_directives (cu),
22cee43f
PMR
10871 import_prefix,
10872 canonical_name,
10873 import_alias,
10874 imported_declaration,
10875 excludes,
10876 0,
10877 &objfile->objfile_obstack);
27aa8d6a
SW
10878}
10879
5230b05a
WT
10880/* ICC<14 does not output the required DW_AT_declaration on incomplete
10881 types, but gives them a size of zero. Starting with version 14,
10882 ICC is compatible with GCC. */
10883
9068261f 10884static bool
5230b05a
WT
10885producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10886{
10887 if (!cu->checked_producer)
10888 check_producer (cu);
10889
10890 return cu->producer_is_icc_lt_14;
10891}
10892
eb77c9df
AB
10893/* ICC generates a DW_AT_type for C void functions. This was observed on
10894 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10895 which says that void functions should not have a DW_AT_type. */
10896
10897static bool
10898producer_is_icc (struct dwarf2_cu *cu)
10899{
10900 if (!cu->checked_producer)
10901 check_producer (cu);
10902
10903 return cu->producer_is_icc;
10904}
10905
1b80a9fa
JK
10906/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10907 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10908 this, it was first present in GCC release 4.3.0. */
10909
9068261f 10910static bool
1b80a9fa
JK
10911producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10912{
10913 if (!cu->checked_producer)
10914 check_producer (cu);
10915
10916 return cu->producer_is_gcc_lt_4_3;
10917}
10918
d721ba37
PA
10919static file_and_directory
10920find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10921{
d721ba37
PA
10922 file_and_directory res;
10923
9291a0cd
TT
10924 /* Find the filename. Do not use dwarf2_name here, since the filename
10925 is not a source language identifier. */
d721ba37
PA
10926 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10927 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10928
d721ba37
PA
10929 if (res.comp_dir == NULL
10930 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10931 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10932 {
d721ba37
PA
10933 res.comp_dir_storage = ldirname (res.name);
10934 if (!res.comp_dir_storage.empty ())
10935 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10936 }
d721ba37 10937 if (res.comp_dir != NULL)
9291a0cd
TT
10938 {
10939 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10940 directory, get rid of it. */
d721ba37 10941 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10942
d721ba37
PA
10943 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10944 res.comp_dir = cp + 1;
9291a0cd
TT
10945 }
10946
d721ba37
PA
10947 if (res.name == NULL)
10948 res.name = "<unknown>";
10949
10950 return res;
9291a0cd
TT
10951}
10952
f4dc4d17
DE
10953/* Handle DW_AT_stmt_list for a compilation unit.
10954 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10955 COMP_DIR is the compilation directory. LOWPC is passed to
10956 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10957
10958static void
10959handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10960 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10961{
976ca316 10962 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10963 struct attribute *attr;
527f3840
JK
10964 struct line_header line_header_local;
10965 hashval_t line_header_local_hash;
527f3840
JK
10966 void **slot;
10967 int decode_mapping;
2ab95328 10968
f4dc4d17
DE
10969 gdb_assert (! cu->per_cu->is_debug_types);
10970
2ab95328 10971 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10972 if (attr == NULL)
10973 return;
10974
9c541725 10975 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10976
10977 /* The line header hash table is only created if needed (it exists to
10978 prevent redundant reading of the line table for partial_units).
10979 If we're given a partial_unit, we'll need it. If we're given a
10980 compile_unit, then use the line header hash table if it's already
10981 created, but don't create one just yet. */
10982
976ca316 10983 if (per_objfile->line_header_hash == NULL
527f3840 10984 && die->tag == DW_TAG_partial_unit)
2ab95328 10985 {
976ca316 10986 per_objfile->line_header_hash
d15acc42
TT
10987 .reset (htab_create_alloc (127, line_header_hash_voidp,
10988 line_header_eq_voidp,
10989 free_line_header_voidp,
10990 xcalloc, xfree));
527f3840 10991 }
2ab95328 10992
9c541725 10993 line_header_local.sect_off = line_offset;
527f3840
JK
10994 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10995 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10996 if (per_objfile->line_header_hash != NULL)
527f3840 10997 {
976ca316 10998 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10999 &line_header_local,
11000 line_header_local_hash, NO_INSERT);
11001
11002 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11003 is not present in *SLOT (since if there is something in *SLOT then
11004 it will be for a partial_unit). */
11005 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11006 {
527f3840 11007 gdb_assert (*slot != NULL);
9a3c8263 11008 cu->line_header = (struct line_header *) *slot;
527f3840 11009 return;
dee91e82 11010 }
2ab95328 11011 }
527f3840
JK
11012
11013 /* dwarf_decode_line_header does not yet provide sufficient information.
11014 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11015 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11016 if (lh == NULL)
527f3840 11017 return;
4c8aa72d
PA
11018
11019 cu->line_header = lh.release ();
11020 cu->line_header_die_owner = die;
527f3840 11021
976ca316 11022 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11023 slot = NULL;
11024 else
11025 {
976ca316 11026 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11027 &line_header_local,
11028 line_header_local_hash, INSERT);
11029 gdb_assert (slot != NULL);
11030 }
11031 if (slot != NULL && *slot == NULL)
11032 {
11033 /* This newly decoded line number information unit will be owned
11034 by line_header_hash hash table. */
11035 *slot = cu->line_header;
4c8aa72d 11036 cu->line_header_die_owner = NULL;
527f3840
JK
11037 }
11038 else
11039 {
11040 /* We cannot free any current entry in (*slot) as that struct line_header
11041 may be already used by multiple CUs. Create only temporary decoded
11042 line_header for this CU - it may happen at most once for each line
11043 number information unit. And if we're not using line_header_hash
11044 then this is what we want as well. */
11045 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11046 }
11047 decode_mapping = (die->tag != DW_TAG_partial_unit);
11048 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11049 decode_mapping);
fff8551c 11050
2ab95328
TT
11051}
11052
95554aad 11053/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11054
c906108c 11055static void
e7c27a73 11056read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11057{
976ca316
SM
11058 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11059 struct objfile *objfile = per_objfile->objfile;
08feed99 11060 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11061 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11062 CORE_ADDR highpc = ((CORE_ADDR) 0);
11063 struct attribute *attr;
c906108c 11064 struct die_info *child_die;
e142c38c 11065 CORE_ADDR baseaddr;
6e70227d 11066
380618d6 11067 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11068 baseaddr = objfile->text_section_offset ();
c906108c 11069
fae299cd 11070 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11071
11072 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11073 from finish_block. */
2acceee2 11074 if (lowpc == ((CORE_ADDR) -1))
c906108c 11075 lowpc = highpc;
3e29f34a 11076 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11077
d721ba37 11078 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11079
f4b8a18d
KW
11080 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11081 standardised yet. As a workaround for the language detection we fall
11082 back to the DW_AT_producer string. */
11083 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11084 cu->language = language_opencl;
11085
3019eac3
DE
11086 /* Similar hack for Go. */
11087 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11088 set_cu_language (DW_LANG_Go, cu);
11089
c24bdb02 11090 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11091
11092 /* Decode line number information if present. We do this before
11093 processing child DIEs, so that the line header table is available
11094 for DW_AT_decl_file. */
d721ba37 11095 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11096
11097 /* Process all dies in compilation unit. */
11098 if (die->child != NULL)
11099 {
11100 child_die = die->child;
11101 while (child_die && child_die->tag)
11102 {
11103 process_die (child_die, cu);
436c571c 11104 child_die = child_die->sibling;
3019eac3
DE
11105 }
11106 }
11107
11108 /* Decode macro information, if present. Dwarf 2 macro information
11109 refers to information in the line number info statement program
11110 header, so we can only read it if we've read the header
11111 successfully. */
0af92d60
JK
11112 attr = dwarf2_attr (die, DW_AT_macros, cu);
11113 if (attr == NULL)
11114 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11115 if (attr && cu->line_header)
11116 {
11117 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11118 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11119
43f3e411 11120 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11121 }
11122 else
11123 {
11124 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11125 if (attr && cu->line_header)
11126 {
11127 unsigned int macro_offset = DW_UNSND (attr);
11128
43f3e411 11129 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11130 }
11131 }
3019eac3
DE
11132}
11133
c24bdb02
KS
11134void
11135dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11136{
f4dc4d17
DE
11137 struct type_unit_group *tu_group;
11138 int first_time;
3019eac3 11139 struct attribute *attr;
9c541725 11140 unsigned int i;
0186c6a7 11141 struct signatured_type *sig_type;
3019eac3 11142
f4dc4d17 11143 gdb_assert (per_cu->is_debug_types);
0186c6a7 11144 sig_type = (struct signatured_type *) per_cu;
3019eac3 11145
c24bdb02 11146 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11147
f4dc4d17 11148 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11149 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11150 if (sig_type->type_unit_group == NULL)
c24bdb02 11151 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11152 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11153
11154 /* If we've already processed this stmt_list there's no real need to
11155 do it again, we could fake it and just recreate the part we need
11156 (file name,index -> symtab mapping). If data shows this optimization
11157 is useful we can do it then. */
8adb8487
TT
11158 type_unit_group_unshareable *tug_unshare
11159 = per_objfile->get_type_unit_group_unshareable (tu_group);
11160 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11161
11162 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11163 debug info. */
fff8551c 11164 line_header_up lh;
f4dc4d17 11165 if (attr != NULL)
3019eac3 11166 {
9c541725 11167 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11168 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11169 }
11170 if (lh == NULL)
11171 {
11172 if (first_time)
c24bdb02 11173 start_symtab ("", NULL, 0);
f4dc4d17
DE
11174 else
11175 {
8adb8487 11176 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11177 gdb_assert (m_builder == nullptr);
8adb8487 11178 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11179 m_builder.reset (new struct buildsym_compunit
11180 (COMPUNIT_OBJFILE (cust), "",
11181 COMPUNIT_DIRNAME (cust),
11182 compunit_language (cust),
11183 0, cust));
770479f2 11184 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11185 }
f4dc4d17 11186 return;
3019eac3
DE
11187 }
11188
c24bdb02
KS
11189 line_header = lh.release ();
11190 line_header_die_owner = die;
3019eac3 11191
f4dc4d17
DE
11192 if (first_time)
11193 {
c24bdb02 11194 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11195
1fd60fc0
DE
11196 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11197 still initializing it, and our caller (a few levels up)
11198 process_full_type_unit still needs to know if this is the first
11199 time. */
11200
8adb8487 11201 tug_unshare->symtabs
4ac93832
TT
11202 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11203 struct symtab *, line_header->file_names_size ());
3019eac3 11204
7ba99d21
AT
11205 auto &file_names = line_header->file_names ();
11206 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11207 {
7ba99d21 11208 file_entry &fe = file_names[i];
c24bdb02
KS
11209 dwarf2_start_subfile (this, fe.name,
11210 fe.include_dir (line_header));
11211 buildsym_compunit *b = get_builder ();
11212 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11213 {
4c8aa72d
PA
11214 /* NOTE: start_subfile will recognize when it's been
11215 passed a file it has already seen. So we can't
11216 assume there's a simple mapping from
11217 cu->line_header->file_names to subfiles, plus
11218 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11219 b->get_current_subfile ()->symtab
11220 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11221 }
11222
c24bdb02 11223 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11224 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11225 }
11226 }
11227 else
3019eac3 11228 {
c24bdb02 11229 gdb_assert (m_builder == nullptr);
8adb8487 11230 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11231 m_builder.reset (new struct buildsym_compunit
11232 (COMPUNIT_OBJFILE (cust), "",
11233 COMPUNIT_DIRNAME (cust),
11234 compunit_language (cust),
11235 0, cust));
770479f2 11236 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11237
7ba99d21
AT
11238 auto &file_names = line_header->file_names ();
11239 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11240 {
7ba99d21 11241 file_entry &fe = file_names[i];
8adb8487 11242 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11243 }
3019eac3
DE
11244 }
11245
f4dc4d17
DE
11246 /* The main symtab is allocated last. Type units don't have DW_AT_name
11247 so they don't have a "real" (so to speak) symtab anyway.
11248 There is later code that will assign the main symtab to all symbols
11249 that don't have one. We need to handle the case of a symbol with a
11250 missing symtab (DW_AT_decl_file) anyway. */
11251}
3019eac3 11252
f4dc4d17
DE
11253/* Process DW_TAG_type_unit.
11254 For TUs we want to skip the first top level sibling if it's not the
11255 actual type being defined by this TU. In this case the first top
11256 level sibling is there to provide context only. */
3019eac3 11257
f4dc4d17
DE
11258static void
11259read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11260{
11261 struct die_info *child_die;
3019eac3 11262
f4dc4d17
DE
11263 prepare_one_comp_unit (cu, die, language_minimal);
11264
11265 /* Initialize (or reinitialize) the machinery for building symtabs.
11266 We do this before processing child DIEs, so that the line header table
11267 is available for DW_AT_decl_file. */
c24bdb02 11268 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11269
11270 if (die->child != NULL)
11271 {
11272 child_die = die->child;
11273 while (child_die && child_die->tag)
11274 {
11275 process_die (child_die, cu);
436c571c 11276 child_die = child_die->sibling;
f4dc4d17
DE
11277 }
11278 }
3019eac3
DE
11279}
11280\f
80626a55
DE
11281/* DWO/DWP files.
11282
11283 http://gcc.gnu.org/wiki/DebugFission
11284 http://gcc.gnu.org/wiki/DebugFissionDWP
11285
11286 To simplify handling of both DWO files ("object" files with the DWARF info)
11287 and DWP files (a file with the DWOs packaged up into one file), we treat
11288 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11289
11290static hashval_t
11291hash_dwo_file (const void *item)
11292{
9a3c8263 11293 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11294 hashval_t hash;
3019eac3 11295
a2ce51a0
DE
11296 hash = htab_hash_string (dwo_file->dwo_name);
11297 if (dwo_file->comp_dir != NULL)
11298 hash += htab_hash_string (dwo_file->comp_dir);
11299 return hash;
3019eac3
DE
11300}
11301
11302static int
11303eq_dwo_file (const void *item_lhs, const void *item_rhs)
11304{
9a3c8263
SM
11305 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11306 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11307
a2ce51a0
DE
11308 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11309 return 0;
11310 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11311 return lhs->comp_dir == rhs->comp_dir;
11312 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11313}
11314
11315/* Allocate a hash table for DWO files. */
11316
51ac9db5 11317static htab_up
298e9637 11318allocate_dwo_file_hash_table ()
3019eac3 11319{
51ac9db5
SM
11320 auto delete_dwo_file = [] (void *item)
11321 {
11322 struct dwo_file *dwo_file = (struct dwo_file *) item;
11323
11324 delete dwo_file;
11325 };
11326
bc68fb19
TT
11327 return htab_up (htab_create_alloc (41,
11328 hash_dwo_file,
11329 eq_dwo_file,
11330 delete_dwo_file,
11331 xcalloc, xfree));
3019eac3
DE
11332}
11333
80626a55
DE
11334/* Lookup DWO file DWO_NAME. */
11335
11336static void **
976ca316 11337lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11338 const char *dwo_name,
11339 const char *comp_dir)
80626a55
DE
11340{
11341 struct dwo_file find_entry;
11342 void **slot;
11343
976ca316
SM
11344 if (per_objfile->per_bfd->dwo_files == NULL)
11345 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11346
0ac5b59e
DE
11347 find_entry.dwo_name = dwo_name;
11348 find_entry.comp_dir = comp_dir;
976ca316 11349 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11350 INSERT);
80626a55
DE
11351
11352 return slot;
11353}
11354
3019eac3
DE
11355static hashval_t
11356hash_dwo_unit (const void *item)
11357{
9a3c8263 11358 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11359
11360 /* This drops the top 32 bits of the id, but is ok for a hash. */
11361 return dwo_unit->signature;
11362}
11363
11364static int
11365eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11366{
9a3c8263
SM
11367 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11368 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11369
11370 /* The signature is assumed to be unique within the DWO file.
11371 So while object file CU dwo_id's always have the value zero,
11372 that's OK, assuming each object file DWO file has only one CU,
11373 and that's the rule for now. */
11374 return lhs->signature == rhs->signature;
11375}
11376
11377/* Allocate a hash table for DWO CUs,TUs.
11378 There is one of these tables for each of CUs,TUs for each DWO file. */
11379
b0b6a987 11380static htab_up
298e9637 11381allocate_dwo_unit_table ()
3019eac3
DE
11382{
11383 /* Start out with a pretty small number.
11384 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11385 return htab_up (htab_create_alloc (3,
11386 hash_dwo_unit,
11387 eq_dwo_unit,
11388 NULL, xcalloc, xfree));
3019eac3
DE
11389}
11390
19c3d4c9 11391/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11392
11393static void
19c3d4c9
DE
11394create_dwo_cu_reader (const struct die_reader_specs *reader,
11395 const gdb_byte *info_ptr,
11396 struct die_info *comp_unit_die,
c0ab21c2
TT
11397 struct dwo_file *dwo_file,
11398 struct dwo_unit *dwo_unit)
3019eac3
DE
11399{
11400 struct dwarf2_cu *cu = reader->cu;
9c541725 11401 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11402 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11403
a084a2a6
AT
11404 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11405 if (!signature.has_value ())
3019eac3 11406 {
b98664d3 11407 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11408 " its dwo_id [in module %s]"),
9d8780f0 11409 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11410 return;
11411 }
11412
3019eac3 11413 dwo_unit->dwo_file = dwo_file;
a084a2a6 11414 dwo_unit->signature = *signature;
8a0459fd 11415 dwo_unit->section = section;
9c541725 11416 dwo_unit->sect_off = sect_off;
3019eac3
DE
11417 dwo_unit->length = cu->per_cu->length;
11418
b4f54984 11419 if (dwarf_read_debug)
9d8780f0
SM
11420 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11421 sect_offset_str (sect_off),
9c541725 11422 hex_string (dwo_unit->signature));
3019eac3
DE
11423}
11424
33c5cd75 11425/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11426 Note: This function processes DWO files only, not DWP files. */
3019eac3 11427
33c5cd75 11428static void
976ca316 11429create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11430 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11431 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11432{
976ca316
SM
11433 struct objfile *objfile = per_objfile->objfile;
11434 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11435 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11436
96b79293 11437 section.read (objfile);
33c5cd75 11438 info_ptr = section.buffer;
3019eac3
DE
11439
11440 if (info_ptr == NULL)
33c5cd75 11441 return;
3019eac3 11442
b4f54984 11443 if (dwarf_read_debug)
19c3d4c9
DE
11444 {
11445 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11446 section.get_name (),
11447 section.get_file_name ());
19c3d4c9 11448 }
3019eac3 11449
33c5cd75 11450 end_ptr = info_ptr + section.size;
3019eac3
DE
11451 while (info_ptr < end_ptr)
11452 {
11453 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11454 struct dwo_unit read_unit {};
33c5cd75
DB
11455 struct dwo_unit *dwo_unit;
11456 void **slot;
11457 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11458
11459 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11460 per_cu.per_bfd = per_bfd;
3019eac3 11461 per_cu.is_debug_types = 0;
33c5cd75
DB
11462 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11463 per_cu.section = &section;
11464
976ca316 11465 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11466 if (!reader.dummy_p)
11467 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11468 &dwo_file, &read_unit);
33c5cd75
DB
11469 info_ptr += per_cu.length;
11470
11471 // If the unit could not be parsed, skip it.
c0ab21c2 11472 if (read_unit.dwo_file == NULL)
33c5cd75 11473 continue;
3019eac3 11474
33c5cd75 11475 if (cus_htab == NULL)
298e9637 11476 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11477
1859c670 11478 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11479 struct dwo_unit);
c0ab21c2 11480 *dwo_unit = read_unit;
b0b6a987 11481 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11482 gdb_assert (slot != NULL);
11483 if (*slot != NULL)
19c3d4c9 11484 {
33c5cd75
DB
11485 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11486 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11487
b98664d3 11488 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11489 " the entry at offset %s, signature %s"),
11490 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11491 hex_string (dwo_unit->signature));
19c3d4c9 11492 }
33c5cd75 11493 *slot = (void *)dwo_unit;
3019eac3 11494 }
3019eac3
DE
11495}
11496
80626a55
DE
11497/* DWP file .debug_{cu,tu}_index section format:
11498 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11499
d2415c6c
DE
11500 DWP Version 1:
11501
80626a55
DE
11502 Both index sections have the same format, and serve to map a 64-bit
11503 signature to a set of section numbers. Each section begins with a header,
11504 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11505 indexes, and a pool of 32-bit section numbers. The index sections will be
11506 aligned at 8-byte boundaries in the file.
11507
d2415c6c
DE
11508 The index section header consists of:
11509
11510 V, 32 bit version number
11511 -, 32 bits unused
11512 N, 32 bit number of compilation units or type units in the index
11513 M, 32 bit number of slots in the hash table
80626a55 11514
d2415c6c 11515 Numbers are recorded using the byte order of the application binary.
80626a55 11516
d2415c6c
DE
11517 The hash table begins at offset 16 in the section, and consists of an array
11518 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11519 order of the application binary). Unused slots in the hash table are 0.
11520 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11521
d2415c6c
DE
11522 The parallel table begins immediately after the hash table
11523 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11524 array of 32-bit indexes (using the byte order of the application binary),
11525 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11526 table contains a 32-bit index into the pool of section numbers. For unused
11527 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11528
73869dc2
DE
11529 The pool of section numbers begins immediately following the hash table
11530 (at offset 16 + 12 * M from the beginning of the section). The pool of
11531 section numbers consists of an array of 32-bit words (using the byte order
11532 of the application binary). Each item in the array is indexed starting
11533 from 0. The hash table entry provides the index of the first section
11534 number in the set. Additional section numbers in the set follow, and the
11535 set is terminated by a 0 entry (section number 0 is not used in ELF).
11536
11537 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11538 section must be the first entry in the set, and the .debug_abbrev.dwo must
11539 be the second entry. Other members of the set may follow in any order.
11540
11541 ---
11542
11543 DWP Version 2:
11544
11545 DWP Version 2 combines all the .debug_info, etc. sections into one,
11546 and the entries in the index tables are now offsets into these sections.
11547 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11548 section.
11549
11550 Index Section Contents:
11551 Header
11552 Hash Table of Signatures dwp_hash_table.hash_table
11553 Parallel Table of Indices dwp_hash_table.unit_table
11554 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11555 Table of Section Sizes dwp_hash_table.v2.sizes
11556
11557 The index section header consists of:
11558
11559 V, 32 bit version number
11560 L, 32 bit number of columns in the table of section offsets
11561 N, 32 bit number of compilation units or type units in the index
11562 M, 32 bit number of slots in the hash table
11563
11564 Numbers are recorded using the byte order of the application binary.
11565
11566 The hash table has the same format as version 1.
11567 The parallel table of indices has the same format as version 1,
11568 except that the entries are origin-1 indices into the table of sections
11569 offsets and the table of section sizes.
11570
11571 The table of offsets begins immediately following the parallel table
11572 (at offset 16 + 12 * M from the beginning of the section). The table is
11573 a two-dimensional array of 32-bit words (using the byte order of the
11574 application binary), with L columns and N+1 rows, in row-major order.
11575 Each row in the array is indexed starting from 0. The first row provides
11576 a key to the remaining rows: each column in this row provides an identifier
11577 for a debug section, and the offsets in the same column of subsequent rows
11578 refer to that section. The section identifiers are:
11579
11580 DW_SECT_INFO 1 .debug_info.dwo
11581 DW_SECT_TYPES 2 .debug_types.dwo
11582 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11583 DW_SECT_LINE 4 .debug_line.dwo
11584 DW_SECT_LOC 5 .debug_loc.dwo
11585 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11586 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11587 DW_SECT_MACRO 8 .debug_macro.dwo
11588
11589 The offsets provided by the CU and TU index sections are the base offsets
11590 for the contributions made by each CU or TU to the corresponding section
11591 in the package file. Each CU and TU header contains an abbrev_offset
11592 field, used to find the abbreviations table for that CU or TU within the
11593 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11594 be interpreted as relative to the base offset given in the index section.
11595 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11596 should be interpreted as relative to the base offset for .debug_line.dwo,
11597 and offsets into other debug sections obtained from DWARF attributes should
11598 also be interpreted as relative to the corresponding base offset.
11599
11600 The table of sizes begins immediately following the table of offsets.
11601 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11602 with L columns and N rows, in row-major order. Each row in the array is
11603 indexed starting from 1 (row 0 is shared by the two tables).
11604
11605 ---
11606
11607 Hash table lookup is handled the same in version 1 and 2:
11608
11609 We assume that N and M will not exceed 2^32 - 1.
11610 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11611
d2415c6c
DE
11612 Given a 64-bit compilation unit signature or a type signature S, an entry
11613 in the hash table is located as follows:
80626a55 11614
d2415c6c
DE
11615 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11616 the low-order k bits all set to 1.
80626a55 11617
d2415c6c 11618 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11619
d2415c6c
DE
11620 3) If the hash table entry at index H matches the signature, use that
11621 entry. If the hash table entry at index H is unused (all zeroes),
11622 terminate the search: the signature is not present in the table.
80626a55 11623
d2415c6c 11624 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11625
d2415c6c 11626 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11627 to stop at an unused slot or find the match. */
80626a55
DE
11628
11629/* Create a hash table to map DWO IDs to their CU/TU entry in
11630 .debug_{info,types}.dwo in DWP_FILE.
11631 Returns NULL if there isn't one.
11632 Note: This function processes DWP files only, not DWO files. */
11633
11634static struct dwp_hash_table *
976ca316 11635create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11636 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11637{
976ca316 11638 struct objfile *objfile = per_objfile->objfile;
400174b1 11639 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11640 const gdb_byte *index_ptr, *index_end;
80626a55 11641 struct dwarf2_section_info *index;
73869dc2 11642 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11643 struct dwp_hash_table *htab;
11644
11645 if (is_debug_types)
11646 index = &dwp_file->sections.tu_index;
11647 else
11648 index = &dwp_file->sections.cu_index;
11649
96b79293 11650 if (index->empty ())
80626a55 11651 return NULL;
96b79293 11652 index->read (objfile);
80626a55
DE
11653
11654 index_ptr = index->buffer;
11655 index_end = index_ptr + index->size;
11656
11657 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11658 index_ptr += 4;
11659 if (version == 2)
11660 nr_columns = read_4_bytes (dbfd, index_ptr);
11661 else
11662 nr_columns = 0;
11663 index_ptr += 4;
80626a55
DE
11664 nr_units = read_4_bytes (dbfd, index_ptr);
11665 index_ptr += 4;
11666 nr_slots = read_4_bytes (dbfd, index_ptr);
11667 index_ptr += 4;
11668
73869dc2 11669 if (version != 1 && version != 2)
80626a55 11670 {
21aa081e 11671 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11672 " [in module %s]"),
21aa081e 11673 pulongest (version), dwp_file->name);
80626a55
DE
11674 }
11675 if (nr_slots != (nr_slots & -nr_slots))
11676 {
21aa081e 11677 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11678 " is not power of 2 [in module %s]"),
21aa081e 11679 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11680 }
11681
976ca316 11682 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11683 htab->version = version;
11684 htab->nr_columns = nr_columns;
80626a55
DE
11685 htab->nr_units = nr_units;
11686 htab->nr_slots = nr_slots;
11687 htab->hash_table = index_ptr;
11688 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11689
11690 /* Exit early if the table is empty. */
11691 if (nr_slots == 0 || nr_units == 0
11692 || (version == 2 && nr_columns == 0))
11693 {
11694 /* All must be zero. */
11695 if (nr_slots != 0 || nr_units != 0
11696 || (version == 2 && nr_columns != 0))
11697 {
b98664d3 11698 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11699 " all zero [in modules %s]"),
11700 dwp_file->name);
11701 }
11702 return htab;
11703 }
11704
11705 if (version == 1)
11706 {
11707 htab->section_pool.v1.indices =
11708 htab->unit_table + sizeof (uint32_t) * nr_slots;
11709 /* It's harder to decide whether the section is too small in v1.
11710 V1 is deprecated anyway so we punt. */
11711 }
11712 else
11713 {
11714 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11715 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11716 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11717 /* Reverse map for error checking. */
11718 int ids_seen[DW_SECT_MAX + 1];
11719 int i;
11720
11721 if (nr_columns < 2)
11722 {
11723 error (_("Dwarf Error: bad DWP hash table, too few columns"
11724 " in section table [in module %s]"),
11725 dwp_file->name);
11726 }
11727 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11728 {
11729 error (_("Dwarf Error: bad DWP hash table, too many columns"
11730 " in section table [in module %s]"),
11731 dwp_file->name);
11732 }
04fd5eed
GB
11733 memset (ids, 255, sizeof_ids);
11734 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11735 for (i = 0; i < nr_columns; ++i)
11736 {
11737 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11738
11739 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11740 {
11741 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11742 " in section table [in module %s]"),
11743 id, dwp_file->name);
11744 }
11745 if (ids_seen[id] != -1)
11746 {
11747 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11748 " id %d in section table [in module %s]"),
11749 id, dwp_file->name);
11750 }
11751 ids_seen[id] = i;
11752 ids[i] = id;
11753 }
11754 /* Must have exactly one info or types section. */
11755 if (((ids_seen[DW_SECT_INFO] != -1)
11756 + (ids_seen[DW_SECT_TYPES] != -1))
11757 != 1)
11758 {
11759 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11760 " DWO info/types section [in module %s]"),
11761 dwp_file->name);
11762 }
11763 /* Must have an abbrev section. */
11764 if (ids_seen[DW_SECT_ABBREV] == -1)
11765 {
11766 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11767 " section [in module %s]"),
11768 dwp_file->name);
11769 }
11770 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11771 htab->section_pool.v2.sizes =
11772 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11773 * nr_units * nr_columns);
11774 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11775 * nr_units * nr_columns))
11776 > index_end)
11777 {
11778 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11779 " [in module %s]"),
11780 dwp_file->name);
11781 }
11782 }
80626a55
DE
11783
11784 return htab;
11785}
11786
11787/* Update SECTIONS with the data from SECTP.
11788
11789 This function is like the other "locate" section routines that are
11790 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11791 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11792
11793 The result is non-zero for success, or zero if an error was found. */
11794
11795static int
73869dc2
DE
11796locate_v1_virtual_dwo_sections (asection *sectp,
11797 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11798{
11799 const struct dwop_section_names *names = &dwop_section_names;
11800
11801 if (section_is_p (sectp->name, &names->abbrev_dwo))
11802 {
11803 /* There can be only one. */
049412e3 11804 if (sections->abbrev.s.section != NULL)
80626a55 11805 return 0;
049412e3 11806 sections->abbrev.s.section = sectp;
fd361982 11807 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11808 }
11809 else if (section_is_p (sectp->name, &names->info_dwo)
11810 || section_is_p (sectp->name, &names->types_dwo))
11811 {
11812 /* There can be only one. */
049412e3 11813 if (sections->info_or_types.s.section != NULL)
80626a55 11814 return 0;
049412e3 11815 sections->info_or_types.s.section = sectp;
fd361982 11816 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11817 }
11818 else if (section_is_p (sectp->name, &names->line_dwo))
11819 {
11820 /* There can be only one. */
049412e3 11821 if (sections->line.s.section != NULL)
80626a55 11822 return 0;
049412e3 11823 sections->line.s.section = sectp;
fd361982 11824 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11825 }
11826 else if (section_is_p (sectp->name, &names->loc_dwo))
11827 {
11828 /* There can be only one. */
049412e3 11829 if (sections->loc.s.section != NULL)
80626a55 11830 return 0;
049412e3 11831 sections->loc.s.section = sectp;
fd361982 11832 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11833 }
11834 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11835 {
11836 /* There can be only one. */
049412e3 11837 if (sections->macinfo.s.section != NULL)
80626a55 11838 return 0;
049412e3 11839 sections->macinfo.s.section = sectp;
fd361982 11840 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11841 }
11842 else if (section_is_p (sectp->name, &names->macro_dwo))
11843 {
11844 /* There can be only one. */
049412e3 11845 if (sections->macro.s.section != NULL)
80626a55 11846 return 0;
049412e3 11847 sections->macro.s.section = sectp;
fd361982 11848 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11849 }
11850 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11851 {
11852 /* There can be only one. */
049412e3 11853 if (sections->str_offsets.s.section != NULL)
80626a55 11854 return 0;
049412e3 11855 sections->str_offsets.s.section = sectp;
fd361982 11856 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11857 }
11858 else
11859 {
11860 /* No other kind of section is valid. */
11861 return 0;
11862 }
11863
11864 return 1;
11865}
11866
73869dc2
DE
11867/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11868 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11869 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11870 This is for DWP version 1 files. */
80626a55
DE
11871
11872static struct dwo_unit *
976ca316 11873create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11874 struct dwp_file *dwp_file,
73869dc2
DE
11875 uint32_t unit_index,
11876 const char *comp_dir,
11877 ULONGEST signature, int is_debug_types)
80626a55 11878{
73869dc2
DE
11879 const struct dwp_hash_table *dwp_htab =
11880 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11881 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11882 const char *kind = is_debug_types ? "TU" : "CU";
11883 struct dwo_file *dwo_file;
11884 struct dwo_unit *dwo_unit;
73869dc2 11885 struct virtual_v1_dwo_sections sections;
80626a55 11886 void **dwo_file_slot;
80626a55
DE
11887 int i;
11888
73869dc2
DE
11889 gdb_assert (dwp_file->version == 1);
11890
b4f54984 11891 if (dwarf_read_debug)
80626a55 11892 {
73869dc2 11893 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11894 kind,
73869dc2 11895 pulongest (unit_index), hex_string (signature),
80626a55
DE
11896 dwp_file->name);
11897 }
11898
19ac8c2e 11899 /* Fetch the sections of this DWO unit.
80626a55
DE
11900 Put a limit on the number of sections we look for so that bad data
11901 doesn't cause us to loop forever. */
11902
73869dc2 11903#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11904 (1 /* .debug_info or .debug_types */ \
11905 + 1 /* .debug_abbrev */ \
11906 + 1 /* .debug_line */ \
11907 + 1 /* .debug_loc */ \
11908 + 1 /* .debug_str_offsets */ \
19ac8c2e 11909 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11910 + 1 /* trailing zero */)
11911
11912 memset (&sections, 0, sizeof (sections));
80626a55 11913
73869dc2 11914 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11915 {
11916 asection *sectp;
11917 uint32_t section_nr =
11918 read_4_bytes (dbfd,
73869dc2
DE
11919 dwp_htab->section_pool.v1.indices
11920 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11921
11922 if (section_nr == 0)
11923 break;
11924 if (section_nr >= dwp_file->num_sections)
11925 {
11926 error (_("Dwarf Error: bad DWP hash table, section number too large"
11927 " [in module %s]"),
11928 dwp_file->name);
11929 }
11930
11931 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11932 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11933 {
11934 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11935 " [in module %s]"),
11936 dwp_file->name);
11937 }
11938 }
11939
11940 if (i < 2
96b79293
TT
11941 || sections.info_or_types.empty ()
11942 || sections.abbrev.empty ())
80626a55
DE
11943 {
11944 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11945 " [in module %s]"),
11946 dwp_file->name);
11947 }
73869dc2 11948 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11949 {
11950 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11951 " [in module %s]"),
11952 dwp_file->name);
11953 }
11954
11955 /* It's easier for the rest of the code if we fake a struct dwo_file and
11956 have dwo_unit "live" in that. At least for now.
11957
11958 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11959 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11960 file, we can combine them back into a virtual DWO file to save space
11961 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11962 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11963
791afaa2
TT
11964 std::string virtual_dwo_name =
11965 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11966 sections.abbrev.get_id (),
11967 sections.line.get_id (),
11968 sections.loc.get_id (),
11969 sections.str_offsets.get_id ());
80626a55 11970 /* Can we use an existing virtual DWO file? */
976ca316 11971 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11972 comp_dir);
80626a55
DE
11973 /* Create one if necessary. */
11974 if (*dwo_file_slot == NULL)
11975 {
b4f54984 11976 if (dwarf_read_debug)
80626a55
DE
11977 {
11978 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11979 virtual_dwo_name.c_str ());
80626a55 11980 }
51ac9db5 11981 dwo_file = new struct dwo_file;
976ca316 11982 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11983 dwo_file->comp_dir = comp_dir;
80626a55
DE
11984 dwo_file->sections.abbrev = sections.abbrev;
11985 dwo_file->sections.line = sections.line;
11986 dwo_file->sections.loc = sections.loc;
11987 dwo_file->sections.macinfo = sections.macinfo;
11988 dwo_file->sections.macro = sections.macro;
11989 dwo_file->sections.str_offsets = sections.str_offsets;
11990 /* The "str" section is global to the entire DWP file. */
11991 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11992 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11993 there's no need to record it in dwo_file.
11994 Also, we can't simply record type sections in dwo_file because
11995 we record a pointer into the vector in dwo_unit. As we collect more
11996 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11997 for it, invalidating all copies of pointers into the previous
11998 contents. */
80626a55
DE
11999 *dwo_file_slot = dwo_file;
12000 }
12001 else
12002 {
b4f54984 12003 if (dwarf_read_debug)
80626a55
DE
12004 {
12005 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12006 virtual_dwo_name.c_str ());
80626a55 12007 }
9a3c8263 12008 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12009 }
80626a55 12010
976ca316 12011 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12012 dwo_unit->dwo_file = dwo_file;
12013 dwo_unit->signature = signature;
8d749320 12014 dwo_unit->section =
976ca316 12015 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12016 *dwo_unit->section = sections.info_or_types;
57d63ce2 12017 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12018
12019 return dwo_unit;
12020}
12021
73869dc2
DE
12022/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12023 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12024 piece within that section used by a TU/CU, return a virtual section
12025 of just that piece. */
12026
12027static struct dwarf2_section_info
976ca316 12028create_dwp_v2_section (dwarf2_per_objfile *per_objfile,
ed2dc618 12029 struct dwarf2_section_info *section,
73869dc2
DE
12030 bfd_size_type offset, bfd_size_type size)
12031{
12032 struct dwarf2_section_info result;
12033 asection *sectp;
12034
12035 gdb_assert (section != NULL);
12036 gdb_assert (!section->is_virtual);
12037
12038 memset (&result, 0, sizeof (result));
12039 result.s.containing_section = section;
dc4ccb6f 12040 result.is_virtual = true;
73869dc2
DE
12041
12042 if (size == 0)
12043 return result;
12044
96b79293 12045 sectp = section->get_bfd_section ();
73869dc2
DE
12046
12047 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12048 bounds of the real section. This is a pretty-rare event, so just
12049 flag an error (easier) instead of a warning and trying to cope. */
12050 if (sectp == NULL
fd361982 12051 || offset + size > bfd_section_size (sectp))
73869dc2 12052 {
73869dc2
DE
12053 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12054 " in section %s [in module %s]"),
fd361982 12055 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12056 objfile_name (per_objfile->objfile));
73869dc2
DE
12057 }
12058
12059 result.virtual_offset = offset;
12060 result.size = size;
12061 return result;
12062}
12063
12064/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12065 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12066 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12067 This is for DWP version 2 files. */
12068
12069static struct dwo_unit *
976ca316 12070create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12071 struct dwp_file *dwp_file,
73869dc2
DE
12072 uint32_t unit_index,
12073 const char *comp_dir,
12074 ULONGEST signature, int is_debug_types)
12075{
73869dc2
DE
12076 const struct dwp_hash_table *dwp_htab =
12077 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12078 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12079 const char *kind = is_debug_types ? "TU" : "CU";
12080 struct dwo_file *dwo_file;
12081 struct dwo_unit *dwo_unit;
12082 struct virtual_v2_dwo_sections sections;
12083 void **dwo_file_slot;
73869dc2
DE
12084 int i;
12085
12086 gdb_assert (dwp_file->version == 2);
12087
b4f54984 12088 if (dwarf_read_debug)
73869dc2
DE
12089 {
12090 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12091 kind,
12092 pulongest (unit_index), hex_string (signature),
12093 dwp_file->name);
12094 }
12095
12096 /* Fetch the section offsets of this DWO unit. */
12097
12098 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12099
12100 for (i = 0; i < dwp_htab->nr_columns; ++i)
12101 {
12102 uint32_t offset = read_4_bytes (dbfd,
12103 dwp_htab->section_pool.v2.offsets
12104 + (((unit_index - 1) * dwp_htab->nr_columns
12105 + i)
12106 * sizeof (uint32_t)));
12107 uint32_t size = read_4_bytes (dbfd,
12108 dwp_htab->section_pool.v2.sizes
12109 + (((unit_index - 1) * dwp_htab->nr_columns
12110 + i)
12111 * sizeof (uint32_t)));
12112
12113 switch (dwp_htab->section_pool.v2.section_ids[i])
12114 {
12115 case DW_SECT_INFO:
12116 case DW_SECT_TYPES:
12117 sections.info_or_types_offset = offset;
12118 sections.info_or_types_size = size;
12119 break;
12120 case DW_SECT_ABBREV:
12121 sections.abbrev_offset = offset;
12122 sections.abbrev_size = size;
12123 break;
12124 case DW_SECT_LINE:
12125 sections.line_offset = offset;
12126 sections.line_size = size;
12127 break;
12128 case DW_SECT_LOC:
12129 sections.loc_offset = offset;
12130 sections.loc_size = size;
12131 break;
12132 case DW_SECT_STR_OFFSETS:
12133 sections.str_offsets_offset = offset;
12134 sections.str_offsets_size = size;
12135 break;
12136 case DW_SECT_MACINFO:
12137 sections.macinfo_offset = offset;
12138 sections.macinfo_size = size;
12139 break;
12140 case DW_SECT_MACRO:
12141 sections.macro_offset = offset;
12142 sections.macro_size = size;
12143 break;
12144 }
12145 }
12146
12147 /* It's easier for the rest of the code if we fake a struct dwo_file and
12148 have dwo_unit "live" in that. At least for now.
12149
12150 The DWP file can be made up of a random collection of CUs and TUs.
12151 However, for each CU + set of TUs that came from the same original DWO
12152 file, we can combine them back into a virtual DWO file to save space
12153 (fewer struct dwo_file objects to allocate). Remember that for really
12154 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12155
791afaa2
TT
12156 std::string virtual_dwo_name =
12157 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12158 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12159 (long) (sections.line_size ? sections.line_offset : 0),
12160 (long) (sections.loc_size ? sections.loc_offset : 0),
12161 (long) (sections.str_offsets_size
12162 ? sections.str_offsets_offset : 0));
73869dc2 12163 /* Can we use an existing virtual DWO file? */
976ca316 12164 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12165 comp_dir);
73869dc2
DE
12166 /* Create one if necessary. */
12167 if (*dwo_file_slot == NULL)
12168 {
b4f54984 12169 if (dwarf_read_debug)
73869dc2
DE
12170 {
12171 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12172 virtual_dwo_name.c_str ());
73869dc2 12173 }
51ac9db5 12174 dwo_file = new struct dwo_file;
976ca316 12175 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12176 dwo_file->comp_dir = comp_dir;
12177 dwo_file->sections.abbrev =
976ca316 12178 create_dwp_v2_section (per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12179 sections.abbrev_offset, sections.abbrev_size);
12180 dwo_file->sections.line =
976ca316 12181 create_dwp_v2_section (per_objfile, &dwp_file->sections.line,
73869dc2
DE
12182 sections.line_offset, sections.line_size);
12183 dwo_file->sections.loc =
976ca316 12184 create_dwp_v2_section (per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12185 sections.loc_offset, sections.loc_size);
12186 dwo_file->sections.macinfo =
976ca316 12187 create_dwp_v2_section (per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12188 sections.macinfo_offset, sections.macinfo_size);
12189 dwo_file->sections.macro =
976ca316 12190 create_dwp_v2_section (per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12191 sections.macro_offset, sections.macro_size);
12192 dwo_file->sections.str_offsets =
976ca316 12193 create_dwp_v2_section (per_objfile,
ed2dc618 12194 &dwp_file->sections.str_offsets,
73869dc2
DE
12195 sections.str_offsets_offset,
12196 sections.str_offsets_size);
12197 /* The "str" section is global to the entire DWP file. */
12198 dwo_file->sections.str = dwp_file->sections.str;
12199 /* The info or types section is assigned below to dwo_unit,
12200 there's no need to record it in dwo_file.
12201 Also, we can't simply record type sections in dwo_file because
12202 we record a pointer into the vector in dwo_unit. As we collect more
12203 types we'll grow the vector and eventually have to reallocate space
12204 for it, invalidating all copies of pointers into the previous
12205 contents. */
12206 *dwo_file_slot = dwo_file;
12207 }
12208 else
12209 {
b4f54984 12210 if (dwarf_read_debug)
73869dc2
DE
12211 {
12212 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12213 virtual_dwo_name.c_str ());
73869dc2 12214 }
9a3c8263 12215 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12216 }
73869dc2 12217
976ca316 12218 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12219 dwo_unit->dwo_file = dwo_file;
12220 dwo_unit->signature = signature;
8d749320 12221 dwo_unit->section =
976ca316
SM
12222 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12223 *dwo_unit->section = create_dwp_v2_section (per_objfile,
ed2dc618 12224 is_debug_types
73869dc2
DE
12225 ? &dwp_file->sections.types
12226 : &dwp_file->sections.info,
12227 sections.info_or_types_offset,
12228 sections.info_or_types_size);
12229 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12230
12231 return dwo_unit;
12232}
12233
57d63ce2
DE
12234/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12235 Returns NULL if the signature isn't found. */
80626a55
DE
12236
12237static struct dwo_unit *
976ca316 12238lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12239 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12240 ULONGEST signature, int is_debug_types)
80626a55 12241{
57d63ce2
DE
12242 const struct dwp_hash_table *dwp_htab =
12243 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12244 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12245 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12246 uint32_t hash = signature & mask;
12247 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12248 unsigned int i;
12249 void **slot;
870f88f7 12250 struct dwo_unit find_dwo_cu;
80626a55
DE
12251
12252 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12253 find_dwo_cu.signature = signature;
19ac8c2e 12254 slot = htab_find_slot (is_debug_types
48b490f2
TT
12255 ? dwp_file->loaded_tus.get ()
12256 : dwp_file->loaded_cus.get (),
19ac8c2e 12257 &find_dwo_cu, INSERT);
80626a55
DE
12258
12259 if (*slot != NULL)
9a3c8263 12260 return (struct dwo_unit *) *slot;
80626a55
DE
12261
12262 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12263 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12264 {
12265 ULONGEST signature_in_table;
12266
12267 signature_in_table =
57d63ce2 12268 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12269 if (signature_in_table == signature)
12270 {
57d63ce2
DE
12271 uint32_t unit_index =
12272 read_4_bytes (dbfd,
12273 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12274
73869dc2
DE
12275 if (dwp_file->version == 1)
12276 {
976ca316
SM
12277 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12278 unit_index, comp_dir,
12279 signature, is_debug_types);
73869dc2
DE
12280 }
12281 else
12282 {
976ca316
SM
12283 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12284 unit_index, comp_dir,
12285 signature, is_debug_types);
73869dc2 12286 }
9a3c8263 12287 return (struct dwo_unit *) *slot;
80626a55
DE
12288 }
12289 if (signature_in_table == 0)
12290 return NULL;
12291 hash = (hash + hash2) & mask;
12292 }
12293
12294 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12295 " [in module %s]"),
12296 dwp_file->name);
12297}
12298
ab5088bf 12299/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12300 Open the file specified by FILE_NAME and hand it off to BFD for
12301 preliminary analysis. Return a newly initialized bfd *, which
12302 includes a canonicalized copy of FILE_NAME.
80626a55 12303 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12304 SEARCH_CWD is true if the current directory is to be searched.
12305 It will be searched before debug-file-directory.
13aaf454
DE
12306 If successful, the file is added to the bfd include table of the
12307 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12308 If unable to find/open the file, return NULL.
3019eac3
DE
12309 NOTE: This function is derived from symfile_bfd_open. */
12310
192b62ce 12311static gdb_bfd_ref_ptr
976ca316 12312try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12313 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12314{
24b9144d 12315 int desc;
9c02c129
DE
12316 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12317 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12318 to debug_file_directory. */
e0cc99a6 12319 const char *search_path;
9c02c129
DE
12320 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12321
e0cc99a6 12322 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12323 if (search_cwd)
12324 {
12325 if (*debug_file_directory != '\0')
e0cc99a6
TT
12326 {
12327 search_path_holder.reset (concat (".", dirname_separator_string,
12328 debug_file_directory,
12329 (char *) NULL));
12330 search_path = search_path_holder.get ();
12331 }
6ac97d4c 12332 else
e0cc99a6 12333 search_path = ".";
6ac97d4c 12334 }
9c02c129 12335 else
e0cc99a6 12336 search_path = debug_file_directory;
3019eac3 12337
24b9144d 12338 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12339 if (is_dwp)
12340 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12341
12342 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12343 desc = openp (search_path, flags, file_name,
3019eac3
DE
12344 O_RDONLY | O_BINARY, &absolute_name);
12345 if (desc < 0)
12346 return NULL;
12347
e0cc99a6
TT
12348 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12349 gnutarget, desc));
9c02c129
DE
12350 if (sym_bfd == NULL)
12351 return NULL;
192b62ce 12352 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12353
192b62ce
TT
12354 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12355 return NULL;
3019eac3 12356
13aaf454
DE
12357 /* Success. Record the bfd as having been included by the objfile's bfd.
12358 This is important because things like demangled_names_hash lives in the
12359 objfile's per_bfd space and may have references to things like symbol
12360 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12361 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12362
3019eac3
DE
12363 return sym_bfd;
12364}
12365
ab5088bf 12366/* Try to open DWO file FILE_NAME.
3019eac3
DE
12367 COMP_DIR is the DW_AT_comp_dir attribute.
12368 The result is the bfd handle of the file.
12369 If there is a problem finding or opening the file, return NULL.
12370 Upon success, the canonicalized path of the file is stored in the bfd,
12371 same as symfile_bfd_open. */
12372
192b62ce 12373static gdb_bfd_ref_ptr
976ca316 12374open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12375 const char *file_name, const char *comp_dir)
3019eac3 12376{
80626a55 12377 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12378 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12379 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12380
12381 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12382
12383 if (comp_dir != NULL)
12384 {
43816ebc
TT
12385 gdb::unique_xmalloc_ptr<char> path_to_try
12386 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12387
12388 /* NOTE: If comp_dir is a relative path, this will also try the
12389 search path, which seems useful. */
976ca316 12390 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12391 0 /*is_dwp*/,
192b62ce 12392 1 /*search_cwd*/));
3019eac3
DE
12393 if (abfd != NULL)
12394 return abfd;
12395 }
12396
12397 /* That didn't work, try debug-file-directory, which, despite its name,
12398 is a list of paths. */
12399
12400 if (*debug_file_directory == '\0')
12401 return NULL;
12402
976ca316 12403 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12404 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12405}
12406
80626a55
DE
12407/* This function is mapped across the sections and remembers the offset and
12408 size of each of the DWO debugging sections we are interested in. */
12409
12410static void
12411dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12412{
9a3c8263 12413 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12414 const struct dwop_section_names *names = &dwop_section_names;
12415
12416 if (section_is_p (sectp->name, &names->abbrev_dwo))
12417 {
049412e3 12418 dwo_sections->abbrev.s.section = sectp;
fd361982 12419 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12420 }
12421 else if (section_is_p (sectp->name, &names->info_dwo))
12422 {
049412e3 12423 dwo_sections->info.s.section = sectp;
fd361982 12424 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12425 }
12426 else if (section_is_p (sectp->name, &names->line_dwo))
12427 {
049412e3 12428 dwo_sections->line.s.section = sectp;
fd361982 12429 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12430 }
12431 else if (section_is_p (sectp->name, &names->loc_dwo))
12432 {
049412e3 12433 dwo_sections->loc.s.section = sectp;
fd361982 12434 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12435 }
41144253 12436 else if (section_is_p (sectp->name, &names->loclists_dwo))
12437 {
12438 dwo_sections->loclists.s.section = sectp;
12439 dwo_sections->loclists.size = bfd_section_size (sectp);
12440 }
80626a55
DE
12441 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12442 {
049412e3 12443 dwo_sections->macinfo.s.section = sectp;
fd361982 12444 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12445 }
12446 else if (section_is_p (sectp->name, &names->macro_dwo))
12447 {
049412e3 12448 dwo_sections->macro.s.section = sectp;
fd361982 12449 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12450 }
12451 else if (section_is_p (sectp->name, &names->str_dwo))
12452 {
049412e3 12453 dwo_sections->str.s.section = sectp;
fd361982 12454 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12455 }
12456 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12457 {
049412e3 12458 dwo_sections->str_offsets.s.section = sectp;
fd361982 12459 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12460 }
12461 else if (section_is_p (sectp->name, &names->types_dwo))
12462 {
12463 struct dwarf2_section_info type_section;
12464
12465 memset (&type_section, 0, sizeof (type_section));
049412e3 12466 type_section.s.section = sectp;
fd361982 12467 type_section.size = bfd_section_size (sectp);
fd5866f6 12468 dwo_sections->types.push_back (type_section);
80626a55
DE
12469 }
12470}
12471
ab5088bf 12472/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12473 by PER_CU. This is for the non-DWP case.
80626a55 12474 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12475
12476static struct dwo_file *
4ab09049
SM
12477open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12478 const char *comp_dir)
3019eac3 12479{
976ca316 12480 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12481
976ca316 12482 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12483 if (dbfd == NULL)
12484 {
b4f54984 12485 if (dwarf_read_debug)
80626a55
DE
12486 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12487 return NULL;
12488 }
263db9a1 12489
51ac9db5 12490 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12491 dwo_file->dwo_name = dwo_name;
12492 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12493 dwo_file->dbfd = std::move (dbfd);
3019eac3 12494
fb1eb2f9 12495 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12496 &dwo_file->sections);
3019eac3 12497
976ca316
SM
12498 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12499 dwo_file->cus);
3019eac3 12500
976ca316 12501 create_debug_types_hash_table (per_objfile, dwo_file.get (),
ed2dc618 12502 dwo_file->sections.types, dwo_file->tus);
3019eac3 12503
b4f54984 12504 if (dwarf_read_debug)
80626a55
DE
12505 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12506
263db9a1 12507 return dwo_file.release ();
3019eac3
DE
12508}
12509
80626a55 12510/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12511 size of each of the DWP debugging sections common to version 1 and 2 that
12512 we are interested in. */
3019eac3 12513
80626a55 12514static void
73869dc2
DE
12515dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12516 void *dwp_file_ptr)
3019eac3 12517{
9a3c8263 12518 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12519 const struct dwop_section_names *names = &dwop_section_names;
12520 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12521
80626a55 12522 /* Record the ELF section number for later lookup: this is what the
73869dc2 12523 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12524 gdb_assert (elf_section_nr < dwp_file->num_sections);
12525 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12526
80626a55
DE
12527 /* Look for specific sections that we need. */
12528 if (section_is_p (sectp->name, &names->str_dwo))
12529 {
049412e3 12530 dwp_file->sections.str.s.section = sectp;
fd361982 12531 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12532 }
12533 else if (section_is_p (sectp->name, &names->cu_index))
12534 {
049412e3 12535 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12536 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12537 }
12538 else if (section_is_p (sectp->name, &names->tu_index))
12539 {
049412e3 12540 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12541 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12542 }
12543}
3019eac3 12544
73869dc2
DE
12545/* This function is mapped across the sections and remembers the offset and
12546 size of each of the DWP version 2 debugging sections that we are interested
12547 in. This is split into a separate function because we don't know if we
12548 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12549
12550static void
12551dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12552{
9a3c8263 12553 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12554 const struct dwop_section_names *names = &dwop_section_names;
12555 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12556
12557 /* Record the ELF section number for later lookup: this is what the
12558 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12559 gdb_assert (elf_section_nr < dwp_file->num_sections);
12560 dwp_file->elf_sections[elf_section_nr] = sectp;
12561
12562 /* Look for specific sections that we need. */
12563 if (section_is_p (sectp->name, &names->abbrev_dwo))
12564 {
049412e3 12565 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12566 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12567 }
12568 else if (section_is_p (sectp->name, &names->info_dwo))
12569 {
049412e3 12570 dwp_file->sections.info.s.section = sectp;
fd361982 12571 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12572 }
12573 else if (section_is_p (sectp->name, &names->line_dwo))
12574 {
049412e3 12575 dwp_file->sections.line.s.section = sectp;
fd361982 12576 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12577 }
12578 else if (section_is_p (sectp->name, &names->loc_dwo))
12579 {
049412e3 12580 dwp_file->sections.loc.s.section = sectp;
fd361982 12581 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12582 }
12583 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12584 {
049412e3 12585 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12586 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12587 }
12588 else if (section_is_p (sectp->name, &names->macro_dwo))
12589 {
049412e3 12590 dwp_file->sections.macro.s.section = sectp;
fd361982 12591 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12592 }
12593 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12594 {
049412e3 12595 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12596 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12597 }
12598 else if (section_is_p (sectp->name, &names->types_dwo))
12599 {
049412e3 12600 dwp_file->sections.types.s.section = sectp;
fd361982 12601 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12602 }
12603}
12604
80626a55 12605/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12606
80626a55
DE
12607static hashval_t
12608hash_dwp_loaded_cutus (const void *item)
12609{
9a3c8263 12610 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12611
80626a55
DE
12612 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12613 return dwo_unit->signature;
3019eac3
DE
12614}
12615
80626a55 12616/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12617
80626a55
DE
12618static int
12619eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12620{
9a3c8263
SM
12621 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12622 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12623
80626a55
DE
12624 return dua->signature == dub->signature;
12625}
3019eac3 12626
80626a55 12627/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12628
48b490f2 12629static htab_up
298e9637 12630allocate_dwp_loaded_cutus_table ()
80626a55 12631{
48b490f2
TT
12632 return htab_up (htab_create_alloc (3,
12633 hash_dwp_loaded_cutus,
12634 eq_dwp_loaded_cutus,
12635 NULL, xcalloc, xfree));
80626a55 12636}
3019eac3 12637
ab5088bf
DE
12638/* Try to open DWP file FILE_NAME.
12639 The result is the bfd handle of the file.
12640 If there is a problem finding or opening the file, return NULL.
12641 Upon success, the canonicalized path of the file is stored in the bfd,
12642 same as symfile_bfd_open. */
12643
192b62ce 12644static gdb_bfd_ref_ptr
976ca316 12645open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12646{
976ca316 12647 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12648 1 /*is_dwp*/,
192b62ce 12649 1 /*search_cwd*/));
6ac97d4c
DE
12650 if (abfd != NULL)
12651 return abfd;
12652
12653 /* Work around upstream bug 15652.
12654 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12655 [Whether that's a "bug" is debatable, but it is getting in our way.]
12656 We have no real idea where the dwp file is, because gdb's realpath-ing
12657 of the executable's path may have discarded the needed info.
12658 [IWBN if the dwp file name was recorded in the executable, akin to
12659 .gnu_debuglink, but that doesn't exist yet.]
12660 Strip the directory from FILE_NAME and search again. */
12661 if (*debug_file_directory != '\0')
12662 {
12663 /* Don't implicitly search the current directory here.
12664 If the user wants to search "." to handle this case,
12665 it must be added to debug-file-directory. */
976ca316
SM
12666 return try_open_dwop_file (per_objfile, lbasename (file_name),
12667 1 /*is_dwp*/,
6ac97d4c
DE
12668 0 /*search_cwd*/);
12669 }
12670
12671 return NULL;
ab5088bf
DE
12672}
12673
80626a55
DE
12674/* Initialize the use of the DWP file for the current objfile.
12675 By convention the name of the DWP file is ${objfile}.dwp.
12676 The result is NULL if it can't be found. */
a766d390 12677
400174b1 12678static std::unique_ptr<struct dwp_file>
976ca316 12679open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12680{
976ca316 12681 struct objfile *objfile = per_objfile->objfile;
80626a55 12682
82bf32bc
JK
12683 /* Try to find first .dwp for the binary file before any symbolic links
12684 resolving. */
6c447423
DE
12685
12686 /* If the objfile is a debug file, find the name of the real binary
12687 file and get the name of dwp file from there. */
d721ba37 12688 std::string dwp_name;
6c447423
DE
12689 if (objfile->separate_debug_objfile_backlink != NULL)
12690 {
12691 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12692 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12693
d721ba37 12694 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12695 }
12696 else
d721ba37
PA
12697 dwp_name = objfile->original_name;
12698
12699 dwp_name += ".dwp";
80626a55 12700
976ca316 12701 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12702 if (dbfd == NULL
12703 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12704 {
12705 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12706 dwp_name = objfile_name (objfile);
12707 dwp_name += ".dwp";
976ca316 12708 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12709 }
12710
80626a55
DE
12711 if (dbfd == NULL)
12712 {
b4f54984 12713 if (dwarf_read_debug)
d721ba37 12714 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12715 return std::unique_ptr<dwp_file> ();
3019eac3 12716 }
400174b1
TT
12717
12718 const char *name = bfd_get_filename (dbfd.get ());
12719 std::unique_ptr<struct dwp_file> dwp_file
12720 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12721
0a0f4c01 12722 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12723 dwp_file->elf_sections =
976ca316 12724 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12725 dwp_file->num_sections, asection *);
12726
400174b1
TT
12727 bfd_map_over_sections (dwp_file->dbfd.get (),
12728 dwarf2_locate_common_dwp_sections,
12729 dwp_file.get ());
80626a55 12730
976ca316 12731 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12732
976ca316 12733 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12734
73869dc2 12735 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12736 if (dwp_file->cus && dwp_file->tus
12737 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12738 {
12739 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12740 pretty bizarre. We use pulongest here because that's the established
4d65956b 12741 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12742 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12743 " TU version %s [in DWP file %s]"),
12744 pulongest (dwp_file->cus->version),
d721ba37 12745 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12746 }
08302ed2
DE
12747
12748 if (dwp_file->cus)
12749 dwp_file->version = dwp_file->cus->version;
12750 else if (dwp_file->tus)
12751 dwp_file->version = dwp_file->tus->version;
12752 else
12753 dwp_file->version = 2;
73869dc2
DE
12754
12755 if (dwp_file->version == 2)
400174b1
TT
12756 bfd_map_over_sections (dwp_file->dbfd.get (),
12757 dwarf2_locate_v2_dwp_sections,
12758 dwp_file.get ());
73869dc2 12759
298e9637
SM
12760 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12761 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12762
b4f54984 12763 if (dwarf_read_debug)
80626a55
DE
12764 {
12765 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12766 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12767 " %s CUs, %s TUs\n",
12768 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12769 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12770 }
12771
12772 return dwp_file;
3019eac3 12773}
c906108c 12774
ab5088bf
DE
12775/* Wrapper around open_and_init_dwp_file, only open it once. */
12776
12777static struct dwp_file *
976ca316 12778get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12779{
976ca316 12780 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12781 {
976ca316
SM
12782 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12783 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12784 }
976ca316 12785 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12786}
12787
80626a55
DE
12788/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12789 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12790 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12791 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12792 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12793
12794 This is called, for example, when wanting to read a variable with a
12795 complex location. Therefore we don't want to do file i/o for every call.
12796 Therefore we don't want to look for a DWO file on every call.
12797 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12798 then we check if we've already seen DWO_NAME, and only THEN do we check
12799 for a DWO file.
12800
1c658ad5 12801 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12802 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12803
3019eac3 12804static struct dwo_unit *
4ab09049 12805lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12806 ULONGEST signature, int is_debug_types)
3019eac3 12807{
976ca316
SM
12808 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12809 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12810 const char *kind = is_debug_types ? "TU" : "CU";
12811 void **dwo_file_slot;
3019eac3 12812 struct dwo_file *dwo_file;
80626a55 12813 struct dwp_file *dwp_file;
cb1df416 12814
6a506a2d
DE
12815 /* First see if there's a DWP file.
12816 If we have a DWP file but didn't find the DWO inside it, don't
12817 look for the original DWO file. It makes gdb behave differently
12818 depending on whether one is debugging in the build tree. */
cf2c3c16 12819
976ca316 12820 dwp_file = get_dwp_file (per_objfile);
80626a55 12821 if (dwp_file != NULL)
cf2c3c16 12822 {
80626a55
DE
12823 const struct dwp_hash_table *dwp_htab =
12824 is_debug_types ? dwp_file->tus : dwp_file->cus;
12825
12826 if (dwp_htab != NULL)
12827 {
12828 struct dwo_unit *dwo_cutu =
976ca316
SM
12829 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12830 is_debug_types);
80626a55
DE
12831
12832 if (dwo_cutu != NULL)
12833 {
b4f54984 12834 if (dwarf_read_debug)
80626a55
DE
12835 {
12836 fprintf_unfiltered (gdb_stdlog,
12837 "Virtual DWO %s %s found: @%s\n",
12838 kind, hex_string (signature),
12839 host_address_to_string (dwo_cutu));
12840 }
12841 return dwo_cutu;
12842 }
12843 }
12844 }
6a506a2d 12845 else
80626a55 12846 {
6a506a2d 12847 /* No DWP file, look for the DWO file. */
80626a55 12848
976ca316 12849 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12850 if (*dwo_file_slot == NULL)
80626a55 12851 {
6a506a2d 12852 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12853 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12854 }
6a506a2d 12855 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12856 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12857
6a506a2d 12858 if (dwo_file != NULL)
19c3d4c9 12859 {
6a506a2d
DE
12860 struct dwo_unit *dwo_cutu = NULL;
12861
12862 if (is_debug_types && dwo_file->tus)
12863 {
12864 struct dwo_unit find_dwo_cutu;
12865
12866 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12867 find_dwo_cutu.signature = signature;
9a3c8263 12868 dwo_cutu
b0b6a987
TT
12869 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12870 &find_dwo_cutu);
6a506a2d 12871 }
33c5cd75 12872 else if (!is_debug_types && dwo_file->cus)
80626a55 12873 {
33c5cd75
DB
12874 struct dwo_unit find_dwo_cutu;
12875
12876 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12877 find_dwo_cutu.signature = signature;
b0b6a987 12878 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12879 &find_dwo_cutu);
6a506a2d
DE
12880 }
12881
12882 if (dwo_cutu != NULL)
12883 {
b4f54984 12884 if (dwarf_read_debug)
6a506a2d
DE
12885 {
12886 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12887 kind, dwo_name, hex_string (signature),
12888 host_address_to_string (dwo_cutu));
12889 }
12890 return dwo_cutu;
80626a55
DE
12891 }
12892 }
2e276125 12893 }
9cdd5dbd 12894
80626a55
DE
12895 /* We didn't find it. This could mean a dwo_id mismatch, or
12896 someone deleted the DWO/DWP file, or the search path isn't set up
12897 correctly to find the file. */
12898
b4f54984 12899 if (dwarf_read_debug)
80626a55
DE
12900 {
12901 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12902 kind, dwo_name, hex_string (signature));
12903 }
3019eac3 12904
6656a72d
DE
12905 /* This is a warning and not a complaint because it can be caused by
12906 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12907 {
12908 /* Print the name of the DWP file if we looked there, helps the user
12909 better diagnose the problem. */
791afaa2 12910 std::string dwp_text;
43942612
DE
12911
12912 if (dwp_file != NULL)
791afaa2
TT
12913 dwp_text = string_printf (" [in DWP file %s]",
12914 lbasename (dwp_file->name));
43942612 12915
9d8780f0 12916 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12917 " [in module %s]"),
4ab09049
SM
12918 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12919 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12920 }
3019eac3 12921 return NULL;
5fb290d7
DJ
12922}
12923
80626a55
DE
12924/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12925 See lookup_dwo_cutu_unit for details. */
12926
12927static struct dwo_unit *
4ab09049 12928lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12929 ULONGEST signature)
12930{
4ab09049
SM
12931 gdb_assert (!cu->per_cu->is_debug_types);
12932
12933 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12934}
12935
12936/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12937 See lookup_dwo_cutu_unit for details. */
12938
12939static struct dwo_unit *
4ab09049 12940lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12941{
4ab09049
SM
12942 gdb_assert (cu->per_cu->is_debug_types);
12943
12944 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12945
12946 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12947}
12948
89e63ee4
DE
12949/* Traversal function for queue_and_load_all_dwo_tus. */
12950
12951static int
12952queue_and_load_dwo_tu (void **slot, void *info)
12953{
12954 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12955 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12956 ULONGEST signature = dwo_unit->signature;
d460f660 12957 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12958
12959 if (sig_type != NULL)
12960 {
12961 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12962
12963 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12964 a real dependency of PER_CU on SIG_TYPE. That is detected later
12965 while processing PER_CU. */
120ce1b5 12966 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
12967 load_full_type_unit (sig_cu, cu->per_objfile);
12968 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12969 }
12970
12971 return 1;
12972}
12973
1b555f17 12974/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12975 The DWO may have the only definition of the type, though it may not be
12976 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12977 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12978
12979static void
1b555f17 12980queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12981{
12982 struct dwo_unit *dwo_unit;
12983 struct dwo_file *dwo_file;
12984
1b555f17
SM
12985 gdb_assert (cu != nullptr);
12986 gdb_assert (!cu->per_cu->is_debug_types);
12987 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12988
1b555f17 12989 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12990 gdb_assert (dwo_unit != NULL);
12991
12992 dwo_file = dwo_unit->dwo_file;
12993 if (dwo_file->tus != NULL)
1b555f17 12994 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12995}
12996
3019eac3 12997/* Read in various DIEs. */
348e048f 12998
d389af10 12999/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13000 Inherit only the children of the DW_AT_abstract_origin DIE not being
13001 already referenced by DW_AT_abstract_origin from the children of the
13002 current DIE. */
d389af10
JK
13003
13004static void
13005inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13006{
13007 struct die_info *child_die;
791afaa2 13008 sect_offset *offsetp;
d389af10
JK
13009 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13010 struct die_info *origin_die;
13011 /* Iterator of the ORIGIN_DIE children. */
13012 struct die_info *origin_child_die;
d389af10 13013 struct attribute *attr;
cd02d79d
PA
13014 struct dwarf2_cu *origin_cu;
13015 struct pending **origin_previous_list_in_scope;
d389af10
JK
13016
13017 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13018 if (!attr)
13019 return;
13020
cd02d79d
PA
13021 /* Note that following die references may follow to a die in a
13022 different cu. */
13023
13024 origin_cu = cu;
13025 origin_die = follow_die_ref (die, attr, &origin_cu);
13026
13027 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13028 symbols in. */
13029 origin_previous_list_in_scope = origin_cu->list_in_scope;
13030 origin_cu->list_in_scope = cu->list_in_scope;
13031
edb3359d
DJ
13032 if (die->tag != origin_die->tag
13033 && !(die->tag == DW_TAG_inlined_subroutine
13034 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13035 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13036 sect_offset_str (die->sect_off),
13037 sect_offset_str (origin_die->sect_off));
d389af10 13038
791afaa2 13039 std::vector<sect_offset> offsets;
d389af10 13040
3ea89b92
PMR
13041 for (child_die = die->child;
13042 child_die && child_die->tag;
436c571c 13043 child_die = child_die->sibling)
3ea89b92
PMR
13044 {
13045 struct die_info *child_origin_die;
13046 struct dwarf2_cu *child_origin_cu;
13047
13048 /* We are trying to process concrete instance entries:
216f72a1 13049 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13050 it's not relevant to our analysis here. i.e. detecting DIEs that are
13051 present in the abstract instance but not referenced in the concrete
13052 one. */
216f72a1
JK
13053 if (child_die->tag == DW_TAG_call_site
13054 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13055 continue;
13056
c38f313d
DJ
13057 /* For each CHILD_DIE, find the corresponding child of
13058 ORIGIN_DIE. If there is more than one layer of
13059 DW_AT_abstract_origin, follow them all; there shouldn't be,
13060 but GCC versions at least through 4.4 generate this (GCC PR
13061 40573). */
3ea89b92
PMR
13062 child_origin_die = child_die;
13063 child_origin_cu = cu;
c38f313d
DJ
13064 while (1)
13065 {
cd02d79d
PA
13066 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13067 child_origin_cu);
c38f313d
DJ
13068 if (attr == NULL)
13069 break;
cd02d79d
PA
13070 child_origin_die = follow_die_ref (child_origin_die, attr,
13071 &child_origin_cu);
c38f313d
DJ
13072 }
13073
d389af10
JK
13074 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13075 counterpart may exist. */
c38f313d 13076 if (child_origin_die != child_die)
d389af10 13077 {
edb3359d
DJ
13078 if (child_die->tag != child_origin_die->tag
13079 && !(child_die->tag == DW_TAG_inlined_subroutine
13080 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13081 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13082 "different tags"),
9d8780f0
SM
13083 sect_offset_str (child_die->sect_off),
13084 sect_offset_str (child_origin_die->sect_off));
c38f313d 13085 if (child_origin_die->parent != origin_die)
b98664d3 13086 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13087 "different parents"),
9d8780f0
SM
13088 sect_offset_str (child_die->sect_off),
13089 sect_offset_str (child_origin_die->sect_off));
c38f313d 13090 else
791afaa2 13091 offsets.push_back (child_origin_die->sect_off);
d389af10 13092 }
d389af10 13093 }
791afaa2
TT
13094 std::sort (offsets.begin (), offsets.end ());
13095 sect_offset *offsets_end = offsets.data () + offsets.size ();
13096 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13097 if (offsetp[-1] == *offsetp)
b98664d3 13098 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13099 "to DIE %s as their abstract origin"),
13100 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13101
791afaa2 13102 offsetp = offsets.data ();
d389af10
JK
13103 origin_child_die = origin_die->child;
13104 while (origin_child_die && origin_child_die->tag)
13105 {
13106 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13107 while (offsetp < offsets_end
9c541725 13108 && *offsetp < origin_child_die->sect_off)
d389af10 13109 offsetp++;
b64f50a1 13110 if (offsetp >= offsets_end
9c541725 13111 || *offsetp > origin_child_die->sect_off)
d389af10 13112 {
adde2bff
DE
13113 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13114 Check whether we're already processing ORIGIN_CHILD_DIE.
13115 This can happen with mutually referenced abstract_origins.
13116 PR 16581. */
13117 if (!origin_child_die->in_process)
13118 process_die (origin_child_die, origin_cu);
d389af10 13119 }
436c571c 13120 origin_child_die = origin_child_die->sibling;
d389af10 13121 }
cd02d79d 13122 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13123
13124 if (cu != origin_cu)
13125 compute_delayed_physnames (origin_cu);
d389af10
JK
13126}
13127
c906108c 13128static void
e7c27a73 13129read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13130{
5e22e966 13131 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13132 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13133 struct context_stack *newobj;
c906108c
SS
13134 CORE_ADDR lowpc;
13135 CORE_ADDR highpc;
13136 struct die_info *child_die;
edb3359d 13137 struct attribute *attr, *call_line, *call_file;
15d034d0 13138 const char *name;
e142c38c 13139 CORE_ADDR baseaddr;
801e3a5b 13140 struct block *block;
edb3359d 13141 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13142 std::vector<struct symbol *> template_args;
34eaf542 13143 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13144
13145 if (inlined_func)
13146 {
13147 /* If we do not have call site information, we can't show the
13148 caller of this inlined function. That's too confusing, so
13149 only use the scope for local variables. */
13150 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13151 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13152 if (call_line == NULL || call_file == NULL)
13153 {
13154 read_lexical_block_scope (die, cu);
13155 return;
13156 }
13157 }
c906108c 13158
b3b3bada 13159 baseaddr = objfile->text_section_offset ();
e142c38c 13160
94af9270 13161 name = dwarf2_name (die, cu);
c906108c 13162
e8d05480
JB
13163 /* Ignore functions with missing or empty names. These are actually
13164 illegal according to the DWARF standard. */
13165 if (name == NULL)
13166 {
b98664d3 13167 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13168 sect_offset_str (die->sect_off));
e8d05480
JB
13169 return;
13170 }
13171
13172 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13173 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13174 <= PC_BOUNDS_INVALID)
e8d05480 13175 {
ae4d0c03
PM
13176 attr = dwarf2_attr (die, DW_AT_external, cu);
13177 if (!attr || !DW_UNSND (attr))
b98664d3 13178 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13179 "for subprogram DIE at %s"),
13180 sect_offset_str (die->sect_off));
e8d05480
JB
13181 return;
13182 }
c906108c 13183
3e29f34a
MR
13184 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13185 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13186
34eaf542
TT
13187 /* If we have any template arguments, then we must allocate a
13188 different sort of symbol. */
436c571c 13189 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13190 {
13191 if (child_die->tag == DW_TAG_template_type_param
13192 || child_die->tag == DW_TAG_template_value_param)
13193 {
8c14c3a3 13194 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13195 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13196 break;
13197 }
13198 }
13199
c24bdb02 13200 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13201 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13202 (struct symbol *) templ_func);
4c2df51b 13203
81873cc8 13204 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13205 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13206 cu->language);
13207
4cecd739
DJ
13208 /* If there is a location expression for DW_AT_frame_base, record
13209 it. */
e142c38c 13210 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13211 if (attr != nullptr)
fe978cb0 13212 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13213
63e43d3a
PMR
13214 /* If there is a location for the static link, record it. */
13215 newobj->static_link = NULL;
13216 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13217 if (attr != nullptr)
63e43d3a 13218 {
224c3ddb
SM
13219 newobj->static_link
13220 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13221 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13222 cu->addr_type ());
63e43d3a
PMR
13223 }
13224
c24bdb02 13225 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13226
639d11d3 13227 if (die->child != NULL)
c906108c 13228 {
639d11d3 13229 child_die = die->child;
c906108c
SS
13230 while (child_die && child_die->tag)
13231 {
34eaf542
TT
13232 if (child_die->tag == DW_TAG_template_type_param
13233 || child_die->tag == DW_TAG_template_value_param)
13234 {
13235 struct symbol *arg = new_symbol (child_die, NULL, cu);
13236
f1078f66 13237 if (arg != NULL)
2f4732b0 13238 template_args.push_back (arg);
34eaf542
TT
13239 }
13240 else
13241 process_die (child_die, cu);
436c571c 13242 child_die = child_die->sibling;
c906108c
SS
13243 }
13244 }
13245
d389af10
JK
13246 inherit_abstract_dies (die, cu);
13247
4a811a97
UW
13248 /* If we have a DW_AT_specification, we might need to import using
13249 directives from the context of the specification DIE. See the
13250 comment in determine_prefix. */
13251 if (cu->language == language_cplus
13252 && dwarf2_attr (die, DW_AT_specification, cu))
13253 {
13254 struct dwarf2_cu *spec_cu = cu;
13255 struct die_info *spec_die = die_specification (die, &spec_cu);
13256
13257 while (spec_die)
13258 {
13259 child_die = spec_die->child;
13260 while (child_die && child_die->tag)
13261 {
13262 if (child_die->tag == DW_TAG_imported_module)
13263 process_die (child_die, spec_cu);
436c571c 13264 child_die = child_die->sibling;
4a811a97
UW
13265 }
13266
13267 /* In some cases, GCC generates specification DIEs that
13268 themselves contain DW_AT_specification attributes. */
13269 spec_die = die_specification (spec_die, &spec_cu);
13270 }
13271 }
13272
c24bdb02 13273 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13274 /* Make a block for the local symbols within. */
c24bdb02 13275 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13276 cstk.static_link, lowpc, highpc);
801e3a5b 13277
df8a16a1 13278 /* For C++, set the block's scope. */
45280282
IB
13279 if ((cu->language == language_cplus
13280 || cu->language == language_fortran
c44af4eb
TT
13281 || cu->language == language_d
13282 || cu->language == language_rust)
4d4ec4e5 13283 && cu->processing_has_namespace_info)
195a3f6c
TT
13284 block_set_scope (block, determine_prefix (die, cu),
13285 &objfile->objfile_obstack);
df8a16a1 13286
801e3a5b
JB
13287 /* If we have address ranges, record them. */
13288 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13289
a60f3166 13290 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13291
34eaf542 13292 /* Attach template arguments to function. */
2f4732b0 13293 if (!template_args.empty ())
34eaf542
TT
13294 {
13295 gdb_assert (templ_func != NULL);
13296
2f4732b0 13297 templ_func->n_template_arguments = template_args.size ();
34eaf542 13298 templ_func->template_arguments
8d749320
SM
13299 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13300 templ_func->n_template_arguments);
34eaf542 13301 memcpy (templ_func->template_arguments,
2f4732b0 13302 template_args.data (),
34eaf542 13303 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13304
13305 /* Make sure that the symtab is set on the new symbols. Even
13306 though they don't appear in this symtab directly, other parts
13307 of gdb assume that symbols do, and this is reasonably
13308 true. */
8634679f 13309 for (symbol *sym : template_args)
3e1d3d8c 13310 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13311 }
13312
208d8187
JB
13313 /* In C++, we can have functions nested inside functions (e.g., when
13314 a function declares a class that has methods). This means that
13315 when we finish processing a function scope, we may need to go
13316 back to building a containing block's symbol lists. */
c24bdb02
KS
13317 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13318 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13319
921e78cf
JB
13320 /* If we've finished processing a top-level function, subsequent
13321 symbols go in the file symbol list. */
c24bdb02
KS
13322 if (cu->get_builder ()->outermost_context_p ())
13323 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13324}
13325
13326/* Process all the DIES contained within a lexical block scope. Start
13327 a new scope, process the dies, and then close the scope. */
13328
13329static void
e7c27a73 13330read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13331{
5e22e966 13332 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13333 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13334 CORE_ADDR lowpc, highpc;
13335 struct die_info *child_die;
e142c38c
DJ
13336 CORE_ADDR baseaddr;
13337
b3b3bada 13338 baseaddr = objfile->text_section_offset ();
c906108c
SS
13339
13340 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13341 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13342 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13343 be nasty. Might be easier to properly extend generic blocks to
af34e669 13344 describe ranges. */
e385593e
JK
13345 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13346 {
13347 case PC_BOUNDS_NOT_PRESENT:
13348 /* DW_TAG_lexical_block has no attributes, process its children as if
13349 there was no wrapping by that DW_TAG_lexical_block.
13350 GCC does no longer produces such DWARF since GCC r224161. */
13351 for (child_die = die->child;
13352 child_die != NULL && child_die->tag;
436c571c 13353 child_die = child_die->sibling)
4f7bc5ed
TT
13354 {
13355 /* We might already be processing this DIE. This can happen
13356 in an unusual circumstance -- where a subroutine A
13357 appears lexically in another subroutine B, but A actually
13358 inlines B. The recursion is broken here, rather than in
13359 inherit_abstract_dies, because it seems better to simply
13360 drop concrete children here. */
13361 if (!child_die->in_process)
13362 process_die (child_die, cu);
13363 }
e385593e
JK
13364 return;
13365 case PC_BOUNDS_INVALID:
13366 return;
13367 }
3e29f34a
MR
13368 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13369 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13370
c24bdb02 13371 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13372 if (die->child != NULL)
c906108c 13373 {
639d11d3 13374 child_die = die->child;
c906108c
SS
13375 while (child_die && child_die->tag)
13376 {
e7c27a73 13377 process_die (child_die, cu);
436c571c 13378 child_die = child_die->sibling;
c906108c
SS
13379 }
13380 }
3ea89b92 13381 inherit_abstract_dies (die, cu);
c24bdb02 13382 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13383
c24bdb02
KS
13384 if (*cu->get_builder ()->get_local_symbols () != NULL
13385 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13386 {
801e3a5b 13387 struct block *block
c24bdb02 13388 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13389 cstk.start_addr, highpc);
801e3a5b
JB
13390
13391 /* Note that recording ranges after traversing children, as we
13392 do here, means that recording a parent's ranges entails
13393 walking across all its children's ranges as they appear in
13394 the address map, which is quadratic behavior.
13395
13396 It would be nicer to record the parent's ranges before
13397 traversing its children, simply overriding whatever you find
13398 there. But since we don't even decide whether to create a
13399 block until after we've traversed its children, that's hard
13400 to do. */
13401 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13402 }
c24bdb02
KS
13403 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13404 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13405}
13406
216f72a1 13407/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13408
13409static void
13410read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13411{
5e22e966 13412 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13413 struct objfile *objfile = per_objfile->objfile;
08feed99 13414 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13415 CORE_ADDR pc, baseaddr;
13416 struct attribute *attr;
13417 struct call_site *call_site, call_site_local;
13418 void **slot;
13419 int nparams;
13420 struct die_info *child_die;
13421
b3b3bada 13422 baseaddr = objfile->text_section_offset ();
96408a79 13423
216f72a1
JK
13424 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13425 if (attr == NULL)
13426 {
13427 /* This was a pre-DWARF-5 GNU extension alias
13428 for DW_AT_call_return_pc. */
13429 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13430 }
96408a79
SA
13431 if (!attr)
13432 {
b98664d3 13433 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13434 "DIE %s [in module %s]"),
13435 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13436 return;
13437 }
cd6c91b4 13438 pc = attr->value_as_address () + baseaddr;
3e29f34a 13439 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13440
13441 if (cu->call_site_htab == NULL)
13442 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13443 NULL, &objfile->objfile_obstack,
13444 hashtab_obstack_allocate, NULL);
13445 call_site_local.pc = pc;
13446 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13447 if (*slot != NULL)
13448 {
b98664d3 13449 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13450 "DIE %s [in module %s]"),
13451 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13452 objfile_name (objfile));
96408a79
SA
13453 return;
13454 }
13455
13456 /* Count parameters at the caller. */
13457
13458 nparams = 0;
13459 for (child_die = die->child; child_die && child_die->tag;
436c571c 13460 child_die = child_die->sibling)
96408a79 13461 {
216f72a1
JK
13462 if (child_die->tag != DW_TAG_call_site_parameter
13463 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13464 {
b98664d3 13465 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13466 "DW_TAG_call_site child DIE %s [in module %s]"),
13467 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13468 objfile_name (objfile));
96408a79
SA
13469 continue;
13470 }
13471
13472 nparams++;
13473 }
13474
224c3ddb
SM
13475 call_site
13476 = ((struct call_site *)
13477 obstack_alloc (&objfile->objfile_obstack,
13478 sizeof (*call_site)
13479 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13480 *slot = call_site;
13481 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13482 call_site->pc = pc;
13483
216f72a1
JK
13484 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13485 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13486 {
13487 struct die_info *func_die;
13488
13489 /* Skip also over DW_TAG_inlined_subroutine. */
13490 for (func_die = die->parent;
13491 func_die && func_die->tag != DW_TAG_subprogram
13492 && func_die->tag != DW_TAG_subroutine_type;
13493 func_die = func_die->parent);
13494
216f72a1
JK
13495 /* DW_AT_call_all_calls is a superset
13496 of DW_AT_call_all_tail_calls. */
96408a79 13497 if (func_die
216f72a1 13498 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13499 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13500 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13501 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13502 {
13503 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13504 not complete. But keep CALL_SITE for look ups via call_site_htab,
13505 both the initial caller containing the real return address PC and
13506 the final callee containing the current PC of a chain of tail
13507 calls do not need to have the tail call list complete. But any
13508 function candidate for a virtual tail call frame searched via
13509 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13510 determined unambiguously. */
13511 }
13512 else
13513 {
13514 struct type *func_type = NULL;
13515
13516 if (func_die)
13517 func_type = get_die_type (func_die, cu);
13518 if (func_type != NULL)
13519 {
78134374 13520 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13521
13522 /* Enlist this call site to the function. */
13523 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13524 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13525 }
13526 else
b98664d3 13527 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13528 "DIE %s [in module %s]"),
13529 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13530 }
13531 }
13532
216f72a1
JK
13533 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13534 if (attr == NULL)
13535 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13536 if (attr == NULL)
13537 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13538 if (attr == NULL)
216f72a1
JK
13539 {
13540 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13541 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13542 }
96408a79 13543 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13544 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13545 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13546 else if (attr->form_is_block ())
96408a79
SA
13547 {
13548 struct dwarf2_locexpr_baton *dlbaton;
13549
8d749320 13550 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13551 dlbaton->data = DW_BLOCK (attr)->data;
13552 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13553 dlbaton->per_objfile = per_objfile;
96408a79
SA
13554 dlbaton->per_cu = cu->per_cu;
13555
13556 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13557 }
cd6c91b4 13558 else if (attr->form_is_ref ())
96408a79 13559 {
96408a79
SA
13560 struct dwarf2_cu *target_cu = cu;
13561 struct die_info *target_die;
13562
ac9ec31b 13563 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13564 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13565 if (die_is_declaration (target_die, target_cu))
13566 {
7d45c7c3 13567 const char *target_physname;
9112db09
JK
13568
13569 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13570 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13571 if (target_physname == NULL)
9112db09 13572 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13573 if (target_physname == NULL)
b98664d3 13574 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13575 "physname, for referencing DIE %s [in module %s]"),
13576 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13577 else
7d455152 13578 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13579 }
13580 else
13581 {
13582 CORE_ADDR lowpc;
13583
13584 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13585 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13586 <= PC_BOUNDS_INVALID)
b98664d3 13587 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13588 "low pc, for referencing DIE %s [in module %s]"),
13589 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13590 else
3e29f34a
MR
13591 {
13592 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13593 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13594 }
96408a79
SA
13595 }
13596 }
13597 else
b98664d3 13598 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13599 "block nor reference, for DIE %s [in module %s]"),
13600 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13601
13602 call_site->per_cu = cu->per_cu;
9f47c707 13603 call_site->per_objfile = per_objfile;
96408a79
SA
13604
13605 for (child_die = die->child;
13606 child_die && child_die->tag;
436c571c 13607 child_die = child_die->sibling)
96408a79 13608 {
96408a79 13609 struct call_site_parameter *parameter;
1788b2d3 13610 struct attribute *loc, *origin;
96408a79 13611
216f72a1
JK
13612 if (child_die->tag != DW_TAG_call_site_parameter
13613 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13614 {
13615 /* Already printed the complaint above. */
13616 continue;
13617 }
13618
13619 gdb_assert (call_site->parameter_count < nparams);
13620 parameter = &call_site->parameter[call_site->parameter_count];
13621
1788b2d3
JK
13622 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13623 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13624 register is contained in DW_AT_call_value. */
96408a79 13625
24c5c679 13626 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13627 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13628 if (origin == NULL)
13629 {
13630 /* This was a pre-DWARF-5 GNU extension alias
13631 for DW_AT_call_parameter. */
13632 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13633 }
cd6c91b4 13634 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13635 {
1788b2d3 13636 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13637
0826b30a 13638 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13639 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13640 {
13641 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13642 binding can be done only inside one CU. Such referenced DIE
13643 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13644 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13645 "DW_TAG_call_site child DIE %s [in module %s]"),
13646 sect_offset_str (child_die->sect_off),
9c541725 13647 objfile_name (objfile));
d76b7dbc
JK
13648 continue;
13649 }
9c541725
PA
13650 parameter->u.param_cu_off
13651 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13652 }
4fc6c0d5 13653 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13654 {
b98664d3 13655 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13656 "DW_TAG_call_site child DIE %s [in module %s]"),
13657 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13658 continue;
13659 }
24c5c679 13660 else
96408a79 13661 {
24c5c679
JK
13662 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13663 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13664 if (parameter->u.dwarf_reg != -1)
13665 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13666 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13667 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13668 &parameter->u.fb_offset))
13669 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13670 else
13671 {
b98664d3 13672 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13673 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13674 "DW_TAG_call_site child DIE %s "
24c5c679 13675 "[in module %s]"),
9d8780f0 13676 sect_offset_str (child_die->sect_off),
9c541725 13677 objfile_name (objfile));
24c5c679
JK
13678 continue;
13679 }
96408a79
SA
13680 }
13681
216f72a1
JK
13682 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13683 if (attr == NULL)
13684 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13685 if (attr == NULL || !attr->form_is_block ())
96408a79 13686 {
b98664d3 13687 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13688 "DW_TAG_call_site child DIE %s [in module %s]"),
13689 sect_offset_str (child_die->sect_off),
9c541725 13690 objfile_name (objfile));
96408a79
SA
13691 continue;
13692 }
13693 parameter->value = DW_BLOCK (attr)->data;
13694 parameter->value_size = DW_BLOCK (attr)->size;
13695
13696 /* Parameters are not pre-cleared by memset above. */
13697 parameter->data_value = NULL;
13698 parameter->data_value_size = 0;
13699 call_site->parameter_count++;
13700
216f72a1
JK
13701 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13702 if (attr == NULL)
13703 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13704 if (attr != nullptr)
96408a79 13705 {
4fc6c0d5 13706 if (!attr->form_is_block ())
b98664d3 13707 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13708 "DW_TAG_call_site child DIE %s [in module %s]"),
13709 sect_offset_str (child_die->sect_off),
9c541725 13710 objfile_name (objfile));
96408a79
SA
13711 else
13712 {
13713 parameter->data_value = DW_BLOCK (attr)->data;
13714 parameter->data_value_size = DW_BLOCK (attr)->size;
13715 }
13716 }
13717 }
13718}
13719
71a3c369
TT
13720/* Helper function for read_variable. If DIE represents a virtual
13721 table, then return the type of the concrete object that is
13722 associated with the virtual table. Otherwise, return NULL. */
13723
13724static struct type *
13725rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13726{
13727 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13728 if (attr == NULL)
13729 return NULL;
13730
13731 /* Find the type DIE. */
13732 struct die_info *type_die = NULL;
13733 struct dwarf2_cu *type_cu = cu;
13734
cd6c91b4 13735 if (attr->form_is_ref ())
71a3c369
TT
13736 type_die = follow_die_ref (die, attr, &type_cu);
13737 if (type_die == NULL)
13738 return NULL;
13739
13740 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13741 return NULL;
13742 return die_containing_type (type_die, type_cu);
13743}
13744
13745/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13746
13747static void
13748read_variable (struct die_info *die, struct dwarf2_cu *cu)
13749{
13750 struct rust_vtable_symbol *storage = NULL;
13751
13752 if (cu->language == language_rust)
13753 {
13754 struct type *containing_type = rust_containing_type (die, cu);
13755
13756 if (containing_type != NULL)
13757 {
5e22e966 13758 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13759
8c14c3a3 13760 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13761 storage->concrete_type = containing_type;
cf724bc9 13762 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13763 }
13764 }
13765
e4a62c65
TV
13766 struct symbol *res = new_symbol (die, NULL, cu, storage);
13767 struct attribute *abstract_origin
13768 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13769 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13770 if (res == NULL && loc && abstract_origin)
13771 {
13772 /* We have a variable without a name, but with a location and an abstract
13773 origin. This may be a concrete instance of an abstract variable
13774 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13775 later. */
13776 struct dwarf2_cu *origin_cu = cu;
13777 struct die_info *origin_die
13778 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13779 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13780 per_objfile->per_bfd->abstract_to_concrete
13781 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13782 }
71a3c369
TT
13783}
13784
43988095
JK
13785/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13786 reading .debug_rnglists.
13787 Callback's type should be:
13788 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13789 Return true if the attributes are present and valid, otherwise,
13790 return false. */
13791
13792template <typename Callback>
13793static bool
13794dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13795 Callback &&callback)
13796{
976ca316
SM
13797 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13798 struct objfile *objfile = per_objfile->objfile;
43988095 13799 bfd *obfd = objfile->obfd;
43988095 13800 /* Base address selection entry. */
2b24b6e4 13801 gdb::optional<CORE_ADDR> base;
43988095 13802 const gdb_byte *buffer;
43988095
JK
13803 CORE_ADDR baseaddr;
13804 bool overflow = false;
13805
43988095
JK
13806 base = cu->base_address;
13807
976ca316
SM
13808 per_objfile->per_bfd->rnglists.read (objfile);
13809 if (offset >= per_objfile->per_bfd->rnglists.size)
43988095 13810 {
b98664d3 13811 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13812 offset);
13813 return false;
13814 }
976ca316 13815 buffer = per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13816
b3b3bada 13817 baseaddr = objfile->text_section_offset ();
43988095
JK
13818
13819 while (1)
13820 {
7814882a
JK
13821 /* Initialize it due to a false compiler warning. */
13822 CORE_ADDR range_beginning = 0, range_end = 0;
976ca316
SM
13823 const gdb_byte *buf_end = (per_objfile->per_bfd->rnglists.buffer
13824 + per_objfile->per_bfd->rnglists.size);
43988095
JK
13825 unsigned int bytes_read;
13826
13827 if (buffer == buf_end)
13828 {
13829 overflow = true;
13830 break;
13831 }
13832 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13833 switch (rlet)
13834 {
13835 case DW_RLE_end_of_list:
13836 break;
13837 case DW_RLE_base_address:
13838 if (buffer + cu->header.addr_size > buf_end)
13839 {
13840 overflow = true;
13841 break;
13842 }
c8a7a66f 13843 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13844 buffer += bytes_read;
13845 break;
13846 case DW_RLE_start_length:
13847 if (buffer + cu->header.addr_size > buf_end)
13848 {
13849 overflow = true;
13850 break;
13851 }
c8a7a66f
TT
13852 range_beginning = cu->header.read_address (obfd, buffer,
13853 &bytes_read);
43988095
JK
13854 buffer += bytes_read;
13855 range_end = (range_beginning
13856 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13857 buffer += bytes_read;
13858 if (buffer > buf_end)
13859 {
13860 overflow = true;
13861 break;
13862 }
13863 break;
13864 case DW_RLE_offset_pair:
13865 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13866 buffer += bytes_read;
13867 if (buffer > buf_end)
13868 {
13869 overflow = true;
13870 break;
13871 }
13872 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13873 buffer += bytes_read;
13874 if (buffer > buf_end)
13875 {
13876 overflow = true;
13877 break;
13878 }
13879 break;
13880 case DW_RLE_start_end:
13881 if (buffer + 2 * cu->header.addr_size > buf_end)
13882 {
13883 overflow = true;
13884 break;
13885 }
c8a7a66f
TT
13886 range_beginning = cu->header.read_address (obfd, buffer,
13887 &bytes_read);
43988095 13888 buffer += bytes_read;
c8a7a66f 13889 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13890 buffer += bytes_read;
13891 break;
13892 default:
b98664d3 13893 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13894 return false;
13895 }
13896 if (rlet == DW_RLE_end_of_list || overflow)
13897 break;
13898 if (rlet == DW_RLE_base_address)
13899 continue;
13900
2b24b6e4 13901 if (!base.has_value ())
43988095
JK
13902 {
13903 /* We have no valid base address for the ranges
13904 data. */
b98664d3 13905 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13906 return false;
13907 }
13908
13909 if (range_beginning > range_end)
13910 {
13911 /* Inverted range entries are invalid. */
b98664d3 13912 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13913 return false;
13914 }
13915
13916 /* Empty range entries have no effect. */
13917 if (range_beginning == range_end)
13918 continue;
13919
2b24b6e4
TT
13920 range_beginning += *base;
13921 range_end += *base;
43988095
JK
13922
13923 /* A not-uncommon case of bad debug info.
13924 Don't pollute the addrmap with bad data. */
13925 if (range_beginning + baseaddr == 0
976ca316 13926 && !per_objfile->per_bfd->has_section_at_zero)
43988095 13927 {
b98664d3 13928 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13929 " [in module %s]"), objfile_name (objfile));
13930 continue;
13931 }
13932
13933 callback (range_beginning, range_end);
13934 }
13935
13936 if (overflow)
13937 {
b98664d3 13938 complaint (_("Offset %d is not terminated "
43988095
JK
13939 "for DW_AT_ranges attribute"),
13940 offset);
13941 return false;
13942 }
13943
13944 return true;
13945}
13946
13947/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13948 Callback's type should be:
13949 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13950 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13951
43988095 13952template <typename Callback>
43039443 13953static int
5f46c5a5 13954dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13955 Callback &&callback)
43039443 13956{
5e22e966
SM
13957 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13958 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13959 struct comp_unit_head *cu_header = &cu->header;
13960 bfd *obfd = objfile->obfd;
13961 unsigned int addr_size = cu_header->addr_size;
13962 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13963 /* Base address selection entry. */
2b24b6e4 13964 gdb::optional<CORE_ADDR> base;
43039443 13965 unsigned int dummy;
d521ce57 13966 const gdb_byte *buffer;
ff013f42 13967 CORE_ADDR baseaddr;
43039443 13968
43988095
JK
13969 if (cu_header->version >= 5)
13970 return dwarf2_rnglists_process (offset, cu, callback);
13971
d00adf39 13972 base = cu->base_address;
43039443 13973
5e22e966
SM
13974 per_objfile->per_bfd->ranges.read (objfile);
13975 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13976 {
b98664d3 13977 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13978 offset);
13979 return 0;
13980 }
5e22e966 13981 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13982
b3b3bada 13983 baseaddr = objfile->text_section_offset ();
ff013f42 13984
43039443
JK
13985 while (1)
13986 {
13987 CORE_ADDR range_beginning, range_end;
13988
c8a7a66f 13989 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13990 buffer += addr_size;
c8a7a66f 13991 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13992 buffer += addr_size;
13993 offset += 2 * addr_size;
13994
13995 /* An end of list marker is a pair of zero addresses. */
13996 if (range_beginning == 0 && range_end == 0)
13997 /* Found the end of list entry. */
13998 break;
13999
14000 /* Each base address selection entry is a pair of 2 values.
14001 The first is the largest possible address, the second is
14002 the base address. Check for a base address here. */
14003 if ((range_beginning & mask) == mask)
14004 {
28d2bfb9
AB
14005 /* If we found the largest possible address, then we already
14006 have the base address in range_end. */
14007 base = range_end;
43039443
JK
14008 continue;
14009 }
14010
2b24b6e4 14011 if (!base.has_value ())
43039443
JK
14012 {
14013 /* We have no valid base address for the ranges
14014 data. */
b98664d3 14015 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14016 return 0;
14017 }
14018
9277c30c
UW
14019 if (range_beginning > range_end)
14020 {
14021 /* Inverted range entries are invalid. */
b98664d3 14022 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14023 return 0;
14024 }
14025
14026 /* Empty range entries have no effect. */
14027 if (range_beginning == range_end)
14028 continue;
14029
2b24b6e4
TT
14030 range_beginning += *base;
14031 range_end += *base;
43039443 14032
01093045
DE
14033 /* A not-uncommon case of bad debug info.
14034 Don't pollute the addrmap with bad data. */
14035 if (range_beginning + baseaddr == 0
5e22e966 14036 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14037 {
b98664d3 14038 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14039 " [in module %s]"), objfile_name (objfile));
01093045
DE
14040 continue;
14041 }
14042
5f46c5a5
JK
14043 callback (range_beginning, range_end);
14044 }
14045
14046 return 1;
14047}
14048
14049/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14050 Return 1 if the attributes are present and valid, otherwise, return 0.
14051 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14052
14053static int
14054dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14055 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14056 dwarf2_psymtab *ranges_pst)
5f46c5a5 14057{
5e22e966 14058 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14059 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14060 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14061 int low_set = 0;
14062 CORE_ADDR low = 0;
14063 CORE_ADDR high = 0;
14064 int retval;
14065
14066 retval = dwarf2_ranges_process (offset, cu,
14067 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14068 {
9277c30c 14069 if (ranges_pst != NULL)
3e29f34a
MR
14070 {
14071 CORE_ADDR lowpc;
14072 CORE_ADDR highpc;
14073
79748972
TT
14074 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14075 range_beginning + baseaddr)
14076 - baseaddr);
14077 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14078 range_end + baseaddr)
14079 - baseaddr);
d320c2b5
TT
14080 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14081 lowpc, highpc - 1, ranges_pst);
3e29f34a 14082 }
ff013f42 14083
43039443
JK
14084 /* FIXME: This is recording everything as a low-high
14085 segment of consecutive addresses. We should have a
14086 data structure for discontiguous block ranges
14087 instead. */
14088 if (! low_set)
14089 {
14090 low = range_beginning;
14091 high = range_end;
14092 low_set = 1;
14093 }
14094 else
14095 {
14096 if (range_beginning < low)
14097 low = range_beginning;
14098 if (range_end > high)
14099 high = range_end;
14100 }
5f46c5a5
JK
14101 });
14102 if (!retval)
14103 return 0;
43039443
JK
14104
14105 if (! low_set)
14106 /* If the first entry is an end-of-list marker, the range
14107 describes an empty scope, i.e. no instructions. */
14108 return 0;
14109
14110 if (low_return)
14111 *low_return = low;
14112 if (high_return)
14113 *high_return = high;
14114 return 1;
14115}
14116
3a2b436a
JK
14117/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14118 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14119 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14120
3a2b436a 14121static enum pc_bounds_kind
af34e669 14122dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14123 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14124 dwarf2_psymtab *pst)
c906108c 14125{
976ca316 14126 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14127 struct attribute *attr;
91da1414 14128 struct attribute *attr_high;
af34e669
DJ
14129 CORE_ADDR low = 0;
14130 CORE_ADDR high = 0;
e385593e 14131 enum pc_bounds_kind ret;
c906108c 14132
91da1414
MW
14133 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14134 if (attr_high)
af34e669 14135 {
e142c38c 14136 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14137 if (attr != nullptr)
91da1414 14138 {
cd6c91b4
TT
14139 low = attr->value_as_address ();
14140 high = attr_high->value_as_address ();
14141 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14142 high += low;
91da1414 14143 }
af34e669
DJ
14144 else
14145 /* Found high w/o low attribute. */
e385593e 14146 return PC_BOUNDS_INVALID;
af34e669
DJ
14147
14148 /* Found consecutive range of addresses. */
3a2b436a 14149 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14150 }
c906108c 14151 else
af34e669 14152 {
e142c38c 14153 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14154 if (attr != NULL)
14155 {
18a8505e 14156 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14157 We take advantage of the fact that DW_AT_ranges does not appear
14158 in DW_TAG_compile_unit of DWO files. */
14159 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14160 unsigned int ranges_offset = (DW_UNSND (attr)
14161 + (need_ranges_base
14162 ? cu->ranges_base
14163 : 0));
2e3cf129 14164
af34e669 14165 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14166 .debug_ranges section. */
2e3cf129 14167 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14168 return PC_BOUNDS_INVALID;
43039443 14169 /* Found discontinuous range of addresses. */
3a2b436a 14170 ret = PC_BOUNDS_RANGES;
af34e669 14171 }
e385593e
JK
14172 else
14173 return PC_BOUNDS_NOT_PRESENT;
af34e669 14174 }
c906108c 14175
48fbe735 14176 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14177 if (high <= low)
e385593e 14178 return PC_BOUNDS_INVALID;
c906108c
SS
14179
14180 /* When using the GNU linker, .gnu.linkonce. sections are used to
14181 eliminate duplicate copies of functions and vtables and such.
14182 The linker will arbitrarily choose one and discard the others.
14183 The AT_*_pc values for such functions refer to local labels in
14184 these sections. If the section from that file was discarded, the
14185 labels are not in the output, so the relocs get a value of 0.
14186 If this is a discarded function, mark the pc bounds as invalid,
14187 so that GDB will ignore it. */
976ca316 14188 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14189 return PC_BOUNDS_INVALID;
c906108c
SS
14190
14191 *lowpc = low;
96408a79
SA
14192 if (highpc)
14193 *highpc = high;
af34e669 14194 return ret;
c906108c
SS
14195}
14196
b084d499
JB
14197/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14198 its low and high PC addresses. Do nothing if these addresses could not
14199 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14200 and HIGHPC to the high address if greater than HIGHPC. */
14201
14202static void
14203dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14204 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14205 struct dwarf2_cu *cu)
14206{
14207 CORE_ADDR low, high;
14208 struct die_info *child = die->child;
14209
e385593e 14210 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14211 {
325fac50
PA
14212 *lowpc = std::min (*lowpc, low);
14213 *highpc = std::max (*highpc, high);
b084d499
JB
14214 }
14215
14216 /* If the language does not allow nested subprograms (either inside
14217 subprograms or lexical blocks), we're done. */
14218 if (cu->language != language_ada)
14219 return;
6e70227d 14220
b084d499
JB
14221 /* Check all the children of the given DIE. If it contains nested
14222 subprograms, then check their pc bounds. Likewise, we need to
14223 check lexical blocks as well, as they may also contain subprogram
14224 definitions. */
14225 while (child && child->tag)
14226 {
14227 if (child->tag == DW_TAG_subprogram
14228 || child->tag == DW_TAG_lexical_block)
14229 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14230 child = child->sibling;
b084d499
JB
14231 }
14232}
14233
fae299cd
DC
14234/* Get the low and high pc's represented by the scope DIE, and store
14235 them in *LOWPC and *HIGHPC. If the correct values can't be
14236 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14237
14238static void
14239get_scope_pc_bounds (struct die_info *die,
14240 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14241 struct dwarf2_cu *cu)
14242{
14243 CORE_ADDR best_low = (CORE_ADDR) -1;
14244 CORE_ADDR best_high = (CORE_ADDR) 0;
14245 CORE_ADDR current_low, current_high;
14246
3a2b436a 14247 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14248 >= PC_BOUNDS_RANGES)
fae299cd
DC
14249 {
14250 best_low = current_low;
14251 best_high = current_high;
14252 }
14253 else
14254 {
14255 struct die_info *child = die->child;
14256
14257 while (child && child->tag)
14258 {
14259 switch (child->tag) {
14260 case DW_TAG_subprogram:
b084d499 14261 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14262 break;
14263 case DW_TAG_namespace:
f55ee35c 14264 case DW_TAG_module:
fae299cd
DC
14265 /* FIXME: carlton/2004-01-16: Should we do this for
14266 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14267 that current GCC's always emit the DIEs corresponding
14268 to definitions of methods of classes as children of a
14269 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14270 the DIEs giving the declarations, which could be
14271 anywhere). But I don't see any reason why the
14272 standards says that they have to be there. */
14273 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14274
14275 if (current_low != ((CORE_ADDR) -1))
14276 {
325fac50
PA
14277 best_low = std::min (best_low, current_low);
14278 best_high = std::max (best_high, current_high);
fae299cd
DC
14279 }
14280 break;
14281 default:
0963b4bd 14282 /* Ignore. */
fae299cd
DC
14283 break;
14284 }
14285
436c571c 14286 child = child->sibling;
fae299cd
DC
14287 }
14288 }
14289
14290 *lowpc = best_low;
14291 *highpc = best_high;
14292}
14293
801e3a5b
JB
14294/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14295 in DIE. */
380bca97 14296
801e3a5b
JB
14297static void
14298dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14299 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14300{
5e22e966 14301 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14302 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14303 struct attribute *attr;
91da1414 14304 struct attribute *attr_high;
801e3a5b 14305
91da1414
MW
14306 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14307 if (attr_high)
801e3a5b 14308 {
801e3a5b 14309 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14310 if (attr != nullptr)
801e3a5b 14311 {
cd6c91b4
TT
14312 CORE_ADDR low = attr->value_as_address ();
14313 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14314
cd6c91b4 14315 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14316 high += low;
9a619af0 14317
3e29f34a
MR
14318 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14319 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14320 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14321 }
14322 }
14323
14324 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14325 if (attr != nullptr)
801e3a5b 14326 {
18a8505e 14327 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14328 We take advantage of the fact that DW_AT_ranges does not appear
14329 in DW_TAG_compile_unit of DWO files. */
14330 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14331
14332 /* The value of the DW_AT_ranges attribute is the offset of the
14333 address range list in the .debug_ranges section. */
ab435259
DE
14334 unsigned long offset = (DW_UNSND (attr)
14335 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14336
2d5f09ec 14337 std::vector<blockrange> blockvec;
5f46c5a5
JK
14338 dwarf2_ranges_process (offset, cu,
14339 [&] (CORE_ADDR start, CORE_ADDR end)
14340 {
58fdfd2c
JK
14341 start += baseaddr;
14342 end += baseaddr;
5f46c5a5
JK
14343 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14344 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14345 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14346 blockvec.emplace_back (start, end);
5f46c5a5 14347 });
2d5f09ec
KB
14348
14349 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14350 }
14351}
14352
685b1105
JK
14353/* Check whether the producer field indicates either of GCC < 4.6, or the
14354 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14355
685b1105
JK
14356static void
14357check_producer (struct dwarf2_cu *cu)
60d5a603 14358{
38360086 14359 int major, minor;
60d5a603
JK
14360
14361 if (cu->producer == NULL)
14362 {
14363 /* For unknown compilers expect their behavior is DWARF version
14364 compliant.
14365
14366 GCC started to support .debug_types sections by -gdwarf-4 since
14367 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14368 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14369 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14370 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14371 }
b1ffba5a 14372 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14373 {
38360086
MW
14374 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14375 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14376 }
5230b05a 14377 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14378 {
14379 cu->producer_is_icc = true;
14380 cu->producer_is_icc_lt_14 = major < 14;
14381 }
c258c396
JD
14382 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14383 cu->producer_is_codewarrior = true;
685b1105
JK
14384 else
14385 {
14386 /* For other non-GCC compilers, expect their behavior is DWARF version
14387 compliant. */
60d5a603
JK
14388 }
14389
9068261f 14390 cu->checked_producer = true;
685b1105 14391}
ba919b58 14392
685b1105
JK
14393/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14394 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14395 during 4.6.0 experimental. */
14396
9068261f 14397static bool
685b1105
JK
14398producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14399{
14400 if (!cu->checked_producer)
14401 check_producer (cu);
14402
14403 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14404}
14405
c258c396
JD
14406
14407/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14408 with incorrect is_stmt attributes. */
14409
14410static bool
14411producer_is_codewarrior (struct dwarf2_cu *cu)
14412{
14413 if (!cu->checked_producer)
14414 check_producer (cu);
14415
14416 return cu->producer_is_codewarrior;
14417}
14418
405feb71 14419/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14420 DW_AT_accessibility. */
14421
14422static enum dwarf_access_attribute
14423dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14424{
14425 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14426 {
14427 /* The default DWARF 2 accessibility for members is public, the default
14428 accessibility for inheritance is private. */
14429
14430 if (die->tag != DW_TAG_inheritance)
14431 return DW_ACCESS_public;
14432 else
14433 return DW_ACCESS_private;
14434 }
14435 else
14436 {
14437 /* DWARF 3+ defines the default accessibility a different way. The same
14438 rules apply now for DW_TAG_inheritance as for the members and it only
14439 depends on the container kind. */
14440
14441 if (die->parent->tag == DW_TAG_class_type)
14442 return DW_ACCESS_private;
14443 else
14444 return DW_ACCESS_public;
14445 }
14446}
14447
74ac6d43
TT
14448/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14449 offset. If the attribute was not found return 0, otherwise return
14450 1. If it was found but could not properly be handled, set *OFFSET
14451 to 0. */
14452
14453static int
14454handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14455 LONGEST *offset)
14456{
14457 struct attribute *attr;
14458
14459 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14460 if (attr != NULL)
14461 {
14462 *offset = 0;
14463
14464 /* Note that we do not check for a section offset first here.
14465 This is because DW_AT_data_member_location is new in DWARF 4,
14466 so if we see it, we can assume that a constant form is really
14467 a constant and not a section offset. */
cd6c91b4 14468 if (attr->form_is_constant ())
0826b30a 14469 *offset = attr->constant_value (0);
cd6c91b4 14470 else if (attr->form_is_section_offset ())
74ac6d43 14471 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14472 else if (attr->form_is_block ())
74ac6d43
TT
14473 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14474 else
14475 dwarf2_complex_location_expr_complaint ();
14476
14477 return 1;
14478 }
14479
14480 return 0;
14481}
14482
7d79de9a
TT
14483/* Look for DW_AT_data_member_location and store the results in FIELD. */
14484
14485static void
14486handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14487 struct field *field)
14488{
14489 struct attribute *attr;
14490
14491 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14492 if (attr != NULL)
14493 {
14494 if (attr->form_is_constant ())
14495 {
14496 LONGEST offset = attr->constant_value (0);
14497 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14498 }
14499 else if (attr->form_is_section_offset ())
14500 dwarf2_complex_location_expr_complaint ();
14501 else if (attr->form_is_block ())
14502 {
14503 bool handled;
14504 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14505 if (handled)
14506 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14507 else
14508 {
5e22e966
SM
14509 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14510 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14511 struct dwarf2_locexpr_baton *dlbaton
14512 = XOBNEW (&objfile->objfile_obstack,
14513 struct dwarf2_locexpr_baton);
14514 dlbaton->data = DW_BLOCK (attr)->data;
14515 dlbaton->size = DW_BLOCK (attr)->size;
14516 /* When using this baton, we want to compute the address
14517 of the field, not the value. This is why
14518 is_reference is set to false here. */
14519 dlbaton->is_reference = false;
5e22e966 14520 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14521 dlbaton->per_cu = cu->per_cu;
14522
14523 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14524 }
14525 }
14526 else
14527 dwarf2_complex_location_expr_complaint ();
14528 }
14529}
14530
c906108c
SS
14531/* Add an aggregate field to the field list. */
14532
14533static void
107d2387 14534dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14535 struct dwarf2_cu *cu)
6e70227d 14536{
5e22e966 14537 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14538 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14539 struct nextfield *new_field;
14540 struct attribute *attr;
14541 struct field *fp;
15d034d0 14542 const char *fieldname = "";
c906108c 14543
7d0ccb61
DJ
14544 if (die->tag == DW_TAG_inheritance)
14545 {
be2daae6
TT
14546 fip->baseclasses.emplace_back ();
14547 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14548 }
14549 else
14550 {
be2daae6
TT
14551 fip->fields.emplace_back ();
14552 new_field = &fip->fields.back ();
7d0ccb61 14553 }
be2daae6 14554
9c6a1327
TT
14555 new_field->offset = die->sect_off;
14556
e142c38c 14557 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14558 if (attr != nullptr)
c906108c 14559 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14560 else
14561 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14562 if (new_field->accessibility != DW_ACCESS_public)
14563 fip->non_public_fields = 1;
60d5a603 14564
e142c38c 14565 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14566 if (attr != nullptr)
c906108c 14567 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14568 else
14569 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14570
14571 fp = &new_field->field;
a9a9bd0f 14572
e142c38c 14573 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14574 {
a9a9bd0f 14575 /* Data member other than a C++ static data member. */
6e70227d 14576
c906108c 14577 /* Get type of field. */
5d14b6e5 14578 fp->set_type (die_type (die, cu));
c906108c 14579
d6a843b5 14580 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14581
c906108c 14582 /* Get bit size of field (zero if none). */
e142c38c 14583 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14584 if (attr != nullptr)
c906108c
SS
14585 {
14586 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14587 }
14588 else
14589 {
14590 FIELD_BITSIZE (*fp) = 0;
14591 }
14592
14593 /* Get bit offset of field. */
7d79de9a 14594 handle_data_member_location (die, cu, fp);
e142c38c 14595 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14596 if (attr != nullptr)
c906108c 14597 {
d5a22e77 14598 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14599 {
14600 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14601 additional bit offset from the MSB of the containing
14602 anonymous object to the MSB of the field. We don't
14603 have to do anything special since we don't need to
14604 know the size of the anonymous object. */
f41f5e61 14605 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14606 }
14607 else
14608 {
14609 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14610 MSB of the anonymous object, subtract off the number of
14611 bits from the MSB of the field to the MSB of the
14612 object, and then subtract off the number of bits of
14613 the field itself. The result is the bit offset of
14614 the LSB of the field. */
c906108c
SS
14615 int anonymous_size;
14616 int bit_offset = DW_UNSND (attr);
14617
e142c38c 14618 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14619 if (attr != nullptr)
c906108c
SS
14620 {
14621 /* The size of the anonymous object containing
14622 the bit field is explicit, so use the
14623 indicated size (in bytes). */
14624 anonymous_size = DW_UNSND (attr);
14625 }
14626 else
14627 {
14628 /* The size of the anonymous object containing
14629 the bit field must be inferred from the type
14630 attribute of the data member containing the
14631 bit field. */
5d14b6e5 14632 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14633 }
f41f5e61
PA
14634 SET_FIELD_BITPOS (*fp,
14635 (FIELD_BITPOS (*fp)
14636 + anonymous_size * bits_per_byte
14637 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14638 }
14639 }
da5b30da
AA
14640 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14641 if (attr != NULL)
14642 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14643 + attr->constant_value (0)));
c906108c
SS
14644
14645 /* Get name of field. */
39cbfefa
DJ
14646 fieldname = dwarf2_name (die, cu);
14647 if (fieldname == NULL)
14648 fieldname = "";
d8151005
DJ
14649
14650 /* The name is already allocated along with this objfile, so we don't
14651 need to duplicate it for the type. */
14652 fp->name = fieldname;
c906108c
SS
14653
14654 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14655 pointer or virtual base class pointer) to private. */
e142c38c 14656 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14657 {
d48cc9dd 14658 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14659 new_field->accessibility = DW_ACCESS_private;
14660 fip->non_public_fields = 1;
14661 }
14662 }
a9a9bd0f 14663 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14664 {
a9a9bd0f
DC
14665 /* C++ static member. */
14666
14667 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14668 is a declaration, but all versions of G++ as of this writing
14669 (so through at least 3.2.1) incorrectly generate
14670 DW_TAG_variable tags. */
6e70227d 14671
ff355380 14672 const char *physname;
c906108c 14673
a9a9bd0f 14674 /* Get name of field. */
39cbfefa
DJ
14675 fieldname = dwarf2_name (die, cu);
14676 if (fieldname == NULL)
c906108c
SS
14677 return;
14678
254e6b9e 14679 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14680 if (attr
14681 /* Only create a symbol if this is an external value.
14682 new_symbol checks this and puts the value in the global symbol
14683 table, which we want. If it is not external, new_symbol
14684 will try to put the value in cu->list_in_scope which is wrong. */
14685 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14686 {
14687 /* A static const member, not much different than an enum as far as
14688 we're concerned, except that we can support more types. */
14689 new_symbol (die, NULL, cu);
14690 }
14691
2df3850c 14692 /* Get physical name. */
ff355380 14693 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14694
d8151005
DJ
14695 /* The name is already allocated along with this objfile, so we don't
14696 need to duplicate it for the type. */
14697 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14698 fp->set_type (die_type (die, cu));
d8151005 14699 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14700 }
14701 else if (die->tag == DW_TAG_inheritance)
14702 {
74ac6d43 14703 /* C++ base class field. */
7d79de9a 14704 handle_data_member_location (die, cu, fp);
c906108c 14705 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14706 fp->set_type (die_type (die, cu));
14707 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14708 }
2ddeaf8a
TT
14709 else
14710 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14711}
14712
883fd55a
KS
14713/* Can the type given by DIE define another type? */
14714
14715static bool
14716type_can_define_types (const struct die_info *die)
14717{
14718 switch (die->tag)
14719 {
14720 case DW_TAG_typedef:
14721 case DW_TAG_class_type:
14722 case DW_TAG_structure_type:
14723 case DW_TAG_union_type:
14724 case DW_TAG_enumeration_type:
14725 return true;
14726
14727 default:
14728 return false;
14729 }
14730}
14731
14732/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14733
14734static void
883fd55a
KS
14735dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14736 struct dwarf2_cu *cu)
6e70227d 14737{
be2daae6
TT
14738 struct decl_field fp;
14739 memset (&fp, 0, sizeof (fp));
98751a41 14740
883fd55a 14741 gdb_assert (type_can_define_types (die));
98751a41 14742
883fd55a 14743 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14744 fp.name = dwarf2_name (die, cu);
14745 fp.type = read_type_die (die, cu);
98751a41 14746
c191a687
KS
14747 /* Save accessibility. */
14748 enum dwarf_access_attribute accessibility;
14749 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14750 if (attr != NULL)
14751 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14752 else
14753 accessibility = dwarf2_default_access_attribute (die, cu);
14754 switch (accessibility)
14755 {
14756 case DW_ACCESS_public:
14757 /* The assumed value if neither private nor protected. */
14758 break;
14759 case DW_ACCESS_private:
be2daae6 14760 fp.is_private = 1;
c191a687
KS
14761 break;
14762 case DW_ACCESS_protected:
be2daae6 14763 fp.is_protected = 1;
c191a687
KS
14764 break;
14765 default:
b98664d3 14766 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14767 }
14768
883fd55a 14769 if (die->tag == DW_TAG_typedef)
be2daae6 14770 fip->typedef_field_list.push_back (fp);
883fd55a 14771 else
be2daae6 14772 fip->nested_types_list.push_back (fp);
98751a41
JK
14773}
14774
9c6a1327
TT
14775/* A convenience typedef that's used when finding the discriminant
14776 field for a variant part. */
1b95cdb7
SM
14777typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14778 offset_map_type;
9c6a1327
TT
14779
14780/* Compute the discriminant range for a given variant. OBSTACK is
14781 where the results will be stored. VARIANT is the variant to
14782 process. IS_UNSIGNED indicates whether the discriminant is signed
14783 or unsigned. */
14784
14785static const gdb::array_view<discriminant_range>
14786convert_variant_range (struct obstack *obstack, const variant_field &variant,
14787 bool is_unsigned)
14788{
14789 std::vector<discriminant_range> ranges;
14790
14791 if (variant.default_branch)
14792 return {};
14793
14794 if (variant.discr_list_data == nullptr)
14795 {
14796 discriminant_range r
14797 = {variant.discriminant_value, variant.discriminant_value};
14798 ranges.push_back (r);
14799 }
14800 else
14801 {
14802 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14803 variant.discr_list_data->size);
14804 while (!data.empty ())
14805 {
14806 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14807 {
14808 complaint (_("invalid discriminant marker: %d"), data[0]);
14809 break;
14810 }
14811 bool is_range = data[0] == DW_DSC_range;
14812 data = data.slice (1);
14813
14814 ULONGEST low, high;
14815 unsigned int bytes_read;
14816
14817 if (data.empty ())
14818 {
14819 complaint (_("DW_AT_discr_list missing low value"));
14820 break;
14821 }
14822 if (is_unsigned)
14823 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14824 else
14825 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14826 &bytes_read);
14827 data = data.slice (bytes_read);
14828
14829 if (is_range)
14830 {
14831 if (data.empty ())
14832 {
14833 complaint (_("DW_AT_discr_list missing high value"));
14834 break;
14835 }
14836 if (is_unsigned)
14837 high = read_unsigned_leb128 (nullptr, data.data (),
14838 &bytes_read);
14839 else
14840 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14841 &bytes_read);
14842 data = data.slice (bytes_read);
14843 }
14844 else
14845 high = low;
14846
14847 ranges.push_back ({ low, high });
14848 }
14849 }
14850
14851 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14852 ranges.size ());
14853 std::copy (ranges.begin (), ranges.end (), result);
14854 return gdb::array_view<discriminant_range> (result, ranges.size ());
14855}
14856
14857static const gdb::array_view<variant_part> create_variant_parts
14858 (struct obstack *obstack,
14859 const offset_map_type &offset_map,
14860 struct field_info *fi,
14861 const std::vector<variant_part_builder> &variant_parts);
14862
14863/* Fill in a "struct variant" for a given variant field. RESULT is
14864 the variant to fill in. OBSTACK is where any needed allocations
14865 will be done. OFFSET_MAP holds the mapping from section offsets to
14866 fields for the type. FI describes the fields of the type we're
14867 processing. FIELD is the variant field we're converting. */
14868
14869static void
14870create_one_variant (variant &result, struct obstack *obstack,
14871 const offset_map_type &offset_map,
14872 struct field_info *fi, const variant_field &field)
14873{
14874 result.discriminants = convert_variant_range (obstack, field, false);
14875 result.first_field = field.first_field + fi->baseclasses.size ();
14876 result.last_field = field.last_field + fi->baseclasses.size ();
14877 result.parts = create_variant_parts (obstack, offset_map, fi,
14878 field.variant_parts);
14879}
14880
14881/* Fill in a "struct variant_part" for a given variant part. RESULT
14882 is the variant part to fill in. OBSTACK is where any needed
14883 allocations will be done. OFFSET_MAP holds the mapping from
14884 section offsets to fields for the type. FI describes the fields of
14885 the type we're processing. BUILDER is the variant part to be
14886 converted. */
14887
14888static void
14889create_one_variant_part (variant_part &result,
14890 struct obstack *obstack,
14891 const offset_map_type &offset_map,
14892 struct field_info *fi,
14893 const variant_part_builder &builder)
14894{
14895 auto iter = offset_map.find (builder.discriminant_offset);
14896 if (iter == offset_map.end ())
14897 {
14898 result.discriminant_index = -1;
14899 /* Doesn't matter. */
14900 result.is_unsigned = false;
14901 }
14902 else
14903 {
14904 result.discriminant_index = iter->second;
14905 result.is_unsigned
b6cdac4b 14906 = TYPE_UNSIGNED (fi->fields[result.discriminant_index].field.type ());
9c6a1327
TT
14907 }
14908
14909 size_t n = builder.variants.size ();
14910 variant *output = new (obstack) variant[n];
14911 for (size_t i = 0; i < n; ++i)
14912 create_one_variant (output[i], obstack, offset_map, fi,
14913 builder.variants[i]);
14914
14915 result.variants = gdb::array_view<variant> (output, n);
14916}
14917
14918/* Create a vector of variant parts that can be attached to a type.
14919 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14920 holds the mapping from section offsets to fields for the type. FI
14921 describes the fields of the type we're processing. VARIANT_PARTS
14922 is the vector to convert. */
14923
14924static const gdb::array_view<variant_part>
14925create_variant_parts (struct obstack *obstack,
14926 const offset_map_type &offset_map,
14927 struct field_info *fi,
14928 const std::vector<variant_part_builder> &variant_parts)
14929{
14930 if (variant_parts.empty ())
14931 return {};
14932
14933 size_t n = variant_parts.size ();
14934 variant_part *result = new (obstack) variant_part[n];
14935 for (size_t i = 0; i < n; ++i)
14936 create_one_variant_part (result[i], obstack, offset_map, fi,
14937 variant_parts[i]);
14938
14939 return gdb::array_view<variant_part> (result, n);
14940}
14941
14942/* Compute the variant part vector for FIP, attaching it to TYPE when
14943 done. */
14944
14945static void
14946add_variant_property (struct field_info *fip, struct type *type,
14947 struct dwarf2_cu *cu)
14948{
14949 /* Map section offsets of fields to their field index. Note the
14950 field index here does not take the number of baseclasses into
14951 account. */
14952 offset_map_type offset_map;
14953 for (int i = 0; i < fip->fields.size (); ++i)
14954 offset_map[fip->fields[i].offset] = i;
14955
5e22e966 14956 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14957 gdb::array_view<variant_part> parts
14958 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14959 fip->variant_parts);
14960
14961 struct dynamic_prop prop;
14962 prop.kind = PROP_VARIANT_PARTS;
14963 prop.data.variant_parts
14964 = ((gdb::array_view<variant_part> *)
14965 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14966
5c54719c 14967 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14968}
14969
c906108c
SS
14970/* Create the vector of fields, and attach it to the type. */
14971
14972static void
fba45db2 14973dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14974 struct dwarf2_cu *cu)
c906108c 14975{
317f7127 14976 int nfields = fip->nfields ();
c906108c
SS
14977
14978 /* Record the field count, allocate space for the array of fields,
14979 and create blank accessibility bitfields if necessary. */
5e33d5f4 14980 type->set_num_fields (nfields);
3cabb6b0
SM
14981 type->set_fields
14982 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14983
b4ba55a1 14984 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14985 {
14986 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14987
14988 TYPE_FIELD_PRIVATE_BITS (type) =
14989 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14990 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14991
14992 TYPE_FIELD_PROTECTED_BITS (type) =
14993 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14994 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14995
774b6a14
TT
14996 TYPE_FIELD_IGNORE_BITS (type) =
14997 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14998 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14999 }
15000
15001 /* If the type has baseclasses, allocate and clear a bit vector for
15002 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15003 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15004 {
be2daae6 15005 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15006 unsigned char *pointer;
c906108c
SS
15007
15008 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15009 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15010 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15011 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15012 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15013 }
15014
9c6a1327
TT
15015 if (!fip->variant_parts.empty ())
15016 add_variant_property (fip, type, cu);
2ddeaf8a 15017
be2daae6
TT
15018 /* Copy the saved-up fields into the field vector. */
15019 for (int i = 0; i < nfields; ++i)
c906108c 15020 {
be2daae6
TT
15021 struct nextfield &field
15022 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15023 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15024
ceacbf6e 15025 type->field (i) = field.field;
be2daae6 15026 switch (field.accessibility)
c906108c 15027 {
c5aa993b 15028 case DW_ACCESS_private:
b4ba55a1 15029 if (cu->language != language_ada)
be2daae6 15030 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15031 break;
c906108c 15032
c5aa993b 15033 case DW_ACCESS_protected:
b4ba55a1 15034 if (cu->language != language_ada)
be2daae6 15035 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15036 break;
c906108c 15037
c5aa993b
JM
15038 case DW_ACCESS_public:
15039 break;
c906108c 15040
c5aa993b
JM
15041 default:
15042 /* Unknown accessibility. Complain and treat it as public. */
15043 {
b98664d3 15044 complaint (_("unsupported accessibility %d"),
be2daae6 15045 field.accessibility);
c5aa993b
JM
15046 }
15047 break;
c906108c 15048 }
be2daae6 15049 if (i < fip->baseclasses.size ())
c906108c 15050 {
be2daae6 15051 switch (field.virtuality)
c906108c 15052 {
c5aa993b
JM
15053 case DW_VIRTUALITY_virtual:
15054 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15055 if (cu->language == language_ada)
a73c6dcd 15056 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15057 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15058 break;
c906108c
SS
15059 }
15060 }
c906108c
SS
15061 }
15062}
15063
7d27a96d
TT
15064/* Return true if this member function is a constructor, false
15065 otherwise. */
15066
15067static int
15068dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15069{
15070 const char *fieldname;
fe978cb0 15071 const char *type_name;
7d27a96d
TT
15072 int len;
15073
15074 if (die->parent == NULL)
15075 return 0;
15076
15077 if (die->parent->tag != DW_TAG_structure_type
15078 && die->parent->tag != DW_TAG_union_type
15079 && die->parent->tag != DW_TAG_class_type)
15080 return 0;
15081
15082 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15083 type_name = dwarf2_name (die->parent, cu);
15084 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15085 return 0;
15086
15087 len = strlen (fieldname);
fe978cb0
PA
15088 return (strncmp (fieldname, type_name, len) == 0
15089 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15090}
15091
e35000a7
TBA
15092/* Check if the given VALUE is a recognized enum
15093 dwarf_defaulted_attribute constant according to DWARF5 spec,
15094 Table 7.24. */
15095
15096static bool
15097is_valid_DW_AT_defaulted (ULONGEST value)
15098{
15099 switch (value)
15100 {
15101 case DW_DEFAULTED_no:
15102 case DW_DEFAULTED_in_class:
15103 case DW_DEFAULTED_out_of_class:
15104 return true;
15105 }
15106
3142e908 15107 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15108 return false;
15109}
15110
c906108c
SS
15111/* Add a member function to the proper fieldlist. */
15112
15113static void
107d2387 15114dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15115 struct type *type, struct dwarf2_cu *cu)
c906108c 15116{
5e22e966 15117 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15118 struct attribute *attr;
c906108c 15119 int i;
be2daae6 15120 struct fnfieldlist *flp = nullptr;
c906108c 15121 struct fn_field *fnp;
15d034d0 15122 const char *fieldname;
f792889a 15123 struct type *this_type;
60d5a603 15124 enum dwarf_access_attribute accessibility;
c906108c 15125
b4ba55a1 15126 if (cu->language == language_ada)
a73c6dcd 15127 error (_("unexpected member function in Ada type"));
b4ba55a1 15128
2df3850c 15129 /* Get name of member function. */
39cbfefa
DJ
15130 fieldname = dwarf2_name (die, cu);
15131 if (fieldname == NULL)
2df3850c 15132 return;
c906108c 15133
c906108c 15134 /* Look up member function name in fieldlist. */
be2daae6 15135 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15136 {
27bfe10e 15137 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15138 {
15139 flp = &fip->fnfieldlists[i];
15140 break;
15141 }
c906108c
SS
15142 }
15143
be2daae6
TT
15144 /* Create a new fnfieldlist if necessary. */
15145 if (flp == nullptr)
c906108c 15146 {
be2daae6
TT
15147 fip->fnfieldlists.emplace_back ();
15148 flp = &fip->fnfieldlists.back ();
c906108c 15149 flp->name = fieldname;
be2daae6 15150 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15151 }
15152
be2daae6
TT
15153 /* Create a new member function field and add it to the vector of
15154 fnfieldlists. */
15155 flp->fnfields.emplace_back ();
15156 fnp = &flp->fnfields.back ();
3da10d80
KS
15157
15158 /* Delay processing of the physname until later. */
9c37b5ae 15159 if (cu->language == language_cplus)
be2daae6
TT
15160 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15161 die, cu);
3da10d80
KS
15162 else
15163 {
1d06ead6 15164 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15165 fnp->physname = physname ? physname : "";
15166 }
15167
c906108c 15168 fnp->type = alloc_type (objfile);
f792889a 15169 this_type = read_type_die (die, cu);
78134374 15170 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15171 {
1f704f76 15172 int nparams = this_type->num_fields ();
c906108c 15173
f792889a 15174 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15175 of the method itself (TYPE_CODE_METHOD). */
15176 smash_to_method_type (fnp->type, type,
f792889a 15177 TYPE_TARGET_TYPE (this_type),
80fc5e77 15178 this_type->fields (),
1f704f76 15179 this_type->num_fields (),
f792889a 15180 TYPE_VARARGS (this_type));
c906108c
SS
15181
15182 /* Handle static member functions.
c5aa993b 15183 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15184 member functions. G++ helps GDB by marking the first
15185 parameter for non-static member functions (which is the this
15186 pointer) as artificial. We obtain this information from
15187 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15188 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15189 fnp->voffset = VOFFSET_STATIC;
15190 }
15191 else
b98664d3 15192 complaint (_("member function type missing for '%s'"),
3da10d80 15193 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15194
15195 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15196 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15197 fnp->fcontext = die_containing_type (die, cu);
c906108c 15198
3e43a32a
MS
15199 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15200 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15201
15202 /* Get accessibility. */
e142c38c 15203 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15204 if (attr != nullptr)
aead7601 15205 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15206 else
15207 accessibility = dwarf2_default_access_attribute (die, cu);
15208 switch (accessibility)
c906108c 15209 {
60d5a603
JK
15210 case DW_ACCESS_private:
15211 fnp->is_private = 1;
15212 break;
15213 case DW_ACCESS_protected:
15214 fnp->is_protected = 1;
15215 break;
c906108c
SS
15216 }
15217
b02dede2 15218 /* Check for artificial methods. */
e142c38c 15219 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15220 if (attr && DW_UNSND (attr) != 0)
15221 fnp->is_artificial = 1;
15222
e35000a7
TBA
15223 /* Check for defaulted methods. */
15224 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15225 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15226 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15227
15228 /* Check for deleted methods. */
15229 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15230 if (attr != nullptr && DW_UNSND (attr) != 0)
15231 fnp->is_deleted = 1;
15232
7d27a96d
TT
15233 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15234
0d564a31 15235 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15236 function. For older versions of GCC, this is an offset in the
15237 appropriate virtual table, as specified by DW_AT_containing_type.
15238 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15239 to the object address. */
15240
e142c38c 15241 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15242 if (attr != nullptr)
8e19ed76 15243 {
4fc6c0d5 15244 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15245 {
aec5aa8b
TT
15246 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15247 {
15248 /* Old-style GCC. */
15249 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15250 }
15251 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15252 || (DW_BLOCK (attr)->size > 1
15253 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15254 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15255 {
aec5aa8b
TT
15256 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15257 if ((fnp->voffset % cu->header.addr_size) != 0)
15258 dwarf2_complex_location_expr_complaint ();
15259 else
15260 fnp->voffset /= cu->header.addr_size;
15261 fnp->voffset += 2;
15262 }
15263 else
15264 dwarf2_complex_location_expr_complaint ();
15265
15266 if (!fnp->fcontext)
7e993ebf
KS
15267 {
15268 /* If there is no `this' field and no DW_AT_containing_type,
15269 we cannot actually find a base class context for the
15270 vtable! */
1f704f76 15271 if (this_type->num_fields () == 0
7e993ebf
KS
15272 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15273 {
b98664d3 15274 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15275 "function \"%s\" (offset %s)"),
15276 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15277 }
15278 else
15279 {
15280 fnp->fcontext
940da03e 15281 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15282 }
15283 }
aec5aa8b 15284 }
cd6c91b4 15285 else if (attr->form_is_section_offset ())
8e19ed76 15286 {
4d3c2250 15287 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15288 }
15289 else
15290 {
4d3c2250
KB
15291 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15292 fieldname);
8e19ed76 15293 }
0d564a31 15294 }
d48cc9dd
DJ
15295 else
15296 {
15297 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15298 if (attr && DW_UNSND (attr))
15299 {
15300 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15301 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15302 "but the vtable offset is not specified"),
9d8780f0 15303 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15304 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15305 TYPE_CPLUS_DYNAMIC (type) = 1;
15306 }
15307 }
c906108c
SS
15308}
15309
15310/* Create the vector of member function fields, and attach it to the type. */
15311
15312static void
fba45db2 15313dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15314 struct dwarf2_cu *cu)
c906108c 15315{
b4ba55a1 15316 if (cu->language == language_ada)
a73c6dcd 15317 error (_("unexpected member functions in Ada type"));
b4ba55a1 15318
c906108c
SS
15319 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15320 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15321 TYPE_ALLOC (type,
15322 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15323
be2daae6 15324 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15325 {
be2daae6 15326 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15327 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15328
be2daae6
TT
15329 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15330 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15331 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15332 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15333
15334 for (int k = 0; k < nf.fnfields.size (); ++k)
15335 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15336 }
15337
be2daae6 15338 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15339}
15340
1168df01
JB
15341/* Returns non-zero if NAME is the name of a vtable member in CU's
15342 language, zero otherwise. */
15343static int
15344is_vtable_name (const char *name, struct dwarf2_cu *cu)
15345{
15346 static const char vptr[] = "_vptr";
15347
9c37b5ae
TT
15348 /* Look for the C++ form of the vtable. */
15349 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15350 return 1;
15351
15352 return 0;
15353}
15354
c0dd20ea 15355/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15356 functions, with the ABI-specified layout. If TYPE describes
15357 such a structure, smash it into a member function type.
61049d3b
DJ
15358
15359 GCC shouldn't do this; it should just output pointer to member DIEs.
15360 This is GCC PR debug/28767. */
c0dd20ea 15361
0b92b5bb
TT
15362static void
15363quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15364{
09e2d7c7 15365 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15366
15367 /* Check for a structure with no name and two children. */
1f704f76 15368 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15369 return;
c0dd20ea
DJ
15370
15371 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15372 if (TYPE_FIELD_NAME (type, 0) == NULL
15373 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15374 || TYPE_FIELD_NAME (type, 1) == NULL
15375 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15376 return;
c0dd20ea
DJ
15377
15378 /* Find the type of the method. */
940da03e 15379 pfn_type = type->field (0).type ();
c0dd20ea 15380 if (pfn_type == NULL
78134374
SM
15381 || pfn_type->code () != TYPE_CODE_PTR
15382 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15383 return;
c0dd20ea
DJ
15384
15385 /* Look for the "this" argument. */
15386 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15387 if (pfn_type->num_fields () == 0
940da03e
SM
15388 /* || pfn_type->field (0).type () == NULL */
15389 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15390 return;
c0dd20ea 15391
940da03e 15392 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15393 new_type = alloc_type (objfile);
09e2d7c7 15394 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15395 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15396 TYPE_VARARGS (pfn_type));
0b92b5bb 15397 smash_to_methodptr_type (type, new_type);
c0dd20ea 15398}
1168df01 15399
2b4424c3
TT
15400/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15401 appropriate error checking and issuing complaints if there is a
15402 problem. */
15403
15404static ULONGEST
15405get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15406{
15407 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15408
15409 if (attr == nullptr)
15410 return 0;
15411
cd6c91b4 15412 if (!attr->form_is_constant ())
2b4424c3 15413 {
b98664d3 15414 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15415 " - DIE at %s [in module %s]"),
15416 sect_offset_str (die->sect_off),
5e22e966 15417 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15418 return 0;
15419 }
15420
15421 ULONGEST align;
15422 if (attr->form == DW_FORM_sdata)
15423 {
15424 LONGEST val = DW_SND (attr);
15425 if (val < 0)
15426 {
b98664d3 15427 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15428 " - DIE at %s [in module %s]"),
15429 sect_offset_str (die->sect_off),
5e22e966 15430 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15431 return 0;
15432 }
15433 align = val;
15434 }
15435 else
15436 align = DW_UNSND (attr);
15437
15438 if (align == 0)
15439 {
b98664d3 15440 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15441 " - DIE at %s [in module %s]"),
15442 sect_offset_str (die->sect_off),
5e22e966 15443 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15444 return 0;
15445 }
15446 if ((align & (align - 1)) != 0)
15447 {
b98664d3 15448 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15449 " - DIE at %s [in module %s]"),
15450 sect_offset_str (die->sect_off),
5e22e966 15451 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15452 return 0;
15453 }
15454
15455 return align;
15456}
15457
15458/* If the DIE has a DW_AT_alignment attribute, use its value to set
15459 the alignment for TYPE. */
15460
15461static void
15462maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15463 struct type *type)
15464{
15465 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15466 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15467 " - DIE at %s [in module %s]"),
15468 sect_offset_str (die->sect_off),
5e22e966 15469 objfile_name (cu->per_objfile->objfile));
2b4424c3 15470}
685b1105 15471
e35000a7
TBA
15472/* Check if the given VALUE is a valid enum dwarf_calling_convention
15473 constant for a type, according to DWARF5 spec, Table 5.5. */
15474
15475static bool
15476is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15477{
15478 switch (value)
15479 {
15480 case DW_CC_normal:
15481 case DW_CC_pass_by_reference:
15482 case DW_CC_pass_by_value:
15483 return true;
15484
15485 default:
15486 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15487 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15488 return false;
15489 }
15490}
15491
d0922fcf
TBA
15492/* Check if the given VALUE is a valid enum dwarf_calling_convention
15493 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15494 also according to GNU-specific values (see include/dwarf2.h). */
15495
15496static bool
15497is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15498{
15499 switch (value)
15500 {
15501 case DW_CC_normal:
15502 case DW_CC_program:
15503 case DW_CC_nocall:
15504 return true;
15505
15506 case DW_CC_GNU_renesas_sh:
15507 case DW_CC_GNU_borland_fastcall_i386:
15508 case DW_CC_GDB_IBM_OpenCL:
15509 return true;
15510
15511 default:
15512 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15513 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15514 return false;
15515 }
15516}
15517
c906108c 15518/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15519 (definition) to create a type for the structure or union. Fill in
15520 the type's name and general properties; the members will not be
83655187
DE
15521 processed until process_structure_scope. A symbol table entry for
15522 the type will also not be done until process_structure_scope (assuming
15523 the type has a name).
c906108c 15524
c767944b
DJ
15525 NOTE: we need to call these functions regardless of whether or not the
15526 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15527 structure or union. This gets the type entered into our set of
83655187 15528 user defined types. */
c906108c 15529
f792889a 15530static struct type *
134d01f1 15531read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15532{
5e22e966 15533 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15534 struct type *type;
15535 struct attribute *attr;
15d034d0 15536 const char *name;
c906108c 15537
348e048f
DE
15538 /* If the definition of this type lives in .debug_types, read that type.
15539 Don't follow DW_AT_specification though, that will take us back up
15540 the chain and we want to go down. */
052c8bb8 15541 attr = die->attr (DW_AT_signature);
435d3d88 15542 if (attr != nullptr)
348e048f 15543 {
ac9ec31b 15544 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15545
ac9ec31b 15546 /* The type's CU may not be the same as CU.
02142a6c 15547 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15548 return set_die_type (die, type, cu);
15549 }
15550
c0dd20ea 15551 type = alloc_type (objfile);
c906108c 15552 INIT_CPLUS_SPECIFIC (type);
93311388 15553
39cbfefa
DJ
15554 name = dwarf2_name (die, cu);
15555 if (name != NULL)
c906108c 15556 {
987504bb 15557 if (cu->language == language_cplus
c44af4eb
TT
15558 || cu->language == language_d
15559 || cu->language == language_rust)
63d06c5c 15560 {
15d034d0 15561 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15562
15563 /* dwarf2_full_name might have already finished building the DIE's
15564 type. If so, there is no need to continue. */
15565 if (get_die_type (die, cu) != NULL)
15566 return get_die_type (die, cu);
15567
d0e39ea2 15568 type->set_name (full_name);
63d06c5c
DC
15569 }
15570 else
15571 {
d8151005
DJ
15572 /* The name is already allocated along with this objfile, so
15573 we don't need to duplicate it for the type. */
d0e39ea2 15574 type->set_name (name);
63d06c5c 15575 }
c906108c
SS
15576 }
15577
15578 if (die->tag == DW_TAG_structure_type)
15579 {
67607e24 15580 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15581 }
15582 else if (die->tag == DW_TAG_union_type)
15583 {
67607e24 15584 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15585 }
15586 else
15587 {
67607e24 15588 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15589 }
15590
0cc2414c
TT
15591 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15592 TYPE_DECLARED_CLASS (type) = 1;
15593
e35000a7
TBA
15594 /* Store the calling convention in the type if it's available in
15595 the die. Otherwise the calling convention remains set to
15596 the default value DW_CC_normal. */
15597 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15598 if (attr != nullptr
15599 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15600 {
15601 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15602 TYPE_CPLUS_CALLING_CONVENTION (type)
15603 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15604 }
15605
e142c38c 15606 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15607 if (attr != nullptr)
c906108c 15608 {
cd6c91b4 15609 if (attr->form_is_constant ())
155bfbd3
JB
15610 TYPE_LENGTH (type) = DW_UNSND (attr);
15611 else
15612 {
f8e89861 15613 struct dynamic_prop prop;
293e7e51 15614 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15615 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15616 TYPE_LENGTH (type) = 0;
15617 }
c906108c
SS
15618 }
15619 else
15620 {
15621 TYPE_LENGTH (type) = 0;
15622 }
15623
2b4424c3
TT
15624 maybe_set_alignment (cu, die, type);
15625
5230b05a 15626 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15627 {
5230b05a
WT
15628 /* ICC<14 does not output the required DW_AT_declaration on
15629 incomplete types, but gives them a size of zero. */
422b1cb0 15630 TYPE_STUB (type) = 1;
685b1105
JK
15631 }
15632 else
15633 TYPE_STUB_SUPPORTED (type) = 1;
15634
dc718098 15635 if (die_is_declaration (die, cu))
876cecd0 15636 TYPE_STUB (type) = 1;
a6c727b2
DJ
15637 else if (attr == NULL && die->child == NULL
15638 && producer_is_realview (cu->producer))
15639 /* RealView does not output the required DW_AT_declaration
15640 on incomplete types. */
15641 TYPE_STUB (type) = 1;
dc718098 15642
c906108c
SS
15643 /* We need to add the type field to the die immediately so we don't
15644 infinitely recurse when dealing with pointers to the structure
0963b4bd 15645 type within the structure itself. */
1c379e20 15646 set_die_type (die, type, cu);
c906108c 15647
7e314c57
JK
15648 /* set_die_type should be already done. */
15649 set_descriptive_type (type, die, cu);
15650
c767944b
DJ
15651 return type;
15652}
15653
9c6a1327
TT
15654static void handle_struct_member_die
15655 (struct die_info *child_die,
15656 struct type *type,
15657 struct field_info *fi,
15658 std::vector<struct symbol *> *template_args,
15659 struct dwarf2_cu *cu);
15660
15661/* A helper for handle_struct_member_die that handles
15662 DW_TAG_variant_part. */
15663
15664static void
15665handle_variant_part (struct die_info *die, struct type *type,
15666 struct field_info *fi,
15667 std::vector<struct symbol *> *template_args,
15668 struct dwarf2_cu *cu)
15669{
15670 variant_part_builder *new_part;
15671 if (fi->current_variant_part == nullptr)
15672 {
15673 fi->variant_parts.emplace_back ();
15674 new_part = &fi->variant_parts.back ();
15675 }
15676 else if (!fi->current_variant_part->processing_variant)
15677 {
15678 complaint (_("nested DW_TAG_variant_part seen "
15679 "- DIE at %s [in module %s]"),
15680 sect_offset_str (die->sect_off),
5e22e966 15681 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15682 return;
15683 }
15684 else
15685 {
15686 variant_field &current = fi->current_variant_part->variants.back ();
15687 current.variant_parts.emplace_back ();
15688 new_part = &current.variant_parts.back ();
15689 }
15690
15691 /* When we recurse, we want callees to add to this new variant
15692 part. */
15693 scoped_restore save_current_variant_part
15694 = make_scoped_restore (&fi->current_variant_part, new_part);
15695
15696 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15697 if (discr == NULL)
15698 {
15699 /* It's a univariant form, an extension we support. */
15700 }
15701 else if (discr->form_is_ref ())
15702 {
15703 struct dwarf2_cu *target_cu = cu;
15704 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15705
15706 new_part->discriminant_offset = target_die->sect_off;
15707 }
15708 else
15709 {
15710 complaint (_("DW_AT_discr does not have DIE reference form"
15711 " - DIE at %s [in module %s]"),
15712 sect_offset_str (die->sect_off),
5e22e966 15713 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15714 }
15715
15716 for (die_info *child_die = die->child;
15717 child_die != NULL;
15718 child_die = child_die->sibling)
15719 handle_struct_member_die (child_die, type, fi, template_args, cu);
15720}
15721
15722/* A helper for handle_struct_member_die that handles
15723 DW_TAG_variant. */
15724
15725static void
15726handle_variant (struct die_info *die, struct type *type,
15727 struct field_info *fi,
15728 std::vector<struct symbol *> *template_args,
15729 struct dwarf2_cu *cu)
15730{
15731 if (fi->current_variant_part == nullptr)
15732 {
15733 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15734 "- DIE at %s [in module %s]"),
15735 sect_offset_str (die->sect_off),
5e22e966 15736 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15737 return;
15738 }
15739 if (fi->current_variant_part->processing_variant)
15740 {
15741 complaint (_("nested DW_TAG_variant seen "
15742 "- DIE at %s [in module %s]"),
15743 sect_offset_str (die->sect_off),
5e22e966 15744 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15745 return;
15746 }
15747
15748 scoped_restore save_processing_variant
15749 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15750 true);
15751
15752 fi->current_variant_part->variants.emplace_back ();
15753 variant_field &variant = fi->current_variant_part->variants.back ();
15754 variant.first_field = fi->fields.size ();
15755
15756 /* In a variant we want to get the discriminant and also add a
15757 field for our sole member child. */
15758 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15759 if (discr == nullptr)
15760 {
15761 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15762 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15763 variant.default_branch = true;
15764 else
15765 variant.discr_list_data = DW_BLOCK (discr);
15766 }
15767 else
15768 variant.discriminant_value = DW_UNSND (discr);
15769
15770 for (die_info *variant_child = die->child;
15771 variant_child != NULL;
15772 variant_child = variant_child->sibling)
15773 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15774
15775 variant.last_field = fi->fields.size ();
15776}
15777
2ddeaf8a
TT
15778/* A helper for process_structure_scope that handles a single member
15779 DIE. */
15780
15781static void
15782handle_struct_member_die (struct die_info *child_die, struct type *type,
15783 struct field_info *fi,
15784 std::vector<struct symbol *> *template_args,
15785 struct dwarf2_cu *cu)
15786{
15787 if (child_die->tag == DW_TAG_member
9c6a1327 15788 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15789 {
15790 /* NOTE: carlton/2002-11-05: A C++ static data member
15791 should be a DW_TAG_member that is a declaration, but
15792 all versions of G++ as of this writing (so through at
15793 least 3.2.1) incorrectly generate DW_TAG_variable
15794 tags for them instead. */
15795 dwarf2_add_field (fi, child_die, cu);
15796 }
15797 else if (child_die->tag == DW_TAG_subprogram)
15798 {
15799 /* Rust doesn't have member functions in the C++ sense.
15800 However, it does emit ordinary functions as children
15801 of a struct DIE. */
15802 if (cu->language == language_rust)
15803 read_func_scope (child_die, cu);
15804 else
15805 {
15806 /* C++ member function. */
15807 dwarf2_add_member_fn (fi, child_die, type, cu);
15808 }
15809 }
15810 else if (child_die->tag == DW_TAG_inheritance)
15811 {
15812 /* C++ base class field. */
15813 dwarf2_add_field (fi, child_die, cu);
15814 }
15815 else if (type_can_define_types (child_die))
15816 dwarf2_add_type_defn (fi, child_die, cu);
15817 else if (child_die->tag == DW_TAG_template_type_param
15818 || child_die->tag == DW_TAG_template_value_param)
15819 {
15820 struct symbol *arg = new_symbol (child_die, NULL, cu);
15821
15822 if (arg != NULL)
15823 template_args->push_back (arg);
15824 }
9c6a1327
TT
15825 else if (child_die->tag == DW_TAG_variant_part)
15826 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15827 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15828 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15829}
15830
c767944b
DJ
15831/* Finish creating a structure or union type, including filling in
15832 its members and creating a symbol for it. */
15833
15834static void
15835process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15836{
5e22e966 15837 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15838 struct die_info *child_die;
c767944b
DJ
15839 struct type *type;
15840
15841 type = get_die_type (die, cu);
15842 if (type == NULL)
15843 type = read_structure_type (die, cu);
15844
3e1d3d8c 15845 bool has_template_parameters = false;
e142c38c 15846 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15847 {
15848 struct field_info fi;
2f4732b0 15849 std::vector<struct symbol *> template_args;
c906108c 15850
639d11d3 15851 child_die = die->child;
c906108c
SS
15852
15853 while (child_die && child_die->tag)
15854 {
2ddeaf8a 15855 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15856 child_die = child_die->sibling;
c906108c
SS
15857 }
15858
34eaf542 15859 /* Attach template arguments to type. */
2f4732b0 15860 if (!template_args.empty ())
34eaf542 15861 {
3e1d3d8c 15862 has_template_parameters = true;
34eaf542 15863 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15864 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15865 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15866 = XOBNEWVEC (&objfile->objfile_obstack,
15867 struct symbol *,
15868 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15869 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15870 template_args.data (),
34eaf542
TT
15871 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15872 * sizeof (struct symbol *)));
34eaf542
TT
15873 }
15874
c906108c 15875 /* Attach fields and member functions to the type. */
317f7127 15876 if (fi.nfields () > 0)
e7c27a73 15877 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15878 if (!fi.fnfieldlists.empty ())
c906108c 15879 {
e7c27a73 15880 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15881
c5aa993b 15882 /* Get the type which refers to the base class (possibly this
c906108c 15883 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15884 class from the DW_AT_containing_type attribute. This use of
15885 DW_AT_containing_type is a GNU extension. */
c906108c 15886
e142c38c 15887 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15888 {
e7c27a73 15889 struct type *t = die_containing_type (die, cu);
c906108c 15890
ae6ae975 15891 set_type_vptr_basetype (type, t);
c906108c
SS
15892 if (type == t)
15893 {
c906108c
SS
15894 int i;
15895
15896 /* Our own class provides vtbl ptr. */
1f704f76 15897 for (i = t->num_fields () - 1;
c906108c
SS
15898 i >= TYPE_N_BASECLASSES (t);
15899 --i)
15900 {
0d5cff50 15901 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15902
1168df01 15903 if (is_vtable_name (fieldname, cu))
c906108c 15904 {
ae6ae975 15905 set_type_vptr_fieldno (type, i);
c906108c
SS
15906 break;
15907 }
15908 }
15909
15910 /* Complain if virtual function table field not found. */
15911 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15912 complaint (_("virtual function table pointer "
3e43a32a 15913 "not found when defining class '%s'"),
7d93a1e0 15914 type->name () ? type->name () : "");
c906108c
SS
15915 }
15916 else
15917 {
ae6ae975 15918 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15919 }
15920 }
f6235d4c 15921 else if (cu->producer
61012eef 15922 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15923 {
15924 /* The IBM XLC compiler does not provide direct indication
15925 of the containing type, but the vtable pointer is
15926 always named __vfp. */
15927
15928 int i;
15929
1f704f76 15930 for (i = type->num_fields () - 1;
f6235d4c
EZ
15931 i >= TYPE_N_BASECLASSES (type);
15932 --i)
15933 {
15934 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15935 {
ae6ae975
DE
15936 set_type_vptr_fieldno (type, i);
15937 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15938 break;
15939 }
15940 }
15941 }
c906108c 15942 }
98751a41
JK
15943
15944 /* Copy fi.typedef_field_list linked list elements content into the
15945 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15946 if (!fi.typedef_field_list.empty ())
98751a41 15947 {
be2daae6 15948 int count = fi.typedef_field_list.size ();
98751a41 15949
a0d7a4ff 15950 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15951 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15952 = ((struct decl_field *)
be2daae6
TT
15953 TYPE_ALLOC (type,
15954 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15955 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15956
be2daae6
TT
15957 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15958 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15959 }
c767944b 15960
883fd55a
KS
15961 /* Copy fi.nested_types_list linked list elements content into the
15962 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15963 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15964 {
be2daae6 15965 int count = fi.nested_types_list.size ();
883fd55a
KS
15966
15967 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15968 TYPE_NESTED_TYPES_ARRAY (type)
15969 = ((struct decl_field *)
be2daae6
TT
15970 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15971 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15972
be2daae6
TT
15973 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15974 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15975 }
c906108c 15976 }
63d06c5c 15977
bb5ed363 15978 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15979 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15980 cu->rust_unions.push_back (type);
0b92b5bb 15981
90aeadfc
DC
15982 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15983 snapshots) has been known to create a die giving a declaration
15984 for a class that has, as a child, a die giving a definition for a
15985 nested class. So we have to process our children even if the
15986 current die is a declaration. Normally, of course, a declaration
15987 won't have any children at all. */
134d01f1 15988
ca040673
DE
15989 child_die = die->child;
15990
90aeadfc
DC
15991 while (child_die != NULL && child_die->tag)
15992 {
15993 if (child_die->tag == DW_TAG_member
15994 || child_die->tag == DW_TAG_variable
34eaf542
TT
15995 || child_die->tag == DW_TAG_inheritance
15996 || child_die->tag == DW_TAG_template_value_param
15997 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15998 {
90aeadfc 15999 /* Do nothing. */
134d01f1 16000 }
90aeadfc
DC
16001 else
16002 process_die (child_die, cu);
134d01f1 16003
436c571c 16004 child_die = child_die->sibling;
134d01f1
DJ
16005 }
16006
fa4028e9
JB
16007 /* Do not consider external references. According to the DWARF standard,
16008 these DIEs are identified by the fact that they have no byte_size
16009 attribute, and a declaration attribute. */
16010 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16011 || !die_is_declaration (die, cu)
16012 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16013 {
16014 struct symbol *sym = new_symbol (die, type, cu);
16015
16016 if (has_template_parameters)
16017 {
a776957c
TT
16018 struct symtab *symtab;
16019 if (sym != nullptr)
16020 symtab = symbol_symtab (sym);
16021 else if (cu->line_header != nullptr)
16022 {
16023 /* Any related symtab will do. */
16024 symtab
7ba99d21 16025 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16026 }
16027 else
16028 {
16029 symtab = nullptr;
16030 complaint (_("could not find suitable "
16031 "symtab for template parameter"
16032 " - DIE at %s [in module %s]"),
16033 sect_offset_str (die->sect_off),
16034 objfile_name (objfile));
16035 }
16036
16037 if (symtab != nullptr)
16038 {
16039 /* Make sure that the symtab is set on the new symbols.
16040 Even though they don't appear in this symtab directly,
16041 other parts of gdb assume that symbols do, and this is
16042 reasonably true. */
16043 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16044 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16045 }
3e1d3d8c
TT
16046 }
16047 }
134d01f1
DJ
16048}
16049
ed6acedd
TT
16050/* Assuming DIE is an enumeration type, and TYPE is its associated
16051 type, update TYPE using some information only available in DIE's
16052 children. In particular, the fields are computed. */
55426c9d
JB
16053
16054static void
16055update_enumeration_type_from_children (struct die_info *die,
16056 struct type *type,
16057 struct dwarf2_cu *cu)
16058{
60f7655a 16059 struct die_info *child_die;
55426c9d
JB
16060 int unsigned_enum = 1;
16061 int flag_enum = 1;
55426c9d 16062
8268c778 16063 auto_obstack obstack;
ed6acedd 16064 std::vector<struct field> fields;
55426c9d 16065
60f7655a
DE
16066 for (child_die = die->child;
16067 child_die != NULL && child_die->tag;
436c571c 16068 child_die = child_die->sibling)
55426c9d
JB
16069 {
16070 struct attribute *attr;
16071 LONGEST value;
16072 const gdb_byte *bytes;
16073 struct dwarf2_locexpr_baton *baton;
16074 const char *name;
60f7655a 16075
55426c9d
JB
16076 if (child_die->tag != DW_TAG_enumerator)
16077 continue;
16078
16079 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16080 if (attr == NULL)
16081 continue;
16082
16083 name = dwarf2_name (child_die, cu);
16084 if (name == NULL)
16085 name = "<anonymous enumerator>";
16086
16087 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16088 &value, &bytes, &baton);
16089 if (value < 0)
16090 {
16091 unsigned_enum = 0;
16092 flag_enum = 0;
16093 }
55426c9d 16094 else
edd45eb0
SM
16095 {
16096 if (count_one_bits_ll (value) >= 2)
16097 flag_enum = 0;
edd45eb0 16098 }
55426c9d 16099
ed6acedd
TT
16100 fields.emplace_back ();
16101 struct field &field = fields.back ();
16102 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16103 SET_FIELD_ENUMVAL (field, value);
16104 }
16105
16106 if (!fields.empty ())
16107 {
5e33d5f4 16108 type->set_num_fields (fields.size ());
3cabb6b0
SM
16109 type->set_fields
16110 ((struct field *)
16111 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16112 memcpy (type->fields (), fields.data (),
ed6acedd 16113 sizeof (struct field) * fields.size ());
55426c9d
JB
16114 }
16115
16116 if (unsigned_enum)
16117 TYPE_UNSIGNED (type) = 1;
16118 if (flag_enum)
16119 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16120}
16121
134d01f1
DJ
16122/* Given a DW_AT_enumeration_type die, set its type. We do not
16123 complete the type's fields yet, or create any symbols. */
c906108c 16124
f792889a 16125static struct type *
134d01f1 16126read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16127{
5e22e966 16128 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16129 struct type *type;
c906108c 16130 struct attribute *attr;
0114d602 16131 const char *name;
134d01f1 16132
348e048f
DE
16133 /* If the definition of this type lives in .debug_types, read that type.
16134 Don't follow DW_AT_specification though, that will take us back up
16135 the chain and we want to go down. */
052c8bb8 16136 attr = die->attr (DW_AT_signature);
435d3d88 16137 if (attr != nullptr)
348e048f 16138 {
ac9ec31b 16139 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16140
ac9ec31b 16141 /* The type's CU may not be the same as CU.
02142a6c 16142 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16143 return set_die_type (die, type, cu);
16144 }
16145
c906108c
SS
16146 type = alloc_type (objfile);
16147
67607e24 16148 type->set_code (TYPE_CODE_ENUM);
94af9270 16149 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16150 if (name != NULL)
d0e39ea2 16151 type->set_name (name);
c906108c 16152
0626fc76
TT
16153 attr = dwarf2_attr (die, DW_AT_type, cu);
16154 if (attr != NULL)
16155 {
16156 struct type *underlying_type = die_type (die, cu);
16157
16158 TYPE_TARGET_TYPE (type) = underlying_type;
16159 }
16160
e142c38c 16161 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16162 if (attr != nullptr)
c906108c
SS
16163 {
16164 TYPE_LENGTH (type) = DW_UNSND (attr);
16165 }
16166 else
16167 {
16168 TYPE_LENGTH (type) = 0;
16169 }
16170
2b4424c3
TT
16171 maybe_set_alignment (cu, die, type);
16172
137033e9
JB
16173 /* The enumeration DIE can be incomplete. In Ada, any type can be
16174 declared as private in the package spec, and then defined only
16175 inside the package body. Such types are known as Taft Amendment
16176 Types. When another package uses such a type, an incomplete DIE
16177 may be generated by the compiler. */
02eb380e 16178 if (die_is_declaration (die, cu))
876cecd0 16179 TYPE_STUB (type) = 1;
02eb380e 16180
0626fc76
TT
16181 /* If this type has an underlying type that is not a stub, then we
16182 may use its attributes. We always use the "unsigned" attribute
16183 in this situation, because ordinarily we guess whether the type
16184 is unsigned -- but the guess can be wrong and the underlying type
16185 can tell us the reality. However, we defer to a local size
16186 attribute if one exists, because this lets the compiler override
16187 the underlying type if needed. */
16188 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16189 {
9e7c9a03
HD
16190 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16191 underlying_type = check_typedef (underlying_type);
16192 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16193 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16194 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16195 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16196 && TYPE_RAW_ALIGN (underlying_type) != 0)
16197 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16198 }
16199
3d567982
TT
16200 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16201
ed6acedd
TT
16202 set_die_type (die, type, cu);
16203
16204 /* Finish the creation of this type by using the enum's children.
16205 Note that, as usual, this must come after set_die_type to avoid
16206 infinite recursion when trying to compute the names of the
16207 enumerators. */
16208 update_enumeration_type_from_children (die, type, cu);
16209
16210 return type;
134d01f1
DJ
16211}
16212
16213/* Given a pointer to a die which begins an enumeration, process all
16214 the dies that define the members of the enumeration, and create the
16215 symbol for the enumeration type.
16216
16217 NOTE: We reverse the order of the element list. */
16218
16219static void
16220process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16221{
f792889a 16222 struct type *this_type;
134d01f1 16223
f792889a
DJ
16224 this_type = get_die_type (die, cu);
16225 if (this_type == NULL)
16226 this_type = read_enumeration_type (die, cu);
9dc481d3 16227
639d11d3 16228 if (die->child != NULL)
c906108c 16229 {
9dc481d3 16230 struct die_info *child_die;
15d034d0 16231 const char *name;
9dc481d3 16232
639d11d3 16233 child_die = die->child;
c906108c
SS
16234 while (child_die && child_die->tag)
16235 {
16236 if (child_die->tag != DW_TAG_enumerator)
16237 {
e7c27a73 16238 process_die (child_die, cu);
c906108c
SS
16239 }
16240 else
16241 {
39cbfefa
DJ
16242 name = dwarf2_name (child_die, cu);
16243 if (name)
ed6acedd 16244 new_symbol (child_die, this_type, cu);
c906108c
SS
16245 }
16246
436c571c 16247 child_die = child_die->sibling;
c906108c 16248 }
c906108c 16249 }
134d01f1 16250
6c83ed52
TT
16251 /* If we are reading an enum from a .debug_types unit, and the enum
16252 is a declaration, and the enum is not the signatured type in the
16253 unit, then we do not want to add a symbol for it. Adding a
16254 symbol would in some cases obscure the true definition of the
16255 enum, giving users an incomplete type when the definition is
16256 actually available. Note that we do not want to do this for all
16257 enums which are just declarations, because C++0x allows forward
16258 enum declarations. */
3019eac3 16259 if (cu->per_cu->is_debug_types
6c83ed52
TT
16260 && die_is_declaration (die, cu))
16261 {
52dc124a 16262 struct signatured_type *sig_type;
6c83ed52 16263
c0f78cd4 16264 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16265 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16266 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16267 return;
16268 }
16269
f792889a 16270 new_symbol (die, this_type, cu);
c906108c
SS
16271}
16272
16273/* Extract all information from a DW_TAG_array_type DIE and put it in
16274 the DIE's type field. For now, this only handles one dimensional
16275 arrays. */
16276
f792889a 16277static struct type *
e7c27a73 16278read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16279{
5e22e966 16280 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16281 struct die_info *child_die;
7e314c57 16282 struct type *type;
c906108c 16283 struct type *element_type, *range_type, *index_type;
c906108c 16284 struct attribute *attr;
15d034d0 16285 const char *name;
a405673c 16286 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16287 unsigned int bit_stride = 0;
c906108c 16288
e7c27a73 16289 element_type = die_type (die, cu);
c906108c 16290
7e314c57
JK
16291 /* The die_type call above may have already set the type for this DIE. */
16292 type = get_die_type (die, cu);
16293 if (type)
16294 return type;
16295
dc53a7ad
JB
16296 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16297 if (attr != NULL)
a405673c
JB
16298 {
16299 int stride_ok;
293e7e51 16300 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16301
16302 byte_stride_prop
16303 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16304 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16305 prop_type);
a405673c
JB
16306 if (!stride_ok)
16307 {
b98664d3 16308 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16309 " - DIE at %s [in module %s]"),
16310 sect_offset_str (die->sect_off),
5e22e966 16311 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16312 /* Ignore this attribute. We will likely not be able to print
16313 arrays of this type correctly, but there is little we can do
16314 to help if we cannot read the attribute's value. */
16315 byte_stride_prop = NULL;
16316 }
16317 }
dc53a7ad
JB
16318
16319 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16320 if (attr != NULL)
16321 bit_stride = DW_UNSND (attr);
16322
c906108c
SS
16323 /* Irix 6.2 native cc creates array types without children for
16324 arrays with unspecified length. */
639d11d3 16325 if (die->child == NULL)
c906108c 16326 {
46bf5051 16327 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16328 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16329 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16330 byte_stride_prop, bit_stride);
f792889a 16331 return set_die_type (die, type, cu);
c906108c
SS
16332 }
16333
791afaa2 16334 std::vector<struct type *> range_types;
639d11d3 16335 child_die = die->child;
c906108c
SS
16336 while (child_die && child_die->tag)
16337 {
16338 if (child_die->tag == DW_TAG_subrange_type)
16339 {
f792889a 16340 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16341
f792889a 16342 if (child_type != NULL)
a02abb62 16343 {
0963b4bd
MS
16344 /* The range type was succesfully read. Save it for the
16345 array type creation. */
791afaa2 16346 range_types.push_back (child_type);
a02abb62 16347 }
c906108c 16348 }
436c571c 16349 child_die = child_die->sibling;
c906108c
SS
16350 }
16351
16352 /* Dwarf2 dimensions are output from left to right, create the
16353 necessary array types in backwards order. */
7ca2d3a3 16354
c906108c 16355 type = element_type;
7ca2d3a3
DL
16356
16357 if (read_array_order (die, cu) == DW_ORD_col_major)
16358 {
16359 int i = 0;
9a619af0 16360
791afaa2 16361 while (i < range_types.size ())
dc53a7ad 16362 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16363 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16364 }
16365 else
16366 {
791afaa2 16367 size_t ndim = range_types.size ();
7ca2d3a3 16368 while (ndim-- > 0)
dc53a7ad 16369 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16370 byte_stride_prop, bit_stride);
7ca2d3a3 16371 }
c906108c 16372
f5f8a009
EZ
16373 /* Understand Dwarf2 support for vector types (like they occur on
16374 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16375 array type. This is not part of the Dwarf2/3 standard yet, but a
16376 custom vendor extension. The main difference between a regular
16377 array and the vector variant is that vectors are passed by value
16378 to functions. */
e142c38c 16379 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16380 if (attr != nullptr)
ea37ba09 16381 make_vector_type (type);
f5f8a009 16382
dbc98a8b
KW
16383 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16384 implementation may choose to implement triple vectors using this
16385 attribute. */
16386 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16387 if (attr != nullptr)
dbc98a8b
KW
16388 {
16389 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16390 TYPE_LENGTH (type) = DW_UNSND (attr);
16391 else
b98664d3 16392 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16393 "than the total size of elements"));
dbc98a8b
KW
16394 }
16395
39cbfefa
DJ
16396 name = dwarf2_name (die, cu);
16397 if (name)
d0e39ea2 16398 type->set_name (name);
6e70227d 16399
2b4424c3
TT
16400 maybe_set_alignment (cu, die, type);
16401
0963b4bd 16402 /* Install the type in the die. */
7e314c57
JK
16403 set_die_type (die, type, cu);
16404
16405 /* set_die_type should be already done. */
b4ba55a1
JB
16406 set_descriptive_type (type, die, cu);
16407
7e314c57 16408 return type;
c906108c
SS
16409}
16410
7ca2d3a3 16411static enum dwarf_array_dim_ordering
6e70227d 16412read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16413{
16414 struct attribute *attr;
16415
16416 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16417
435d3d88 16418 if (attr != nullptr)
aead7601 16419 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16420
0963b4bd
MS
16421 /* GNU F77 is a special case, as at 08/2004 array type info is the
16422 opposite order to the dwarf2 specification, but data is still
16423 laid out as per normal fortran.
7ca2d3a3 16424
0963b4bd
MS
16425 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16426 version checking. */
7ca2d3a3 16427
905e0470
PM
16428 if (cu->language == language_fortran
16429 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16430 {
16431 return DW_ORD_row_major;
16432 }
16433
6e70227d 16434 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16435 {
16436 case array_column_major:
16437 return DW_ORD_col_major;
16438 case array_row_major:
16439 default:
16440 return DW_ORD_row_major;
16441 };
16442}
16443
72019c9c 16444/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16445 the DIE's type field. */
72019c9c 16446
f792889a 16447static struct type *
72019c9c
GM
16448read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16449{
7e314c57
JK
16450 struct type *domain_type, *set_type;
16451 struct attribute *attr;
f792889a 16452
7e314c57
JK
16453 domain_type = die_type (die, cu);
16454
16455 /* The die_type call above may have already set the type for this DIE. */
16456 set_type = get_die_type (die, cu);
16457 if (set_type)
16458 return set_type;
16459
16460 set_type = create_set_type (NULL, domain_type);
16461
16462 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16463 if (attr != nullptr)
d09039dd 16464 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16465
2b4424c3
TT
16466 maybe_set_alignment (cu, die, set_type);
16467
f792889a 16468 return set_die_type (die, set_type, cu);
72019c9c 16469}
7ca2d3a3 16470
0971de02
TT
16471/* A helper for read_common_block that creates a locexpr baton.
16472 SYM is the symbol which we are marking as computed.
16473 COMMON_DIE is the DIE for the common block.
16474 COMMON_LOC is the location expression attribute for the common
16475 block itself.
16476 MEMBER_LOC is the location expression attribute for the particular
16477 member of the common block that we are processing.
16478 CU is the CU from which the above come. */
16479
16480static void
16481mark_common_block_symbol_computed (struct symbol *sym,
16482 struct die_info *common_die,
16483 struct attribute *common_loc,
16484 struct attribute *member_loc,
16485 struct dwarf2_cu *cu)
16486{
5e22e966 16487 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16488 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16489 struct dwarf2_locexpr_baton *baton;
16490 gdb_byte *ptr;
16491 unsigned int cu_off;
08feed99 16492 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16493 LONGEST offset = 0;
16494
16495 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16496 gdb_assert (common_loc->form_is_block ());
16497 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16498 || member_loc->form_is_constant ());
0971de02 16499
8d749320 16500 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16501 baton->per_objfile = per_objfile;
0971de02
TT
16502 baton->per_cu = cu->per_cu;
16503 gdb_assert (baton->per_cu);
16504
16505 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16506
cd6c91b4 16507 if (member_loc->form_is_constant ())
0971de02 16508 {
0826b30a 16509 offset = member_loc->constant_value (0);
0971de02
TT
16510 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16511 }
16512 else
16513 baton->size += DW_BLOCK (member_loc)->size;
16514
224c3ddb 16515 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16516 baton->data = ptr;
16517
16518 *ptr++ = DW_OP_call4;
9c541725 16519 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16520 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16521 ptr += 4;
16522
cd6c91b4 16523 if (member_loc->form_is_constant ())
0971de02
TT
16524 {
16525 *ptr++ = DW_OP_addr;
16526 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16527 ptr += cu->header.addr_size;
16528 }
16529 else
16530 {
16531 /* We have to copy the data here, because DW_OP_call4 will only
16532 use a DW_AT_location attribute. */
16533 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16534 ptr += DW_BLOCK (member_loc)->size;
16535 }
16536
16537 *ptr++ = DW_OP_plus;
16538 gdb_assert (ptr - baton->data == baton->size);
16539
0971de02 16540 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16541 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16542}
16543
4357ac6c
TT
16544/* Create appropriate locally-scoped variables for all the
16545 DW_TAG_common_block entries. Also create a struct common_block
16546 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16547 is used to separate the common blocks name namespace from regular
4357ac6c 16548 variable names. */
c906108c
SS
16549
16550static void
e7c27a73 16551read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16552{
0971de02
TT
16553 struct attribute *attr;
16554
16555 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16556 if (attr != nullptr)
0971de02
TT
16557 {
16558 /* Support the .debug_loc offsets. */
4fc6c0d5 16559 if (attr->form_is_block ())
0971de02
TT
16560 {
16561 /* Ok. */
16562 }
cd6c91b4 16563 else if (attr->form_is_section_offset ())
0971de02
TT
16564 {
16565 dwarf2_complex_location_expr_complaint ();
16566 attr = NULL;
16567 }
16568 else
16569 {
16570 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16571 "common block member");
16572 attr = NULL;
16573 }
16574 }
16575
639d11d3 16576 if (die->child != NULL)
c906108c 16577 {
5e22e966 16578 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16579 struct die_info *child_die;
16580 size_t n_entries = 0, size;
16581 struct common_block *common_block;
16582 struct symbol *sym;
74ac6d43 16583
4357ac6c
TT
16584 for (child_die = die->child;
16585 child_die && child_die->tag;
436c571c 16586 child_die = child_die->sibling)
4357ac6c
TT
16587 ++n_entries;
16588
16589 size = (sizeof (struct common_block)
16590 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16591 common_block
16592 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16593 size);
4357ac6c
TT
16594 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16595 common_block->n_entries = 0;
16596
16597 for (child_die = die->child;
16598 child_die && child_die->tag;
436c571c 16599 child_die = child_die->sibling)
4357ac6c
TT
16600 {
16601 /* Create the symbol in the DW_TAG_common_block block in the current
16602 symbol scope. */
e7c27a73 16603 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16604 if (sym != NULL)
16605 {
16606 struct attribute *member_loc;
16607
16608 common_block->contents[common_block->n_entries++] = sym;
16609
16610 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16611 cu);
16612 if (member_loc)
16613 {
16614 /* GDB has handled this for a long time, but it is
16615 not specified by DWARF. It seems to have been
16616 emitted by gfortran at least as recently as:
16617 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16618 complaint (_("Variable in common block has "
0971de02 16619 "DW_AT_data_member_location "
9d8780f0
SM
16620 "- DIE at %s [in module %s]"),
16621 sect_offset_str (child_die->sect_off),
518817b3 16622 objfile_name (objfile));
0971de02 16623
cd6c91b4 16624 if (member_loc->form_is_section_offset ())
0971de02 16625 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16626 else if (member_loc->form_is_constant ()
4fc6c0d5 16627 || member_loc->form_is_block ())
0971de02 16628 {
435d3d88 16629 if (attr != nullptr)
0971de02
TT
16630 mark_common_block_symbol_computed (sym, die, attr,
16631 member_loc, cu);
16632 }
16633 else
16634 dwarf2_complex_location_expr_complaint ();
16635 }
16636 }
c906108c 16637 }
4357ac6c
TT
16638
16639 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16640 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16641 }
16642}
16643
0114d602 16644/* Create a type for a C++ namespace. */
d9fa45fe 16645
0114d602
DJ
16646static struct type *
16647read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16648{
5e22e966 16649 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16650 const char *previous_prefix, *name;
9219021c 16651 int is_anonymous;
0114d602
DJ
16652 struct type *type;
16653
16654 /* For extensions, reuse the type of the original namespace. */
16655 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16656 {
16657 struct die_info *ext_die;
16658 struct dwarf2_cu *ext_cu = cu;
9a619af0 16659
0114d602
DJ
16660 ext_die = dwarf2_extension (die, &ext_cu);
16661 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16662
16663 /* EXT_CU may not be the same as CU.
02142a6c 16664 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16665 return set_die_type (die, type, cu);
16666 }
9219021c 16667
e142c38c 16668 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16669
16670 /* Now build the name of the current namespace. */
16671
0114d602
DJ
16672 previous_prefix = determine_prefix (die, cu);
16673 if (previous_prefix[0] != '\0')
16674 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16675 previous_prefix, name, 0, cu);
0114d602
DJ
16676
16677 /* Create the type. */
19f392bc 16678 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16679
60531b24 16680 return set_die_type (die, type, cu);
0114d602
DJ
16681}
16682
22cee43f 16683/* Read a namespace scope. */
0114d602
DJ
16684
16685static void
16686read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16687{
5e22e966 16688 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16689 int is_anonymous;
9219021c 16690
5c4e30ca
DC
16691 /* Add a symbol associated to this if we haven't seen the namespace
16692 before. Also, add a using directive if it's an anonymous
16693 namespace. */
9219021c 16694
f2f0e013 16695 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16696 {
16697 struct type *type;
16698
0114d602 16699 type = read_type_die (die, cu);
e7c27a73 16700 new_symbol (die, type, cu);
5c4e30ca 16701
e8e80198 16702 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16703 if (is_anonymous)
0114d602
DJ
16704 {
16705 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16706
eb1e02fd 16707 std::vector<const char *> excludes;
804d2729 16708 add_using_directive (using_directives (cu),
7d93a1e0 16709 previous_prefix, type->name (), NULL,
eb1e02fd 16710 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16711 }
5c4e30ca 16712 }
9219021c 16713
639d11d3 16714 if (die->child != NULL)
d9fa45fe 16715 {
639d11d3 16716 struct die_info *child_die = die->child;
6e70227d 16717
d9fa45fe
DC
16718 while (child_die && child_die->tag)
16719 {
e7c27a73 16720 process_die (child_die, cu);
436c571c 16721 child_die = child_die->sibling;
d9fa45fe
DC
16722 }
16723 }
38d518c9
EZ
16724}
16725
f55ee35c
JK
16726/* Read a Fortran module as type. This DIE can be only a declaration used for
16727 imported module. Still we need that type as local Fortran "use ... only"
16728 declaration imports depend on the created type in determine_prefix. */
16729
16730static struct type *
16731read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16732{
5e22e966 16733 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16734 const char *module_name;
f55ee35c
JK
16735 struct type *type;
16736
16737 module_name = dwarf2_name (die, cu);
19f392bc 16738 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16739
f55ee35c
JK
16740 return set_die_type (die, type, cu);
16741}
16742
5d7cb8df
JK
16743/* Read a Fortran module. */
16744
16745static void
16746read_module (struct die_info *die, struct dwarf2_cu *cu)
16747{
16748 struct die_info *child_die = die->child;
530e8392
KB
16749 struct type *type;
16750
16751 type = read_type_die (die, cu);
16752 new_symbol (die, type, cu);
5d7cb8df 16753
5d7cb8df
JK
16754 while (child_die && child_die->tag)
16755 {
16756 process_die (child_die, cu);
436c571c 16757 child_die = child_die->sibling;
5d7cb8df
JK
16758 }
16759}
16760
38d518c9
EZ
16761/* Return the name of the namespace represented by DIE. Set
16762 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16763 namespace. */
16764
16765static const char *
e142c38c 16766namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16767{
16768 struct die_info *current_die;
16769 const char *name = NULL;
16770
16771 /* Loop through the extensions until we find a name. */
16772
16773 for (current_die = die;
16774 current_die != NULL;
f2f0e013 16775 current_die = dwarf2_extension (die, &cu))
38d518c9 16776 {
96553a0c
DE
16777 /* We don't use dwarf2_name here so that we can detect the absence
16778 of a name -> anonymous namespace. */
7d45c7c3 16779 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16780
38d518c9
EZ
16781 if (name != NULL)
16782 break;
16783 }
16784
16785 /* Is it an anonymous namespace? */
16786
16787 *is_anonymous = (name == NULL);
16788 if (*is_anonymous)
2b1dbab0 16789 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16790
16791 return name;
d9fa45fe
DC
16792}
16793
c906108c
SS
16794/* Extract all information from a DW_TAG_pointer_type DIE and add to
16795 the user defined type vector. */
16796
f792889a 16797static struct type *
e7c27a73 16798read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16799{
5e22e966 16800 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16801 struct comp_unit_head *cu_header = &cu->header;
c906108c 16802 struct type *type;
8b2dbe47
KB
16803 struct attribute *attr_byte_size;
16804 struct attribute *attr_address_class;
16805 int byte_size, addr_class;
7e314c57
JK
16806 struct type *target_type;
16807
16808 target_type = die_type (die, cu);
c906108c 16809
7e314c57
JK
16810 /* The die_type call above may have already set the type for this DIE. */
16811 type = get_die_type (die, cu);
16812 if (type)
16813 return type;
16814
16815 type = lookup_pointer_type (target_type);
8b2dbe47 16816
e142c38c 16817 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16818 if (attr_byte_size)
16819 byte_size = DW_UNSND (attr_byte_size);
c906108c 16820 else
8b2dbe47
KB
16821 byte_size = cu_header->addr_size;
16822
e142c38c 16823 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16824 if (attr_address_class)
16825 addr_class = DW_UNSND (attr_address_class);
16826 else
16827 addr_class = DW_ADDR_none;
16828
2b4424c3
TT
16829 ULONGEST alignment = get_alignment (cu, die);
16830
16831 /* If the pointer size, alignment, or address class is different
16832 than the default, create a type variant marked as such and set
16833 the length accordingly. */
16834 if (TYPE_LENGTH (type) != byte_size
16835 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16836 && alignment != TYPE_RAW_ALIGN (type))
16837 || addr_class != DW_ADDR_none)
c906108c 16838 {
5e2b427d 16839 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16840 {
16841 int type_flags;
16842
849957d9 16843 type_flags = gdbarch_address_class_type_flags
5e2b427d 16844 (gdbarch, byte_size, addr_class);
876cecd0
TT
16845 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16846 == 0);
8b2dbe47
KB
16847 type = make_type_with_address_space (type, type_flags);
16848 }
16849 else if (TYPE_LENGTH (type) != byte_size)
16850 {
b98664d3 16851 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16852 }
2b4424c3
TT
16853 else if (TYPE_RAW_ALIGN (type) != alignment)
16854 {
b98664d3 16855 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16856 " - DIE at %s [in module %s]"),
16857 sect_offset_str (die->sect_off),
5e22e966 16858 objfile_name (cu->per_objfile->objfile));
2b4424c3 16859 }
6e70227d 16860 else
9a619af0
MS
16861 {
16862 /* Should we also complain about unhandled address classes? */
16863 }
c906108c 16864 }
8b2dbe47
KB
16865
16866 TYPE_LENGTH (type) = byte_size;
2b4424c3 16867 set_type_align (type, alignment);
f792889a 16868 return set_die_type (die, type, cu);
c906108c
SS
16869}
16870
16871/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16872 the user defined type vector. */
16873
f792889a 16874static struct type *
e7c27a73 16875read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16876{
16877 struct type *type;
16878 struct type *to_type;
16879 struct type *domain;
16880
e7c27a73
DJ
16881 to_type = die_type (die, cu);
16882 domain = die_containing_type (die, cu);
0d5de010 16883
7e314c57
JK
16884 /* The calls above may have already set the type for this DIE. */
16885 type = get_die_type (die, cu);
16886 if (type)
16887 return type;
16888
78134374 16889 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16890 type = lookup_methodptr_type (to_type);
78134374 16891 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16892 {
5e22e966 16893 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16894
16895 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16896 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16897 TYPE_VARARGS (to_type));
16898 type = lookup_methodptr_type (new_type);
16899 }
0d5de010
DJ
16900 else
16901 type = lookup_memberptr_type (to_type, domain);
c906108c 16902
f792889a 16903 return set_die_type (die, type, cu);
c906108c
SS
16904}
16905
4297a3f0 16906/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16907 the user defined type vector. */
16908
f792889a 16909static struct type *
4297a3f0
AV
16910read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16911 enum type_code refcode)
c906108c 16912{
e7c27a73 16913 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16914 struct type *type, *target_type;
c906108c
SS
16915 struct attribute *attr;
16916
4297a3f0
AV
16917 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16918
7e314c57
JK
16919 target_type = die_type (die, cu);
16920
16921 /* The die_type call above may have already set the type for this DIE. */
16922 type = get_die_type (die, cu);
16923 if (type)
16924 return type;
16925
4297a3f0 16926 type = lookup_reference_type (target_type, refcode);
e142c38c 16927 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16928 if (attr != nullptr)
c906108c
SS
16929 {
16930 TYPE_LENGTH (type) = DW_UNSND (attr);
16931 }
16932 else
16933 {
107d2387 16934 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16935 }
2b4424c3 16936 maybe_set_alignment (cu, die, type);
f792889a 16937 return set_die_type (die, type, cu);
c906108c
SS
16938}
16939
cf363f18
MW
16940/* Add the given cv-qualifiers to the element type of the array. GCC
16941 outputs DWARF type qualifiers that apply to an array, not the
16942 element type. But GDB relies on the array element type to carry
16943 the cv-qualifiers. This mimics section 6.7.3 of the C99
16944 specification. */
16945
16946static struct type *
16947add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16948 struct type *base_type, int cnst, int voltl)
16949{
16950 struct type *el_type, *inner_array;
16951
16952 base_type = copy_type (base_type);
16953 inner_array = base_type;
16954
78134374 16955 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16956 {
16957 TYPE_TARGET_TYPE (inner_array) =
16958 copy_type (TYPE_TARGET_TYPE (inner_array));
16959 inner_array = TYPE_TARGET_TYPE (inner_array);
16960 }
16961
16962 el_type = TYPE_TARGET_TYPE (inner_array);
16963 cnst |= TYPE_CONST (el_type);
16964 voltl |= TYPE_VOLATILE (el_type);
16965 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16966
16967 return set_die_type (die, base_type, cu);
16968}
16969
f792889a 16970static struct type *
e7c27a73 16971read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16972{
f792889a 16973 struct type *base_type, *cv_type;
c906108c 16974
e7c27a73 16975 base_type = die_type (die, cu);
7e314c57
JK
16976
16977 /* The die_type call above may have already set the type for this DIE. */
16978 cv_type = get_die_type (die, cu);
16979 if (cv_type)
16980 return cv_type;
16981
2f608a3a
KW
16982 /* In case the const qualifier is applied to an array type, the element type
16983 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16984 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16985 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16986
f792889a
DJ
16987 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16988 return set_die_type (die, cv_type, cu);
c906108c
SS
16989}
16990
f792889a 16991static struct type *
e7c27a73 16992read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16993{
f792889a 16994 struct type *base_type, *cv_type;
c906108c 16995
e7c27a73 16996 base_type = die_type (die, cu);
7e314c57
JK
16997
16998 /* The die_type call above may have already set the type for this DIE. */
16999 cv_type = get_die_type (die, cu);
17000 if (cv_type)
17001 return cv_type;
17002
cf363f18
MW
17003 /* In case the volatile qualifier is applied to an array type, the
17004 element type is so qualified, not the array type (section 6.7.3
17005 of C99). */
78134374 17006 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17007 return add_array_cv_type (die, cu, base_type, 0, 1);
17008
f792889a
DJ
17009 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17010 return set_die_type (die, cv_type, cu);
c906108c
SS
17011}
17012
06d66ee9
TT
17013/* Handle DW_TAG_restrict_type. */
17014
17015static struct type *
17016read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17017{
17018 struct type *base_type, *cv_type;
17019
17020 base_type = die_type (die, cu);
17021
17022 /* The die_type call above may have already set the type for this DIE. */
17023 cv_type = get_die_type (die, cu);
17024 if (cv_type)
17025 return cv_type;
17026
17027 cv_type = make_restrict_type (base_type);
17028 return set_die_type (die, cv_type, cu);
17029}
17030
a2c2acaf
MW
17031/* Handle DW_TAG_atomic_type. */
17032
17033static struct type *
17034read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17035{
17036 struct type *base_type, *cv_type;
17037
17038 base_type = die_type (die, cu);
17039
17040 /* The die_type call above may have already set the type for this DIE. */
17041 cv_type = get_die_type (die, cu);
17042 if (cv_type)
17043 return cv_type;
17044
17045 cv_type = make_atomic_type (base_type);
17046 return set_die_type (die, cv_type, cu);
17047}
17048
c906108c
SS
17049/* Extract all information from a DW_TAG_string_type DIE and add to
17050 the user defined type vector. It isn't really a user defined type,
17051 but it behaves like one, with other DIE's using an AT_user_def_type
17052 attribute to reference it. */
17053
f792889a 17054static struct type *
e7c27a73 17055read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17056{
5e22e966 17057 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17058 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17059 struct type *type, *range_type, *index_type, *char_type;
17060 struct attribute *attr;
216a7e6b
AB
17061 struct dynamic_prop prop;
17062 bool length_is_constant = true;
17063 LONGEST length;
17064
17065 /* There are a couple of places where bit sizes might be made use of
17066 when parsing a DW_TAG_string_type, however, no producer that we know
17067 of make use of these. Handling bit sizes that are a multiple of the
17068 byte size is easy enough, but what about other bit sizes? Lets deal
17069 with that problem when we have to. Warn about these attributes being
17070 unsupported, then parse the type and ignore them like we always
17071 have. */
17072 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17073 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17074 {
17075 static bool warning_printed = false;
17076 if (!warning_printed)
17077 {
17078 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17079 "currently supported on DW_TAG_string_type."));
17080 warning_printed = true;
17081 }
17082 }
c906108c 17083
e142c38c 17084 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17085 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17086 {
17087 /* The string length describes the location at which the length of
17088 the string can be found. The size of the length field can be
17089 specified with one of the attributes below. */
17090 struct type *prop_type;
17091 struct attribute *len
17092 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17093 if (len == nullptr)
17094 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17095 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17096 {
17097 /* Pass 0 as the default as we know this attribute is constant
17098 and the default value will not be returned. */
0826b30a 17099 LONGEST sz = len->constant_value (0);
293e7e51 17100 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17101 }
17102 else
17103 {
17104 /* If the size is not specified then we assume it is the size of
17105 an address on this target. */
293e7e51 17106 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17107 }
17108
17109 /* Convert the attribute into a dynamic property. */
17110 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17111 length = 1;
17112 else
17113 length_is_constant = false;
17114 }
17115 else if (attr != nullptr)
17116 {
17117 /* This DW_AT_string_length just contains the length with no
17118 indirection. There's no need to create a dynamic property in this
17119 case. Pass 0 for the default value as we know it will not be
17120 returned in this case. */
0826b30a 17121 length = attr->constant_value (0);
216a7e6b
AB
17122 }
17123 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17124 {
216a7e6b 17125 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17126 length = attr->constant_value (1);
c906108c
SS
17127 }
17128 else
17129 {
216a7e6b
AB
17130 /* Use 1 as a fallback length if we have nothing else. */
17131 length = 1;
c906108c 17132 }
6ccb9162 17133
46bf5051 17134 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17135 if (length_is_constant)
17136 range_type = create_static_range_type (NULL, index_type, 1, length);
17137 else
17138 {
17139 struct dynamic_prop low_bound;
17140
17141 low_bound.kind = PROP_CONST;
17142 low_bound.data.const_val = 1;
17143 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17144 }
3b7538c0
UW
17145 char_type = language_string_char_type (cu->language_defn, gdbarch);
17146 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17147
f792889a 17148 return set_die_type (die, type, cu);
c906108c
SS
17149}
17150
4d804846
JB
17151/* Assuming that DIE corresponds to a function, returns nonzero
17152 if the function is prototyped. */
17153
17154static int
17155prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17156{
17157 struct attribute *attr;
17158
17159 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17160 if (attr && (DW_UNSND (attr) != 0))
17161 return 1;
17162
17163 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17164 is only meaningful for C, but the concept also extends to other
4d804846
JB
17165 languages that allow unprototyped functions (Eg: Objective C).
17166 For all other languages, assume that functions are always
17167 prototyped. */
17168 if (cu->language != language_c
17169 && cu->language != language_objc
17170 && cu->language != language_opencl)
17171 return 1;
17172
17173 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17174 prototyped and unprototyped functions; default to prototyped,
17175 since that is more common in modern code (and RealView warns
17176 about unprototyped functions). */
17177 if (producer_is_realview (cu->producer))
17178 return 1;
17179
17180 return 0;
17181}
17182
c906108c
SS
17183/* Handle DIES due to C code like:
17184
17185 struct foo
c5aa993b
JM
17186 {
17187 int (*funcp)(int a, long l);
17188 int b;
17189 };
c906108c 17190
0963b4bd 17191 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17192
f792889a 17193static struct type *
e7c27a73 17194read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17195{
5e22e966 17196 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17197 struct type *type; /* Type that this function returns. */
17198 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17199 struct attribute *attr;
17200
e7c27a73 17201 type = die_type (die, cu);
7e314c57
JK
17202
17203 /* The die_type call above may have already set the type for this DIE. */
17204 ftype = get_die_type (die, cu);
17205 if (ftype)
17206 return ftype;
17207
0c8b41f1 17208 ftype = lookup_function_type (type);
c906108c 17209
4d804846 17210 if (prototyped_function_p (die, cu))
a6c727b2 17211 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17212
c055b101
CV
17213 /* Store the calling convention in the type if it's available in
17214 the subroutine die. Otherwise set the calling convention to
17215 the default value DW_CC_normal. */
17216 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17217 if (attr != nullptr
17218 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17219 TYPE_CALLING_CONVENTION (ftype)
17220 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17221 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17222 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17223 else
17224 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17225
743649fd
MW
17226 /* Record whether the function returns normally to its caller or not
17227 if the DWARF producer set that information. */
17228 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17229 if (attr && (DW_UNSND (attr) != 0))
17230 TYPE_NO_RETURN (ftype) = 1;
17231
76c10ea2
GM
17232 /* We need to add the subroutine type to the die immediately so
17233 we don't infinitely recurse when dealing with parameters
0963b4bd 17234 declared as the same subroutine type. */
76c10ea2 17235 set_die_type (die, ftype, cu);
6e70227d 17236
639d11d3 17237 if (die->child != NULL)
c906108c 17238 {
bb5ed363 17239 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17240 struct die_info *child_die;
8072405b 17241 int nparams, iparams;
c906108c
SS
17242
17243 /* Count the number of parameters.
17244 FIXME: GDB currently ignores vararg functions, but knows about
17245 vararg member functions. */
8072405b 17246 nparams = 0;
639d11d3 17247 child_die = die->child;
c906108c
SS
17248 while (child_die && child_die->tag)
17249 {
17250 if (child_die->tag == DW_TAG_formal_parameter)
17251 nparams++;
17252 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17253 TYPE_VARARGS (ftype) = 1;
436c571c 17254 child_die = child_die->sibling;
c906108c
SS
17255 }
17256
17257 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17258 ftype->set_num_fields (nparams);
3cabb6b0
SM
17259 ftype->set_fields
17260 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17261
8072405b
JK
17262 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17263 even if we error out during the parameters reading below. */
17264 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17265 ftype->field (iparams).set_type (void_type);
8072405b
JK
17266
17267 iparams = 0;
639d11d3 17268 child_die = die->child;
c906108c
SS
17269 while (child_die && child_die->tag)
17270 {
17271 if (child_die->tag == DW_TAG_formal_parameter)
17272 {
3ce3b1ba
PA
17273 struct type *arg_type;
17274
17275 /* DWARF version 2 has no clean way to discern C++
17276 static and non-static member functions. G++ helps
17277 GDB by marking the first parameter for non-static
17278 member functions (which is the this pointer) as
17279 artificial. We pass this information to
17280 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17281
17282 DWARF version 3 added DW_AT_object_pointer, which GCC
17283 4.5 does not yet generate. */
e142c38c 17284 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17285 if (attr != nullptr)
c906108c
SS
17286 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17287 else
9c37b5ae 17288 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17289 arg_type = die_type (child_die, cu);
17290
17291 /* RealView does not mark THIS as const, which the testsuite
17292 expects. GCC marks THIS as const in method definitions,
17293 but not in the class specifications (GCC PR 43053). */
17294 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17295 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17296 {
17297 int is_this = 0;
17298 struct dwarf2_cu *arg_cu = cu;
17299 const char *name = dwarf2_name (child_die, cu);
17300
17301 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17302 if (attr != nullptr)
3ce3b1ba
PA
17303 {
17304 /* If the compiler emits this, use it. */
17305 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17306 is_this = 1;
17307 }
17308 else if (name && strcmp (name, "this") == 0)
17309 /* Function definitions will have the argument names. */
17310 is_this = 1;
17311 else if (name == NULL && iparams == 0)
17312 /* Declarations may not have the names, so like
17313 elsewhere in GDB, assume an artificial first
17314 argument is "this". */
17315 is_this = 1;
17316
17317 if (is_this)
17318 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17319 arg_type, 0);
17320 }
17321
5d14b6e5 17322 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17323 iparams++;
17324 }
436c571c 17325 child_die = child_die->sibling;
c906108c
SS
17326 }
17327 }
17328
76c10ea2 17329 return ftype;
c906108c
SS
17330}
17331
f792889a 17332static struct type *
e7c27a73 17333read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17334{
5e22e966 17335 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17336 const char *name = NULL;
3c8e0968 17337 struct type *this_type, *target_type;
c906108c 17338
94af9270 17339 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17340 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17341 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17342 set_die_type (die, this_type, cu);
3c8e0968
DE
17343 target_type = die_type (die, cu);
17344 if (target_type != this_type)
17345 TYPE_TARGET_TYPE (this_type) = target_type;
17346 else
17347 {
17348 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17349 spec and cause infinite loops in GDB. */
b98664d3 17350 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17351 "- DIE at %s [in module %s]"),
17352 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17353 TYPE_TARGET_TYPE (this_type) = NULL;
17354 }
e4003a34
TV
17355 if (name == NULL)
17356 {
17357 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17358 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17359 Handle these by just returning the target type, rather than
17360 constructing an anonymous typedef type and trying to handle this
17361 elsewhere. */
17362 set_die_type (die, target_type, cu);
17363 return target_type;
17364 }
f792889a 17365 return this_type;
c906108c
SS
17366}
17367
9b790ce7
UW
17368/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17369 (which may be different from NAME) to the architecture back-end to allow
17370 it to guess the correct format if necessary. */
17371
17372static struct type *
17373dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17374 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17375{
08feed99 17376 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17377 const struct floatformat **format;
17378 struct type *type;
17379
17380 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17381 if (format)
103a685e 17382 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17383 else
77b7c781 17384 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17385
17386 return type;
17387}
17388
eb77c9df
AB
17389/* Allocate an integer type of size BITS and name NAME. */
17390
17391static struct type *
17392dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17393 int bits, int unsigned_p, const char *name)
17394{
17395 struct type *type;
17396
17397 /* Versions of Intel's C Compiler generate an integer type called "void"
17398 instead of using DW_TAG_unspecified_type. This has been seen on
17399 at least versions 14, 17, and 18. */
35ee2dc2
AB
17400 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17401 && strcmp (name, "void") == 0)
eb77c9df
AB
17402 type = objfile_type (objfile)->builtin_void;
17403 else
17404 type = init_integer_type (objfile, bits, unsigned_p, name);
17405
17406 return type;
17407}
17408
8bdc1658
AB
17409/* Initialise and return a floating point type of size BITS suitable for
17410 use as a component of a complex number. The NAME_HINT is passed through
17411 when initialising the floating point type and is the name of the complex
17412 type.
17413
17414 As DWARF doesn't currently provide an explicit name for the components
17415 of a complex number, but it can be helpful to have these components
17416 named, we try to select a suitable name based on the size of the
17417 component. */
17418static struct type *
17419dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17420 struct objfile *objfile,
103a685e
TT
17421 int bits, const char *name_hint,
17422 enum bfd_endian byte_order)
8bdc1658 17423{
08feed99 17424 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17425 struct type *tt = nullptr;
17426
35add35e
AB
17427 /* Try to find a suitable floating point builtin type of size BITS.
17428 We're going to use the name of this type as the name for the complex
17429 target type that we are about to create. */
1db455a7 17430 switch (cu->language)
8bdc1658 17431 {
1db455a7
AB
17432 case language_fortran:
17433 switch (bits)
17434 {
17435 case 32:
17436 tt = builtin_f_type (gdbarch)->builtin_real;
17437 break;
17438 case 64:
17439 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17440 break;
17441 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17442 case 128:
17443 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17444 break;
17445 }
8bdc1658 17446 break;
1db455a7
AB
17447 default:
17448 switch (bits)
17449 {
17450 case 32:
17451 tt = builtin_type (gdbarch)->builtin_float;
17452 break;
17453 case 64:
17454 tt = builtin_type (gdbarch)->builtin_double;
17455 break;
17456 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17457 case 128:
17458 tt = builtin_type (gdbarch)->builtin_long_double;
17459 break;
17460 }
8bdc1658
AB
17461 break;
17462 }
17463
35add35e
AB
17464 /* If the type we found doesn't match the size we were looking for, then
17465 pretend we didn't find a type at all, the complex target type we
17466 create will then be nameless. */
a12e5744 17467 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17468 tt = nullptr;
17469
7d93a1e0 17470 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17471 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17472}
17473
c906108c
SS
17474/* Find a representation of a given base type and install
17475 it in the TYPE field of the die. */
17476
f792889a 17477static struct type *
e7c27a73 17478read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17479{
5e22e966 17480 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17481 struct type *type;
17482 struct attribute *attr;
19f392bc 17483 int encoding = 0, bits = 0;
15d034d0 17484 const char *name;
34877895 17485 gdbarch *arch;
c906108c 17486
e142c38c 17487 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17488 if (attr != nullptr)
34877895 17489 encoding = DW_UNSND (attr);
e142c38c 17490 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17491 if (attr != nullptr)
34877895 17492 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17493 name = dwarf2_name (die, cu);
6ccb9162 17494 if (!name)
34877895 17495 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17496
08feed99 17497 arch = objfile->arch ();
103a685e
TT
17498 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17499
34877895
PJ
17500 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17501 if (attr)
103a685e
TT
17502 {
17503 int endianity = DW_UNSND (attr);
17504
17505 switch (endianity)
17506 {
17507 case DW_END_big:
17508 byte_order = BFD_ENDIAN_BIG;
17509 break;
17510 case DW_END_little:
17511 byte_order = BFD_ENDIAN_LITTLE;
17512 break;
17513 default:
17514 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17515 break;
17516 }
17517 }
6ccb9162
UW
17518
17519 switch (encoding)
c906108c 17520 {
6ccb9162
UW
17521 case DW_ATE_address:
17522 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17523 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17524 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17525 break;
17526 case DW_ATE_boolean:
19f392bc 17527 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17528 break;
17529 case DW_ATE_complex_float:
103a685e
TT
17530 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17531 byte_order);
78134374 17532 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17533 {
17534 if (name == nullptr)
17535 {
17536 struct obstack *obstack
5e22e966 17537 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17538 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17539 nullptr);
17540 }
17541 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17542 }
17543 else
17544 type = init_complex_type (name, type);
6ccb9162
UW
17545 break;
17546 case DW_ATE_decimal_float:
19f392bc 17547 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17548 break;
17549 case DW_ATE_float:
103a685e 17550 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17551 break;
17552 case DW_ATE_signed:
eb77c9df 17553 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17554 break;
17555 case DW_ATE_unsigned:
3b2b8fea
TT
17556 if (cu->language == language_fortran
17557 && name
61012eef 17558 && startswith (name, "character("))
19f392bc
UW
17559 type = init_character_type (objfile, bits, 1, name);
17560 else
eb77c9df 17561 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17562 break;
17563 case DW_ATE_signed_char:
6e70227d 17564 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17565 || cu->language == language_pascal
17566 || cu->language == language_fortran)
19f392bc
UW
17567 type = init_character_type (objfile, bits, 0, name);
17568 else
eb77c9df 17569 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17570 break;
17571 case DW_ATE_unsigned_char:
868a0084 17572 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17573 || cu->language == language_pascal
c44af4eb
TT
17574 || cu->language == language_fortran
17575 || cu->language == language_rust)
19f392bc
UW
17576 type = init_character_type (objfile, bits, 1, name);
17577 else
eb77c9df 17578 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17579 break;
75079b2b 17580 case DW_ATE_UTF:
53e710ac 17581 {
53e710ac
PA
17582 if (bits == 16)
17583 type = builtin_type (arch)->builtin_char16;
17584 else if (bits == 32)
17585 type = builtin_type (arch)->builtin_char32;
17586 else
17587 {
b98664d3 17588 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17589 bits);
eb77c9df 17590 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17591 }
17592 return set_die_type (die, type, cu);
17593 }
75079b2b
TT
17594 break;
17595
6ccb9162 17596 default:
b98664d3 17597 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17598 dwarf_type_encoding_name (encoding));
77b7c781 17599 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17600 break;
c906108c 17601 }
6ccb9162 17602
0114d602 17603 if (name && strcmp (name, "char") == 0)
876cecd0 17604 TYPE_NOSIGN (type) = 1;
0114d602 17605
2b4424c3
TT
17606 maybe_set_alignment (cu, die, type);
17607
103a685e 17608 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17609
f792889a 17610 return set_die_type (die, type, cu);
c906108c
SS
17611}
17612
80180f79
SA
17613/* Parse dwarf attribute if it's a block, reference or constant and put the
17614 resulting value of the attribute into struct bound_prop.
17615 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17616
17617static int
17618attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17619 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17620 struct type *default_type)
80180f79
SA
17621{
17622 struct dwarf2_property_baton *baton;
5e22e966 17623 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17624 struct objfile *objfile = per_objfile->objfile;
17625 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17626
9a49df9d
AB
17627 gdb_assert (default_type != NULL);
17628
80180f79
SA
17629 if (attr == NULL || prop == NULL)
17630 return 0;
17631
4fc6c0d5 17632 if (attr->form_is_block ())
80180f79 17633 {
8d749320 17634 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17635 baton->property_type = default_type;
80180f79 17636 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17637 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17638 baton->locexpr.size = DW_BLOCK (attr)->size;
17639 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17640 switch (attr->name)
17641 {
17642 case DW_AT_string_length:
17643 baton->locexpr.is_reference = true;
17644 break;
17645 default:
17646 baton->locexpr.is_reference = false;
17647 break;
17648 }
80180f79
SA
17649 prop->data.baton = baton;
17650 prop->kind = PROP_LOCEXPR;
17651 gdb_assert (prop->data.baton != NULL);
17652 }
cd6c91b4 17653 else if (attr->form_is_ref ())
80180f79
SA
17654 {
17655 struct dwarf2_cu *target_cu = cu;
17656 struct die_info *target_die;
17657 struct attribute *target_attr;
17658
17659 target_die = follow_die_ref (die, attr, &target_cu);
17660 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17661 if (target_attr == NULL)
17662 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17663 target_cu);
80180f79
SA
17664 if (target_attr == NULL)
17665 return 0;
17666
df25ebbd 17667 switch (target_attr->name)
80180f79 17668 {
df25ebbd 17669 case DW_AT_location:
cd6c91b4 17670 if (target_attr->form_is_section_offset ())
df25ebbd 17671 {
8d749320 17672 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17673 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17674 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17675 prop->data.baton = baton;
17676 prop->kind = PROP_LOCLIST;
17677 gdb_assert (prop->data.baton != NULL);
17678 }
4fc6c0d5 17679 else if (target_attr->form_is_block ())
df25ebbd 17680 {
8d749320 17681 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17682 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17683 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17684 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17685 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17686 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17687 baton->locexpr.is_reference = true;
df25ebbd
JB
17688 prop->data.baton = baton;
17689 prop->kind = PROP_LOCEXPR;
17690 gdb_assert (prop->data.baton != NULL);
17691 }
17692 else
17693 {
17694 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17695 "dynamic property");
17696 return 0;
17697 }
17698 break;
17699 case DW_AT_data_member_location:
17700 {
17701 LONGEST offset;
17702
17703 if (!handle_data_member_location (target_die, target_cu,
17704 &offset))
17705 return 0;
17706
8d749320 17707 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17708 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17709 target_cu);
df25ebbd
JB
17710 baton->offset_info.offset = offset;
17711 baton->offset_info.type = die_type (target_die, target_cu);
17712 prop->data.baton = baton;
17713 prop->kind = PROP_ADDR_OFFSET;
17714 break;
17715 }
80180f79
SA
17716 }
17717 }
cd6c91b4 17718 else if (attr->form_is_constant ())
80180f79 17719 {
0826b30a 17720 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17721 prop->kind = PROP_CONST;
17722 }
17723 else
17724 {
17725 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17726 dwarf2_name (die, cu));
17727 return 0;
17728 }
17729
17730 return 1;
17731}
17732
09ba997f 17733/* See read.h. */
9a49df9d 17734
09ba997f 17735struct type *
293e7e51 17736dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17737{
9a49df9d
AB
17738 struct type *int_type;
17739
17740 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17741#define TRY_TYPE(F) \
17742 int_type = (unsigned_p \
17743 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17744 : objfile_type (objfile)->builtin_ ## F); \
17745 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17746 return int_type
17747
17748 TRY_TYPE (char);
17749 TRY_TYPE (short);
17750 TRY_TYPE (int);
17751 TRY_TYPE (long);
17752 TRY_TYPE (long_long);
17753
17754#undef TRY_TYPE
17755
17756 gdb_assert_not_reached ("unable to find suitable integer type");
17757}
17758
09ba997f 17759/* See read.h. */
11a8b164 17760
09ba997f 17761struct type *
293e7e51 17762dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17763{
293e7e51
SM
17764 int addr_size = this->per_cu->addr_size ();
17765 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17766}
17767
b86352cf
AB
17768/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17769 present (which is valid) then compute the default type based on the
17770 compilation units address size. */
17771
17772static struct type *
17773read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17774{
17775 struct type *index_type = die_type (die, cu);
17776
17777 /* Dwarf-2 specifications explicitly allows to create subrange types
17778 without specifying a base type.
17779 In that case, the base type must be set to the type of
17780 the lower bound, upper bound or count, in that order, if any of these
17781 three attributes references an object that has a type.
17782 If no base type is found, the Dwarf-2 specifications say that
17783 a signed integer type of size equal to the size of an address should
17784 be used.
17785 For the following C code: `extern char gdb_int [];'
17786 GCC produces an empty range DIE.
17787 FIXME: muller/2010-05-28: Possible references to object for low bound,
17788 high bound or count are not yet handled by this code. */
78134374 17789 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17790 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17791
17792 return index_type;
17793}
17794
a02abb62
JB
17795/* Read the given DW_AT_subrange DIE. */
17796
f792889a 17797static struct type *
a02abb62
JB
17798read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17799{
4c9ad8c2 17800 struct type *base_type, *orig_base_type;
a02abb62
JB
17801 struct type *range_type;
17802 struct attribute *attr;
729efb13 17803 struct dynamic_prop low, high;
4fae6e18 17804 int low_default_is_valid;
c451ebe5 17805 int high_bound_is_count = 0;
15d034d0 17806 const char *name;
d359392f 17807 ULONGEST negative_mask;
e77813c8 17808
b86352cf
AB
17809 orig_base_type = read_subrange_index_type (die, cu);
17810
4c9ad8c2
TT
17811 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17812 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17813 creating the range type, but we use the result of check_typedef
17814 when examining properties of the type. */
17815 base_type = check_typedef (orig_base_type);
a02abb62 17816
7e314c57
JK
17817 /* The die_type call above may have already set the type for this DIE. */
17818 range_type = get_die_type (die, cu);
17819 if (range_type)
17820 return range_type;
17821
729efb13
SA
17822 low.kind = PROP_CONST;
17823 high.kind = PROP_CONST;
17824 high.data.const_val = 0;
17825
4fae6e18
JK
17826 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17827 omitting DW_AT_lower_bound. */
17828 switch (cu->language)
6e70227d 17829 {
4fae6e18
JK
17830 case language_c:
17831 case language_cplus:
729efb13 17832 low.data.const_val = 0;
4fae6e18
JK
17833 low_default_is_valid = 1;
17834 break;
17835 case language_fortran:
729efb13 17836 low.data.const_val = 1;
4fae6e18
JK
17837 low_default_is_valid = 1;
17838 break;
17839 case language_d:
4fae6e18 17840 case language_objc:
c44af4eb 17841 case language_rust:
729efb13 17842 low.data.const_val = 0;
4fae6e18
JK
17843 low_default_is_valid = (cu->header.version >= 4);
17844 break;
17845 case language_ada:
17846 case language_m2:
17847 case language_pascal:
729efb13 17848 low.data.const_val = 1;
4fae6e18
JK
17849 low_default_is_valid = (cu->header.version >= 4);
17850 break;
17851 default:
729efb13 17852 low.data.const_val = 0;
4fae6e18
JK
17853 low_default_is_valid = 0;
17854 break;
a02abb62
JB
17855 }
17856
e142c38c 17857 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17858 if (attr != nullptr)
9a49df9d 17859 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17860 else if (!low_default_is_valid)
b98664d3 17861 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17862 "- DIE at %s [in module %s]"),
17863 sect_offset_str (die->sect_off),
5e22e966 17864 objfile_name (cu->per_objfile->objfile));
a02abb62 17865
506f5c41
TV
17866 struct attribute *attr_ub, *attr_count;
17867 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17868 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17869 {
506f5c41 17870 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17871 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17872 {
c451ebe5
SA
17873 /* If bounds are constant do the final calculation here. */
17874 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17875 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17876 else
17877 high_bound_is_count = 1;
c2ff108b 17878 }
506f5c41
TV
17879 else
17880 {
17881 if (attr_ub != NULL)
17882 complaint (_("Unresolved DW_AT_upper_bound "
17883 "- DIE at %s [in module %s]"),
17884 sect_offset_str (die->sect_off),
5e22e966 17885 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17886 if (attr_count != NULL)
17887 complaint (_("Unresolved DW_AT_count "
17888 "- DIE at %s [in module %s]"),
17889 sect_offset_str (die->sect_off),
5e22e966 17890 objfile_name (cu->per_objfile->objfile));
506f5c41 17891 }
e77813c8 17892 }
a02abb62 17893
4e962e74
TT
17894 LONGEST bias = 0;
17895 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17896 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17897 bias = bias_attr->constant_value (0);
4e962e74 17898
dbb9c2b1
JB
17899 /* Normally, the DWARF producers are expected to use a signed
17900 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17901 But this is unfortunately not always the case, as witnessed
17902 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17903 is used instead. To work around that ambiguity, we treat
17904 the bounds as signed, and thus sign-extend their values, when
17905 the base type is signed. */
6e70227d 17906 negative_mask =
d359392f 17907 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17908 if (low.kind == PROP_CONST
17909 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17910 low.data.const_val |= negative_mask;
17911 if (high.kind == PROP_CONST
17912 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17913 high.data.const_val |= negative_mask;
43bbcdc2 17914
5bbd8269
AB
17915 /* Check for bit and byte strides. */
17916 struct dynamic_prop byte_stride_prop;
17917 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17918 if (attr_byte_stride != nullptr)
17919 {
293e7e51 17920 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17921 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17922 prop_type);
17923 }
17924
17925 struct dynamic_prop bit_stride_prop;
17926 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17927 if (attr_bit_stride != nullptr)
17928 {
17929 /* It only makes sense to have either a bit or byte stride. */
17930 if (attr_byte_stride != nullptr)
17931 {
17932 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17933 "- DIE at %s [in module %s]"),
17934 sect_offset_str (die->sect_off),
5e22e966 17935 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17936 attr_bit_stride = nullptr;
17937 }
17938 else
17939 {
293e7e51 17940 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17941 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17942 prop_type);
17943 }
17944 }
17945
17946 if (attr_byte_stride != nullptr
17947 || attr_bit_stride != nullptr)
17948 {
17949 bool byte_stride_p = (attr_byte_stride != nullptr);
17950 struct dynamic_prop *stride
17951 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17952
17953 range_type
17954 = create_range_type_with_stride (NULL, orig_base_type, &low,
17955 &high, bias, stride, byte_stride_p);
17956 }
17957 else
17958 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17959
c451ebe5
SA
17960 if (high_bound_is_count)
17961 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17962
c2ff108b
JK
17963 /* Ada expects an empty array on no boundary attributes. */
17964 if (attr == NULL && cu->language != language_ada)
729efb13 17965 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17966
39cbfefa
DJ
17967 name = dwarf2_name (die, cu);
17968 if (name)
d0e39ea2 17969 range_type->set_name (name);
6e70227d 17970
e142c38c 17971 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17972 if (attr != nullptr)
a02abb62
JB
17973 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17974
2b4424c3
TT
17975 maybe_set_alignment (cu, die, range_type);
17976
7e314c57
JK
17977 set_die_type (die, range_type, cu);
17978
17979 /* set_die_type should be already done. */
b4ba55a1
JB
17980 set_descriptive_type (range_type, die, cu);
17981
7e314c57 17982 return range_type;
a02abb62 17983}
6e70227d 17984
f792889a 17985static struct type *
81a17f79
JB
17986read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17987{
17988 struct type *type;
81a17f79 17989
5e22e966 17990 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17991 type->set_name (dwarf2_name (die, cu));
81a17f79 17992
74a2f8ff 17993 /* In Ada, an unspecified type is typically used when the description
85102364 17994 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17995 such a type, we treat it as a stub, and try to resolve it later on,
17996 when needed. */
17997 if (cu->language == language_ada)
17998 TYPE_STUB (type) = 1;
17999
f792889a 18000 return set_die_type (die, type, cu);
81a17f79 18001}
a02abb62 18002
639d11d3
DC
18003/* Read a single die and all its descendents. Set the die's sibling
18004 field to NULL; set other fields in the die correctly, and set all
18005 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18006 location of the info_ptr after reading all of those dies. PARENT
18007 is the parent of the die in question. */
18008
18009static struct die_info *
dee91e82 18010read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18011 const gdb_byte *info_ptr,
18012 const gdb_byte **new_info_ptr,
dee91e82 18013 struct die_info *parent)
639d11d3
DC
18014{
18015 struct die_info *die;
d521ce57 18016 const gdb_byte *cur_ptr;
639d11d3 18017
3e225074 18018 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18019 if (die == NULL)
18020 {
18021 *new_info_ptr = cur_ptr;
18022 return NULL;
18023 }
93311388 18024 store_in_ref_table (die, reader->cu);
639d11d3 18025
3e225074 18026 if (die->has_children)
bf6af496 18027 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18028 else
18029 {
18030 die->child = NULL;
18031 *new_info_ptr = cur_ptr;
18032 }
18033
18034 die->sibling = NULL;
18035 die->parent = parent;
18036 return die;
18037}
18038
18039/* Read a die, all of its descendents, and all of its siblings; set
18040 all of the fields of all of the dies correctly. Arguments are as
18041 in read_die_and_children. */
18042
18043static struct die_info *
bf6af496 18044read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18045 const gdb_byte *info_ptr,
18046 const gdb_byte **new_info_ptr,
bf6af496 18047 struct die_info *parent)
639d11d3
DC
18048{
18049 struct die_info *first_die, *last_sibling;
d521ce57 18050 const gdb_byte *cur_ptr;
639d11d3 18051
c906108c 18052 cur_ptr = info_ptr;
639d11d3
DC
18053 first_die = last_sibling = NULL;
18054
18055 while (1)
c906108c 18056 {
639d11d3 18057 struct die_info *die
dee91e82 18058 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18059
1d325ec1 18060 if (die == NULL)
c906108c 18061 {
639d11d3
DC
18062 *new_info_ptr = cur_ptr;
18063 return first_die;
c906108c 18064 }
1d325ec1
DJ
18065
18066 if (!first_die)
18067 first_die = die;
c906108c 18068 else
1d325ec1
DJ
18069 last_sibling->sibling = die;
18070
18071 last_sibling = die;
c906108c 18072 }
c906108c
SS
18073}
18074
bf6af496
DE
18075/* Read a die, all of its descendents, and all of its siblings; set
18076 all of the fields of all of the dies correctly. Arguments are as
18077 in read_die_and_children.
18078 This the main entry point for reading a DIE and all its children. */
18079
18080static struct die_info *
18081read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18082 const gdb_byte *info_ptr,
18083 const gdb_byte **new_info_ptr,
bf6af496
DE
18084 struct die_info *parent)
18085{
18086 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18087 new_info_ptr, parent);
18088
b4f54984 18089 if (dwarf_die_debug)
bf6af496
DE
18090 {
18091 fprintf_unfiltered (gdb_stdlog,
18092 "Read die from %s@0x%x of %s:\n",
96b79293 18093 reader->die_section->get_name (),
bf6af496
DE
18094 (unsigned) (info_ptr - reader->die_section->buffer),
18095 bfd_get_filename (reader->abfd));
b4f54984 18096 dump_die (die, dwarf_die_debug);
bf6af496
DE
18097 }
18098
18099 return die;
18100}
18101
3019eac3
DE
18102/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18103 attributes.
18104 The caller is responsible for filling in the extra attributes
18105 and updating (*DIEP)->num_attrs.
18106 Set DIEP to point to a newly allocated die with its information,
3e225074 18107 except for its child, sibling, and parent fields. */
93311388 18108
d521ce57 18109static const gdb_byte *
3019eac3 18110read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18111 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18112 int num_extra_attrs)
93311388 18113{
b64f50a1 18114 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18115 struct abbrev_info *abbrev;
18116 struct die_info *die;
18117 struct dwarf2_cu *cu = reader->cu;
18118 bfd *abfd = reader->abfd;
18119
9c541725 18120 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18121 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18122 info_ptr += bytes_read;
18123 if (!abbrev_number)
18124 {
18125 *diep = NULL;
93311388
DE
18126 return info_ptr;
18127 }
18128
685af9cd 18129 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18130 if (!abbrev)
348e048f
DE
18131 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18132 abbrev_number,
18133 bfd_get_filename (abfd));
18134
3019eac3 18135 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18136 die->sect_off = sect_off;
93311388
DE
18137 die->tag = abbrev->tag;
18138 die->abbrev = abbrev_number;
3e225074 18139 die->has_children = abbrev->has_children;
93311388 18140
3019eac3
DE
18141 /* Make the result usable.
18142 The caller needs to update num_attrs after adding the extra
18143 attributes. */
93311388
DE
18144 die->num_attrs = abbrev->num_attrs;
18145
18a8505e 18146 std::vector<int> indexes_that_need_reprocess;
93311388 18147 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18148 {
18149 bool need_reprocess;
18150 info_ptr =
18151 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18152 info_ptr, &need_reprocess);
18153 if (need_reprocess)
18154 indexes_that_need_reprocess.push_back (i);
18155 }
18156
052c8bb8 18157 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18158 if (attr != nullptr)
18159 cu->str_offsets_base = DW_UNSND (attr);
93311388 18160
41144253 18161 attr = die->attr (DW_AT_loclists_base);
18162 if (attr != nullptr)
18163 cu->loclist_base = DW_UNSND (attr);
18164
a39fdb41 18165 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18166 if (maybe_addr_base.has_value ())
18167 cu->addr_base = *maybe_addr_base;
18168 for (int index : indexes_that_need_reprocess)
18169 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18170 *diep = die;
93311388
DE
18171 return info_ptr;
18172}
18173
3019eac3
DE
18174/* Read a die and all its attributes.
18175 Set DIEP to point to a newly allocated die with its information,
3e225074 18176 except for its child, sibling, and parent fields. */
3019eac3 18177
d521ce57 18178static const gdb_byte *
3019eac3 18179read_full_die (const struct die_reader_specs *reader,
3e225074 18180 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18181{
d521ce57 18182 const gdb_byte *result;
bf6af496 18183
3e225074 18184 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18185
b4f54984 18186 if (dwarf_die_debug)
bf6af496
DE
18187 {
18188 fprintf_unfiltered (gdb_stdlog,
18189 "Read die from %s@0x%x of %s:\n",
96b79293 18190 reader->die_section->get_name (),
bf6af496
DE
18191 (unsigned) (info_ptr - reader->die_section->buffer),
18192 bfd_get_filename (reader->abfd));
b4f54984 18193 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18194 }
18195
18196 return result;
3019eac3 18197}
433df2d4 18198\f
c906108c 18199
72bf9492
DJ
18200/* Returns nonzero if TAG represents a type that we might generate a partial
18201 symbol for. */
18202
18203static int
18204is_type_tag_for_partial (int tag)
18205{
18206 switch (tag)
18207 {
18208#if 0
18209 /* Some types that would be reasonable to generate partial symbols for,
18210 that we don't at present. */
18211 case DW_TAG_array_type:
18212 case DW_TAG_file_type:
18213 case DW_TAG_ptr_to_member_type:
18214 case DW_TAG_set_type:
18215 case DW_TAG_string_type:
18216 case DW_TAG_subroutine_type:
18217#endif
18218 case DW_TAG_base_type:
18219 case DW_TAG_class_type:
680b30c7 18220 case DW_TAG_interface_type:
72bf9492
DJ
18221 case DW_TAG_enumeration_type:
18222 case DW_TAG_structure_type:
18223 case DW_TAG_subrange_type:
18224 case DW_TAG_typedef:
18225 case DW_TAG_union_type:
18226 return 1;
18227 default:
18228 return 0;
18229 }
18230}
18231
18232/* Load all DIEs that are interesting for partial symbols into memory. */
18233
18234static struct partial_die_info *
dee91e82 18235load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18236 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18237{
dee91e82 18238 struct dwarf2_cu *cu = reader->cu;
5e22e966 18239 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18240 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18241 unsigned int bytes_read;
5afb4e99 18242 unsigned int load_all = 0;
72bf9492
DJ
18243 int nesting_level = 1;
18244
18245 parent_die = NULL;
18246 last_die = NULL;
18247
7adf1e79
DE
18248 gdb_assert (cu->per_cu != NULL);
18249 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18250 load_all = 1;
18251
72bf9492
DJ
18252 cu->partial_dies
18253 = htab_create_alloc_ex (cu->header.length / 12,
18254 partial_die_hash,
18255 partial_die_eq,
18256 NULL,
18257 &cu->comp_unit_obstack,
18258 hashtab_obstack_allocate,
18259 dummy_obstack_deallocate);
18260
72bf9492
DJ
18261 while (1)
18262 {
685af9cd 18263 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18264
18265 /* A NULL abbrev means the end of a series of children. */
18266 if (abbrev == NULL)
18267 {
18268 if (--nesting_level == 0)
cd9983dd
YQ
18269 return first_die;
18270
72bf9492
DJ
18271 info_ptr += bytes_read;
18272 last_die = parent_die;
18273 parent_die = parent_die->die_parent;
18274 continue;
18275 }
18276
98bfdba5
PA
18277 /* Check for template arguments. We never save these; if
18278 they're seen, we just mark the parent, and go on our way. */
18279 if (parent_die != NULL
18280 && cu->language == language_cplus
18281 && (abbrev->tag == DW_TAG_template_type_param
18282 || abbrev->tag == DW_TAG_template_value_param))
18283 {
18284 parent_die->has_template_arguments = 1;
18285
18286 if (!load_all)
18287 {
18288 /* We don't need a partial DIE for the template argument. */
dee91e82 18289 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18290 continue;
18291 }
18292 }
18293
0d99eb77 18294 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18295 Skip their other children. */
18296 if (!load_all
18297 && cu->language == language_cplus
18298 && parent_die != NULL
f9b5d5ea
TV
18299 && parent_die->tag == DW_TAG_subprogram
18300 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 18301 {
dee91e82 18302 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18303 continue;
18304 }
18305
5afb4e99
DJ
18306 /* Check whether this DIE is interesting enough to save. Normally
18307 we would not be interested in members here, but there may be
18308 later variables referencing them via DW_AT_specification (for
18309 static members). */
18310 if (!load_all
18311 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18312 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18313 && abbrev->tag != DW_TAG_enumerator
18314 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18315 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18316 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18317 && abbrev->tag != DW_TAG_variable
5afb4e99 18318 && abbrev->tag != DW_TAG_namespace
f55ee35c 18319 && abbrev->tag != DW_TAG_module
95554aad 18320 && abbrev->tag != DW_TAG_member
74921315
KS
18321 && abbrev->tag != DW_TAG_imported_unit
18322 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18323 {
18324 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18325 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18326 continue;
18327 }
18328
6f06d47b
YQ
18329 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18330 abbrev);
cd9983dd 18331
48fbe735 18332 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18333
18334 /* This two-pass algorithm for processing partial symbols has a
18335 high cost in cache pressure. Thus, handle some simple cases
18336 here which cover the majority of C partial symbols. DIEs
18337 which neither have specification tags in them, nor could have
18338 specification tags elsewhere pointing at them, can simply be
18339 processed and discarded.
18340
18341 This segment is also optional; scan_partial_symbols and
18342 add_partial_symbol will handle these DIEs if we chain
18343 them in normally. When compilers which do not emit large
18344 quantities of duplicate debug information are more common,
18345 this code can probably be removed. */
18346
18347 /* Any complete simple types at the top level (pretty much all
18348 of them, for a language without namespaces), can be processed
18349 directly. */
18350 if (parent_die == NULL
cd9983dd
YQ
18351 && pdi.has_specification == 0
18352 && pdi.is_declaration == 0
18353 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18354 || pdi.tag == DW_TAG_base_type
18355 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18356 {
7d00ffec 18357 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
18358 add_partial_symbol (&pdi, cu);
18359
cd9983dd 18360 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18361 continue;
18362 }
18363
d8228535
JK
18364 /* The exception for DW_TAG_typedef with has_children above is
18365 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18366 type_name_or_error will error on such types later.
d8228535
JK
18367
18368 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18369 it could not find the child DIEs referenced later, this is checked
18370 above. In correct DWARF DW_TAG_typedef should have no children. */
18371
cd9983dd 18372 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18373 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18374 "- DIE at %s [in module %s]"),
cd9983dd 18375 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18376
72bf9492
DJ
18377 /* If we're at the second level, and we're an enumerator, and
18378 our parent has no specification (meaning possibly lives in a
18379 namespace elsewhere), then we can add the partial symbol now
18380 instead of queueing it. */
cd9983dd 18381 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18382 && parent_die != NULL
18383 && parent_die->die_parent == NULL
18384 && parent_die->tag == DW_TAG_enumeration_type
18385 && parent_die->has_specification == 0)
18386 {
7d00ffec 18387 if (pdi.raw_name == NULL)
b98664d3 18388 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18389 else if (building_psymtab)
f0fbb768 18390 add_partial_symbol (&pdi, cu);
72bf9492 18391
cd9983dd 18392 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18393 continue;
18394 }
18395
cd9983dd 18396 struct partial_die_info *part_die
6f06d47b 18397 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18398
72bf9492
DJ
18399 /* We'll save this DIE so link it in. */
18400 part_die->die_parent = parent_die;
18401 part_die->die_sibling = NULL;
18402 part_die->die_child = NULL;
18403
18404 if (last_die && last_die == parent_die)
18405 last_die->die_child = part_die;
18406 else if (last_die)
18407 last_die->die_sibling = part_die;
18408
18409 last_die = part_die;
18410
18411 if (first_die == NULL)
18412 first_die = part_die;
18413
18414 /* Maybe add the DIE to the hash table. Not all DIEs that we
18415 find interesting need to be in the hash table, because we
18416 also have the parent/sibling/child chains; only those that we
18417 might refer to by offset later during partial symbol reading.
18418
18419 For now this means things that might have be the target of a
18420 DW_AT_specification, DW_AT_abstract_origin, or
18421 DW_AT_extension. DW_AT_extension will refer only to
18422 namespaces; DW_AT_abstract_origin refers to functions (and
18423 many things under the function DIE, but we do not recurse
18424 into function DIEs during partial symbol reading) and
18425 possibly variables as well; DW_AT_specification refers to
18426 declarations. Declarations ought to have the DW_AT_declaration
18427 flag. It happens that GCC forgets to put it in sometimes, but
18428 only for functions, not for types.
18429
18430 Adding more things than necessary to the hash table is harmless
18431 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18432 wasted time in find_partial_die, when we reread the compilation
18433 unit with load_all_dies set. */
72bf9492 18434
5afb4e99 18435 if (load_all
72929c62 18436 || abbrev->tag == DW_TAG_constant
5afb4e99 18437 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18438 || abbrev->tag == DW_TAG_variable
18439 || abbrev->tag == DW_TAG_namespace
18440 || part_die->is_declaration)
18441 {
18442 void **slot;
18443
18444 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18445 to_underlying (part_die->sect_off),
18446 INSERT);
72bf9492
DJ
18447 *slot = part_die;
18448 }
18449
72bf9492 18450 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18451 we have no reason to follow the children of structures; for other
98bfdba5
PA
18452 languages we have to, so that we can get at method physnames
18453 to infer fully qualified class names, for DW_AT_specification,
18454 and for C++ template arguments. For C++, we also look one level
18455 inside functions to find template arguments (if the name of the
18456 function does not already contain the template arguments).
bc30ff58 18457
0a4b0913
AB
18458 For Ada and Fortran, we need to scan the children of subprograms
18459 and lexical blocks as well because these languages allow the
18460 definition of nested entities that could be interesting for the
18461 debugger, such as nested subprograms for instance. */
72bf9492 18462 if (last_die->has_children
5afb4e99
DJ
18463 && (load_all
18464 || last_die->tag == DW_TAG_namespace
f55ee35c 18465 || last_die->tag == DW_TAG_module
72bf9492 18466 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18467 || (cu->language == language_cplus
18468 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
18469 && (last_die->raw_name == NULL
18470 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
18471 || (cu->language != language_c
18472 && (last_die->tag == DW_TAG_class_type
680b30c7 18473 || last_die->tag == DW_TAG_interface_type
72bf9492 18474 || last_die->tag == DW_TAG_structure_type
bc30ff58 18475 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18476 || ((cu->language == language_ada
18477 || cu->language == language_fortran)
bc30ff58
JB
18478 && (last_die->tag == DW_TAG_subprogram
18479 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18480 {
18481 nesting_level++;
18482 parent_die = last_die;
18483 continue;
18484 }
18485
18486 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18487 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18488
18489 /* Back to the top, do it again. */
18490 }
18491}
18492
6f06d47b
YQ
18493partial_die_info::partial_die_info (sect_offset sect_off_,
18494 struct abbrev_info *abbrev)
18495 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18496{
18497}
18498
7d00ffec
TT
18499/* See class definition. */
18500
18501const char *
18502partial_die_info::name (dwarf2_cu *cu)
18503{
18504 if (!canonical_name && raw_name != nullptr)
18505 {
18506 struct objfile *objfile = cu->per_objfile->objfile;
18507 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18508 canonical_name = 1;
18509 }
18510
18511 return raw_name;
18512}
18513
35cc7ed7
YQ
18514/* Read a minimal amount of information into the minimal die structure.
18515 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18516
48fbe735
YQ
18517const gdb_byte *
18518partial_die_info::read (const struct die_reader_specs *reader,
18519 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18520{
dee91e82 18521 struct dwarf2_cu *cu = reader->cu;
976ca316 18522 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 18523 unsigned int i;
c5aa993b 18524 int has_low_pc_attr = 0;
c906108c 18525 int has_high_pc_attr = 0;
91da1414 18526 int high_pc_relative = 0;
c906108c 18527
fd0a254f 18528 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18529 {
e7da7f8f 18530 attribute attr;
18a8505e 18531 bool need_reprocess;
e7da7f8f 18532 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18533 info_ptr, &need_reprocess);
18534 /* String and address offsets that need to do the reprocessing have
18535 already been read at this point, so there is no need to wait until
18536 the loop terminates to do the reprocessing. */
18537 if (need_reprocess)
e7da7f8f 18538 read_attribute_reprocess (reader, &attr);
c906108c 18539 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18540 partial symbol table. */
c906108c
SS
18541 switch (attr.name)
18542 {
18543 case DW_AT_name:
48fbe735 18544 switch (tag)
71c25dea
TT
18545 {
18546 case DW_TAG_compile_unit:
95554aad 18547 case DW_TAG_partial_unit:
348e048f 18548 case DW_TAG_type_unit:
71c25dea
TT
18549 /* Compilation units have a DW_AT_name that is a filename, not
18550 a source language identifier. */
18551 case DW_TAG_enumeration_type:
18552 case DW_TAG_enumerator:
18553 /* These tags always have simple identifiers already; no need
18554 to canonicalize them. */
7d00ffec
TT
18555 canonical_name = 1;
18556 raw_name = DW_STRING (&attr);
71c25dea
TT
18557 break;
18558 default:
7d00ffec
TT
18559 canonical_name = 0;
18560 raw_name = DW_STRING (&attr);
71c25dea
TT
18561 break;
18562 }
c906108c 18563 break;
31ef98ae 18564 case DW_AT_linkage_name:
c906108c 18565 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18566 /* Note that both forms of linkage name might appear. We
18567 assume they will be the same, and we only store the last
18568 one we see. */
e61108c9 18569 linkage_name = attr.value_as_string ();
787de330
TT
18570 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18571 See https://github.com/rust-lang/rust/issues/32925. */
18572 if (cu->language == language_rust && linkage_name != NULL
18573 && strchr (linkage_name, '{') != NULL)
18574 linkage_name = NULL;
c906108c
SS
18575 break;
18576 case DW_AT_low_pc:
18577 has_low_pc_attr = 1;
cd6c91b4 18578 lowpc = attr.value_as_address ();
c906108c
SS
18579 break;
18580 case DW_AT_high_pc:
18581 has_high_pc_attr = 1;
cd6c91b4
TT
18582 highpc = attr.value_as_address ();
18583 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18584 high_pc_relative = 1;
c906108c
SS
18585 break;
18586 case DW_AT_location:
0963b4bd 18587 /* Support the .debug_loc offsets. */
4fc6c0d5 18588 if (attr.form_is_block ())
8e19ed76 18589 {
48fbe735 18590 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18591 }
cd6c91b4 18592 else if (attr.form_is_section_offset ())
8e19ed76 18593 {
4d3c2250 18594 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18595 }
18596 else
18597 {
4d3c2250
KB
18598 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18599 "partial symbol information");
8e19ed76 18600 }
c906108c 18601 break;
c906108c 18602 case DW_AT_external:
48fbe735 18603 is_external = DW_UNSND (&attr);
c906108c
SS
18604 break;
18605 case DW_AT_declaration:
48fbe735 18606 is_declaration = DW_UNSND (&attr);
c906108c
SS
18607 break;
18608 case DW_AT_type:
48fbe735 18609 has_type = 1;
c906108c
SS
18610 break;
18611 case DW_AT_abstract_origin:
18612 case DW_AT_specification:
72bf9492 18613 case DW_AT_extension:
48fbe735 18614 has_specification = 1;
0826b30a 18615 spec_offset = attr.get_ref_die_offset ();
48fbe735 18616 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18617 || cu->per_cu->is_dwz);
c906108c
SS
18618 break;
18619 case DW_AT_sibling:
18620 /* Ignore absolute siblings, they might point outside of
18621 the current compile unit. */
18622 if (attr.form == DW_FORM_ref_addr)
b98664d3 18623 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18624 else
b9502d3f 18625 {
48fbe735 18626 const gdb_byte *buffer = reader->buffer;
0826b30a 18627 sect_offset off = attr.get_ref_die_offset ();
9c541725 18628 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18629
18630 if (sibling_ptr < info_ptr)
b98664d3 18631 complaint (_("DW_AT_sibling points backwards"));
22869d73 18632 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18633 reader->die_section->overflow_complaint ();
b9502d3f 18634 else
48fbe735 18635 sibling = sibling_ptr;
b9502d3f 18636 }
c906108c 18637 break;
fa4028e9 18638 case DW_AT_byte_size:
48fbe735 18639 has_byte_size = 1;
fa4028e9 18640 break;
ff908ebf 18641 case DW_AT_const_value:
48fbe735 18642 has_const_value = 1;
ff908ebf 18643 break;
68511cec
CES
18644 case DW_AT_calling_convention:
18645 /* DWARF doesn't provide a way to identify a program's source-level
18646 entry point. DW_AT_calling_convention attributes are only meant
18647 to describe functions' calling conventions.
18648
18649 However, because it's a necessary piece of information in
0c1b455e
TT
18650 Fortran, and before DWARF 4 DW_CC_program was the only
18651 piece of debugging information whose definition refers to
18652 a 'main program' at all, several compilers marked Fortran
18653 main programs with DW_CC_program --- even when those
18654 functions use the standard calling conventions.
18655
18656 Although DWARF now specifies a way to provide this
18657 information, we support this practice for backward
18658 compatibility. */
68511cec 18659 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18660 && cu->language == language_fortran)
48fbe735 18661 main_subprogram = 1;
68511cec 18662 break;
481860b3
GB
18663 case DW_AT_inline:
18664 if (DW_UNSND (&attr) == DW_INL_inlined
18665 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18666 may_be_inlined = 1;
481860b3 18667 break;
95554aad
TT
18668
18669 case DW_AT_import:
48fbe735 18670 if (tag == DW_TAG_imported_unit)
36586728 18671 {
0826b30a 18672 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18673 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18674 || cu->per_cu->is_dwz);
18675 }
95554aad
TT
18676 break;
18677
0c1b455e 18678 case DW_AT_main_subprogram:
48fbe735 18679 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18680 break;
18681
05caa1d2
TT
18682 case DW_AT_ranges:
18683 {
18684 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18685 but that requires a full DIE, so instead we just
18686 reimplement it. */
18687 int need_ranges_base = tag != DW_TAG_compile_unit;
18688 unsigned int ranges_offset = (DW_UNSND (&attr)
18689 + (need_ranges_base
18690 ? cu->ranges_base
18691 : 0));
18692
18693 /* Value of the DW_AT_ranges attribute is the offset in the
18694 .debug_ranges section. */
18695 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18696 nullptr))
18697 has_pc_info = 1;
18698 }
18699 break;
18700
c906108c
SS
18701 default:
18702 break;
18703 }
18704 }
18705
10d06d82
TT
18706 /* For Ada, if both the name and the linkage name appear, we prefer
18707 the latter. This lets "catch exception" work better, regardless
18708 of the order in which the name and linkage name were emitted.
18709 Really, though, this is just a workaround for the fact that gdb
18710 doesn't store both the name and the linkage name. */
18711 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 18712 raw_name = linkage_name;
10d06d82 18713
91da1414 18714 if (high_pc_relative)
48fbe735 18715 highpc += lowpc;
91da1414 18716
9373cf26
JK
18717 if (has_low_pc_attr && has_high_pc_attr)
18718 {
18719 /* When using the GNU linker, .gnu.linkonce. sections are used to
18720 eliminate duplicate copies of functions and vtables and such.
18721 The linker will arbitrarily choose one and discard the others.
18722 The AT_*_pc values for such functions refer to local labels in
18723 these sections. If the section from that file was discarded, the
18724 labels are not in the output, so the relocs get a value of 0.
18725 If this is a discarded function, mark the pc bounds as invalid,
18726 so that GDB will ignore it. */
976ca316 18727 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 18728 {
976ca316 18729 struct objfile *objfile = per_objfile->objfile;
08feed99 18730 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18731
b98664d3 18732 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18733 "for DIE at %s [in module %s]"),
48fbe735
YQ
18734 paddress (gdbarch, lowpc),
18735 sect_offset_str (sect_off),
9d8780f0 18736 objfile_name (objfile));
9373cf26
JK
18737 }
18738 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18739 else if (lowpc >= highpc)
9373cf26 18740 {
976ca316 18741 struct objfile *objfile = per_objfile->objfile;
08feed99 18742 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18743
b98664d3 18744 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18745 "for DIE at %s [in module %s]"),
48fbe735
YQ
18746 paddress (gdbarch, lowpc),
18747 paddress (gdbarch, highpc),
18748 sect_offset_str (sect_off),
9c541725 18749 objfile_name (objfile));
9373cf26
JK
18750 }
18751 else
48fbe735 18752 has_pc_info = 1;
9373cf26 18753 }
85cbf3d3 18754
c906108c
SS
18755 return info_ptr;
18756}
18757
72bf9492
DJ
18758/* Find a cached partial DIE at OFFSET in CU. */
18759
d590ff25
YQ
18760struct partial_die_info *
18761dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18762{
18763 struct partial_die_info *lookup_die = NULL;
6f06d47b 18764 struct partial_die_info part_die (sect_off);
72bf9492 18765
9a3c8263 18766 lookup_die = ((struct partial_die_info *)
d590ff25 18767 htab_find_with_hash (partial_dies, &part_die,
9c541725 18768 to_underlying (sect_off)));
72bf9492 18769
72bf9492
DJ
18770 return lookup_die;
18771}
18772
348e048f
DE
18773/* Find a partial DIE at OFFSET, which may or may not be in CU,
18774 except in the case of .debug_types DIEs which do not reference
18775 outside their CU (they do however referencing other types via
55f1336d 18776 DW_FORM_ref_sig8). */
72bf9492 18777
122cf0f2 18778static const struct cu_partial_die_info
9c541725 18779find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18780{
976ca316
SM
18781 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18782 struct objfile *objfile = per_objfile->objfile;
5afb4e99 18783 struct partial_die_info *pd = NULL;
72bf9492 18784
36586728 18785 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18786 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18787 {
d590ff25 18788 pd = cu->find_partial_die (sect_off);
5afb4e99 18789 if (pd != NULL)
fb816e8b 18790 return { cu, pd };
0d99eb77
DE
18791 /* We missed recording what we needed.
18792 Load all dies and try again. */
5afb4e99 18793 }
0d99eb77
DE
18794 else
18795 {
18796 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18797 if (cu->per_cu->is_debug_types)
0d99eb77 18798 {
9d8780f0
SM
18799 error (_("Dwarf Error: Type Unit at offset %s contains"
18800 " external reference to offset %s [in module %s].\n"),
18801 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18802 bfd_get_filename (objfile->obfd));
18803 }
7188ed02
SM
18804 dwarf2_per_cu_data *per_cu
18805 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 18806 per_objfile);
72bf9492 18807
976ca316 18808 cu = per_objfile->get_cu (per_cu);
7188ed02 18809 if (cu == NULL || cu->partial_dies == NULL)
976ca316 18810 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 18811
976ca316 18812 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
18813
18814 cu->last_used = 0;
18815 pd = cu->find_partial_die (sect_off);
0d99eb77 18816 }
5afb4e99 18817
dee91e82
DE
18818 /* If we didn't find it, and not all dies have been loaded,
18819 load them all and try again. */
18820
7188ed02 18821 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 18822 {
7188ed02 18823 cu->per_cu->load_all_dies = 1;
fd820528
DE
18824
18825 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18826 THIS_CU->cu may already be in use. So we can't just free it and
18827 replace its DIEs with the ones we read in. Instead, we leave those
18828 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18829 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18830 set. */
976ca316 18831 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 18832
7188ed02 18833 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18834 }
18835
18836 if (pd == NULL)
18837 internal_error (__FILE__, __LINE__,
9d8780f0 18838 _("could not find partial DIE %s "
3e43a32a 18839 "in cache [from module %s]\n"),
9d8780f0 18840 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 18841 return { cu, pd };
72bf9492
DJ
18842}
18843
abc72ce4
DE
18844/* See if we can figure out if the class lives in a namespace. We do
18845 this by looking for a member function; its demangled name will
18846 contain namespace info, if there is any. */
18847
18848static void
18849guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18850 struct dwarf2_cu *cu)
18851{
18852 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18853 what template types look like, because the demangler
18854 frequently doesn't give the same name as the debug info. We
18855 could fix this by only using the demangled name to get the
18856 prefix (but see comment in read_structure_type). */
18857
18858 struct partial_die_info *real_pdi;
18859 struct partial_die_info *child_pdi;
18860
18861 /* If this DIE (this DIE's specification, if any) has a parent, then
18862 we should not do this. We'll prepend the parent's fully qualified
18863 name when we create the partial symbol. */
18864
18865 real_pdi = struct_pdi;
18866 while (real_pdi->has_specification)
fb816e8b 18867 {
122cf0f2
AB
18868 auto res = find_partial_die (real_pdi->spec_offset,
18869 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18870 real_pdi = res.pdi;
18871 cu = res.cu;
18872 }
abc72ce4
DE
18873
18874 if (real_pdi->die_parent != NULL)
18875 return;
18876
18877 for (child_pdi = struct_pdi->die_child;
18878 child_pdi != NULL;
18879 child_pdi = child_pdi->die_sibling)
18880 {
18881 if (child_pdi->tag == DW_TAG_subprogram
18882 && child_pdi->linkage_name != NULL)
18883 {
43816ebc 18884 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
18885 (cu->language_defn->class_name_from_physname
18886 (child_pdi->linkage_name));
abc72ce4
DE
18887 if (actual_class_name != NULL)
18888 {
5e22e966 18889 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
18890 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
18891 struct_pdi->canonical_name = 1;
abc72ce4
DE
18892 }
18893 break;
18894 }
18895 }
18896}
18897
25c11aca
TV
18898/* Return true if a DIE with TAG may have the DW_AT_const_value
18899 attribute. */
18900
18901static bool
18902can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18903{
18904 switch (tag)
18905 {
18906 case DW_TAG_constant:
18907 case DW_TAG_enumerator:
18908 case DW_TAG_formal_parameter:
18909 case DW_TAG_template_value_param:
18910 case DW_TAG_variable:
18911 return true;
18912 }
18913
18914 return false;
18915}
18916
52356b79
YQ
18917void
18918partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18919{
abc72ce4
DE
18920 /* Once we've fixed up a die, there's no point in doing so again.
18921 This also avoids a memory leak if we were to call
18922 guess_partial_die_structure_name multiple times. */
52356b79 18923 if (fixup_called)
abc72ce4
DE
18924 return;
18925
72bf9492
DJ
18926 /* If we found a reference attribute and the DIE has no name, try
18927 to find a name in the referred to DIE. */
18928
7d00ffec 18929 if (raw_name == NULL && has_specification)
72bf9492
DJ
18930 {
18931 struct partial_die_info *spec_die;
72bf9492 18932
122cf0f2 18933 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18934 spec_die = res.pdi;
18935 cu = res.cu;
72bf9492 18936
52356b79 18937 spec_die->fixup (cu);
72bf9492 18938
7d00ffec 18939 if (spec_die->raw_name)
72bf9492 18940 {
7d00ffec
TT
18941 raw_name = spec_die->raw_name;
18942 canonical_name = spec_die->canonical_name;
72bf9492
DJ
18943
18944 /* Copy DW_AT_external attribute if it is set. */
18945 if (spec_die->is_external)
52356b79 18946 is_external = spec_die->is_external;
72bf9492
DJ
18947 }
18948 }
18949
25c11aca
TV
18950 if (!has_const_value && has_specification
18951 && can_have_DW_AT_const_value_p (tag))
18952 {
18953 struct partial_die_info *spec_die;
18954
18955 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18956 spec_die = res.pdi;
18957 cu = res.cu;
18958
18959 spec_die->fixup (cu);
18960
18961 if (spec_die->has_const_value)
18962 {
18963 /* Copy DW_AT_const_value attribute if it is set. */
18964 has_const_value = spec_die->has_const_value;
18965 }
18966 }
18967
72bf9492 18968 /* Set default names for some unnamed DIEs. */
72bf9492 18969
7d00ffec
TT
18970 if (raw_name == NULL && tag == DW_TAG_namespace)
18971 {
18972 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
18973 canonical_name = 1;
18974 }
72bf9492 18975
abc72ce4
DE
18976 /* If there is no parent die to provide a namespace, and there are
18977 children, see if we can determine the namespace from their linkage
122d1940 18978 name. */
abc72ce4 18979 if (cu->language == language_cplus
5e22e966 18980 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18981 && die_parent == NULL
18982 && has_children
18983 && (tag == DW_TAG_class_type
18984 || tag == DW_TAG_structure_type
18985 || tag == DW_TAG_union_type))
18986 guess_partial_die_structure_name (this, cu);
abc72ce4 18987
53832f31
TT
18988 /* GCC might emit a nameless struct or union that has a linkage
18989 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 18990 if (raw_name == NULL
52356b79
YQ
18991 && (tag == DW_TAG_class_type
18992 || tag == DW_TAG_interface_type
18993 || tag == DW_TAG_structure_type
18994 || tag == DW_TAG_union_type)
18995 && linkage_name != NULL)
53832f31 18996 {
43816ebc
TT
18997 gdb::unique_xmalloc_ptr<char> demangled
18998 (gdb_demangle (linkage_name, DMGL_TYPES));
18999 if (demangled != nullptr)
53832f31 19000 {
96408a79
SA
19001 const char *base;
19002
19003 /* Strip any leading namespaces/classes, keep only the base name.
19004 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19005 base = strrchr (demangled.get (), ':');
19006 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19007 base++;
19008 else
43816ebc 19009 base = demangled.get ();
96408a79 19010
5e22e966 19011 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19012 raw_name = objfile->intern (base);
19013 canonical_name = 1;
53832f31
TT
19014 }
19015 }
19016
52356b79 19017 fixup_called = 1;
72bf9492
DJ
19018}
19019
41144253 19020/* Read the .debug_loclists header contents from the given SECTION in the
19021 HEADER. */
19022static void
19023read_loclist_header (struct loclist_header *header,
19024 struct dwarf2_section_info *section)
19025{
19026 unsigned int bytes_read;
19027 bfd *abfd = section->get_bfd_owner ();
19028 const gdb_byte *info_ptr = section->buffer;
19029 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19030 info_ptr += bytes_read;
19031 header->version = read_2_bytes (abfd, info_ptr);
19032 info_ptr += 2;
19033 header->addr_size = read_1_byte (abfd, info_ptr);
19034 info_ptr += 1;
19035 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19036 info_ptr += 1;
19037 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19038}
19039
19040/* Return the DW_AT_loclists_base value for the CU. */
19041static ULONGEST
19042lookup_loclist_base (struct dwarf2_cu *cu)
19043{
19044 /* For the .dwo unit, the loclist_base points to the first offset following
19045 the header. The header consists of the following entities-
19046 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19047 bit format)
19048 2. version (2 bytes)
19049 3. address size (1 byte)
19050 4. segment selector size (1 byte)
19051 5. offset entry count (4 bytes)
19052 These sizes are derived as per the DWARFv5 standard. */
19053 if (cu->dwo_unit != nullptr)
19054 {
19055 if (cu->header.initial_length_size == 4)
19056 return LOCLIST_HEADER_SIZE32;
19057 return LOCLIST_HEADER_SIZE64;
19058 }
19059 return cu->loclist_base;
19060}
19061
19062/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19063 array of offsets in the .debug_loclists section. */
19064static CORE_ADDR
19065read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19066{
976ca316
SM
19067 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19068 struct objfile *objfile = per_objfile->objfile;
41144253 19069 bfd *abfd = objfile->obfd;
19070 ULONGEST loclist_base = lookup_loclist_base (cu);
19071 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19072
19073 section->read (objfile);
19074 if (section->buffer == NULL)
19075 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19076 "section [in module %s]"), objfile_name (objfile));
19077 struct loclist_header header;
19078 read_loclist_header (&header, section);
19079 if (loclist_index >= header.offset_entry_count)
19080 complaint (_("DW_FORM_loclistx pointing outside of "
19081 ".debug_loclists offset array [in module %s]"),
19082 objfile_name (objfile));
19083 if (loclist_base + loclist_index * cu->header.offset_size
19084 >= section->size)
19085 complaint (_("DW_FORM_loclistx pointing outside of "
19086 ".debug_loclists section [in module %s]"),
19087 objfile_name (objfile));
19088 const gdb_byte *info_ptr
19089 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19090
19091 if (cu->header.offset_size == 4)
19092 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19093 else
19094 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19095}
19096
18a8505e
AT
19097/* Process the attributes that had to be skipped in the first round. These
19098 attributes are the ones that need str_offsets_base or addr_base attributes.
19099 They could not have been processed in the first round, because at the time
19100 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19101static void
19102read_attribute_reprocess (const struct die_reader_specs *reader,
19103 struct attribute *attr)
18a8505e
AT
19104{
19105 struct dwarf2_cu *cu = reader->cu;
19106 switch (attr->form)
19107 {
19108 case DW_FORM_addrx:
19109 case DW_FORM_GNU_addr_index:
19110 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19111 break;
41144253 19112 case DW_FORM_loclistx:
19113 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19114 break;
18a8505e
AT
19115 case DW_FORM_strx:
19116 case DW_FORM_strx1:
19117 case DW_FORM_strx2:
19118 case DW_FORM_strx3:
19119 case DW_FORM_strx4:
19120 case DW_FORM_GNU_str_index:
19121 {
19122 unsigned int str_index = DW_UNSND (attr);
19123 if (reader->dwo_file != NULL)
19124 {
19125 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19126 DW_STRING_IS_CANONICAL (attr) = 0;
19127 }
19128 else
19129 {
19130 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19131 DW_STRING_IS_CANONICAL (attr) = 0;
19132 }
19133 break;
19134 }
19135 default:
19136 gdb_assert_not_reached (_("Unexpected DWARF form."));
19137 }
19138}
19139
a8329558 19140/* Read an attribute value described by an attribute form. */
c906108c 19141
d521ce57 19142static const gdb_byte *
dee91e82
DE
19143read_attribute_value (const struct die_reader_specs *reader,
19144 struct attribute *attr, unsigned form,
18a8505e
AT
19145 LONGEST implicit_const, const gdb_byte *info_ptr,
19146 bool *need_reprocess)
c906108c 19147{
dee91e82 19148 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19149 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19150 struct objfile *objfile = per_objfile->objfile;
dee91e82 19151 bfd *abfd = reader->abfd;
e7c27a73 19152 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19153 unsigned int bytes_read;
19154 struct dwarf_block *blk;
18a8505e 19155 *need_reprocess = false;
c906108c 19156
aead7601 19157 attr->form = (enum dwarf_form) form;
a8329558 19158 switch (form)
c906108c 19159 {
c906108c 19160 case DW_FORM_ref_addr:
ae411497 19161 if (cu->header.version == 2)
c8a7a66f
TT
19162 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19163 &bytes_read);
ae411497 19164 else
8266302d
TT
19165 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19166 &bytes_read);
ae411497
TT
19167 info_ptr += bytes_read;
19168 break;
36586728 19169 case DW_FORM_GNU_ref_alt:
8266302d 19170 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19171 info_ptr += bytes_read;
19172 break;
ae411497 19173 case DW_FORM_addr:
08feed99
TT
19174 {
19175 struct gdbarch *gdbarch = objfile->arch ();
19176 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19177 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19178 info_ptr += bytes_read;
19179 }
c906108c
SS
19180 break;
19181 case DW_FORM_block2:
7b5a2f43 19182 blk = dwarf_alloc_block (cu);
c906108c
SS
19183 blk->size = read_2_bytes (abfd, info_ptr);
19184 info_ptr += 2;
19185 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19186 info_ptr += blk->size;
19187 DW_BLOCK (attr) = blk;
19188 break;
19189 case DW_FORM_block4:
7b5a2f43 19190 blk = dwarf_alloc_block (cu);
c906108c
SS
19191 blk->size = read_4_bytes (abfd, info_ptr);
19192 info_ptr += 4;
19193 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19194 info_ptr += blk->size;
19195 DW_BLOCK (attr) = blk;
19196 break;
19197 case DW_FORM_data2:
19198 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19199 info_ptr += 2;
19200 break;
19201 case DW_FORM_data4:
19202 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19203 info_ptr += 4;
19204 break;
19205 case DW_FORM_data8:
19206 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19207 info_ptr += 8;
19208 break;
0224619f
JK
19209 case DW_FORM_data16:
19210 blk = dwarf_alloc_block (cu);
19211 blk->size = 16;
19212 blk->data = read_n_bytes (abfd, info_ptr, 16);
19213 info_ptr += 16;
19214 DW_BLOCK (attr) = blk;
19215 break;
2dc7f7b3 19216 case DW_FORM_sec_offset:
8266302d 19217 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19218 info_ptr += bytes_read;
19219 break;
41144253 19220 case DW_FORM_loclistx:
19221 {
19222 *need_reprocess = true;
19223 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19224 info_ptr += bytes_read;
19225 }
19226 break;
c906108c 19227 case DW_FORM_string:
9b1c24c8 19228 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19229 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19230 info_ptr += bytes_read;
19231 break;
4bdf3d34 19232 case DW_FORM_strp:
36586728
TT
19233 if (!cu->per_cu->is_dwz)
19234 {
976ca316 19235 DW_STRING (attr) = read_indirect_string (per_objfile,
ed2dc618 19236 abfd, info_ptr, cu_header,
36586728
TT
19237 &bytes_read);
19238 DW_STRING_IS_CANONICAL (attr) = 0;
19239 info_ptr += bytes_read;
19240 break;
19241 }
19242 /* FALLTHROUGH */
43988095
JK
19243 case DW_FORM_line_strp:
19244 if (!cu->per_cu->is_dwz)
19245 {
976ca316
SM
19246 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19247 &bytes_read);
43988095
JK
19248 DW_STRING_IS_CANONICAL (attr) = 0;
19249 info_ptr += bytes_read;
19250 break;
19251 }
19252 /* FALLTHROUGH */
36586728
TT
19253 case DW_FORM_GNU_strp_alt:
19254 {
976ca316 19255 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
19256 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19257 &bytes_read);
36586728 19258
0314b390 19259 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19260 DW_STRING_IS_CANONICAL (attr) = 0;
19261 info_ptr += bytes_read;
19262 }
4bdf3d34 19263 break;
2dc7f7b3 19264 case DW_FORM_exprloc:
c906108c 19265 case DW_FORM_block:
7b5a2f43 19266 blk = dwarf_alloc_block (cu);
c906108c
SS
19267 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19268 info_ptr += bytes_read;
19269 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19270 info_ptr += blk->size;
19271 DW_BLOCK (attr) = blk;
19272 break;
19273 case DW_FORM_block1:
7b5a2f43 19274 blk = dwarf_alloc_block (cu);
c906108c
SS
19275 blk->size = read_1_byte (abfd, info_ptr);
19276 info_ptr += 1;
19277 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19278 info_ptr += blk->size;
19279 DW_BLOCK (attr) = blk;
19280 break;
19281 case DW_FORM_data1:
19282 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19283 info_ptr += 1;
19284 break;
19285 case DW_FORM_flag:
19286 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19287 info_ptr += 1;
19288 break;
2dc7f7b3
TT
19289 case DW_FORM_flag_present:
19290 DW_UNSND (attr) = 1;
19291 break;
c906108c
SS
19292 case DW_FORM_sdata:
19293 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19294 info_ptr += bytes_read;
19295 break;
19296 case DW_FORM_udata:
18a8505e 19297 case DW_FORM_rnglistx:
c906108c
SS
19298 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19299 info_ptr += bytes_read;
19300 break;
19301 case DW_FORM_ref1:
9c541725 19302 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19303 + read_1_byte (abfd, info_ptr));
c906108c
SS
19304 info_ptr += 1;
19305 break;
19306 case DW_FORM_ref2:
9c541725 19307 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19308 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19309 info_ptr += 2;
19310 break;
19311 case DW_FORM_ref4:
9c541725 19312 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19313 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19314 info_ptr += 4;
19315 break;
613e1657 19316 case DW_FORM_ref8:
9c541725 19317 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19318 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19319 info_ptr += 8;
19320 break;
55f1336d 19321 case DW_FORM_ref_sig8:
ac9ec31b 19322 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19323 info_ptr += 8;
19324 break;
c906108c 19325 case DW_FORM_ref_udata:
9c541725 19326 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19327 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19328 info_ptr += bytes_read;
19329 break;
c906108c 19330 case DW_FORM_indirect:
a8329558
KW
19331 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19332 info_ptr += bytes_read;
43988095
JK
19333 if (form == DW_FORM_implicit_const)
19334 {
19335 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19336 info_ptr += bytes_read;
19337 }
19338 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19339 info_ptr, need_reprocess);
43988095
JK
19340 break;
19341 case DW_FORM_implicit_const:
19342 DW_SND (attr) = implicit_const;
a8329558 19343 break;
336d760d 19344 case DW_FORM_addrx:
3019eac3 19345 case DW_FORM_GNU_addr_index:
18a8505e
AT
19346 *need_reprocess = true;
19347 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19348 info_ptr += bytes_read;
19349 break;
cf532bd1 19350 case DW_FORM_strx:
15f18d14
AT
19351 case DW_FORM_strx1:
19352 case DW_FORM_strx2:
19353 case DW_FORM_strx3:
19354 case DW_FORM_strx4:
3019eac3 19355 case DW_FORM_GNU_str_index:
3019eac3 19356 {
15f18d14
AT
19357 ULONGEST str_index;
19358 if (form == DW_FORM_strx1)
19359 {
19360 str_index = read_1_byte (abfd, info_ptr);
19361 info_ptr += 1;
19362 }
19363 else if (form == DW_FORM_strx2)
19364 {
19365 str_index = read_2_bytes (abfd, info_ptr);
19366 info_ptr += 2;
19367 }
19368 else if (form == DW_FORM_strx3)
19369 {
19370 str_index = read_3_bytes (abfd, info_ptr);
19371 info_ptr += 3;
19372 }
19373 else if (form == DW_FORM_strx4)
19374 {
19375 str_index = read_4_bytes (abfd, info_ptr);
19376 info_ptr += 4;
19377 }
19378 else
19379 {
19380 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19381 info_ptr += bytes_read;
19382 }
18a8505e
AT
19383 *need_reprocess = true;
19384 DW_UNSND (attr) = str_index;
19385 }
3019eac3 19386 break;
c906108c 19387 default:
8a3fe4f8 19388 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19389 dwarf_form_name (form),
19390 bfd_get_filename (abfd));
c906108c 19391 }
28e94949 19392
36586728 19393 /* Super hack. */
cd6c91b4 19394 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19395 attr->form = DW_FORM_GNU_ref_alt;
19396
28e94949
JB
19397 /* We have seen instances where the compiler tried to emit a byte
19398 size attribute of -1 which ended up being encoded as an unsigned
19399 0xffffffff. Although 0xffffffff is technically a valid size value,
19400 an object of this size seems pretty unlikely so we can relatively
19401 safely treat these cases as if the size attribute was invalid and
19402 treat them as zero by default. */
19403 if (attr->name == DW_AT_byte_size
19404 && form == DW_FORM_data4
19405 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19406 {
19407 complaint
b98664d3 19408 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19409 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19410 DW_UNSND (attr) = 0;
19411 }
28e94949 19412
c906108c
SS
19413 return info_ptr;
19414}
19415
a8329558
KW
19416/* Read an attribute described by an abbreviated attribute. */
19417
d521ce57 19418static const gdb_byte *
dee91e82
DE
19419read_attribute (const struct die_reader_specs *reader,
19420 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19421 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19422{
19423 attr->name = abbrev->name;
43988095 19424 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19425 abbrev->implicit_const, info_ptr,
19426 need_reprocess);
a8329558
KW
19427}
19428
43988095
JK
19429/* Return pointer to string at .debug_str offset STR_OFFSET. */
19430
19431static const char *
976ca316 19432read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 19433 LONGEST str_offset)
43988095 19434{
976ca316
SM
19435 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19436 str_offset, "DW_FORM_strp");
c906108c
SS
19437}
19438
43988095
JK
19439/* Return pointer to string at .debug_str offset as read from BUF.
19440 BUF is assumed to be in a compilation unit described by CU_HEADER.
19441 Return *BYTES_READ_PTR count of bytes read from BUF. */
19442
d521ce57 19443static const char *
976ca316 19444read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 19445 const gdb_byte *buf,
cf2c3c16
TT
19446 const struct comp_unit_head *cu_header,
19447 unsigned int *bytes_read_ptr)
19448{
8266302d 19449 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19450
976ca316 19451 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
19452}
19453
86c0bb4c 19454/* See read.h. */
43988095 19455
86c0bb4c
TT
19456const char *
19457dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19458 const struct comp_unit_head *cu_header,
19459 unsigned int *bytes_read_ptr)
43988095 19460{
86c0bb4c 19461 bfd *abfd = objfile->obfd;
8266302d 19462 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19463
5989a64e 19464 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19465}
19466
3019eac3 19467/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19468 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19469 ADDR_SIZE is the size of addresses from the CU header. */
19470
19471static CORE_ADDR
976ca316
SM
19472read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19473 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 19474{
976ca316 19475 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
19476 bfd *abfd = objfile->obfd;
19477 const gdb_byte *info_ptr;
18a8505e 19478 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19479
976ca316
SM
19480 per_objfile->per_bfd->addr.read (objfile);
19481 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19482 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19483 objfile_name (objfile));
18a8505e 19484 if (addr_base_or_zero + addr_index * addr_size
976ca316 19485 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
19486 error (_("DW_FORM_addr_index pointing outside of "
19487 ".debug_addr section [in module %s]"),
4262abfb 19488 objfile_name (objfile));
976ca316
SM
19489 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19490 + addr_index * addr_size);
3019eac3
DE
19491 if (addr_size == 4)
19492 return bfd_get_32 (abfd, info_ptr);
19493 else
19494 return bfd_get_64 (abfd, info_ptr);
19495}
19496
19497/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19498
19499static CORE_ADDR
19500read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19501{
5e22e966 19502 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19503 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19504}
19505
19506/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19507
19508static CORE_ADDR
d521ce57 19509read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19510 unsigned int *bytes_read)
19511{
5e22e966 19512 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19513 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19514
19515 return read_addr_index (cu, addr_index);
19516}
19517
450a1bfc 19518/* See read.h. */
3019eac3
DE
19519
19520CORE_ADDR
82ca3f51 19521dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 19522 dwarf2_per_objfile *per_objfile,
82ca3f51 19523 unsigned int addr_index)
3019eac3 19524{
976ca316 19525 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 19526 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19527 int addr_size;
19528
3019eac3
DE
19529 /* We need addr_base and addr_size.
19530 If we don't have PER_CU->cu, we have to get it.
19531 Nasty, but the alternative is storing the needed info in PER_CU,
19532 which at this point doesn't seem justified: it's not clear how frequently
19533 it would get used and it would increase the size of every PER_CU.
19534 Entry points like dwarf2_per_cu_addr_size do a similar thing
19535 so we're not in uncharted territory here.
19536 Alas we need to be a bit more complicated as addr_base is contained
19537 in the DIE.
19538
19539 We don't need to read the entire CU(/TU).
19540 We just need the header and top level die.
a1b64ce1 19541
3019eac3 19542 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19543 For now we skip this optimization. */
3019eac3
DE
19544
19545 if (cu != NULL)
19546 {
19547 addr_base = cu->addr_base;
19548 addr_size = cu->header.addr_size;
19549 }
19550 else
19551 {
976ca316 19552 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
19553 addr_base = reader.cu->addr_base;
19554 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19555 }
19556
976ca316 19557 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
19558}
19559
18a8505e
AT
19560/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19561 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19562 DWO file. */
3019eac3 19563
d521ce57 19564static const char *
18a8505e
AT
19565read_str_index (struct dwarf2_cu *cu,
19566 struct dwarf2_section_info *str_section,
19567 struct dwarf2_section_info *str_offsets_section,
19568 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19569{
976ca316
SM
19570 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19571 struct objfile *objfile = per_objfile->objfile;
c5164cbc 19572 const char *objf_name = objfile_name (objfile);
3019eac3 19573 bfd *abfd = objfile->obfd;
d521ce57 19574 const gdb_byte *info_ptr;
3019eac3 19575 ULONGEST str_offset;
cf532bd1 19576 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19577
96b79293
TT
19578 str_section->read (objfile);
19579 str_offsets_section->read (objfile);
73869dc2 19580 if (str_section->buffer == NULL)
18a8505e 19581 error (_("%s used without %s section"
9d8780f0 19582 " in CU at offset %s [in module %s]"),
96b79293 19583 form_name, str_section->get_name (),
18a8505e 19584 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19585 if (str_offsets_section->buffer == NULL)
18a8505e 19586 error (_("%s used without %s section"
9d8780f0 19587 " in CU at offset %s [in module %s]"),
96b79293 19588 form_name, str_section->get_name (),
18a8505e 19589 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19590 info_ptr = (str_offsets_section->buffer
18a8505e 19591 + str_offsets_base
3019eac3
DE
19592 + str_index * cu->header.offset_size);
19593 if (cu->header.offset_size == 4)
19594 str_offset = bfd_get_32 (abfd, info_ptr);
19595 else
19596 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19597 if (str_offset >= str_section->size)
57d63ce2 19598 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19599 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19600 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19601 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19602}
19603
18a8505e
AT
19604/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19605
19606static const char *
19607read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19608{
19609 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19610 ? reader->cu->header.addr_size : 0;
19611 return read_str_index (reader->cu,
19612 &reader->dwo_file->sections.str,
19613 &reader->dwo_file->sections.str_offsets,
19614 str_offsets_base, str_index);
19615}
19616
19617/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19618
19619static const char *
19620read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19621{
5e22e966 19622 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19623 const char *objf_name = objfile_name (objfile);
19624 static const char form_name[] = "DW_FORM_GNU_str_index";
19625 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19626
19627 if (!cu->str_offsets_base.has_value ())
19628 error (_("%s used in Fission stub without %s"
19629 " in CU at offset 0x%lx [in module %s]"),
19630 form_name, str_offsets_attr_name,
19631 (long) cu->header.offset_size, objf_name);
19632
19633 return read_str_index (cu,
5e22e966
SM
19634 &cu->per_objfile->per_bfd->str,
19635 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19636 *cu->str_offsets_base, str_index);
19637}
19638
3019eac3
DE
19639/* Return the length of an LEB128 number in BUF. */
19640
19641static int
19642leb128_size (const gdb_byte *buf)
19643{
19644 const gdb_byte *begin = buf;
19645 gdb_byte byte;
19646
19647 while (1)
19648 {
19649 byte = *buf++;
19650 if ((byte & 128) == 0)
19651 return buf - begin;
19652 }
19653}
19654
c906108c 19655static void
e142c38c 19656set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19657{
19658 switch (lang)
19659 {
19660 case DW_LANG_C89:
76bee0cc 19661 case DW_LANG_C99:
0cfd832f 19662 case DW_LANG_C11:
c906108c 19663 case DW_LANG_C:
d1be3247 19664 case DW_LANG_UPC:
e142c38c 19665 cu->language = language_c;
c906108c 19666 break;
9c37b5ae 19667 case DW_LANG_Java:
c906108c 19668 case DW_LANG_C_plus_plus:
0cfd832f
MW
19669 case DW_LANG_C_plus_plus_11:
19670 case DW_LANG_C_plus_plus_14:
e142c38c 19671 cu->language = language_cplus;
c906108c 19672 break;
6aecb9c2
JB
19673 case DW_LANG_D:
19674 cu->language = language_d;
19675 break;
c906108c
SS
19676 case DW_LANG_Fortran77:
19677 case DW_LANG_Fortran90:
b21b22e0 19678 case DW_LANG_Fortran95:
f7de9aab
MW
19679 case DW_LANG_Fortran03:
19680 case DW_LANG_Fortran08:
e142c38c 19681 cu->language = language_fortran;
c906108c 19682 break;
a766d390
DE
19683 case DW_LANG_Go:
19684 cu->language = language_go;
19685 break;
c906108c 19686 case DW_LANG_Mips_Assembler:
e142c38c 19687 cu->language = language_asm;
c906108c
SS
19688 break;
19689 case DW_LANG_Ada83:
8aaf0b47 19690 case DW_LANG_Ada95:
bc5f45f8
JB
19691 cu->language = language_ada;
19692 break;
72019c9c
GM
19693 case DW_LANG_Modula2:
19694 cu->language = language_m2;
19695 break;
fe8e67fd
PM
19696 case DW_LANG_Pascal83:
19697 cu->language = language_pascal;
19698 break;
22566fbd
DJ
19699 case DW_LANG_ObjC:
19700 cu->language = language_objc;
19701 break;
c44af4eb
TT
19702 case DW_LANG_Rust:
19703 case DW_LANG_Rust_old:
19704 cu->language = language_rust;
19705 break;
c906108c
SS
19706 case DW_LANG_Cobol74:
19707 case DW_LANG_Cobol85:
c906108c 19708 default:
e142c38c 19709 cu->language = language_minimal;
c906108c
SS
19710 break;
19711 }
e142c38c 19712 cu->language_defn = language_def (cu->language);
c906108c
SS
19713}
19714
19715/* Return the named attribute or NULL if not there. */
19716
19717static struct attribute *
e142c38c 19718dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19719{
a48e046c 19720 for (;;)
c906108c 19721 {
a48e046c
TT
19722 unsigned int i;
19723 struct attribute *spec = NULL;
19724
19725 for (i = 0; i < die->num_attrs; ++i)
19726 {
19727 if (die->attrs[i].name == name)
19728 return &die->attrs[i];
19729 if (die->attrs[i].name == DW_AT_specification
19730 || die->attrs[i].name == DW_AT_abstract_origin)
19731 spec = &die->attrs[i];
19732 }
19733
19734 if (!spec)
19735 break;
c906108c 19736
f2f0e013 19737 die = follow_die_ref (die, spec, &cu);
f2f0e013 19738 }
c5aa993b 19739
c906108c
SS
19740 return NULL;
19741}
19742
7d45c7c3
KB
19743/* Return the string associated with a string-typed attribute, or NULL if it
19744 is either not found or is of an incorrect type. */
19745
19746static const char *
19747dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19748{
19749 struct attribute *attr;
19750 const char *str = NULL;
19751
19752 attr = dwarf2_attr (die, name, cu);
19753
19754 if (attr != NULL)
19755 {
e61108c9
TT
19756 str = attr->value_as_string ();
19757 if (str == nullptr)
b98664d3 19758 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19759 "DIE at %s in module %s"),
19760 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19761 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19762 }
19763
19764 return str;
19765}
19766
a084a2a6 19767/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19768 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19769static const char *
19770dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19771{
19772 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19773 if (dwo_name == nullptr)
19774 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19775 return dwo_name;
19776}
19777
05cf31d1
JB
19778/* Return non-zero iff the attribute NAME is defined for the given DIE,
19779 and holds a non-zero value. This function should only be used for
2dc7f7b3 19780 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19781
19782static int
19783dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19784{
19785 struct attribute *attr = dwarf2_attr (die, name, cu);
19786
19787 return (attr && DW_UNSND (attr));
19788}
19789
3ca72b44 19790static int
e142c38c 19791die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19792{
05cf31d1
JB
19793 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19794 which value is non-zero. However, we have to be careful with
19795 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19796 (via dwarf2_flag_true_p) follows this attribute. So we may
19797 end up accidently finding a declaration attribute that belongs
19798 to a different DIE referenced by the specification attribute,
19799 even though the given DIE does not have a declaration attribute. */
19800 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19801 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19802}
19803
63d06c5c 19804/* Return the die giving the specification for DIE, if there is
f2f0e013 19805 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19806 containing the return value on output. If there is no
19807 specification, but there is an abstract origin, that is
19808 returned. */
63d06c5c
DC
19809
19810static struct die_info *
f2f0e013 19811die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19812{
f2f0e013
DJ
19813 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19814 *spec_cu);
63d06c5c 19815
edb3359d
DJ
19816 if (spec_attr == NULL)
19817 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19818
63d06c5c
DC
19819 if (spec_attr == NULL)
19820 return NULL;
19821 else
f2f0e013 19822 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19823}
c906108c 19824
527f3840
JK
19825/* Stub for free_line_header to match void * callback types. */
19826
19827static void
19828free_line_header_voidp (void *arg)
19829{
9a3c8263 19830 struct line_header *lh = (struct line_header *) arg;
527f3840 19831
fff8551c 19832 delete lh;
527f3840
JK
19833}
19834
83769d0b 19835/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19836
19837static struct dwarf2_section_info *
19838get_debug_line_section (struct dwarf2_cu *cu)
19839{
19840 struct dwarf2_section_info *section;
976ca316 19841 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
19842
19843 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19844 DWO file. */
19845 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19846 section = &cu->dwo_unit->dwo_file->sections.line;
19847 else if (cu->per_cu->is_dwz)
19848 {
976ca316 19849 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
19850
19851 section = &dwz->line;
19852 }
19853 else
976ca316 19854 section = &per_objfile->per_bfd->line;
36586728
TT
19855
19856 return section;
19857}
19858
debd256d 19859/* Read the statement program header starting at OFFSET in
3019eac3 19860 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19861 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19862 Returns NULL if there is a problem reading the header, e.g., if it
19863 has a version we don't understand.
debd256d
JB
19864
19865 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19866 the returned object point into the dwarf line section buffer,
19867 and must not be freed. */
ae2de4f8 19868
fff8551c 19869static line_header_up
9c541725 19870dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19871{
3019eac3 19872 struct dwarf2_section_info *section;
976ca316 19873 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 19874
36586728 19875 section = get_debug_line_section (cu);
976ca316 19876 section->read (per_objfile->objfile);
3019eac3 19877 if (section->buffer == NULL)
debd256d 19878 {
3019eac3 19879 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19880 complaint (_("missing .debug_line.dwo section"));
3019eac3 19881 else
b98664d3 19882 complaint (_("missing .debug_line section"));
debd256d
JB
19883 return 0;
19884 }
19885
0df7ad3a 19886 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 19887 per_objfile, section, &cu->header);
debd256d 19888}
c906108c 19889
c6da4cef 19890/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19891 Return the file name of the psymtab for the given file_entry.
c6da4cef 19892 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19893 If space for the result is malloc'd, *NAME_HOLDER will be set.
19894 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19895
d521ce57 19896static const char *
7ba99d21 19897psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19898 const dwarf2_psymtab *pst,
c89b44cd
TT
19899 const char *comp_dir,
19900 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19901{
d521ce57
TT
19902 const char *include_name = fe.name;
19903 const char *include_name_to_compare = include_name;
72b9f47f 19904 const char *pst_filename;
c6da4cef
DE
19905 int file_is_pst;
19906
8c43009f 19907 const char *dir_name = fe.include_dir (lh);
c6da4cef 19908
c89b44cd 19909 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19910 if (!IS_ABSOLUTE_PATH (include_name)
19911 && (dir_name != NULL || comp_dir != NULL))
19912 {
19913 /* Avoid creating a duplicate psymtab for PST.
19914 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19915 Before we do the comparison, however, we need to account
19916 for DIR_NAME and COMP_DIR.
19917 First prepend dir_name (if non-NULL). If we still don't
19918 have an absolute path prepend comp_dir (if non-NULL).
19919 However, the directory we record in the include-file's
19920 psymtab does not contain COMP_DIR (to match the
19921 corresponding symtab(s)).
19922
19923 Example:
19924
19925 bash$ cd /tmp
19926 bash$ gcc -g ./hello.c
19927 include_name = "hello.c"
19928 dir_name = "."
19929 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19930 DW_AT_name = "./hello.c"
19931
19932 */
c6da4cef
DE
19933
19934 if (dir_name != NULL)
19935 {
c89b44cd
TT
19936 name_holder->reset (concat (dir_name, SLASH_STRING,
19937 include_name, (char *) NULL));
19938 include_name = name_holder->get ();
c6da4cef 19939 include_name_to_compare = include_name;
c6da4cef
DE
19940 }
19941 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19942 {
c89b44cd
TT
19943 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19944 include_name, (char *) NULL));
19945 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19946 }
19947 }
19948
19949 pst_filename = pst->filename;
c89b44cd 19950 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19951 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19952 {
c89b44cd
TT
19953 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19954 pst_filename, (char *) NULL));
19955 pst_filename = copied_name.get ();
c6da4cef
DE
19956 }
19957
1e3fad37 19958 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19959
c6da4cef
DE
19960 if (file_is_pst)
19961 return NULL;
19962 return include_name;
19963}
19964
d9b3de22
DE
19965/* State machine to track the state of the line number program. */
19966
6f77053d 19967class lnp_state_machine
d9b3de22 19968{
6f77053d
PA
19969public:
19970 /* Initialize a machine state for the start of a line number
19971 program. */
804d2729
TT
19972 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19973 bool record_lines_p);
6f77053d 19974
8c43009f
PA
19975 file_entry *current_file ()
19976 {
19977 /* lh->file_names is 0-based, but the file name numbers in the
19978 statement program are 1-based. */
6f77053d
PA
19979 return m_line_header->file_name_at (m_file);
19980 }
19981
19982 /* Record the line in the state machine. END_SEQUENCE is true if
19983 we're processing the end of a sequence. */
19984 void record_line (bool end_sequence);
19985
7ab6656f
OJ
19986 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19987 nop-out rest of the lines in this sequence. */
6f77053d
PA
19988 void check_line_address (struct dwarf2_cu *cu,
19989 const gdb_byte *line_ptr,
7ab6656f 19990 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19991
19992 void handle_set_discriminator (unsigned int discriminator)
19993 {
19994 m_discriminator = discriminator;
19995 m_line_has_non_zero_discriminator |= discriminator != 0;
19996 }
19997
19998 /* Handle DW_LNE_set_address. */
19999 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20000 {
20001 m_op_index = 0;
20002 address += baseaddr;
20003 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20004 }
20005
20006 /* Handle DW_LNS_advance_pc. */
20007 void handle_advance_pc (CORE_ADDR adjust);
20008
20009 /* Handle a special opcode. */
20010 void handle_special_opcode (unsigned char op_code);
20011
20012 /* Handle DW_LNS_advance_line. */
20013 void handle_advance_line (int line_delta)
20014 {
20015 advance_line (line_delta);
20016 }
20017
20018 /* Handle DW_LNS_set_file. */
20019 void handle_set_file (file_name_index file);
20020
20021 /* Handle DW_LNS_negate_stmt. */
20022 void handle_negate_stmt ()
20023 {
20024 m_is_stmt = !m_is_stmt;
20025 }
20026
20027 /* Handle DW_LNS_const_add_pc. */
20028 void handle_const_add_pc ();
20029
20030 /* Handle DW_LNS_fixed_advance_pc. */
20031 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20032 {
20033 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20034 m_op_index = 0;
20035 }
20036
20037 /* Handle DW_LNS_copy. */
20038 void handle_copy ()
20039 {
20040 record_line (false);
20041 m_discriminator = 0;
20042 }
20043
20044 /* Handle DW_LNE_end_sequence. */
20045 void handle_end_sequence ()
20046 {
804d2729 20047 m_currently_recording_lines = true;
6f77053d
PA
20048 }
20049
20050private:
20051 /* Advance the line by LINE_DELTA. */
20052 void advance_line (int line_delta)
20053 {
20054 m_line += line_delta;
20055
20056 if (line_delta != 0)
20057 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20058 }
20059
804d2729
TT
20060 struct dwarf2_cu *m_cu;
20061
6f77053d
PA
20062 gdbarch *m_gdbarch;
20063
20064 /* True if we're recording lines.
20065 Otherwise we're building partial symtabs and are just interested in
20066 finding include files mentioned by the line number program. */
20067 bool m_record_lines_p;
20068
8c43009f 20069 /* The line number header. */
6f77053d 20070 line_header *m_line_header;
8c43009f 20071
6f77053d
PA
20072 /* These are part of the standard DWARF line number state machine,
20073 and initialized according to the DWARF spec. */
d9b3de22 20074
6f77053d 20075 unsigned char m_op_index = 0;
7ba99d21
AT
20076 /* The line table index of the current file. */
20077 file_name_index m_file = 1;
6f77053d
PA
20078 unsigned int m_line = 1;
20079
20080 /* These are initialized in the constructor. */
20081
20082 CORE_ADDR m_address;
20083 bool m_is_stmt;
20084 unsigned int m_discriminator;
d9b3de22
DE
20085
20086 /* Additional bits of state we need to track. */
20087
20088 /* The last file that we called dwarf2_start_subfile for.
20089 This is only used for TLLs. */
6f77053d 20090 unsigned int m_last_file = 0;
d9b3de22 20091 /* The last file a line number was recorded for. */
6f77053d 20092 struct subfile *m_last_subfile = NULL;
d9b3de22 20093
1313c56e
AB
20094 /* The address of the last line entry. */
20095 CORE_ADDR m_last_address;
20096
20097 /* Set to true when a previous line at the same address (using
20098 m_last_address) had m_is_stmt true. This is reset to false when a
20099 line entry at a new address (m_address different to m_last_address) is
20100 processed. */
20101 bool m_stmt_at_address = false;
20102
804d2729
TT
20103 /* When true, record the lines we decode. */
20104 bool m_currently_recording_lines = false;
d9b3de22
DE
20105
20106 /* The last line number that was recorded, used to coalesce
20107 consecutive entries for the same line. This can happen, for
20108 example, when discriminators are present. PR 17276. */
6f77053d
PA
20109 unsigned int m_last_line = 0;
20110 bool m_line_has_non_zero_discriminator = false;
8c43009f 20111};
d9b3de22 20112
6f77053d
PA
20113void
20114lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20115{
20116 CORE_ADDR addr_adj = (((m_op_index + adjust)
20117 / m_line_header->maximum_ops_per_instruction)
20118 * m_line_header->minimum_instruction_length);
20119 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20120 m_op_index = ((m_op_index + adjust)
20121 % m_line_header->maximum_ops_per_instruction);
20122}
d9b3de22 20123
6f77053d
PA
20124void
20125lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20126{
6f77053d 20127 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20128 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20129 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20130 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20131 / m_line_header->maximum_ops_per_instruction)
20132 * m_line_header->minimum_instruction_length);
20133 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20134 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20135 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20136
258bf0ee 20137 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20138 advance_line (line_delta);
20139 record_line (false);
20140 m_discriminator = 0;
20141}
d9b3de22 20142
6f77053d
PA
20143void
20144lnp_state_machine::handle_set_file (file_name_index file)
20145{
20146 m_file = file;
20147
20148 const file_entry *fe = current_file ();
20149 if (fe == NULL)
20150 dwarf2_debug_line_missing_file_complaint ();
20151 else if (m_record_lines_p)
20152 {
20153 const char *dir = fe->include_dir (m_line_header);
20154
c24bdb02 20155 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20156 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20157 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20158 }
20159}
20160
20161void
20162lnp_state_machine::handle_const_add_pc ()
20163{
20164 CORE_ADDR adjust
20165 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20166
20167 CORE_ADDR addr_adj
20168 = (((m_op_index + adjust)
20169 / m_line_header->maximum_ops_per_instruction)
20170 * m_line_header->minimum_instruction_length);
20171
20172 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20173 m_op_index = ((m_op_index + adjust)
20174 % m_line_header->maximum_ops_per_instruction);
20175}
d9b3de22 20176
a05a36a5
DE
20177/* Return non-zero if we should add LINE to the line number table.
20178 LINE is the line to add, LAST_LINE is the last line that was added,
20179 LAST_SUBFILE is the subfile for LAST_LINE.
20180 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20181 had a non-zero discriminator.
20182
20183 We have to be careful in the presence of discriminators.
20184 E.g., for this line:
20185
20186 for (i = 0; i < 100000; i++);
20187
20188 clang can emit four line number entries for that one line,
20189 each with a different discriminator.
20190 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20191
20192 However, we want gdb to coalesce all four entries into one.
20193 Otherwise the user could stepi into the middle of the line and
20194 gdb would get confused about whether the pc really was in the
20195 middle of the line.
20196
20197 Things are further complicated by the fact that two consecutive
20198 line number entries for the same line is a heuristic used by gcc
20199 to denote the end of the prologue. So we can't just discard duplicate
20200 entries, we have to be selective about it. The heuristic we use is
20201 that we only collapse consecutive entries for the same line if at least
20202 one of those entries has a non-zero discriminator. PR 17276.
20203
20204 Note: Addresses in the line number state machine can never go backwards
20205 within one sequence, thus this coalescing is ok. */
20206
20207static int
804d2729
TT
20208dwarf_record_line_p (struct dwarf2_cu *cu,
20209 unsigned int line, unsigned int last_line,
a05a36a5
DE
20210 int line_has_non_zero_discriminator,
20211 struct subfile *last_subfile)
20212{
c24bdb02 20213 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20214 return 1;
20215 if (line != last_line)
20216 return 1;
20217 /* Same line for the same file that we've seen already.
20218 As a last check, for pr 17276, only record the line if the line
20219 has never had a non-zero discriminator. */
20220 if (!line_has_non_zero_discriminator)
20221 return 1;
20222 return 0;
20223}
20224
804d2729
TT
20225/* Use the CU's builder to record line number LINE beginning at
20226 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20227
20228static void
d9b3de22 20229dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20230 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20231 struct dwarf2_cu *cu)
252a6764
DE
20232{
20233 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20234
27e0867f
DE
20235 if (dwarf_line_debug)
20236 {
20237 fprintf_unfiltered (gdb_stdlog,
20238 "Recording line %u, file %s, address %s\n",
20239 line, lbasename (subfile->name),
20240 paddress (gdbarch, address));
20241 }
20242
804d2729 20243 if (cu != nullptr)
8c95582d 20244 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20245}
20246
20247/* Subroutine of dwarf_decode_lines_1 to simplify it.
20248 Mark the end of a set of line number records.
d9b3de22 20249 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20250 If SUBFILE is NULL the request is ignored. */
20251
20252static void
20253dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20254 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20255{
27e0867f
DE
20256 if (subfile == NULL)
20257 return;
20258
20259 if (dwarf_line_debug)
20260 {
20261 fprintf_unfiltered (gdb_stdlog,
20262 "Finishing current line, file %s, address %s\n",
20263 lbasename (subfile->name),
20264 paddress (gdbarch, address));
20265 }
20266
8c95582d 20267 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20268}
20269
6f77053d
PA
20270void
20271lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20272{
d9b3de22
DE
20273 if (dwarf_line_debug)
20274 {
20275 fprintf_unfiltered (gdb_stdlog,
20276 "Processing actual line %u: file %u,"
94a72be7 20277 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20278 m_line, m_file,
6f77053d 20279 paddress (m_gdbarch, m_address),
94a72be7
AB
20280 m_is_stmt, m_discriminator,
20281 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20282 }
20283
6f77053d 20284 file_entry *fe = current_file ();
8c43009f
PA
20285
20286 if (fe == NULL)
d9b3de22
DE
20287 dwarf2_debug_line_missing_file_complaint ();
20288 /* For now we ignore lines not starting on an instruction boundary.
20289 But not when processing end_sequence for compatibility with the
20290 previous version of the code. */
6f77053d 20291 else if (m_op_index == 0 || end_sequence)
d9b3de22 20292 {
8c43009f 20293 fe->included_p = 1;
8c95582d 20294 if (m_record_lines_p)
d9b3de22 20295 {
1313c56e
AB
20296 /* When we switch files we insert an end maker in the first file,
20297 switch to the second file and add a new line entry. The
20298 problem is that the end marker inserted in the first file will
20299 discard any previous line entries at the same address. If the
20300 line entries in the first file are marked as is-stmt, while
20301 the new line in the second file is non-stmt, then this means
20302 the end marker will discard is-stmt lines so we can have a
20303 non-stmt line. This means that there are less addresses at
20304 which the user can insert a breakpoint.
20305
20306 To improve this we track the last address in m_last_address,
20307 and whether we have seen an is-stmt at this address. Then
20308 when switching files, if we have seen a stmt at the current
20309 address, and we are switching to create a non-stmt line, then
20310 discard the new line. */
20311 bool file_changed
20312 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20313 bool ignore_this_line
20314 = (file_changed && !end_sequence && m_last_address == m_address
20315 && !m_is_stmt && m_stmt_at_address);
20316
20317 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 20318 {
804d2729
TT
20319 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20320 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20321 }
20322
1313c56e 20323 if (!end_sequence && !ignore_this_line)
d9b3de22 20324 {
8c95582d
AB
20325 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20326
804d2729 20327 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20328 m_line_has_non_zero_discriminator,
20329 m_last_subfile))
d9b3de22 20330 {
c24bdb02 20331 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20332 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20333 builder->get_current_subfile (),
8c95582d 20334 m_line, m_address, is_stmt,
804d2729 20335 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20336 }
c24bdb02 20337 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20338 m_last_line = m_line;
d9b3de22
DE
20339 }
20340 }
20341 }
1313c56e
AB
20342
20343 /* Track whether we have seen any m_is_stmt true at m_address in case we
20344 have multiple line table entries all at m_address. */
20345 if (m_last_address != m_address)
20346 {
20347 m_stmt_at_address = false;
20348 m_last_address = m_address;
20349 }
20350 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
20351}
20352
804d2729
TT
20353lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20354 line_header *lh, bool record_lines_p)
d9b3de22 20355{
804d2729 20356 m_cu = cu;
6f77053d
PA
20357 m_gdbarch = arch;
20358 m_record_lines_p = record_lines_p;
20359 m_line_header = lh;
d9b3de22 20360
804d2729 20361 m_currently_recording_lines = true;
d9b3de22 20362
d9b3de22
DE
20363 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20364 was a line entry for it so that the backend has a chance to adjust it
20365 and also record it in case it needs it. This is currently used by MIPS
20366 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20367 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20368 m_is_stmt = lh->default_is_stmt;
20369 m_discriminator = 0;
1313c56e
AB
20370
20371 m_last_address = m_address;
20372 m_stmt_at_address = false;
252a6764
DE
20373}
20374
6f77053d
PA
20375void
20376lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20377 const gdb_byte *line_ptr,
7ab6656f 20378 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20379{
7ab6656f
OJ
20380 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20381 the pc range of the CU. However, we restrict the test to only ADDRESS
20382 values of zero to preserve GDB's previous behaviour which is to handle
20383 the specific case of a function being GC'd by the linker. */
924c2928 20384
7ab6656f 20385 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20386 {
20387 /* This line table is for a function which has been
20388 GCd by the linker. Ignore it. PR gdb/12528 */
20389
5e22e966 20390 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20391 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20392
b98664d3 20393 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20394 line_offset, objfile_name (objfile));
804d2729
TT
20395 m_currently_recording_lines = false;
20396 /* Note: m_currently_recording_lines is left as false until we see
20397 DW_LNE_end_sequence. */
924c2928
DE
20398 }
20399}
20400
f3f5162e 20401/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20402 Process the line number information in LH.
20403 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20404 program in order to set included_p for every referenced header. */
debd256d 20405
c906108c 20406static void
43f3e411
DE
20407dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20408 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20409{
d521ce57
TT
20410 const gdb_byte *line_ptr, *extended_end;
20411 const gdb_byte *line_end;
a8c50c1f 20412 unsigned int bytes_read, extended_len;
699ca60a 20413 unsigned char op_code, extended_op;
e142c38c 20414 CORE_ADDR baseaddr;
5e22e966 20415 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20416 bfd *abfd = objfile->obfd;
08feed99 20417 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20418 /* True if we're recording line info (as opposed to building partial
20419 symtabs and just interested in finding include files mentioned by
20420 the line number program). */
20421 bool record_lines_p = !decode_for_pst_p;
e142c38c 20422
b3b3bada 20423 baseaddr = objfile->text_section_offset ();
c906108c 20424
debd256d
JB
20425 line_ptr = lh->statement_program_start;
20426 line_end = lh->statement_program_end;
c906108c
SS
20427
20428 /* Read the statement sequences until there's nothing left. */
20429 while (line_ptr < line_end)
20430 {
6f77053d
PA
20431 /* The DWARF line number program state machine. Reset the state
20432 machine at the start of each sequence. */
804d2729 20433 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20434 bool end_sequence = false;
d9b3de22 20435
8c43009f 20436 if (record_lines_p)
c906108c 20437 {
8c43009f
PA
20438 /* Start a subfile for the current file of the state
20439 machine. */
20440 const file_entry *fe = state_machine.current_file ();
20441
20442 if (fe != NULL)
804d2729 20443 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20444 }
20445
a738430d 20446 /* Decode the table. */
d9b3de22 20447 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20448 {
20449 op_code = read_1_byte (abfd, line_ptr);
20450 line_ptr += 1;
9aa1fe7e 20451
debd256d 20452 if (op_code >= lh->opcode_base)
6e70227d 20453 {
8e07a239 20454 /* Special opcode. */
6f77053d 20455 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20456 }
20457 else switch (op_code)
c906108c
SS
20458 {
20459 case DW_LNS_extended_op:
3e43a32a
MS
20460 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20461 &bytes_read);
473b7be6 20462 line_ptr += bytes_read;
a8c50c1f 20463 extended_end = line_ptr + extended_len;
c906108c
SS
20464 extended_op = read_1_byte (abfd, line_ptr);
20465 line_ptr += 1;
20466 switch (extended_op)
20467 {
20468 case DW_LNE_end_sequence:
6f77053d
PA
20469 state_machine.handle_end_sequence ();
20470 end_sequence = true;
c906108c
SS
20471 break;
20472 case DW_LNE_set_address:
d9b3de22
DE
20473 {
20474 CORE_ADDR address
c8a7a66f 20475 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20476 line_ptr += bytes_read;
6f77053d
PA
20477
20478 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20479 lowpc - baseaddr, address);
6f77053d 20480 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20481 }
c906108c
SS
20482 break;
20483 case DW_LNE_define_file:
debd256d 20484 {
d521ce57 20485 const char *cur_file;
ecfb656c
PA
20486 unsigned int mod_time, length;
20487 dir_index dindex;
6e70227d 20488
3e43a32a
MS
20489 cur_file = read_direct_string (abfd, line_ptr,
20490 &bytes_read);
debd256d 20491 line_ptr += bytes_read;
ecfb656c 20492 dindex = (dir_index)
debd256d
JB
20493 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20494 line_ptr += bytes_read;
20495 mod_time =
20496 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20497 line_ptr += bytes_read;
20498 length =
20499 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20500 line_ptr += bytes_read;
ecfb656c 20501 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20502 }
c906108c 20503 break;
d0c6ba3d 20504 case DW_LNE_set_discriminator:
6f77053d
PA
20505 {
20506 /* The discriminator is not interesting to the
20507 debugger; just ignore it. We still need to
20508 check its value though:
20509 if there are consecutive entries for the same
20510 (non-prologue) line we want to coalesce them.
20511 PR 17276. */
20512 unsigned int discr
20513 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20514 line_ptr += bytes_read;
20515
20516 state_machine.handle_set_discriminator (discr);
20517 }
d0c6ba3d 20518 break;
c906108c 20519 default:
b98664d3 20520 complaint (_("mangled .debug_line section"));
debd256d 20521 return;
c906108c 20522 }
a8c50c1f
DJ
20523 /* Make sure that we parsed the extended op correctly. If e.g.
20524 we expected a different address size than the producer used,
20525 we may have read the wrong number of bytes. */
20526 if (line_ptr != extended_end)
20527 {
b98664d3 20528 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20529 return;
20530 }
c906108c
SS
20531 break;
20532 case DW_LNS_copy:
6f77053d 20533 state_machine.handle_copy ();
c906108c
SS
20534 break;
20535 case DW_LNS_advance_pc:
2dc7f7b3
TT
20536 {
20537 CORE_ADDR adjust
20538 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20539 line_ptr += bytes_read;
6f77053d
PA
20540
20541 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20542 }
c906108c
SS
20543 break;
20544 case DW_LNS_advance_line:
a05a36a5
DE
20545 {
20546 int line_delta
20547 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20548 line_ptr += bytes_read;
6f77053d
PA
20549
20550 state_machine.handle_advance_line (line_delta);
a05a36a5 20551 }
c906108c
SS
20552 break;
20553 case DW_LNS_set_file:
d9b3de22 20554 {
6f77053d 20555 file_name_index file
ecfb656c
PA
20556 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20557 &bytes_read);
d9b3de22 20558 line_ptr += bytes_read;
8c43009f 20559
6f77053d 20560 state_machine.handle_set_file (file);
d9b3de22 20561 }
c906108c
SS
20562 break;
20563 case DW_LNS_set_column:
0ad93d4f 20564 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20565 line_ptr += bytes_read;
20566 break;
20567 case DW_LNS_negate_stmt:
6f77053d 20568 state_machine.handle_negate_stmt ();
c906108c
SS
20569 break;
20570 case DW_LNS_set_basic_block:
c906108c 20571 break;
c2c6d25f
JM
20572 /* Add to the address register of the state machine the
20573 address increment value corresponding to special opcode
a738430d
MK
20574 255. I.e., this value is scaled by the minimum
20575 instruction length since special opcode 255 would have
b021a221 20576 scaled the increment. */
c906108c 20577 case DW_LNS_const_add_pc:
6f77053d 20578 state_machine.handle_const_add_pc ();
c906108c
SS
20579 break;
20580 case DW_LNS_fixed_advance_pc:
3e29f34a 20581 {
6f77053d 20582 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20583 line_ptr += 2;
6f77053d
PA
20584
20585 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20586 }
c906108c 20587 break;
9aa1fe7e 20588 default:
a738430d
MK
20589 {
20590 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20591 int i;
a738430d 20592
debd256d 20593 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20594 {
20595 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20596 line_ptr += bytes_read;
20597 }
20598 }
c906108c
SS
20599 }
20600 }
d9b3de22
DE
20601
20602 if (!end_sequence)
20603 dwarf2_debug_line_missing_end_sequence_complaint ();
20604
20605 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20606 in which case we still finish recording the last line). */
6f77053d 20607 state_machine.record_line (true);
c906108c 20608 }
f3f5162e
DE
20609}
20610
20611/* Decode the Line Number Program (LNP) for the given line_header
20612 structure and CU. The actual information extracted and the type
20613 of structures created from the LNP depends on the value of PST.
20614
20615 1. If PST is NULL, then this procedure uses the data from the program
20616 to create all necessary symbol tables, and their linetables.
20617
20618 2. If PST is not NULL, this procedure reads the program to determine
20619 the list of files included by the unit represented by PST, and
20620 builds all the associated partial symbol tables.
20621
20622 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20623 It is used for relative paths in the line table.
20624 NOTE: When processing partial symtabs (pst != NULL),
20625 comp_dir == pst->dirname.
20626
20627 NOTE: It is important that psymtabs have the same file name (via strcmp)
20628 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20629 symtab we don't use it in the name of the psymtabs we create.
20630 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20631 A good testcase for this is mb-inline.exp.
20632
527f3840
JK
20633 LOWPC is the lowest address in CU (or 0 if not known).
20634
20635 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20636 for its PC<->lines mapping information. Otherwise only the filename
20637 table is read in. */
f3f5162e
DE
20638
20639static void
20640dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20641 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20642 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20643{
5e22e966 20644 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20645 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20646
527f3840
JK
20647 if (decode_mapping)
20648 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20649
20650 if (decode_for_pst_p)
20651 {
aaa75496
JB
20652 /* Now that we're done scanning the Line Header Program, we can
20653 create the psymtab of each included file. */
7ba99d21
AT
20654 for (auto &file_entry : lh->file_names ())
20655 if (file_entry.included_p == 1)
aaa75496 20656 {
c89b44cd 20657 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20658 const char *include_name =
7ba99d21
AT
20659 psymtab_include_file_name (lh, file_entry, pst,
20660 comp_dir, &name_holder);
c6da4cef 20661 if (include_name != NULL)
aaa75496
JB
20662 dwarf2_create_include_psymtab (include_name, pst, objfile);
20663 }
20664 }
cb1df416
DJ
20665 else
20666 {
20667 /* Make sure a symtab is created for every file, even files
20668 which contain only variables (i.e. no code with associated
20669 line numbers). */
c24bdb02
KS
20670 buildsym_compunit *builder = cu->get_builder ();
20671 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20672
7ba99d21 20673 for (auto &fe : lh->file_names ())
cb1df416 20674 {
804d2729 20675 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20676 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20677 {
c24bdb02 20678 builder->get_current_subfile ()->symtab
804d2729 20679 = allocate_symtab (cust,
c24bdb02 20680 builder->get_current_subfile ()->name);
43f3e411 20681 }
c24bdb02 20682 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20683 }
20684 }
c906108c
SS
20685}
20686
20687/* Start a subfile for DWARF. FILENAME is the name of the file and
20688 DIRNAME the name of the source directory which contains FILENAME
4d663531 20689 or NULL if not known.
c906108c
SS
20690 This routine tries to keep line numbers from identical absolute and
20691 relative file names in a common subfile.
20692
20693 Using the `list' example from the GDB testsuite, which resides in
20694 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20695 of /srcdir/list0.c yields the following debugging information for list0.c:
20696
c5aa993b 20697 DW_AT_name: /srcdir/list0.c
4d663531 20698 DW_AT_comp_dir: /compdir
357e46e7 20699 files.files[0].name: list0.h
c5aa993b 20700 files.files[0].dir: /srcdir
357e46e7 20701 files.files[1].name: list0.c
c5aa993b 20702 files.files[1].dir: /srcdir
c906108c
SS
20703
20704 The line number information for list0.c has to end up in a single
4f1520fb
FR
20705 subfile, so that `break /srcdir/list0.c:1' works as expected.
20706 start_subfile will ensure that this happens provided that we pass the
20707 concatenation of files.files[1].dir and files.files[1].name as the
20708 subfile's name. */
c906108c
SS
20709
20710static void
804d2729
TT
20711dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20712 const char *dirname)
c906108c 20713{
43816ebc 20714 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20715
4d663531 20716 /* In order not to lose the line information directory,
4f1520fb
FR
20717 we concatenate it to the filename when it makes sense.
20718 Note that the Dwarf3 standard says (speaking of filenames in line
20719 information): ``The directory index is ignored for file names
20720 that represent full path names''. Thus ignoring dirname in the
20721 `else' branch below isn't an issue. */
c906108c 20722
d5166ae1 20723 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20724 {
43816ebc
TT
20725 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20726 filename = copy.get ();
d521ce57 20727 }
c906108c 20728
c24bdb02 20729 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20730}
20731
804d2729
TT
20732/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20733 buildsym_compunit constructor. */
f4dc4d17 20734
c24bdb02
KS
20735struct compunit_symtab *
20736dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20737 CORE_ADDR low_pc)
f4dc4d17 20738{
c24bdb02 20739 gdb_assert (m_builder == nullptr);
43f3e411 20740
c24bdb02 20741 m_builder.reset (new struct buildsym_compunit
f6e649dd 20742 (this->per_objfile->objfile,
c24bdb02 20743 name, comp_dir, language, low_pc));
93b8bea4 20744
c24bdb02 20745 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20746
c24bdb02
KS
20747 get_builder ()->record_debugformat ("DWARF 2");
20748 get_builder ()->record_producer (producer);
f4dc4d17 20749
c24bdb02 20750 processing_has_namespace_info = false;
43f3e411 20751
c24bdb02 20752 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20753}
20754
4c2df51b
DJ
20755static void
20756var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20757 struct dwarf2_cu *cu)
4c2df51b 20758{
5e22e966 20759 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20760 struct comp_unit_head *cu_header = &cu->header;
20761
4c2df51b
DJ
20762 /* NOTE drow/2003-01-30: There used to be a comment and some special
20763 code here to turn a symbol with DW_AT_external and a
20764 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20765 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20766 with some versions of binutils) where shared libraries could have
20767 relocations against symbols in their debug information - the
20768 minimal symbol would have the right address, but the debug info
20769 would not. It's no longer necessary, because we will explicitly
20770 apply relocations when we read in the debug information now. */
20771
20772 /* A DW_AT_location attribute with no contents indicates that a
20773 variable has been optimized away. */
4fc6c0d5 20774 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20775 {
f1e6e072 20776 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20777 return;
20778 }
20779
20780 /* Handle one degenerate form of location expression specially, to
20781 preserve GDB's previous behavior when section offsets are
336d760d
AT
20782 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20783 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20784
4fc6c0d5 20785 if (attr->form_is_block ()
3019eac3
DE
20786 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20787 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20788 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20789 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20790 && (DW_BLOCK (attr)->size
20791 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20792 {
891d2f0b 20793 unsigned int dummy;
4c2df51b 20794
3019eac3 20795 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20796 SET_SYMBOL_VALUE_ADDRESS
20797 (sym, cu->header.read_address (objfile->obfd,
20798 DW_BLOCK (attr)->data + 1,
20799 &dummy));
3019eac3 20800 else
38583298
TT
20801 SET_SYMBOL_VALUE_ADDRESS
20802 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20803 &dummy));
f1e6e072 20804 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20805 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20806 SET_SYMBOL_VALUE_ADDRESS
20807 (sym,
20808 SYMBOL_VALUE_ADDRESS (sym)
20809 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20810 return;
20811 }
20812
20813 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20814 expression evaluator, and use LOC_COMPUTED only when necessary
20815 (i.e. when the value of a register or memory location is
20816 referenced, or a thread-local block, etc.). Then again, it might
20817 not be worthwhile. I'm assuming that it isn't unless performance
20818 or memory numbers show me otherwise. */
20819
f1e6e072 20820 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20821
f1e6e072 20822 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20823 cu->has_loclist = true;
4c2df51b
DJ
20824}
20825
c906108c
SS
20826/* Given a pointer to a DWARF information entry, figure out if we need
20827 to make a symbol table entry for it, and if so, create a new entry
20828 and return a pointer to it.
20829 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20830 used the passed type.
20831 If SPACE is not NULL, use it to hold the new symbol. If it is
20832 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20833
20834static struct symbol *
5e2db402
TT
20835new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20836 struct symbol *space)
c906108c 20837{
976ca316
SM
20838 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20839 struct objfile *objfile = per_objfile->objfile;
08feed99 20840 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20841 struct symbol *sym = NULL;
15d034d0 20842 const char *name;
c906108c
SS
20843 struct attribute *attr = NULL;
20844 struct attribute *attr2 = NULL;
e142c38c 20845 CORE_ADDR baseaddr;
e37fd15a
SW
20846 struct pending **list_to_add = NULL;
20847
edb3359d 20848 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20849
b3b3bada 20850 baseaddr = objfile->text_section_offset ();
c906108c 20851
94af9270 20852 name = dwarf2_name (die, cu);
c906108c
SS
20853 if (name)
20854 {
34eaf542 20855 int suppress_add = 0;
94af9270 20856
34eaf542
TT
20857 if (space)
20858 sym = space;
20859 else
8c14c3a3 20860 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20861 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20862
20863 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20864 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20865 /* Fortran does not have mangling standard and the mangling does differ
20866 between gfortran, iFort etc. */
bcfe6157
TT
20867 const char *physname
20868 = (cu->language == language_fortran
20869 ? dwarf2_full_name (name, die, cu)
20870 : dwarf2_physname (name, die, cu));
20871 const char *linkagename = dw2_linkage_name (die, cu);
20872
20873 if (linkagename == nullptr || cu->language == language_ada)
20874 sym->set_linkage_name (physname);
20875 else
20876 {
20877 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20878 sym->set_linkage_name (linkagename);
20879 }
f55ee35c 20880
c906108c 20881 /* Default assumptions.
c5aa993b 20882 Use the passed type or decode it from the die. */
176620f1 20883 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20884 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20885 if (type != NULL)
20886 SYMBOL_TYPE (sym) = type;
20887 else
e7c27a73 20888 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20889 attr = dwarf2_attr (die,
20890 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20891 cu);
435d3d88 20892 if (attr != nullptr)
c906108c
SS
20893 {
20894 SYMBOL_LINE (sym) = DW_UNSND (attr);
20895 }
cb1df416 20896
edb3359d
DJ
20897 attr = dwarf2_attr (die,
20898 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20899 cu);
435d3d88 20900 if (attr != nullptr)
cb1df416 20901 {
ecfb656c 20902 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20903 struct file_entry *fe;
9a619af0 20904
ecfb656c
PA
20905 if (cu->line_header != NULL)
20906 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20907 else
20908 fe = NULL;
20909
20910 if (fe == NULL)
b98664d3 20911 complaint (_("file index out of range"));
8c43009f
PA
20912 else
20913 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20914 }
20915
c906108c
SS
20916 switch (die->tag)
20917 {
20918 case DW_TAG_label:
e142c38c 20919 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20920 if (attr != nullptr)
3e29f34a
MR
20921 {
20922 CORE_ADDR addr;
20923
cd6c91b4 20924 addr = attr->value_as_address ();
3e29f34a 20925 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20926 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20927 }
0f5238ed
TT
20928 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20929 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20930 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20931 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20932 break;
20933 case DW_TAG_subprogram:
20934 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20935 finish_block. */
f1e6e072 20936 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20937 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20938 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20939 || cu->language == language_ada
20940 || cu->language == language_fortran)
c906108c 20941 {
2cfa0c8d 20942 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20943 Ada and Fortran subprograms, whether marked external or
20944 not, are always stored as a global symbol, because we want
20945 to be able to access them globally. For instance, we want
20946 to be able to break on a nested subprogram without having
20947 to specify the context. */
c24bdb02 20948 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20949 }
20950 else
20951 {
e37fd15a 20952 list_to_add = cu->list_in_scope;
c906108c
SS
20953 }
20954 break;
edb3359d
DJ
20955 case DW_TAG_inlined_subroutine:
20956 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20957 finish_block. */
f1e6e072 20958 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20959 SYMBOL_INLINED (sym) = 1;
481860b3 20960 list_to_add = cu->list_in_scope;
edb3359d 20961 break;
34eaf542
TT
20962 case DW_TAG_template_value_param:
20963 suppress_add = 1;
20964 /* Fall through. */
72929c62 20965 case DW_TAG_constant:
c906108c 20966 case DW_TAG_variable:
254e6b9e 20967 case DW_TAG_member:
0963b4bd
MS
20968 /* Compilation with minimal debug info may result in
20969 variables with missing type entries. Change the
20970 misleading `void' type to something sensible. */
78134374 20971 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20972 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20973
e142c38c 20974 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20975 /* In the case of DW_TAG_member, we should only be called for
20976 static const members. */
20977 if (die->tag == DW_TAG_member)
20978 {
3863f96c
DE
20979 /* dwarf2_add_field uses die_is_declaration,
20980 so we do the same. */
254e6b9e
DE
20981 gdb_assert (die_is_declaration (die, cu));
20982 gdb_assert (attr);
20983 }
435d3d88 20984 if (attr != nullptr)
c906108c 20985 {
e7c27a73 20986 dwarf2_const_value (attr, sym, cu);
e142c38c 20987 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20988 if (!suppress_add)
34eaf542
TT
20989 {
20990 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20991 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20992 else
e37fd15a 20993 list_to_add = cu->list_in_scope;
34eaf542 20994 }
c906108c
SS
20995 break;
20996 }
e142c38c 20997 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20998 if (attr != nullptr)
c906108c 20999 {
e7c27a73 21000 var_decode_location (attr, sym, cu);
e142c38c 21001 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21002
21003 /* Fortran explicitly imports any global symbols to the local
21004 scope by DW_TAG_common_block. */
21005 if (cu->language == language_fortran && die->parent
21006 && die->parent->tag == DW_TAG_common_block)
21007 attr2 = NULL;
21008
caac4577
JG
21009 if (SYMBOL_CLASS (sym) == LOC_STATIC
21010 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 21011 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
21012 {
21013 /* When a static variable is eliminated by the linker,
21014 the corresponding debug information is not stripped
21015 out, but the variable address is set to null;
21016 do not add such variables into symbol table. */
21017 }
21018 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21019 {
4b610737
TT
21020 if (SYMBOL_CLASS (sym) == LOC_STATIC
21021 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 21022 && per_objfile->per_bfd->can_copy)
4b610737
TT
21023 {
21024 /* A global static variable might be subject to
21025 copy relocation. We first check for a local
21026 minsym, though, because maybe the symbol was
21027 marked hidden, in which case this would not
21028 apply. */
21029 bound_minimal_symbol found
21030 = (lookup_minimal_symbol_linkage
987012b8 21031 (sym->linkage_name (), objfile));
4b610737
TT
21032 if (found.minsym != nullptr)
21033 sym->maybe_copied = 1;
21034 }
f55ee35c 21035
1c809c68
TT
21036 /* A variable with DW_AT_external is never static,
21037 but it may be block-scoped. */
804d2729 21038 list_to_add
c24bdb02
KS
21039 = ((cu->list_in_scope
21040 == cu->get_builder ()->get_file_symbols ())
21041 ? cu->get_builder ()->get_global_symbols ()
804d2729 21042 : cu->list_in_scope);
1c809c68 21043 }
c906108c 21044 else
e37fd15a 21045 list_to_add = cu->list_in_scope;
c906108c
SS
21046 }
21047 else
21048 {
21049 /* We do not know the address of this symbol.
c5aa993b
JM
21050 If it is an external symbol and we have type information
21051 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21052 The address of the variable will then be determined from
21053 the minimal symbol table whenever the variable is
21054 referenced. */
e142c38c 21055 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21056
21057 /* Fortran explicitly imports any global symbols to the local
21058 scope by DW_TAG_common_block. */
21059 if (cu->language == language_fortran && die->parent
21060 && die->parent->tag == DW_TAG_common_block)
21061 {
21062 /* SYMBOL_CLASS doesn't matter here because
21063 read_common_block is going to reset it. */
21064 if (!suppress_add)
21065 list_to_add = cu->list_in_scope;
21066 }
21067 else if (attr2 && (DW_UNSND (attr2) != 0)
21068 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21069 {
0fe7935b
DJ
21070 /* A variable with DW_AT_external is never static, but it
21071 may be block-scoped. */
804d2729 21072 list_to_add
c24bdb02
KS
21073 = ((cu->list_in_scope
21074 == cu->get_builder ()->get_file_symbols ())
21075 ? cu->get_builder ()->get_global_symbols ()
804d2729 21076 : cu->list_in_scope);
0fe7935b 21077
f1e6e072 21078 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21079 }
442ddf59
JK
21080 else if (!die_is_declaration (die, cu))
21081 {
21082 /* Use the default LOC_OPTIMIZED_OUT class. */
21083 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21084 if (!suppress_add)
21085 list_to_add = cu->list_in_scope;
442ddf59 21086 }
c906108c
SS
21087 }
21088 break;
21089 case DW_TAG_formal_parameter:
a60f3166
TT
21090 {
21091 /* If we are inside a function, mark this as an argument. If
21092 not, we might be looking at an argument to an inlined function
21093 when we do not have enough information to show inlined frames;
21094 pretend it's a local variable in that case so that the user can
21095 still see it. */
804d2729 21096 struct context_stack *curr
c24bdb02 21097 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21098 if (curr != nullptr && curr->name != nullptr)
21099 SYMBOL_IS_ARGUMENT (sym) = 1;
21100 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21101 if (attr != nullptr)
a60f3166
TT
21102 {
21103 var_decode_location (attr, sym, cu);
21104 }
21105 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21106 if (attr != nullptr)
a60f3166
TT
21107 {
21108 dwarf2_const_value (attr, sym, cu);
21109 }
f346a30d 21110
a60f3166
TT
21111 list_to_add = cu->list_in_scope;
21112 }
c906108c
SS
21113 break;
21114 case DW_TAG_unspecified_parameters:
21115 /* From varargs functions; gdb doesn't seem to have any
21116 interest in this information, so just ignore it for now.
21117 (FIXME?) */
21118 break;
34eaf542
TT
21119 case DW_TAG_template_type_param:
21120 suppress_add = 1;
21121 /* Fall through. */
c906108c 21122 case DW_TAG_class_type:
680b30c7 21123 case DW_TAG_interface_type:
c906108c
SS
21124 case DW_TAG_structure_type:
21125 case DW_TAG_union_type:
72019c9c 21126 case DW_TAG_set_type:
c906108c 21127 case DW_TAG_enumeration_type:
f1e6e072 21128 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21129 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21130
63d06c5c 21131 {
9c37b5ae 21132 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21133 really ever be static objects: otherwise, if you try
21134 to, say, break of a class's method and you're in a file
21135 which doesn't mention that class, it won't work unless
21136 the check for all static symbols in lookup_symbol_aux
21137 saves you. See the OtherFileClass tests in
21138 gdb.c++/namespace.exp. */
21139
e37fd15a 21140 if (!suppress_add)
34eaf542 21141 {
c24bdb02 21142 buildsym_compunit *builder = cu->get_builder ();
804d2729 21143 list_to_add
c24bdb02 21144 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21145 && cu->language == language_cplus
c24bdb02 21146 ? builder->get_global_symbols ()
804d2729 21147 : cu->list_in_scope);
63d06c5c 21148
64382290 21149 /* The semantics of C++ state that "struct foo {
9c37b5ae 21150 ... }" also defines a typedef for "foo". */
64382290 21151 if (cu->language == language_cplus
45280282 21152 || cu->language == language_ada
c44af4eb
TT
21153 || cu->language == language_d
21154 || cu->language == language_rust)
64382290
TT
21155 {
21156 /* The symbol's name is already allocated along
21157 with this objfile, so we don't need to
21158 duplicate it for the type. */
7d93a1e0 21159 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21160 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21161 }
63d06c5c
DC
21162 }
21163 }
c906108c
SS
21164 break;
21165 case DW_TAG_typedef:
f1e6e072 21166 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21167 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21168 list_to_add = cu->list_in_scope;
63d06c5c 21169 break;
c906108c 21170 case DW_TAG_base_type:
a02abb62 21171 case DW_TAG_subrange_type:
f1e6e072 21172 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21173 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21174 list_to_add = cu->list_in_scope;
c906108c
SS
21175 break;
21176 case DW_TAG_enumerator:
e142c38c 21177 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21178 if (attr != nullptr)
c906108c 21179 {
e7c27a73 21180 dwarf2_const_value (attr, sym, cu);
c906108c 21181 }
63d06c5c
DC
21182 {
21183 /* NOTE: carlton/2003-11-10: See comment above in the
21184 DW_TAG_class_type, etc. block. */
21185
804d2729 21186 list_to_add
c24bdb02 21187 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21188 && cu->language == language_cplus
c24bdb02 21189 ? cu->get_builder ()->get_global_symbols ()
804d2729 21190 : cu->list_in_scope);
63d06c5c 21191 }
c906108c 21192 break;
74921315 21193 case DW_TAG_imported_declaration:
5c4e30ca 21194 case DW_TAG_namespace:
f1e6e072 21195 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21196 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21197 break;
530e8392
KB
21198 case DW_TAG_module:
21199 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21200 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21201 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21202 break;
4357ac6c 21203 case DW_TAG_common_block:
f1e6e072 21204 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21205 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21206 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21207 break;
c906108c
SS
21208 default:
21209 /* Not a tag we recognize. Hopefully we aren't processing
21210 trash data, but since we must specifically ignore things
21211 we don't recognize, there is nothing else we should do at
0963b4bd 21212 this point. */
b98664d3 21213 complaint (_("unsupported tag: '%s'"),
4d3c2250 21214 dwarf_tag_name (die->tag));
c906108c
SS
21215 break;
21216 }
df8a16a1 21217
e37fd15a
SW
21218 if (suppress_add)
21219 {
21220 sym->hash_next = objfile->template_symbols;
21221 objfile->template_symbols = sym;
21222 list_to_add = NULL;
21223 }
21224
21225 if (list_to_add != NULL)
d3cb6808 21226 add_symbol_to_list (sym, list_to_add);
e37fd15a 21227
df8a16a1
DJ
21228 /* For the benefit of old versions of GCC, check for anonymous
21229 namespaces based on the demangled name. */
4d4ec4e5 21230 if (!cu->processing_has_namespace_info
94af9270 21231 && cu->language == language_cplus)
c24bdb02 21232 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21233 }
21234 return (sym);
21235}
21236
98bfdba5
PA
21237/* Given an attr with a DW_FORM_dataN value in host byte order,
21238 zero-extend it as appropriate for the symbol's type. The DWARF
21239 standard (v4) is not entirely clear about the meaning of using
21240 DW_FORM_dataN for a constant with a signed type, where the type is
21241 wider than the data. The conclusion of a discussion on the DWARF
21242 list was that this is unspecified. We choose to always zero-extend
21243 because that is the interpretation long in use by GCC. */
c906108c 21244
98bfdba5 21245static gdb_byte *
ff39bb5e 21246dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21247 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21248{
5e22e966 21249 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21250 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21251 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21252 LONGEST l = DW_UNSND (attr);
21253
21254 if (bits < sizeof (*value) * 8)
21255 {
21256 l &= ((LONGEST) 1 << bits) - 1;
21257 *value = l;
21258 }
21259 else if (bits == sizeof (*value) * 8)
21260 *value = l;
21261 else
21262 {
224c3ddb 21263 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21264 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21265 return bytes;
21266 }
21267
21268 return NULL;
21269}
21270
21271/* Read a constant value from an attribute. Either set *VALUE, or if
21272 the value does not fit in *VALUE, set *BYTES - either already
21273 allocated on the objfile obstack, or newly allocated on OBSTACK,
21274 or, set *BATON, if we translated the constant to a location
21275 expression. */
21276
21277static void
ff39bb5e 21278dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21279 const char *name, struct obstack *obstack,
21280 struct dwarf2_cu *cu,
d521ce57 21281 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21282 struct dwarf2_locexpr_baton **baton)
21283{
5e22e966 21284 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21285 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21286 struct comp_unit_head *cu_header = &cu->header;
c906108c 21287 struct dwarf_block *blk;
98bfdba5
PA
21288 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21289 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21290
21291 *value = 0;
21292 *bytes = NULL;
21293 *baton = NULL;
c906108c
SS
21294
21295 switch (attr->form)
21296 {
21297 case DW_FORM_addr:
336d760d 21298 case DW_FORM_addrx:
3019eac3 21299 case DW_FORM_GNU_addr_index:
ac56253d 21300 {
ac56253d
TT
21301 gdb_byte *data;
21302
98bfdba5
PA
21303 if (TYPE_LENGTH (type) != cu_header->addr_size)
21304 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21305 cu_header->addr_size,
98bfdba5 21306 TYPE_LENGTH (type));
ac56253d
TT
21307 /* Symbols of this form are reasonably rare, so we just
21308 piggyback on the existing location code rather than writing
21309 a new implementation of symbol_computed_ops. */
8d749320 21310 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21311 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21312 (*baton)->per_cu = cu->per_cu;
21313 gdb_assert ((*baton)->per_cu);
ac56253d 21314
98bfdba5 21315 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21316 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21317 (*baton)->data = data;
ac56253d
TT
21318
21319 data[0] = DW_OP_addr;
21320 store_unsigned_integer (&data[1], cu_header->addr_size,
21321 byte_order, DW_ADDR (attr));
21322 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21323 }
c906108c 21324 break;
4ac36638 21325 case DW_FORM_string:
93b5768b 21326 case DW_FORM_strp:
cf532bd1 21327 case DW_FORM_strx:
3019eac3 21328 case DW_FORM_GNU_str_index:
36586728 21329 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21330 /* DW_STRING is already allocated on the objfile obstack, point
21331 directly to it. */
d521ce57 21332 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21333 break;
c906108c
SS
21334 case DW_FORM_block1:
21335 case DW_FORM_block2:
21336 case DW_FORM_block4:
21337 case DW_FORM_block:
2dc7f7b3 21338 case DW_FORM_exprloc:
0224619f 21339 case DW_FORM_data16:
c906108c 21340 blk = DW_BLOCK (attr);
98bfdba5
PA
21341 if (TYPE_LENGTH (type) != blk->size)
21342 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21343 TYPE_LENGTH (type));
21344 *bytes = blk->data;
c906108c 21345 break;
2df3850c
JM
21346
21347 /* The DW_AT_const_value attributes are supposed to carry the
21348 symbol's value "represented as it would be on the target
21349 architecture." By the time we get here, it's already been
21350 converted to host endianness, so we just need to sign- or
21351 zero-extend it as appropriate. */
21352 case DW_FORM_data1:
3aef2284 21353 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21354 break;
c906108c 21355 case DW_FORM_data2:
3aef2284 21356 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21357 break;
c906108c 21358 case DW_FORM_data4:
3aef2284 21359 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21360 break;
c906108c 21361 case DW_FORM_data8:
3aef2284 21362 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21363 break;
21364
c906108c 21365 case DW_FORM_sdata:
663c44ac 21366 case DW_FORM_implicit_const:
98bfdba5 21367 *value = DW_SND (attr);
2df3850c
JM
21368 break;
21369
c906108c 21370 case DW_FORM_udata:
98bfdba5 21371 *value = DW_UNSND (attr);
c906108c 21372 break;
2df3850c 21373
c906108c 21374 default:
b98664d3 21375 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21376 dwarf_form_name (attr->form));
98bfdba5 21377 *value = 0;
c906108c
SS
21378 break;
21379 }
21380}
21381
2df3850c 21382
98bfdba5
PA
21383/* Copy constant value from an attribute to a symbol. */
21384
2df3850c 21385static void
ff39bb5e 21386dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21387 struct dwarf2_cu *cu)
2df3850c 21388{
5e22e966 21389 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21390 LONGEST value;
d521ce57 21391 const gdb_byte *bytes;
98bfdba5 21392 struct dwarf2_locexpr_baton *baton;
2df3850c 21393
98bfdba5 21394 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21395 sym->print_name (),
98bfdba5
PA
21396 &objfile->objfile_obstack, cu,
21397 &value, &bytes, &baton);
2df3850c 21398
98bfdba5
PA
21399 if (baton != NULL)
21400 {
98bfdba5 21401 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21402 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21403 }
21404 else if (bytes != NULL)
21405 {
21406 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21407 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21408 }
21409 else
21410 {
21411 SYMBOL_VALUE (sym) = value;
f1e6e072 21412 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21413 }
2df3850c
JM
21414}
21415
c906108c
SS
21416/* Return the type of the die in question using its DW_AT_type attribute. */
21417
21418static struct type *
e7c27a73 21419die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21420{
c906108c 21421 struct attribute *type_attr;
c906108c 21422
e142c38c 21423 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21424 if (!type_attr)
21425 {
5e22e966 21426 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21427 /* A missing DW_AT_type represents a void type. */
518817b3 21428 return objfile_type (objfile)->builtin_void;
c906108c 21429 }
348e048f 21430
673bfd45 21431 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21432}
21433
b4ba55a1
JB
21434/* True iff CU's producer generates GNAT Ada auxiliary information
21435 that allows to find parallel types through that information instead
21436 of having to do expensive parallel lookups by type name. */
21437
21438static int
21439need_gnat_info (struct dwarf2_cu *cu)
21440{
de4cb04a
JB
21441 /* Assume that the Ada compiler was GNAT, which always produces
21442 the auxiliary information. */
21443 return (cu->language == language_ada);
b4ba55a1
JB
21444}
21445
b4ba55a1
JB
21446/* Return the auxiliary type of the die in question using its
21447 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21448 attribute is not present. */
21449
21450static struct type *
21451die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21452{
b4ba55a1 21453 struct attribute *type_attr;
b4ba55a1
JB
21454
21455 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21456 if (!type_attr)
21457 return NULL;
21458
673bfd45 21459 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21460}
21461
21462/* If DIE has a descriptive_type attribute, then set the TYPE's
21463 descriptive type accordingly. */
21464
21465static void
21466set_descriptive_type (struct type *type, struct die_info *die,
21467 struct dwarf2_cu *cu)
21468{
21469 struct type *descriptive_type = die_descriptive_type (die, cu);
21470
21471 if (descriptive_type)
21472 {
21473 ALLOCATE_GNAT_AUX_TYPE (type);
21474 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21475 }
21476}
21477
c906108c
SS
21478/* Return the containing type of the die in question using its
21479 DW_AT_containing_type attribute. */
21480
21481static struct type *
e7c27a73 21482die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21483{
c906108c 21484 struct attribute *type_attr;
5e22e966 21485 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21486
e142c38c 21487 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21488 if (!type_attr)
21489 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21490 "[in module %s]"), objfile_name (objfile));
33ac96f0 21491
673bfd45 21492 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21493}
21494
ac9ec31b
DE
21495/* Return an error marker type to use for the ill formed type in DIE/CU. */
21496
21497static struct type *
21498build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21499{
976ca316
SM
21500 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21501 struct objfile *objfile = per_objfile->objfile;
528e1572 21502 char *saved;
ac9ec31b 21503
528e1572
SM
21504 std::string message
21505 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21506 objfile_name (objfile),
21507 sect_offset_str (cu->header.sect_off),
21508 sect_offset_str (die->sect_off));
efba19b0 21509 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21510
19f392bc 21511 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21512}
21513
673bfd45 21514/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21515 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21516 DW_AT_containing_type.
673bfd45
DE
21517 If there is no type substitute an error marker. */
21518
c906108c 21519static struct type *
ff39bb5e 21520lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21521 struct dwarf2_cu *cu)
c906108c 21522{
976ca316
SM
21523 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21524 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
21525 struct type *this_type;
21526
ac9ec31b
DE
21527 gdb_assert (attr->name == DW_AT_type
21528 || attr->name == DW_AT_GNAT_descriptive_type
21529 || attr->name == DW_AT_containing_type);
21530
673bfd45
DE
21531 /* First see if we have it cached. */
21532
36586728
TT
21533 if (attr->form == DW_FORM_GNU_ref_alt)
21534 {
21535 struct dwarf2_per_cu_data *per_cu;
0826b30a 21536 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21537
976ca316
SM
21538 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
21539 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 21540 }
cd6c91b4 21541 else if (attr->form_is_ref ())
673bfd45 21542 {
0826b30a 21543 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21544
976ca316 21545 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 21546 }
55f1336d 21547 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21548 {
ac9ec31b 21549 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21550
ac9ec31b 21551 return get_signatured_type (die, signature, cu);
673bfd45
DE
21552 }
21553 else
21554 {
b98664d3 21555 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21556 " at %s [in module %s]"),
21557 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21558 objfile_name (objfile));
ac9ec31b 21559 return build_error_marker_type (cu, die);
673bfd45
DE
21560 }
21561
21562 /* If not cached we need to read it in. */
21563
21564 if (this_type == NULL)
21565 {
ac9ec31b 21566 struct die_info *type_die = NULL;
673bfd45
DE
21567 struct dwarf2_cu *type_cu = cu;
21568
cd6c91b4 21569 if (attr->form_is_ref ())
ac9ec31b
DE
21570 type_die = follow_die_ref (die, attr, &type_cu);
21571 if (type_die == NULL)
21572 return build_error_marker_type (cu, die);
21573 /* If we find the type now, it's probably because the type came
3019eac3
DE
21574 from an inter-CU reference and the type's CU got expanded before
21575 ours. */
ac9ec31b 21576 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21577 }
21578
21579 /* If we still don't have a type use an error marker. */
21580
21581 if (this_type == NULL)
ac9ec31b 21582 return build_error_marker_type (cu, die);
673bfd45 21583
f792889a 21584 return this_type;
c906108c
SS
21585}
21586
673bfd45
DE
21587/* Return the type in DIE, CU.
21588 Returns NULL for invalid types.
21589
02142a6c 21590 This first does a lookup in die_type_hash,
673bfd45
DE
21591 and only reads the die in if necessary.
21592
21593 NOTE: This can be called when reading in partial or full symbols. */
21594
f792889a 21595static struct type *
e7c27a73 21596read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21597{
f792889a
DJ
21598 struct type *this_type;
21599
21600 this_type = get_die_type (die, cu);
21601 if (this_type)
21602 return this_type;
21603
673bfd45
DE
21604 return read_type_die_1 (die, cu);
21605}
21606
21607/* Read the type in DIE, CU.
21608 Returns NULL for invalid types. */
21609
21610static struct type *
21611read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21612{
21613 struct type *this_type = NULL;
21614
c906108c
SS
21615 switch (die->tag)
21616 {
21617 case DW_TAG_class_type:
680b30c7 21618 case DW_TAG_interface_type:
c906108c
SS
21619 case DW_TAG_structure_type:
21620 case DW_TAG_union_type:
f792889a 21621 this_type = read_structure_type (die, cu);
c906108c
SS
21622 break;
21623 case DW_TAG_enumeration_type:
f792889a 21624 this_type = read_enumeration_type (die, cu);
c906108c
SS
21625 break;
21626 case DW_TAG_subprogram:
21627 case DW_TAG_subroutine_type:
edb3359d 21628 case DW_TAG_inlined_subroutine:
f792889a 21629 this_type = read_subroutine_type (die, cu);
c906108c
SS
21630 break;
21631 case DW_TAG_array_type:
f792889a 21632 this_type = read_array_type (die, cu);
c906108c 21633 break;
72019c9c 21634 case DW_TAG_set_type:
f792889a 21635 this_type = read_set_type (die, cu);
72019c9c 21636 break;
c906108c 21637 case DW_TAG_pointer_type:
f792889a 21638 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21639 break;
21640 case DW_TAG_ptr_to_member_type:
f792889a 21641 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21642 break;
21643 case DW_TAG_reference_type:
4297a3f0
AV
21644 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21645 break;
21646 case DW_TAG_rvalue_reference_type:
21647 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21648 break;
21649 case DW_TAG_const_type:
f792889a 21650 this_type = read_tag_const_type (die, cu);
c906108c
SS
21651 break;
21652 case DW_TAG_volatile_type:
f792889a 21653 this_type = read_tag_volatile_type (die, cu);
c906108c 21654 break;
06d66ee9
TT
21655 case DW_TAG_restrict_type:
21656 this_type = read_tag_restrict_type (die, cu);
21657 break;
c906108c 21658 case DW_TAG_string_type:
f792889a 21659 this_type = read_tag_string_type (die, cu);
c906108c
SS
21660 break;
21661 case DW_TAG_typedef:
f792889a 21662 this_type = read_typedef (die, cu);
c906108c 21663 break;
a02abb62 21664 case DW_TAG_subrange_type:
f792889a 21665 this_type = read_subrange_type (die, cu);
a02abb62 21666 break;
c906108c 21667 case DW_TAG_base_type:
f792889a 21668 this_type = read_base_type (die, cu);
c906108c 21669 break;
81a17f79 21670 case DW_TAG_unspecified_type:
f792889a 21671 this_type = read_unspecified_type (die, cu);
81a17f79 21672 break;
0114d602
DJ
21673 case DW_TAG_namespace:
21674 this_type = read_namespace_type (die, cu);
21675 break;
f55ee35c
JK
21676 case DW_TAG_module:
21677 this_type = read_module_type (die, cu);
21678 break;
a2c2acaf
MW
21679 case DW_TAG_atomic_type:
21680 this_type = read_tag_atomic_type (die, cu);
21681 break;
c906108c 21682 default:
b98664d3 21683 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21684 dwarf_tag_name (die->tag));
c906108c
SS
21685 break;
21686 }
63d06c5c 21687
f792889a 21688 return this_type;
63d06c5c
DC
21689}
21690
abc72ce4
DE
21691/* See if we can figure out if the class lives in a namespace. We do
21692 this by looking for a member function; its demangled name will
21693 contain namespace info, if there is any.
21694 Return the computed name or NULL.
21695 Space for the result is allocated on the objfile's obstack.
21696 This is the full-die version of guess_partial_die_structure_name.
21697 In this case we know DIE has no useful parent. */
21698
43816ebc 21699static const char *
abc72ce4
DE
21700guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21701{
21702 struct die_info *spec_die;
21703 struct dwarf2_cu *spec_cu;
21704 struct die_info *child;
5e22e966 21705 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21706
21707 spec_cu = cu;
21708 spec_die = die_specification (die, &spec_cu);
21709 if (spec_die != NULL)
21710 {
21711 die = spec_die;
21712 cu = spec_cu;
21713 }
21714
21715 for (child = die->child;
21716 child != NULL;
21717 child = child->sibling)
21718 {
21719 if (child->tag == DW_TAG_subprogram)
21720 {
73b9be8b 21721 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21722
7d45c7c3 21723 if (linkage_name != NULL)
abc72ce4 21724 {
43816ebc 21725 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 21726 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 21727 const char *name = NULL;
abc72ce4
DE
21728
21729 if (actual_name != NULL)
21730 {
15d034d0 21731 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21732
21733 if (die_name != NULL
43816ebc 21734 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21735 {
21736 /* Strip off the class name from the full name.
21737 We want the prefix. */
21738 int die_name_len = strlen (die_name);
43816ebc
TT
21739 int actual_name_len = strlen (actual_name.get ());
21740 const char *ptr = actual_name.get ();
abc72ce4
DE
21741
21742 /* Test for '::' as a sanity check. */
21743 if (actual_name_len > die_name_len + 2
43816ebc 21744 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21745 name = obstack_strndup (
e3b94546 21746 &objfile->per_bfd->storage_obstack,
43816ebc 21747 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21748 }
21749 }
abc72ce4
DE
21750 return name;
21751 }
21752 }
21753 }
21754
21755 return NULL;
21756}
21757
96408a79
SA
21758/* GCC might emit a nameless typedef that has a linkage name. Determine the
21759 prefix part in such case. See
21760 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21761
a121b7c1 21762static const char *
96408a79
SA
21763anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21764{
21765 struct attribute *attr;
e6a959d6 21766 const char *base;
96408a79
SA
21767
21768 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21769 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21770 return NULL;
21771
7d45c7c3 21772 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21773 return NULL;
21774
73b9be8b 21775 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21776 if (attr == NULL || DW_STRING (attr) == NULL)
21777 return NULL;
21778
21779 /* dwarf2_name had to be already called. */
21780 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21781
21782 /* Strip the base name, keep any leading namespaces/classes. */
21783 base = strrchr (DW_STRING (attr), ':');
21784 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21785 return "";
21786
5e22e966 21787 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21788 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21789 DW_STRING (attr),
21790 &base[-1] - DW_STRING (attr));
96408a79
SA
21791}
21792
fdde2d81 21793/* Return the name of the namespace/class that DIE is defined within,
0114d602 21794 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21795
0114d602
DJ
21796 For example, if we're within the method foo() in the following
21797 code:
21798
21799 namespace N {
21800 class C {
21801 void foo () {
21802 }
21803 };
21804 }
21805
21806 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21807
0d5cff50 21808static const char *
e142c38c 21809determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21810{
976ca316 21811 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
21812 struct die_info *parent, *spec_die;
21813 struct dwarf2_cu *spec_cu;
21814 struct type *parent_type;
a121b7c1 21815 const char *retval;
63d06c5c 21816
9c37b5ae 21817 if (cu->language != language_cplus
c44af4eb
TT
21818 && cu->language != language_fortran && cu->language != language_d
21819 && cu->language != language_rust)
0114d602
DJ
21820 return "";
21821
96408a79
SA
21822 retval = anonymous_struct_prefix (die, cu);
21823 if (retval)
21824 return retval;
21825
0114d602
DJ
21826 /* We have to be careful in the presence of DW_AT_specification.
21827 For example, with GCC 3.4, given the code
21828
21829 namespace N {
21830 void foo() {
21831 // Definition of N::foo.
21832 }
21833 }
21834
21835 then we'll have a tree of DIEs like this:
21836
21837 1: DW_TAG_compile_unit
21838 2: DW_TAG_namespace // N
21839 3: DW_TAG_subprogram // declaration of N::foo
21840 4: DW_TAG_subprogram // definition of N::foo
21841 DW_AT_specification // refers to die #3
21842
21843 Thus, when processing die #4, we have to pretend that we're in
21844 the context of its DW_AT_specification, namely the contex of die
21845 #3. */
21846 spec_cu = cu;
21847 spec_die = die_specification (die, &spec_cu);
21848 if (spec_die == NULL)
21849 parent = die->parent;
21850 else
63d06c5c 21851 {
0114d602
DJ
21852 parent = spec_die->parent;
21853 cu = spec_cu;
63d06c5c 21854 }
0114d602
DJ
21855
21856 if (parent == NULL)
21857 return "";
98bfdba5
PA
21858 else if (parent->building_fullname)
21859 {
21860 const char *name;
21861 const char *parent_name;
21862
21863 /* It has been seen on RealView 2.2 built binaries,
21864 DW_TAG_template_type_param types actually _defined_ as
21865 children of the parent class:
21866
21867 enum E {};
21868 template class <class Enum> Class{};
21869 Class<enum E> class_e;
21870
21871 1: DW_TAG_class_type (Class)
21872 2: DW_TAG_enumeration_type (E)
21873 3: DW_TAG_enumerator (enum1:0)
21874 3: DW_TAG_enumerator (enum2:1)
21875 ...
21876 2: DW_TAG_template_type_param
21877 DW_AT_type DW_FORM_ref_udata (E)
21878
21879 Besides being broken debug info, it can put GDB into an
21880 infinite loop. Consider:
21881
21882 When we're building the full name for Class<E>, we'll start
21883 at Class, and go look over its template type parameters,
21884 finding E. We'll then try to build the full name of E, and
21885 reach here. We're now trying to build the full name of E,
21886 and look over the parent DIE for containing scope. In the
21887 broken case, if we followed the parent DIE of E, we'd again
21888 find Class, and once again go look at its template type
21889 arguments, etc., etc. Simply don't consider such parent die
21890 as source-level parent of this die (it can't be, the language
21891 doesn't allow it), and break the loop here. */
21892 name = dwarf2_name (die, cu);
21893 parent_name = dwarf2_name (parent, cu);
b98664d3 21894 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21895 name ? name : "<unknown>",
21896 parent_name ? parent_name : "<unknown>");
21897 return "";
21898 }
63d06c5c 21899 else
0114d602
DJ
21900 switch (parent->tag)
21901 {
63d06c5c 21902 case DW_TAG_namespace:
0114d602 21903 parent_type = read_type_die (parent, cu);
acebe513
UW
21904 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21905 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21906 Work around this problem here. */
21907 if (cu->language == language_cplus
7d93a1e0 21908 && strcmp (parent_type->name (), "::") == 0)
acebe513 21909 return "";
0114d602 21910 /* We give a name to even anonymous namespaces. */
7d93a1e0 21911 return parent_type->name ();
63d06c5c 21912 case DW_TAG_class_type:
680b30c7 21913 case DW_TAG_interface_type:
63d06c5c 21914 case DW_TAG_structure_type:
0114d602 21915 case DW_TAG_union_type:
f55ee35c 21916 case DW_TAG_module:
0114d602 21917 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21918 if (parent_type->name () != NULL)
21919 return parent_type->name ();
0114d602
DJ
21920 else
21921 /* An anonymous structure is only allowed non-static data
21922 members; no typedefs, no member functions, et cetera.
21923 So it does not need a prefix. */
21924 return "";
abc72ce4 21925 case DW_TAG_compile_unit:
95554aad 21926 case DW_TAG_partial_unit:
abc72ce4
DE
21927 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21928 if (cu->language == language_cplus
976ca316 21929 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21930 && die->child != NULL
21931 && (die->tag == DW_TAG_class_type
21932 || die->tag == DW_TAG_structure_type
21933 || die->tag == DW_TAG_union_type))
21934 {
43816ebc 21935 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21936 if (name != NULL)
21937 return name;
21938 }
21939 return "";
0a4b0913
AB
21940 case DW_TAG_subprogram:
21941 /* Nested subroutines in Fortran get a prefix with the name
21942 of the parent's subroutine. */
21943 if (cu->language == language_fortran)
21944 {
21945 if ((die->tag == DW_TAG_subprogram)
21946 && (dwarf2_name (parent, cu) != NULL))
21947 return dwarf2_name (parent, cu);
21948 }
21949 return determine_prefix (parent, cu);
3d567982
TT
21950 case DW_TAG_enumeration_type:
21951 parent_type = read_type_die (parent, cu);
21952 if (TYPE_DECLARED_CLASS (parent_type))
21953 {
7d93a1e0
SM
21954 if (parent_type->name () != NULL)
21955 return parent_type->name ();
3d567982
TT
21956 return "";
21957 }
21958 /* Fall through. */
63d06c5c 21959 default:
8176b9b8 21960 return determine_prefix (parent, cu);
63d06c5c 21961 }
63d06c5c
DC
21962}
21963
3e43a32a
MS
21964/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21965 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21966 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21967 an obconcat, otherwise allocate storage for the result. The CU argument is
21968 used to determine the language and hence, the appropriate separator. */
987504bb 21969
f55ee35c 21970#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21971
21972static char *
f55ee35c
JK
21973typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21974 int physname, struct dwarf2_cu *cu)
63d06c5c 21975{
f55ee35c 21976 const char *lead = "";
5c315b68 21977 const char *sep;
63d06c5c 21978
3e43a32a
MS
21979 if (suffix == NULL || suffix[0] == '\0'
21980 || prefix == NULL || prefix[0] == '\0')
987504bb 21981 sep = "";
45280282
IB
21982 else if (cu->language == language_d)
21983 {
21984 /* For D, the 'main' function could be defined in any module, but it
21985 should never be prefixed. */
21986 if (strcmp (suffix, "D main") == 0)
21987 {
21988 prefix = "";
21989 sep = "";
21990 }
21991 else
21992 sep = ".";
21993 }
f55ee35c
JK
21994 else if (cu->language == language_fortran && physname)
21995 {
21996 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21997 DW_AT_MIPS_linkage_name is preferred and used instead. */
21998
21999 lead = "__";
22000 sep = "_MOD_";
22001 }
987504bb
JJ
22002 else
22003 sep = "::";
63d06c5c 22004
6dd47d34
DE
22005 if (prefix == NULL)
22006 prefix = "";
22007 if (suffix == NULL)
22008 suffix = "";
22009
987504bb
JJ
22010 if (obs == NULL)
22011 {
3e43a32a 22012 char *retval
224c3ddb
SM
22013 = ((char *)
22014 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22015
f55ee35c
JK
22016 strcpy (retval, lead);
22017 strcat (retval, prefix);
6dd47d34
DE
22018 strcat (retval, sep);
22019 strcat (retval, suffix);
63d06c5c
DC
22020 return retval;
22021 }
987504bb
JJ
22022 else
22023 {
22024 /* We have an obstack. */
f55ee35c 22025 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22026 }
63d06c5c
DC
22027}
22028
71c25dea
TT
22029/* Get name of a die, return NULL if not found. */
22030
15d034d0
TT
22031static const char *
22032dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 22033 struct objfile *objfile)
71c25dea
TT
22034{
22035 if (name && cu->language == language_cplus)
22036 {
596dc4ad
TT
22037 gdb::unique_xmalloc_ptr<char> canon_name
22038 = cp_canonicalize_string (name);
71c25dea 22039
596dc4ad
TT
22040 if (canon_name != nullptr)
22041 name = objfile->intern (canon_name.get ());
71c25dea
TT
22042 }
22043
22044 return name;
c906108c
SS
22045}
22046
96553a0c
DE
22047/* Get name of a die, return NULL if not found.
22048 Anonymous namespaces are converted to their magic string. */
9219021c 22049
15d034d0 22050static const char *
e142c38c 22051dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22052{
22053 struct attribute *attr;
5e22e966 22054 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22055
e142c38c 22056 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22057 if ((!attr || !DW_STRING (attr))
96553a0c 22058 && die->tag != DW_TAG_namespace
53832f31
TT
22059 && die->tag != DW_TAG_class_type
22060 && die->tag != DW_TAG_interface_type
22061 && die->tag != DW_TAG_structure_type
22062 && die->tag != DW_TAG_union_type)
71c25dea
TT
22063 return NULL;
22064
22065 switch (die->tag)
22066 {
22067 case DW_TAG_compile_unit:
95554aad 22068 case DW_TAG_partial_unit:
71c25dea
TT
22069 /* Compilation units have a DW_AT_name that is a filename, not
22070 a source language identifier. */
22071 case DW_TAG_enumeration_type:
22072 case DW_TAG_enumerator:
22073 /* These tags always have simple identifiers already; no need
22074 to canonicalize them. */
22075 return DW_STRING (attr);
907af001 22076
96553a0c
DE
22077 case DW_TAG_namespace:
22078 if (attr != NULL && DW_STRING (attr) != NULL)
22079 return DW_STRING (attr);
22080 return CP_ANONYMOUS_NAMESPACE_STR;
22081
907af001
UW
22082 case DW_TAG_class_type:
22083 case DW_TAG_interface_type:
22084 case DW_TAG_structure_type:
22085 case DW_TAG_union_type:
22086 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22087 structures or unions. These were of the form "._%d" in GCC 4.1,
22088 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22089 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22090 if (attr && DW_STRING (attr)
61012eef
GB
22091 && (startswith (DW_STRING (attr), "._")
22092 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22093 return NULL;
53832f31
TT
22094
22095 /* GCC might emit a nameless typedef that has a linkage name. See
22096 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22097 if (!attr || DW_STRING (attr) == NULL)
22098 {
73b9be8b 22099 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22100 if (attr == NULL || DW_STRING (attr) == NULL)
22101 return NULL;
22102
df5c6c50
JK
22103 /* Avoid demangling DW_STRING (attr) the second time on a second
22104 call for the same DIE. */
22105 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 22106 {
43816ebc
TT
22107 gdb::unique_xmalloc_ptr<char> demangled
22108 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
22109 if (demangled == nullptr)
22110 return nullptr;
43816ebc 22111
be1e3d3e 22112 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 22113 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 22114 }
67430cd0
TT
22115
22116 /* Strip any leading namespaces/classes, keep only the base name.
22117 DW_AT_name for named DIEs does not contain the prefixes. */
22118 const char *base = strrchr (DW_STRING (attr), ':');
22119 if (base && base > DW_STRING (attr) && base[-1] == ':')
22120 return &base[1];
22121 else
22122 return DW_STRING (attr);
53832f31 22123 }
907af001
UW
22124 break;
22125
71c25dea 22126 default:
907af001
UW
22127 break;
22128 }
22129
22130 if (!DW_STRING_IS_CANONICAL (attr))
22131 {
be1e3d3e
TT
22132 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22133 objfile);
907af001 22134 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22135 }
907af001 22136 return DW_STRING (attr);
9219021c
DC
22137}
22138
22139/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22140 is none. *EXT_CU is the CU containing DIE on input, and the CU
22141 containing the return value on output. */
9219021c
DC
22142
22143static struct die_info *
f2f0e013 22144dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22145{
22146 struct attribute *attr;
9219021c 22147
f2f0e013 22148 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22149 if (attr == NULL)
22150 return NULL;
22151
f2f0e013 22152 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22153}
22154
f9aca02d 22155static void
d97bc12b 22156dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22157{
22158 unsigned int i;
22159
d97bc12b 22160 print_spaces (indent, f);
9d8780f0 22161 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22162 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22163 sect_offset_str (die->sect_off));
d97bc12b
DE
22164
22165 if (die->parent != NULL)
22166 {
22167 print_spaces (indent, f);
9d8780f0
SM
22168 fprintf_unfiltered (f, " parent at offset: %s\n",
22169 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22170 }
22171
22172 print_spaces (indent, f);
22173 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22174 dwarf_bool_name (die->child != NULL));
c906108c 22175
d97bc12b
DE
22176 print_spaces (indent, f);
22177 fprintf_unfiltered (f, " attributes:\n");
22178
c906108c
SS
22179 for (i = 0; i < die->num_attrs; ++i)
22180 {
d97bc12b
DE
22181 print_spaces (indent, f);
22182 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22183 dwarf_attr_name (die->attrs[i].name),
22184 dwarf_form_name (die->attrs[i].form));
d97bc12b 22185
c906108c
SS
22186 switch (die->attrs[i].form)
22187 {
c906108c 22188 case DW_FORM_addr:
336d760d 22189 case DW_FORM_addrx:
3019eac3 22190 case DW_FORM_GNU_addr_index:
d97bc12b 22191 fprintf_unfiltered (f, "address: ");
5af949e3 22192 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22193 break;
22194 case DW_FORM_block2:
22195 case DW_FORM_block4:
22196 case DW_FORM_block:
22197 case DW_FORM_block1:
56eb65bd
SP
22198 fprintf_unfiltered (f, "block: size %s",
22199 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22200 break;
2dc7f7b3 22201 case DW_FORM_exprloc:
56eb65bd
SP
22202 fprintf_unfiltered (f, "expression: size %s",
22203 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22204 break;
0224619f
JK
22205 case DW_FORM_data16:
22206 fprintf_unfiltered (f, "constant of 16 bytes");
22207 break;
4568ecf9
DE
22208 case DW_FORM_ref_addr:
22209 fprintf_unfiltered (f, "ref address: ");
22210 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22211 break;
36586728
TT
22212 case DW_FORM_GNU_ref_alt:
22213 fprintf_unfiltered (f, "alt ref address: ");
22214 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22215 break;
10b3939b
DJ
22216 case DW_FORM_ref1:
22217 case DW_FORM_ref2:
22218 case DW_FORM_ref4:
4568ecf9
DE
22219 case DW_FORM_ref8:
22220 case DW_FORM_ref_udata:
d97bc12b 22221 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22222 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22223 break;
c906108c
SS
22224 case DW_FORM_data1:
22225 case DW_FORM_data2:
22226 case DW_FORM_data4:
ce5d95e1 22227 case DW_FORM_data8:
c906108c
SS
22228 case DW_FORM_udata:
22229 case DW_FORM_sdata:
43bbcdc2
PH
22230 fprintf_unfiltered (f, "constant: %s",
22231 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22232 break;
2dc7f7b3
TT
22233 case DW_FORM_sec_offset:
22234 fprintf_unfiltered (f, "section offset: %s",
22235 pulongest (DW_UNSND (&die->attrs[i])));
22236 break;
55f1336d 22237 case DW_FORM_ref_sig8:
ac9ec31b
DE
22238 fprintf_unfiltered (f, "signature: %s",
22239 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22240 break;
c906108c 22241 case DW_FORM_string:
4bdf3d34 22242 case DW_FORM_strp:
43988095 22243 case DW_FORM_line_strp:
cf532bd1 22244 case DW_FORM_strx:
3019eac3 22245 case DW_FORM_GNU_str_index:
36586728 22246 case DW_FORM_GNU_strp_alt:
8285870a 22247 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22248 DW_STRING (&die->attrs[i])
8285870a
JK
22249 ? DW_STRING (&die->attrs[i]) : "",
22250 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22251 break;
22252 case DW_FORM_flag:
22253 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22254 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22255 else
d97bc12b 22256 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22257 break;
2dc7f7b3
TT
22258 case DW_FORM_flag_present:
22259 fprintf_unfiltered (f, "flag: TRUE");
22260 break;
a8329558 22261 case DW_FORM_indirect:
0963b4bd
MS
22262 /* The reader will have reduced the indirect form to
22263 the "base form" so this form should not occur. */
5f48f8f3 22264 fprintf_unfiltered (f,
3e43a32a 22265 "unexpected attribute form: DW_FORM_indirect");
a8329558 22266 break;
663c44ac
JK
22267 case DW_FORM_implicit_const:
22268 fprintf_unfiltered (f, "constant: %s",
22269 plongest (DW_SND (&die->attrs[i])));
22270 break;
c906108c 22271 default:
d97bc12b 22272 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22273 die->attrs[i].form);
d97bc12b 22274 break;
c906108c 22275 }
d97bc12b 22276 fprintf_unfiltered (f, "\n");
c906108c
SS
22277 }
22278}
22279
f9aca02d 22280static void
d97bc12b 22281dump_die_for_error (struct die_info *die)
c906108c 22282{
d97bc12b
DE
22283 dump_die_shallow (gdb_stderr, 0, die);
22284}
22285
22286static void
22287dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22288{
22289 int indent = level * 4;
22290
22291 gdb_assert (die != NULL);
22292
22293 if (level >= max_level)
22294 return;
22295
22296 dump_die_shallow (f, indent, die);
22297
22298 if (die->child != NULL)
c906108c 22299 {
d97bc12b
DE
22300 print_spaces (indent, f);
22301 fprintf_unfiltered (f, " Children:");
22302 if (level + 1 < max_level)
22303 {
22304 fprintf_unfiltered (f, "\n");
22305 dump_die_1 (f, level + 1, max_level, die->child);
22306 }
22307 else
22308 {
3e43a32a
MS
22309 fprintf_unfiltered (f,
22310 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22311 }
22312 }
22313
22314 if (die->sibling != NULL && level > 0)
22315 {
22316 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22317 }
22318}
22319
d97bc12b
DE
22320/* This is called from the pdie macro in gdbinit.in.
22321 It's not static so gcc will keep a copy callable from gdb. */
22322
22323void
22324dump_die (struct die_info *die, int max_level)
22325{
22326 dump_die_1 (gdb_stdlog, 0, max_level, die);
22327}
22328
f9aca02d 22329static void
51545339 22330store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22331{
51545339 22332 void **slot;
c906108c 22333
9c541725
PA
22334 slot = htab_find_slot_with_hash (cu->die_hash, die,
22335 to_underlying (die->sect_off),
b64f50a1 22336 INSERT);
51545339
DJ
22337
22338 *slot = die;
c906108c
SS
22339}
22340
348e048f
DE
22341/* Follow reference or signature attribute ATTR of SRC_DIE.
22342 On entry *REF_CU is the CU of SRC_DIE.
22343 On exit *REF_CU is the CU of the result. */
22344
22345static struct die_info *
ff39bb5e 22346follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22347 struct dwarf2_cu **ref_cu)
22348{
22349 struct die_info *die;
22350
cd6c91b4 22351 if (attr->form_is_ref ())
348e048f 22352 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22353 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22354 die = follow_die_sig (src_die, attr, ref_cu);
22355 else
22356 {
22357 dump_die_for_error (src_die);
22358 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22359 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22360 }
22361
22362 return die;
03dd20cc
DJ
22363}
22364
5c631832 22365/* Follow reference OFFSET.
673bfd45
DE
22366 On entry *REF_CU is the CU of the source die referencing OFFSET.
22367 On exit *REF_CU is the CU of the result.
22368 Returns NULL if OFFSET is invalid. */
f504f079 22369
f9aca02d 22370static struct die_info *
9c541725 22371follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22372 struct dwarf2_cu **ref_cu)
c906108c 22373{
10b3939b 22374 struct die_info temp_die;
f2f0e013 22375 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 22376 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 22377
348e048f
DE
22378 gdb_assert (cu->per_cu != NULL);
22379
98bfdba5
PA
22380 target_cu = cu;
22381
3019eac3 22382 if (cu->per_cu->is_debug_types)
348e048f
DE
22383 {
22384 /* .debug_types CUs cannot reference anything outside their CU.
22385 If they need to, they have to reference a signatured type via
55f1336d 22386 DW_FORM_ref_sig8. */
4057dfde 22387 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22388 return NULL;
348e048f 22389 }
36586728 22390 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22391 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22392 {
22393 struct dwarf2_per_cu_data *per_cu;
9a619af0 22394
9c541725 22395 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 22396 per_objfile);
03dd20cc
DJ
22397
22398 /* If necessary, add it to the queue and load its DIEs. */
976ca316
SM
22399 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22400 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
03dd20cc 22401
976ca316 22402 target_cu = per_objfile->get_cu (per_cu);
10b3939b 22403 }
98bfdba5
PA
22404 else if (cu->dies == NULL)
22405 {
22406 /* We're loading full DIEs during partial symbol reading. */
976ca316
SM
22407 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22408 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
98bfdba5 22409 }
c906108c 22410
f2f0e013 22411 *ref_cu = target_cu;
9c541725 22412 temp_die.sect_off = sect_off;
c24bdb02
KS
22413
22414 if (target_cu != cu)
22415 target_cu->ancestor = cu;
22416
9a3c8263 22417 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22418 &temp_die,
22419 to_underlying (sect_off));
5c631832 22420}
10b3939b 22421
5c631832
JK
22422/* Follow reference attribute ATTR of SRC_DIE.
22423 On entry *REF_CU is the CU of SRC_DIE.
22424 On exit *REF_CU is the CU of the result. */
22425
22426static struct die_info *
ff39bb5e 22427follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22428 struct dwarf2_cu **ref_cu)
22429{
0826b30a 22430 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22431 struct dwarf2_cu *cu = *ref_cu;
22432 struct die_info *die;
22433
9c541725 22434 die = follow_die_offset (sect_off,
36586728
TT
22435 (attr->form == DW_FORM_GNU_ref_alt
22436 || cu->per_cu->is_dwz),
22437 ref_cu);
5c631832 22438 if (!die)
9d8780f0
SM
22439 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22440 "at %s [in module %s]"),
22441 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22442 objfile_name (cu->per_objfile->objfile));
348e048f 22443
5c631832
JK
22444 return die;
22445}
22446
d4c9a4f8 22447/* See read.h. */
5c631832
JK
22448
22449struct dwarf2_locexpr_baton
9c541725 22450dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22451 dwarf2_per_cu_data *per_cu,
976ca316 22452 dwarf2_per_objfile *per_objfile,
8b9737bf 22453 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22454 void *baton, bool resolve_abstract_p)
5c631832 22455{
5c631832
JK
22456 struct die_info *die;
22457 struct attribute *attr;
22458 struct dwarf2_locexpr_baton retval;
976ca316 22459 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 22460
976ca316 22461 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 22462 if (cu == nullptr)
976ca316 22463 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
22464
22465 if (cu == nullptr)
cc12ce38
DE
22466 {
22467 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22468 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22469 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22470 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22471 }
918dd910 22472
9c541725 22473 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22474 if (!die)
9d8780f0
SM
22475 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22476 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22477
22478 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22479 if (!attr && resolve_abstract_p
976ca316
SM
22480 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22481 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22482 {
22483 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22484 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22485 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22486
3360b6e7 22487 for (const auto &cand_off
976ca316 22488 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22489 {
3360b6e7
TV
22490 struct dwarf2_cu *cand_cu = cu;
22491 struct die_info *cand
22492 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22493 if (!cand
22494 || !cand->parent
e4a62c65
TV
22495 || cand->parent->tag != DW_TAG_subprogram)
22496 continue;
22497
22498 CORE_ADDR pc_low, pc_high;
22499 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22500 if (pc_low == ((CORE_ADDR) -1))
22501 continue;
22502 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22503 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22504 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22505 continue;
22506
22507 die = cand;
22508 attr = dwarf2_attr (die, DW_AT_location, cu);
22509 break;
22510 }
22511 }
22512
5c631832
JK
22513 if (!attr)
22514 {
e103e986
JK
22515 /* DWARF: "If there is no such attribute, then there is no effect.".
22516 DATA is ignored if SIZE is 0. */
5c631832 22517
e103e986 22518 retval.data = NULL;
5c631832
JK
22519 retval.size = 0;
22520 }
cd6c91b4 22521 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22522 {
22523 struct dwarf2_loclist_baton loclist_baton;
22524 CORE_ADDR pc = (*get_frame_pc) (baton);
22525 size_t size;
22526
22527 fill_in_loclist_baton (cu, &loclist_baton, attr);
22528
22529 retval.data = dwarf2_find_location_expression (&loclist_baton,
22530 &size, pc);
22531 retval.size = size;
22532 }
5c631832
JK
22533 else
22534 {
4fc6c0d5 22535 if (!attr->form_is_block ())
9d8780f0 22536 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22537 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22538 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22539
22540 retval.data = DW_BLOCK (attr)->data;
22541 retval.size = DW_BLOCK (attr)->size;
22542 }
976ca316 22543 retval.per_objfile = per_objfile;
5c631832 22544 retval.per_cu = cu->per_cu;
918dd910 22545
976ca316 22546 per_objfile->age_comp_units ();
918dd910 22547
5c631832 22548 return retval;
348e048f
DE
22549}
22550
d4c9a4f8 22551/* See read.h. */
8b9737bf
TT
22552
22553struct dwarf2_locexpr_baton
22554dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22555 dwarf2_per_cu_data *per_cu,
14095eb3 22556 dwarf2_per_objfile *per_objfile,
8b9737bf
TT
22557 CORE_ADDR (*get_frame_pc) (void *baton),
22558 void *baton)
22559{
9c541725 22560 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22561
14095eb3
SM
22562 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22563 get_frame_pc, baton);
8b9737bf
TT
22564}
22565
b6807d98
TT
22566/* Write a constant of a given type as target-ordered bytes into
22567 OBSTACK. */
22568
22569static const gdb_byte *
22570write_constant_as_bytes (struct obstack *obstack,
22571 enum bfd_endian byte_order,
22572 struct type *type,
22573 ULONGEST value,
22574 LONGEST *len)
22575{
22576 gdb_byte *result;
22577
22578 *len = TYPE_LENGTH (type);
224c3ddb 22579 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22580 store_unsigned_integer (result, *len, byte_order, value);
22581
22582 return result;
22583}
22584
d4c9a4f8 22585/* See read.h. */
b6807d98
TT
22586
22587const gdb_byte *
9c541725 22588dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 22589 dwarf2_per_cu_data *per_cu,
14095eb3 22590 dwarf2_per_objfile *per_objfile,
d4c9a4f8 22591 obstack *obstack,
b6807d98
TT
22592 LONGEST *len)
22593{
b6807d98
TT
22594 struct die_info *die;
22595 struct attribute *attr;
22596 const gdb_byte *result = NULL;
22597 struct type *type;
22598 LONGEST value;
22599 enum bfd_endian byte_order;
14095eb3 22600 struct objfile *objfile = per_objfile->objfile;
b6807d98 22601
7188ed02 22602 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22603 if (cu == nullptr)
22604 cu = load_cu (per_cu, per_objfile, false);
22605
22606 if (cu == nullptr)
cc12ce38
DE
22607 {
22608 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22609 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22610 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22611 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22612 }
b6807d98 22613
9c541725 22614 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22615 if (!die)
9d8780f0
SM
22616 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22617 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22618
22619 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22620 if (attr == NULL)
22621 return NULL;
22622
e3b94546 22623 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22624 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22625
22626 switch (attr->form)
22627 {
22628 case DW_FORM_addr:
336d760d 22629 case DW_FORM_addrx:
b6807d98
TT
22630 case DW_FORM_GNU_addr_index:
22631 {
22632 gdb_byte *tem;
22633
22634 *len = cu->header.addr_size;
224c3ddb 22635 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22636 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22637 result = tem;
22638 }
22639 break;
22640 case DW_FORM_string:
22641 case DW_FORM_strp:
cf532bd1 22642 case DW_FORM_strx:
b6807d98
TT
22643 case DW_FORM_GNU_str_index:
22644 case DW_FORM_GNU_strp_alt:
22645 /* DW_STRING is already allocated on the objfile obstack, point
22646 directly to it. */
22647 result = (const gdb_byte *) DW_STRING (attr);
22648 *len = strlen (DW_STRING (attr));
22649 break;
22650 case DW_FORM_block1:
22651 case DW_FORM_block2:
22652 case DW_FORM_block4:
22653 case DW_FORM_block:
22654 case DW_FORM_exprloc:
0224619f 22655 case DW_FORM_data16:
b6807d98
TT
22656 result = DW_BLOCK (attr)->data;
22657 *len = DW_BLOCK (attr)->size;
22658 break;
22659
22660 /* The DW_AT_const_value attributes are supposed to carry the
22661 symbol's value "represented as it would be on the target
22662 architecture." By the time we get here, it's already been
22663 converted to host endianness, so we just need to sign- or
22664 zero-extend it as appropriate. */
22665 case DW_FORM_data1:
22666 type = die_type (die, cu);
22667 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22668 if (result == NULL)
22669 result = write_constant_as_bytes (obstack, byte_order,
22670 type, value, len);
22671 break;
22672 case DW_FORM_data2:
22673 type = die_type (die, cu);
22674 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22675 if (result == NULL)
22676 result = write_constant_as_bytes (obstack, byte_order,
22677 type, value, len);
22678 break;
22679 case DW_FORM_data4:
22680 type = die_type (die, cu);
22681 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22682 if (result == NULL)
22683 result = write_constant_as_bytes (obstack, byte_order,
22684 type, value, len);
22685 break;
22686 case DW_FORM_data8:
22687 type = die_type (die, cu);
22688 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22689 if (result == NULL)
22690 result = write_constant_as_bytes (obstack, byte_order,
22691 type, value, len);
22692 break;
22693
22694 case DW_FORM_sdata:
663c44ac 22695 case DW_FORM_implicit_const:
b6807d98
TT
22696 type = die_type (die, cu);
22697 result = write_constant_as_bytes (obstack, byte_order,
22698 type, DW_SND (attr), len);
22699 break;
22700
22701 case DW_FORM_udata:
22702 type = die_type (die, cu);
22703 result = write_constant_as_bytes (obstack, byte_order,
22704 type, DW_UNSND (attr), len);
22705 break;
22706
22707 default:
b98664d3 22708 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22709 dwarf_form_name (attr->form));
22710 break;
22711 }
22712
22713 return result;
22714}
22715
d4c9a4f8 22716/* See read.h. */
7942e96e
AA
22717
22718struct type *
9c541725 22719dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
22720 dwarf2_per_cu_data *per_cu,
22721 dwarf2_per_objfile *per_objfile)
7942e96e 22722{
7942e96e
AA
22723 struct die_info *die;
22724
7188ed02 22725 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22726 if (cu == nullptr)
22727 cu = load_cu (per_cu, per_objfile, false);
22728
22729 if (cu == nullptr)
22730 return nullptr;
7942e96e 22731
9c541725 22732 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22733 if (!die)
22734 return NULL;
22735
22736 return die_type (die, cu);
22737}
22738
8cb5117c 22739/* See read.h. */
8a9b8146
TT
22740
22741struct type *
b64f50a1 22742dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
22743 dwarf2_per_cu_data *per_cu,
22744 dwarf2_per_objfile *per_objfile)
8a9b8146 22745{
9c541725 22746 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 22747 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
22748}
22749
ac9ec31b 22750/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22751 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22752 On exit *REF_CU is the CU of the result.
22753 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22754
22755static struct die_info *
ac9ec31b
DE
22756follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22757 struct dwarf2_cu **ref_cu)
348e048f 22758{
348e048f 22759 struct die_info temp_die;
c24bdb02 22760 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 22761 struct die_info *die;
976ca316 22762 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 22763
348e048f 22764
ac9ec31b
DE
22765 /* While it might be nice to assert sig_type->type == NULL here,
22766 we can get here for DW_AT_imported_declaration where we need
22767 the DIE not the type. */
348e048f
DE
22768
22769 /* If necessary, add it to the queue and load its DIEs. */
22770
976ca316 22771 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
120ce1b5 22772 language_minimal))
976ca316 22773 read_signatured_type (sig_type, per_objfile);
348e048f 22774
976ca316 22775 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 22776 gdb_assert (sig_cu != NULL);
9c541725
PA
22777 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22778 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22779 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22780 to_underlying (temp_die.sect_off));
348e048f
DE
22781 if (die)
22782 {
796a7ff8
DE
22783 /* For .gdb_index version 7 keep track of included TUs.
22784 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
22785 if (per_objfile->per_bfd->index_table != NULL
22786 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22787 {
ae640021 22788 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22789 }
22790
348e048f 22791 *ref_cu = sig_cu;
c24bdb02
KS
22792 if (sig_cu != cu)
22793 sig_cu->ancestor = cu;
22794
348e048f
DE
22795 return die;
22796 }
22797
ac9ec31b
DE
22798 return NULL;
22799}
22800
22801/* Follow signatured type referenced by ATTR in SRC_DIE.
22802 On entry *REF_CU is the CU of SRC_DIE.
22803 On exit *REF_CU is the CU of the result.
22804 The result is the DIE of the type.
22805 If the referenced type cannot be found an error is thrown. */
22806
22807static struct die_info *
ff39bb5e 22808follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22809 struct dwarf2_cu **ref_cu)
22810{
22811 ULONGEST signature = DW_SIGNATURE (attr);
22812 struct signatured_type *sig_type;
22813 struct die_info *die;
22814
22815 gdb_assert (attr->form == DW_FORM_ref_sig8);
22816
a2ce51a0 22817 sig_type = lookup_signatured_type (*ref_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 {
22822 error (_("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 (src_die->sect_off),
5e22e966 22825 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22826 }
22827
22828 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22829 if (die == NULL)
22830 {
22831 dump_die_for_error (src_die);
22832 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22833 " from DIE at %s [in module %s]"),
22834 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22835 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22836 }
22837
22838 return die;
22839}
22840
22841/* Get the type specified by SIGNATURE referenced in DIE/CU,
22842 reading in and processing the type unit if necessary. */
22843
22844static struct type *
22845get_signatured_type (struct die_info *die, ULONGEST signature,
22846 struct dwarf2_cu *cu)
22847{
976ca316 22848 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
22849 struct signatured_type *sig_type;
22850 struct dwarf2_cu *type_cu;
22851 struct die_info *type_die;
22852 struct type *type;
22853
a2ce51a0 22854 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22855 /* sig_type will be NULL if the signatured type is missing from
22856 the debug info. */
22857 if (sig_type == NULL)
22858 {
b98664d3 22859 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22860 " from DIE at %s [in module %s]"),
22861 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22862 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22863 return build_error_marker_type (cu, die);
22864 }
22865
22866 /* If we already know the type we're done. */
976ca316 22867 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
22868 if (type != nullptr)
22869 return type;
ac9ec31b
DE
22870
22871 type_cu = cu;
22872 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22873 if (type_die != NULL)
22874 {
22875 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22876 is created. This is important, for example, because for c++ classes
22877 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22878 type = read_type_die (type_die, type_cu);
22879 if (type == NULL)
22880 {
b98664d3 22881 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22882 " referenced from DIE at %s [in module %s]"),
22883 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22884 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22885 type = build_error_marker_type (cu, die);
22886 }
22887 }
22888 else
22889 {
b98664d3 22890 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22891 " from DIE at %s [in module %s]"),
22892 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22893 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22894 type = build_error_marker_type (cu, die);
22895 }
e286671b 22896
976ca316 22897 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
22898
22899 return type;
22900}
22901
22902/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22903 reading in and processing the type unit if necessary. */
22904
22905static struct type *
ff39bb5e 22906get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22907 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22908{
22909 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22910 if (attr->form_is_ref ())
ac9ec31b
DE
22911 {
22912 struct dwarf2_cu *type_cu = cu;
22913 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22914
22915 return read_type_die (type_die, type_cu);
22916 }
22917 else if (attr->form == DW_FORM_ref_sig8)
22918 {
22919 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22920 }
22921 else
22922 {
976ca316 22923 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 22924
b98664d3 22925 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22926 " at %s [in module %s]"),
22927 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 22928 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22929 return build_error_marker_type (cu, die);
22930 }
348e048f
DE
22931}
22932
e5fe5e75 22933/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22934
22935static void
ab432490
SM
22936load_full_type_unit (dwarf2_per_cu_data *per_cu,
22937 dwarf2_per_objfile *per_objfile)
348e048f 22938{
52dc124a 22939 struct signatured_type *sig_type;
348e048f 22940
f4dc4d17 22941 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22942 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22943
6721b2ec
DE
22944 /* We have the per_cu, but we need the signatured_type.
22945 Fortunately this is an easy translation. */
22946 gdb_assert (per_cu->is_debug_types);
22947 sig_type = (struct signatured_type *) per_cu;
348e048f 22948
7188ed02 22949 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22950
ab432490 22951 read_signatured_type (sig_type, per_objfile);
348e048f 22952
7188ed02 22953 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
22954}
22955
3019eac3
DE
22956/* Read in a signatured type and build its CU and DIEs.
22957 If the type is a stub for the real type in a DWO file,
22958 read in the real type from the DWO file as well. */
dee91e82
DE
22959
22960static void
ab432490
SM
22961read_signatured_type (signatured_type *sig_type,
22962 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22963{
22964 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22965
3019eac3 22966 gdb_assert (per_cu->is_debug_types);
7188ed02 22967 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22968
2e671100 22969 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
22970
22971 if (!reader.dummy_p)
22972 {
22973 struct dwarf2_cu *cu = reader.cu;
22974 const gdb_byte *info_ptr = reader.info_ptr;
22975
22976 gdb_assert (cu->die_hash == NULL);
22977 cu->die_hash =
22978 htab_create_alloc_ex (cu->header.length / 12,
22979 die_hash,
22980 die_eq,
22981 NULL,
22982 &cu->comp_unit_obstack,
22983 hashtab_obstack_allocate,
22984 dummy_obstack_deallocate);
22985
3e225074 22986 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22987 reader.comp_unit_die->child
22988 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22989 reader.comp_unit_die);
22990 cu->dies = reader.comp_unit_die;
22991 /* comp_unit_die is not stored in die_hash, no need. */
22992
22993 /* We try not to read any attributes in this function, because
22994 not all CUs needed for references have been loaded yet, and
22995 symbol table processing isn't initialized. But we have to
22996 set the CU language, or we won't be able to build types
22997 correctly. Similarly, if we do not read the producer, we can
22998 not apply producer-specific interpretation. */
22999 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
23000
23001 reader.keep ();
c0ab21c2
TT
23002 }
23003
7ee85ab1 23004 sig_type->per_cu.tu_read = 1;
c906108c
SS
23005}
23006
c906108c
SS
23007/* Decode simple location descriptions.
23008 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
23009 the location and return the value. If COMPUTED is non-null, it is
23010 set to true to indicate that decoding was successful, and false
23011 otherwise. If COMPUTED is null, then this function may emit a
23012 complaint. */
c906108c
SS
23013
23014static CORE_ADDR
7d79de9a 23015decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 23016{
5e22e966 23017 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
23018 size_t i;
23019 size_t size = blk->size;
d521ce57 23020 const gdb_byte *data = blk->data;
21ae7a4d
JK
23021 CORE_ADDR stack[64];
23022 int stacki;
23023 unsigned int bytes_read, unsnd;
23024 gdb_byte op;
c906108c 23025
7d79de9a
TT
23026 if (computed != nullptr)
23027 *computed = false;
23028
21ae7a4d
JK
23029 i = 0;
23030 stacki = 0;
23031 stack[stacki] = 0;
23032 stack[++stacki] = 0;
23033
23034 while (i < size)
23035 {
23036 op = data[i++];
23037 switch (op)
23038 {
23039 case DW_OP_lit0:
23040 case DW_OP_lit1:
23041 case DW_OP_lit2:
23042 case DW_OP_lit3:
23043 case DW_OP_lit4:
23044 case DW_OP_lit5:
23045 case DW_OP_lit6:
23046 case DW_OP_lit7:
23047 case DW_OP_lit8:
23048 case DW_OP_lit9:
23049 case DW_OP_lit10:
23050 case DW_OP_lit11:
23051 case DW_OP_lit12:
23052 case DW_OP_lit13:
23053 case DW_OP_lit14:
23054 case DW_OP_lit15:
23055 case DW_OP_lit16:
23056 case DW_OP_lit17:
23057 case DW_OP_lit18:
23058 case DW_OP_lit19:
23059 case DW_OP_lit20:
23060 case DW_OP_lit21:
23061 case DW_OP_lit22:
23062 case DW_OP_lit23:
23063 case DW_OP_lit24:
23064 case DW_OP_lit25:
23065 case DW_OP_lit26:
23066 case DW_OP_lit27:
23067 case DW_OP_lit28:
23068 case DW_OP_lit29:
23069 case DW_OP_lit30:
23070 case DW_OP_lit31:
23071 stack[++stacki] = op - DW_OP_lit0;
23072 break;
f1bea926 23073
21ae7a4d
JK
23074 case DW_OP_reg0:
23075 case DW_OP_reg1:
23076 case DW_OP_reg2:
23077 case DW_OP_reg3:
23078 case DW_OP_reg4:
23079 case DW_OP_reg5:
23080 case DW_OP_reg6:
23081 case DW_OP_reg7:
23082 case DW_OP_reg8:
23083 case DW_OP_reg9:
23084 case DW_OP_reg10:
23085 case DW_OP_reg11:
23086 case DW_OP_reg12:
23087 case DW_OP_reg13:
23088 case DW_OP_reg14:
23089 case DW_OP_reg15:
23090 case DW_OP_reg16:
23091 case DW_OP_reg17:
23092 case DW_OP_reg18:
23093 case DW_OP_reg19:
23094 case DW_OP_reg20:
23095 case DW_OP_reg21:
23096 case DW_OP_reg22:
23097 case DW_OP_reg23:
23098 case DW_OP_reg24:
23099 case DW_OP_reg25:
23100 case DW_OP_reg26:
23101 case DW_OP_reg27:
23102 case DW_OP_reg28:
23103 case DW_OP_reg29:
23104 case DW_OP_reg30:
23105 case DW_OP_reg31:
23106 stack[++stacki] = op - DW_OP_reg0;
23107 if (i < size)
7d79de9a
TT
23108 {
23109 if (computed == nullptr)
23110 dwarf2_complex_location_expr_complaint ();
23111 else
23112 return 0;
23113 }
21ae7a4d 23114 break;
c906108c 23115
21ae7a4d
JK
23116 case DW_OP_regx:
23117 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23118 i += bytes_read;
23119 stack[++stacki] = unsnd;
23120 if (i < size)
7d79de9a
TT
23121 {
23122 if (computed == nullptr)
23123 dwarf2_complex_location_expr_complaint ();
23124 else
23125 return 0;
23126 }
21ae7a4d 23127 break;
c906108c 23128
21ae7a4d 23129 case DW_OP_addr:
c8a7a66f
TT
23130 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23131 &bytes_read);
21ae7a4d
JK
23132 i += bytes_read;
23133 break;
d53d4ac5 23134
21ae7a4d
JK
23135 case DW_OP_const1u:
23136 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23137 i += 1;
23138 break;
23139
23140 case DW_OP_const1s:
23141 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23142 i += 1;
23143 break;
23144
23145 case DW_OP_const2u:
23146 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23147 i += 2;
23148 break;
23149
23150 case DW_OP_const2s:
23151 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23152 i += 2;
23153 break;
d53d4ac5 23154
21ae7a4d
JK
23155 case DW_OP_const4u:
23156 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23157 i += 4;
23158 break;
23159
23160 case DW_OP_const4s:
23161 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23162 i += 4;
23163 break;
23164
585861ea
JK
23165 case DW_OP_const8u:
23166 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23167 i += 8;
23168 break;
23169
21ae7a4d
JK
23170 case DW_OP_constu:
23171 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23172 &bytes_read);
23173 i += bytes_read;
23174 break;
23175
23176 case DW_OP_consts:
23177 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23178 i += bytes_read;
23179 break;
23180
23181 case DW_OP_dup:
23182 stack[stacki + 1] = stack[stacki];
23183 stacki++;
23184 break;
23185
23186 case DW_OP_plus:
23187 stack[stacki - 1] += stack[stacki];
23188 stacki--;
23189 break;
23190
23191 case DW_OP_plus_uconst:
23192 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23193 &bytes_read);
23194 i += bytes_read;
23195 break;
23196
23197 case DW_OP_minus:
23198 stack[stacki - 1] -= stack[stacki];
23199 stacki--;
23200 break;
23201
23202 case DW_OP_deref:
23203 /* If we're not the last op, then we definitely can't encode
23204 this using GDB's address_class enum. This is valid for partial
23205 global symbols, although the variable's address will be bogus
23206 in the psymtab. */
23207 if (i < size)
7d79de9a
TT
23208 {
23209 if (computed == nullptr)
23210 dwarf2_complex_location_expr_complaint ();
23211 else
23212 return 0;
23213 }
21ae7a4d
JK
23214 break;
23215
23216 case DW_OP_GNU_push_tls_address:
4aa4e28b 23217 case DW_OP_form_tls_address:
21ae7a4d
JK
23218 /* The top of the stack has the offset from the beginning
23219 of the thread control block at which the variable is located. */
23220 /* Nothing should follow this operator, so the top of stack would
23221 be returned. */
23222 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23223 address will be bogus in the psymtab. Make it always at least
23224 non-zero to not look as a variable garbage collected by linker
23225 which have DW_OP_addr 0. */
21ae7a4d 23226 if (i < size)
7d79de9a
TT
23227 {
23228 if (computed == nullptr)
23229 dwarf2_complex_location_expr_complaint ();
23230 else
23231 return 0;
23232 }
585861ea 23233 stack[stacki]++;
21ae7a4d
JK
23234 break;
23235
23236 case DW_OP_GNU_uninit:
7d79de9a
TT
23237 if (computed != nullptr)
23238 return 0;
21ae7a4d
JK
23239 break;
23240
336d760d 23241 case DW_OP_addrx:
3019eac3 23242 case DW_OP_GNU_addr_index:
49f6c839 23243 case DW_OP_GNU_const_index:
3019eac3
DE
23244 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23245 &bytes_read);
23246 i += bytes_read;
23247 break;
23248
21ae7a4d 23249 default:
7d79de9a
TT
23250 if (computed == nullptr)
23251 {
23252 const char *name = get_DW_OP_name (op);
21ae7a4d 23253
7d79de9a
TT
23254 if (name)
23255 complaint (_("unsupported stack op: '%s'"),
23256 name);
23257 else
23258 complaint (_("unsupported stack op: '%02x'"),
23259 op);
23260 }
21ae7a4d
JK
23261
23262 return (stack[stacki]);
d53d4ac5 23263 }
3c6e0cb3 23264
21ae7a4d
JK
23265 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23266 outside of the allocated space. Also enforce minimum>0. */
23267 if (stacki >= ARRAY_SIZE (stack) - 1)
23268 {
7d79de9a
TT
23269 if (computed == nullptr)
23270 complaint (_("location description stack overflow"));
21ae7a4d
JK
23271 return 0;
23272 }
23273
23274 if (stacki <= 0)
23275 {
7d79de9a
TT
23276 if (computed == nullptr)
23277 complaint (_("location description stack underflow"));
21ae7a4d
JK
23278 return 0;
23279 }
23280 }
7d79de9a
TT
23281
23282 if (computed != nullptr)
23283 *computed = true;
21ae7a4d 23284 return (stack[stacki]);
c906108c
SS
23285}
23286
23287/* memory allocation interface */
23288
c906108c 23289static struct dwarf_block *
7b5a2f43 23290dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23291{
8d749320 23292 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23293}
23294
c906108c 23295static struct die_info *
b60c80d6 23296dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23297{
23298 struct die_info *die;
b60c80d6
DJ
23299 size_t size = sizeof (struct die_info);
23300
23301 if (num_attrs > 1)
23302 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23303
b60c80d6 23304 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23305 memset (die, 0, sizeof (struct die_info));
23306 return (die);
23307}
2e276125
JB
23308
23309\f
a036ba48 23310
c90ec28a 23311/* Macro support. */
cf2c3c16 23312
9eac9650
TT
23313/* An overload of dwarf_decode_macros that finds the correct section
23314 and ensures it is read in before calling the other overload. */
23315
23316static void
23317dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23318 int section_is_gnu)
23319{
976ca316
SM
23320 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23321 struct objfile *objfile = per_objfile->objfile;
5a0e026f 23322 const struct line_header *lh = cu->line_header;
9eac9650
TT
23323 unsigned int offset_size = cu->header.offset_size;
23324 struct dwarf2_section_info *section;
23325 const char *section_name;
23326
23327 if (cu->dwo_unit != nullptr)
23328 {
23329 if (section_is_gnu)
23330 {
23331 section = &cu->dwo_unit->dwo_file->sections.macro;
23332 section_name = ".debug_macro.dwo";
23333 }
23334 else
23335 {
23336 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23337 section_name = ".debug_macinfo.dwo";
23338 }
23339 }
23340 else
23341 {
23342 if (section_is_gnu)
23343 {
976ca316 23344 section = &per_objfile->per_bfd->macro;
9eac9650
TT
23345 section_name = ".debug_macro";
23346 }
23347 else
23348 {
976ca316 23349 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
23350 section_name = ".debug_macinfo";
23351 }
23352 }
23353
23354 section->read (objfile);
23355 if (section->buffer == nullptr)
23356 {
23357 complaint (_("missing %s section"), section_name);
23358 return;
23359 }
23360
23361 buildsym_compunit *builder = cu->get_builder ();
23362
976ca316 23363 dwarf_decode_macros (per_objfile, builder, section, lh,
9eac9650
TT
23364 offset_size, offset, section_is_gnu);
23365}
23366
3019eac3
DE
23367/* Return the .debug_loc section to use for CU.
23368 For DWO files use .debug_loc.dwo. */
23369
23370static struct dwarf2_section_info *
23371cu_debug_loc_section (struct dwarf2_cu *cu)
23372{
976ca316 23373 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23374
3019eac3 23375 if (cu->dwo_unit)
43988095
JK
23376 {
23377 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23378
43988095
JK
23379 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23380 }
976ca316
SM
23381 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23382 : &per_objfile->per_bfd->loc);
3019eac3
DE
23383}
23384
8cf6f0b1
TT
23385/* A helper function that fills in a dwarf2_loclist_baton. */
23386
23387static void
23388fill_in_loclist_baton (struct dwarf2_cu *cu,
23389 struct dwarf2_loclist_baton *baton,
ff39bb5e 23390 const struct attribute *attr)
8cf6f0b1 23391{
976ca316 23392 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
23393 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23394
976ca316 23395 section->read (per_objfile->objfile);
8cf6f0b1 23396
976ca316 23397 baton->per_objfile = per_objfile;
8cf6f0b1
TT
23398 baton->per_cu = cu->per_cu;
23399 gdb_assert (baton->per_cu);
23400 /* We don't know how long the location list is, but make sure we
23401 don't run off the edge of the section. */
3019eac3
DE
23402 baton->size = section->size - DW_UNSND (attr);
23403 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23404 if (cu->base_address.has_value ())
23405 baton->base_address = *cu->base_address;
23406 else
23407 baton->base_address = 0;
f664829e 23408 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23409}
23410
4c2df51b 23411static void
ff39bb5e 23412dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23413 struct dwarf2_cu *cu, int is_block)
4c2df51b 23414{
976ca316
SM
23415 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23416 struct objfile *objfile = per_objfile->objfile;
3019eac3 23417 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23418
cd6c91b4 23419 if (attr->form_is_section_offset ()
3019eac3 23420 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23421 the section. If so, fall through to the complaint in the
23422 other branch. */
2c7d5afc 23423 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23424 {
0d53c4c4 23425 struct dwarf2_loclist_baton *baton;
4c2df51b 23426
8d749320 23427 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23428
8cf6f0b1 23429 fill_in_loclist_baton (cu, baton, attr);
be391dca 23430
2b24b6e4 23431 if (!cu->base_address.has_value ())
b98664d3 23432 complaint (_("Location list used without "
3e43a32a 23433 "specifying the CU base address."));
4c2df51b 23434
f1e6e072
TT
23435 SYMBOL_ACLASS_INDEX (sym) = (is_block
23436 ? dwarf2_loclist_block_index
23437 : dwarf2_loclist_index);
0d53c4c4
DJ
23438 SYMBOL_LOCATION_BATON (sym) = baton;
23439 }
23440 else
23441 {
23442 struct dwarf2_locexpr_baton *baton;
23443
8d749320 23444 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 23445 baton->per_objfile = per_objfile;
ae0d2f24
UW
23446 baton->per_cu = cu->per_cu;
23447 gdb_assert (baton->per_cu);
0d53c4c4 23448
4fc6c0d5 23449 if (attr->form_is_block ())
0d53c4c4
DJ
23450 {
23451 /* Note that we're just copying the block's data pointer
23452 here, not the actual data. We're still pointing into the
6502dd73
DJ
23453 info_buffer for SYM's objfile; right now we never release
23454 that buffer, but when we do clean up properly this may
23455 need to change. */
0d53c4c4
DJ
23456 baton->size = DW_BLOCK (attr)->size;
23457 baton->data = DW_BLOCK (attr)->data;
23458 }
23459 else
23460 {
23461 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23462 sym->natural_name ());
0d53c4c4 23463 baton->size = 0;
0d53c4c4 23464 }
6e70227d 23465
f1e6e072
TT
23466 SYMBOL_ACLASS_INDEX (sym) = (is_block
23467 ? dwarf2_locexpr_block_index
23468 : dwarf2_locexpr_index);
0d53c4c4
DJ
23469 SYMBOL_LOCATION_BATON (sym) = baton;
23470 }
4c2df51b 23471}
6502dd73 23472
2e6a9f79 23473/* See read.h. */
96408a79 23474
2e6a9f79
SM
23475const comp_unit_head *
23476dwarf2_per_cu_data::get_header () const
96408a79 23477{
2e6a9f79
SM
23478 if (!m_header_read_in)
23479 {
23480 const gdb_byte *info_ptr
23481 = this->section->buffer + to_underlying (this->sect_off);
96408a79 23482
2e6a9f79 23483 memset (&m_header, 0, sizeof (m_header));
96408a79 23484
2e6a9f79
SM
23485 read_comp_unit_head (&m_header, info_ptr, this->section,
23486 rcuh_kind::COMPILE);
23487 }
96408a79 23488
2e6a9f79 23489 return &m_header;
96408a79
SA
23490}
23491
09ba997f 23492/* See read.h. */
ae0d2f24 23493
98714339 23494int
09ba997f 23495dwarf2_per_cu_data::addr_size () const
ae0d2f24 23496{
2e6a9f79 23497 return this->get_header ()->addr_size;
ae0d2f24
UW
23498}
23499
09ba997f 23500/* See read.h. */
9eae7c52
TT
23501
23502int
09ba997f 23503dwarf2_per_cu_data::offset_size () const
9eae7c52 23504{
2e6a9f79 23505 return this->get_header ()->offset_size;
96408a79
SA
23506}
23507
09ba997f 23508/* See read.h. */
96408a79
SA
23509
23510int
09ba997f 23511dwarf2_per_cu_data::ref_addr_size () const
96408a79 23512{
2e6a9f79 23513 const comp_unit_head *header = this->get_header ();
96408a79 23514
2e6a9f79
SM
23515 if (header->version == 2)
23516 return header->addr_size;
96408a79 23517 else
2e6a9f79 23518 return header->offset_size;
181cebd4
JK
23519}
23520
09ba997f 23521/* See read.h. */
9aa1f1e3 23522
09ba997f 23523struct type *
293e7e51 23524dwarf2_cu::addr_type () const
9a49df9d 23525{
293e7e51 23526 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23527 struct type *void_type = objfile_type (objfile)->builtin_void;
23528 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23529 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23530
23531 if (TYPE_LENGTH (addr_type) == addr_size)
23532 return addr_type;
23533
09ba997f 23534 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23535 return addr_type;
23536}
23537
22b6cd70
TT
23538/* A helper function for dwarf2_find_containing_comp_unit that returns
23539 the index of the result, and that searches a vector. It will
23540 return a result even if the offset in question does not actually
23541 occur in any CU. This is separate so that it can be unit
23542 tested. */
ae038cb0 23543
22b6cd70
TT
23544static int
23545dwarf2_find_containing_comp_unit
23546 (sect_offset sect_off,
23547 unsigned int offset_in_dwz,
23548 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23549{
ae038cb0
DJ
23550 int low, high;
23551
ae038cb0 23552 low = 0;
22b6cd70 23553 high = all_comp_units.size () - 1;
ae038cb0
DJ
23554 while (high > low)
23555 {
36586728 23556 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23557 int mid = low + (high - low) / 2;
9a619af0 23558
22b6cd70 23559 mid_cu = all_comp_units[mid];
36586728 23560 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23561 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23562 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23563 high = mid;
23564 else
23565 low = mid + 1;
23566 }
23567 gdb_assert (low == high);
22b6cd70
TT
23568 return low;
23569}
23570
23571/* Locate the .debug_info compilation unit from CU's objfile which contains
23572 the DIE at OFFSET. Raises an error on failure. */
23573
23574static struct dwarf2_per_cu_data *
23575dwarf2_find_containing_comp_unit (sect_offset sect_off,
23576 unsigned int offset_in_dwz,
976ca316 23577 dwarf2_per_objfile *per_objfile)
22b6cd70 23578{
976ca316
SM
23579 int low = dwarf2_find_containing_comp_unit
23580 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
23581 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23582
45b8ae0c 23583 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23584 {
36586728 23585 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23586 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23587 "offset %s [in module %s]"),
23588 sect_offset_str (sect_off),
976ca316 23589 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 23590
976ca316 23591 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23592 <= sect_off);
976ca316 23593 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23594 }
23595 else
23596 {
976ca316 23597 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23598 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23599 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23600 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23601 return this_cu;
23602 }
23603}
23604
22b6cd70
TT
23605#if GDB_SELF_TEST
23606
23607namespace selftests {
23608namespace find_containing_comp_unit {
23609
23610static void
23611run_test ()
23612{
23613 struct dwarf2_per_cu_data one {};
23614 struct dwarf2_per_cu_data two {};
23615 struct dwarf2_per_cu_data three {};
23616 struct dwarf2_per_cu_data four {};
23617
23618 one.length = 5;
23619 two.sect_off = sect_offset (one.length);
23620 two.length = 7;
23621
23622 three.length = 5;
23623 three.is_dwz = 1;
23624 four.sect_off = sect_offset (three.length);
23625 four.length = 7;
23626 four.is_dwz = 1;
23627
23628 std::vector<dwarf2_per_cu_data *> units;
23629 units.push_back (&one);
23630 units.push_back (&two);
23631 units.push_back (&three);
23632 units.push_back (&four);
23633
23634 int result;
23635
23636 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23637 SELF_CHECK (units[result] == &one);
23638 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23639 SELF_CHECK (units[result] == &one);
23640 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23641 SELF_CHECK (units[result] == &two);
23642
23643 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23644 SELF_CHECK (units[result] == &three);
23645 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23646 SELF_CHECK (units[result] == &three);
23647 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23648 SELF_CHECK (units[result] == &four);
23649}
23650
23651}
23652}
23653
23654#endif /* GDB_SELF_TEST */
23655
9e021579 23656/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23657
9e021579
SM
23658dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23659 dwarf2_per_objfile *per_objfile)
23660 : per_cu (per_cu),
23661 per_objfile (per_objfile),
9068261f
AB
23662 mark (false),
23663 has_loclist (false),
23664 checked_producer (false),
23665 producer_is_gxx_lt_4_6 (false),
23666 producer_is_gcc_lt_4_3 (false),
eb77c9df 23667 producer_is_icc (false),
9068261f 23668 producer_is_icc_lt_14 (false),
c258c396 23669 producer_is_codewarrior (false),
9068261f 23670 processing_has_namespace_info (false)
93311388 23671{
9816fde3
JK
23672}
23673
23674/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23675
23676static void
95554aad
TT
23677prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23678 enum language pretend_language)
9816fde3
JK
23679{
23680 struct attribute *attr;
23681
23682 /* Set the language we're debugging. */
23683 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23684 if (attr != nullptr)
9816fde3
JK
23685 set_cu_language (DW_UNSND (attr), cu);
23686 else
9cded63f 23687 {
95554aad 23688 cu->language = pretend_language;
9cded63f
TT
23689 cu->language_defn = language_def (cu->language);
23690 }
dee91e82 23691
7d45c7c3 23692 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23693}
23694
7188ed02 23695/* See read.h. */
ae038cb0 23696
7188ed02
SM
23697dwarf2_cu *
23698dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23699{
7188ed02
SM
23700 auto it = m_dwarf2_cus.find (per_cu);
23701 if (it == m_dwarf2_cus.end ())
23702 return nullptr;
ae038cb0 23703
7188ed02
SM
23704 return it->second;
23705}
23706
23707/* See read.h. */
23708
23709void
23710dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23711{
23712 gdb_assert (this->get_cu (per_cu) == nullptr);
23713
23714 m_dwarf2_cus[per_cu] = cu;
23715}
23716
23717/* See read.h. */
23718
23719void
23720dwarf2_per_objfile::age_comp_units ()
23721{
23722 /* Start by clearing all marks. */
23723 for (auto pair : m_dwarf2_cus)
23724 pair.second->mark = false;
23725
23726 /* Traverse all CUs, mark them and their dependencies if used recently
23727 enough. */
23728 for (auto pair : m_dwarf2_cus)
ae038cb0 23729 {
7188ed02
SM
23730 dwarf2_cu *cu = pair.second;
23731
23732 cu->last_used++;
23733 if (cu->last_used <= dwarf_max_cache_age)
23734 dwarf2_mark (cu);
ae038cb0
DJ
23735 }
23736
7188ed02
SM
23737 /* Delete all CUs still not marked. */
23738 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 23739 {
7188ed02 23740 dwarf2_cu *cu = it->second;
ae038cb0 23741
7188ed02 23742 if (!cu->mark)
ae038cb0 23743 {
7188ed02
SM
23744 delete cu;
23745 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
23746 }
23747 else
7188ed02 23748 it++;
ae038cb0
DJ
23749 }
23750}
23751
7188ed02 23752/* See read.h. */
ae038cb0 23753
7188ed02
SM
23754void
23755dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23756{
7188ed02
SM
23757 auto it = m_dwarf2_cus.find (per_cu);
23758 if (it == m_dwarf2_cus.end ())
23759 return;
ae038cb0 23760
7188ed02 23761 delete it->second;
ae038cb0 23762
7188ed02
SM
23763 m_dwarf2_cus.erase (it);
23764}
ae038cb0 23765
7188ed02
SM
23766dwarf2_per_objfile::~dwarf2_per_objfile ()
23767{
23768 remove_all_cus ();
ae038cb0
DJ
23769}
23770
dee91e82
DE
23771/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23772 We store these in a hash table separate from the DIEs, and preserve them
23773 when the DIEs are flushed out of cache.
23774
23775 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23776 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23777 or the type may come from a DWO file. Furthermore, while it's more logical
23778 to use per_cu->section+offset, with Fission the section with the data is in
23779 the DWO file but we don't know that section at the point we need it.
23780 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23781 because we can enter the lookup routine, get_die_type_at_offset, from
23782 outside this file, and thus won't necessarily have PER_CU->cu.
23783 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23784
dee91e82 23785struct dwarf2_per_cu_offset_and_type
1c379e20 23786{
dee91e82 23787 const struct dwarf2_per_cu_data *per_cu;
9c541725 23788 sect_offset sect_off;
1c379e20
DJ
23789 struct type *type;
23790};
23791
dee91e82 23792/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23793
23794static hashval_t
dee91e82 23795per_cu_offset_and_type_hash (const void *item)
1c379e20 23796{
9a3c8263
SM
23797 const struct dwarf2_per_cu_offset_and_type *ofs
23798 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23799
9c541725 23800 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23801}
23802
dee91e82 23803/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23804
23805static int
dee91e82 23806per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23807{
9a3c8263
SM
23808 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23809 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23810 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23811 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23812
dee91e82 23813 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23814 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23815}
23816
23817/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23818 table if necessary. For convenience, return TYPE.
23819
23820 The DIEs reading must have careful ordering to:
85102364 23821 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23822 reading current DIE.
23823 * Not trying to dereference contents of still incompletely read in types
23824 while reading in other DIEs.
23825 * Enable referencing still incompletely read in types just by a pointer to
23826 the type without accessing its fields.
23827
23828 Therefore caller should follow these rules:
23829 * Try to fetch any prerequisite types we may need to build this DIE type
23830 before building the type and calling set_die_type.
e71ec853 23831 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23832 possible before fetching more types to complete the current type.
23833 * Make the type as complete as possible before fetching more types. */
1c379e20 23834
f792889a 23835static struct type *
1c379e20
DJ
23836set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23837{
976ca316 23838 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 23839 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 23840 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
23841 struct attribute *attr;
23842 struct dynamic_prop prop;
1c379e20 23843
b4ba55a1
JB
23844 /* For Ada types, make sure that the gnat-specific data is always
23845 initialized (if not already set). There are a few types where
23846 we should not be doing so, because the type-specific area is
23847 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23848 where the type-specific area is used to store the floatformat).
23849 But this is not a problem, because the gnat-specific information
23850 is actually not needed for these types. */
23851 if (need_gnat_info (cu)
78134374
SM
23852 && type->code () != TYPE_CODE_FUNC
23853 && type->code () != TYPE_CODE_FLT
23854 && type->code () != TYPE_CODE_METHODPTR
23855 && type->code () != TYPE_CODE_MEMBERPTR
23856 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23857 && !HAVE_GNAT_AUX_INFO (type))
23858 INIT_GNAT_SPECIFIC (type);
23859
3f2f83dd
KB
23860 /* Read DW_AT_allocated and set in type. */
23861 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 23862 if (attr != NULL)
3f2f83dd 23863 {
293e7e51 23864 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23865 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23866 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 23867 }
3f2f83dd
KB
23868
23869 /* Read DW_AT_associated and set in type. */
23870 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 23871 if (attr != NULL)
3f2f83dd 23872 {
293e7e51 23873 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23874 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23875 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 23876 }
3f2f83dd 23877
3cdcd0ce
JB
23878 /* Read DW_AT_data_location and set in type. */
23879 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23880 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23881 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23882
976ca316
SM
23883 if (per_objfile->die_type_hash == NULL)
23884 per_objfile->die_type_hash
0335378b
TT
23885 = htab_up (htab_create_alloc (127,
23886 per_cu_offset_and_type_hash,
23887 per_cu_offset_and_type_eq,
23888 NULL, xcalloc, xfree));
1c379e20 23889
dee91e82 23890 ofs.per_cu = cu->per_cu;
9c541725 23891 ofs.sect_off = die->sect_off;
1c379e20 23892 ofs.type = type;
dee91e82 23893 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 23894 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23895 if (*slot)
b98664d3 23896 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23897 sect_offset_str (die->sect_off));
8d749320
SM
23898 *slot = XOBNEW (&objfile->objfile_obstack,
23899 struct dwarf2_per_cu_offset_and_type);
1c379e20 23900 **slot = ofs;
f792889a 23901 return type;
1c379e20
DJ
23902}
23903
9c541725 23904/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23905 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23906
23907static struct type *
9c541725 23908get_die_type_at_offset (sect_offset sect_off,
aa66c379 23909 dwarf2_per_cu_data *per_cu,
976ca316 23910 dwarf2_per_objfile *per_objfile)
1c379e20 23911{
dee91e82 23912 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23913
976ca316 23914 if (per_objfile->die_type_hash == NULL)
f792889a 23915 return NULL;
1c379e20 23916
dee91e82 23917 ofs.per_cu = per_cu;
9c541725 23918 ofs.sect_off = sect_off;
9a3c8263 23919 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 23920 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23921 if (slot)
23922 return slot->type;
23923 else
23924 return NULL;
23925}
23926
02142a6c 23927/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23928 or return NULL if DIE does not have a saved type. */
23929
23930static struct type *
23931get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23932{
aa66c379 23933 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
23934}
23935
10b3939b
DJ
23936/* Add a dependence relationship from CU to REF_PER_CU. */
23937
23938static void
23939dwarf2_add_dependence (struct dwarf2_cu *cu,
23940 struct dwarf2_per_cu_data *ref_per_cu)
23941{
23942 void **slot;
23943
23944 if (cu->dependencies == NULL)
23945 cu->dependencies
23946 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23947 NULL, &cu->comp_unit_obstack,
23948 hashtab_obstack_allocate,
23949 dummy_obstack_deallocate);
23950
23951 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23952 if (*slot == NULL)
23953 *slot = ref_per_cu;
23954}
1c379e20 23955
f504f079
DE
23956/* Subroutine of dwarf2_mark to pass to htab_traverse.
23957 Set the mark field in every compilation unit in the
7188ed02
SM
23958 cache that we must keep because we are keeping CU.
23959
23960 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 23961
10b3939b
DJ
23962static int
23963dwarf2_mark_helper (void **slot, void *data)
23964{
7188ed02
SM
23965 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23966 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23967 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
23968
23969 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23970 reading of the chain. As such dependencies remain valid it is not much
23971 useful to track and undo them during QUIT cleanups. */
7188ed02 23972 if (cu == nullptr)
d07ed419
JK
23973 return 1;
23974
7188ed02 23975 if (cu->mark)
10b3939b 23976 return 1;
10b3939b 23977
7188ed02
SM
23978 cu->mark = true;
23979
23980 if (cu->dependencies != nullptr)
23981 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
23982
23983 return 1;
23984}
23985
f504f079
DE
23986/* Set the mark field in CU and in every other compilation unit in the
23987 cache that we must keep because we are keeping CU. */
23988
ae038cb0
DJ
23989static void
23990dwarf2_mark (struct dwarf2_cu *cu)
23991{
23992 if (cu->mark)
23993 return;
7188ed02 23994
9068261f 23995 cu->mark = true;
ae038cb0 23996
7188ed02
SM
23997 if (cu->dependencies != nullptr)
23998 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
23999}
24000
72bf9492
DJ
24001/* Trivial hash function for partial_die_info: the hash value of a DIE
24002 is its offset in .debug_info for this objfile. */
24003
24004static hashval_t
24005partial_die_hash (const void *item)
24006{
9a3c8263
SM
24007 const struct partial_die_info *part_die
24008 = (const struct partial_die_info *) item;
9a619af0 24009
9c541725 24010 return to_underlying (part_die->sect_off);
72bf9492
DJ
24011}
24012
24013/* Trivial comparison function for partial_die_info structures: two DIEs
24014 are equal if they have the same offset. */
24015
24016static int
24017partial_die_eq (const void *item_lhs, const void *item_rhs)
24018{
9a3c8263
SM
24019 const struct partial_die_info *part_die_lhs
24020 = (const struct partial_die_info *) item_lhs;
24021 const struct partial_die_info *part_die_rhs
24022 = (const struct partial_die_info *) item_rhs;
9a619af0 24023
9c541725 24024 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24025}
24026
3c3bb058
AB
24027struct cmd_list_element *set_dwarf_cmdlist;
24028struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 24029
9291a0cd 24030static void
cd4fb1b2
SM
24031show_check_physname (struct ui_file *file, int from_tty,
24032 struct cmd_list_element *c, const char *value)
9291a0cd 24033{
cd4fb1b2
SM
24034 fprintf_filtered (file,
24035 _("Whether to check \"physname\" is %s.\n"),
24036 value);
9291a0cd
TT
24037}
24038
6c265988 24039void _initialize_dwarf2_read ();
cd4fb1b2 24040void
6c265988 24041_initialize_dwarf2_read ()
9291a0cd 24042{
0743fc83 24043 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24044Set DWARF specific variables.\n\
590042fc 24045Configure DWARF variables such as the cache size."),
0743fc83
TT
24046 &set_dwarf_cmdlist, "maintenance set dwarf ",
24047 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24048
0743fc83 24049 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24050Show DWARF specific variables.\n\
24051Show DWARF variables such as the cache size."),
0743fc83
TT
24052 &show_dwarf_cmdlist, "maintenance show dwarf ",
24053 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24054
cd4fb1b2
SM
24055 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24056 &dwarf_max_cache_age, _("\
24057Set the upper bound on the age of cached DWARF compilation units."), _("\
24058Show the upper bound on the age of cached DWARF compilation units."), _("\
24059A higher limit means that cached compilation units will be stored\n\
24060in memory longer, and more total memory will be used. Zero disables\n\
24061caching, which can slow down startup."),
24062 NULL,
24063 show_dwarf_max_cache_age,
24064 &set_dwarf_cmdlist,
24065 &show_dwarf_cmdlist);
156942c7 24066
cd4fb1b2
SM
24067 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24068Set debugging of the DWARF reader."), _("\
24069Show debugging of the DWARF reader."), _("\
24070When enabled (non-zero), debugging messages are printed during DWARF\n\
24071reading and symtab expansion. A value of 1 (one) provides basic\n\
24072information. A value greater than 1 provides more verbose information."),
24073 NULL,
24074 NULL,
24075 &setdebuglist, &showdebuglist);
9291a0cd 24076
cd4fb1b2
SM
24077 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24078Set debugging of the DWARF DIE reader."), _("\
24079Show debugging of the DWARF DIE reader."), _("\
24080When enabled (non-zero), DIEs are dumped after they are read in.\n\
24081The value is the maximum depth to print."),
24082 NULL,
24083 NULL,
24084 &setdebuglist, &showdebuglist);
9291a0cd 24085
cd4fb1b2
SM
24086 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24087Set debugging of the dwarf line reader."), _("\
24088Show debugging of the dwarf line reader."), _("\
24089When enabled (non-zero), line number entries are dumped as they are read in.\n\
24090A value of 1 (one) provides basic information.\n\
24091A value greater than 1 provides more verbose information."),
24092 NULL,
24093 NULL,
24094 &setdebuglist, &showdebuglist);
437afbb8 24095
cd4fb1b2
SM
24096 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24097Set cross-checking of \"physname\" code against demangler."), _("\
24098Show cross-checking of \"physname\" code against demangler."), _("\
24099When enabled, GDB's internal \"physname\" code is checked against\n\
24100the demangler."),
24101 NULL, show_check_physname,
24102 &setdebuglist, &showdebuglist);
900e11f9 24103
e615022a
DE
24104 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24105 no_class, &use_deprecated_index_sections, _("\
24106Set whether to use deprecated gdb_index sections."), _("\
24107Show whether to use deprecated gdb_index sections."), _("\
24108When enabled, deprecated .gdb_index sections are used anyway.\n\
24109Normally they are ignored either because of a missing feature or\n\
24110performance issue.\n\
24111Warning: This option must be enabled before gdb reads the file."),
24112 NULL,
24113 NULL,
24114 &setlist, &showlist);
24115
f1e6e072
TT
24116 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24117 &dwarf2_locexpr_funcs);
24118 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24119 &dwarf2_loclist_funcs);
24120
24121 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24122 &dwarf2_block_frame_base_locexpr_funcs);
24123 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24124 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24125
24126#if GDB_SELF_TEST
24127 selftests::register_test ("dw2_expand_symtabs_matching",
24128 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24129 selftests::register_test ("dwarf2_find_containing_comp_unit",
24130 selftests::find_containing_comp_unit::run_test);
c62446b1 24131#endif
6502dd73 24132}
This page took 5.572278 seconds and 4 git commands to generate.