hurd: add missing include
[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;
2ec9a5e0 3071
4485a1c1 3072 gdb::array_view<const gdb_byte> main_index_contents
976ca316 3073 = get_gdb_index_contents (objfile, per_objfile->per_bfd);
4485a1c1
SM
3074
3075 if (main_index_contents.empty ())
3076 return 0;
3077
3063847f 3078 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3079 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3080 use_deprecated_index_sections,
3081 main_index_contents, map.get (), &cu_list,
3082 &cu_list_elements, &types_list,
3083 &types_list_elements))
2ec9a5e0
TT
3084 return 0;
3085
0fefef59 3086 /* Don't use the index if it's empty. */
3063847f 3087 if (map->symbol_table.empty ())
0fefef59
DE
3088 return 0;
3089
2ec9a5e0
TT
3090 /* If there is a .dwz file, read it so we can get its CU list as
3091 well. */
976ca316 3092 dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 3093 if (dwz != NULL)
2ec9a5e0 3094 {
2ec9a5e0
TT
3095 struct mapped_index dwz_map;
3096 const gdb_byte *dwz_types_ignore;
3097 offset_type dwz_types_elements_ignore;
3098
4485a1c1
SM
3099 gdb::array_view<const gdb_byte> dwz_index_content
3100 = get_gdb_index_contents_dwz (objfile, dwz);
3101
3102 if (dwz_index_content.empty ())
3103 return 0;
3104
3810f182 3105 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3106 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3107 &dwz_list, &dwz_list_elements,
3108 &dwz_types_ignore,
3109 &dwz_types_elements_ignore))
2ec9a5e0
TT
3110 {
3111 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3112 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3113 return 0;
3114 }
3115 }
3116
976ca316 3117 create_cus_from_index (per_objfile->per_bfd, cu_list, cu_list_elements,
12359b5e 3118 dwz_list, dwz_list_elements);
1fd400ff 3119
8b70b953
TT
3120 if (types_list_elements)
3121 {
8b70b953
TT
3122 /* We can only handle a single .debug_types when we have an
3123 index. */
976ca316 3124 if (per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3125 return 0;
3126
976ca316 3127 dwarf2_section_info *section = &per_objfile->per_bfd->types[0];
8b70b953 3128
976ca316 3129 create_signatured_type_table_from_index (per_objfile->per_bfd,
168c9250
SM
3130 section, types_list,
3131 types_list_elements);
8b70b953 3132 }
9291a0cd 3133
976ca316 3134 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3135
976ca316
SM
3136 per_objfile->per_bfd->index_table = std::move (map);
3137 per_objfile->per_bfd->using_index = 1;
3138 per_objfile->per_bfd->quick_file_names_table =
3139 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3140
3141 return 1;
3142}
3143
dee91e82 3144/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3145
dee91e82
DE
3146static void
3147dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3148 const gdb_byte *info_ptr,
3e225074 3149 struct die_info *comp_unit_die)
9291a0cd 3150{
dee91e82 3151 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3152 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3153 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3154 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3155 struct attribute *attr;
7b9f3c50
DE
3156 void **slot;
3157 struct quick_file_names *qfn;
9291a0cd 3158
0186c6a7
DE
3159 gdb_assert (! this_cu->is_debug_types);
3160
07261596
TT
3161 /* Our callers never want to match partial units -- instead they
3162 will match the enclosing full CU. */
3163 if (comp_unit_die->tag == DW_TAG_partial_unit)
3164 {
3165 this_cu->v.quick->no_file_data = 1;
3166 return;
3167 }
3168
0186c6a7 3169 lh_cu = this_cu;
7b9f3c50 3170 slot = NULL;
dee91e82 3171
fff8551c 3172 line_header_up lh;
9c541725 3173 sect_offset line_offset {};
fff8551c 3174
dee91e82 3175 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3176 if (attr != nullptr)
9291a0cd 3177 {
7b9f3c50
DE
3178 struct quick_file_names find_entry;
3179
9c541725 3180 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3181
3182 /* We may have already read in this line header (TU line header sharing).
3183 If we have we're done. */
094b34ac 3184 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3185 find_entry.hash.line_sect_off = line_offset;
976ca316 3186 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3187 &find_entry, INSERT);
3188 if (*slot != NULL)
3189 {
9a3c8263 3190 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3191 return;
7b9f3c50
DE
3192 }
3193
3019eac3 3194 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3195 }
3196 if (lh == NULL)
3197 {
094b34ac 3198 lh_cu->v.quick->no_file_data = 1;
dee91e82 3199 return;
9291a0cd
TT
3200 }
3201
976ca316 3202 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3203 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3204 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3205 gdb_assert (slot != NULL);
3206 *slot = qfn;
9291a0cd 3207
d721ba37 3208 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3209
aa391654
TT
3210 int offset = 0;
3211 if (strcmp (fnd.name, "<unknown>") != 0)
3212 ++offset;
3213
7ba99d21 3214 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3215 qfn->file_names =
976ca316 3216 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3217 qfn->num_file_names);
aa391654
TT
3218 if (offset != 0)
3219 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3220 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3221 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3222 fnd.comp_dir).release ();
7b9f3c50 3223 qfn->real_names = NULL;
9291a0cd 3224
094b34ac 3225 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3226}
3227
3228/* A helper for the "quick" functions which attempts to read the line
3229 table for THIS_CU. */
3230
3231static struct quick_file_names *
ab432490
SM
3232dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3233 dwarf2_per_objfile *per_objfile)
dee91e82 3234{
0186c6a7
DE
3235 /* This should never be called for TUs. */
3236 gdb_assert (! this_cu->is_debug_types);
3237 /* Nor type unit groups. */
197400e8 3238 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3239
dee91e82
DE
3240 if (this_cu->v.quick->file_names != NULL)
3241 return this_cu->v.quick->file_names;
3242 /* If we know there is no line data, no point in looking again. */
3243 if (this_cu->v.quick->no_file_data)
3244 return NULL;
3245
ab432490 3246 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3247 if (!reader.dummy_p)
3e225074 3248 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3249
3250 if (this_cu->v.quick->no_file_data)
3251 return NULL;
3252 return this_cu->v.quick->file_names;
9291a0cd
TT
3253}
3254
3255/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3256 real path for a given file name from the line table. */
2fdf6df6 3257
9291a0cd 3258static const char *
976ca316 3259dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3260 struct quick_file_names *qfn, int index)
9291a0cd 3261{
7b9f3c50 3262 if (qfn->real_names == NULL)
976ca316 3263 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3264 qfn->num_file_names, const char *);
9291a0cd 3265
7b9f3c50 3266 if (qfn->real_names[index] == NULL)
14278e1f 3267 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3268
7b9f3c50 3269 return qfn->real_names[index];
9291a0cd
TT
3270}
3271
3272static struct symtab *
3273dw2_find_last_source_symtab (struct objfile *objfile)
3274{
976ca316
SM
3275 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3276 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3277 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3278
43f3e411
DE
3279 if (cust == NULL)
3280 return NULL;
ed2dc618 3281
43f3e411 3282 return compunit_primary_filetab (cust);
9291a0cd
TT
3283}
3284
7b9f3c50
DE
3285/* Traversal function for dw2_forget_cached_source_info. */
3286
3287static int
3288dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3289{
7b9f3c50 3290 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3291
7b9f3c50 3292 if (file_data->real_names)
9291a0cd 3293 {
7b9f3c50 3294 int i;
9291a0cd 3295
7b9f3c50 3296 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3297 {
7b9f3c50
DE
3298 xfree ((void*) file_data->real_names[i]);
3299 file_data->real_names[i] = NULL;
9291a0cd
TT
3300 }
3301 }
7b9f3c50
DE
3302
3303 return 1;
3304}
3305
3306static void
3307dw2_forget_cached_source_info (struct objfile *objfile)
3308{
976ca316 3309 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3310
976ca316 3311 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3312 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3313}
3314
f8eba3c6
TT
3315/* Helper function for dw2_map_symtabs_matching_filename that expands
3316 the symtabs and calls the iterator. */
3317
3318static int
3319dw2_map_expand_apply (struct objfile *objfile,
3320 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3321 const char *name, const char *real_path,
14bc53a8 3322 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3323{
43f3e411 3324 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3325
3326 /* Don't visit already-expanded CUs. */
af758d11
SM
3327 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3328 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3329 return 0;
3330
3331 /* This may expand more than one symtab, and we want to iterate over
3332 all of them. */
97a1449a 3333 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3334
14bc53a8
PA
3335 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3336 last_made, callback);
f8eba3c6
TT
3337}
3338
3339/* Implementation of the map_symtabs_matching_filename method. */
3340
14bc53a8
PA
3341static bool
3342dw2_map_symtabs_matching_filename
3343 (struct objfile *objfile, const char *name, const char *real_path,
3344 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3345{
c011a4f4 3346 const char *name_basename = lbasename (name);
976ca316 3347 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3348
848e3e78
DE
3349 /* The rule is CUs specify all the files, including those used by
3350 any TU, so there's no need to scan TUs here. */
f4dc4d17 3351
976ca316 3352 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3353 {
3d7bb9d9 3354 /* We only need to look at symtabs not already expanded. */
976ca316 3355 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3356 continue;
3357
976ca316 3358 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3359 if (file_data == NULL)
9291a0cd
TT
3360 continue;
3361
b76e467d 3362 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3363 {
7b9f3c50 3364 const char *this_name = file_data->file_names[j];
da235a7c 3365 const char *this_real_name;
9291a0cd 3366
af529f8f 3367 if (compare_filenames_for_search (this_name, name))
9291a0cd 3368 {
f5b95b50 3369 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3370 callback))
3371 return true;
288e77a7 3372 continue;
4aac40c8 3373 }
9291a0cd 3374
c011a4f4
DE
3375 /* Before we invoke realpath, which can get expensive when many
3376 files are involved, do a quick comparison of the basenames. */
3377 if (! basenames_may_differ
3378 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3379 continue;
3380
976ca316 3381 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3382 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3383 {
da235a7c 3384 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3385 callback))
3386 return true;
288e77a7 3387 continue;
da235a7c 3388 }
9291a0cd 3389
da235a7c
JK
3390 if (real_path != NULL)
3391 {
af529f8f
JK
3392 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3393 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3394 if (this_real_name != NULL
af529f8f 3395 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3396 {
f5b95b50 3397 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3398 callback))
3399 return true;
288e77a7 3400 continue;
9291a0cd
TT
3401 }
3402 }
3403 }
3404 }
3405
14bc53a8 3406 return false;
9291a0cd
TT
3407}
3408
da51c347
DE
3409/* Struct used to manage iterating over all CUs looking for a symbol. */
3410
3411struct dw2_symtab_iterator
9291a0cd 3412{
ed2dc618 3413 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3414 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3415 /* If set, only look for symbols that match that block. Valid values are
3416 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3417 gdb::optional<block_enum> block_index;
da51c347
DE
3418 /* The kind of symbol we're looking for. */
3419 domain_enum domain;
3420 /* The list of CUs from the index entry of the symbol,
3421 or NULL if not found. */
3422 offset_type *vec;
3423 /* The next element in VEC to look at. */
3424 int next;
3425 /* The number of elements in VEC, or zero if there is no match. */
3426 int length;
8943b874
DE
3427 /* Have we seen a global version of the symbol?
3428 If so we can ignore all further global instances.
3429 This is to work around gold/15646, inefficient gold-generated
3430 indices. */
3431 int global_seen;
da51c347 3432};
9291a0cd 3433
2b79f376 3434/* Initialize the index symtab iterator ITER. */
2fdf6df6 3435
9291a0cd 3436static void
da51c347 3437dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
976ca316 3438 dwarf2_per_objfile *per_objfile,
c7f839cb 3439 gdb::optional<block_enum> block_index,
da51c347
DE
3440 domain_enum domain,
3441 const char *name)
3442{
976ca316 3443 iter->per_objfile = per_objfile;
da51c347
DE
3444 iter->block_index = block_index;
3445 iter->domain = domain;
3446 iter->next = 0;
8943b874 3447 iter->global_seen = 0;
da51c347 3448
976ca316 3449 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3450
3451 /* index is NULL if OBJF_READNOW. */
3452 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3453 iter->length = MAYBE_SWAP (*iter->vec);
3454 else
3455 {
3456 iter->vec = NULL;
3457 iter->length = 0;
3458 }
3459}
3460
3461/* Return the next matching CU or NULL if there are no more. */
3462
3463static struct dwarf2_per_cu_data *
3464dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3465{
976ca316 3466 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3467
da51c347
DE
3468 for ( ; iter->next < iter->length; ++iter->next)
3469 {
3470 offset_type cu_index_and_attrs =
3471 MAYBE_SWAP (iter->vec[iter->next + 1]);
3472 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3473 gdb_index_symbol_kind symbol_kind =
3474 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3475 /* Only check the symbol attributes if they're present.
3476 Indices prior to version 7 don't record them,
3477 and indices >= 7 may elide them for certain symbols
3478 (gold does this). */
3479 int attrs_valid =
976ca316 3480 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3481 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3482
3190f0c6 3483 /* Don't crash on bad data. */
976ca316
SM
3484 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3485 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3486 {
b98664d3 3487 complaint (_(".gdb_index entry has bad CU index"
976ca316 3488 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3489 continue;
3490 }
3491
976ca316 3492 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3493
da51c347 3494 /* Skip if already read in. */
976ca316 3495 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3496 continue;
3497
8943b874
DE
3498 /* Check static vs global. */
3499 if (attrs_valid)
3500 {
2b79f376
SM
3501 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3502
3503 if (iter->block_index.has_value ())
3504 {
3505 bool want_static = *iter->block_index == STATIC_BLOCK;
3506
3507 if (is_static != want_static)
3508 continue;
3509 }
3510
8943b874 3511 /* Work around gold/15646. */
f030440d
TV
3512 if (!is_static
3513 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3514 {
3515 if (iter->global_seen)
3516 continue;
3517
3518 iter->global_seen = 1;
3519 }
8943b874 3520 }
da51c347
DE
3521
3522 /* Only check the symbol's kind if it has one. */
3523 if (attrs_valid)
3524 {
3525 switch (iter->domain)
3526 {
3527 case VAR_DOMAIN:
3528 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3529 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3530 /* Some types are also in VAR_DOMAIN. */
3531 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3532 continue;
3533 break;
3534 case STRUCT_DOMAIN:
3535 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3536 continue;
3537 break;
3538 case LABEL_DOMAIN:
3539 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3540 continue;
3541 break;
59c35742
AB
3542 case MODULE_DOMAIN:
3543 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3544 continue;
3545 break;
da51c347
DE
3546 default:
3547 break;
3548 }
3549 }
3550
3551 ++iter->next;
3552 return per_cu;
3553 }
3554
3555 return NULL;
3556}
3557
43f3e411 3558static struct compunit_symtab *
c7f839cb 3559dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3560 const char *name, domain_enum domain)
9291a0cd 3561{
43f3e411 3562 struct compunit_symtab *stab_best = NULL;
976ca316 3563 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3564
b5ec771e
PA
3565 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3566
ed2dc618
SM
3567 struct dw2_symtab_iterator iter;
3568 struct dwarf2_per_cu_data *per_cu;
da51c347 3569
976ca316 3570 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3571
ed2dc618
SM
3572 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3573 {
3574 struct symbol *sym, *with_opaque = NULL;
97a1449a 3575 struct compunit_symtab *stab
976ca316 3576 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3577 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3578 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3579
ed2dc618
SM
3580 sym = block_find_symbol (block, name, domain,
3581 block_find_non_opaque_type_preferred,
3582 &with_opaque);
b2e2f908 3583
ed2dc618
SM
3584 /* Some caution must be observed with overloaded functions
3585 and methods, since the index will not contain any overload
3586 information (but NAME might contain it). */
da51c347 3587
ed2dc618
SM
3588 if (sym != NULL
3589 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3590 return stab;
3591 if (with_opaque != NULL
3592 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3593 stab_best = stab;
da51c347 3594
ed2dc618 3595 /* Keep looking through other CUs. */
9291a0cd 3596 }
9291a0cd 3597
da51c347 3598 return stab_best;
9291a0cd
TT
3599}
3600
3601static void
3602dw2_print_stats (struct objfile *objfile)
3603{
976ca316
SM
3604 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3605 int total = (per_objfile->per_bfd->all_comp_units.size ()
3606 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3607 int count = 0;
9291a0cd 3608
ed2dc618 3609 for (int i = 0; i < total; ++i)
9291a0cd 3610 {
976ca316 3611 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3612
976ca316 3613 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3614 ++count;
3615 }
e4a48d9d 3616 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3617 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3618}
3619
779bd270
DE
3620/* This dumps minimal information about the index.
3621 It is called via "mt print objfiles".
3622 One use is to verify .gdb_index has been loaded by the
3623 gdb.dwarf2/gdb-index.exp testcase. */
3624
9291a0cd
TT
3625static void
3626dw2_dump (struct objfile *objfile)
3627{
976ca316 3628 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3629
976ca316 3630 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3631 printf_filtered (".gdb_index:");
976ca316 3632 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3633 {
3634 printf_filtered (" version %d\n",
976ca316 3635 per_objfile->per_bfd->index_table->version);
779bd270
DE
3636 }
3637 else
3638 printf_filtered (" faked for \"readnow\"\n");
3639 printf_filtered ("\n");
9291a0cd
TT
3640}
3641
9291a0cd
TT
3642static void
3643dw2_expand_symtabs_for_function (struct objfile *objfile,
3644 const char *func_name)
3645{
976ca316 3646 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3647
ed2dc618
SM
3648 struct dw2_symtab_iterator iter;
3649 struct dwarf2_per_cu_data *per_cu;
da51c347 3650
976ca316 3651 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3652
ed2dc618 3653 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3654 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3655
9291a0cd
TT
3656}
3657
3658static void
3659dw2_expand_all_symtabs (struct objfile *objfile)
3660{
976ca316
SM
3661 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3662 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3663 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3664
ed2dc618 3665 for (int i = 0; i < total_units; ++i)
9291a0cd 3666 {
976ca316 3667 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3668
58f0c718
TT
3669 /* We don't want to directly expand a partial CU, because if we
3670 read it with the wrong language, then assertion failures can
3671 be triggered later on. See PR symtab/23010. So, tell
3672 dw2_instantiate_symtab to skip partial CUs -- any important
3673 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3674 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3675 }
3676}
3677
3678static void
652a8996
JK
3679dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3680 const char *fullname)
9291a0cd 3681{
976ca316 3682 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3683
3684 /* We don't need to consider type units here.
3685 This is only called for examining code, e.g. expand_line_sal.
3686 There can be an order of magnitude (or more) more type units
3687 than comp units, and we avoid them if we can. */
3688
976ca316 3689 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3690 {
3d7bb9d9 3691 /* We only need to look at symtabs not already expanded. */
976ca316 3692 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3693 continue;
3694
976ca316 3695 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3696 if (file_data == NULL)
9291a0cd
TT
3697 continue;
3698
b76e467d 3699 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3700 {
652a8996
JK
3701 const char *this_fullname = file_data->file_names[j];
3702
3703 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3704 {
976ca316 3705 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3706 break;
3707 }
3708 }
3709 }
3710}
3711
9a0bacfb
TV
3712static void
3713dw2_expand_symtabs_matching_symbol
3714 (mapped_index_base &index,
3715 const lookup_name_info &lookup_name_in,
3716 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3717 enum search_domain kind,
fcf23d5b
SM
3718 gdb::function_view<bool (offset_type)> match_callback,
3719 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3720
3721static void
3722dw2_expand_symtabs_matching_one
97a1449a
SM
3723 (dwarf2_per_cu_data *per_cu,
3724 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3725 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3726 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3727
9291a0cd 3728static void
199b4314
TT
3729dw2_map_matching_symbols
3730 (struct objfile *objfile,
b054970d 3731 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3732 int global,
3733 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3734 symbol_compare_ftype *ordered_compare)
9291a0cd 3735{
1aa98955 3736 /* Used for Ada. */
976ca316 3737 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3738
9a0bacfb
TV
3739 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3740
976ca316 3741 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3742 {
3743 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3744 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3745 using GNU index. */
976ca316 3746 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3747
9a0bacfb
TV
3748 const char *match_name = name.ada ().lookup_name ().c_str ();
3749 auto matcher = [&] (const char *symname)
3750 {
3751 if (ordered_compare == nullptr)
3752 return true;
3753 return ordered_compare (symname, match_name) == 0;
3754 };
3755
3756 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3757 [&] (offset_type namei)
3758 {
3759 struct dw2_symtab_iterator iter;
3760 struct dwarf2_per_cu_data *per_cu;
3761
976ca316 3762 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
9a0bacfb
TV
3763 match_name);
3764 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3765 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3766 nullptr);
9a0bacfb 3767 return true;
976ca316 3768 }, per_objfile);
9a0bacfb
TV
3769 }
3770 else
3771 {
3772 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3773 proceed assuming all symtabs have been read in. */
3774 }
1aa98955
TV
3775
3776 for (compunit_symtab *cust : objfile->compunits ())
3777 {
3778 const struct block *block;
3779
3780 if (cust == NULL)
3781 continue;
3782 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3783 if (!iterate_over_symbols_terminated (block, name,
3784 domain, callback))
3785 return;
3786 }
9291a0cd
TT
3787}
3788
e1ef7d7a
PA
3789/* Starting from a search name, return the string that finds the upper
3790 bound of all strings that start with SEARCH_NAME in a sorted name
3791 list. Returns the empty string to indicate that the upper bound is
3792 the end of the list. */
3793
3794static std::string
3795make_sort_after_prefix_name (const char *search_name)
3796{
3797 /* When looking to complete "func", we find the upper bound of all
3798 symbols that start with "func" by looking for where we'd insert
3799 the closest string that would follow "func" in lexicographical
3800 order. Usually, that's "func"-with-last-character-incremented,
3801 i.e. "fund". Mind non-ASCII characters, though. Usually those
3802 will be UTF-8 multi-byte sequences, but we can't be certain.
3803 Especially mind the 0xff character, which is a valid character in
3804 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3805 rule out compilers allowing it in identifiers. Note that
3806 conveniently, strcmp/strcasecmp are specified to compare
3807 characters interpreted as unsigned char. So what we do is treat
3808 the whole string as a base 256 number composed of a sequence of
3809 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3810 to 0, and carries 1 to the following more-significant position.
3811 If the very first character in SEARCH_NAME ends up incremented
3812 and carries/overflows, then the upper bound is the end of the
3813 list. The string after the empty string is also the empty
3814 string.
3815
3816 Some examples of this operation:
3817
3818 SEARCH_NAME => "+1" RESULT
3819
3820 "abc" => "abd"
3821 "ab\xff" => "ac"
3822 "\xff" "a" "\xff" => "\xff" "b"
3823 "\xff" => ""
3824 "\xff\xff" => ""
3825 "" => ""
3826
3827 Then, with these symbols for example:
3828
3829 func
3830 func1
3831 fund
3832
3833 completing "func" looks for symbols between "func" and
3834 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3835 which finds "func" and "func1", but not "fund".
3836
3837 And with:
3838
3839 funcÿ (Latin1 'ÿ' [0xff])
3840 funcÿ1
3841 fund
3842
3843 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3844 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3845
3846 And with:
3847
3848 ÿÿ (Latin1 'ÿ' [0xff])
3849 ÿÿ1
3850
3851 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3852 the end of the list.
3853 */
3854 std::string after = search_name;
3855 while (!after.empty () && (unsigned char) after.back () == 0xff)
3856 after.pop_back ();
3857 if (!after.empty ())
3858 after.back () = (unsigned char) after.back () + 1;
3859 return after;
3860}
3861
5c58de74 3862/* See declaration. */
61d96d7e 3863
5c58de74
PA
3864std::pair<std::vector<name_component>::const_iterator,
3865 std::vector<name_component>::const_iterator>
44ed8f3e 3866mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3867 (const lookup_name_info &lookup_name_without_params, language lang,
3868 dwarf2_per_objfile *per_objfile) const
3f563c84 3869{
5c58de74
PA
3870 auto *name_cmp
3871 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3872
3b00ef10 3873 const char *lang_name
e0802d59 3874 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3875
3f563c84
PA
3876 /* Comparison function object for lower_bound that matches against a
3877 given symbol name. */
3878 auto lookup_compare_lower = [&] (const name_component &elem,
3879 const char *name)
3880 {
fcf23d5b 3881 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3882 const char *elem_name = elem_qualified + elem.name_offset;
3883 return name_cmp (elem_name, name) < 0;
3884 };
3885
3886 /* Comparison function object for upper_bound that matches against a
3887 given symbol name. */
3888 auto lookup_compare_upper = [&] (const char *name,
3889 const name_component &elem)
3890 {
fcf23d5b 3891 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3892 const char *elem_name = elem_qualified + elem.name_offset;
3893 return name_cmp (name, elem_name) < 0;
3894 };
3895
5c58de74
PA
3896 auto begin = this->name_components.begin ();
3897 auto end = this->name_components.end ();
3f563c84
PA
3898
3899 /* Find the lower bound. */
3900 auto lower = [&] ()
3901 {
3b00ef10 3902 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3903 return begin;
3904 else
3b00ef10 3905 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3906 } ();
3907
3908 /* Find the upper bound. */
3909 auto upper = [&] ()
3910 {
5c58de74 3911 if (lookup_name_without_params.completion_mode ())
3f563c84 3912 {
e1ef7d7a
PA
3913 /* In completion mode, we want UPPER to point past all
3914 symbols names that have the same prefix. I.e., with
3915 these symbols, and completing "func":
3916
3917 function << lower bound
3918 function1
3919 other_function << upper bound
3920
3921 We find the upper bound by looking for the insertion
3922 point of "func"-with-last-character-incremented,
3923 i.e. "fund". */
3b00ef10 3924 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3925 if (after.empty ())
3f563c84 3926 return end;
e6b2f5ef
PA
3927 return std::lower_bound (lower, end, after.c_str (),
3928 lookup_compare_lower);
3f563c84
PA
3929 }
3930 else
3b00ef10 3931 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3932 } ();
3933
5c58de74
PA
3934 return {lower, upper};
3935}
3936
3937/* See declaration. */
3938
3939void
fcf23d5b 3940mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3941{
3942 if (!this->name_components.empty ())
3943 return;
3944
3945 this->name_components_casing = case_sensitivity;
3946 auto *name_cmp
3947 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3948
3949 /* The code below only knows how to break apart components of C++
3950 symbol names (and other languages that use '::' as
3b00ef10 3951 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3952 auto count = this->symbol_name_count ();
3953 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3954 {
44ed8f3e 3955 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3956 continue;
3957
fcf23d5b 3958 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3959
3960 /* Add each name component to the name component table. */
3961 unsigned int previous_len = 0;
3b00ef10
TT
3962
3963 if (strstr (name, "::") != nullptr)
3964 {
3965 for (unsigned int current_len = cp_find_first_component (name);
3966 name[current_len] != '\0';
3967 current_len += cp_find_first_component (name + current_len))
3968 {
3969 gdb_assert (name[current_len] == ':');
3970 this->name_components.push_back ({previous_len, idx});
3971 /* Skip the '::'. */
3972 current_len += 2;
3973 previous_len = current_len;
3974 }
3975 }
3976 else
5c58de74 3977 {
3b00ef10
TT
3978 /* Handle the Ada encoded (aka mangled) form here. */
3979 for (const char *iter = strstr (name, "__");
3980 iter != nullptr;
3981 iter = strstr (iter, "__"))
3982 {
3983 this->name_components.push_back ({previous_len, idx});
3984 iter += 2;
3985 previous_len = iter - name;
3986 }
5c58de74 3987 }
3b00ef10 3988
5c58de74
PA
3989 this->name_components.push_back ({previous_len, idx});
3990 }
3991
3992 /* Sort name_components elements by name. */
3993 auto name_comp_compare = [&] (const name_component &left,
3994 const name_component &right)
3995 {
fcf23d5b
SM
3996 const char *left_qualified
3997 = this->symbol_name_at (left.idx, per_objfile);
3998 const char *right_qualified
3999 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4000
4001 const char *left_name = left_qualified + left.name_offset;
4002 const char *right_name = right_qualified + right.name_offset;
4003
4004 return name_cmp (left_name, right_name) < 0;
4005 };
4006
4007 std::sort (this->name_components.begin (),
4008 this->name_components.end (),
4009 name_comp_compare);
4010}
4011
4012/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4013 mapped_index_base instead of the containing objfile. This is split
4014 to a separate function in order to be able to unit test the
4015 name_components matching using a mock mapped_index_base. For each
5c58de74 4016 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4017 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4018
4019static void
4020dw2_expand_symtabs_matching_symbol
44ed8f3e 4021 (mapped_index_base &index,
5c58de74
PA
4022 const lookup_name_info &lookup_name_in,
4023 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4024 enum search_domain kind,
fcf23d5b
SM
4025 gdb::function_view<bool (offset_type)> match_callback,
4026 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4027{
4028 lookup_name_info lookup_name_without_params
4029 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4030
4031 /* Build the symbol name component sorted vector, if we haven't
4032 yet. */
fcf23d5b 4033 index.build_name_components (per_objfile);
5c58de74 4034
3f563c84
PA
4035 /* The same symbol may appear more than once in the range though.
4036 E.g., if we're looking for symbols that complete "w", and we have
4037 a symbol named "w1::w2", we'll find the two name components for
4038 that same symbol in the range. To be sure we only call the
4039 callback once per symbol, we first collect the symbol name
4040 indexes that matched in a temporary vector and ignore
4041 duplicates. */
4042 std::vector<offset_type> matches;
3f563c84 4043
3b00ef10
TT
4044 struct name_and_matcher
4045 {
4046 symbol_name_matcher_ftype *matcher;
ecc6c606 4047 const char *name;
3b00ef10
TT
4048
4049 bool operator== (const name_and_matcher &other) const
3f563c84 4050 {
ecc6c606 4051 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4052 }
4053 };
4054
4055 /* A vector holding all the different symbol name matchers, for all
4056 languages. */
4057 std::vector<name_and_matcher> matchers;
4058
4059 for (int i = 0; i < nr_languages; i++)
4060 {
4061 enum language lang_e = (enum language) i;
4062
4063 const language_defn *lang = language_def (lang_e);
4064 symbol_name_matcher_ftype *name_matcher
4065 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4066
3b00ef10
TT
4067 name_and_matcher key {
4068 name_matcher,
4069 lookup_name_without_params.language_lookup_name (lang_e)
4070 };
4071
4072 /* Don't insert the same comparison routine more than once.
4073 Note that we do this linear walk. This is not a problem in
4074 practice because the number of supported languages is
4075 low. */
4076 if (std::find (matchers.begin (), matchers.end (), key)
4077 != matchers.end ())
9291a0cd 4078 continue;
3b00ef10
TT
4079 matchers.push_back (std::move (key));
4080
4081 auto bounds
4082 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4083 lang_e, per_objfile);
3b00ef10
TT
4084
4085 /* Now for each symbol name in range, check to see if we have a name
4086 match, and if so, call the MATCH_CALLBACK callback. */
4087
4088 for (; bounds.first != bounds.second; ++bounds.first)
4089 {
fcf23d5b
SM
4090 const char *qualified
4091 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4092
4093 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4094 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4095 continue;
9291a0cd 4096
3b00ef10
TT
4097 matches.push_back (bounds.first->idx);
4098 }
3f563c84
PA
4099 }
4100
4101 std::sort (matches.begin (), matches.end ());
4102
4103 /* Finally call the callback, once per match. */
4104 ULONGEST prev = -1;
4105 for (offset_type idx : matches)
4106 {
4107 if (prev != idx)
4108 {
3b00ef10
TT
4109 if (!match_callback (idx))
4110 break;
3f563c84
PA
4111 prev = idx;
4112 }
4113 }
4114
4115 /* Above we use a type wider than idx's for 'prev', since 0 and
4116 (offset_type)-1 are both possible values. */
4117 static_assert (sizeof (prev) > sizeof (offset_type), "");
4118}
4119
c62446b1
PA
4120#if GDB_SELF_TEST
4121
4122namespace selftests { namespace dw2_expand_symtabs_matching {
4123
a3c5fafd
PA
4124/* A mock .gdb_index/.debug_names-like name index table, enough to
4125 exercise dw2_expand_symtabs_matching_symbol, which works with the
4126 mapped_index_base interface. Builds an index from the symbol list
4127 passed as parameter to the constructor. */
4128class mock_mapped_index : public mapped_index_base
c62446b1
PA
4129{
4130public:
a3c5fafd
PA
4131 mock_mapped_index (gdb::array_view<const char *> symbols)
4132 : m_symbol_table (symbols)
c62446b1
PA
4133 {}
4134
a3c5fafd 4135 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4136
a3c5fafd 4137 /* Return the number of names in the symbol table. */
632e107b 4138 size_t symbol_name_count () const override
c62446b1 4139 {
a3c5fafd 4140 return m_symbol_table.size ();
c62446b1
PA
4141 }
4142
a3c5fafd 4143 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4144 const char *symbol_name_at
4145 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4146 {
4147 return m_symbol_table[idx];
4148 }
c62446b1 4149
a3c5fafd
PA
4150private:
4151 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4152};
4153
4154/* Convenience function that converts a NULL pointer to a "<null>"
4155 string, to pass to print routines. */
4156
4157static const char *
4158string_or_null (const char *str)
4159{
4160 return str != NULL ? str : "<null>";
4161}
4162
4163/* Check if a lookup_name_info built from
4164 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4165 index. EXPECTED_LIST is the list of expected matches, in expected
4166 matching order. If no match expected, then an empty list is
4167 specified. Returns true on success. On failure prints a warning
4168 indicating the file:line that failed, and returns false. */
4169
4170static bool
4171check_match (const char *file, int line,
4172 mock_mapped_index &mock_index,
4173 const char *name, symbol_name_match_type match_type,
4174 bool completion_mode,
fcf23d5b
SM
4175 std::initializer_list<const char *> expected_list,
4176 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4177{
4178 lookup_name_info lookup_name (name, match_type, completion_mode);
4179
4180 bool matched = true;
4181
4182 auto mismatch = [&] (const char *expected_str,
4183 const char *got)
4184 {
4185 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4186 "expected=\"%s\", got=\"%s\"\n"),
4187 file, line,
4188 (match_type == symbol_name_match_type::FULL
4189 ? "FULL" : "WILD"),
4190 name, string_or_null (expected_str), string_or_null (got));
4191 matched = false;
4192 };
4193
4194 auto expected_it = expected_list.begin ();
4195 auto expected_end = expected_list.end ();
4196
a3c5fafd 4197 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4198 NULL, ALL_DOMAIN,
4199 [&] (offset_type idx)
4200 {
fcf23d5b 4201 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4202 const char *expected_str
4203 = expected_it == expected_end ? NULL : *expected_it++;
4204
4205 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4206 mismatch (expected_str, matched_name);
3b00ef10 4207 return true;
fcf23d5b 4208 }, per_objfile);
c62446b1
PA
4209
4210 const char *expected_str
4211 = expected_it == expected_end ? NULL : *expected_it++;
4212 if (expected_str != NULL)
4213 mismatch (expected_str, NULL);
4214
4215 return matched;
4216}
4217
4218/* The symbols added to the mock mapped_index for testing (in
4219 canonical form). */
4220static const char *test_symbols[] = {
4221 "function",
4222 "std::bar",
4223 "std::zfunction",
4224 "std::zfunction2",
4225 "w1::w2",
4226 "ns::foo<char*>",
4227 "ns::foo<int>",
4228 "ns::foo<long>",
a20714ff
PA
4229 "ns2::tmpl<int>::foo2",
4230 "(anonymous namespace)::A::B::C",
c62446b1 4231
e1ef7d7a
PA
4232 /* These are used to check that the increment-last-char in the
4233 matching algorithm for completion doesn't match "t1_fund" when
4234 completing "t1_func". */
4235 "t1_func",
4236 "t1_func1",
4237 "t1_fund",
4238 "t1_fund1",
4239
4240 /* A UTF-8 name with multi-byte sequences to make sure that
4241 cp-name-parser understands this as a single identifier ("função"
4242 is "function" in PT). */
4243 u8"u8função",
4244
4245 /* \377 (0xff) is Latin1 'ÿ'. */
4246 "yfunc\377",
4247
4248 /* \377 (0xff) is Latin1 'ÿ'. */
4249 "\377",
4250 "\377\377123",
4251
c62446b1
PA
4252 /* A name with all sorts of complications. Starts with "z" to make
4253 it easier for the completion tests below. */
4254#define Z_SYM_NAME \
4255 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4256 "::tuple<(anonymous namespace)::ui*, " \
4257 "std::default_delete<(anonymous namespace)::ui>, void>"
4258
4259 Z_SYM_NAME
4260};
4261
a3c5fafd
PA
4262/* Returns true if the mapped_index_base::find_name_component_bounds
4263 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4264 in completion mode. */
5c58de74
PA
4265
4266static bool
a3c5fafd 4267check_find_bounds_finds (mapped_index_base &index,
5c58de74 4268 const char *search_name,
fcf23d5b
SM
4269 gdb::array_view<const char *> expected_syms,
4270 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4271{
4272 lookup_name_info lookup_name (search_name,
4273 symbol_name_match_type::FULL, true);
4274
3b00ef10 4275 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4276 language_cplus,
4277 per_objfile);
5c58de74
PA
4278
4279 size_t distance = std::distance (bounds.first, bounds.second);
4280 if (distance != expected_syms.size ())
4281 return false;
4282
4283 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4284 {
4285 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4286 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4287 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4288 return false;
4289 }
4290
4291 return true;
4292}
4293
4294/* Test the lower-level mapped_index::find_name_component_bounds
4295 method. */
4296
c62446b1 4297static void
5c58de74
PA
4298test_mapped_index_find_name_component_bounds ()
4299{
4300 mock_mapped_index mock_index (test_symbols);
4301
fcf23d5b 4302 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4303
4304 /* Test the lower-level mapped_index::find_name_component_bounds
4305 method in completion mode. */
4306 {
4307 static const char *expected_syms[] = {
4308 "t1_func",
4309 "t1_func1",
5c58de74
PA
4310 };
4311
fcf23d5b
SM
4312 SELF_CHECK (check_find_bounds_finds
4313 (mock_index, "t1_func", expected_syms,
4314 NULL /* per_objfile */));
5c58de74
PA
4315 }
4316
4317 /* Check that the increment-last-char in the name matching algorithm
4318 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4319 {
4320 static const char *expected_syms1[] = {
4321 "\377",
4322 "\377\377123",
4323 };
fcf23d5b
SM
4324 SELF_CHECK (check_find_bounds_finds
4325 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4326
4327 static const char *expected_syms2[] = {
4328 "\377\377123",
4329 };
fcf23d5b
SM
4330 SELF_CHECK (check_find_bounds_finds
4331 (mock_index, "\377\377", expected_syms2,
4332 NULL /* per_objfile */));
5c58de74
PA
4333 }
4334}
4335
4336/* Test dw2_expand_symtabs_matching_symbol. */
4337
4338static void
4339test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4340{
4341 mock_mapped_index mock_index (test_symbols);
4342
4343 /* We let all tests run until the end even if some fails, for debug
4344 convenience. */
4345 bool any_mismatch = false;
4346
4347 /* Create the expected symbols list (an initializer_list). Needed
4348 because lists have commas, and we need to pass them to CHECK,
4349 which is a macro. */
4350#define EXPECT(...) { __VA_ARGS__ }
4351
4352 /* Wrapper for check_match that passes down the current
4353 __FILE__/__LINE__. */
4354#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4355 any_mismatch |= !check_match (__FILE__, __LINE__, \
4356 mock_index, \
4357 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4358 EXPECTED_LIST, NULL)
c62446b1
PA
4359
4360 /* Identity checks. */
4361 for (const char *sym : test_symbols)
4362 {
4363 /* Should be able to match all existing symbols. */
4364 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4365 EXPECT (sym));
4366
4367 /* Should be able to match all existing symbols with
4368 parameters. */
4369 std::string with_params = std::string (sym) + "(int)";
4370 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4371 EXPECT (sym));
4372
4373 /* Should be able to match all existing symbols with
4374 parameters and qualifiers. */
4375 with_params = std::string (sym) + " ( int ) const";
4376 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4377 EXPECT (sym));
4378
4379 /* This should really find sym, but cp-name-parser.y doesn't
4380 know about lvalue/rvalue qualifiers yet. */
4381 with_params = std::string (sym) + " ( int ) &&";
4382 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4383 {});
4384 }
4385
e1ef7d7a
PA
4386 /* Check that the name matching algorithm for completion doesn't get
4387 confused with Latin1 'ÿ' / 0xff. */
4388 {
4389 static const char str[] = "\377";
4390 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4391 EXPECT ("\377", "\377\377123"));
4392 }
4393
4394 /* Check that the increment-last-char in the matching algorithm for
4395 completion doesn't match "t1_fund" when completing "t1_func". */
4396 {
4397 static const char str[] = "t1_func";
4398 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4399 EXPECT ("t1_func", "t1_func1"));
4400 }
4401
c62446b1
PA
4402 /* Check that completion mode works at each prefix of the expected
4403 symbol name. */
4404 {
4405 static const char str[] = "function(int)";
4406 size_t len = strlen (str);
4407 std::string lookup;
4408
4409 for (size_t i = 1; i < len; i++)
4410 {
4411 lookup.assign (str, i);
4412 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4413 EXPECT ("function"));
4414 }
4415 }
4416
4417 /* While "w" is a prefix of both components, the match function
4418 should still only be called once. */
4419 {
4420 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4421 EXPECT ("w1::w2"));
a20714ff
PA
4422 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4423 EXPECT ("w1::w2"));
c62446b1
PA
4424 }
4425
4426 /* Same, with a "complicated" symbol. */
4427 {
4428 static const char str[] = Z_SYM_NAME;
4429 size_t len = strlen (str);
4430 std::string lookup;
4431
4432 for (size_t i = 1; i < len; i++)
4433 {
4434 lookup.assign (str, i);
4435 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4436 EXPECT (Z_SYM_NAME));
4437 }
4438 }
4439
4440 /* In FULL mode, an incomplete symbol doesn't match. */
4441 {
4442 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4443 {});
4444 }
4445
4446 /* A complete symbol with parameters matches any overload, since the
4447 index has no overload info. */
4448 {
4449 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4450 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4451 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4452 EXPECT ("std::zfunction", "std::zfunction2"));
4453 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4454 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4455 }
4456
4457 /* Check that whitespace is ignored appropriately. A symbol with a
4458 template argument list. */
4459 {
4460 static const char expected[] = "ns::foo<int>";
4461 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4462 EXPECT (expected));
a20714ff
PA
4463 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4464 EXPECT (expected));
c62446b1
PA
4465 }
4466
4467 /* Check that whitespace is ignored appropriately. A symbol with a
4468 template argument list that includes a pointer. */
4469 {
4470 static const char expected[] = "ns::foo<char*>";
4471 /* Try both completion and non-completion modes. */
4472 static const bool completion_mode[2] = {false, true};
4473 for (size_t i = 0; i < 2; i++)
4474 {
4475 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4476 completion_mode[i], EXPECT (expected));
a20714ff
PA
4477 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4478 completion_mode[i], EXPECT (expected));
c62446b1
PA
4479
4480 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4481 completion_mode[i], EXPECT (expected));
a20714ff
PA
4482 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4483 completion_mode[i], EXPECT (expected));
c62446b1
PA
4484 }
4485 }
4486
4487 {
4488 /* Check method qualifiers are ignored. */
4489 static const char expected[] = "ns::foo<char*>";
4490 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4491 symbol_name_match_type::FULL, true, EXPECT (expected));
4492 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4493 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4494 CHECK_MATCH ("foo < char * > ( int ) const",
4495 symbol_name_match_type::WILD, true, EXPECT (expected));
4496 CHECK_MATCH ("foo < char * > ( int ) &&",
4497 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4498 }
4499
4500 /* Test lookup names that don't match anything. */
4501 {
a20714ff
PA
4502 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4503 {});
4504
c62446b1
PA
4505 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4506 {});
4507 }
4508
a20714ff
PA
4509 /* Some wild matching tests, exercising "(anonymous namespace)",
4510 which should not be confused with a parameter list. */
4511 {
4512 static const char *syms[] = {
4513 "A::B::C",
4514 "B::C",
4515 "C",
4516 "A :: B :: C ( int )",
4517 "B :: C ( int )",
4518 "C ( int )",
4519 };
4520
4521 for (const char *s : syms)
4522 {
4523 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4524 EXPECT ("(anonymous namespace)::A::B::C"));
4525 }
4526 }
4527
4528 {
4529 static const char expected[] = "ns2::tmpl<int>::foo2";
4530 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4531 EXPECT (expected));
4532 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4533 EXPECT (expected));
4534 }
4535
c62446b1
PA
4536 SELF_CHECK (!any_mismatch);
4537
4538#undef EXPECT
4539#undef CHECK_MATCH
4540}
4541
5c58de74
PA
4542static void
4543run_test ()
4544{
4545 test_mapped_index_find_name_component_bounds ();
4546 test_dw2_expand_symtabs_matching_symbol ();
4547}
4548
c62446b1
PA
4549}} // namespace selftests::dw2_expand_symtabs_matching
4550
4551#endif /* GDB_SELF_TEST */
4552
4b514bc8
JK
4553/* If FILE_MATCHER is NULL or if PER_CU has
4554 dwarf2_per_cu_quick_data::MARK set (see
4555 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4556 EXPANSION_NOTIFY on it. */
4557
4558static void
4559dw2_expand_symtabs_matching_one
97a1449a
SM
4560 (dwarf2_per_cu_data *per_cu,
4561 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4562 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4563 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4564{
4565 if (file_matcher == NULL || per_cu->v.quick->mark)
4566 {
af758d11 4567 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4568
97a1449a
SM
4569 compunit_symtab *symtab
4570 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4571 gdb_assert (symtab != nullptr);
4b514bc8 4572
af758d11
SM
4573 if (expansion_notify != NULL && symtab_was_null)
4574 expansion_notify (symtab);
4b514bc8
JK
4575 }
4576}
4577
3f563c84
PA
4578/* Helper for dw2_expand_matching symtabs. Called on each symbol
4579 matched, to expand corresponding CUs that were marked. IDX is the
4580 index of the symbol name that matched. */
4581
4582static void
4583dw2_expand_marked_cus
976ca316 4584 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4585 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4586 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4587 search_domain kind)
4588{
3f563c84
PA
4589 offset_type *vec, vec_len, vec_idx;
4590 bool global_seen = false;
976ca316 4591 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4592
61920122 4593 vec = (offset_type *) (index.constant_pool
f00a2de2 4594 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4595 vec_len = MAYBE_SWAP (vec[0]);
4596 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4597 {
61920122
PA
4598 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4599 /* This value is only valid for index versions >= 7. */
4600 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4601 gdb_index_symbol_kind symbol_kind =
4602 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4603 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4604 /* Only check the symbol attributes if they're present.
4605 Indices prior to version 7 don't record them,
4606 and indices >= 7 may elide them for certain symbols
4607 (gold does this). */
4608 int attrs_valid =
4609 (index.version >= 7
4610 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4611
4612 /* Work around gold/15646. */
f030440d
TV
4613 if (attrs_valid
4614 && !is_static
4615 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4616 {
f030440d 4617 if (global_seen)
61920122 4618 continue;
f030440d
TV
4619
4620 global_seen = true;
61920122 4621 }
3190f0c6 4622
61920122
PA
4623 /* Only check the symbol's kind if it has one. */
4624 if (attrs_valid)
4625 {
4626 switch (kind)
8943b874 4627 {
61920122
PA
4628 case VARIABLES_DOMAIN:
4629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4630 continue;
4631 break;
4632 case FUNCTIONS_DOMAIN:
4633 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4634 continue;
61920122
PA
4635 break;
4636 case TYPES_DOMAIN:
4637 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4638 continue;
4639 break;
59c35742
AB
4640 case MODULES_DOMAIN:
4641 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4642 continue;
4643 break;
61920122
PA
4644 default:
4645 break;
8943b874 4646 }
61920122 4647 }
8943b874 4648
61920122 4649 /* Don't crash on bad data. */
976ca316
SM
4650 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4651 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4652 {
b98664d3 4653 complaint (_(".gdb_index entry has bad CU index"
976ca316 4654 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4655 continue;
4656 }
4657
976ca316
SM
4658 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4659 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4660 expansion_notify);
61920122
PA
4661 }
4662}
4663
4b514bc8
JK
4664/* If FILE_MATCHER is non-NULL, set all the
4665 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4666 that match FILE_MATCHER. */
4667
61920122 4668static void
4b514bc8 4669dw_expand_symtabs_matching_file_matcher
976ca316 4670 (dwarf2_per_objfile *per_objfile,
ed2dc618 4671 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4672{
4b514bc8 4673 if (file_matcher == NULL)
61920122
PA
4674 return;
4675
4b514bc8
JK
4676 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4677 htab_eq_pointer,
4678 NULL, xcalloc, xfree));
4679 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4680 htab_eq_pointer,
4681 NULL, xcalloc, xfree));
61920122 4682
4b514bc8
JK
4683 /* The rule is CUs specify all the files, including those used by
4684 any TU, so there's no need to scan TUs here. */
61920122 4685
976ca316 4686 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4687 {
927aa2e7
JK
4688 QUIT;
4689
4690 per_cu->v.quick->mark = 0;
4691
4692 /* We only need to look at symtabs not already expanded. */
976ca316 4693 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4694 continue;
4695
976ca316 4696 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4697 if (file_data == NULL)
4698 continue;
4699
4700 if (htab_find (visited_not_found.get (), file_data) != NULL)
4701 continue;
4702 else if (htab_find (visited_found.get (), file_data) != NULL)
4703 {
4704 per_cu->v.quick->mark = 1;
4705 continue;
4706 }
4707
b76e467d 4708 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4709 {
4710 const char *this_real_name;
4711
4712 if (file_matcher (file_data->file_names[j], false))
4713 {
4714 per_cu->v.quick->mark = 1;
4715 break;
4716 }
4717
4718 /* Before we invoke realpath, which can get expensive when many
4719 files are involved, do a quick comparison of the basenames. */
4720 if (!basenames_may_differ
4721 && !file_matcher (lbasename (file_data->file_names[j]),
4722 true))
4723 continue;
4724
976ca316 4725 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4726 if (file_matcher (this_real_name, false))
4727 {
4728 per_cu->v.quick->mark = 1;
4729 break;
4730 }
4731 }
4732
b76e467d
SM
4733 void **slot = htab_find_slot (per_cu->v.quick->mark
4734 ? visited_found.get ()
4735 : visited_not_found.get (),
4736 file_data, INSERT);
927aa2e7
JK
4737 *slot = file_data;
4738 }
4739}
4740
4741static void
4742dw2_expand_symtabs_matching
4743 (struct objfile *objfile,
4744 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4745 const lookup_name_info *lookup_name,
927aa2e7
JK
4746 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4747 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4748 enum search_domain kind)
4749{
976ca316 4750 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4751
4752 /* index_table is NULL if OBJF_READNOW. */
976ca316 4753 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4754 return;
4755
976ca316 4756 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4757
c1a66c06
TV
4758 if (symbol_matcher == NULL && lookup_name == NULL)
4759 {
976ca316 4760 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4761 {
4762 QUIT;
4763
976ca316 4764 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4765 file_matcher, expansion_notify);
c1a66c06
TV
4766 }
4767 return;
4768 }
4769
976ca316 4770 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4771
c1a66c06 4772 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4773 symbol_matcher,
4774 kind, [&] (offset_type idx)
4775 {
976ca316
SM
4776 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4777 kind);
3b00ef10 4778 return true;
976ca316 4779 }, per_objfile);
927aa2e7
JK
4780}
4781
4782/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4783 symtab. */
4784
4785static struct compunit_symtab *
4786recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4787 CORE_ADDR pc)
4788{
4789 int i;
4790
4791 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4792 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4793 return cust;
4794
4795 if (cust->includes == NULL)
4796 return NULL;
4797
4798 for (i = 0; cust->includes[i]; ++i)
4799 {
4800 struct compunit_symtab *s = cust->includes[i];
4801
4802 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4803 if (s != NULL)
4804 return s;
4805 }
4806
4807 return NULL;
4808}
4809
4810static struct compunit_symtab *
4811dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4812 struct bound_minimal_symbol msymbol,
4813 CORE_ADDR pc,
4814 struct obj_section *section,
4815 int warn_if_readin)
4816{
4817 struct dwarf2_per_cu_data *data;
4818 struct compunit_symtab *result;
4819
d320c2b5 4820 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4821 return NULL;
4822
b3b3bada 4823 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4824 data = (struct dwarf2_per_cu_data *) addrmap_find
4825 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4826 if (!data)
4827 return NULL;
4828
af758d11
SM
4829 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4830 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4831 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4832 paddress (objfile->arch (), pc));
927aa2e7 4833
97a1449a
SM
4834 result = recursively_find_pc_sect_compunit_symtab
4835 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4836
927aa2e7
JK
4837 gdb_assert (result != NULL);
4838 return result;
4839}
4840
4841static void
4842dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4843 void *data, int need_fullname)
4844{
976ca316 4845 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4846
976ca316 4847 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4848 {
976ca316 4849 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4850
4851 htab_up visited (htab_create_alloc (10,
4852 htab_hash_pointer, htab_eq_pointer,
4853 NULL, xcalloc, xfree));
4854
4855 /* The rule is CUs specify all the files, including those used
4856 by any TU, so there's no need to scan TUs here. We can
4857 ignore file names coming from already-expanded CUs. */
4858
976ca316 4859 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4860 {
976ca316 4861 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4862 {
4863 void **slot = htab_find_slot (visited.get (),
4864 per_cu->v.quick->file_names,
4865 INSERT);
4866
4867 *slot = per_cu->v.quick->file_names;
4868 }
4869 }
4870
976ca316 4871 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4872 {
927aa2e7 4873 /* We only need to look at symtabs not already expanded. */
976ca316 4874 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4875 continue;
4876
ab432490 4877 quick_file_names *file_data
976ca316 4878 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4879 if (file_data == NULL)
4880 continue;
4881
b76e467d 4882 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4883 if (*slot)
4884 {
4885 /* Already visited. */
4886 continue;
4887 }
4888 *slot = file_data;
4889
4890 for (int j = 0; j < file_data->num_file_names; ++j)
4891 {
4892 const char *filename = file_data->file_names[j];
976ca316 4893 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4894 }
4895 }
4896 }
4897
976ca316 4898 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4899 {
4900 gdb::unique_xmalloc_ptr<char> this_real_name;
4901
4902 if (need_fullname)
4903 this_real_name = gdb_realpath (filename);
4904 (*fun) (filename, this_real_name.get (), data);
4905 });
4906}
4907
4908static int
4909dw2_has_symbols (struct objfile *objfile)
4910{
4911 return 1;
4912}
4913
4914const struct quick_symbol_functions dwarf2_gdb_index_functions =
4915{
4916 dw2_has_symbols,
4917 dw2_find_last_source_symtab,
4918 dw2_forget_cached_source_info,
4919 dw2_map_symtabs_matching_filename,
4920 dw2_lookup_symbol,
d3214198 4921 NULL,
927aa2e7
JK
4922 dw2_print_stats,
4923 dw2_dump,
927aa2e7
JK
4924 dw2_expand_symtabs_for_function,
4925 dw2_expand_all_symtabs,
4926 dw2_expand_symtabs_with_fullname,
4927 dw2_map_matching_symbols,
4928 dw2_expand_symtabs_matching,
4929 dw2_find_pc_sect_compunit_symtab,
4930 NULL,
4931 dw2_map_symbol_filenames
4932};
4933
4934/* DWARF-5 debug_names reader. */
4935
4936/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4937static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4938
4939/* A helper function that reads the .debug_names section in SECTION
4940 and fills in MAP. FILENAME is the name of the file containing the
4941 section; it is used for error reporting.
4942
4943 Returns true if all went well, false otherwise. */
4944
4945static bool
4946read_debug_names_from_section (struct objfile *objfile,
4947 const char *filename,
4948 struct dwarf2_section_info *section,
4949 mapped_debug_names &map)
4950{
96b79293 4951 if (section->empty ())
927aa2e7
JK
4952 return false;
4953
4954 /* Older elfutils strip versions could keep the section in the main
4955 executable while splitting it for the separate debug info file. */
96b79293 4956 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4957 return false;
4958
96b79293 4959 section->read (objfile);
927aa2e7 4960
08feed99 4961 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4962
4963 const gdb_byte *addr = section->buffer;
4964
96b79293 4965 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4966
4967 unsigned int bytes_read;
4968 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4969 addr += bytes_read;
4970
4971 map.dwarf5_is_dwarf64 = bytes_read != 4;
4972 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4973 if (bytes_read + length != section->size)
4974 {
4975 /* There may be multiple per-CU indices. */
4976 warning (_("Section .debug_names in %s length %s does not match "
4977 "section length %s, ignoring .debug_names."),
4978 filename, plongest (bytes_read + length),
4979 pulongest (section->size));
4980 return false;
4981 }
4982
4983 /* The version number. */
4984 uint16_t version = read_2_bytes (abfd, addr);
4985 addr += 2;
4986 if (version != 5)
4987 {
4988 warning (_("Section .debug_names in %s has unsupported version %d, "
4989 "ignoring .debug_names."),
4990 filename, version);
4991 return false;
4992 }
4993
4994 /* Padding. */
4995 uint16_t padding = read_2_bytes (abfd, addr);
4996 addr += 2;
4997 if (padding != 0)
4998 {
4999 warning (_("Section .debug_names in %s has unsupported padding %d, "
5000 "ignoring .debug_names."),
5001 filename, padding);
5002 return false;
5003 }
5004
5005 /* comp_unit_count - The number of CUs in the CU list. */
5006 map.cu_count = read_4_bytes (abfd, addr);
5007 addr += 4;
5008
5009 /* local_type_unit_count - The number of TUs in the local TU
5010 list. */
5011 map.tu_count = read_4_bytes (abfd, addr);
5012 addr += 4;
5013
5014 /* foreign_type_unit_count - The number of TUs in the foreign TU
5015 list. */
5016 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5017 addr += 4;
5018 if (foreign_tu_count != 0)
5019 {
5020 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5021 "ignoring .debug_names."),
5022 filename, static_cast<unsigned long> (foreign_tu_count));
5023 return false;
5024 }
5025
5026 /* bucket_count - The number of hash buckets in the hash lookup
5027 table. */
5028 map.bucket_count = read_4_bytes (abfd, addr);
5029 addr += 4;
5030
5031 /* name_count - The number of unique names in the index. */
5032 map.name_count = read_4_bytes (abfd, addr);
5033 addr += 4;
5034
5035 /* abbrev_table_size - The size in bytes of the abbreviations
5036 table. */
5037 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5038 addr += 4;
5039
5040 /* augmentation_string_size - The size in bytes of the augmentation
5041 string. This value is rounded up to a multiple of 4. */
5042 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5043 addr += 4;
5044 map.augmentation_is_gdb = ((augmentation_string_size
5045 == sizeof (dwarf5_augmentation))
5046 && memcmp (addr, dwarf5_augmentation,
5047 sizeof (dwarf5_augmentation)) == 0);
5048 augmentation_string_size += (-augmentation_string_size) & 3;
5049 addr += augmentation_string_size;
5050
5051 /* List of CUs */
5052 map.cu_table_reordered = addr;
5053 addr += map.cu_count * map.offset_size;
5054
5055 /* List of Local TUs */
5056 map.tu_table_reordered = addr;
5057 addr += map.tu_count * map.offset_size;
5058
5059 /* Hash Lookup Table */
5060 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5061 addr += map.bucket_count * 4;
5062 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5063 addr += map.name_count * 4;
5064
5065 /* Name Table */
5066 map.name_table_string_offs_reordered = addr;
5067 addr += map.name_count * map.offset_size;
5068 map.name_table_entry_offs_reordered = addr;
5069 addr += map.name_count * map.offset_size;
5070
5071 const gdb_byte *abbrev_table_start = addr;
5072 for (;;)
5073 {
927aa2e7
JK
5074 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5075 addr += bytes_read;
5076 if (index_num == 0)
5077 break;
5078
5079 const auto insertpair
5080 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5081 if (!insertpair.second)
5082 {
5083 warning (_("Section .debug_names in %s has duplicate index %s, "
5084 "ignoring .debug_names."),
5085 filename, pulongest (index_num));
5086 return false;
5087 }
5088 mapped_debug_names::index_val &indexval = insertpair.first->second;
5089 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5090 addr += bytes_read;
5091
5092 for (;;)
5093 {
5094 mapped_debug_names::index_val::attr attr;
5095 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5096 addr += bytes_read;
5097 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5098 addr += bytes_read;
5099 if (attr.form == DW_FORM_implicit_const)
5100 {
5101 attr.implicit_const = read_signed_leb128 (abfd, addr,
5102 &bytes_read);
5103 addr += bytes_read;
5104 }
5105 if (attr.dw_idx == 0 && attr.form == 0)
5106 break;
5107 indexval.attr_vec.push_back (std::move (attr));
5108 }
5109 }
5110 if (addr != abbrev_table_start + abbrev_table_size)
5111 {
5112 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5113 "of size %s vs. written as %u, ignoring .debug_names."),
5114 filename, plongest (addr - abbrev_table_start),
5115 abbrev_table_size);
927aa2e7
JK
5116 return false;
5117 }
5118 map.entry_pool = addr;
5119
5120 return true;
5121}
5122
5123/* A helper for create_cus_from_debug_names that handles the MAP's CU
5124 list. */
5125
5126static void
168c9250 5127create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5128 const mapped_debug_names &map,
5129 dwarf2_section_info &section,
b76e467d 5130 bool is_dwz)
927aa2e7 5131{
3ee6bb11
TV
5132 if (!map.augmentation_is_gdb)
5133 {
5134 for (uint32_t i = 0; i < map.cu_count; ++i)
5135 {
5136 sect_offset sect_off
5137 = (sect_offset) (extract_unsigned_integer
5138 (map.cu_table_reordered + i * map.offset_size,
5139 map.offset_size,
5140 map.dwarf5_byte_order));
5141 /* We don't know the length of the CU, because the CU list in a
5142 .debug_names index can be incomplete, so we can't use the start of
5143 the next CU as end of this CU. We create the CUs here with length 0,
5144 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5145 dwarf2_per_cu_data *per_cu
168c9250
SM
5146 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5147 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5148 }
5149 }
5150
927aa2e7
JK
5151 sect_offset sect_off_prev;
5152 for (uint32_t i = 0; i <= map.cu_count; ++i)
5153 {
5154 sect_offset sect_off_next;
5155 if (i < map.cu_count)
5156 {
5157 sect_off_next
5158 = (sect_offset) (extract_unsigned_integer
5159 (map.cu_table_reordered + i * map.offset_size,
5160 map.offset_size,
5161 map.dwarf5_byte_order));
5162 }
5163 else
5164 sect_off_next = (sect_offset) section.size;
5165 if (i >= 1)
5166 {
5167 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5168 dwarf2_per_cu_data *per_cu
168c9250 5169 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5170 sect_off_prev, length);
168c9250 5171 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5172 }
5173 sect_off_prev = sect_off_next;
5174 }
5175}
5176
5177/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5178 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5179
5180static void
168c9250 5181create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5182 const mapped_debug_names &map,
5183 const mapped_debug_names &dwz_map)
5184{
168c9250
SM
5185 gdb_assert (per_bfd->all_comp_units.empty ());
5186 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5187
168c9250 5188 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5189 false /* is_dwz */);
927aa2e7
JK
5190
5191 if (dwz_map.cu_count == 0)
5192 return;
5193
168c9250
SM
5194 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5195 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5196 true /* is_dwz */);
927aa2e7
JK
5197}
5198
5199/* Read .debug_names. If everything went ok, initialize the "quick"
5200 elements of all the CUs and return true. Otherwise, return false. */
5201
5202static bool
976ca316 5203dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5204{
fcf23d5b
SM
5205 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5206 mapped_debug_names dwz_map;
976ca316 5207 struct objfile *objfile = per_objfile->objfile;
927aa2e7
JK
5208
5209 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5210 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5211 return false;
5212
5213 /* Don't use the index if it's empty. */
22ca247e 5214 if (map->name_count == 0)
927aa2e7
JK
5215 return false;
5216
5217 /* If there is a .dwz file, read it so we can get its CU list as
5218 well. */
976ca316 5219 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
927aa2e7
JK
5220 if (dwz != NULL)
5221 {
5222 if (!read_debug_names_from_section (objfile,
00f93c44 5223 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5224 &dwz->debug_names, dwz_map))
5225 {
5226 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5227 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5228 return false;
5229 }
5230 }
5231
976ca316 5232 create_cus_from_debug_names (per_objfile->per_bfd, *map, dwz_map);
927aa2e7 5233
22ca247e 5234 if (map->tu_count != 0)
927aa2e7
JK
5235 {
5236 /* We can only handle a single .debug_types when we have an
5237 index. */
976ca316 5238 if (per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5239 return false;
5240
976ca316 5241 dwarf2_section_info *section = &per_objfile->per_bfd->types[0];
927aa2e7
JK
5242
5243 create_signatured_type_table_from_debug_names
976ca316 5244 (per_objfile, *map, section, &per_objfile->per_bfd->abbrev);
927aa2e7
JK
5245 }
5246
976ca316
SM
5247 create_addrmap_from_aranges (per_objfile,
5248 &per_objfile->per_bfd->debug_aranges);
927aa2e7 5249
976ca316
SM
5250 per_objfile->per_bfd->debug_names_table = std::move (map);
5251 per_objfile->per_bfd->using_index = 1;
5252 per_objfile->per_bfd->quick_file_names_table =
5253 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5254
5255 return true;
5256}
5257
927aa2e7
JK
5258/* Type used to manage iterating over all CUs looking for a symbol for
5259 .debug_names. */
5260
5261class dw2_debug_names_iterator
5262{
5263public:
927aa2e7 5264 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5265 gdb::optional<block_enum> block_index,
5266 domain_enum domain,
fcf23d5b 5267 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5268 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5269 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5270 m_per_objfile (per_objfile)
927aa2e7
JK
5271 {}
5272
5273 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5274 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5275 : m_map (map),
5276 m_search (search),
fcf23d5b
SM
5277 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5278 m_per_objfile (per_objfile)
927aa2e7
JK
5279 {}
5280
3b00ef10
TT
5281 dw2_debug_names_iterator (const mapped_debug_names &map,
5282 block_enum block_index, domain_enum domain,
fcf23d5b 5283 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5284 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5285 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5286 m_per_objfile (per_objfile)
3b00ef10
TT
5287 {}
5288
927aa2e7
JK
5289 /* Return the next matching CU or NULL if there are no more. */
5290 dwarf2_per_cu_data *next ();
5291
5292private:
5293 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5294 const char *name,
5295 dwarf2_per_objfile *per_objfile);
927aa2e7 5296 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5297 uint32_t namei,
5298 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5299
5300 /* The internalized form of .debug_names. */
5301 const mapped_debug_names &m_map;
5302
2b79f376
SM
5303 /* If set, only look for symbols that match that block. Valid values are
5304 GLOBAL_BLOCK and STATIC_BLOCK. */
5305 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5306
5307 /* The kind of symbol we're looking for. */
5308 const domain_enum m_domain = UNDEF_DOMAIN;
5309 const search_domain m_search = ALL_DOMAIN;
5310
5311 /* The list of CUs from the index entry of the symbol, or NULL if
5312 not found. */
5313 const gdb_byte *m_addr;
fcf23d5b
SM
5314
5315 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5316};
5317
5318const char *
fcf23d5b 5319mapped_debug_names::namei_to_name
976ca316 5320 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5321{
5322 const ULONGEST namei_string_offs
5323 = extract_unsigned_integer ((name_table_string_offs_reordered
5324 + namei * offset_size),
5325 offset_size,
5326 dwarf5_byte_order);
976ca316 5327 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5328}
5329
5330/* Find a slot in .debug_names for the object named NAME. If NAME is
5331 found, return pointer to its pool data. If NAME cannot be found,
5332 return NULL. */
5333
5334const gdb_byte *
5335dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5336 (const mapped_debug_names &map, const char *name,
5337 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5338{
5339 int (*cmp) (const char *, const char *);
5340
54ee4252 5341 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5342 if (current_language->la_language == language_cplus
5343 || current_language->la_language == language_fortran
5344 || current_language->la_language == language_d)
5345 {
5346 /* NAME is already canonical. Drop any qualifiers as
5347 .debug_names does not contain any. */
5348
5349 if (strchr (name, '(') != NULL)
5350 {
54ee4252 5351 without_params = cp_remove_params (name);
927aa2e7 5352 if (without_params != NULL)
54ee4252 5353 name = without_params.get ();
927aa2e7
JK
5354 }
5355 }
5356
5357 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5358
5359 const uint32_t full_hash = dwarf5_djb_hash (name);
5360 uint32_t namei
5361 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5362 (map.bucket_table_reordered
5363 + (full_hash % map.bucket_count)), 4,
5364 map.dwarf5_byte_order);
5365 if (namei == 0)
5366 return NULL;
5367 --namei;
5368 if (namei >= map.name_count)
5369 {
b98664d3 5370 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5371 "[in module %s]"),
5372 namei, map.name_count,
fcf23d5b 5373 objfile_name (per_objfile->objfile));
927aa2e7
JK
5374 return NULL;
5375 }
5376
5377 for (;;)
5378 {
5379 const uint32_t namei_full_hash
5380 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5381 (map.hash_table_reordered + namei), 4,
5382 map.dwarf5_byte_order);
5383 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5384 return NULL;
5385
5386 if (full_hash == namei_full_hash)
5387 {
fcf23d5b 5388 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5389
5390#if 0 /* An expensive sanity check. */
5391 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5392 {
b98664d3 5393 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5394 "[in module %s]"),
5395 namei, objfile_name (dwarf2_per_objfile->objfile));
5396 return NULL;
5397 }
5398#endif
5399
5400 if (cmp (namei_string, name) == 0)
5401 {
5402 const ULONGEST namei_entry_offs
5403 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5404 + namei * map.offset_size),
5405 map.offset_size, map.dwarf5_byte_order);
5406 return map.entry_pool + namei_entry_offs;
5407 }
5408 }
5409
5410 ++namei;
5411 if (namei >= map.name_count)
5412 return NULL;
5413 }
5414}
5415
5416const gdb_byte *
5417dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5418 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5419{
5420 if (namei >= map.name_count)
5421 {
b98664d3 5422 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5423 "[in module %s]"),
5424 namei, map.name_count,
fcf23d5b 5425 objfile_name (per_objfile->objfile));
927aa2e7
JK
5426 return NULL;
5427 }
5428
5429 const ULONGEST namei_entry_offs
5430 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5431 + namei * map.offset_size),
5432 map.offset_size, map.dwarf5_byte_order);
5433 return map.entry_pool + namei_entry_offs;
5434}
5435
5436/* See dw2_debug_names_iterator. */
5437
5438dwarf2_per_cu_data *
5439dw2_debug_names_iterator::next ()
5440{
5441 if (m_addr == NULL)
5442 return NULL;
5443
fcf23d5b
SM
5444 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5445 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5446 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5447
5448 again:
5449
5450 unsigned int bytes_read;
5451 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5452 m_addr += bytes_read;
5453 if (abbrev == 0)
5454 return NULL;
5455
5456 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5457 if (indexval_it == m_map.abbrev_map.cend ())
5458 {
b98664d3 5459 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5460 "[in module %s]"),
ed2dc618 5461 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5462 return NULL;
5463 }
5464 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5465 enum class symbol_linkage {
5466 unknown,
5467 static_,
5468 extern_,
23c13d42 5469 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5470 dwarf2_per_cu_data *per_cu = NULL;
5471 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5472 {
5473 ULONGEST ull;
5474 switch (attr.form)
5475 {
5476 case DW_FORM_implicit_const:
5477 ull = attr.implicit_const;
5478 break;
5479 case DW_FORM_flag_present:
5480 ull = 1;
5481 break;
5482 case DW_FORM_udata:
5483 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5484 m_addr += bytes_read;
5485 break;
6dc55ce9 5486 case DW_FORM_ref4:
5487 ull = read_4_bytes (abfd, m_addr);
5488 m_addr += 4;
5489 break;
5490 case DW_FORM_ref8:
5491 ull = read_8_bytes (abfd, m_addr);
5492 m_addr += 8;
5493 break;
5494 case DW_FORM_ref_sig8:
5495 ull = read_8_bytes (abfd, m_addr);
5496 m_addr += 8;
5497 break;
927aa2e7 5498 default:
b98664d3 5499 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5500 dwarf_form_name (attr.form),
ed2dc618 5501 objfile_name (objfile));
927aa2e7
JK
5502 return NULL;
5503 }
5504 switch (attr.dw_idx)
5505 {
5506 case DW_IDX_compile_unit:
5507 /* Don't crash on bad data. */
fcf23d5b 5508 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5509 {
b98664d3 5510 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5511 " [in module %s]"),
5512 pulongest (ull),
fcf23d5b 5513 objfile_name (objfile));
927aa2e7
JK
5514 continue;
5515 }
fcf23d5b 5516 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5517 break;
8af5c486
JK
5518 case DW_IDX_type_unit:
5519 /* Don't crash on bad data. */
fcf23d5b 5520 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5521 {
b98664d3 5522 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5523 " [in module %s]"),
5524 pulongest (ull),
fcf23d5b 5525 objfile_name (objfile));
8af5c486
JK
5526 continue;
5527 }
fcf23d5b 5528 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5529 break;
6dc55ce9 5530 case DW_IDX_die_offset:
5531 /* In a per-CU index (as opposed to a per-module index), index
5532 entries without CU attribute implicitly refer to the single CU. */
5533 if (per_cu == NULL)
fcf23d5b 5534 per_cu = per_bfd->get_cu (0);
6dc55ce9 5535 break;
927aa2e7
JK
5536 case DW_IDX_GNU_internal:
5537 if (!m_map.augmentation_is_gdb)
5538 break;
23c13d42 5539 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5540 break;
5541 case DW_IDX_GNU_external:
5542 if (!m_map.augmentation_is_gdb)
5543 break;
23c13d42 5544 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5545 break;
5546 }
5547 }
5548
5549 /* Skip if already read in. */
fcf23d5b 5550 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5551 goto again;
5552
5553 /* Check static vs global. */
23c13d42 5554 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5555 {
2b79f376 5556 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5557 const bool symbol_is_static =
5558 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5559 if (want_static != symbol_is_static)
2b79f376 5560 goto again;
927aa2e7
JK
5561 }
5562
5563 /* Match dw2_symtab_iter_next, symbol_kind
5564 and debug_names::psymbol_tag. */
5565 switch (m_domain)
5566 {
5567 case VAR_DOMAIN:
5568 switch (indexval.dwarf_tag)
5569 {
5570 case DW_TAG_variable:
5571 case DW_TAG_subprogram:
5572 /* Some types are also in VAR_DOMAIN. */
5573 case DW_TAG_typedef:
5574 case DW_TAG_structure_type:
5575 break;
5576 default:
5577 goto again;
5578 }
5579 break;
5580 case STRUCT_DOMAIN:
5581 switch (indexval.dwarf_tag)
5582 {
5583 case DW_TAG_typedef:
5584 case DW_TAG_structure_type:
5585 break;
5586 default:
5587 goto again;
5588 }
5589 break;
5590 case LABEL_DOMAIN:
5591 switch (indexval.dwarf_tag)
5592 {
5593 case 0:
5594 case DW_TAG_variable:
5595 break;
5596 default:
5597 goto again;
5598 }
5599 break;
59c35742
AB
5600 case MODULE_DOMAIN:
5601 switch (indexval.dwarf_tag)
5602 {
5603 case DW_TAG_module:
5604 break;
5605 default:
5606 goto again;
5607 }
5608 break;
927aa2e7
JK
5609 default:
5610 break;
5611 }
5612
5613 /* Match dw2_expand_symtabs_matching, symbol_kind and
5614 debug_names::psymbol_tag. */
5615 switch (m_search)
4b514bc8 5616 {
927aa2e7
JK
5617 case VARIABLES_DOMAIN:
5618 switch (indexval.dwarf_tag)
4b514bc8 5619 {
927aa2e7
JK
5620 case DW_TAG_variable:
5621 break;
5622 default:
5623 goto again;
4b514bc8 5624 }
927aa2e7
JK
5625 break;
5626 case FUNCTIONS_DOMAIN:
5627 switch (indexval.dwarf_tag)
4b514bc8 5628 {
927aa2e7
JK
5629 case DW_TAG_subprogram:
5630 break;
5631 default:
5632 goto again;
4b514bc8 5633 }
927aa2e7
JK
5634 break;
5635 case TYPES_DOMAIN:
5636 switch (indexval.dwarf_tag)
5637 {
5638 case DW_TAG_typedef:
5639 case DW_TAG_structure_type:
5640 break;
5641 default:
5642 goto again;
5643 }
5644 break;
59c35742
AB
5645 case MODULES_DOMAIN:
5646 switch (indexval.dwarf_tag)
5647 {
5648 case DW_TAG_module:
5649 break;
5650 default:
5651 goto again;
5652 }
927aa2e7
JK
5653 default:
5654 break;
4b514bc8 5655 }
927aa2e7
JK
5656
5657 return per_cu;
4b514bc8 5658}
61920122 5659
927aa2e7 5660static struct compunit_symtab *
c7f839cb 5661dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5662 const char *name, domain_enum domain)
4b514bc8 5663{
976ca316 5664 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5665
976ca316 5666 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5667 if (!mapp)
61920122 5668 {
927aa2e7
JK
5669 /* index is NULL if OBJF_READNOW. */
5670 return NULL;
5671 }
5672 const auto &map = *mapp;
9291a0cd 5673
976ca316 5674 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5675
927aa2e7
JK
5676 struct compunit_symtab *stab_best = NULL;
5677 struct dwarf2_per_cu_data *per_cu;
5678 while ((per_cu = iter.next ()) != NULL)
5679 {
5680 struct symbol *sym, *with_opaque = NULL;
97a1449a 5681 compunit_symtab *stab
976ca316 5682 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5683 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5684 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5685
927aa2e7
JK
5686 sym = block_find_symbol (block, name, domain,
5687 block_find_non_opaque_type_preferred,
5688 &with_opaque);
9703b513 5689
927aa2e7
JK
5690 /* Some caution must be observed with overloaded functions and
5691 methods, since the index will not contain any overload
5692 information (but NAME might contain it). */
a3ec0bb1 5693
927aa2e7 5694 if (sym != NULL
987012b8 5695 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5696 return stab;
5697 if (with_opaque != NULL
987012b8 5698 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5699 stab_best = stab;
9703b513 5700
927aa2e7 5701 /* Keep looking through other CUs. */
9703b513
TT
5702 }
5703
927aa2e7 5704 return stab_best;
9703b513
TT
5705}
5706
927aa2e7
JK
5707/* This dumps minimal information about .debug_names. It is called
5708 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5709 uses this to verify that .debug_names has been loaded. */
9291a0cd 5710
927aa2e7
JK
5711static void
5712dw2_debug_names_dump (struct objfile *objfile)
5713{
976ca316 5714 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5715
976ca316 5716 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5717 printf_filtered (".debug_names:");
976ca316 5718 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5719 printf_filtered (" exists\n");
5720 else
5721 printf_filtered (" faked for \"readnow\"\n");
5722 printf_filtered ("\n");
9291a0cd
TT
5723}
5724
9291a0cd 5725static void
927aa2e7
JK
5726dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5727 const char *func_name)
9291a0cd 5728{
976ca316 5729 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5730
976ca316
SM
5731 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5732 if (per_objfile->per_bfd->debug_names_table)
24c79950 5733 {
976ca316 5734 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5735
fcf23d5b 5736 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5737 per_objfile);
24c79950 5738
927aa2e7
JK
5739 struct dwarf2_per_cu_data *per_cu;
5740 while ((per_cu = iter.next ()) != NULL)
976ca316 5741 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5742 }
5743}
24c79950 5744
3b00ef10
TT
5745static void
5746dw2_debug_names_map_matching_symbols
5747 (struct objfile *objfile,
5748 const lookup_name_info &name, domain_enum domain,
5749 int global,
5750 gdb::function_view<symbol_found_callback_ftype> callback,
5751 symbol_compare_ftype *ordered_compare)
5752{
976ca316 5753 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5754
5755 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5756 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5757 return;
5758
976ca316 5759 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5760 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5761
5762 const char *match_name = name.ada ().lookup_name ().c_str ();
5763 auto matcher = [&] (const char *symname)
5764 {
5765 if (ordered_compare == nullptr)
5766 return true;
5767 return ordered_compare (symname, match_name) == 0;
5768 };
5769
5770 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5771 [&] (offset_type namei)
5772 {
5773 /* The name was matched, now expand corresponding CUs that were
5774 marked. */
fcf23d5b 5775 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5776 per_objfile);
3b00ef10
TT
5777
5778 struct dwarf2_per_cu_data *per_cu;
5779 while ((per_cu = iter.next ()) != NULL)
976ca316 5780 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5781 nullptr);
3b00ef10 5782 return true;
976ca316 5783 }, per_objfile);
3b00ef10
TT
5784
5785 /* It's a shame we couldn't do this inside the
5786 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5787 that have already been expanded. Instead, this loop matches what
5788 the psymtab code does. */
976ca316 5789 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5790 {
976ca316 5791 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5792 if (symtab != nullptr)
3b00ef10
TT
5793 {
5794 const struct block *block
af758d11 5795 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5796 if (!iterate_over_symbols_terminated (block, name,
5797 domain, callback))
5798 break;
5799 }
5800 }
5801}
5802
927aa2e7
JK
5803static void
5804dw2_debug_names_expand_symtabs_matching
5805 (struct objfile *objfile,
5806 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5807 const lookup_name_info *lookup_name,
927aa2e7
JK
5808 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5809 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5810 enum search_domain kind)
5811{
976ca316 5812 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5813
927aa2e7 5814 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5815 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5816 return;
9291a0cd 5817
976ca316 5818 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5819
c1a66c06
TV
5820 if (symbol_matcher == NULL && lookup_name == NULL)
5821 {
976ca316 5822 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5823 {
5824 QUIT;
5825
976ca316
SM
5826 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5827 expansion_notify);
c1a66c06
TV
5828 }
5829 return;
5830 }
5831
976ca316 5832 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5833
c1a66c06 5834 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5835 symbol_matcher,
5836 kind, [&] (offset_type namei)
927aa2e7 5837 {
927aa2e7
JK
5838 /* The name was matched, now expand corresponding CUs that were
5839 marked. */
976ca316 5840 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5841
927aa2e7
JK
5842 struct dwarf2_per_cu_data *per_cu;
5843 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5844 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5845 expansion_notify);
3b00ef10 5846 return true;
976ca316 5847 }, per_objfile);
9291a0cd
TT
5848}
5849
927aa2e7 5850const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5851{
5852 dw2_has_symbols,
5853 dw2_find_last_source_symtab,
5854 dw2_forget_cached_source_info,
f8eba3c6 5855 dw2_map_symtabs_matching_filename,
927aa2e7 5856 dw2_debug_names_lookup_symbol,
d3214198 5857 NULL,
9291a0cd 5858 dw2_print_stats,
927aa2e7 5859 dw2_debug_names_dump,
927aa2e7 5860 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5861 dw2_expand_all_symtabs,
652a8996 5862 dw2_expand_symtabs_with_fullname,
3b00ef10 5863 dw2_debug_names_map_matching_symbols,
927aa2e7 5864 dw2_debug_names_expand_symtabs_matching,
43f3e411 5865 dw2_find_pc_sect_compunit_symtab,
71a3c369 5866 NULL,
9291a0cd
TT
5867 dw2_map_symbol_filenames
5868};
5869
4485a1c1 5870/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5871 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5872
5873template <typename T>
5874static gdb::array_view<const gdb_byte>
5875get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5876{
5877 dwarf2_section_info *section = &section_owner->gdb_index;
5878
96b79293 5879 if (section->empty ())
4485a1c1
SM
5880 return {};
5881
5882 /* Older elfutils strip versions could keep the section in the main
5883 executable while splitting it for the separate debug info file. */
96b79293 5884 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5885 return {};
5886
96b79293 5887 section->read (obj);
4485a1c1 5888
8bebfcda
PA
5889 /* dwarf2_section_info::size is a bfd_size_type, while
5890 gdb::array_view works with size_t. On 32-bit hosts, with
5891 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5892 is 32-bit. So we need an explicit narrowing conversion here.
5893 This is fine, because it's impossible to allocate or mmap an
5894 array/buffer larger than what size_t can represent. */
5895 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5896}
5897
87d6a7aa
SM
5898/* Lookup the index cache for the contents of the index associated to
5899 DWARF2_OBJ. */
5900
5901static gdb::array_view<const gdb_byte>
5989a64e 5902get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5903{
5904 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5905 if (build_id == nullptr)
5906 return {};
5907
5908 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5909 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5910}
5911
5912/* Same as the above, but for DWZ. */
5913
5914static gdb::array_view<const gdb_byte>
5915get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5916{
5917 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5918 if (build_id == nullptr)
5919 return {};
5920
5921 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5922}
5923
3c0aa29a 5924/* See symfile.h. */
9291a0cd 5925
3c0aa29a
PA
5926bool
5927dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5928{
976ca316
SM
5929 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5930 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5931
9291a0cd
TT
5932 /* If we're about to read full symbols, don't bother with the
5933 indices. In this case we also don't care if some other debug
5934 format is making psymtabs, because they are all about to be
5935 expanded anyway. */
5936 if ((objfile->flags & OBJF_READNOW))
5937 {
17ee85fc
TT
5938 /* When using READNOW, the using_index flag (set below) indicates that
5939 PER_BFD was already initialized, when we loaded some other objfile. */
5940 if (per_bfd->using_index)
5941 {
5942 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 5943 per_objfile->resize_symtabs ();
17ee85fc
TT
5944 return true;
5945 }
5946
5947 per_bfd->using_index = 1;
976ca316
SM
5948 create_all_comp_units (per_objfile);
5949 create_all_type_units (per_objfile);
17ee85fc
TT
5950 per_bfd->quick_file_names_table
5951 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5952 per_objfile->resize_symtabs ();
9291a0cd 5953
17ee85fc
TT
5954 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5955 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 5956 {
17ee85fc 5957 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 5958
17ee85fc 5959 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5960 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5961 }
5962
5963 /* Return 1 so that gdb sees the "quick" functions. However,
5964 these functions will be no-ops because we will have expanded
5965 all symtabs. */
3c0aa29a
PA
5966 *index_kind = dw_index_kind::GDB_INDEX;
5967 return true;
9291a0cd
TT
5968 }
5969
17ee85fc
TT
5970 /* Was a debug names index already read when we processed an objfile sharing
5971 PER_BFD? */
5972 if (per_bfd->debug_names_table != nullptr)
5973 {
5974 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 5975 per_objfile->resize_symtabs ();
17ee85fc
TT
5976 return true;
5977 }
5978
5979 /* Was a GDB index already read when we processed an objfile sharing
5980 PER_BFD? */
5981 if (per_bfd->index_table != nullptr)
5982 {
5983 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 5984 per_objfile->resize_symtabs ();
17ee85fc
TT
5985 return true;
5986 }
5987
976ca316 5988 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
5989 {
5990 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 5991 per_objfile->resize_symtabs ();
3c0aa29a
PA
5992 return true;
5993 }
927aa2e7 5994
976ca316 5995 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5996 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5997 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5998 {
5999 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6000 per_objfile->resize_symtabs ();
3c0aa29a
PA
6001 return true;
6002 }
9291a0cd 6003
87d6a7aa 6004 /* ... otherwise, try to find the index in the index cache. */
976ca316 6005 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6006 get_gdb_index_contents_from_cache,
6007 get_gdb_index_contents_from_cache_dwz))
6008 {
6009 global_index_cache.hit ();
6010 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6011 per_objfile->resize_symtabs ();
87d6a7aa
SM
6012 return true;
6013 }
6014
6015 global_index_cache.miss ();
3c0aa29a 6016 return false;
9291a0cd
TT
6017}
6018
6019\f
6020
dce234bc
PP
6021/* Build a partial symbol table. */
6022
6023void
f29dff0a 6024dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6025{
976ca316
SM
6026 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6027 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6028
6029 if (per_bfd->partial_symtabs != nullptr)
6030 {
6031 /* Partial symbols were already read, so now we can simply
6032 attach them. */
6033 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6034 per_objfile->resize_symtabs ();
17ee85fc
TT
6035 return;
6036 }
c9bf0622 6037
6eee24ce 6038 init_psymbol_list (objfile, 1024);
c906108c 6039
a70b8144 6040 try
c9bf0622
TT
6041 {
6042 /* This isn't really ideal: all the data we allocate on the
6043 objfile's obstack is still uselessly kept around. However,
6044 freeing it seems unsafe. */
906768f9 6045 psymtab_discarder psymtabs (objfile);
976ca316 6046 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6047 psymtabs.keep ();
87d6a7aa 6048
976ca316 6049 per_objfile->resize_symtabs ();
af758d11 6050
87d6a7aa 6051 /* (maybe) store an index in the cache. */
976ca316 6052 global_index_cache.store (per_objfile);
c9bf0622 6053 }
230d2906 6054 catch (const gdb_exception_error &except)
492d29ea
PA
6055 {
6056 exception_print (gdb_stderr, except);
6057 }
17ee85fc
TT
6058
6059 /* Finish by setting the local reference to partial symtabs, so that
6060 we don't try to read them again if reading another objfile with the same
6061 BFD. If we can't in fact share, this won't make a difference anyway as
6062 the dwarf2_per_bfd object won't be shared. */
6063 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6064}
c906108c 6065
3b80fe9b
DE
6066/* Find the base address of the compilation unit for range lists and
6067 location lists. It will normally be specified by DW_AT_low_pc.
6068 In DWARF-3 draft 4, the base address could be overridden by
6069 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6070 compilation units with discontinuous ranges. */
6071
6072static void
6073dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6074{
6075 struct attribute *attr;
6076
2b24b6e4 6077 cu->base_address.reset ();
3b80fe9b
DE
6078
6079 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6080 if (attr != nullptr)
2b24b6e4 6081 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6082 else
6083 {
6084 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6085 if (attr != nullptr)
2b24b6e4 6086 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6087 }
6088}
6089
36586728
TT
6090/* Helper function that returns the proper abbrev section for
6091 THIS_CU. */
6092
6093static struct dwarf2_section_info *
6094get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6095{
6096 struct dwarf2_section_info *abbrev;
c3699833 6097 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6098
6099 if (this_cu->is_dwz)
c3699833 6100 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6101 else
c3699833 6102 abbrev = &per_bfd->abbrev;
36586728
TT
6103
6104 return abbrev;
6105}
6106
f4dc4d17
DE
6107/* Fetch the abbreviation table offset from a comp or type unit header. */
6108
6109static sect_offset
976ca316 6110read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6111 struct dwarf2_section_info *section,
9c541725 6112 sect_offset sect_off)
f4dc4d17 6113{
96b79293 6114 bfd *abfd = section->get_bfd_owner ();
d521ce57 6115 const gdb_byte *info_ptr;
ac298888 6116 unsigned int initial_length_size, offset_size;
43988095 6117 uint16_t version;
f4dc4d17 6118
976ca316 6119 section->read (per_objfile->objfile);
9c541725 6120 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6121 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6122 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6123 info_ptr += initial_length_size;
6124
6125 version = read_2_bytes (abfd, info_ptr);
6126 info_ptr += 2;
6127 if (version >= 5)
6128 {
6129 /* Skip unit type and address size. */
6130 info_ptr += 2;
6131 }
6132
24aa364d 6133 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6134}
6135
b83470bf
TT
6136/* A partial symtab that is used only for include files. */
6137struct dwarf2_include_psymtab : public partial_symtab
6138{
6139 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6140 : partial_symtab (filename, objfile)
6141 {
6142 }
6143
6144 void read_symtab (struct objfile *objfile) override
6145 {
194d088f
TV
6146 /* It's an include file, no symbols to read for it.
6147 Everything is in the includer symtab. */
6148
6149 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6150 expansion of the includer psymtab. We use the dependencies[0] field to
6151 model the includer. But if we go the regular route of calling
6152 expand_psymtab here, and having expand_psymtab call expand_dependencies
6153 to expand the includer, we'll only use expand_psymtab on the includer
6154 (making it a non-toplevel psymtab), while if we expand the includer via
6155 another path, we'll use read_symtab (making it a toplevel psymtab).
6156 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6157 psymtab, and trigger read_symtab on the includer here directly. */
6158 includer ()->read_symtab (objfile);
b83470bf
TT
6159 }
6160
6161 void expand_psymtab (struct objfile *objfile) override
6162 {
194d088f
TV
6163 /* This is not called by read_symtab, and should not be called by any
6164 expand_dependencies. */
6165 gdb_assert (false);
b83470bf
TT
6166 }
6167
5717c425 6168 bool readin_p (struct objfile *objfile) const override
b83470bf 6169 {
5717c425 6170 return includer ()->readin_p (objfile);
b83470bf
TT
6171 }
6172
5717c425 6173 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6174 {
6175 return nullptr;
6176 }
6177
6178private:
194d088f
TV
6179 partial_symtab *includer () const
6180 {
6181 /* An include psymtab has exactly one dependency: the psymtab that
6182 includes it. */
6183 gdb_assert (this->number_of_dependencies == 1);
6184 return this->dependencies[0];
6185 }
b83470bf
TT
6186};
6187
aaa75496
JB
6188/* Allocate a new partial symtab for file named NAME and mark this new
6189 partial symtab as being an include of PST. */
6190
6191static void
891813be 6192dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6193 struct objfile *objfile)
6194{
b83470bf 6195 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6196
fbd9ab74 6197 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6198 subpst->dirname = pst->dirname;
fbd9ab74 6199
a9342b62 6200 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6201 subpst->dependencies[0] = pst;
6202 subpst->number_of_dependencies = 1;
aaa75496
JB
6203}
6204
6205/* Read the Line Number Program data and extract the list of files
6206 included by the source file represented by PST. Build an include
d85a05f0 6207 partial symtab for each of these included files. */
aaa75496
JB
6208
6209static void
6210dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6211 struct die_info *die,
891813be 6212 dwarf2_psymtab *pst)
aaa75496 6213{
fff8551c 6214 line_header_up lh;
d85a05f0 6215 struct attribute *attr;
aaa75496 6216
d85a05f0 6217 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6218 if (attr != nullptr)
9c541725 6219 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6220 if (lh == NULL)
6221 return; /* No linetable, so no includes. */
6222
79748972
TT
6223 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6224 that we pass in the raw text_low here; that is ok because we're
6225 only decoding the line table to make include partial symtabs, and
6226 so the addresses aren't really used. */
4ae976d1 6227 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6228 pst->raw_text_low (), 1);
aaa75496
JB
6229}
6230
348e048f 6231static hashval_t
52dc124a 6232hash_signatured_type (const void *item)
348e048f 6233{
9a3c8263
SM
6234 const struct signatured_type *sig_type
6235 = (const struct signatured_type *) item;
9a619af0 6236
348e048f 6237 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6238 return sig_type->signature;
348e048f
DE
6239}
6240
6241static int
52dc124a 6242eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6243{
9a3c8263
SM
6244 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6245 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6246
348e048f
DE
6247 return lhs->signature == rhs->signature;
6248}
6249
1fd400ff
TT
6250/* Allocate a hash table for signatured types. */
6251
b0b6a987 6252static htab_up
298e9637 6253allocate_signatured_type_table ()
1fd400ff 6254{
b0b6a987
TT
6255 return htab_up (htab_create_alloc (41,
6256 hash_signatured_type,
6257 eq_signatured_type,
6258 NULL, xcalloc, xfree));
1fd400ff
TT
6259}
6260
d467dd73 6261/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6262
6263static int
d467dd73 6264add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6265{
9a3c8263 6266 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6267 std::vector<signatured_type *> *all_type_units
6268 = (std::vector<signatured_type *> *) datum;
1fd400ff 6269
b2bdb8cf 6270 all_type_units->push_back (sigt);
1fd400ff
TT
6271
6272 return 1;
6273}
6274
78d4d2c5 6275/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6276 and fill them into TYPES_HTAB. It will process only type units,
6277 therefore DW_UT_type. */
c88ee1f0 6278
78d4d2c5 6279static void
976ca316 6280create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6281 struct dwo_file *dwo_file,
b0b6a987 6282 dwarf2_section_info *section, htab_up &types_htab,
43988095 6283 rcuh_kind section_kind)
348e048f 6284{
976ca316 6285 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6286 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6287 bfd *abfd;
6288 const gdb_byte *info_ptr, *end_ptr;
348e048f 6289
4bdcc0c1
DE
6290 abbrev_section = (dwo_file != NULL
6291 ? &dwo_file->sections.abbrev
976ca316 6292 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6293
b4f54984 6294 if (dwarf_read_debug)
43988095 6295 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6296 section->get_name (),
6297 abbrev_section->get_file_name ());
09406207 6298
96b79293 6299 section->read (objfile);
78d4d2c5 6300 info_ptr = section->buffer;
348e048f 6301
78d4d2c5
JK
6302 if (info_ptr == NULL)
6303 return;
348e048f 6304
78d4d2c5
JK
6305 /* We can't set abfd until now because the section may be empty or
6306 not present, in which case the bfd is unknown. */
96b79293 6307 abfd = section->get_bfd_owner ();
348e048f 6308
c0ab21c2
TT
6309 /* We don't use cutu_reader here because we don't need to read
6310 any dies: the signature is in the header. */
3019eac3 6311
78d4d2c5
JK
6312 end_ptr = info_ptr + section->size;
6313 while (info_ptr < end_ptr)
6314 {
78d4d2c5
JK
6315 struct signatured_type *sig_type;
6316 struct dwo_unit *dwo_tu;
6317 void **slot;
6318 const gdb_byte *ptr = info_ptr;
6319 struct comp_unit_head header;
6320 unsigned int length;
8b70b953 6321
9c541725 6322 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6323
a49dd8dd
JK
6324 /* Initialize it due to a false compiler warning. */
6325 header.signature = -1;
9c541725 6326 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6327
78d4d2c5
JK
6328 /* We need to read the type's signature in order to build the hash
6329 table, but we don't need anything else just yet. */
348e048f 6330
976ca316 6331 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6332 abbrev_section, ptr, section_kind);
348e048f 6333
4057dfde 6334 length = header.get_length ();
6caca83c 6335
78d4d2c5
JK
6336 /* Skip dummy type units. */
6337 if (ptr >= info_ptr + length
43988095
JK
6338 || peek_abbrev_code (abfd, ptr) == 0
6339 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6340 {
6341 info_ptr += length;
6342 continue;
6343 }
dee91e82 6344
78d4d2c5
JK
6345 if (types_htab == NULL)
6346 {
6347 if (dwo_file)
298e9637 6348 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6349 else
298e9637 6350 types_htab = allocate_signatured_type_table ();
78d4d2c5 6351 }
8b70b953 6352
78d4d2c5
JK
6353 if (dwo_file)
6354 {
6355 sig_type = NULL;
976ca316 6356 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6357 dwo_tu->dwo_file = dwo_file;
43988095 6358 dwo_tu->signature = header.signature;
9c541725 6359 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6360 dwo_tu->section = section;
9c541725 6361 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6362 dwo_tu->length = length;
6363 }
6364 else
6365 {
6366 /* N.B.: type_offset is not usable if this type uses a DWO file.
6367 The real type_offset is in the DWO file. */
6368 dwo_tu = NULL;
976ca316 6369 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6370 sig_type->signature = header.signature;
9c541725 6371 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6372 sig_type->per_cu.is_debug_types = 1;
6373 sig_type->per_cu.section = section;
9c541725 6374 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6375 sig_type->per_cu.length = length;
6376 }
6377
b0b6a987 6378 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6379 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6380 INSERT);
6381 gdb_assert (slot != NULL);
6382 if (*slot != NULL)
6383 {
9c541725 6384 sect_offset dup_sect_off;
0349ea22 6385
3019eac3
DE
6386 if (dwo_file)
6387 {
78d4d2c5
JK
6388 const struct dwo_unit *dup_tu
6389 = (const struct dwo_unit *) *slot;
6390
9c541725 6391 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6392 }
6393 else
6394 {
78d4d2c5
JK
6395 const struct signatured_type *dup_tu
6396 = (const struct signatured_type *) *slot;
6397
9c541725 6398 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6399 }
8b70b953 6400
b98664d3 6401 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6402 " the entry at offset %s, signature %s"),
6403 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6404 hex_string (header.signature));
78d4d2c5
JK
6405 }
6406 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6407
78d4d2c5 6408 if (dwarf_read_debug > 1)
9d8780f0
SM
6409 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6410 sect_offset_str (sect_off),
43988095 6411 hex_string (header.signature));
3019eac3 6412
78d4d2c5
JK
6413 info_ptr += length;
6414 }
6415}
3019eac3 6416
78d4d2c5
JK
6417/* Create the hash table of all entries in the .debug_types
6418 (or .debug_types.dwo) section(s).
6419 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6420 otherwise it is NULL.
b3c8eb43 6421
78d4d2c5 6422 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6423
78d4d2c5 6424 Note: This function processes DWO files only, not DWP files. */
348e048f 6425
78d4d2c5 6426static void
976ca316 6427create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6428 struct dwo_file *dwo_file,
fd5866f6 6429 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6430 htab_up &types_htab)
78d4d2c5 6431{
fd5866f6 6432 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6433 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6434 rcuh_kind::TYPE);
3019eac3
DE
6435}
6436
6437/* Create the hash table of all entries in the .debug_types section,
6438 and initialize all_type_units.
6439 The result is zero if there is an error (e.g. missing .debug_types section),
6440 otherwise non-zero. */
6441
6442static int
976ca316 6443create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6444{
b0b6a987 6445 htab_up types_htab;
3019eac3 6446
976ca316
SM
6447 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6448 types_htab, rcuh_kind::COMPILE);
6449 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6450 types_htab);
3019eac3
DE
6451 if (types_htab == NULL)
6452 {
976ca316 6453 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6454 return 0;
6455 }
6456
976ca316 6457 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6458
976ca316
SM
6459 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6460 per_objfile->per_bfd->all_type_units.reserve
6461 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6462
976ca316 6463 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6464 add_signatured_type_cu_to_table,
976ca316 6465 &per_objfile->per_bfd->all_type_units);
1fd400ff 6466
348e048f
DE
6467 return 1;
6468}
6469
5989a64e 6470/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6471 If SLOT is non-NULL, it is the entry to use in the hash table.
6472 Otherwise we find one. */
6473
6474static struct signatured_type *
976ca316 6475add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6476{
976ca316
SM
6477 if (per_objfile->per_bfd->all_type_units.size ()
6478 == per_objfile->per_bfd->all_type_units.capacity ())
6479 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6480
976ca316 6481 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6482
976ca316 6483 per_objfile->resize_symtabs ();
af758d11 6484
976ca316 6485 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6486 sig_type->signature = sig;
6487 sig_type->per_cu.is_debug_types = 1;
976ca316 6488 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6489 {
6490 sig_type->per_cu.v.quick =
976ca316 6491 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6492 struct dwarf2_per_cu_quick_data);
6493 }
6494
6495 if (slot == NULL)
6496 {
976ca316 6497 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6498 sig_type, INSERT);
6499 }
6500 gdb_assert (*slot == NULL);
6501 *slot = sig_type;
6502 /* The rest of sig_type must be filled in by the caller. */
6503 return sig_type;
6504}
6505
a2ce51a0
DE
6506/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6507 Fill in SIG_ENTRY with DWO_ENTRY. */
6508
6509static void
976ca316 6510fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6511 struct signatured_type *sig_entry,
6512 struct dwo_unit *dwo_entry)
6513{
976ca316 6514 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6515
7ee85ab1 6516 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6517 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6518 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6519 if (per_bfd->using_index)
6aa5f3a6
DE
6520 {
6521 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6522 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6523 }
6524 else
6525 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6526 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6527 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6528 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6529 gdb_assert (sig_entry->dwo_unit == NULL);
6530
6531 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6532 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6533 sig_entry->per_cu.length = dwo_entry->length;
6534 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6535 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6536 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6537 sig_entry->dwo_unit = dwo_entry;
6538}
6539
6540/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6541 If we haven't read the TU yet, create the signatured_type data structure
6542 for a TU to be read in directly from a DWO file, bypassing the stub.
6543 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6544 using .gdb_index, then when reading a CU we want to stay in the DWO file
6545 containing that CU. Otherwise we could end up reading several other DWO
6546 files (due to comdat folding) to process the transitive closure of all the
6547 mentioned TUs, and that can be slow. The current DWO file will have every
6548 type signature that it needs.
a2ce51a0
DE
6549 We only do this for .gdb_index because in the psymtab case we already have
6550 to read all the DWOs to build the type unit groups. */
6551
6552static struct signatured_type *
6553lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6554{
976ca316 6555 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6556 struct dwo_file *dwo_file;
6557 struct dwo_unit find_dwo_entry, *dwo_entry;
6558 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6559 void **slot;
a2ce51a0 6560
976ca316 6561 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6562
6aa5f3a6
DE
6563 /* If TU skeletons have been removed then we may not have read in any
6564 TUs yet. */
976ca316
SM
6565 if (per_objfile->per_bfd->signatured_types == NULL)
6566 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6567
6568 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6569 Use the global signatured_types array to do our own comdat-folding
6570 of types. If this is the first time we're reading this TU, and
6571 the TU has an entry in .gdb_index, replace the recorded data from
6572 .gdb_index with this TU. */
a2ce51a0 6573
a2ce51a0 6574 find_sig_entry.signature = sig;
976ca316 6575 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6576 &find_sig_entry, INSERT);
9a3c8263 6577 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6578
6579 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6580 read. Don't reassign the global entry to point to this DWO if that's
6581 the case. Also note that if the TU is already being read, it may not
6582 have come from a DWO, the program may be a mix of Fission-compiled
6583 code and non-Fission-compiled code. */
6584
6585 /* Have we already tried to read this TU?
6586 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6587 needn't exist in the global table yet). */
6588 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6589 return sig_entry;
6590
6aa5f3a6
DE
6591 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6592 dwo_unit of the TU itself. */
6593 dwo_file = cu->dwo_unit->dwo_file;
6594
a2ce51a0
DE
6595 /* Ok, this is the first time we're reading this TU. */
6596 if (dwo_file->tus == NULL)
6597 return NULL;
6598 find_dwo_entry.signature = sig;
b0b6a987
TT
6599 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6600 &find_dwo_entry);
a2ce51a0
DE
6601 if (dwo_entry == NULL)
6602 return NULL;
6603
6aa5f3a6
DE
6604 /* If the global table doesn't have an entry for this TU, add one. */
6605 if (sig_entry == NULL)
976ca316 6606 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6607
976ca316 6608 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6609 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6610 return sig_entry;
6611}
6612
a2ce51a0
DE
6613/* Subroutine of lookup_signatured_type.
6614 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6615 then try the DWP file. If the TU stub (skeleton) has been removed then
6616 it won't be in .gdb_index. */
a2ce51a0
DE
6617
6618static struct signatured_type *
6619lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6620{
976ca316
SM
6621 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6622 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6623 struct dwo_unit *dwo_entry;
6624 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6625 void **slot;
a2ce51a0 6626
976ca316 6627 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6628 gdb_assert (dwp_file != NULL);
6629
6aa5f3a6
DE
6630 /* If TU skeletons have been removed then we may not have read in any
6631 TUs yet. */
976ca316
SM
6632 if (per_objfile->per_bfd->signatured_types == NULL)
6633 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6634
6aa5f3a6 6635 find_sig_entry.signature = sig;
976ca316 6636 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6637 &find_sig_entry, INSERT);
9a3c8263 6638 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6639
6640 /* Have we already tried to read this TU?
6641 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6642 needn't exist in the global table yet). */
6643 if (sig_entry != NULL)
6644 return sig_entry;
6645
a2ce51a0
DE
6646 if (dwp_file->tus == NULL)
6647 return NULL;
976ca316
SM
6648 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6649 1 /* is_debug_types */);
a2ce51a0
DE
6650 if (dwo_entry == NULL)
6651 return NULL;
6652
976ca316
SM
6653 sig_entry = add_type_unit (per_objfile, sig, slot);
6654 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6655
a2ce51a0
DE
6656 return sig_entry;
6657}
6658
380bca97 6659/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6660 Returns NULL if signature SIG is not present in the table.
6661 It is up to the caller to complain about this. */
348e048f
DE
6662
6663static struct signatured_type *
a2ce51a0 6664lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6665{
976ca316 6666 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6667
976ca316 6668 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6669 {
6670 /* We're in a DWO/DWP file, and we're using .gdb_index.
6671 These cases require special processing. */
976ca316 6672 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6673 return lookup_dwo_signatured_type (cu, sig);
6674 else
6675 return lookup_dwp_signatured_type (cu, sig);
6676 }
6677 else
6678 {
6679 struct signatured_type find_entry, *entry;
348e048f 6680
976ca316 6681 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6682 return NULL;
6683 find_entry.signature = sig;
9a3c8263 6684 entry = ((struct signatured_type *)
976ca316 6685 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6686 &find_entry));
a2ce51a0
DE
6687 return entry;
6688 }
348e048f 6689}
18a8505e 6690
42e7ad6c 6691/* Low level DIE reading support. */
348e048f 6692
d85a05f0
DJ
6693/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6694
6695static void
6696init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6697 struct dwarf2_cu *cu,
3019eac3 6698 struct dwarf2_section_info *section,
685af9cd
TT
6699 struct dwo_file *dwo_file,
6700 struct abbrev_table *abbrev_table)
d85a05f0 6701{
fceca515 6702 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6703 reader->abfd = section->get_bfd_owner ();
d85a05f0 6704 reader->cu = cu;
3019eac3 6705 reader->dwo_file = dwo_file;
dee91e82
DE
6706 reader->die_section = section;
6707 reader->buffer = section->buffer;
f664829e 6708 reader->buffer_end = section->buffer + section->size;
685af9cd 6709 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6710}
6711
c0ab21c2 6712/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6713 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6714 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6715 already.
6716
6717 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6718 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6719 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6720 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6721 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6722 STUB_COMP_DIR may be non-NULL.
3e225074 6723 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6724 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6725 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6726 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6727 kept around for at least as long as *RESULT_READER.
6728
b0c7bfa9
DE
6729 The result is non-zero if a valid (non-dummy) DIE was found. */
6730
6731static int
4ab09049 6732read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6733 struct dwo_unit *dwo_unit,
b0c7bfa9 6734 struct die_info *stub_comp_unit_die,
a2ce51a0 6735 const char *stub_comp_dir,
b0c7bfa9 6736 struct die_reader_specs *result_reader,
d521ce57 6737 const gdb_byte **result_info_ptr,
b0c7bfa9 6738 struct die_info **result_comp_unit_die,
685af9cd 6739 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6740{
976ca316 6741 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6742 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6743 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6744 bfd *abfd;
d521ce57 6745 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6746 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6747 int i,num_extra_attrs;
6748 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6749 struct die_info *comp_unit_die;
6750
b0aeadb3
DE
6751 /* At most one of these may be provided. */
6752 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6753
b0c7bfa9
DE
6754 /* These attributes aren't processed until later:
6755 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6756 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6757 referenced later. However, these attributes are found in the stub
6758 which we won't have later. In order to not impose this complication
6759 on the rest of the code, we read them here and copy them to the
6760 DWO CU/TU die. */
b0c7bfa9
DE
6761
6762 stmt_list = NULL;
6763 low_pc = NULL;
6764 high_pc = NULL;
6765 ranges = NULL;
6766 comp_dir = NULL;
6767
6768 if (stub_comp_unit_die != NULL)
6769 {
6770 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6771 DWO file. */
4ab09049 6772 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6773 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6774 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6775 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6776 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6777 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6778
a39fdb41 6779 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6780
18a8505e
AT
6781 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6782 here (if needed). We need the value before we can process
6783 DW_AT_ranges. */
a39fdb41 6784 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6785 }
a2ce51a0
DE
6786 else if (stub_comp_dir != NULL)
6787 {
6788 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6789 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6790 comp_dir->name = DW_AT_comp_dir;
6791 comp_dir->form = DW_FORM_string;
6792 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6793 DW_STRING (comp_dir) = stub_comp_dir;
6794 }
b0c7bfa9
DE
6795
6796 /* Set up for reading the DWO CU/TU. */
6797 cu->dwo_unit = dwo_unit;
685af9cd 6798 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6799 section->read (objfile);
6800 abfd = section->get_bfd_owner ();
9c541725
PA
6801 begin_info_ptr = info_ptr = (section->buffer
6802 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6803 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6804
4ab09049 6805 if (per_cu->is_debug_types)
b0c7bfa9 6806 {
4ab09049 6807 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6808
976ca316
SM
6809 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6810 section, dwo_abbrev_section,
43988095 6811 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6812 /* This is not an assert because it can be caused by bad debug info. */
43988095 6813 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6814 {
6815 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6816 " TU at offset %s [in module %s]"),
a2ce51a0 6817 hex_string (sig_type->signature),
43988095 6818 hex_string (cu->header.signature),
9d8780f0 6819 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6820 bfd_get_filename (abfd));
6821 }
9c541725 6822 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6823 /* For DWOs coming from DWP files, we don't know the CU length
6824 nor the type's offset in the TU until now. */
4057dfde 6825 dwo_unit->length = cu->header.get_length ();
9c541725 6826 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6827
6828 /* Establish the type offset that can be used to lookup the type.
6829 For DWO files, we don't know it until now. */
9c541725
PA
6830 sig_type->type_offset_in_section
6831 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6832 }
6833 else
6834 {
976ca316
SM
6835 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6836 section, dwo_abbrev_section,
43988095 6837 info_ptr, rcuh_kind::COMPILE);
9c541725 6838 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6839 /* For DWOs coming from DWP files, we don't know the CU length
6840 until now. */
4057dfde 6841 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6842 }
6843
685af9cd 6844 *result_dwo_abbrev_table
86de1d91
TT
6845 = abbrev_table::read (objfile, dwo_abbrev_section,
6846 cu->header.abbrev_sect_off);
685af9cd
TT
6847 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6848 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6849
6850 /* Read in the die, but leave space to copy over the attributes
6851 from the stub. This has the benefit of simplifying the rest of
6852 the code - all the work to maintain the illusion of a single
6853 DW_TAG_{compile,type}_unit DIE is done here. */
6854 num_extra_attrs = ((stmt_list != NULL)
6855 + (low_pc != NULL)
6856 + (high_pc != NULL)
6857 + (ranges != NULL)
6858 + (comp_dir != NULL));
6859 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6860 num_extra_attrs);
b0c7bfa9
DE
6861
6862 /* Copy over the attributes from the stub to the DIE we just read in. */
6863 comp_unit_die = *result_comp_unit_die;
6864 i = comp_unit_die->num_attrs;
6865 if (stmt_list != NULL)
6866 comp_unit_die->attrs[i++] = *stmt_list;
6867 if (low_pc != NULL)
6868 comp_unit_die->attrs[i++] = *low_pc;
6869 if (high_pc != NULL)
6870 comp_unit_die->attrs[i++] = *high_pc;
6871 if (ranges != NULL)
6872 comp_unit_die->attrs[i++] = *ranges;
6873 if (comp_dir != NULL)
6874 comp_unit_die->attrs[i++] = *comp_dir;
6875 comp_unit_die->num_attrs += num_extra_attrs;
6876
b4f54984 6877 if (dwarf_die_debug)
bf6af496
DE
6878 {
6879 fprintf_unfiltered (gdb_stdlog,
6880 "Read die from %s@0x%x of %s:\n",
96b79293 6881 section->get_name (),
bf6af496
DE
6882 (unsigned) (begin_info_ptr - section->buffer),
6883 bfd_get_filename (abfd));
b4f54984 6884 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6885 }
6886
b0c7bfa9
DE
6887 /* Skip dummy compilation units. */
6888 if (info_ptr >= begin_info_ptr + dwo_unit->length
6889 || peek_abbrev_code (abfd, info_ptr) == 0)
6890 return 0;
6891
6892 *result_info_ptr = info_ptr;
6893 return 1;
6894}
6895
a084a2a6
AT
6896/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6897 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6898 signature is part of the header. */
6899static gdb::optional<ULONGEST>
6900lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6901{
6902 if (cu->header.version >= 5)
6903 return cu->header.signature;
6904 struct attribute *attr;
6905 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6906 if (attr == nullptr)
6907 return gdb::optional<ULONGEST> ();
6908 return DW_UNSND (attr);
6909}
6910
c0ab21c2 6911/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6912 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6913 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6914
6915static struct dwo_unit *
4ab09049 6916lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6917{
4ab09049 6918 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6919 struct dwo_unit *dwo_unit;
c0ab21c2 6920 const char *comp_dir;
b0c7bfa9 6921
a2ce51a0
DE
6922 gdb_assert (cu != NULL);
6923
b0c7bfa9 6924 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6925 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6926 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6927
4ab09049
SM
6928 if (per_cu->is_debug_types)
6929 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6930 else
6931 {
a084a2a6 6932 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6933
a084a2a6 6934 if (!signature.has_value ())
b0c7bfa9
DE
6935 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6936 " [in module %s]"),
4ab09049
SM
6937 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6938
6939 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6940 }
6941
b0c7bfa9
DE
6942 return dwo_unit;
6943}
6944
c0ab21c2 6945/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6946 See it for a description of the parameters.
fcd3b13d 6947 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6948
c0ab21c2 6949void
9e021579
SM
6950cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6951 dwarf2_per_objfile *per_objfile,
2e671100 6952 dwarf2_cu *existing_cu)
a2ce51a0 6953{
a2ce51a0 6954 struct signatured_type *sig_type;
a2ce51a0
DE
6955
6956 /* Verify we can do the following downcast, and that we have the
6957 data we need. */
6958 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6959 sig_type = (struct signatured_type *) this_cu;
6960 gdb_assert (sig_type->dwo_unit != NULL);
6961
2e671100
SM
6962 dwarf2_cu *cu;
6963
6964 if (existing_cu != nullptr)
6aa5f3a6 6965 {
2e671100
SM
6966 cu = existing_cu;
6967 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6968 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6969 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6970 }
6971 else
6972 {
7188ed02
SM
6973 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6974 in per_objfile yet. */
6975 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6976 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6977 cu = m_new_cu.get ();
6aa5f3a6
DE
6978 }
6979
6980 /* A future optimization, if needed, would be to use an existing
6981 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6982 could share abbrev tables. */
a2ce51a0 6983
2e671100 6984 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6985 NULL /* stub_comp_unit_die */,
6986 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6987 this, &info_ptr,
3e225074 6988 &comp_unit_die,
c0ab21c2 6989 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6990 {
6991 /* Dummy die. */
c0ab21c2 6992 dummy_p = true;
a2ce51a0 6993 }
a2ce51a0
DE
6994}
6995
fd820528 6996/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6997 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6998
f4dc4d17
DE
6999 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7000 Otherwise the table specified in the comp unit header is read in and used.
7001 This is an optimization for when we already have the abbrev table.
7002
2e671100
SM
7003 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7004 allocated. */
aaa75496 7005
ab432490 7006cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7007 dwarf2_per_objfile *per_objfile,
c0ab21c2 7008 struct abbrev_table *abbrev_table,
2e671100 7009 dwarf2_cu *existing_cu,
c0ab21c2
TT
7010 bool skip_partial)
7011 : die_reader_specs {},
6751ebae 7012 m_this_cu (this_cu)
c906108c 7013{
976ca316 7014 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7015 struct dwarf2_section_info *section = this_cu->section;
96b79293 7016 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7017 const gdb_byte *begin_info_ptr;
dee91e82 7018 struct signatured_type *sig_type = NULL;
4bdcc0c1 7019 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7020 /* Non-zero if CU currently points to a DWO file and we need to
7021 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7022 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7023 int rereading_dwo_cu = 0;
c906108c 7024
b4f54984 7025 if (dwarf_die_debug)
9d8780f0 7026 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7027 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7028 sect_offset_str (this_cu->sect_off));
09406207 7029
a2ce51a0
DE
7030 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7031 file (instead of going through the stub), short-circuit all of this. */
7032 if (this_cu->reading_dwo_directly)
7033 {
7034 /* Narrow down the scope of possibilities to have to understand. */
7035 gdb_assert (this_cu->is_debug_types);
7036 gdb_assert (abbrev_table == NULL);
976ca316 7037 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7038 return;
7039 }
7040
dee91e82 7041 /* This is cheap if the section is already read in. */
96b79293 7042 section->read (objfile);
dee91e82 7043
9c541725 7044 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7045
7046 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7047
2e671100
SM
7048 dwarf2_cu *cu;
7049
7050 if (existing_cu != nullptr)
dee91e82 7051 {
2e671100 7052 cu = existing_cu;
42e7ad6c
DE
7053 /* If this CU is from a DWO file we need to start over, we need to
7054 refetch the attributes from the skeleton CU.
7055 This could be optimized by retrieving those attributes from when we
7056 were here the first time: the previous comp_unit_die was stored in
7057 comp_unit_obstack. But there's no data yet that we need this
7058 optimization. */
7059 if (cu->dwo_unit != NULL)
7060 rereading_dwo_cu = 1;
dee91e82
DE
7061 }
7062 else
7063 {
7188ed02
SM
7064 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7065 in per_objfile yet. */
976ca316
SM
7066 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7067 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7068 cu = m_new_cu.get ();
42e7ad6c 7069 }
dee91e82 7070
b0c7bfa9 7071 /* Get the header. */
9c541725 7072 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7073 {
7074 /* We already have the header, there's no need to read it in again. */
9c541725 7075 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7076 }
7077 else
7078 {
3019eac3 7079 if (this_cu->is_debug_types)
dee91e82 7080 {
976ca316
SM
7081 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7082 section, abbrev_section,
7083 info_ptr, rcuh_kind::TYPE);
dee91e82 7084
42e7ad6c
DE
7085 /* Since per_cu is the first member of struct signatured_type,
7086 we can go from a pointer to one to a pointer to the other. */
7087 sig_type = (struct signatured_type *) this_cu;
43988095 7088 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7089 gdb_assert (sig_type->type_offset_in_tu
7090 == cu->header.type_cu_offset_in_tu);
7091 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7092
42e7ad6c
DE
7093 /* LENGTH has not been set yet for type units if we're
7094 using .gdb_index. */
4057dfde 7095 this_cu->length = cu->header.get_length ();
3019eac3
DE
7096
7097 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7098 sig_type->type_offset_in_section =
7099 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7100
7101 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7102 }
7103 else
7104 {
976ca316
SM
7105 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7106 section, abbrev_section,
43988095
JK
7107 info_ptr,
7108 rcuh_kind::COMPILE);
dee91e82 7109
9c541725 7110 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7111 if (this_cu->length == 0)
7112 this_cu->length = cu->header.get_length ();
7113 else
7114 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7115 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7116 }
7117 }
10b3939b 7118
6caca83c 7119 /* Skip dummy compilation units. */
dee91e82 7120 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7121 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7122 {
7123 dummy_p = true;
7124 return;
7125 }
6caca83c 7126
433df2d4
DE
7127 /* If we don't have them yet, read the abbrevs for this compilation unit.
7128 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7129 done. */
f4dc4d17 7130 if (abbrev_table != NULL)
685af9cd
TT
7131 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7132 else
f4dc4d17 7133 {
c0ab21c2 7134 m_abbrev_table_holder
86de1d91
TT
7135 = abbrev_table::read (objfile, abbrev_section,
7136 cu->header.abbrev_sect_off);
c0ab21c2 7137 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7138 }
af703f96 7139
dee91e82 7140 /* Read the top level CU/TU die. */
c0ab21c2 7141 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7142 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7143
58f0c718 7144 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7145 {
7146 dummy_p = true;
7147 return;
7148 }
58f0c718 7149
b0c7bfa9 7150 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7151 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7152 table from the DWO file and pass the ownership over to us. It will be
7153 referenced from READER, so we must make sure to free it after we're done
7154 with READER.
7155
b0c7bfa9
DE
7156 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7157 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7158 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7159 if (dwo_name != nullptr)
3019eac3 7160 {
3019eac3 7161 struct dwo_unit *dwo_unit;
b0c7bfa9 7162 struct die_info *dwo_comp_unit_die;
3019eac3 7163
3e225074 7164 if (comp_unit_die->has_children)
6a506a2d 7165 {
b98664d3 7166 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7167 " has children (offset %s) [in module %s]"),
7168 sect_offset_str (this_cu->sect_off),
7169 bfd_get_filename (abfd));
6a506a2d 7170 }
4ab09049 7171 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7172 if (dwo_unit != NULL)
3019eac3 7173 {
4ab09049 7174 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7175 comp_unit_die, NULL,
c0ab21c2 7176 this, &info_ptr,
3e225074 7177 &dwo_comp_unit_die,
c0ab21c2 7178 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7179 {
7180 /* Dummy die. */
c0ab21c2 7181 dummy_p = true;
6a506a2d
DE
7182 return;
7183 }
7184 comp_unit_die = dwo_comp_unit_die;
7185 }
7186 else
7187 {
7188 /* Yikes, we couldn't find the rest of the DIE, we only have
7189 the stub. A complaint has already been logged. There's
7190 not much more we can do except pass on the stub DIE to
7191 die_reader_func. We don't want to throw an error on bad
7192 debug info. */
3019eac3
DE
7193 }
7194 }
c0ab21c2 7195}
3019eac3 7196
6751ebae
TT
7197void
7198cutu_reader::keep ()
c0ab21c2 7199{
b0c7bfa9 7200 /* Done, clean up. */
6751ebae
TT
7201 gdb_assert (!dummy_p);
7202 if (m_new_cu != NULL)
348e048f 7203 {
7188ed02
SM
7204 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7205 now. */
7206 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7207 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7208 }
dee91e82
DE
7209}
7210
18a8505e
AT
7211/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7212 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7213 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7214
7215 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7216 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7217
7218 We fill in THIS_CU->length.
7219
dee91e82 7220 THIS_CU->cu is always freed when done.
3019eac3 7221 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7222 to care whether it refers to the "main" CU or the DWO CU.
7223
7224 When parent_cu is passed, it is used to provide a default value for
7225 str_offsets_base and addr_base from the parent. */
dee91e82 7226
ab432490 7227cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7228 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7229 struct dwarf2_cu *parent_cu,
7230 struct dwo_file *dwo_file)
7231 : die_reader_specs {},
7232 m_this_cu (this_cu)
dee91e82 7233{
976ca316 7234 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7235 struct dwarf2_section_info *section = this_cu->section;
96b79293 7236 bfd *abfd = section->get_bfd_owner ();
33e80786 7237 struct dwarf2_section_info *abbrev_section;
d521ce57 7238 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7239
b4f54984 7240 if (dwarf_die_debug)
9d8780f0 7241 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7242 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7243 sect_offset_str (this_cu->sect_off));
09406207 7244
976ca316 7245 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7246
33e80786
DE
7247 abbrev_section = (dwo_file != NULL
7248 ? &dwo_file->sections.abbrev
7249 : get_abbrev_section_for_cu (this_cu));
7250
dee91e82 7251 /* This is cheap if the section is already read in. */
96b79293 7252 section->read (objfile);
dee91e82 7253
976ca316 7254 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7255
9c541725 7256 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7257 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7258 section, abbrev_section, info_ptr,
43988095
JK
7259 (this_cu->is_debug_types
7260 ? rcuh_kind::TYPE
7261 : rcuh_kind::COMPILE));
dee91e82 7262
18a8505e
AT
7263 if (parent_cu != nullptr)
7264 {
c0ab21c2
TT
7265 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7266 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7267 }
4057dfde 7268 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7269
7270 /* Skip dummy compilation units. */
7271 if (info_ptr >= begin_info_ptr + this_cu->length
7272 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7273 {
7274 dummy_p = true;
7275 return;
7276 }
72bf9492 7277
c0ab21c2 7278 m_abbrev_table_holder
86de1d91
TT
7279 = abbrev_table::read (objfile, abbrev_section,
7280 m_new_cu->header.abbrev_sect_off);
dee91e82 7281
c0ab21c2
TT
7282 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7283 m_abbrev_table_holder.get ());
3e225074 7284 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7285}
7286
0018ea6f
DE
7287\f
7288/* Type Unit Groups.
dee91e82 7289
0018ea6f
DE
7290 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7291 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7292 so that all types coming from the same compilation (.o file) are grouped
7293 together. A future step could be to put the types in the same symtab as
7294 the CU the types ultimately came from. */
ff013f42 7295
f4dc4d17
DE
7296static hashval_t
7297hash_type_unit_group (const void *item)
7298{
9a3c8263
SM
7299 const struct type_unit_group *tu_group
7300 = (const struct type_unit_group *) item;
f4dc4d17 7301
094b34ac 7302 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7303}
348e048f
DE
7304
7305static int
f4dc4d17 7306eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7307{
9a3c8263
SM
7308 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7309 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7310
094b34ac 7311 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7312}
348e048f 7313
f4dc4d17
DE
7314/* Allocate a hash table for type unit groups. */
7315
eaa5fa8b 7316static htab_up
298e9637 7317allocate_type_unit_groups_table ()
f4dc4d17 7318{
eaa5fa8b
TT
7319 return htab_up (htab_create_alloc (3,
7320 hash_type_unit_group,
7321 eq_type_unit_group,
7322 NULL, xcalloc, xfree));
f4dc4d17 7323}
dee91e82 7324
f4dc4d17
DE
7325/* Type units that don't have DW_AT_stmt_list are grouped into their own
7326 partial symtabs. We combine several TUs per psymtab to not let the size
7327 of any one psymtab grow too big. */
7328#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7329#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7330
094b34ac 7331/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7332 Create the type_unit_group object used to hold one or more TUs. */
7333
7334static struct type_unit_group *
094b34ac 7335create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7336{
976ca316
SM
7337 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7338 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7339 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7340 struct type_unit_group *tu_group;
f4dc4d17 7341
976ca316 7342 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7343 per_cu = &tu_group->per_cu;
1859c670 7344 per_cu->per_bfd = per_bfd;
f4dc4d17 7345
1859c670 7346 if (per_bfd->using_index)
094b34ac 7347 {
1859c670 7348 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7349 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7350 }
7351 else
7352 {
9c541725 7353 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7354 dwarf2_psymtab *pst;
528e1572 7355 std::string name;
094b34ac
DE
7356
7357 /* Give the symtab a useful name for debug purposes. */
7358 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7359 name = string_printf ("<type_units_%d>",
7360 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7361 else
528e1572 7362 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7363
976ca316 7364 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7365 pst->anonymous = true;
094b34ac 7366 }
f4dc4d17 7367
094b34ac 7368 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7369 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7370
7371 return tu_group;
7372}
7373
094b34ac
DE
7374/* Look up the type_unit_group for type unit CU, and create it if necessary.
7375 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7376
7377static struct type_unit_group *
ff39bb5e 7378get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7379{
976ca316
SM
7380 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7381 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7382 struct type_unit_group *tu_group;
7383 void **slot;
7384 unsigned int line_offset;
7385 struct type_unit_group type_unit_group_for_lookup;
7386
976ca316
SM
7387 if (per_objfile->per_bfd->type_unit_groups == NULL)
7388 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7389
7390 /* Do we need to create a new group, or can we use an existing one? */
7391
7392 if (stmt_list)
7393 {
7394 line_offset = DW_UNSND (stmt_list);
7395 ++tu_stats->nr_symtab_sharers;
7396 }
7397 else
7398 {
7399 /* Ugh, no stmt_list. Rare, but we have to handle it.
7400 We can do various things here like create one group per TU or
7401 spread them over multiple groups to split up the expansion work.
7402 To avoid worst case scenarios (too many groups or too large groups)
7403 we, umm, group them in bunches. */
7404 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7405 | (tu_stats->nr_stmt_less_type_units
7406 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7407 ++tu_stats->nr_stmt_less_type_units;
7408 }
7409
094b34ac 7410 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7411 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7412 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7413 &type_unit_group_for_lookup, INSERT);
7414 if (*slot != NULL)
7415 {
9a3c8263 7416 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7417 gdb_assert (tu_group != NULL);
7418 }
7419 else
7420 {
9c541725 7421 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7422 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7423 *slot = tu_group;
7424 ++tu_stats->nr_symtabs;
7425 }
7426
7427 return tu_group;
7428}
0018ea6f
DE
7429\f
7430/* Partial symbol tables. */
7431
7432/* Create a psymtab named NAME and assign it to PER_CU.
7433
7434 The caller must fill in the following details:
7435 dirname, textlow, texthigh. */
7436
891813be 7437static dwarf2_psymtab *
7aa104c4
SM
7438create_partial_symtab (dwarf2_per_cu_data *per_cu,
7439 dwarf2_per_objfile *per_objfile,
7440 const char *name)
0018ea6f 7441{
7aa104c4 7442 struct objfile *objfile = per_objfile->objfile;
891813be 7443 dwarf2_psymtab *pst;
0018ea6f 7444
9f4e76a4 7445 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7446
6d94535f 7447 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7448
7449 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7450 per_cu->v.psymtab = pst;
7451
7452 return pst;
7453}
7454
c0ab21c2 7455/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7456
7457static void
7458process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7459 const gdb_byte *info_ptr,
0018ea6f 7460 struct die_info *comp_unit_die,
c0ab21c2 7461 enum language pretend_language)
0018ea6f
DE
7462{
7463 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7464 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7465 struct objfile *objfile = per_objfile->objfile;
08feed99 7466 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7467 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7468 CORE_ADDR baseaddr;
7469 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7470 dwarf2_psymtab *pst;
3a2b436a 7471 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7472 const char *filename;
0018ea6f 7473
0018ea6f
DE
7474 gdb_assert (! per_cu->is_debug_types);
7475
c0ab21c2 7476 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7477
0018ea6f 7478 /* Allocate a new partial symbol table structure. */
2e927613
TV
7479 gdb::unique_xmalloc_ptr<char> debug_filename;
7480 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7481 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7482 if (filename == NULL)
0018ea6f 7483 filename = "";
2e927613
TV
7484 else if (strcmp (filename, artificial) == 0)
7485 {
7486 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7487 sect_offset_str (per_cu->sect_off),
7488 (char *) NULL));
2e927613
TV
7489 filename = debug_filename.get ();
7490 }
0018ea6f 7491
7aa104c4 7492 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7493
7494 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7495 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7496
b3b3bada 7497 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7498
7499 dwarf2_find_base_address (comp_unit_die, cu);
7500
7501 /* Possibly set the default values of LOWPC and HIGHPC from
7502 `DW_AT_ranges'. */
3a2b436a
JK
7503 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7504 &best_highpc, cu, pst);
7505 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7506 {
7507 CORE_ADDR low
7508 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7509 - baseaddr);
7510 CORE_ADDR high
7511 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7512 - baseaddr - 1);
7513 /* Store the contiguous range if it is not empty; it can be
7514 empty for CUs with no code. */
d320c2b5
TT
7515 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7516 low, high, pst);
79748972 7517 }
0018ea6f
DE
7518
7519 /* Check if comp unit has_children.
7520 If so, read the rest of the partial symbols from this comp unit.
7521 If not, there's no more debug_info for this comp unit. */
3e225074 7522 if (comp_unit_die->has_children)
0018ea6f
DE
7523 {
7524 struct partial_die_info *first_die;
7525 CORE_ADDR lowpc, highpc;
7526
7527 lowpc = ((CORE_ADDR) -1);
7528 highpc = ((CORE_ADDR) 0);
7529
7530 first_die = load_partial_dies (reader, info_ptr, 1);
7531
7532 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7533 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7534
7535 /* If we didn't find a lowpc, set it to highpc to avoid
7536 complaints from `maint check'. */
7537 if (lowpc == ((CORE_ADDR) -1))
7538 lowpc = highpc;
7539
7540 /* If the compilation unit didn't have an explicit address range,
7541 then use the information extracted from its child dies. */
e385593e 7542 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7543 {
7544 best_lowpc = lowpc;
7545 best_highpc = highpc;
7546 }
7547 }
4ae976d1 7548 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7549 best_lowpc + baseaddr)
7550 - baseaddr);
4ae976d1 7551 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7552 best_highpc + baseaddr)
7553 - baseaddr);
0018ea6f 7554
8763cede 7555 end_psymtab_common (objfile, pst);
0018ea6f 7556
ae640021 7557 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7558 {
7559 int i;
ae640021 7560 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7561
7562 /* Fill in 'dependencies' here; we fill in 'users' in a
7563 post-pass. */
7564 pst->number_of_dependencies = len;
a9342b62
TT
7565 pst->dependencies
7566 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7567 for (i = 0; i < len; ++i)
7568 {
7569 pst->dependencies[i]
7570 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7571 }
0018ea6f 7572
ae640021 7573 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7574 }
7575
7576 /* Get the list of files included in the current compilation unit,
7577 and build a psymtab for each of them. */
7578 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7579
b4f54984 7580 if (dwarf_read_debug)
b926417a
TT
7581 fprintf_unfiltered (gdb_stdlog,
7582 "Psymtab for %s unit @%s: %s - %s"
7583 ", %d global, %d static syms\n",
7584 per_cu->is_debug_types ? "type" : "comp",
7585 sect_offset_str (per_cu->sect_off),
7586 paddress (gdbarch, pst->text_low (objfile)),
7587 paddress (gdbarch, pst->text_high (objfile)),
7588 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7589}
7590
7591/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7592 Process compilation unit THIS_CU for a psymtab. */
7593
7594static void
ab432490
SM
7595process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7596 dwarf2_per_objfile *per_objfile,
135f5437 7597 bool want_partial_unit,
b93601f3 7598 enum language pretend_language)
0018ea6f
DE
7599{
7600 /* If this compilation unit was already read in, free the
7601 cached copy in order to read it in again. This is
7602 necessary because we skipped some symbols when we first
7603 read in the compilation unit (see load_partial_dies).
7604 This problem could be avoided, but the benefit is unclear. */
7188ed02 7605 per_objfile->remove_cu (this_cu);
0018ea6f 7606
2e671100 7607 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7608
58990295
TV
7609 switch (reader.comp_unit_die->tag)
7610 {
7611 case DW_TAG_compile_unit:
7612 this_cu->unit_type = DW_UT_compile;
7613 break;
7614 case DW_TAG_partial_unit:
7615 this_cu->unit_type = DW_UT_partial;
7616 break;
7617 default:
7618 abort ();
7619 }
7620
c0ab21c2 7621 if (reader.dummy_p)
f1902523 7622 {
c0ab21c2 7623 /* Nothing. */
f1902523 7624 }
c0ab21c2 7625 else if (this_cu->is_debug_types)
3e225074
TT
7626 build_type_psymtabs_reader (&reader, reader.info_ptr,
7627 reader.comp_unit_die);
135f5437
TT
7628 else if (want_partial_unit
7629 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7630 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7631 reader.comp_unit_die,
c0ab21c2 7632 pretend_language);
0018ea6f 7633
7188ed02 7634 this_cu->lang = reader.cu->language;
58990295 7635
0018ea6f 7636 /* Age out any secondary CUs. */
7188ed02 7637 per_objfile->age_comp_units ();
0018ea6f 7638}
f4dc4d17
DE
7639
7640/* Reader function for build_type_psymtabs. */
7641
7642static void
7643build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7644 const gdb_byte *info_ptr,
3e225074 7645 struct die_info *type_unit_die)
f4dc4d17 7646{
976ca316
SM
7647 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7648 struct objfile *objfile = per_objfile->objfile;
f4dc4d17
DE
7649 struct dwarf2_cu *cu = reader->cu;
7650 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7651 struct signatured_type *sig_type;
f4dc4d17
DE
7652 struct type_unit_group *tu_group;
7653 struct attribute *attr;
7654 struct partial_die_info *first_die;
7655 CORE_ADDR lowpc, highpc;
891813be 7656 dwarf2_psymtab *pst;
f4dc4d17 7657
0186c6a7
DE
7658 gdb_assert (per_cu->is_debug_types);
7659 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7660
3e225074 7661 if (! type_unit_die->has_children)
f4dc4d17
DE
7662 return;
7663
052c8bb8 7664 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7665 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7666
df07e2c7 7667 if (tu_group->tus == nullptr)
a8b3b8e9 7668 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7669 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7670
7671 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7672 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7673 pst->anonymous = true;
f4dc4d17
DE
7674
7675 first_die = load_partial_dies (reader, info_ptr, 1);
7676
7677 lowpc = (CORE_ADDR) -1;
7678 highpc = (CORE_ADDR) 0;
7679 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7680
8763cede 7681 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7682}
7683
73051182
DE
7684/* Struct used to sort TUs by their abbreviation table offset. */
7685
7686struct tu_abbrev_offset
7687{
b2bdb8cf
SM
7688 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7689 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7690 {}
7691
7692 signatured_type *sig_type;
73051182
DE
7693 sect_offset abbrev_offset;
7694};
7695
484cf504 7696/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7697
484cf504
TT
7698static bool
7699sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7700 const struct tu_abbrev_offset &b)
73051182 7701{
484cf504 7702 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7703}
7704
7705/* Efficiently read all the type units.
7706 This does the bulk of the work for build_type_psymtabs.
7707
7708 The efficiency is because we sort TUs by the abbrev table they use and
7709 only read each abbrev table once. In one program there are 200K TUs
7710 sharing 8K abbrev tables.
7711
7712 The main purpose of this function is to support building the
5989a64e 7713 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7714 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7715 can collapse the search space by grouping them by stmt_list.
7716 The savings can be significant, in the same program from above the 200K TUs
7717 share 8K stmt_list tables.
7718
7719 FUNC is expected to call get_type_unit_group, which will create the
7720 struct type_unit_group if necessary and add it to
5989a64e 7721 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7722
7723static void
976ca316 7724build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7725{
976ca316 7726 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7727 abbrev_table_up abbrev_table;
73051182 7728 sect_offset abbrev_offset;
73051182
DE
7729
7730 /* It's up to the caller to not call us multiple times. */
976ca316 7731 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7732
976ca316 7733 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7734 return;
7735
7736 /* TUs typically share abbrev tables, and there can be way more TUs than
7737 abbrev tables. Sort by abbrev table to reduce the number of times we
7738 read each abbrev table in.
7739 Alternatives are to punt or to maintain a cache of abbrev tables.
7740 This is simpler and efficient enough for now.
7741
7742 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7743 symtab to use). Typically TUs with the same abbrev offset have the same
7744 stmt_list value too so in practice this should work well.
7745
7746 The basic algorithm here is:
7747
7748 sort TUs by abbrev table
7749 for each TU with same abbrev table:
7750 read abbrev table if first user
7751 read TU top level DIE
7752 [IWBN if DWO skeletons had DW_AT_stmt_list]
7753 call FUNC */
7754
b4f54984 7755 if (dwarf_read_debug)
73051182
DE
7756 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7757
7758 /* Sort in a separate table to maintain the order of all_type_units
7759 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7760 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7761 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7762
976ca316 7763 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7764 sorted_by_abbrev.emplace_back
976ca316 7765 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7766 sig_type->per_cu.sect_off));
73051182 7767
484cf504
TT
7768 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7769 sort_tu_by_abbrev_offset);
73051182 7770
9c541725 7771 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7772
b2bdb8cf 7773 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7774 {
73051182
DE
7775 /* Switch to the next abbrev table if necessary. */
7776 if (abbrev_table == NULL
b2bdb8cf 7777 || tu.abbrev_offset != abbrev_offset)
73051182 7778 {
b2bdb8cf 7779 abbrev_offset = tu.abbrev_offset;
73051182 7780 abbrev_table =
976ca316
SM
7781 abbrev_table::read (per_objfile->objfile,
7782 &per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7783 ++tu_stats->nr_uniq_abbrev_tables;
7784 }
7785
976ca316 7786 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7787 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7788 if (!reader.dummy_p)
7789 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7790 reader.comp_unit_die);
73051182 7791 }
6aa5f3a6 7792}
73051182 7793
6aa5f3a6
DE
7794/* Print collected type unit statistics. */
7795
7796static void
976ca316 7797print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7798{
976ca316 7799 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7800
7801 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7802 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
976ca316 7803 per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7804 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7805 tu_stats->nr_uniq_abbrev_tables);
7806 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7807 tu_stats->nr_symtabs);
7808 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7809 tu_stats->nr_symtab_sharers);
7810 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7811 tu_stats->nr_stmt_less_type_units);
7812 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7813 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7814}
7815
f4dc4d17
DE
7816/* Traversal function for build_type_psymtabs. */
7817
7818static int
7819build_type_psymtab_dependencies (void **slot, void *info)
7820{
976ca316
SM
7821 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7822 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 7823 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7824 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7825 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7826 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7827 int i;
7828
7829 gdb_assert (len > 0);
197400e8 7830 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7831
7832 pst->number_of_dependencies = len;
a9342b62 7833 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7834 for (i = 0; i < len; ++i)
f4dc4d17 7835 {
df07e2c7 7836 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7837 gdb_assert (iter->per_cu.is_debug_types);
7838 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7839 iter->type_unit_group = tu_group;
f4dc4d17
DE
7840 }
7841
df07e2c7
AB
7842 delete tu_group->tus;
7843 tu_group->tus = nullptr;
348e048f
DE
7844
7845 return 1;
7846}
7847
7848/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7849 Build partial symbol tables for the .debug_types comp-units. */
7850
7851static void
976ca316 7852build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7853{
976ca316 7854 if (! create_all_type_units (per_objfile))
348e048f
DE
7855 return;
7856
976ca316 7857 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7858}
f4dc4d17 7859
6aa5f3a6
DE
7860/* Traversal function for process_skeletonless_type_unit.
7861 Read a TU in a DWO file and build partial symbols for it. */
7862
7863static int
7864process_skeletonless_type_unit (void **slot, void *info)
7865{
7866 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7867 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7868 struct signatured_type find_entry, *entry;
7869
7870 /* If this TU doesn't exist in the global table, add it and read it in. */
7871
976ca316
SM
7872 if (per_objfile->per_bfd->signatured_types == NULL)
7873 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7874
7875 find_entry.signature = dwo_unit->signature;
976ca316 7876 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7877 &find_entry, INSERT);
6aa5f3a6
DE
7878 /* If we've already seen this type there's nothing to do. What's happening
7879 is we're doing our own version of comdat-folding here. */
7880 if (*slot != NULL)
7881 return 1;
7882
7883 /* This does the job that create_all_type_units would have done for
7884 this TU. */
976ca316
SM
7885 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7886 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7887 *slot = entry;
7888
7889 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7890 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7891 if (!reader.dummy_p)
7892 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7893 reader.comp_unit_die);
6aa5f3a6
DE
7894
7895 return 1;
7896}
7897
7898/* Traversal function for process_skeletonless_type_units. */
7899
7900static int
7901process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7902{
7903 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7904
7905 if (dwo_file->tus != NULL)
b0b6a987
TT
7906 htab_traverse_noresize (dwo_file->tus.get (),
7907 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7908
7909 return 1;
7910}
7911
7912/* Scan all TUs of DWO files, verifying we've processed them.
7913 This is needed in case a TU was emitted without its skeleton.
7914 Note: This can't be done until we know what all the DWO files are. */
7915
7916static void
976ca316 7917process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7918{
7919 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7920 if (get_dwp_file (per_objfile) == NULL
7921 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7922 {
976ca316 7923 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7924 process_dwo_file_for_skeletonless_type_units,
976ca316 7925 per_objfile);
6aa5f3a6 7926 }
348e048f
DE
7927}
7928
ed2dc618 7929/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7930
7931static void
976ca316 7932set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7933{
976ca316 7934 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7935 {
891813be 7936 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7937
36586728
TT
7938 if (pst == NULL)
7939 continue;
7940
b76e467d 7941 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7942 {
7943 /* Set the 'user' field only if it is not already set. */
7944 if (pst->dependencies[j]->user == NULL)
7945 pst->dependencies[j]->user = pst;
7946 }
7947 }
7948}
7949
93311388
DE
7950/* Build the partial symbol table by doing a quick pass through the
7951 .debug_info and .debug_abbrev sections. */
72bf9492 7952
93311388 7953static void
976ca316 7954dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7955{
976ca316 7956 struct objfile *objfile = per_objfile->objfile;
93311388 7957
b4f54984 7958 if (dwarf_read_debug)
45cfd468
DE
7959 {
7960 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7961 objfile_name (objfile));
45cfd468
DE
7962 }
7963
76935768 7964 scoped_restore restore_reading_psyms
976ca316 7965 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 7966 true);
98bfdba5 7967
976ca316 7968 per_objfile->per_bfd->info.read (objfile);
91c24f0a 7969
93311388
DE
7970 /* Any cached compilation units will be linked by the per-objfile
7971 read_in_chain. Make sure to free them when we're done. */
976ca316 7972 free_cached_comp_units freer (per_objfile);
72bf9492 7973
976ca316 7974 build_type_psymtabs (per_objfile);
348e048f 7975
976ca316 7976 create_all_comp_units (per_objfile);
c906108c 7977
60606b2c
TT
7978 /* Create a temporary address map on a temporary obstack. We later
7979 copy this to the final obstack. */
8268c778 7980 auto_obstack temp_obstack;
791afaa2
TT
7981
7982 scoped_restore save_psymtabs_addrmap
d320c2b5 7983 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7984 addrmap_create_mutable (&temp_obstack));
72bf9492 7985
976ca316 7986 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7987 {
7988 if (per_cu->v.psymtab != NULL)
7989 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7990 continue;
976ca316 7991 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 7992 language_minimal);
3d5afab3 7993 }
ff013f42 7994
6aa5f3a6 7995 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 7996 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
7997
7998 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 7999 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8000 {
976ca316
SM
8001 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8002 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8003 }
8004
b4f54984 8005 if (dwarf_read_debug)
976ca316 8006 print_tu_stats (per_objfile);
6aa5f3a6 8007
976ca316 8008 set_partial_user (per_objfile);
95554aad 8009
d320c2b5
TT
8010 objfile->partial_symtabs->psymtabs_addrmap
8011 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8012 objfile->partial_symtabs->obstack ());
791afaa2
TT
8013 /* At this point we want to keep the address map. */
8014 save_psymtabs_addrmap.release ();
ff013f42 8015
b4f54984 8016 if (dwarf_read_debug)
45cfd468 8017 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8018 objfile_name (objfile));
ae038cb0
DJ
8019}
8020
dee91e82
DE
8021/* Load the partial DIEs for a secondary CU into memory.
8022 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8023
dee91e82 8024static void
ab432490 8025load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8026 dwarf2_per_objfile *per_objfile,
8027 dwarf2_cu *existing_cu)
dee91e82 8028{
2e671100 8029 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8030
8031 if (!reader.dummy_p)
8032 {
8033 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8034 language_minimal);
8035
8036 /* Check if comp unit has_children.
8037 If so, read the rest of the partial symbols from this comp unit.
8038 If not, there's no more debug_info for this comp unit. */
3e225074 8039 if (reader.comp_unit_die->has_children)
c0ab21c2 8040 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8041
8042 reader.keep ();
c0ab21c2 8043 }
ae038cb0
DJ
8044}
8045
ae038cb0 8046static void
976ca316 8047read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8048 struct dwarf2_section_info *section,
f1902523 8049 struct dwarf2_section_info *abbrev_section,
b76e467d 8050 unsigned int is_dwz)
ae038cb0 8051{
d521ce57 8052 const gdb_byte *info_ptr;
976ca316 8053 struct objfile *objfile = per_objfile->objfile;
be391dca 8054
b4f54984 8055 if (dwarf_read_debug)
bf6af496 8056 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8057 section->get_name (),
8058 section->get_file_name ());
bf6af496 8059
96b79293 8060 section->read (objfile);
ae038cb0 8061
36586728 8062 info_ptr = section->buffer;
6e70227d 8063
36586728 8064 while (info_ptr < section->buffer + section->size)
ae038cb0 8065 {
ae038cb0 8066 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8067
9c541725 8068 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8069
f1902523 8070 comp_unit_head cu_header;
976ca316 8071 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8072 abbrev_section, info_ptr,
8073 rcuh_kind::COMPILE);
ae038cb0
DJ
8074
8075 /* Save the compilation unit for later lookup. */
f1902523 8076 if (cu_header.unit_type != DW_UT_type)
976ca316 8077 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8078 else
8079 {
976ca316 8080 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8081 sig_type->signature = cu_header.signature;
8082 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8083 this_cu = &sig_type->per_cu;
8084 }
8085 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8086 this_cu->sect_off = sect_off;
f1902523 8087 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8088 this_cu->is_dwz = is_dwz;
8a0459fd 8089 this_cu->section = section;
ae038cb0 8090
976ca316 8091 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8092
8093 info_ptr = info_ptr + this_cu->length;
8094 }
36586728
TT
8095}
8096
8097/* Create a list of all compilation units in OBJFILE.
8098 This is only done for -readnow and building partial symtabs. */
8099
8100static void
976ca316 8101create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8102{
976ca316
SM
8103 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8104 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8105 &per_objfile->per_bfd->abbrev, 0);
36586728 8106
976ca316 8107 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8108 if (dwz != NULL)
976ca316 8109 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8110}
8111
5734ee8b 8112/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8113 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8114 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8115 DW_AT_ranges). See the comments of add_partial_subprogram on how
8116 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8117
72bf9492
DJ
8118static void
8119scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8120 CORE_ADDR *highpc, int set_addrmap,
8121 struct dwarf2_cu *cu)
c906108c 8122{
72bf9492 8123 struct partial_die_info *pdi;
c906108c 8124
91c24f0a
DC
8125 /* Now, march along the PDI's, descending into ones which have
8126 interesting children but skipping the children of the other ones,
8127 until we reach the end of the compilation unit. */
c906108c 8128
72bf9492 8129 pdi = first_die;
91c24f0a 8130
72bf9492
DJ
8131 while (pdi != NULL)
8132 {
52356b79 8133 pdi->fixup (cu);
c906108c 8134
f55ee35c 8135 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8136 children, so we need to look at them. Ditto for anonymous
8137 enums. */
933c6fe4 8138
7d00ffec 8139 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8140 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8141 || pdi->tag == DW_TAG_imported_unit
8142 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8143 {
72bf9492 8144 switch (pdi->tag)
c906108c
SS
8145 {
8146 case DW_TAG_subprogram:
b1dc1806 8147 case DW_TAG_inlined_subroutine:
cdc07690 8148 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8149 break;
72929c62 8150 case DW_TAG_constant:
c906108c
SS
8151 case DW_TAG_variable:
8152 case DW_TAG_typedef:
91c24f0a 8153 case DW_TAG_union_type:
317d2668
TV
8154 if (!pdi->is_declaration
8155 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8156 {
72bf9492 8157 add_partial_symbol (pdi, cu);
63d06c5c
DC
8158 }
8159 break;
c906108c 8160 case DW_TAG_class_type:
680b30c7 8161 case DW_TAG_interface_type:
c906108c 8162 case DW_TAG_structure_type:
72bf9492 8163 if (!pdi->is_declaration)
c906108c 8164 {
72bf9492 8165 add_partial_symbol (pdi, cu);
c906108c 8166 }
b7fee5a3
KS
8167 if ((cu->language == language_rust
8168 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8169 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8170 set_addrmap, cu);
c906108c 8171 break;
91c24f0a 8172 case DW_TAG_enumeration_type:
72bf9492
DJ
8173 if (!pdi->is_declaration)
8174 add_partial_enumeration (pdi, cu);
c906108c
SS
8175 break;
8176 case DW_TAG_base_type:
a02abb62 8177 case DW_TAG_subrange_type:
c906108c 8178 /* File scope base type definitions are added to the partial
c5aa993b 8179 symbol table. */
72bf9492 8180 add_partial_symbol (pdi, cu);
c906108c 8181 break;
d9fa45fe 8182 case DW_TAG_namespace:
cdc07690 8183 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8184 break;
5d7cb8df 8185 case DW_TAG_module:
59c35742
AB
8186 if (!pdi->is_declaration)
8187 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8188 break;
95554aad
TT
8189 case DW_TAG_imported_unit:
8190 {
8191 struct dwarf2_per_cu_data *per_cu;
8192
f4dc4d17
DE
8193 /* For now we don't handle imported units in type units. */
8194 if (cu->per_cu->is_debug_types)
8195 {
8196 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8197 " supported in type units [in module %s]"),
5e22e966 8198 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8199 }
8200
e3b94546 8201 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8202 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8203
8204 /* Go read the partial unit, if needed. */
8205 if (per_cu->v.psymtab == NULL)
ab432490
SM
8206 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8207 cu->language);
95554aad 8208
ae640021 8209 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8210 }
8211 break;
74921315
KS
8212 case DW_TAG_imported_declaration:
8213 add_partial_symbol (pdi, cu);
8214 break;
c906108c
SS
8215 default:
8216 break;
8217 }
8218 }
8219
72bf9492
DJ
8220 /* If the die has a sibling, skip to the sibling. */
8221
8222 pdi = pdi->die_sibling;
8223 }
8224}
8225
8226/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8227
72bf9492 8228 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8229 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8230 Enumerators are an exception; they use the scope of their parent
8231 enumeration type, i.e. the name of the enumeration type is not
8232 prepended to the enumerator.
91c24f0a 8233
72bf9492
DJ
8234 There are two complexities. One is DW_AT_specification; in this
8235 case "parent" means the parent of the target of the specification,
8236 instead of the direct parent of the DIE. The other is compilers
8237 which do not emit DW_TAG_namespace; in this case we try to guess
8238 the fully qualified name of structure types from their members'
8239 linkage names. This must be done using the DIE's children rather
8240 than the children of any DW_AT_specification target. We only need
8241 to do this for structures at the top level, i.e. if the target of
8242 any DW_AT_specification (if any; otherwise the DIE itself) does not
8243 have a parent. */
8244
8245/* Compute the scope prefix associated with PDI's parent, in
8246 compilation unit CU. The result will be allocated on CU's
8247 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8248 field. NULL is returned if no prefix is necessary. */
15d034d0 8249static const char *
72bf9492
DJ
8250partial_die_parent_scope (struct partial_die_info *pdi,
8251 struct dwarf2_cu *cu)
8252{
15d034d0 8253 const char *grandparent_scope;
72bf9492 8254 struct partial_die_info *parent, *real_pdi;
91c24f0a 8255
72bf9492
DJ
8256 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8257 then this means the parent of the specification DIE. */
8258
8259 real_pdi = pdi;
72bf9492 8260 while (real_pdi->has_specification)
fb816e8b 8261 {
122cf0f2
AB
8262 auto res = find_partial_die (real_pdi->spec_offset,
8263 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8264 real_pdi = res.pdi;
8265 cu = res.cu;
8266 }
72bf9492
DJ
8267
8268 parent = real_pdi->die_parent;
8269 if (parent == NULL)
8270 return NULL;
8271
8272 if (parent->scope_set)
8273 return parent->scope;
8274
52356b79 8275 parent->fixup (cu);
72bf9492 8276
10b3939b 8277 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8278
acebe513
UW
8279 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8280 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8281 Work around this problem here. */
8282 if (cu->language == language_cplus
6e70227d 8283 && parent->tag == DW_TAG_namespace
7d00ffec 8284 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8285 && grandparent_scope == NULL)
8286 {
8287 parent->scope = NULL;
8288 parent->scope_set = 1;
8289 return NULL;
8290 }
8291
0a4b0913 8292 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8293 if (pdi->tag == DW_TAG_enumerator)
8294 /* Enumerators should not get the name of the enumeration as a prefix. */
8295 parent->scope = grandparent_scope;
8296 else if (parent->tag == DW_TAG_namespace
f55ee35c 8297 || parent->tag == DW_TAG_module
72bf9492
DJ
8298 || parent->tag == DW_TAG_structure_type
8299 || parent->tag == DW_TAG_class_type
680b30c7 8300 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8301 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8302 || parent->tag == DW_TAG_enumeration_type
8303 || (cu->language == language_fortran
8304 && parent->tag == DW_TAG_subprogram
8305 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8306 {
8307 if (grandparent_scope == NULL)
7d00ffec 8308 parent->scope = parent->name (cu);
72bf9492 8309 else
3e43a32a
MS
8310 parent->scope = typename_concat (&cu->comp_unit_obstack,
8311 grandparent_scope,
7d00ffec 8312 parent->name (cu), 0, cu);
72bf9492 8313 }
72bf9492
DJ
8314 else
8315 {
8316 /* FIXME drow/2004-04-01: What should we be doing with
8317 function-local names? For partial symbols, we should probably be
8318 ignoring them. */
fa9c3fa0
TT
8319 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8320 dwarf_tag_name (parent->tag),
8321 sect_offset_str (pdi->sect_off));
72bf9492 8322 parent->scope = grandparent_scope;
c906108c
SS
8323 }
8324
72bf9492
DJ
8325 parent->scope_set = 1;
8326 return parent->scope;
8327}
8328
8329/* Return the fully scoped name associated with PDI, from compilation unit
8330 CU. The result will be allocated with malloc. */
4568ecf9 8331
43816ebc 8332static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8333partial_die_full_name (struct partial_die_info *pdi,
8334 struct dwarf2_cu *cu)
8335{
15d034d0 8336 const char *parent_scope;
72bf9492 8337
98bfdba5
PA
8338 /* If this is a template instantiation, we can not work out the
8339 template arguments from partial DIEs. So, unfortunately, we have
8340 to go through the full DIEs. At least any work we do building
8341 types here will be reused if full symbols are loaded later. */
8342 if (pdi->has_template_arguments)
8343 {
52356b79 8344 pdi->fixup (cu);
98bfdba5 8345
7d00ffec 8346 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8347 {
8348 struct die_info *die;
8349 struct attribute attr;
8350 struct dwarf2_cu *ref_cu = cu;
8351
b64f50a1 8352 /* DW_FORM_ref_addr is using section offset. */
b4069958 8353 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8354 attr.form = DW_FORM_ref_addr;
9c541725 8355 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8356 die = follow_die_ref (NULL, &attr, &ref_cu);
8357
43816ebc 8358 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8359 }
8360 }
8361
72bf9492
DJ
8362 parent_scope = partial_die_parent_scope (pdi, cu);
8363 if (parent_scope == NULL)
8364 return NULL;
8365 else
43816ebc 8366 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8367 pdi->name (cu),
8368 0, cu));
c906108c
SS
8369}
8370
8371static void
72bf9492 8372add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8373{
976ca316
SM
8374 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8375 struct objfile *objfile = per_objfile->objfile;
08feed99 8376 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8377 CORE_ADDR addr = 0;
15d034d0 8378 const char *actual_name = NULL;
e142c38c
DJ
8379 CORE_ADDR baseaddr;
8380
b3b3bada 8381 baseaddr = objfile->text_section_offset ();
c906108c 8382
43816ebc
TT
8383 gdb::unique_xmalloc_ptr<char> built_actual_name
8384 = partial_die_full_name (pdi, cu);
15d034d0 8385 if (built_actual_name != NULL)
43816ebc 8386 actual_name = built_actual_name.get ();
63d06c5c 8387
72bf9492 8388 if (actual_name == NULL)
7d00ffec 8389 actual_name = pdi->name (cu);
72bf9492 8390
76e288d1
TT
8391 partial_symbol psymbol;
8392 memset (&psymbol, 0, sizeof (psymbol));
8393 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8394 psymbol.ginfo.section = -1;
8395
8396 /* The code below indicates that the psymbol should be installed by
8397 setting this. */
8398 gdb::optional<psymbol_placement> where;
8399
c906108c
SS
8400 switch (pdi->tag)
8401 {
b1dc1806 8402 case DW_TAG_inlined_subroutine:
c906108c 8403 case DW_TAG_subprogram:
79748972
TT
8404 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8405 - baseaddr);
0a4b0913
AB
8406 if (pdi->is_external
8407 || cu->language == language_ada
8408 || (cu->language == language_fortran
8409 && pdi->die_parent != NULL
8410 && pdi->die_parent->tag == DW_TAG_subprogram))
8411 {
8412 /* Normally, only "external" DIEs are part of the global scope.
8413 But in Ada and Fortran, we want to be able to access nested
8414 procedures globally. So all Ada and Fortran subprograms are
8415 stored in the global scope. */
76e288d1 8416 where = psymbol_placement::GLOBAL;
c906108c
SS
8417 }
8418 else
76e288d1
TT
8419 where = psymbol_placement::STATIC;
8420
8421 psymbol.domain = VAR_DOMAIN;
8422 psymbol.aclass = LOC_BLOCK;
8423 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8424 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8425
8426 if (pdi->main_subprogram && actual_name != NULL)
8427 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8428 break;
72929c62 8429 case DW_TAG_constant:
76e288d1
TT
8430 psymbol.domain = VAR_DOMAIN;
8431 psymbol.aclass = LOC_STATIC;
8432 where = (pdi->is_external
8433 ? psymbol_placement::GLOBAL
8434 : psymbol_placement::STATIC);
72929c62 8435 break;
c906108c 8436 case DW_TAG_variable:
95554aad
TT
8437 if (pdi->d.locdesc)
8438 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8439
95554aad 8440 if (pdi->d.locdesc
caac4577 8441 && addr == 0
976ca316 8442 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8443 {
8444 /* A global or static variable may also have been stripped
8445 out by the linker if unused, in which case its address
8446 will be nullified; do not add such variables into partial
8447 symbol table then. */
8448 }
8449 else if (pdi->is_external)
c906108c
SS
8450 {
8451 /* Global Variable.
8452 Don't enter into the minimal symbol tables as there is
8453 a minimal symbol table entry from the ELF symbols already.
8454 Enter into partial symbol table if it has a location
8455 descriptor or a type.
8456 If the location descriptor is missing, new_symbol will create
8457 a LOC_UNRESOLVED symbol, the address of the variable will then
8458 be determined from the minimal symbol table whenever the variable
8459 is referenced.
8460 The address for the partial symbol table entry is not
8461 used by GDB, but it comes in handy for debugging partial symbol
8462 table building. */
8463
95554aad 8464 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8465 {
8466 psymbol.domain = VAR_DOMAIN;
8467 psymbol.aclass = LOC_STATIC;
8468 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8469 psymbol.ginfo.value.address = addr;
8470 where = psymbol_placement::GLOBAL;
8471 }
c906108c
SS
8472 }
8473 else
8474 {
ff908ebf
AW
8475 int has_loc = pdi->d.locdesc != NULL;
8476
8477 /* Static Variable. Skip symbols whose value we cannot know (those
8478 without location descriptors or constant values). */
8479 if (!has_loc && !pdi->has_const_value)
43816ebc 8480 return;
ff908ebf 8481
76e288d1
TT
8482 psymbol.domain = VAR_DOMAIN;
8483 psymbol.aclass = LOC_STATIC;
8484 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8485 if (has_loc)
8486 psymbol.ginfo.value.address = addr;
8487 where = psymbol_placement::STATIC;
c906108c
SS
8488 }
8489 break;
8490 case DW_TAG_typedef:
8491 case DW_TAG_base_type:
a02abb62 8492 case DW_TAG_subrange_type:
76e288d1
TT
8493 psymbol.domain = VAR_DOMAIN;
8494 psymbol.aclass = LOC_TYPEDEF;
8495 where = psymbol_placement::STATIC;
c906108c 8496 break;
74921315 8497 case DW_TAG_imported_declaration:
72bf9492 8498 case DW_TAG_namespace:
76e288d1
TT
8499 psymbol.domain = VAR_DOMAIN;
8500 psymbol.aclass = LOC_TYPEDEF;
8501 where = psymbol_placement::GLOBAL;
72bf9492 8502 break;
530e8392 8503 case DW_TAG_module:
a5fd13a9
BH
8504 /* With Fortran 77 there might be a "BLOCK DATA" module
8505 available without any name. If so, we skip the module as it
8506 doesn't bring any value. */
8507 if (actual_name != nullptr)
76e288d1
TT
8508 {
8509 psymbol.domain = MODULE_DOMAIN;
8510 psymbol.aclass = LOC_TYPEDEF;
8511 where = psymbol_placement::GLOBAL;
8512 }
530e8392 8513 break;
c906108c 8514 case DW_TAG_class_type:
680b30c7 8515 case DW_TAG_interface_type:
c906108c
SS
8516 case DW_TAG_structure_type:
8517 case DW_TAG_union_type:
8518 case DW_TAG_enumeration_type:
fa4028e9
JB
8519 /* Skip external references. The DWARF standard says in the section
8520 about "Structure, Union, and Class Type Entries": "An incomplete
8521 structure, union or class type is represented by a structure,
8522 union or class entry that does not have a byte size attribute
8523 and that has a DW_AT_declaration attribute." */
8524 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8525 return;
fa4028e9 8526
63d06c5c
DC
8527 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8528 static vs. global. */
76e288d1
TT
8529 psymbol.domain = STRUCT_DOMAIN;
8530 psymbol.aclass = LOC_TYPEDEF;
8531 where = (cu->language == language_cplus
8532 ? psymbol_placement::GLOBAL
8533 : psymbol_placement::STATIC);
c906108c
SS
8534 break;
8535 case DW_TAG_enumerator:
76e288d1
TT
8536 psymbol.domain = VAR_DOMAIN;
8537 psymbol.aclass = LOC_CONST;
8538 where = (cu->language == language_cplus
8539 ? psymbol_placement::GLOBAL
8540 : psymbol_placement::STATIC);
c906108c
SS
8541 break;
8542 default:
8543 break;
8544 }
76e288d1
TT
8545
8546 if (where.has_value ())
8547 {
f049a313
TT
8548 if (built_actual_name != nullptr)
8549 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8550 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8551 psymbol.ginfo.set_linkage_name (actual_name);
8552 else
8553 {
8554 psymbol.ginfo.set_demangled_name (actual_name,
8555 &objfile->objfile_obstack);
8556 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8557 }
76e288d1
TT
8558 add_psymbol_to_list (psymbol, *where, objfile);
8559 }
c906108c
SS
8560}
8561
5c4e30ca
DC
8562/* Read a partial die corresponding to a namespace; also, add a symbol
8563 corresponding to that namespace to the symbol table. NAMESPACE is
8564 the name of the enclosing namespace. */
91c24f0a 8565
72bf9492
DJ
8566static void
8567add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8568 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8569 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8570{
72bf9492 8571 /* Add a symbol for the namespace. */
e7c27a73 8572
72bf9492 8573 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8574
8575 /* Now scan partial symbols in that namespace. */
8576
91c24f0a 8577 if (pdi->has_children)
cdc07690 8578 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8579}
8580
5d7cb8df
JK
8581/* Read a partial die corresponding to a Fortran module. */
8582
8583static void
8584add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8585 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8586{
530e8392
KB
8587 /* Add a symbol for the namespace. */
8588
8589 add_partial_symbol (pdi, cu);
8590
f55ee35c 8591 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8592
8593 if (pdi->has_children)
cdc07690 8594 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8595}
8596
b1dc1806
XR
8597/* Read a partial die corresponding to a subprogram or an inlined
8598 subprogram and create a partial symbol for that subprogram.
8599 When the CU language allows it, this routine also defines a partial
8600 symbol for each nested subprogram that this subprogram contains.
8601 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8602 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8603
cdc07690
YQ
8604 PDI may also be a lexical block, in which case we simply search
8605 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8606 Again, this is only performed when the CU language allows this
8607 type of definitions. */
8608
8609static void
8610add_partial_subprogram (struct partial_die_info *pdi,
8611 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8612 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8613{
b1dc1806 8614 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8615 {
8616 if (pdi->has_pc_info)
8617 {
8618 if (pdi->lowpc < *lowpc)
8619 *lowpc = pdi->lowpc;
8620 if (pdi->highpc > *highpc)
8621 *highpc = pdi->highpc;
cdc07690 8622 if (set_addrmap)
5734ee8b 8623 {
5e22e966 8624 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8625 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8626 CORE_ADDR baseaddr;
b926417a
TT
8627 CORE_ADDR this_highpc;
8628 CORE_ADDR this_lowpc;
5734ee8b 8629
b3b3bada 8630 baseaddr = objfile->text_section_offset ();
b926417a
TT
8631 this_lowpc
8632 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8633 pdi->lowpc + baseaddr)
8634 - baseaddr);
8635 this_highpc
8636 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8637 pdi->highpc + baseaddr)
8638 - baseaddr);
d320c2b5 8639 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8640 this_lowpc, this_highpc - 1,
9291a0cd 8641 cu->per_cu->v.psymtab);
5734ee8b 8642 }
481860b3
GB
8643 }
8644
8645 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8646 {
bc30ff58 8647 if (!pdi->is_declaration)
e8d05480
JB
8648 /* Ignore subprogram DIEs that do not have a name, they are
8649 illegal. Do not emit a complaint at this point, we will
8650 do so when we convert this psymtab into a symtab. */
7d00ffec 8651 if (pdi->name (cu))
e8d05480 8652 add_partial_symbol (pdi, cu);
bc30ff58
JB
8653 }
8654 }
6e70227d 8655
bc30ff58
JB
8656 if (! pdi->has_children)
8657 return;
8658
0a4b0913 8659 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8660 {
8661 pdi = pdi->die_child;
8662 while (pdi != NULL)
8663 {
52356b79 8664 pdi->fixup (cu);
bc30ff58 8665 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8666 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8667 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8668 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8669 pdi = pdi->die_sibling;
8670 }
8671 }
8672}
8673
91c24f0a
DC
8674/* Read a partial die corresponding to an enumeration type. */
8675
72bf9492
DJ
8676static void
8677add_partial_enumeration (struct partial_die_info *enum_pdi,
8678 struct dwarf2_cu *cu)
91c24f0a 8679{
72bf9492 8680 struct partial_die_info *pdi;
91c24f0a 8681
7d00ffec 8682 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8683 add_partial_symbol (enum_pdi, cu);
8684
8685 pdi = enum_pdi->die_child;
8686 while (pdi)
91c24f0a 8687 {
7d00ffec 8688 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8689 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8690 else
72bf9492
DJ
8691 add_partial_symbol (pdi, cu);
8692 pdi = pdi->die_sibling;
91c24f0a 8693 }
91c24f0a
DC
8694}
8695
6caca83c
CC
8696/* Return the initial uleb128 in the die at INFO_PTR. */
8697
8698static unsigned int
d521ce57 8699peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8700{
8701 unsigned int bytes_read;
8702
8703 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8704}
8705
685af9cd
TT
8706/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8707 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8708
4bb7a0a7
DJ
8709 Return the corresponding abbrev, or NULL if the number is zero (indicating
8710 an empty DIE). In either case *BYTES_READ will be set to the length of
8711 the initial number. */
8712
8713static struct abbrev_info *
685af9cd
TT
8714peek_die_abbrev (const die_reader_specs &reader,
8715 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8716{
685af9cd 8717 dwarf2_cu *cu = reader.cu;
5e22e966 8718 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8719 unsigned int abbrev_number
8720 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8721
8722 if (abbrev_number == 0)
8723 return NULL;
8724
685af9cd 8725 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8726 if (!abbrev)
8727 {
422b9917 8728 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8729 " at offset %s [in module %s]"),
422b9917 8730 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8731 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8732 }
8733
8734 return abbrev;
8735}
8736
93311388
DE
8737/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8738 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8739 DIE. Any children of the skipped DIEs will also be skipped. */
8740
d521ce57
TT
8741static const gdb_byte *
8742skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8743{
4bb7a0a7
DJ
8744 while (1)
8745 {
685af9cd
TT
8746 unsigned int bytes_read;
8747 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8748
4bb7a0a7
DJ
8749 if (abbrev == NULL)
8750 return info_ptr + bytes_read;
8751 else
dee91e82 8752 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8753 }
8754}
8755
93311388
DE
8756/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8757 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8758 abbrev corresponding to that skipped uleb128 should be passed in
8759 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8760 children. */
8761
d521ce57
TT
8762static const gdb_byte *
8763skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8764 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8765{
8766 unsigned int bytes_read;
8767 struct attribute attr;
dee91e82
DE
8768 bfd *abfd = reader->abfd;
8769 struct dwarf2_cu *cu = reader->cu;
d521ce57 8770 const gdb_byte *buffer = reader->buffer;
f664829e 8771 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8772 unsigned int form, i;
8773
8774 for (i = 0; i < abbrev->num_attrs; i++)
8775 {
8776 /* The only abbrev we care about is DW_AT_sibling. */
8777 if (abbrev->attrs[i].name == DW_AT_sibling)
8778 {
18a8505e
AT
8779 bool ignored;
8780 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8781 &ignored);
4bb7a0a7 8782 if (attr.form == DW_FORM_ref_addr)
b98664d3 8783 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8784 else
b9502d3f 8785 {
0826b30a 8786 sect_offset off = attr.get_ref_die_offset ();
9c541725 8787 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8788
8789 if (sibling_ptr < info_ptr)
b98664d3 8790 complaint (_("DW_AT_sibling points backwards"));
22869d73 8791 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8792 reader->die_section->overflow_complaint ();
b9502d3f
WN
8793 else
8794 return sibling_ptr;
8795 }
4bb7a0a7
DJ
8796 }
8797
8798 /* If it isn't DW_AT_sibling, skip this attribute. */
8799 form = abbrev->attrs[i].form;
8800 skip_attribute:
8801 switch (form)
8802 {
4bb7a0a7 8803 case DW_FORM_ref_addr:
ae411497
TT
8804 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8805 and later it is offset sized. */
8806 if (cu->header.version == 2)
8807 info_ptr += cu->header.addr_size;
8808 else
8809 info_ptr += cu->header.offset_size;
8810 break;
36586728
TT
8811 case DW_FORM_GNU_ref_alt:
8812 info_ptr += cu->header.offset_size;
8813 break;
ae411497 8814 case DW_FORM_addr:
4bb7a0a7
DJ
8815 info_ptr += cu->header.addr_size;
8816 break;
8817 case DW_FORM_data1:
8818 case DW_FORM_ref1:
8819 case DW_FORM_flag:
8fe0f950 8820 case DW_FORM_strx1:
4bb7a0a7
DJ
8821 info_ptr += 1;
8822 break;
2dc7f7b3 8823 case DW_FORM_flag_present:
43988095 8824 case DW_FORM_implicit_const:
2dc7f7b3 8825 break;
4bb7a0a7
DJ
8826 case DW_FORM_data2:
8827 case DW_FORM_ref2:
8fe0f950 8828 case DW_FORM_strx2:
4bb7a0a7
DJ
8829 info_ptr += 2;
8830 break;
8fe0f950
AT
8831 case DW_FORM_strx3:
8832 info_ptr += 3;
8833 break;
4bb7a0a7
DJ
8834 case DW_FORM_data4:
8835 case DW_FORM_ref4:
8fe0f950 8836 case DW_FORM_strx4:
4bb7a0a7
DJ
8837 info_ptr += 4;
8838 break;
8839 case DW_FORM_data8:
8840 case DW_FORM_ref8:
55f1336d 8841 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8842 info_ptr += 8;
8843 break;
0224619f
JK
8844 case DW_FORM_data16:
8845 info_ptr += 16;
8846 break;
4bb7a0a7 8847 case DW_FORM_string:
9b1c24c8 8848 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8849 info_ptr += bytes_read;
8850 break;
2dc7f7b3 8851 case DW_FORM_sec_offset:
4bb7a0a7 8852 case DW_FORM_strp:
36586728 8853 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8854 info_ptr += cu->header.offset_size;
8855 break;
2dc7f7b3 8856 case DW_FORM_exprloc:
4bb7a0a7
DJ
8857 case DW_FORM_block:
8858 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8859 info_ptr += bytes_read;
8860 break;
8861 case DW_FORM_block1:
8862 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8863 break;
8864 case DW_FORM_block2:
8865 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8866 break;
8867 case DW_FORM_block4:
8868 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8869 break;
336d760d 8870 case DW_FORM_addrx:
cf532bd1 8871 case DW_FORM_strx:
4bb7a0a7
DJ
8872 case DW_FORM_sdata:
8873 case DW_FORM_udata:
8874 case DW_FORM_ref_udata:
3019eac3
DE
8875 case DW_FORM_GNU_addr_index:
8876 case DW_FORM_GNU_str_index:
18a8505e 8877 case DW_FORM_rnglistx:
41144253 8878 case DW_FORM_loclistx:
d521ce57 8879 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8880 break;
8881 case DW_FORM_indirect:
8882 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8883 info_ptr += bytes_read;
8884 /* We need to continue parsing from here, so just go back to
8885 the top. */
8886 goto skip_attribute;
8887
8888 default:
3e43a32a
MS
8889 error (_("Dwarf Error: Cannot handle %s "
8890 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8891 dwarf_form_name (form),
8892 bfd_get_filename (abfd));
8893 }
8894 }
8895
8896 if (abbrev->has_children)
dee91e82 8897 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8898 else
8899 return info_ptr;
8900}
8901
93311388 8902/* Locate ORIG_PDI's sibling.
dee91e82 8903 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8904
d521ce57 8905static const gdb_byte *
dee91e82
DE
8906locate_pdi_sibling (const struct die_reader_specs *reader,
8907 struct partial_die_info *orig_pdi,
d521ce57 8908 const gdb_byte *info_ptr)
91c24f0a
DC
8909{
8910 /* Do we know the sibling already? */
72bf9492 8911
91c24f0a
DC
8912 if (orig_pdi->sibling)
8913 return orig_pdi->sibling;
8914
8915 /* Are there any children to deal with? */
8916
8917 if (!orig_pdi->has_children)
8918 return info_ptr;
8919
4bb7a0a7 8920 /* Skip the children the long way. */
91c24f0a 8921
dee91e82 8922 return skip_children (reader, info_ptr);
91c24f0a
DC
8923}
8924
257e7a09 8925/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8926 not NULL. */
c906108c 8927
891813be
TT
8928void
8929dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8930{
976ca316 8931 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8932
976ca316 8933 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8934
077cbab2
TT
8935 /* If this psymtab is constructed from a debug-only objfile, the
8936 has_section_at_zero flag will not necessarily be correct. We
8937 can get the correct value for this flag by looking at the data
8938 associated with the (presumably stripped) associated objfile. */
8939 if (objfile->separate_debug_objfile_backlink)
c906108c 8940 {
976ca316 8941 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8942 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8943
976ca316
SM
8944 per_objfile->per_bfd->has_section_at_zero
8945 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8946 }
98bfdba5 8947
8566b89b 8948 expand_psymtab (objfile);
95554aad 8949
976ca316 8950 process_cu_includes (per_objfile);
c906108c 8951}
9cdd5dbd
DE
8952\f
8953/* Reading in full CUs. */
c906108c 8954
10b3939b
DJ
8955/* Add PER_CU to the queue. */
8956
8957static void
120ce1b5
SM
8958queue_comp_unit (dwarf2_per_cu_data *per_cu,
8959 dwarf2_per_objfile *per_objfile,
95554aad 8960 enum language pretend_language)
10b3939b 8961{
10b3939b 8962 per_cu->queued = 1;
120ce1b5 8963 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8964}
8965
89e63ee4
DE
8966/* If PER_CU is not yet queued, add it to the queue.
8967 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8968 dependency.
0907af0c 8969 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8970 meaning either PER_CU is already queued or it is already loaded.
8971
8972 N.B. There is an invariant here that if a CU is queued then it is loaded.
8973 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8974
8975static int
89e63ee4 8976maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8977 dwarf2_per_cu_data *per_cu,
8978 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8979 enum language pretend_language)
8980{
8981 /* We may arrive here during partial symbol reading, if we need full
8982 DIEs to process an unusual case (e.g. template arguments). Do
8983 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8984 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8985 {
7188ed02
SM
8986 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8987
8988 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8989 return 1;
8990 return 0;
8991 }
8992
8993 /* Mark the dependence relation so that we don't flush PER_CU
8994 too early. */
89e63ee4
DE
8995 if (dependent_cu != NULL)
8996 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8997
8998 /* If it's already on the queue, we have nothing to do. */
8999 if (per_cu->queued)
9000 return 0;
9001
9002 /* If the compilation unit is already loaded, just mark it as
9003 used. */
7188ed02
SM
9004 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9005 if (cu != nullptr)
0907af0c 9006 {
7188ed02 9007 cu->last_used = 0;
0907af0c
DE
9008 return 0;
9009 }
9010
9011 /* Add it to the queue. */
120ce1b5 9012 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9013
9014 return 1;
9015}
9016
10b3939b
DJ
9017/* Process the queue. */
9018
9019static void
976ca316 9020process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9021{
b4f54984 9022 if (dwarf_read_debug)
45cfd468
DE
9023 {
9024 fprintf_unfiltered (gdb_stdlog,
9025 "Expanding one or more symtabs of objfile %s ...\n",
976ca316 9026 objfile_name (per_objfile->objfile));
45cfd468
DE
9027 }
9028
03dd20cc
DJ
9029 /* The queue starts out with one item, but following a DIE reference
9030 may load a new CU, adding it to the end of the queue. */
976ca316 9031 while (!per_objfile->per_bfd->queue.empty ())
10b3939b 9032 {
976ca316 9033 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
7188ed02 9034 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9035
976ca316 9036 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9037 {
976ca316 9038 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9039
7188ed02
SM
9040 /* Skip dummy CUs. */
9041 if (cu != nullptr)
73be47f5 9042 {
7188ed02
SM
9043 unsigned int debug_print_threshold;
9044 char buf[100];
9045
9046 if (per_cu->is_debug_types)
9047 {
9048 struct signatured_type *sig_type =
9049 (struct signatured_type *) per_cu;
9050
9051 sprintf (buf, "TU %s at offset %s",
9052 hex_string (sig_type->signature),
9053 sect_offset_str (per_cu->sect_off));
9054 /* There can be 100s of TUs.
9055 Only print them in verbose mode. */
9056 debug_print_threshold = 2;
9057 }
9058 else
9059 {
9060 sprintf (buf, "CU at offset %s",
9061 sect_offset_str (per_cu->sect_off));
9062 debug_print_threshold = 1;
9063 }
247f5c4f 9064
7188ed02
SM
9065 if (dwarf_read_debug >= debug_print_threshold)
9066 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17 9067
7188ed02
SM
9068 if (per_cu->is_debug_types)
9069 process_full_type_unit (cu, item.pretend_language);
9070 else
9071 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9072
7188ed02
SM
9073 if (dwarf_read_debug >= debug_print_threshold)
9074 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9075 }
f4dc4d17 9076 }
10b3939b 9077
7188ed02 9078 per_cu->queued = 0;
976ca316 9079 per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9080 }
9081
b4f54984 9082 if (dwarf_read_debug)
45cfd468
DE
9083 {
9084 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
976ca316 9085 objfile_name (per_objfile->objfile));
45cfd468 9086 }
10b3939b
DJ
9087}
9088
10b3939b
DJ
9089/* Read in full symbols for PST, and anything it depends on. */
9090
8566b89b
TT
9091void
9092dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9093{
af758d11 9094 gdb_assert (!readin_p (objfile));
95554aad 9095
17ee85fc
TT
9096 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9097 free_cached_comp_units freer (per_objfile);
48993951 9098 expand_dependencies (objfile);
aaa75496 9099
97a1449a 9100 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9101 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9102}
9103
af758d11
SM
9104/* See psympriv.h. */
9105
9106bool
9107dwarf2_psymtab::readin_p (struct objfile *objfile) const
9108{
9109 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9110 return per_objfile->symtab_set_p (per_cu_data);
9111}
9112
9113/* See psympriv.h. */
9114
9115compunit_symtab *
9116dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9117{
9118 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9119 return per_objfile->get_symtab (per_cu_data);
9120}
9121
dee91e82
DE
9122/* Trivial hash function for die_info: the hash value of a DIE
9123 is its offset in .debug_info for this objfile. */
10b3939b 9124
dee91e82
DE
9125static hashval_t
9126die_hash (const void *item)
10b3939b 9127{
9a3c8263 9128 const struct die_info *die = (const struct die_info *) item;
6502dd73 9129
9c541725 9130 return to_underlying (die->sect_off);
dee91e82 9131}
63d06c5c 9132
dee91e82
DE
9133/* Trivial comparison function for die_info structures: two DIEs
9134 are equal if they have the same offset. */
98bfdba5 9135
dee91e82
DE
9136static int
9137die_eq (const void *item_lhs, const void *item_rhs)
9138{
9a3c8263
SM
9139 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9140 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9141
9c541725 9142 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9143}
c906108c 9144
c0ab21c2 9145/* Load the DIEs associated with PER_CU into memory. */
c906108c 9146
dee91e82 9147static void
ab432490
SM
9148load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9149 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9150 bool skip_partial,
9151 enum language pretend_language)
dee91e82 9152{
c0ab21c2
TT
9153 gdb_assert (! this_cu->is_debug_types);
9154
7188ed02
SM
9155 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9156 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9157 if (reader.dummy_p)
9158 return;
9159
9160 struct dwarf2_cu *cu = reader.cu;
9161 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9162
dee91e82
DE
9163 gdb_assert (cu->die_hash == NULL);
9164 cu->die_hash =
9165 htab_create_alloc_ex (cu->header.length / 12,
9166 die_hash,
9167 die_eq,
9168 NULL,
9169 &cu->comp_unit_obstack,
9170 hashtab_obstack_allocate,
9171 dummy_obstack_deallocate);
e142c38c 9172
3e225074 9173 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9174 reader.comp_unit_die->child
9175 = read_die_and_siblings (&reader, reader.info_ptr,
9176 &info_ptr, reader.comp_unit_die);
9177 cu->dies = reader.comp_unit_die;
dee91e82 9178 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9179
9180 /* We try not to read any attributes in this function, because not
9cdd5dbd 9181 all CUs needed for references have been loaded yet, and symbol
10b3939b 9182 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9183 or we won't be able to build types correctly.
9184 Similarly, if we do not read the producer, we can not apply
9185 producer-specific interpretation. */
c0ab21c2 9186 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9187
9188 reader.keep ();
10b3939b
DJ
9189}
9190
3da10d80
KS
9191/* Add a DIE to the delayed physname list. */
9192
9193static void
9194add_to_method_list (struct type *type, int fnfield_index, int index,
9195 const char *name, struct die_info *die,
9196 struct dwarf2_cu *cu)
9197{
9198 struct delayed_method_info mi;
9199 mi.type = type;
9200 mi.fnfield_index = fnfield_index;
9201 mi.index = index;
9202 mi.name = name;
9203 mi.die = die;
c89b44cd 9204 cu->method_list.push_back (mi);
3da10d80
KS
9205}
9206
3693fdb3
PA
9207/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9208 "const" / "volatile". If so, decrements LEN by the length of the
9209 modifier and return true. Otherwise return false. */
9210
9211template<size_t N>
9212static bool
9213check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9214{
9215 size_t mod_len = sizeof (mod) - 1;
9216 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9217 {
9218 len -= mod_len;
9219 return true;
9220 }
9221 return false;
9222}
9223
3da10d80
KS
9224/* Compute the physnames of any methods on the CU's method list.
9225
9226 The computation of method physnames is delayed in order to avoid the
9227 (bad) condition that one of the method's formal parameters is of an as yet
9228 incomplete type. */
9229
9230static void
9231compute_delayed_physnames (struct dwarf2_cu *cu)
9232{
3693fdb3 9233 /* Only C++ delays computing physnames. */
c89b44cd 9234 if (cu->method_list.empty ())
3693fdb3
PA
9235 return;
9236 gdb_assert (cu->language == language_cplus);
9237
52941706 9238 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9239 {
1d06ead6 9240 const char *physname;
3da10d80 9241 struct fn_fieldlist *fn_flp
c89b44cd
TT
9242 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9243 physname = dwarf2_physname (mi.name, mi.die, cu);
9244 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9245 = physname ? physname : "";
3693fdb3
PA
9246
9247 /* Since there's no tag to indicate whether a method is a
9248 const/volatile overload, extract that information out of the
9249 demangled name. */
9250 if (physname != NULL)
9251 {
9252 size_t len = strlen (physname);
9253
9254 while (1)
9255 {
9256 if (physname[len] == ')') /* shortcut */
9257 break;
9258 else if (check_modifier (physname, len, " const"))
c89b44cd 9259 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9260 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9261 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9262 else
9263 break;
9264 }
9265 }
3da10d80 9266 }
c89b44cd
TT
9267
9268 /* The list is no longer needed. */
9269 cu->method_list.clear ();
3da10d80
KS
9270}
9271
a766d390
DE
9272/* Go objects should be embedded in a DW_TAG_module DIE,
9273 and it's not clear if/how imported objects will appear.
9274 To keep Go support simple until that's worked out,
9275 go back through what we've read and create something usable.
9276 We could do this while processing each DIE, and feels kinda cleaner,
9277 but that way is more invasive.
9278 This is to, for example, allow the user to type "p var" or "b main"
9279 without having to specify the package name, and allow lookups
9280 of module.object to work in contexts that use the expression
9281 parser. */
9282
9283static void
9284fixup_go_packaging (struct dwarf2_cu *cu)
9285{
421d1616 9286 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9287 struct pending *list;
9288 int i;
9289
c24bdb02 9290 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9291 list != NULL;
9292 list = list->next)
a766d390
DE
9293 {
9294 for (i = 0; i < list->nsyms; ++i)
9295 {
9296 struct symbol *sym = list->symbol[i];
9297
c1b5c1eb 9298 if (sym->language () == language_go
a766d390
DE
9299 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9300 {
421d1616
TT
9301 gdb::unique_xmalloc_ptr<char> this_package_name
9302 (go_symbol_package_name (sym));
a766d390
DE
9303
9304 if (this_package_name == NULL)
9305 continue;
9306 if (package_name == NULL)
421d1616 9307 package_name = std::move (this_package_name);
a766d390
DE
9308 else
9309 {
5e22e966 9310 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9311 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9312 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9313 (symbol_symtab (sym) != NULL
9314 ? symtab_to_filename_for_display
9315 (symbol_symtab (sym))
e3b94546 9316 : objfile_name (objfile)),
421d1616 9317 this_package_name.get (), package_name.get ());
a766d390
DE
9318 }
9319 }
9320 }
9321 }
9322
9323 if (package_name != NULL)
9324 {
5e22e966 9325 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9326 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9327 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9328 saved_package_name);
a766d390
DE
9329 struct symbol *sym;
9330
8c14c3a3 9331 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9332 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9333 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9334 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9335 e.g., "main" finds the "main" module and not C's main(). */
9336 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9337 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9338 SYMBOL_TYPE (sym) = type;
9339
c24bdb02 9340 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9341 }
9342}
9343
c9317f21
TT
9344/* Allocate a fully-qualified name consisting of the two parts on the
9345 obstack. */
9346
9347static const char *
9348rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9349{
9350 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9351}
9352
9c6a1327
TT
9353/* A helper that allocates a variant part to attach to a Rust enum
9354 type. OBSTACK is where the results should be allocated. TYPE is
9355 the type we're processing. DISCRIMINANT_INDEX is the index of the
9356 discriminant. It must be the index of one of the fields of TYPE.
9357 DEFAULT_INDEX is the index of the default field; or -1 if there is
9358 no default. RANGES is indexed by "effective" field number (the
9359 field index, but omitting the discriminant and default fields) and
9360 must hold the discriminant values used by the variants. Note that
9361 RANGES must have a lifetime at least as long as OBSTACK -- either
9362 already allocated on it, or static. */
c9317f21 9363
9c6a1327
TT
9364static void
9365alloc_rust_variant (struct obstack *obstack, struct type *type,
9366 int discriminant_index, int default_index,
9367 gdb::array_view<discriminant_range> ranges)
9368{
9369 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9370 must be handled by the caller. */
9371 gdb_assert (discriminant_index >= 0
1f704f76 9372 && discriminant_index < type->num_fields ());
c9317f21 9373 gdb_assert (default_index == -1
1f704f76 9374 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9375
9c6a1327 9376 /* We have one variant for each non-discriminant field. */
1f704f76 9377 int n_variants = type->num_fields () - 1;
c9317f21 9378
9c6a1327
TT
9379 variant *variants = new (obstack) variant[n_variants];
9380 int var_idx = 0;
9381 int range_idx = 0;
1f704f76 9382 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9383 {
9384 if (i == discriminant_index)
9385 continue;
c9317f21 9386
9c6a1327
TT
9387 variants[var_idx].first_field = i;
9388 variants[var_idx].last_field = i + 1;
9389
9390 /* The default field does not need a range, but other fields do.
9391 We skipped the discriminant above. */
9392 if (i != default_index)
9393 {
9394 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9395 ++range_idx;
9396 }
c9317f21 9397
9c6a1327
TT
9398 ++var_idx;
9399 }
9400
9401 gdb_assert (range_idx == ranges.size ());
9402 gdb_assert (var_idx == n_variants);
9403
9404 variant_part *part = new (obstack) variant_part;
9405 part->discriminant_index = discriminant_index;
9406 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9407 discriminant_index));
9408 part->variants = gdb::array_view<variant> (variants, n_variants);
9409
9410 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9411 gdb::array_view<variant_part> *prop_value
9412 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9413
9c6a1327
TT
9414 struct dynamic_prop prop;
9415 prop.kind = PROP_VARIANT_PARTS;
9416 prop.data.variant_parts = prop_value;
9417
5c54719c 9418 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9419}
9420
9421/* Some versions of rustc emitted enums in an unusual way.
9422
9423 Ordinary enums were emitted as unions. The first element of each
9424 structure in the union was named "RUST$ENUM$DISR". This element
9425 held the discriminant.
9426
9427 These versions of Rust also implemented the "non-zero"
9428 optimization. When the enum had two values, and one is empty and
9429 the other holds a pointer that cannot be zero, the pointer is used
9430 as the discriminant, with a zero value meaning the empty variant.
9431 Here, the union's first member is of the form
9432 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9433 where the fieldnos are the indices of the fields that should be
9434 traversed in order to find the field (which may be several fields deep)
9435 and the variantname is the name of the variant of the case when the
9436 field is zero.
9437
9438 This function recognizes whether TYPE is of one of these forms,
9439 and, if so, smashes it to be a variant type. */
9440
9441static void
9442quirk_rust_enum (struct type *type, struct objfile *objfile)
9443{
78134374 9444 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9445
9446 /* We don't need to deal with empty enums. */
1f704f76 9447 if (type->num_fields () == 0)
c9317f21
TT
9448 return;
9449
9450#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9451 if (type->num_fields () == 1
c9317f21
TT
9452 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9453 {
9454 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9455
9456 /* Decode the field name to find the offset of the
9457 discriminant. */
9458 ULONGEST bit_offset = 0;
9459 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9460 while (name[0] >= '0' && name[0] <= '9')
9461 {
9462 char *tail;
9463 unsigned long index = strtoul (name, &tail, 10);
9464 name = tail;
9465 if (*name != '$'
1f704f76 9466 || index >= field_type->num_fields ()
c9317f21
TT
9467 || (TYPE_FIELD_LOC_KIND (field_type, index)
9468 != FIELD_LOC_KIND_BITPOS))
9469 {
b98664d3 9470 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9471 "[in module %s]"),
9472 TYPE_FIELD_NAME (type, 0),
9473 objfile_name (objfile));
9474 return;
9475 }
9476 ++name;
9477
9478 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9479 field_type = TYPE_FIELD_TYPE (field_type, index);
9480 }
9481
9c6a1327
TT
9482 /* Smash this type to be a structure type. We have to do this
9483 because the type has already been recorded. */
67607e24 9484 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9485 type->set_num_fields (3);
9c6a1327 9486 /* Save the field we care about. */
ceacbf6e 9487 struct field saved_field = type->field (0);
3cabb6b0
SM
9488 type->set_fields
9489 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9490
9c6a1327
TT
9491 /* Put the discriminant at index 0. */
9492 TYPE_FIELD_TYPE (type, 0) = field_type;
9493 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9494 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9495 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9496
9497 /* The order of fields doesn't really matter, so put the real
9498 field at index 1 and the data-less field at index 2. */
ceacbf6e 9499 type->field (1) = saved_field;
9c6a1327 9500 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9501 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9502 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9503 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9504 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9505
9506 const char *dataless_name
7d93a1e0 9507 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9508 name);
9509 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9510 dataless_name);
9c6a1327 9511 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9512 /* NAME points into the original discriminant name, which
9513 already has the correct lifetime. */
9c6a1327 9514 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9515 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9516
9c6a1327
TT
9517 /* Indicate that this is a variant type. */
9518 static discriminant_range ranges[1] = { { 0, 0 } };
9519 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9520 }
77c2dba3
TT
9521 /* A union with a single anonymous field is probably an old-style
9522 univariant enum. */
1f704f76 9523 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9524 {
c9317f21
TT
9525 /* Smash this type to be a structure type. We have to do this
9526 because the type has already been recorded. */
67607e24 9527 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9528
9c6a1327 9529 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9530 const char *variant_name
7d93a1e0 9531 = rust_last_path_segment (field_type->name ());
9c6a1327 9532 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9533 field_type->set_name
9534 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9535 type->name (), variant_name));
c9317f21
TT
9536 }
9537 else
9538 {
9539 struct type *disr_type = nullptr;
1f704f76 9540 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9541 {
9542 disr_type = TYPE_FIELD_TYPE (type, i);
9543
78134374 9544 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9545 {
9546 /* All fields of a true enum will be structs. */
9547 return;
9548 }
1f704f76 9549 else if (disr_type->num_fields () == 0)
c9317f21
TT
9550 {
9551 /* Could be data-less variant, so keep going. */
a037790e 9552 disr_type = nullptr;
c9317f21
TT
9553 }
9554 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9555 "RUST$ENUM$DISR") != 0)
9556 {
9557 /* Not a Rust enum. */
9558 return;
9559 }
9560 else
9561 {
9562 /* Found one. */
9563 break;
9564 }
9565 }
9566
9567 /* If we got here without a discriminant, then it's probably
9568 just a union. */
9569 if (disr_type == nullptr)
9570 return;
9571
9572 /* Smash this type to be a structure type. We have to do this
9573 because the type has already been recorded. */
67607e24 9574 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9575
9c6a1327 9576 /* Make space for the discriminant field. */
ceacbf6e 9577 struct field *disr_field = &disr_type->field (0);
9c6a1327 9578 field *new_fields
1f704f76 9579 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9580 * sizeof (struct field)));
80fc5e77 9581 memcpy (new_fields + 1, type->fields (),
1f704f76 9582 type->num_fields () * sizeof (struct field));
3cabb6b0 9583 type->set_fields (new_fields);
1f704f76 9584 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9585
9586 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9587 type->field (0) = *disr_field;
9c6a1327
TT
9588 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9589 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9590
9591 /* We need a way to find the correct discriminant given a
9592 variant name. For convenience we build a map here. */
9593 struct type *enum_type = FIELD_TYPE (*disr_field);
9594 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9595 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9596 {
9597 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9598 {
9599 const char *name
9600 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9601 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9602 }
9603 }
9604
1f704f76 9605 int n_fields = type->num_fields ();
9c6a1327
TT
9606 /* We don't need a range entry for the discriminant, but we do
9607 need one for every other field, as there is no default
9608 variant. */
9609 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9610 discriminant_range,
9611 n_fields - 1);
c9317f21
TT
9612 /* Skip the discriminant here. */
9613 for (int i = 1; i < n_fields; ++i)
9614 {
9615 /* Find the final word in the name of this variant's type.
9616 That name can be used to look up the correct
9617 discriminant. */
9618 const char *variant_name
7d93a1e0 9619 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9620
9621 auto iter = discriminant_map.find (variant_name);
9622 if (iter != discriminant_map.end ())
9c6a1327
TT
9623 {
9624 ranges[i].low = iter->second;
9625 ranges[i].high = iter->second;
9626 }
c9317f21 9627
bedda9ac 9628 /* Remove the discriminant field, if it exists. */
9c6a1327 9629 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9630 if (sub_type->num_fields () > 0)
bedda9ac 9631 {
5e33d5f4 9632 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9633 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9634 }
9c6a1327 9635 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9636 sub_type->set_name
9637 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9638 type->name (), variant_name));
c9317f21 9639 }
9c6a1327
TT
9640
9641 /* Indicate that this is a variant type. */
9642 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9643 gdb::array_view<discriminant_range> (ranges,
9644 n_fields - 1));
c9317f21
TT
9645 }
9646}
9647
9648/* Rewrite some Rust unions to be structures with variants parts. */
9649
9650static void
9651rust_union_quirks (struct dwarf2_cu *cu)
9652{
9653 gdb_assert (cu->language == language_rust);
52941706 9654 for (type *type_ : cu->rust_unions)
5e22e966 9655 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9656 /* We don't need this any more. */
9657 cu->rust_unions.clear ();
c9317f21
TT
9658}
9659
8adb8487
TT
9660/* See read.h. */
9661
9662type_unit_group_unshareable *
9663dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9664{
9665 auto iter = this->m_type_units.find (tu_group);
9666 if (iter != this->m_type_units.end ())
9667 return iter->second.get ();
9668
9669 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9670 type_unit_group_unshareable *result = uniq.get ();
9671 this->m_type_units[tu_group] = std::move (uniq);
9672 return result;
9673}
9674
e286671b
TT
9675struct type *
9676dwarf2_per_objfile::get_type_for_signatured_type
9677 (signatured_type *sig_type) const
9678{
9679 auto iter = this->m_type_map.find (sig_type);
9680 if (iter == this->m_type_map.end ())
9681 return nullptr;
9682
9683 return iter->second;
9684}
9685
9686void dwarf2_per_objfile::set_type_for_signatured_type
9687 (signatured_type *sig_type, struct type *type)
9688{
9689 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9690
9691 this->m_type_map[sig_type] = type;
9692}
9693
95554aad
TT
9694/* A helper function for computing the list of all symbol tables
9695 included by PER_CU. */
9696
9697static void
4c39bc03 9698recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9699 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9700 dwarf2_per_cu_data *per_cu,
9701 dwarf2_per_objfile *per_objfile,
43f3e411 9702 struct compunit_symtab *immediate_parent)
95554aad 9703{
af758d11 9704 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9705 if (*slot != NULL)
9706 {
9707 /* This inclusion and its children have been processed. */
9708 return;
9709 }
9710
9711 *slot = per_cu;
af758d11 9712
95554aad 9713 /* Only add a CU if it has a symbol table. */
43182c09 9714 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9715 if (cust != NULL)
ec94af83
DE
9716 {
9717 /* If this is a type unit only add its symbol table if we haven't
9718 seen it yet (type unit per_cu's can share symtabs). */
9719 if (per_cu->is_debug_types)
9720 {
43f3e411 9721 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9722 if (*slot == NULL)
9723 {
43f3e411 9724 *slot = cust;
4c39bc03 9725 result->push_back (cust);
43f3e411
DE
9726 if (cust->user == NULL)
9727 cust->user = immediate_parent;
ec94af83
DE
9728 }
9729 }
9730 else
f9125b6c 9731 {
4c39bc03 9732 result->push_back (cust);
43f3e411
DE
9733 if (cust->user == NULL)
9734 cust->user = immediate_parent;
f9125b6c 9735 }
ec94af83 9736 }
95554aad 9737
ae640021
AB
9738 if (!per_cu->imported_symtabs_empty ())
9739 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9740 {
9741 recursively_compute_inclusions (result, all_children,
43182c09
SM
9742 all_type_symtabs, ptr, per_objfile,
9743 cust);
ae640021 9744 }
95554aad
TT
9745}
9746
43f3e411 9747/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9748 PER_CU. */
9749
9750static void
43182c09
SM
9751compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9752 dwarf2_per_objfile *per_objfile)
95554aad 9753{
f4dc4d17
DE
9754 gdb_assert (! per_cu->is_debug_types);
9755
ae640021 9756 if (!per_cu->imported_symtabs_empty ())
95554aad 9757 {
ae640021 9758 int len;
4c39bc03 9759 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9760 htab_t all_children, all_type_symtabs;
43182c09 9761 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9762
9763 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9764 if (cust == NULL)
95554aad
TT
9765 return;
9766
9767 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9768 NULL, xcalloc, xfree);
ec94af83
DE
9769 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9770 NULL, xcalloc, xfree);
95554aad 9771
ae640021 9772 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9773 {
9774 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9775 all_type_symtabs, ptr, per_objfile,
9776 cust);
ec94af83 9777 }
95554aad 9778
ec94af83 9779 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9780 len = result_symtabs.size ();
43f3e411 9781 cust->includes
f6e649dd 9782 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9783 struct compunit_symtab *, len + 1);
4c39bc03
TT
9784 memcpy (cust->includes, result_symtabs.data (),
9785 len * sizeof (compunit_symtab *));
43f3e411 9786 cust->includes[len] = NULL;
95554aad 9787
95554aad 9788 htab_delete (all_children);
ec94af83 9789 htab_delete (all_type_symtabs);
95554aad
TT
9790 }
9791}
9792
9793/* Compute the 'includes' field for the symtabs of all the CUs we just
9794 read. */
9795
9796static void
976ca316 9797process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9798{
976ca316 9799 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9800 {
9801 if (! iter->is_debug_types)
976ca316 9802 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9803 }
95554aad 9804
976ca316 9805 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9806}
9807
8fc0b21d 9808/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9809 already been loaded into memory. */
9810
9811static void
8fc0b21d 9812process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9813{
976ca316
SM
9814 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9815 struct objfile *objfile = per_objfile->objfile;
08feed99 9816 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9817 CORE_ADDR lowpc, highpc;
43f3e411 9818 struct compunit_symtab *cust;
10b3939b 9819 CORE_ADDR baseaddr;
4359dff1 9820 struct block *static_block;
3e29f34a 9821 CORE_ADDR addr;
10b3939b 9822
b3b3bada 9823 baseaddr = objfile->text_section_offset ();
10b3939b 9824
c89b44cd
TT
9825 /* Clear the list here in case something was left over. */
9826 cu->method_list.clear ();
10b3939b 9827
95554aad
TT
9828 cu->language = pretend_language;
9829 cu->language_defn = language_def (cu->language);
9830
c906108c 9831 /* Do line number decoding in read_file_scope () */
10b3939b 9832 process_die (cu->dies, cu);
c906108c 9833
a766d390
DE
9834 /* For now fudge the Go package. */
9835 if (cu->language == language_go)
9836 fixup_go_packaging (cu);
9837
5f48f8f3 9838 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9839 should be complete, and it should now be safe to compute all of the
9840 physnames. */
9841 compute_delayed_physnames (cu);
3da10d80 9842
c9317f21
TT
9843 if (cu->language == language_rust)
9844 rust_union_quirks (cu);
9845
fae299cd
DC
9846 /* Some compilers don't define a DW_AT_high_pc attribute for the
9847 compilation unit. If the DW_AT_high_pc is missing, synthesize
9848 it, by scanning the DIE's below the compilation unit. */
10b3939b 9849 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9850
3e29f34a 9851 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9852 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9853
9854 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9855 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9856 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9857 addrmap to help ensure it has an accurate map of pc values belonging to
9858 this comp unit. */
9859 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9860
c24bdb02 9861 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9862 SECT_OFF_TEXT (objfile),
9863 0);
c906108c 9864
43f3e411 9865 if (cust != NULL)
c906108c 9866 {
df15bd07 9867 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9868
8be455d7
JK
9869 /* Set symtab language to language from DW_AT_language. If the
9870 compilation is from a C file generated by language preprocessors, do
9871 not set the language if it was already deduced by start_subfile. */
43f3e411 9872 if (!(cu->language == language_c
40e3ad0e 9873 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9874 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9875
9876 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9877 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9878 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9879 there were bugs in prologue debug info, fixed later in GCC-4.5
9880 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9881
9882 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9883 needed, it would be wrong due to missing DW_AT_producer there.
9884
9885 Still one can confuse GDB by using non-standard GCC compilation
9886 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9887 */
ab260dad 9888 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9889 cust->locations_valid = 1;
e0d00bc7
JK
9890
9891 if (gcc_4_minor >= 5)
43f3e411 9892 cust->epilogue_unwind_valid = 1;
96408a79 9893
43f3e411 9894 cust->call_site_htab = cu->call_site_htab;
c906108c 9895 }
9291a0cd 9896
976ca316 9897 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9898
95554aad 9899 /* Push it for inclusion processing later. */
976ca316 9900 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9901
9902 /* Not needed any more. */
c24bdb02 9903 cu->reset_builder ();
f4dc4d17 9904}
45cfd468 9905
8fc0b21d 9906/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9907 already been loaded into memory. */
9908
9909static void
8fc0b21d 9910process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9911 enum language pretend_language)
9912{
976ca316
SM
9913 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9914 struct objfile *objfile = per_objfile->objfile;
43f3e411 9915 struct compunit_symtab *cust;
0186c6a7
DE
9916 struct signatured_type *sig_type;
9917
8fc0b21d
SM
9918 gdb_assert (cu->per_cu->is_debug_types);
9919 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9920
c89b44cd
TT
9921 /* Clear the list here in case something was left over. */
9922 cu->method_list.clear ();
f4dc4d17 9923
f4dc4d17
DE
9924 cu->language = pretend_language;
9925 cu->language_defn = language_def (cu->language);
9926
9927 /* The symbol tables are set up in read_type_unit_scope. */
9928 process_die (cu->dies, cu);
9929
9930 /* For now fudge the Go package. */
9931 if (cu->language == language_go)
9932 fixup_go_packaging (cu);
9933
5f48f8f3 9934 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9935 should be complete, and it should now be safe to compute all of the
9936 physnames. */
9937 compute_delayed_physnames (cu);
f4dc4d17 9938
c9317f21
TT
9939 if (cu->language == language_rust)
9940 rust_union_quirks (cu);
9941
f4dc4d17
DE
9942 /* TUs share symbol tables.
9943 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9944 of it with end_expandable_symtab. Otherwise, complete the addition of
9945 this TU's symbols to the existing symtab. */
8adb8487 9946 type_unit_group_unshareable *tug_unshare =
976ca316 9947 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9948 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9949 {
c24bdb02
KS
9950 buildsym_compunit *builder = cu->get_builder ();
9951 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9952 tug_unshare->compunit_symtab = cust;
f4dc4d17 9953
43f3e411 9954 if (cust != NULL)
f4dc4d17
DE
9955 {
9956 /* Set symtab language to language from DW_AT_language. If the
9957 compilation is from a C file generated by language preprocessors,
9958 do not set the language if it was already deduced by
9959 start_subfile. */
43f3e411
DE
9960 if (!(cu->language == language_c
9961 && COMPUNIT_FILETABS (cust)->language != language_c))
9962 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9963 }
9964 }
9965 else
9966 {
c24bdb02 9967 cu->get_builder ()->augment_type_symtab ();
8adb8487 9968 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9969 }
9970
976ca316 9971 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9972
9973 /* Not needed any more. */
c24bdb02 9974 cu->reset_builder ();
c906108c
SS
9975}
9976
95554aad
TT
9977/* Process an imported unit DIE. */
9978
9979static void
9980process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9981{
9982 struct attribute *attr;
9983
f4dc4d17
DE
9984 /* For now we don't handle imported units in type units. */
9985 if (cu->per_cu->is_debug_types)
9986 {
9987 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9988 " supported in type units [in module %s]"),
5e22e966 9989 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9990 }
9991
95554aad
TT
9992 attr = dwarf2_attr (die, DW_AT_import, cu);
9993 if (attr != NULL)
9994 {
0826b30a 9995 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9996 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9997 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9998 dwarf2_per_cu_data *per_cu
ab432490 9999 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10000
58990295
TV
10001 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10002 into another compilation unit, at root level. Regard this as a hint,
10003 and ignore it. */
10004 if (die->parent && die->parent->parent == NULL
10005 && per_cu->unit_type == DW_UT_compile
10006 && per_cu->lang == language_cplus)
10007 return;
10008
69d751e3 10009 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10010 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 10011 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 10012
ae640021 10013 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10014 }
10015}
10016
4c8aa72d
PA
10017/* RAII object that represents a process_die scope: i.e.,
10018 starts/finishes processing a DIE. */
10019class process_die_scope
adde2bff 10020{
4c8aa72d
PA
10021public:
10022 process_die_scope (die_info *die, dwarf2_cu *cu)
10023 : m_die (die), m_cu (cu)
10024 {
10025 /* We should only be processing DIEs not already in process. */
10026 gdb_assert (!m_die->in_process);
10027 m_die->in_process = true;
10028 }
8c3cb9fa 10029
4c8aa72d
PA
10030 ~process_die_scope ()
10031 {
10032 m_die->in_process = false;
10033
10034 /* If we're done processing the DIE for the CU that owns the line
10035 header, we don't need the line header anymore. */
10036 if (m_cu->line_header_die_owner == m_die)
10037 {
10038 delete m_cu->line_header;
10039 m_cu->line_header = NULL;
10040 m_cu->line_header_die_owner = NULL;
10041 }
10042 }
10043
10044private:
10045 die_info *m_die;
10046 dwarf2_cu *m_cu;
10047};
adde2bff 10048
c906108c
SS
10049/* Process a die and its children. */
10050
10051static void
e7c27a73 10052process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10053{
4c8aa72d 10054 process_die_scope scope (die, cu);
adde2bff 10055
c906108c
SS
10056 switch (die->tag)
10057 {
10058 case DW_TAG_padding:
10059 break;
10060 case DW_TAG_compile_unit:
95554aad 10061 case DW_TAG_partial_unit:
e7c27a73 10062 read_file_scope (die, cu);
c906108c 10063 break;
348e048f
DE
10064 case DW_TAG_type_unit:
10065 read_type_unit_scope (die, cu);
10066 break;
c906108c 10067 case DW_TAG_subprogram:
0a4b0913
AB
10068 /* Nested subprograms in Fortran get a prefix. */
10069 if (cu->language == language_fortran
10070 && die->parent != NULL
10071 && die->parent->tag == DW_TAG_subprogram)
10072 cu->processing_has_namespace_info = true;
10073 /* Fall through. */
c906108c 10074 case DW_TAG_inlined_subroutine:
edb3359d 10075 read_func_scope (die, cu);
c906108c
SS
10076 break;
10077 case DW_TAG_lexical_block:
14898363
L
10078 case DW_TAG_try_block:
10079 case DW_TAG_catch_block:
e7c27a73 10080 read_lexical_block_scope (die, cu);
c906108c 10081 break;
216f72a1 10082 case DW_TAG_call_site:
96408a79
SA
10083 case DW_TAG_GNU_call_site:
10084 read_call_site_scope (die, cu);
10085 break;
c906108c 10086 case DW_TAG_class_type:
680b30c7 10087 case DW_TAG_interface_type:
c906108c
SS
10088 case DW_TAG_structure_type:
10089 case DW_TAG_union_type:
134d01f1 10090 process_structure_scope (die, cu);
c906108c
SS
10091 break;
10092 case DW_TAG_enumeration_type:
134d01f1 10093 process_enumeration_scope (die, cu);
c906108c 10094 break;
134d01f1 10095
f792889a
DJ
10096 /* These dies have a type, but processing them does not create
10097 a symbol or recurse to process the children. Therefore we can
10098 read them on-demand through read_type_die. */
c906108c 10099 case DW_TAG_subroutine_type:
72019c9c 10100 case DW_TAG_set_type:
c906108c 10101 case DW_TAG_array_type:
c906108c 10102 case DW_TAG_pointer_type:
c906108c 10103 case DW_TAG_ptr_to_member_type:
c906108c 10104 case DW_TAG_reference_type:
4297a3f0 10105 case DW_TAG_rvalue_reference_type:
c906108c 10106 case DW_TAG_string_type:
c906108c 10107 break;
134d01f1 10108
c906108c 10109 case DW_TAG_base_type:
a02abb62 10110 case DW_TAG_subrange_type:
cb249c71 10111 case DW_TAG_typedef:
134d01f1
DJ
10112 /* Add a typedef symbol for the type definition, if it has a
10113 DW_AT_name. */
f792889a 10114 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10115 break;
c906108c 10116 case DW_TAG_common_block:
e7c27a73 10117 read_common_block (die, cu);
c906108c
SS
10118 break;
10119 case DW_TAG_common_inclusion:
10120 break;
d9fa45fe 10121 case DW_TAG_namespace:
9068261f 10122 cu->processing_has_namespace_info = true;
e7c27a73 10123 read_namespace (die, cu);
d9fa45fe 10124 break;
5d7cb8df 10125 case DW_TAG_module:
9068261f 10126 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10127 read_module (die, cu);
10128 break;
d9fa45fe 10129 case DW_TAG_imported_declaration:
9068261f 10130 cu->processing_has_namespace_info = true;
74921315
KS
10131 if (read_namespace_alias (die, cu))
10132 break;
86a73007
TT
10133 /* The declaration is not a global namespace alias. */
10134 /* Fall through. */
d9fa45fe 10135 case DW_TAG_imported_module:
9068261f 10136 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10137 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10138 || cu->language != language_fortran))
b98664d3 10139 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10140 dwarf_tag_name (die->tag));
10141 read_import_statement (die, cu);
d9fa45fe 10142 break;
95554aad
TT
10143
10144 case DW_TAG_imported_unit:
10145 process_imported_unit_die (die, cu);
10146 break;
10147
71a3c369
TT
10148 case DW_TAG_variable:
10149 read_variable (die, cu);
10150 break;
10151
c906108c 10152 default:
e7c27a73 10153 new_symbol (die, NULL, cu);
c906108c
SS
10154 break;
10155 }
10156}
ca69b9e6
DE
10157\f
10158/* DWARF name computation. */
c906108c 10159
94af9270
KS
10160/* A helper function for dwarf2_compute_name which determines whether DIE
10161 needs to have the name of the scope prepended to the name listed in the
10162 die. */
10163
10164static int
10165die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10166{
1c809c68
TT
10167 struct attribute *attr;
10168
94af9270
KS
10169 switch (die->tag)
10170 {
10171 case DW_TAG_namespace:
10172 case DW_TAG_typedef:
10173 case DW_TAG_class_type:
10174 case DW_TAG_interface_type:
10175 case DW_TAG_structure_type:
10176 case DW_TAG_union_type:
10177 case DW_TAG_enumeration_type:
10178 case DW_TAG_enumerator:
10179 case DW_TAG_subprogram:
08a76f8a 10180 case DW_TAG_inlined_subroutine:
94af9270 10181 case DW_TAG_member:
74921315 10182 case DW_TAG_imported_declaration:
94af9270
KS
10183 return 1;
10184
10185 case DW_TAG_variable:
c2b0a229 10186 case DW_TAG_constant:
94af9270
KS
10187 /* We only need to prefix "globally" visible variables. These include
10188 any variable marked with DW_AT_external or any variable that
10189 lives in a namespace. [Variables in anonymous namespaces
10190 require prefixing, but they are not DW_AT_external.] */
10191
10192 if (dwarf2_attr (die, DW_AT_specification, cu))
10193 {
10194 struct dwarf2_cu *spec_cu = cu;
9a619af0 10195
94af9270
KS
10196 return die_needs_namespace (die_specification (die, &spec_cu),
10197 spec_cu);
10198 }
10199
1c809c68 10200 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10201 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10202 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10203 return 0;
10204 /* A variable in a lexical block of some kind does not need a
10205 namespace, even though in C++ such variables may be external
10206 and have a mangled name. */
10207 if (die->parent->tag == DW_TAG_lexical_block
10208 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10209 || die->parent->tag == DW_TAG_catch_block
10210 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10211 return 0;
10212 return 1;
94af9270
KS
10213
10214 default:
10215 return 0;
10216 }
10217}
10218
73b9be8b
KS
10219/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10220 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10221 defined for the given DIE. */
10222
10223static struct attribute *
10224dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10225{
10226 struct attribute *attr;
10227
10228 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10229 if (attr == NULL)
10230 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10231
10232 return attr;
10233}
10234
10235/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10236 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10237 defined for the given DIE. */
10238
10239static const char *
10240dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10241{
10242 const char *linkage_name;
10243
10244 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10245 if (linkage_name == NULL)
10246 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10247
787de330
TT
10248 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10249 See https://github.com/rust-lang/rust/issues/32925. */
10250 if (cu->language == language_rust && linkage_name != NULL
10251 && strchr (linkage_name, '{') != NULL)
10252 linkage_name = NULL;
10253
73b9be8b
KS
10254 return linkage_name;
10255}
10256
94af9270 10257/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10258 compute the physname for the object, which include a method's:
9c37b5ae 10259 - formal parameters (C++),
a766d390 10260 - receiver type (Go),
a766d390
DE
10261
10262 The term "physname" is a bit confusing.
10263 For C++, for example, it is the demangled name.
10264 For Go, for example, it's the mangled name.
94af9270 10265
af6b7be1
JB
10266 For Ada, return the DIE's linkage name rather than the fully qualified
10267 name. PHYSNAME is ignored..
10268
5989a64e 10269 The result is allocated on the objfile->per_bfd's obstack and
45940949 10270 canonicalized. */
94af9270
KS
10271
10272static const char *
15d034d0
TT
10273dwarf2_compute_name (const char *name,
10274 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10275 int physname)
10276{
5e22e966 10277 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10278
94af9270
KS
10279 if (name == NULL)
10280 name = dwarf2_name (die, cu);
10281
2ee7123e
DE
10282 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10283 but otherwise compute it by typename_concat inside GDB.
10284 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10285 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10286 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10287 will set the demangled name to the result of dwarf2_full_name, and it is
10288 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10289 if (cu->language == language_ada
10290 || (cu->language == language_fortran && physname))
10291 {
10292 /* For Ada unit, we prefer the linkage name over the name, as
10293 the former contains the exported name, which the user expects
10294 to be able to reference. Ideally, we want the user to be able
10295 to reference this entity using either natural or linkage name,
10296 but we haven't started looking at this enhancement yet. */
73b9be8b 10297 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10298
2ee7123e
DE
10299 if (linkage_name != NULL)
10300 return linkage_name;
f55ee35c
JK
10301 }
10302
94af9270
KS
10303 /* These are the only languages we know how to qualify names in. */
10304 if (name != NULL
9c37b5ae 10305 && (cu->language == language_cplus
c44af4eb
TT
10306 || cu->language == language_fortran || cu->language == language_d
10307 || cu->language == language_rust))
94af9270
KS
10308 {
10309 if (die_needs_namespace (die, cu))
10310 {
0d5cff50 10311 const char *prefix;
34a68019 10312 const char *canonical_name = NULL;
94af9270 10313
d7e74731
PA
10314 string_file buf;
10315
94af9270 10316 prefix = determine_prefix (die, cu);
94af9270
KS
10317 if (*prefix != '\0')
10318 {
43816ebc
TT
10319 gdb::unique_xmalloc_ptr<char> prefixed_name
10320 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10321
43816ebc 10322 buf.puts (prefixed_name.get ());
94af9270
KS
10323 }
10324 else
d7e74731 10325 buf.puts (name);
94af9270 10326
98bfdba5
PA
10327 /* Template parameters may be specified in the DIE's DW_AT_name, or
10328 as children with DW_TAG_template_type_param or
10329 DW_TAG_value_type_param. If the latter, add them to the name
10330 here. If the name already has template parameters, then
10331 skip this step; some versions of GCC emit both, and
10332 it is more efficient to use the pre-computed name.
10333
10334 Something to keep in mind about this process: it is very
10335 unlikely, or in some cases downright impossible, to produce
10336 something that will match the mangled name of a function.
10337 If the definition of the function has the same debug info,
10338 we should be able to match up with it anyway. But fallbacks
10339 using the minimal symbol, for instance to find a method
10340 implemented in a stripped copy of libstdc++, will not work.
10341 If we do not have debug info for the definition, we will have to
10342 match them up some other way.
10343
10344 When we do name matching there is a related problem with function
10345 templates; two instantiated function templates are allowed to
10346 differ only by their return types, which we do not add here. */
10347
10348 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10349 {
10350 struct attribute *attr;
10351 struct die_info *child;
10352 int first = 1;
10353
10354 die->building_fullname = 1;
10355
10356 for (child = die->child; child != NULL; child = child->sibling)
10357 {
10358 struct type *type;
12df843f 10359 LONGEST value;
d521ce57 10360 const gdb_byte *bytes;
98bfdba5
PA
10361 struct dwarf2_locexpr_baton *baton;
10362 struct value *v;
10363
10364 if (child->tag != DW_TAG_template_type_param
10365 && child->tag != DW_TAG_template_value_param)
10366 continue;
10367
10368 if (first)
10369 {
d7e74731 10370 buf.puts ("<");
98bfdba5
PA
10371 first = 0;
10372 }
10373 else
d7e74731 10374 buf.puts (", ");
98bfdba5
PA
10375
10376 attr = dwarf2_attr (child, DW_AT_type, cu);
10377 if (attr == NULL)
10378 {
b98664d3 10379 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10380 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10381 continue;
10382 }
10383 type = die_type (child, cu);
10384
10385 if (child->tag == DW_TAG_template_type_param)
10386 {
c1ec8cea
TT
10387 c_print_type (type, "", &buf, -1, 0, cu->language,
10388 &type_print_raw_options);
98bfdba5
PA
10389 continue;
10390 }
10391
10392 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10393 if (attr == NULL)
10394 {
b98664d3 10395 complaint (_("template parameter missing "
3e43a32a 10396 "DW_AT_const_value"));
d7e74731 10397 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10398 continue;
10399 }
10400
10401 dwarf2_const_value_attr (attr, type, name,
10402 &cu->comp_unit_obstack, cu,
10403 &value, &bytes, &baton);
10404
10405 if (TYPE_NOSIGN (type))
10406 /* GDB prints characters as NUMBER 'CHAR'. If that's
10407 changed, this can use value_print instead. */
d7e74731 10408 c_printchar (value, type, &buf);
98bfdba5
PA
10409 else
10410 {
10411 struct value_print_options opts;
10412
10413 if (baton != NULL)
10414 v = dwarf2_evaluate_loc_desc (type, NULL,
10415 baton->data,
10416 baton->size,
9f47c707
SM
10417 baton->per_cu,
10418 baton->per_objfile);
98bfdba5
PA
10419 else if (bytes != NULL)
10420 {
10421 v = allocate_value (type);
10422 memcpy (value_contents_writeable (v), bytes,
10423 TYPE_LENGTH (type));
10424 }
10425 else
10426 v = value_from_longest (type, value);
10427
3e43a32a
MS
10428 /* Specify decimal so that we do not depend on
10429 the radix. */
98bfdba5
PA
10430 get_formatted_print_options (&opts, 'd');
10431 opts.raw = 1;
d7e74731 10432 value_print (v, &buf, &opts);
98bfdba5 10433 release_value (v);
98bfdba5
PA
10434 }
10435 }
10436
10437 die->building_fullname = 0;
10438
10439 if (!first)
10440 {
10441 /* Close the argument list, with a space if necessary
10442 (nested templates). */
d7e74731
PA
10443 if (!buf.empty () && buf.string ().back () == '>')
10444 buf.puts (" >");
98bfdba5 10445 else
d7e74731 10446 buf.puts (">");
98bfdba5
PA
10447 }
10448 }
10449
9c37b5ae 10450 /* For C++ methods, append formal parameter type
94af9270 10451 information, if PHYSNAME. */
6e70227d 10452
94af9270 10453 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10454 && cu->language == language_cplus)
94af9270
KS
10455 {
10456 struct type *type = read_type_die (die, cu);
10457
d7e74731 10458 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10459 &type_print_raw_options);
94af9270 10460
9c37b5ae 10461 if (cu->language == language_cplus)
94af9270 10462 {
60430eff
DJ
10463 /* Assume that an artificial first parameter is
10464 "this", but do not crash if it is not. RealView
10465 marks unnamed (and thus unused) parameters as
10466 artificial; there is no way to differentiate
10467 the two cases. */
1f704f76 10468 if (type->num_fields () > 0
94af9270 10469 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10470 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10471 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10472 0))))
d7e74731 10473 buf.puts (" const");
94af9270
KS
10474 }
10475 }
10476
d7e74731 10477 const std::string &intermediate_name = buf.string ();
94af9270
KS
10478
10479 if (cu->language == language_cplus)
34a68019 10480 canonical_name
322a8516 10481 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10482 objfile);
34a68019
TT
10483
10484 /* If we only computed INTERMEDIATE_NAME, or if
10485 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10486 intern it. */
322a8516 10487 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10488 name = objfile->intern (intermediate_name);
34a68019
TT
10489 else
10490 name = canonical_name;
94af9270
KS
10491 }
10492 }
10493
10494 return name;
10495}
10496
0114d602
DJ
10497/* Return the fully qualified name of DIE, based on its DW_AT_name.
10498 If scope qualifiers are appropriate they will be added. The result
34a68019 10499 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10500 not have a name. NAME may either be from a previous call to
10501 dwarf2_name or NULL.
10502
9c37b5ae 10503 The output string will be canonicalized (if C++). */
0114d602
DJ
10504
10505static const char *
15d034d0 10506dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10507{
94af9270
KS
10508 return dwarf2_compute_name (name, die, cu, 0);
10509}
0114d602 10510
94af9270
KS
10511/* Construct a physname for the given DIE in CU. NAME may either be
10512 from a previous call to dwarf2_name or NULL. The result will be
10513 allocated on the objfile_objstack or NULL if the DIE does not have a
10514 name.
0114d602 10515
9c37b5ae 10516 The output string will be canonicalized (if C++). */
0114d602 10517
94af9270 10518static const char *
15d034d0 10519dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10520{
5e22e966 10521 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10522 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10523 int need_copy = 1;
10524
10525 /* In this case dwarf2_compute_name is just a shortcut not building anything
10526 on its own. */
10527 if (!die_needs_namespace (die, cu))
10528 return dwarf2_compute_name (name, die, cu, 1);
10529
906bb4c5
TT
10530 if (cu->language != language_rust)
10531 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10532
10533 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10534 has computed. */
791afaa2 10535 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10536 if (mangled != NULL)
900e11f9 10537 {
900e11f9 10538
59cc4834
JB
10539 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10540 {
10541 /* Do nothing (do not demangle the symbol name). */
10542 }
10543 else if (cu->language == language_go)
a766d390 10544 {
5e2db402
TT
10545 /* This is a lie, but we already lie to the caller new_symbol.
10546 new_symbol assumes we return the mangled name.
a766d390 10547 This just undoes that lie until things are cleaned up. */
a766d390
DE
10548 }
10549 else
10550 {
0eb876f5
JB
10551 /* Use DMGL_RET_DROP for C++ template functions to suppress
10552 their return type. It is easier for GDB users to search
10553 for such functions as `name(params)' than `long name(params)'.
10554 In such case the minimal symbol names do not match the full
10555 symbol names but for template functions there is never a need
10556 to look up their definition from their declaration so
10557 the only disadvantage remains the minimal symbol variant
10558 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10559 demangled.reset (gdb_demangle (mangled,
10560 (DMGL_PARAMS | DMGL_ANSI
10561 | DMGL_RET_DROP)));
a766d390 10562 }
900e11f9 10563 if (demangled)
791afaa2 10564 canon = demangled.get ();
900e11f9
JK
10565 else
10566 {
10567 canon = mangled;
10568 need_copy = 0;
10569 }
10570 }
10571
10572 if (canon == NULL || check_physname)
10573 {
10574 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10575
10576 if (canon != NULL && strcmp (physname, canon) != 0)
10577 {
10578 /* It may not mean a bug in GDB. The compiler could also
10579 compute DW_AT_linkage_name incorrectly. But in such case
10580 GDB would need to be bug-to-bug compatible. */
10581
b98664d3 10582 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10583 "(from linkage <%s>) - DIE at %s [in module %s]"),
10584 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10585 objfile_name (objfile));
900e11f9
JK
10586
10587 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10588 is available here - over computed PHYSNAME. It is safer
10589 against both buggy GDB and buggy compilers. */
10590
10591 retval = canon;
10592 }
10593 else
10594 {
10595 retval = physname;
10596 need_copy = 0;
10597 }
10598 }
10599 else
10600 retval = canon;
10601
10602 if (need_copy)
be1e3d3e 10603 retval = objfile->intern (retval);
900e11f9 10604
900e11f9 10605 return retval;
0114d602
DJ
10606}
10607
74921315
KS
10608/* Inspect DIE in CU for a namespace alias. If one exists, record
10609 a new symbol for it.
10610
10611 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10612
10613static int
10614read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10615{
10616 struct attribute *attr;
10617
10618 /* If the die does not have a name, this is not a namespace
10619 alias. */
10620 attr = dwarf2_attr (die, DW_AT_name, cu);
10621 if (attr != NULL)
10622 {
10623 int num;
10624 struct die_info *d = die;
10625 struct dwarf2_cu *imported_cu = cu;
10626
10627 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10628 keep inspecting DIEs until we hit the underlying import. */
10629#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10630 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10631 {
10632 attr = dwarf2_attr (d, DW_AT_import, cu);
10633 if (attr == NULL)
10634 break;
10635
10636 d = follow_die_ref (d, attr, &imported_cu);
10637 if (d->tag != DW_TAG_imported_declaration)
10638 break;
10639 }
10640
10641 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10642 {
b98664d3 10643 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10644 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10645 return 0;
10646 }
10647
10648 if (attr != NULL)
10649 {
10650 struct type *type;
0826b30a 10651 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10652
aa66c379 10653 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10654 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10655 {
10656 /* This declaration is a global namespace alias. Add
10657 a symbol for it whose type is the aliased namespace. */
10658 new_symbol (die, type, cu);
10659 return 1;
10660 }
10661 }
10662 }
10663
10664 return 0;
10665}
10666
22cee43f 10667/* Return the using directives repository (global or local?) to use in the
804d2729 10668 current context for CU.
22cee43f
PMR
10669
10670 For Ada, imported declarations can materialize renamings, which *may* be
10671 global. However it is impossible (for now?) in DWARF to distinguish
10672 "external" imported declarations and "static" ones. As all imported
10673 declarations seem to be static in all other languages, make them all CU-wide
10674 global only in Ada. */
10675
10676static struct using_direct **
804d2729 10677using_directives (struct dwarf2_cu *cu)
22cee43f 10678{
c24bdb02
KS
10679 if (cu->language == language_ada
10680 && cu->get_builder ()->outermost_context_p ())
10681 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10682 else
c24bdb02 10683 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10684}
10685
27aa8d6a
SW
10686/* Read the import statement specified by the given die and record it. */
10687
10688static void
10689read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10690{
5e22e966 10691 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10692 struct attribute *import_attr;
32019081 10693 struct die_info *imported_die, *child_die;
de4affc9 10694 struct dwarf2_cu *imported_cu;
27aa8d6a 10695 const char *imported_name;
794684b6 10696 const char *imported_name_prefix;
13387711
SW
10697 const char *canonical_name;
10698 const char *import_alias;
10699 const char *imported_declaration = NULL;
794684b6 10700 const char *import_prefix;
eb1e02fd 10701 std::vector<const char *> excludes;
13387711 10702
27aa8d6a
SW
10703 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10704 if (import_attr == NULL)
10705 {
b98664d3 10706 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10707 dwarf_tag_name (die->tag));
10708 return;
10709 }
10710
de4affc9
CC
10711 imported_cu = cu;
10712 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10713 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10714 if (imported_name == NULL)
10715 {
10716 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10717
10718 The import in the following code:
10719 namespace A
10720 {
10721 typedef int B;
10722 }
10723
10724 int main ()
10725 {
10726 using A::B;
10727 B b;
10728 return b;
10729 }
10730
10731 ...
10732 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10733 <52> DW_AT_decl_file : 1
10734 <53> DW_AT_decl_line : 6
10735 <54> DW_AT_import : <0x75>
10736 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10737 <59> DW_AT_name : B
10738 <5b> DW_AT_decl_file : 1
10739 <5c> DW_AT_decl_line : 2
10740 <5d> DW_AT_type : <0x6e>
10741 ...
10742 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10743 <76> DW_AT_byte_size : 4
10744 <77> DW_AT_encoding : 5 (signed)
10745
10746 imports the wrong die ( 0x75 instead of 0x58 ).
10747 This case will be ignored until the gcc bug is fixed. */
10748 return;
10749 }
10750
82856980
SW
10751 /* Figure out the local name after import. */
10752 import_alias = dwarf2_name (die, cu);
27aa8d6a 10753
794684b6
SW
10754 /* Figure out where the statement is being imported to. */
10755 import_prefix = determine_prefix (die, cu);
10756
10757 /* Figure out what the scope of the imported die is and prepend it
10758 to the name of the imported die. */
de4affc9 10759 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10760
f55ee35c
JK
10761 if (imported_die->tag != DW_TAG_namespace
10762 && imported_die->tag != DW_TAG_module)
794684b6 10763 {
13387711
SW
10764 imported_declaration = imported_name;
10765 canonical_name = imported_name_prefix;
794684b6 10766 }
13387711 10767 else if (strlen (imported_name_prefix) > 0)
12aaed36 10768 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10769 imported_name_prefix,
10770 (cu->language == language_d ? "." : "::"),
10771 imported_name, (char *) NULL);
13387711
SW
10772 else
10773 canonical_name = imported_name;
794684b6 10774
32019081
JK
10775 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10776 for (child_die = die->child; child_die && child_die->tag;
436c571c 10777 child_die = child_die->sibling)
32019081
JK
10778 {
10779 /* DWARF-4: A Fortran use statement with a “rename list” may be
10780 represented by an imported module entry with an import attribute
10781 referring to the module and owned entries corresponding to those
10782 entities that are renamed as part of being imported. */
10783
10784 if (child_die->tag != DW_TAG_imported_declaration)
10785 {
b98664d3 10786 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10787 "- DIE at %s [in module %s]"),
10788 sect_offset_str (child_die->sect_off),
10789 objfile_name (objfile));
32019081
JK
10790 continue;
10791 }
10792
10793 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10794 if (import_attr == NULL)
10795 {
b98664d3 10796 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10797 dwarf_tag_name (child_die->tag));
10798 continue;
10799 }
10800
10801 imported_cu = cu;
10802 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10803 &imported_cu);
10804 imported_name = dwarf2_name (imported_die, imported_cu);
10805 if (imported_name == NULL)
10806 {
b98664d3 10807 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10808 "imported name - DIE at %s [in module %s]"),
10809 sect_offset_str (child_die->sect_off),
10810 objfile_name (objfile));
32019081
JK
10811 continue;
10812 }
10813
eb1e02fd 10814 excludes.push_back (imported_name);
32019081
JK
10815
10816 process_die (child_die, cu);
10817 }
10818
804d2729 10819 add_using_directive (using_directives (cu),
22cee43f
PMR
10820 import_prefix,
10821 canonical_name,
10822 import_alias,
10823 imported_declaration,
10824 excludes,
10825 0,
10826 &objfile->objfile_obstack);
27aa8d6a
SW
10827}
10828
5230b05a
WT
10829/* ICC<14 does not output the required DW_AT_declaration on incomplete
10830 types, but gives them a size of zero. Starting with version 14,
10831 ICC is compatible with GCC. */
10832
9068261f 10833static bool
5230b05a
WT
10834producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10835{
10836 if (!cu->checked_producer)
10837 check_producer (cu);
10838
10839 return cu->producer_is_icc_lt_14;
10840}
10841
eb77c9df
AB
10842/* ICC generates a DW_AT_type for C void functions. This was observed on
10843 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10844 which says that void functions should not have a DW_AT_type. */
10845
10846static bool
10847producer_is_icc (struct dwarf2_cu *cu)
10848{
10849 if (!cu->checked_producer)
10850 check_producer (cu);
10851
10852 return cu->producer_is_icc;
10853}
10854
1b80a9fa
JK
10855/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10856 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10857 this, it was first present in GCC release 4.3.0. */
10858
9068261f 10859static bool
1b80a9fa
JK
10860producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10861{
10862 if (!cu->checked_producer)
10863 check_producer (cu);
10864
10865 return cu->producer_is_gcc_lt_4_3;
10866}
10867
d721ba37
PA
10868static file_and_directory
10869find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10870{
d721ba37
PA
10871 file_and_directory res;
10872
9291a0cd
TT
10873 /* Find the filename. Do not use dwarf2_name here, since the filename
10874 is not a source language identifier. */
d721ba37
PA
10875 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10876 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10877
d721ba37
PA
10878 if (res.comp_dir == NULL
10879 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10880 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10881 {
d721ba37
PA
10882 res.comp_dir_storage = ldirname (res.name);
10883 if (!res.comp_dir_storage.empty ())
10884 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10885 }
d721ba37 10886 if (res.comp_dir != NULL)
9291a0cd
TT
10887 {
10888 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10889 directory, get rid of it. */
d721ba37 10890 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10891
d721ba37
PA
10892 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10893 res.comp_dir = cp + 1;
9291a0cd
TT
10894 }
10895
d721ba37
PA
10896 if (res.name == NULL)
10897 res.name = "<unknown>";
10898
10899 return res;
9291a0cd
TT
10900}
10901
f4dc4d17
DE
10902/* Handle DW_AT_stmt_list for a compilation unit.
10903 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10904 COMP_DIR is the compilation directory. LOWPC is passed to
10905 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10906
10907static void
10908handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10909 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10910{
976ca316 10911 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10912 struct attribute *attr;
527f3840
JK
10913 struct line_header line_header_local;
10914 hashval_t line_header_local_hash;
527f3840
JK
10915 void **slot;
10916 int decode_mapping;
2ab95328 10917
f4dc4d17
DE
10918 gdb_assert (! cu->per_cu->is_debug_types);
10919
2ab95328 10920 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10921 if (attr == NULL)
10922 return;
10923
9c541725 10924 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10925
10926 /* The line header hash table is only created if needed (it exists to
10927 prevent redundant reading of the line table for partial_units).
10928 If we're given a partial_unit, we'll need it. If we're given a
10929 compile_unit, then use the line header hash table if it's already
10930 created, but don't create one just yet. */
10931
976ca316 10932 if (per_objfile->line_header_hash == NULL
527f3840 10933 && die->tag == DW_TAG_partial_unit)
2ab95328 10934 {
976ca316 10935 per_objfile->line_header_hash
d15acc42
TT
10936 .reset (htab_create_alloc (127, line_header_hash_voidp,
10937 line_header_eq_voidp,
10938 free_line_header_voidp,
10939 xcalloc, xfree));
527f3840 10940 }
2ab95328 10941
9c541725 10942 line_header_local.sect_off = line_offset;
527f3840
JK
10943 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10944 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10945 if (per_objfile->line_header_hash != NULL)
527f3840 10946 {
976ca316 10947 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10948 &line_header_local,
10949 line_header_local_hash, NO_INSERT);
10950
10951 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10952 is not present in *SLOT (since if there is something in *SLOT then
10953 it will be for a partial_unit). */
10954 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10955 {
527f3840 10956 gdb_assert (*slot != NULL);
9a3c8263 10957 cu->line_header = (struct line_header *) *slot;
527f3840 10958 return;
dee91e82 10959 }
2ab95328 10960 }
527f3840
JK
10961
10962 /* dwarf_decode_line_header does not yet provide sufficient information.
10963 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10964 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10965 if (lh == NULL)
527f3840 10966 return;
4c8aa72d
PA
10967
10968 cu->line_header = lh.release ();
10969 cu->line_header_die_owner = die;
527f3840 10970
976ca316 10971 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10972 slot = NULL;
10973 else
10974 {
976ca316 10975 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10976 &line_header_local,
10977 line_header_local_hash, INSERT);
10978 gdb_assert (slot != NULL);
10979 }
10980 if (slot != NULL && *slot == NULL)
10981 {
10982 /* This newly decoded line number information unit will be owned
10983 by line_header_hash hash table. */
10984 *slot = cu->line_header;
4c8aa72d 10985 cu->line_header_die_owner = NULL;
527f3840
JK
10986 }
10987 else
10988 {
10989 /* We cannot free any current entry in (*slot) as that struct line_header
10990 may be already used by multiple CUs. Create only temporary decoded
10991 line_header for this CU - it may happen at most once for each line
10992 number information unit. And if we're not using line_header_hash
10993 then this is what we want as well. */
10994 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10995 }
10996 decode_mapping = (die->tag != DW_TAG_partial_unit);
10997 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10998 decode_mapping);
fff8551c 10999
2ab95328
TT
11000}
11001
95554aad 11002/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11003
c906108c 11004static void
e7c27a73 11005read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11006{
976ca316
SM
11007 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11008 struct objfile *objfile = per_objfile->objfile;
08feed99 11009 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11010 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11011 CORE_ADDR highpc = ((CORE_ADDR) 0);
11012 struct attribute *attr;
c906108c 11013 struct die_info *child_die;
e142c38c 11014 CORE_ADDR baseaddr;
6e70227d 11015
380618d6 11016 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11017 baseaddr = objfile->text_section_offset ();
c906108c 11018
fae299cd 11019 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11020
11021 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11022 from finish_block. */
2acceee2 11023 if (lowpc == ((CORE_ADDR) -1))
c906108c 11024 lowpc = highpc;
3e29f34a 11025 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11026
d721ba37 11027 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11028
f4b8a18d
KW
11029 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11030 standardised yet. As a workaround for the language detection we fall
11031 back to the DW_AT_producer string. */
11032 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11033 cu->language = language_opencl;
11034
3019eac3
DE
11035 /* Similar hack for Go. */
11036 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11037 set_cu_language (DW_LANG_Go, cu);
11038
c24bdb02 11039 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11040
11041 /* Decode line number information if present. We do this before
11042 processing child DIEs, so that the line header table is available
11043 for DW_AT_decl_file. */
d721ba37 11044 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11045
11046 /* Process all dies in compilation unit. */
11047 if (die->child != NULL)
11048 {
11049 child_die = die->child;
11050 while (child_die && child_die->tag)
11051 {
11052 process_die (child_die, cu);
436c571c 11053 child_die = child_die->sibling;
3019eac3
DE
11054 }
11055 }
11056
11057 /* Decode macro information, if present. Dwarf 2 macro information
11058 refers to information in the line number info statement program
11059 header, so we can only read it if we've read the header
11060 successfully. */
0af92d60
JK
11061 attr = dwarf2_attr (die, DW_AT_macros, cu);
11062 if (attr == NULL)
11063 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11064 if (attr && cu->line_header)
11065 {
11066 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11067 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11068
43f3e411 11069 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11070 }
11071 else
11072 {
11073 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11074 if (attr && cu->line_header)
11075 {
11076 unsigned int macro_offset = DW_UNSND (attr);
11077
43f3e411 11078 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11079 }
11080 }
3019eac3
DE
11081}
11082
c24bdb02
KS
11083void
11084dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11085{
f4dc4d17
DE
11086 struct type_unit_group *tu_group;
11087 int first_time;
3019eac3 11088 struct attribute *attr;
9c541725 11089 unsigned int i;
0186c6a7 11090 struct signatured_type *sig_type;
3019eac3 11091
f4dc4d17 11092 gdb_assert (per_cu->is_debug_types);
0186c6a7 11093 sig_type = (struct signatured_type *) per_cu;
3019eac3 11094
c24bdb02 11095 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11096
f4dc4d17 11097 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11098 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11099 if (sig_type->type_unit_group == NULL)
c24bdb02 11100 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11101 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11102
11103 /* If we've already processed this stmt_list there's no real need to
11104 do it again, we could fake it and just recreate the part we need
11105 (file name,index -> symtab mapping). If data shows this optimization
11106 is useful we can do it then. */
8adb8487
TT
11107 type_unit_group_unshareable *tug_unshare
11108 = per_objfile->get_type_unit_group_unshareable (tu_group);
11109 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11110
11111 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11112 debug info. */
fff8551c 11113 line_header_up lh;
f4dc4d17 11114 if (attr != NULL)
3019eac3 11115 {
9c541725 11116 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11117 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11118 }
11119 if (lh == NULL)
11120 {
11121 if (first_time)
c24bdb02 11122 start_symtab ("", NULL, 0);
f4dc4d17
DE
11123 else
11124 {
8adb8487 11125 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11126 gdb_assert (m_builder == nullptr);
8adb8487 11127 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11128 m_builder.reset (new struct buildsym_compunit
11129 (COMPUNIT_OBJFILE (cust), "",
11130 COMPUNIT_DIRNAME (cust),
11131 compunit_language (cust),
11132 0, cust));
770479f2 11133 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11134 }
f4dc4d17 11135 return;
3019eac3
DE
11136 }
11137
c24bdb02
KS
11138 line_header = lh.release ();
11139 line_header_die_owner = die;
3019eac3 11140
f4dc4d17
DE
11141 if (first_time)
11142 {
c24bdb02 11143 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11144
1fd60fc0
DE
11145 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11146 still initializing it, and our caller (a few levels up)
11147 process_full_type_unit still needs to know if this is the first
11148 time. */
11149
8adb8487 11150 tug_unshare->symtabs
4ac93832
TT
11151 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11152 struct symtab *, line_header->file_names_size ());
3019eac3 11153
7ba99d21
AT
11154 auto &file_names = line_header->file_names ();
11155 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11156 {
7ba99d21 11157 file_entry &fe = file_names[i];
c24bdb02
KS
11158 dwarf2_start_subfile (this, fe.name,
11159 fe.include_dir (line_header));
11160 buildsym_compunit *b = get_builder ();
11161 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11162 {
4c8aa72d
PA
11163 /* NOTE: start_subfile will recognize when it's been
11164 passed a file it has already seen. So we can't
11165 assume there's a simple mapping from
11166 cu->line_header->file_names to subfiles, plus
11167 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11168 b->get_current_subfile ()->symtab
11169 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11170 }
11171
c24bdb02 11172 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11173 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11174 }
11175 }
11176 else
3019eac3 11177 {
c24bdb02 11178 gdb_assert (m_builder == nullptr);
8adb8487 11179 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11180 m_builder.reset (new struct buildsym_compunit
11181 (COMPUNIT_OBJFILE (cust), "",
11182 COMPUNIT_DIRNAME (cust),
11183 compunit_language (cust),
11184 0, cust));
770479f2 11185 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11186
7ba99d21
AT
11187 auto &file_names = line_header->file_names ();
11188 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11189 {
7ba99d21 11190 file_entry &fe = file_names[i];
8adb8487 11191 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11192 }
3019eac3
DE
11193 }
11194
f4dc4d17
DE
11195 /* The main symtab is allocated last. Type units don't have DW_AT_name
11196 so they don't have a "real" (so to speak) symtab anyway.
11197 There is later code that will assign the main symtab to all symbols
11198 that don't have one. We need to handle the case of a symbol with a
11199 missing symtab (DW_AT_decl_file) anyway. */
11200}
3019eac3 11201
f4dc4d17
DE
11202/* Process DW_TAG_type_unit.
11203 For TUs we want to skip the first top level sibling if it's not the
11204 actual type being defined by this TU. In this case the first top
11205 level sibling is there to provide context only. */
3019eac3 11206
f4dc4d17
DE
11207static void
11208read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11209{
11210 struct die_info *child_die;
3019eac3 11211
f4dc4d17
DE
11212 prepare_one_comp_unit (cu, die, language_minimal);
11213
11214 /* Initialize (or reinitialize) the machinery for building symtabs.
11215 We do this before processing child DIEs, so that the line header table
11216 is available for DW_AT_decl_file. */
c24bdb02 11217 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11218
11219 if (die->child != NULL)
11220 {
11221 child_die = die->child;
11222 while (child_die && child_die->tag)
11223 {
11224 process_die (child_die, cu);
436c571c 11225 child_die = child_die->sibling;
f4dc4d17
DE
11226 }
11227 }
3019eac3
DE
11228}
11229\f
80626a55
DE
11230/* DWO/DWP files.
11231
11232 http://gcc.gnu.org/wiki/DebugFission
11233 http://gcc.gnu.org/wiki/DebugFissionDWP
11234
11235 To simplify handling of both DWO files ("object" files with the DWARF info)
11236 and DWP files (a file with the DWOs packaged up into one file), we treat
11237 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11238
11239static hashval_t
11240hash_dwo_file (const void *item)
11241{
9a3c8263 11242 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11243 hashval_t hash;
3019eac3 11244
a2ce51a0
DE
11245 hash = htab_hash_string (dwo_file->dwo_name);
11246 if (dwo_file->comp_dir != NULL)
11247 hash += htab_hash_string (dwo_file->comp_dir);
11248 return hash;
3019eac3
DE
11249}
11250
11251static int
11252eq_dwo_file (const void *item_lhs, const void *item_rhs)
11253{
9a3c8263
SM
11254 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11255 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11256
a2ce51a0
DE
11257 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11258 return 0;
11259 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11260 return lhs->comp_dir == rhs->comp_dir;
11261 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11262}
11263
11264/* Allocate a hash table for DWO files. */
11265
51ac9db5 11266static htab_up
298e9637 11267allocate_dwo_file_hash_table ()
3019eac3 11268{
51ac9db5
SM
11269 auto delete_dwo_file = [] (void *item)
11270 {
11271 struct dwo_file *dwo_file = (struct dwo_file *) item;
11272
11273 delete dwo_file;
11274 };
11275
bc68fb19
TT
11276 return htab_up (htab_create_alloc (41,
11277 hash_dwo_file,
11278 eq_dwo_file,
11279 delete_dwo_file,
11280 xcalloc, xfree));
3019eac3
DE
11281}
11282
80626a55
DE
11283/* Lookup DWO file DWO_NAME. */
11284
11285static void **
976ca316 11286lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11287 const char *dwo_name,
11288 const char *comp_dir)
80626a55
DE
11289{
11290 struct dwo_file find_entry;
11291 void **slot;
11292
976ca316
SM
11293 if (per_objfile->per_bfd->dwo_files == NULL)
11294 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11295
0ac5b59e
DE
11296 find_entry.dwo_name = dwo_name;
11297 find_entry.comp_dir = comp_dir;
976ca316 11298 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11299 INSERT);
80626a55
DE
11300
11301 return slot;
11302}
11303
3019eac3
DE
11304static hashval_t
11305hash_dwo_unit (const void *item)
11306{
9a3c8263 11307 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11308
11309 /* This drops the top 32 bits of the id, but is ok for a hash. */
11310 return dwo_unit->signature;
11311}
11312
11313static int
11314eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11315{
9a3c8263
SM
11316 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11317 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11318
11319 /* The signature is assumed to be unique within the DWO file.
11320 So while object file CU dwo_id's always have the value zero,
11321 that's OK, assuming each object file DWO file has only one CU,
11322 and that's the rule for now. */
11323 return lhs->signature == rhs->signature;
11324}
11325
11326/* Allocate a hash table for DWO CUs,TUs.
11327 There is one of these tables for each of CUs,TUs for each DWO file. */
11328
b0b6a987 11329static htab_up
298e9637 11330allocate_dwo_unit_table ()
3019eac3
DE
11331{
11332 /* Start out with a pretty small number.
11333 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11334 return htab_up (htab_create_alloc (3,
11335 hash_dwo_unit,
11336 eq_dwo_unit,
11337 NULL, xcalloc, xfree));
3019eac3
DE
11338}
11339
19c3d4c9 11340/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11341
11342static void
19c3d4c9
DE
11343create_dwo_cu_reader (const struct die_reader_specs *reader,
11344 const gdb_byte *info_ptr,
11345 struct die_info *comp_unit_die,
c0ab21c2
TT
11346 struct dwo_file *dwo_file,
11347 struct dwo_unit *dwo_unit)
3019eac3
DE
11348{
11349 struct dwarf2_cu *cu = reader->cu;
9c541725 11350 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11351 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11352
a084a2a6
AT
11353 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11354 if (!signature.has_value ())
3019eac3 11355 {
b98664d3 11356 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11357 " its dwo_id [in module %s]"),
9d8780f0 11358 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11359 return;
11360 }
11361
3019eac3 11362 dwo_unit->dwo_file = dwo_file;
a084a2a6 11363 dwo_unit->signature = *signature;
8a0459fd 11364 dwo_unit->section = section;
9c541725 11365 dwo_unit->sect_off = sect_off;
3019eac3
DE
11366 dwo_unit->length = cu->per_cu->length;
11367
b4f54984 11368 if (dwarf_read_debug)
9d8780f0
SM
11369 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11370 sect_offset_str (sect_off),
9c541725 11371 hex_string (dwo_unit->signature));
3019eac3
DE
11372}
11373
33c5cd75 11374/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11375 Note: This function processes DWO files only, not DWP files. */
3019eac3 11376
33c5cd75 11377static void
976ca316 11378create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11379 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11380 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11381{
976ca316
SM
11382 struct objfile *objfile = per_objfile->objfile;
11383 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11384 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11385
96b79293 11386 section.read (objfile);
33c5cd75 11387 info_ptr = section.buffer;
3019eac3
DE
11388
11389 if (info_ptr == NULL)
33c5cd75 11390 return;
3019eac3 11391
b4f54984 11392 if (dwarf_read_debug)
19c3d4c9
DE
11393 {
11394 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11395 section.get_name (),
11396 section.get_file_name ());
19c3d4c9 11397 }
3019eac3 11398
33c5cd75 11399 end_ptr = info_ptr + section.size;
3019eac3
DE
11400 while (info_ptr < end_ptr)
11401 {
11402 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11403 struct dwo_unit read_unit {};
33c5cd75
DB
11404 struct dwo_unit *dwo_unit;
11405 void **slot;
11406 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11407
11408 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11409 per_cu.per_bfd = per_bfd;
3019eac3 11410 per_cu.is_debug_types = 0;
33c5cd75
DB
11411 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11412 per_cu.section = &section;
11413
976ca316 11414 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11415 if (!reader.dummy_p)
11416 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11417 &dwo_file, &read_unit);
33c5cd75
DB
11418 info_ptr += per_cu.length;
11419
11420 // If the unit could not be parsed, skip it.
c0ab21c2 11421 if (read_unit.dwo_file == NULL)
33c5cd75 11422 continue;
3019eac3 11423
33c5cd75 11424 if (cus_htab == NULL)
298e9637 11425 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11426
1859c670 11427 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11428 struct dwo_unit);
c0ab21c2 11429 *dwo_unit = read_unit;
b0b6a987 11430 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11431 gdb_assert (slot != NULL);
11432 if (*slot != NULL)
19c3d4c9 11433 {
33c5cd75
DB
11434 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11435 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11436
b98664d3 11437 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11438 " the entry at offset %s, signature %s"),
11439 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11440 hex_string (dwo_unit->signature));
19c3d4c9 11441 }
33c5cd75 11442 *slot = (void *)dwo_unit;
3019eac3 11443 }
3019eac3
DE
11444}
11445
80626a55
DE
11446/* DWP file .debug_{cu,tu}_index section format:
11447 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11448
d2415c6c
DE
11449 DWP Version 1:
11450
80626a55
DE
11451 Both index sections have the same format, and serve to map a 64-bit
11452 signature to a set of section numbers. Each section begins with a header,
11453 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11454 indexes, and a pool of 32-bit section numbers. The index sections will be
11455 aligned at 8-byte boundaries in the file.
11456
d2415c6c
DE
11457 The index section header consists of:
11458
11459 V, 32 bit version number
11460 -, 32 bits unused
11461 N, 32 bit number of compilation units or type units in the index
11462 M, 32 bit number of slots in the hash table
80626a55 11463
d2415c6c 11464 Numbers are recorded using the byte order of the application binary.
80626a55 11465
d2415c6c
DE
11466 The hash table begins at offset 16 in the section, and consists of an array
11467 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11468 order of the application binary). Unused slots in the hash table are 0.
11469 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11470
d2415c6c
DE
11471 The parallel table begins immediately after the hash table
11472 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11473 array of 32-bit indexes (using the byte order of the application binary),
11474 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11475 table contains a 32-bit index into the pool of section numbers. For unused
11476 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11477
73869dc2
DE
11478 The pool of section numbers begins immediately following the hash table
11479 (at offset 16 + 12 * M from the beginning of the section). The pool of
11480 section numbers consists of an array of 32-bit words (using the byte order
11481 of the application binary). Each item in the array is indexed starting
11482 from 0. The hash table entry provides the index of the first section
11483 number in the set. Additional section numbers in the set follow, and the
11484 set is terminated by a 0 entry (section number 0 is not used in ELF).
11485
11486 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11487 section must be the first entry in the set, and the .debug_abbrev.dwo must
11488 be the second entry. Other members of the set may follow in any order.
11489
11490 ---
11491
11492 DWP Version 2:
11493
11494 DWP Version 2 combines all the .debug_info, etc. sections into one,
11495 and the entries in the index tables are now offsets into these sections.
11496 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11497 section.
11498
11499 Index Section Contents:
11500 Header
11501 Hash Table of Signatures dwp_hash_table.hash_table
11502 Parallel Table of Indices dwp_hash_table.unit_table
11503 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11504 Table of Section Sizes dwp_hash_table.v2.sizes
11505
11506 The index section header consists of:
11507
11508 V, 32 bit version number
11509 L, 32 bit number of columns in the table of section offsets
11510 N, 32 bit number of compilation units or type units in the index
11511 M, 32 bit number of slots in the hash table
11512
11513 Numbers are recorded using the byte order of the application binary.
11514
11515 The hash table has the same format as version 1.
11516 The parallel table of indices has the same format as version 1,
11517 except that the entries are origin-1 indices into the table of sections
11518 offsets and the table of section sizes.
11519
11520 The table of offsets begins immediately following the parallel table
11521 (at offset 16 + 12 * M from the beginning of the section). The table is
11522 a two-dimensional array of 32-bit words (using the byte order of the
11523 application binary), with L columns and N+1 rows, in row-major order.
11524 Each row in the array is indexed starting from 0. The first row provides
11525 a key to the remaining rows: each column in this row provides an identifier
11526 for a debug section, and the offsets in the same column of subsequent rows
11527 refer to that section. The section identifiers are:
11528
11529 DW_SECT_INFO 1 .debug_info.dwo
11530 DW_SECT_TYPES 2 .debug_types.dwo
11531 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11532 DW_SECT_LINE 4 .debug_line.dwo
11533 DW_SECT_LOC 5 .debug_loc.dwo
11534 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11535 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11536 DW_SECT_MACRO 8 .debug_macro.dwo
11537
11538 The offsets provided by the CU and TU index sections are the base offsets
11539 for the contributions made by each CU or TU to the corresponding section
11540 in the package file. Each CU and TU header contains an abbrev_offset
11541 field, used to find the abbreviations table for that CU or TU within the
11542 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11543 be interpreted as relative to the base offset given in the index section.
11544 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11545 should be interpreted as relative to the base offset for .debug_line.dwo,
11546 and offsets into other debug sections obtained from DWARF attributes should
11547 also be interpreted as relative to the corresponding base offset.
11548
11549 The table of sizes begins immediately following the table of offsets.
11550 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11551 with L columns and N rows, in row-major order. Each row in the array is
11552 indexed starting from 1 (row 0 is shared by the two tables).
11553
11554 ---
11555
11556 Hash table lookup is handled the same in version 1 and 2:
11557
11558 We assume that N and M will not exceed 2^32 - 1.
11559 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11560
d2415c6c
DE
11561 Given a 64-bit compilation unit signature or a type signature S, an entry
11562 in the hash table is located as follows:
80626a55 11563
d2415c6c
DE
11564 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11565 the low-order k bits all set to 1.
80626a55 11566
d2415c6c 11567 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11568
d2415c6c
DE
11569 3) If the hash table entry at index H matches the signature, use that
11570 entry. If the hash table entry at index H is unused (all zeroes),
11571 terminate the search: the signature is not present in the table.
80626a55 11572
d2415c6c 11573 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11574
d2415c6c 11575 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11576 to stop at an unused slot or find the match. */
80626a55
DE
11577
11578/* Create a hash table to map DWO IDs to their CU/TU entry in
11579 .debug_{info,types}.dwo in DWP_FILE.
11580 Returns NULL if there isn't one.
11581 Note: This function processes DWP files only, not DWO files. */
11582
11583static struct dwp_hash_table *
976ca316 11584create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11585 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11586{
976ca316 11587 struct objfile *objfile = per_objfile->objfile;
400174b1 11588 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11589 const gdb_byte *index_ptr, *index_end;
80626a55 11590 struct dwarf2_section_info *index;
73869dc2 11591 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11592 struct dwp_hash_table *htab;
11593
11594 if (is_debug_types)
11595 index = &dwp_file->sections.tu_index;
11596 else
11597 index = &dwp_file->sections.cu_index;
11598
96b79293 11599 if (index->empty ())
80626a55 11600 return NULL;
96b79293 11601 index->read (objfile);
80626a55
DE
11602
11603 index_ptr = index->buffer;
11604 index_end = index_ptr + index->size;
11605
11606 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11607 index_ptr += 4;
11608 if (version == 2)
11609 nr_columns = read_4_bytes (dbfd, index_ptr);
11610 else
11611 nr_columns = 0;
11612 index_ptr += 4;
80626a55
DE
11613 nr_units = read_4_bytes (dbfd, index_ptr);
11614 index_ptr += 4;
11615 nr_slots = read_4_bytes (dbfd, index_ptr);
11616 index_ptr += 4;
11617
73869dc2 11618 if (version != 1 && version != 2)
80626a55 11619 {
21aa081e 11620 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11621 " [in module %s]"),
21aa081e 11622 pulongest (version), dwp_file->name);
80626a55
DE
11623 }
11624 if (nr_slots != (nr_slots & -nr_slots))
11625 {
21aa081e 11626 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11627 " is not power of 2 [in module %s]"),
21aa081e 11628 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11629 }
11630
976ca316 11631 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11632 htab->version = version;
11633 htab->nr_columns = nr_columns;
80626a55
DE
11634 htab->nr_units = nr_units;
11635 htab->nr_slots = nr_slots;
11636 htab->hash_table = index_ptr;
11637 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11638
11639 /* Exit early if the table is empty. */
11640 if (nr_slots == 0 || nr_units == 0
11641 || (version == 2 && nr_columns == 0))
11642 {
11643 /* All must be zero. */
11644 if (nr_slots != 0 || nr_units != 0
11645 || (version == 2 && nr_columns != 0))
11646 {
b98664d3 11647 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11648 " all zero [in modules %s]"),
11649 dwp_file->name);
11650 }
11651 return htab;
11652 }
11653
11654 if (version == 1)
11655 {
11656 htab->section_pool.v1.indices =
11657 htab->unit_table + sizeof (uint32_t) * nr_slots;
11658 /* It's harder to decide whether the section is too small in v1.
11659 V1 is deprecated anyway so we punt. */
11660 }
11661 else
11662 {
11663 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11664 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11665 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11666 /* Reverse map for error checking. */
11667 int ids_seen[DW_SECT_MAX + 1];
11668 int i;
11669
11670 if (nr_columns < 2)
11671 {
11672 error (_("Dwarf Error: bad DWP hash table, too few columns"
11673 " in section table [in module %s]"),
11674 dwp_file->name);
11675 }
11676 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11677 {
11678 error (_("Dwarf Error: bad DWP hash table, too many columns"
11679 " in section table [in module %s]"),
11680 dwp_file->name);
11681 }
04fd5eed
GB
11682 memset (ids, 255, sizeof_ids);
11683 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11684 for (i = 0; i < nr_columns; ++i)
11685 {
11686 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11687
11688 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11689 {
11690 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11691 " in section table [in module %s]"),
11692 id, dwp_file->name);
11693 }
11694 if (ids_seen[id] != -1)
11695 {
11696 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11697 " id %d in section table [in module %s]"),
11698 id, dwp_file->name);
11699 }
11700 ids_seen[id] = i;
11701 ids[i] = id;
11702 }
11703 /* Must have exactly one info or types section. */
11704 if (((ids_seen[DW_SECT_INFO] != -1)
11705 + (ids_seen[DW_SECT_TYPES] != -1))
11706 != 1)
11707 {
11708 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11709 " DWO info/types section [in module %s]"),
11710 dwp_file->name);
11711 }
11712 /* Must have an abbrev section. */
11713 if (ids_seen[DW_SECT_ABBREV] == -1)
11714 {
11715 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11716 " section [in module %s]"),
11717 dwp_file->name);
11718 }
11719 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11720 htab->section_pool.v2.sizes =
11721 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11722 * nr_units * nr_columns);
11723 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11724 * nr_units * nr_columns))
11725 > index_end)
11726 {
11727 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11728 " [in module %s]"),
11729 dwp_file->name);
11730 }
11731 }
80626a55
DE
11732
11733 return htab;
11734}
11735
11736/* Update SECTIONS with the data from SECTP.
11737
11738 This function is like the other "locate" section routines that are
11739 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11740 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11741
11742 The result is non-zero for success, or zero if an error was found. */
11743
11744static int
73869dc2
DE
11745locate_v1_virtual_dwo_sections (asection *sectp,
11746 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11747{
11748 const struct dwop_section_names *names = &dwop_section_names;
11749
11750 if (section_is_p (sectp->name, &names->abbrev_dwo))
11751 {
11752 /* There can be only one. */
049412e3 11753 if (sections->abbrev.s.section != NULL)
80626a55 11754 return 0;
049412e3 11755 sections->abbrev.s.section = sectp;
fd361982 11756 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11757 }
11758 else if (section_is_p (sectp->name, &names->info_dwo)
11759 || section_is_p (sectp->name, &names->types_dwo))
11760 {
11761 /* There can be only one. */
049412e3 11762 if (sections->info_or_types.s.section != NULL)
80626a55 11763 return 0;
049412e3 11764 sections->info_or_types.s.section = sectp;
fd361982 11765 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11766 }
11767 else if (section_is_p (sectp->name, &names->line_dwo))
11768 {
11769 /* There can be only one. */
049412e3 11770 if (sections->line.s.section != NULL)
80626a55 11771 return 0;
049412e3 11772 sections->line.s.section = sectp;
fd361982 11773 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11774 }
11775 else if (section_is_p (sectp->name, &names->loc_dwo))
11776 {
11777 /* There can be only one. */
049412e3 11778 if (sections->loc.s.section != NULL)
80626a55 11779 return 0;
049412e3 11780 sections->loc.s.section = sectp;
fd361982 11781 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11782 }
11783 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11784 {
11785 /* There can be only one. */
049412e3 11786 if (sections->macinfo.s.section != NULL)
80626a55 11787 return 0;
049412e3 11788 sections->macinfo.s.section = sectp;
fd361982 11789 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11790 }
11791 else if (section_is_p (sectp->name, &names->macro_dwo))
11792 {
11793 /* There can be only one. */
049412e3 11794 if (sections->macro.s.section != NULL)
80626a55 11795 return 0;
049412e3 11796 sections->macro.s.section = sectp;
fd361982 11797 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11798 }
11799 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11800 {
11801 /* There can be only one. */
049412e3 11802 if (sections->str_offsets.s.section != NULL)
80626a55 11803 return 0;
049412e3 11804 sections->str_offsets.s.section = sectp;
fd361982 11805 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11806 }
11807 else
11808 {
11809 /* No other kind of section is valid. */
11810 return 0;
11811 }
11812
11813 return 1;
11814}
11815
73869dc2
DE
11816/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11817 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11818 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11819 This is for DWP version 1 files. */
80626a55
DE
11820
11821static struct dwo_unit *
976ca316 11822create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11823 struct dwp_file *dwp_file,
73869dc2
DE
11824 uint32_t unit_index,
11825 const char *comp_dir,
11826 ULONGEST signature, int is_debug_types)
80626a55 11827{
73869dc2
DE
11828 const struct dwp_hash_table *dwp_htab =
11829 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11830 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11831 const char *kind = is_debug_types ? "TU" : "CU";
11832 struct dwo_file *dwo_file;
11833 struct dwo_unit *dwo_unit;
73869dc2 11834 struct virtual_v1_dwo_sections sections;
80626a55 11835 void **dwo_file_slot;
80626a55
DE
11836 int i;
11837
73869dc2
DE
11838 gdb_assert (dwp_file->version == 1);
11839
b4f54984 11840 if (dwarf_read_debug)
80626a55 11841 {
73869dc2 11842 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11843 kind,
73869dc2 11844 pulongest (unit_index), hex_string (signature),
80626a55
DE
11845 dwp_file->name);
11846 }
11847
19ac8c2e 11848 /* Fetch the sections of this DWO unit.
80626a55
DE
11849 Put a limit on the number of sections we look for so that bad data
11850 doesn't cause us to loop forever. */
11851
73869dc2 11852#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11853 (1 /* .debug_info or .debug_types */ \
11854 + 1 /* .debug_abbrev */ \
11855 + 1 /* .debug_line */ \
11856 + 1 /* .debug_loc */ \
11857 + 1 /* .debug_str_offsets */ \
19ac8c2e 11858 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11859 + 1 /* trailing zero */)
11860
11861 memset (&sections, 0, sizeof (sections));
80626a55 11862
73869dc2 11863 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11864 {
11865 asection *sectp;
11866 uint32_t section_nr =
11867 read_4_bytes (dbfd,
73869dc2
DE
11868 dwp_htab->section_pool.v1.indices
11869 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11870
11871 if (section_nr == 0)
11872 break;
11873 if (section_nr >= dwp_file->num_sections)
11874 {
11875 error (_("Dwarf Error: bad DWP hash table, section number too large"
11876 " [in module %s]"),
11877 dwp_file->name);
11878 }
11879
11880 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11881 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11882 {
11883 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11884 " [in module %s]"),
11885 dwp_file->name);
11886 }
11887 }
11888
11889 if (i < 2
96b79293
TT
11890 || sections.info_or_types.empty ()
11891 || sections.abbrev.empty ())
80626a55
DE
11892 {
11893 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11894 " [in module %s]"),
11895 dwp_file->name);
11896 }
73869dc2 11897 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11898 {
11899 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11900 " [in module %s]"),
11901 dwp_file->name);
11902 }
11903
11904 /* It's easier for the rest of the code if we fake a struct dwo_file and
11905 have dwo_unit "live" in that. At least for now.
11906
11907 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11908 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11909 file, we can combine them back into a virtual DWO file to save space
11910 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11911 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11912
791afaa2
TT
11913 std::string virtual_dwo_name =
11914 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11915 sections.abbrev.get_id (),
11916 sections.line.get_id (),
11917 sections.loc.get_id (),
11918 sections.str_offsets.get_id ());
80626a55 11919 /* Can we use an existing virtual DWO file? */
976ca316 11920 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11921 comp_dir);
80626a55
DE
11922 /* Create one if necessary. */
11923 if (*dwo_file_slot == NULL)
11924 {
b4f54984 11925 if (dwarf_read_debug)
80626a55
DE
11926 {
11927 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11928 virtual_dwo_name.c_str ());
80626a55 11929 }
51ac9db5 11930 dwo_file = new struct dwo_file;
976ca316 11931 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11932 dwo_file->comp_dir = comp_dir;
80626a55
DE
11933 dwo_file->sections.abbrev = sections.abbrev;
11934 dwo_file->sections.line = sections.line;
11935 dwo_file->sections.loc = sections.loc;
11936 dwo_file->sections.macinfo = sections.macinfo;
11937 dwo_file->sections.macro = sections.macro;
11938 dwo_file->sections.str_offsets = sections.str_offsets;
11939 /* The "str" section is global to the entire DWP file. */
11940 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11941 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11942 there's no need to record it in dwo_file.
11943 Also, we can't simply record type sections in dwo_file because
11944 we record a pointer into the vector in dwo_unit. As we collect more
11945 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11946 for it, invalidating all copies of pointers into the previous
11947 contents. */
80626a55
DE
11948 *dwo_file_slot = dwo_file;
11949 }
11950 else
11951 {
b4f54984 11952 if (dwarf_read_debug)
80626a55
DE
11953 {
11954 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11955 virtual_dwo_name.c_str ());
80626a55 11956 }
9a3c8263 11957 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11958 }
80626a55 11959
976ca316 11960 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11961 dwo_unit->dwo_file = dwo_file;
11962 dwo_unit->signature = signature;
8d749320 11963 dwo_unit->section =
976ca316 11964 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11965 *dwo_unit->section = sections.info_or_types;
57d63ce2 11966 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11967
11968 return dwo_unit;
11969}
11970
73869dc2
DE
11971/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11972 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11973 piece within that section used by a TU/CU, return a virtual section
11974 of just that piece. */
11975
11976static struct dwarf2_section_info
976ca316 11977create_dwp_v2_section (dwarf2_per_objfile *per_objfile,
ed2dc618 11978 struct dwarf2_section_info *section,
73869dc2
DE
11979 bfd_size_type offset, bfd_size_type size)
11980{
11981 struct dwarf2_section_info result;
11982 asection *sectp;
11983
11984 gdb_assert (section != NULL);
11985 gdb_assert (!section->is_virtual);
11986
11987 memset (&result, 0, sizeof (result));
11988 result.s.containing_section = section;
dc4ccb6f 11989 result.is_virtual = true;
73869dc2
DE
11990
11991 if (size == 0)
11992 return result;
11993
96b79293 11994 sectp = section->get_bfd_section ();
73869dc2
DE
11995
11996 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11997 bounds of the real section. This is a pretty-rare event, so just
11998 flag an error (easier) instead of a warning and trying to cope. */
11999 if (sectp == NULL
fd361982 12000 || offset + size > bfd_section_size (sectp))
73869dc2 12001 {
73869dc2
DE
12002 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12003 " in section %s [in module %s]"),
fd361982 12004 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12005 objfile_name (per_objfile->objfile));
73869dc2
DE
12006 }
12007
12008 result.virtual_offset = offset;
12009 result.size = size;
12010 return result;
12011}
12012
12013/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12014 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12015 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12016 This is for DWP version 2 files. */
12017
12018static struct dwo_unit *
976ca316 12019create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12020 struct dwp_file *dwp_file,
73869dc2
DE
12021 uint32_t unit_index,
12022 const char *comp_dir,
12023 ULONGEST signature, int is_debug_types)
12024{
73869dc2
DE
12025 const struct dwp_hash_table *dwp_htab =
12026 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12027 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12028 const char *kind = is_debug_types ? "TU" : "CU";
12029 struct dwo_file *dwo_file;
12030 struct dwo_unit *dwo_unit;
12031 struct virtual_v2_dwo_sections sections;
12032 void **dwo_file_slot;
73869dc2
DE
12033 int i;
12034
12035 gdb_assert (dwp_file->version == 2);
12036
b4f54984 12037 if (dwarf_read_debug)
73869dc2
DE
12038 {
12039 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12040 kind,
12041 pulongest (unit_index), hex_string (signature),
12042 dwp_file->name);
12043 }
12044
12045 /* Fetch the section offsets of this DWO unit. */
12046
12047 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12048
12049 for (i = 0; i < dwp_htab->nr_columns; ++i)
12050 {
12051 uint32_t offset = read_4_bytes (dbfd,
12052 dwp_htab->section_pool.v2.offsets
12053 + (((unit_index - 1) * dwp_htab->nr_columns
12054 + i)
12055 * sizeof (uint32_t)));
12056 uint32_t size = read_4_bytes (dbfd,
12057 dwp_htab->section_pool.v2.sizes
12058 + (((unit_index - 1) * dwp_htab->nr_columns
12059 + i)
12060 * sizeof (uint32_t)));
12061
12062 switch (dwp_htab->section_pool.v2.section_ids[i])
12063 {
12064 case DW_SECT_INFO:
12065 case DW_SECT_TYPES:
12066 sections.info_or_types_offset = offset;
12067 sections.info_or_types_size = size;
12068 break;
12069 case DW_SECT_ABBREV:
12070 sections.abbrev_offset = offset;
12071 sections.abbrev_size = size;
12072 break;
12073 case DW_SECT_LINE:
12074 sections.line_offset = offset;
12075 sections.line_size = size;
12076 break;
12077 case DW_SECT_LOC:
12078 sections.loc_offset = offset;
12079 sections.loc_size = size;
12080 break;
12081 case DW_SECT_STR_OFFSETS:
12082 sections.str_offsets_offset = offset;
12083 sections.str_offsets_size = size;
12084 break;
12085 case DW_SECT_MACINFO:
12086 sections.macinfo_offset = offset;
12087 sections.macinfo_size = size;
12088 break;
12089 case DW_SECT_MACRO:
12090 sections.macro_offset = offset;
12091 sections.macro_size = size;
12092 break;
12093 }
12094 }
12095
12096 /* It's easier for the rest of the code if we fake a struct dwo_file and
12097 have dwo_unit "live" in that. At least for now.
12098
12099 The DWP file can be made up of a random collection of CUs and TUs.
12100 However, for each CU + set of TUs that came from the same original DWO
12101 file, we can combine them back into a virtual DWO file to save space
12102 (fewer struct dwo_file objects to allocate). Remember that for really
12103 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12104
791afaa2
TT
12105 std::string virtual_dwo_name =
12106 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12107 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12108 (long) (sections.line_size ? sections.line_offset : 0),
12109 (long) (sections.loc_size ? sections.loc_offset : 0),
12110 (long) (sections.str_offsets_size
12111 ? sections.str_offsets_offset : 0));
73869dc2 12112 /* Can we use an existing virtual DWO file? */
976ca316 12113 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12114 comp_dir);
73869dc2
DE
12115 /* Create one if necessary. */
12116 if (*dwo_file_slot == NULL)
12117 {
b4f54984 12118 if (dwarf_read_debug)
73869dc2
DE
12119 {
12120 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12121 virtual_dwo_name.c_str ());
73869dc2 12122 }
51ac9db5 12123 dwo_file = new struct dwo_file;
976ca316 12124 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12125 dwo_file->comp_dir = comp_dir;
12126 dwo_file->sections.abbrev =
976ca316 12127 create_dwp_v2_section (per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12128 sections.abbrev_offset, sections.abbrev_size);
12129 dwo_file->sections.line =
976ca316 12130 create_dwp_v2_section (per_objfile, &dwp_file->sections.line,
73869dc2
DE
12131 sections.line_offset, sections.line_size);
12132 dwo_file->sections.loc =
976ca316 12133 create_dwp_v2_section (per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12134 sections.loc_offset, sections.loc_size);
12135 dwo_file->sections.macinfo =
976ca316 12136 create_dwp_v2_section (per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12137 sections.macinfo_offset, sections.macinfo_size);
12138 dwo_file->sections.macro =
976ca316 12139 create_dwp_v2_section (per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12140 sections.macro_offset, sections.macro_size);
12141 dwo_file->sections.str_offsets =
976ca316 12142 create_dwp_v2_section (per_objfile,
ed2dc618 12143 &dwp_file->sections.str_offsets,
73869dc2
DE
12144 sections.str_offsets_offset,
12145 sections.str_offsets_size);
12146 /* The "str" section is global to the entire DWP file. */
12147 dwo_file->sections.str = dwp_file->sections.str;
12148 /* The info or types section is assigned below to dwo_unit,
12149 there's no need to record it in dwo_file.
12150 Also, we can't simply record type sections in dwo_file because
12151 we record a pointer into the vector in dwo_unit. As we collect more
12152 types we'll grow the vector and eventually have to reallocate space
12153 for it, invalidating all copies of pointers into the previous
12154 contents. */
12155 *dwo_file_slot = dwo_file;
12156 }
12157 else
12158 {
b4f54984 12159 if (dwarf_read_debug)
73869dc2
DE
12160 {
12161 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12162 virtual_dwo_name.c_str ());
73869dc2 12163 }
9a3c8263 12164 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12165 }
73869dc2 12166
976ca316 12167 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12168 dwo_unit->dwo_file = dwo_file;
12169 dwo_unit->signature = signature;
8d749320 12170 dwo_unit->section =
976ca316
SM
12171 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12172 *dwo_unit->section = create_dwp_v2_section (per_objfile,
ed2dc618 12173 is_debug_types
73869dc2
DE
12174 ? &dwp_file->sections.types
12175 : &dwp_file->sections.info,
12176 sections.info_or_types_offset,
12177 sections.info_or_types_size);
12178 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12179
12180 return dwo_unit;
12181}
12182
57d63ce2
DE
12183/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12184 Returns NULL if the signature isn't found. */
80626a55
DE
12185
12186static struct dwo_unit *
976ca316 12187lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12188 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12189 ULONGEST signature, int is_debug_types)
80626a55 12190{
57d63ce2
DE
12191 const struct dwp_hash_table *dwp_htab =
12192 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12193 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12194 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12195 uint32_t hash = signature & mask;
12196 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12197 unsigned int i;
12198 void **slot;
870f88f7 12199 struct dwo_unit find_dwo_cu;
80626a55
DE
12200
12201 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12202 find_dwo_cu.signature = signature;
19ac8c2e 12203 slot = htab_find_slot (is_debug_types
48b490f2
TT
12204 ? dwp_file->loaded_tus.get ()
12205 : dwp_file->loaded_cus.get (),
19ac8c2e 12206 &find_dwo_cu, INSERT);
80626a55
DE
12207
12208 if (*slot != NULL)
9a3c8263 12209 return (struct dwo_unit *) *slot;
80626a55
DE
12210
12211 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12212 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12213 {
12214 ULONGEST signature_in_table;
12215
12216 signature_in_table =
57d63ce2 12217 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12218 if (signature_in_table == signature)
12219 {
57d63ce2
DE
12220 uint32_t unit_index =
12221 read_4_bytes (dbfd,
12222 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12223
73869dc2
DE
12224 if (dwp_file->version == 1)
12225 {
976ca316
SM
12226 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12227 unit_index, comp_dir,
12228 signature, is_debug_types);
73869dc2
DE
12229 }
12230 else
12231 {
976ca316
SM
12232 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12233 unit_index, comp_dir,
12234 signature, is_debug_types);
73869dc2 12235 }
9a3c8263 12236 return (struct dwo_unit *) *slot;
80626a55
DE
12237 }
12238 if (signature_in_table == 0)
12239 return NULL;
12240 hash = (hash + hash2) & mask;
12241 }
12242
12243 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12244 " [in module %s]"),
12245 dwp_file->name);
12246}
12247
ab5088bf 12248/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12249 Open the file specified by FILE_NAME and hand it off to BFD for
12250 preliminary analysis. Return a newly initialized bfd *, which
12251 includes a canonicalized copy of FILE_NAME.
80626a55 12252 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12253 SEARCH_CWD is true if the current directory is to be searched.
12254 It will be searched before debug-file-directory.
13aaf454
DE
12255 If successful, the file is added to the bfd include table of the
12256 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12257 If unable to find/open the file, return NULL.
3019eac3
DE
12258 NOTE: This function is derived from symfile_bfd_open. */
12259
192b62ce 12260static gdb_bfd_ref_ptr
976ca316 12261try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12262 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12263{
24b9144d 12264 int desc;
9c02c129
DE
12265 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12266 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12267 to debug_file_directory. */
e0cc99a6 12268 const char *search_path;
9c02c129
DE
12269 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12270
e0cc99a6 12271 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12272 if (search_cwd)
12273 {
12274 if (*debug_file_directory != '\0')
e0cc99a6
TT
12275 {
12276 search_path_holder.reset (concat (".", dirname_separator_string,
12277 debug_file_directory,
12278 (char *) NULL));
12279 search_path = search_path_holder.get ();
12280 }
6ac97d4c 12281 else
e0cc99a6 12282 search_path = ".";
6ac97d4c 12283 }
9c02c129 12284 else
e0cc99a6 12285 search_path = debug_file_directory;
3019eac3 12286
24b9144d 12287 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12288 if (is_dwp)
12289 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12290
12291 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12292 desc = openp (search_path, flags, file_name,
3019eac3
DE
12293 O_RDONLY | O_BINARY, &absolute_name);
12294 if (desc < 0)
12295 return NULL;
12296
e0cc99a6
TT
12297 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12298 gnutarget, desc));
9c02c129
DE
12299 if (sym_bfd == NULL)
12300 return NULL;
192b62ce 12301 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12302
192b62ce
TT
12303 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12304 return NULL;
3019eac3 12305
13aaf454
DE
12306 /* Success. Record the bfd as having been included by the objfile's bfd.
12307 This is important because things like demangled_names_hash lives in the
12308 objfile's per_bfd space and may have references to things like symbol
12309 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12310 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12311
3019eac3
DE
12312 return sym_bfd;
12313}
12314
ab5088bf 12315/* Try to open DWO file FILE_NAME.
3019eac3
DE
12316 COMP_DIR is the DW_AT_comp_dir attribute.
12317 The result is the bfd handle of the file.
12318 If there is a problem finding or opening the file, return NULL.
12319 Upon success, the canonicalized path of the file is stored in the bfd,
12320 same as symfile_bfd_open. */
12321
192b62ce 12322static gdb_bfd_ref_ptr
976ca316 12323open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12324 const char *file_name, const char *comp_dir)
3019eac3 12325{
80626a55 12326 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12327 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12328 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12329
12330 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12331
12332 if (comp_dir != NULL)
12333 {
43816ebc
TT
12334 gdb::unique_xmalloc_ptr<char> path_to_try
12335 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12336
12337 /* NOTE: If comp_dir is a relative path, this will also try the
12338 search path, which seems useful. */
976ca316 12339 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12340 0 /*is_dwp*/,
192b62ce 12341 1 /*search_cwd*/));
3019eac3
DE
12342 if (abfd != NULL)
12343 return abfd;
12344 }
12345
12346 /* That didn't work, try debug-file-directory, which, despite its name,
12347 is a list of paths. */
12348
12349 if (*debug_file_directory == '\0')
12350 return NULL;
12351
976ca316 12352 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12353 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12354}
12355
80626a55
DE
12356/* This function is mapped across the sections and remembers the offset and
12357 size of each of the DWO debugging sections we are interested in. */
12358
12359static void
12360dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12361{
9a3c8263 12362 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12363 const struct dwop_section_names *names = &dwop_section_names;
12364
12365 if (section_is_p (sectp->name, &names->abbrev_dwo))
12366 {
049412e3 12367 dwo_sections->abbrev.s.section = sectp;
fd361982 12368 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12369 }
12370 else if (section_is_p (sectp->name, &names->info_dwo))
12371 {
049412e3 12372 dwo_sections->info.s.section = sectp;
fd361982 12373 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12374 }
12375 else if (section_is_p (sectp->name, &names->line_dwo))
12376 {
049412e3 12377 dwo_sections->line.s.section = sectp;
fd361982 12378 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12379 }
12380 else if (section_is_p (sectp->name, &names->loc_dwo))
12381 {
049412e3 12382 dwo_sections->loc.s.section = sectp;
fd361982 12383 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12384 }
41144253 12385 else if (section_is_p (sectp->name, &names->loclists_dwo))
12386 {
12387 dwo_sections->loclists.s.section = sectp;
12388 dwo_sections->loclists.size = bfd_section_size (sectp);
12389 }
80626a55
DE
12390 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12391 {
049412e3 12392 dwo_sections->macinfo.s.section = sectp;
fd361982 12393 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12394 }
12395 else if (section_is_p (sectp->name, &names->macro_dwo))
12396 {
049412e3 12397 dwo_sections->macro.s.section = sectp;
fd361982 12398 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12399 }
12400 else if (section_is_p (sectp->name, &names->str_dwo))
12401 {
049412e3 12402 dwo_sections->str.s.section = sectp;
fd361982 12403 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12404 }
12405 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12406 {
049412e3 12407 dwo_sections->str_offsets.s.section = sectp;
fd361982 12408 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12409 }
12410 else if (section_is_p (sectp->name, &names->types_dwo))
12411 {
12412 struct dwarf2_section_info type_section;
12413
12414 memset (&type_section, 0, sizeof (type_section));
049412e3 12415 type_section.s.section = sectp;
fd361982 12416 type_section.size = bfd_section_size (sectp);
fd5866f6 12417 dwo_sections->types.push_back (type_section);
80626a55
DE
12418 }
12419}
12420
ab5088bf 12421/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12422 by PER_CU. This is for the non-DWP case.
80626a55 12423 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12424
12425static struct dwo_file *
4ab09049
SM
12426open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12427 const char *comp_dir)
3019eac3 12428{
976ca316 12429 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12430
976ca316 12431 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12432 if (dbfd == NULL)
12433 {
b4f54984 12434 if (dwarf_read_debug)
80626a55
DE
12435 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12436 return NULL;
12437 }
263db9a1 12438
51ac9db5 12439 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12440 dwo_file->dwo_name = dwo_name;
12441 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12442 dwo_file->dbfd = std::move (dbfd);
3019eac3 12443
fb1eb2f9 12444 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12445 &dwo_file->sections);
3019eac3 12446
976ca316
SM
12447 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12448 dwo_file->cus);
3019eac3 12449
976ca316 12450 create_debug_types_hash_table (per_objfile, dwo_file.get (),
ed2dc618 12451 dwo_file->sections.types, dwo_file->tus);
3019eac3 12452
b4f54984 12453 if (dwarf_read_debug)
80626a55
DE
12454 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12455
263db9a1 12456 return dwo_file.release ();
3019eac3
DE
12457}
12458
80626a55 12459/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12460 size of each of the DWP debugging sections common to version 1 and 2 that
12461 we are interested in. */
3019eac3 12462
80626a55 12463static void
73869dc2
DE
12464dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12465 void *dwp_file_ptr)
3019eac3 12466{
9a3c8263 12467 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12468 const struct dwop_section_names *names = &dwop_section_names;
12469 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12470
80626a55 12471 /* Record the ELF section number for later lookup: this is what the
73869dc2 12472 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12473 gdb_assert (elf_section_nr < dwp_file->num_sections);
12474 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12475
80626a55
DE
12476 /* Look for specific sections that we need. */
12477 if (section_is_p (sectp->name, &names->str_dwo))
12478 {
049412e3 12479 dwp_file->sections.str.s.section = sectp;
fd361982 12480 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12481 }
12482 else if (section_is_p (sectp->name, &names->cu_index))
12483 {
049412e3 12484 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12485 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12486 }
12487 else if (section_is_p (sectp->name, &names->tu_index))
12488 {
049412e3 12489 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12490 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12491 }
12492}
3019eac3 12493
73869dc2
DE
12494/* This function is mapped across the sections and remembers the offset and
12495 size of each of the DWP version 2 debugging sections that we are interested
12496 in. This is split into a separate function because we don't know if we
12497 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12498
12499static void
12500dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12501{
9a3c8263 12502 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12503 const struct dwop_section_names *names = &dwop_section_names;
12504 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12505
12506 /* Record the ELF section number for later lookup: this is what the
12507 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12508 gdb_assert (elf_section_nr < dwp_file->num_sections);
12509 dwp_file->elf_sections[elf_section_nr] = sectp;
12510
12511 /* Look for specific sections that we need. */
12512 if (section_is_p (sectp->name, &names->abbrev_dwo))
12513 {
049412e3 12514 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12515 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12516 }
12517 else if (section_is_p (sectp->name, &names->info_dwo))
12518 {
049412e3 12519 dwp_file->sections.info.s.section = sectp;
fd361982 12520 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12521 }
12522 else if (section_is_p (sectp->name, &names->line_dwo))
12523 {
049412e3 12524 dwp_file->sections.line.s.section = sectp;
fd361982 12525 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12526 }
12527 else if (section_is_p (sectp->name, &names->loc_dwo))
12528 {
049412e3 12529 dwp_file->sections.loc.s.section = sectp;
fd361982 12530 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12531 }
12532 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12533 {
049412e3 12534 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12535 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12536 }
12537 else if (section_is_p (sectp->name, &names->macro_dwo))
12538 {
049412e3 12539 dwp_file->sections.macro.s.section = sectp;
fd361982 12540 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12541 }
12542 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12543 {
049412e3 12544 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12545 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12546 }
12547 else if (section_is_p (sectp->name, &names->types_dwo))
12548 {
049412e3 12549 dwp_file->sections.types.s.section = sectp;
fd361982 12550 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12551 }
12552}
12553
80626a55 12554/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12555
80626a55
DE
12556static hashval_t
12557hash_dwp_loaded_cutus (const void *item)
12558{
9a3c8263 12559 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12560
80626a55
DE
12561 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12562 return dwo_unit->signature;
3019eac3
DE
12563}
12564
80626a55 12565/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12566
80626a55
DE
12567static int
12568eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12569{
9a3c8263
SM
12570 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12571 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12572
80626a55
DE
12573 return dua->signature == dub->signature;
12574}
3019eac3 12575
80626a55 12576/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12577
48b490f2 12578static htab_up
298e9637 12579allocate_dwp_loaded_cutus_table ()
80626a55 12580{
48b490f2
TT
12581 return htab_up (htab_create_alloc (3,
12582 hash_dwp_loaded_cutus,
12583 eq_dwp_loaded_cutus,
12584 NULL, xcalloc, xfree));
80626a55 12585}
3019eac3 12586
ab5088bf
DE
12587/* Try to open DWP file FILE_NAME.
12588 The result is the bfd handle of the file.
12589 If there is a problem finding or opening the file, return NULL.
12590 Upon success, the canonicalized path of the file is stored in the bfd,
12591 same as symfile_bfd_open. */
12592
192b62ce 12593static gdb_bfd_ref_ptr
976ca316 12594open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12595{
976ca316 12596 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12597 1 /*is_dwp*/,
192b62ce 12598 1 /*search_cwd*/));
6ac97d4c
DE
12599 if (abfd != NULL)
12600 return abfd;
12601
12602 /* Work around upstream bug 15652.
12603 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12604 [Whether that's a "bug" is debatable, but it is getting in our way.]
12605 We have no real idea where the dwp file is, because gdb's realpath-ing
12606 of the executable's path may have discarded the needed info.
12607 [IWBN if the dwp file name was recorded in the executable, akin to
12608 .gnu_debuglink, but that doesn't exist yet.]
12609 Strip the directory from FILE_NAME and search again. */
12610 if (*debug_file_directory != '\0')
12611 {
12612 /* Don't implicitly search the current directory here.
12613 If the user wants to search "." to handle this case,
12614 it must be added to debug-file-directory. */
976ca316
SM
12615 return try_open_dwop_file (per_objfile, lbasename (file_name),
12616 1 /*is_dwp*/,
6ac97d4c
DE
12617 0 /*search_cwd*/);
12618 }
12619
12620 return NULL;
ab5088bf
DE
12621}
12622
80626a55
DE
12623/* Initialize the use of the DWP file for the current objfile.
12624 By convention the name of the DWP file is ${objfile}.dwp.
12625 The result is NULL if it can't be found. */
a766d390 12626
400174b1 12627static std::unique_ptr<struct dwp_file>
976ca316 12628open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12629{
976ca316 12630 struct objfile *objfile = per_objfile->objfile;
80626a55 12631
82bf32bc
JK
12632 /* Try to find first .dwp for the binary file before any symbolic links
12633 resolving. */
6c447423
DE
12634
12635 /* If the objfile is a debug file, find the name of the real binary
12636 file and get the name of dwp file from there. */
d721ba37 12637 std::string dwp_name;
6c447423
DE
12638 if (objfile->separate_debug_objfile_backlink != NULL)
12639 {
12640 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12641 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12642
d721ba37 12643 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12644 }
12645 else
d721ba37
PA
12646 dwp_name = objfile->original_name;
12647
12648 dwp_name += ".dwp";
80626a55 12649
976ca316 12650 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12651 if (dbfd == NULL
12652 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12653 {
12654 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12655 dwp_name = objfile_name (objfile);
12656 dwp_name += ".dwp";
976ca316 12657 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12658 }
12659
80626a55
DE
12660 if (dbfd == NULL)
12661 {
b4f54984 12662 if (dwarf_read_debug)
d721ba37 12663 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12664 return std::unique_ptr<dwp_file> ();
3019eac3 12665 }
400174b1
TT
12666
12667 const char *name = bfd_get_filename (dbfd.get ());
12668 std::unique_ptr<struct dwp_file> dwp_file
12669 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12670
0a0f4c01 12671 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12672 dwp_file->elf_sections =
976ca316 12673 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12674 dwp_file->num_sections, asection *);
12675
400174b1
TT
12676 bfd_map_over_sections (dwp_file->dbfd.get (),
12677 dwarf2_locate_common_dwp_sections,
12678 dwp_file.get ());
80626a55 12679
976ca316 12680 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12681
976ca316 12682 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12683
73869dc2 12684 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12685 if (dwp_file->cus && dwp_file->tus
12686 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12687 {
12688 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12689 pretty bizarre. We use pulongest here because that's the established
4d65956b 12690 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12691 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12692 " TU version %s [in DWP file %s]"),
12693 pulongest (dwp_file->cus->version),
d721ba37 12694 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12695 }
08302ed2
DE
12696
12697 if (dwp_file->cus)
12698 dwp_file->version = dwp_file->cus->version;
12699 else if (dwp_file->tus)
12700 dwp_file->version = dwp_file->tus->version;
12701 else
12702 dwp_file->version = 2;
73869dc2
DE
12703
12704 if (dwp_file->version == 2)
400174b1
TT
12705 bfd_map_over_sections (dwp_file->dbfd.get (),
12706 dwarf2_locate_v2_dwp_sections,
12707 dwp_file.get ());
73869dc2 12708
298e9637
SM
12709 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12710 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12711
b4f54984 12712 if (dwarf_read_debug)
80626a55
DE
12713 {
12714 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12715 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12716 " %s CUs, %s TUs\n",
12717 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12718 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12719 }
12720
12721 return dwp_file;
3019eac3 12722}
c906108c 12723
ab5088bf
DE
12724/* Wrapper around open_and_init_dwp_file, only open it once. */
12725
12726static struct dwp_file *
976ca316 12727get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12728{
976ca316 12729 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12730 {
976ca316
SM
12731 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12732 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12733 }
976ca316 12734 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12735}
12736
80626a55
DE
12737/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12738 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12739 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12740 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12741 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12742
12743 This is called, for example, when wanting to read a variable with a
12744 complex location. Therefore we don't want to do file i/o for every call.
12745 Therefore we don't want to look for a DWO file on every call.
12746 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12747 then we check if we've already seen DWO_NAME, and only THEN do we check
12748 for a DWO file.
12749
1c658ad5 12750 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12751 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12752
3019eac3 12753static struct dwo_unit *
4ab09049 12754lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12755 ULONGEST signature, int is_debug_types)
3019eac3 12756{
976ca316
SM
12757 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12758 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12759 const char *kind = is_debug_types ? "TU" : "CU";
12760 void **dwo_file_slot;
3019eac3 12761 struct dwo_file *dwo_file;
80626a55 12762 struct dwp_file *dwp_file;
cb1df416 12763
6a506a2d
DE
12764 /* First see if there's a DWP file.
12765 If we have a DWP file but didn't find the DWO inside it, don't
12766 look for the original DWO file. It makes gdb behave differently
12767 depending on whether one is debugging in the build tree. */
cf2c3c16 12768
976ca316 12769 dwp_file = get_dwp_file (per_objfile);
80626a55 12770 if (dwp_file != NULL)
cf2c3c16 12771 {
80626a55
DE
12772 const struct dwp_hash_table *dwp_htab =
12773 is_debug_types ? dwp_file->tus : dwp_file->cus;
12774
12775 if (dwp_htab != NULL)
12776 {
12777 struct dwo_unit *dwo_cutu =
976ca316
SM
12778 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12779 is_debug_types);
80626a55
DE
12780
12781 if (dwo_cutu != NULL)
12782 {
b4f54984 12783 if (dwarf_read_debug)
80626a55
DE
12784 {
12785 fprintf_unfiltered (gdb_stdlog,
12786 "Virtual DWO %s %s found: @%s\n",
12787 kind, hex_string (signature),
12788 host_address_to_string (dwo_cutu));
12789 }
12790 return dwo_cutu;
12791 }
12792 }
12793 }
6a506a2d 12794 else
80626a55 12795 {
6a506a2d 12796 /* No DWP file, look for the DWO file. */
80626a55 12797
976ca316 12798 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12799 if (*dwo_file_slot == NULL)
80626a55 12800 {
6a506a2d 12801 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12802 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12803 }
6a506a2d 12804 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12805 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12806
6a506a2d 12807 if (dwo_file != NULL)
19c3d4c9 12808 {
6a506a2d
DE
12809 struct dwo_unit *dwo_cutu = NULL;
12810
12811 if (is_debug_types && dwo_file->tus)
12812 {
12813 struct dwo_unit find_dwo_cutu;
12814
12815 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12816 find_dwo_cutu.signature = signature;
9a3c8263 12817 dwo_cutu
b0b6a987
TT
12818 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12819 &find_dwo_cutu);
6a506a2d 12820 }
33c5cd75 12821 else if (!is_debug_types && dwo_file->cus)
80626a55 12822 {
33c5cd75
DB
12823 struct dwo_unit find_dwo_cutu;
12824
12825 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12826 find_dwo_cutu.signature = signature;
b0b6a987 12827 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12828 &find_dwo_cutu);
6a506a2d
DE
12829 }
12830
12831 if (dwo_cutu != NULL)
12832 {
b4f54984 12833 if (dwarf_read_debug)
6a506a2d
DE
12834 {
12835 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12836 kind, dwo_name, hex_string (signature),
12837 host_address_to_string (dwo_cutu));
12838 }
12839 return dwo_cutu;
80626a55
DE
12840 }
12841 }
2e276125 12842 }
9cdd5dbd 12843
80626a55
DE
12844 /* We didn't find it. This could mean a dwo_id mismatch, or
12845 someone deleted the DWO/DWP file, or the search path isn't set up
12846 correctly to find the file. */
12847
b4f54984 12848 if (dwarf_read_debug)
80626a55
DE
12849 {
12850 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12851 kind, dwo_name, hex_string (signature));
12852 }
3019eac3 12853
6656a72d
DE
12854 /* This is a warning and not a complaint because it can be caused by
12855 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12856 {
12857 /* Print the name of the DWP file if we looked there, helps the user
12858 better diagnose the problem. */
791afaa2 12859 std::string dwp_text;
43942612
DE
12860
12861 if (dwp_file != NULL)
791afaa2
TT
12862 dwp_text = string_printf (" [in DWP file %s]",
12863 lbasename (dwp_file->name));
43942612 12864
9d8780f0 12865 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12866 " [in module %s]"),
4ab09049
SM
12867 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12868 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12869 }
3019eac3 12870 return NULL;
5fb290d7
DJ
12871}
12872
80626a55
DE
12873/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12874 See lookup_dwo_cutu_unit for details. */
12875
12876static struct dwo_unit *
4ab09049 12877lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12878 ULONGEST signature)
12879{
4ab09049
SM
12880 gdb_assert (!cu->per_cu->is_debug_types);
12881
12882 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12883}
12884
12885/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12886 See lookup_dwo_cutu_unit for details. */
12887
12888static struct dwo_unit *
4ab09049 12889lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12890{
4ab09049
SM
12891 gdb_assert (cu->per_cu->is_debug_types);
12892
12893 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12894
12895 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12896}
12897
89e63ee4
DE
12898/* Traversal function for queue_and_load_all_dwo_tus. */
12899
12900static int
12901queue_and_load_dwo_tu (void **slot, void *info)
12902{
12903 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12904 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12905 ULONGEST signature = dwo_unit->signature;
d460f660 12906 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12907
12908 if (sig_type != NULL)
12909 {
12910 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12911
12912 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12913 a real dependency of PER_CU on SIG_TYPE. That is detected later
12914 while processing PER_CU. */
120ce1b5 12915 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
12916 load_full_type_unit (sig_cu, cu->per_objfile);
12917 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12918 }
12919
12920 return 1;
12921}
12922
1b555f17 12923/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12924 The DWO may have the only definition of the type, though it may not be
12925 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12926 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12927
12928static void
1b555f17 12929queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12930{
12931 struct dwo_unit *dwo_unit;
12932 struct dwo_file *dwo_file;
12933
1b555f17
SM
12934 gdb_assert (cu != nullptr);
12935 gdb_assert (!cu->per_cu->is_debug_types);
12936 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12937
1b555f17 12938 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12939 gdb_assert (dwo_unit != NULL);
12940
12941 dwo_file = dwo_unit->dwo_file;
12942 if (dwo_file->tus != NULL)
1b555f17 12943 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12944}
12945
3019eac3 12946/* Read in various DIEs. */
348e048f 12947
d389af10 12948/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12949 Inherit only the children of the DW_AT_abstract_origin DIE not being
12950 already referenced by DW_AT_abstract_origin from the children of the
12951 current DIE. */
d389af10
JK
12952
12953static void
12954inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12955{
12956 struct die_info *child_die;
791afaa2 12957 sect_offset *offsetp;
d389af10
JK
12958 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12959 struct die_info *origin_die;
12960 /* Iterator of the ORIGIN_DIE children. */
12961 struct die_info *origin_child_die;
d389af10 12962 struct attribute *attr;
cd02d79d
PA
12963 struct dwarf2_cu *origin_cu;
12964 struct pending **origin_previous_list_in_scope;
d389af10
JK
12965
12966 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12967 if (!attr)
12968 return;
12969
cd02d79d
PA
12970 /* Note that following die references may follow to a die in a
12971 different cu. */
12972
12973 origin_cu = cu;
12974 origin_die = follow_die_ref (die, attr, &origin_cu);
12975
12976 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12977 symbols in. */
12978 origin_previous_list_in_scope = origin_cu->list_in_scope;
12979 origin_cu->list_in_scope = cu->list_in_scope;
12980
edb3359d
DJ
12981 if (die->tag != origin_die->tag
12982 && !(die->tag == DW_TAG_inlined_subroutine
12983 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12984 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12985 sect_offset_str (die->sect_off),
12986 sect_offset_str (origin_die->sect_off));
d389af10 12987
791afaa2 12988 std::vector<sect_offset> offsets;
d389af10 12989
3ea89b92
PMR
12990 for (child_die = die->child;
12991 child_die && child_die->tag;
436c571c 12992 child_die = child_die->sibling)
3ea89b92
PMR
12993 {
12994 struct die_info *child_origin_die;
12995 struct dwarf2_cu *child_origin_cu;
12996
12997 /* We are trying to process concrete instance entries:
216f72a1 12998 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12999 it's not relevant to our analysis here. i.e. detecting DIEs that are
13000 present in the abstract instance but not referenced in the concrete
13001 one. */
216f72a1
JK
13002 if (child_die->tag == DW_TAG_call_site
13003 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13004 continue;
13005
c38f313d
DJ
13006 /* For each CHILD_DIE, find the corresponding child of
13007 ORIGIN_DIE. If there is more than one layer of
13008 DW_AT_abstract_origin, follow them all; there shouldn't be,
13009 but GCC versions at least through 4.4 generate this (GCC PR
13010 40573). */
3ea89b92
PMR
13011 child_origin_die = child_die;
13012 child_origin_cu = cu;
c38f313d
DJ
13013 while (1)
13014 {
cd02d79d
PA
13015 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13016 child_origin_cu);
c38f313d
DJ
13017 if (attr == NULL)
13018 break;
cd02d79d
PA
13019 child_origin_die = follow_die_ref (child_origin_die, attr,
13020 &child_origin_cu);
c38f313d
DJ
13021 }
13022
d389af10
JK
13023 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13024 counterpart may exist. */
c38f313d 13025 if (child_origin_die != child_die)
d389af10 13026 {
edb3359d
DJ
13027 if (child_die->tag != child_origin_die->tag
13028 && !(child_die->tag == DW_TAG_inlined_subroutine
13029 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13030 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13031 "different tags"),
9d8780f0
SM
13032 sect_offset_str (child_die->sect_off),
13033 sect_offset_str (child_origin_die->sect_off));
c38f313d 13034 if (child_origin_die->parent != origin_die)
b98664d3 13035 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13036 "different parents"),
9d8780f0
SM
13037 sect_offset_str (child_die->sect_off),
13038 sect_offset_str (child_origin_die->sect_off));
c38f313d 13039 else
791afaa2 13040 offsets.push_back (child_origin_die->sect_off);
d389af10 13041 }
d389af10 13042 }
791afaa2
TT
13043 std::sort (offsets.begin (), offsets.end ());
13044 sect_offset *offsets_end = offsets.data () + offsets.size ();
13045 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13046 if (offsetp[-1] == *offsetp)
b98664d3 13047 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13048 "to DIE %s as their abstract origin"),
13049 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13050
791afaa2 13051 offsetp = offsets.data ();
d389af10
JK
13052 origin_child_die = origin_die->child;
13053 while (origin_child_die && origin_child_die->tag)
13054 {
13055 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13056 while (offsetp < offsets_end
9c541725 13057 && *offsetp < origin_child_die->sect_off)
d389af10 13058 offsetp++;
b64f50a1 13059 if (offsetp >= offsets_end
9c541725 13060 || *offsetp > origin_child_die->sect_off)
d389af10 13061 {
adde2bff
DE
13062 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13063 Check whether we're already processing ORIGIN_CHILD_DIE.
13064 This can happen with mutually referenced abstract_origins.
13065 PR 16581. */
13066 if (!origin_child_die->in_process)
13067 process_die (origin_child_die, origin_cu);
d389af10 13068 }
436c571c 13069 origin_child_die = origin_child_die->sibling;
d389af10 13070 }
cd02d79d 13071 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13072
13073 if (cu != origin_cu)
13074 compute_delayed_physnames (origin_cu);
d389af10
JK
13075}
13076
c906108c 13077static void
e7c27a73 13078read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13079{
5e22e966 13080 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13081 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13082 struct context_stack *newobj;
c906108c
SS
13083 CORE_ADDR lowpc;
13084 CORE_ADDR highpc;
13085 struct die_info *child_die;
edb3359d 13086 struct attribute *attr, *call_line, *call_file;
15d034d0 13087 const char *name;
e142c38c 13088 CORE_ADDR baseaddr;
801e3a5b 13089 struct block *block;
edb3359d 13090 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13091 std::vector<struct symbol *> template_args;
34eaf542 13092 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13093
13094 if (inlined_func)
13095 {
13096 /* If we do not have call site information, we can't show the
13097 caller of this inlined function. That's too confusing, so
13098 only use the scope for local variables. */
13099 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13100 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13101 if (call_line == NULL || call_file == NULL)
13102 {
13103 read_lexical_block_scope (die, cu);
13104 return;
13105 }
13106 }
c906108c 13107
b3b3bada 13108 baseaddr = objfile->text_section_offset ();
e142c38c 13109
94af9270 13110 name = dwarf2_name (die, cu);
c906108c 13111
e8d05480
JB
13112 /* Ignore functions with missing or empty names. These are actually
13113 illegal according to the DWARF standard. */
13114 if (name == NULL)
13115 {
b98664d3 13116 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13117 sect_offset_str (die->sect_off));
e8d05480
JB
13118 return;
13119 }
13120
13121 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13122 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13123 <= PC_BOUNDS_INVALID)
e8d05480 13124 {
ae4d0c03
PM
13125 attr = dwarf2_attr (die, DW_AT_external, cu);
13126 if (!attr || !DW_UNSND (attr))
b98664d3 13127 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13128 "for subprogram DIE at %s"),
13129 sect_offset_str (die->sect_off));
e8d05480
JB
13130 return;
13131 }
c906108c 13132
3e29f34a
MR
13133 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13134 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13135
34eaf542
TT
13136 /* If we have any template arguments, then we must allocate a
13137 different sort of symbol. */
436c571c 13138 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13139 {
13140 if (child_die->tag == DW_TAG_template_type_param
13141 || child_die->tag == DW_TAG_template_value_param)
13142 {
8c14c3a3 13143 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13144 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13145 break;
13146 }
13147 }
13148
c24bdb02 13149 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13150 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13151 (struct symbol *) templ_func);
4c2df51b 13152
81873cc8 13153 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13154 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13155 cu->language);
13156
4cecd739
DJ
13157 /* If there is a location expression for DW_AT_frame_base, record
13158 it. */
e142c38c 13159 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13160 if (attr != nullptr)
fe978cb0 13161 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13162
63e43d3a
PMR
13163 /* If there is a location for the static link, record it. */
13164 newobj->static_link = NULL;
13165 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13166 if (attr != nullptr)
63e43d3a 13167 {
224c3ddb
SM
13168 newobj->static_link
13169 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13170 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13171 cu->addr_type ());
63e43d3a
PMR
13172 }
13173
c24bdb02 13174 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13175
639d11d3 13176 if (die->child != NULL)
c906108c 13177 {
639d11d3 13178 child_die = die->child;
c906108c
SS
13179 while (child_die && child_die->tag)
13180 {
34eaf542
TT
13181 if (child_die->tag == DW_TAG_template_type_param
13182 || child_die->tag == DW_TAG_template_value_param)
13183 {
13184 struct symbol *arg = new_symbol (child_die, NULL, cu);
13185
f1078f66 13186 if (arg != NULL)
2f4732b0 13187 template_args.push_back (arg);
34eaf542
TT
13188 }
13189 else
13190 process_die (child_die, cu);
436c571c 13191 child_die = child_die->sibling;
c906108c
SS
13192 }
13193 }
13194
d389af10
JK
13195 inherit_abstract_dies (die, cu);
13196
4a811a97
UW
13197 /* If we have a DW_AT_specification, we might need to import using
13198 directives from the context of the specification DIE. See the
13199 comment in determine_prefix. */
13200 if (cu->language == language_cplus
13201 && dwarf2_attr (die, DW_AT_specification, cu))
13202 {
13203 struct dwarf2_cu *spec_cu = cu;
13204 struct die_info *spec_die = die_specification (die, &spec_cu);
13205
13206 while (spec_die)
13207 {
13208 child_die = spec_die->child;
13209 while (child_die && child_die->tag)
13210 {
13211 if (child_die->tag == DW_TAG_imported_module)
13212 process_die (child_die, spec_cu);
436c571c 13213 child_die = child_die->sibling;
4a811a97
UW
13214 }
13215
13216 /* In some cases, GCC generates specification DIEs that
13217 themselves contain DW_AT_specification attributes. */
13218 spec_die = die_specification (spec_die, &spec_cu);
13219 }
13220 }
13221
c24bdb02 13222 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13223 /* Make a block for the local symbols within. */
c24bdb02 13224 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13225 cstk.static_link, lowpc, highpc);
801e3a5b 13226
df8a16a1 13227 /* For C++, set the block's scope. */
45280282
IB
13228 if ((cu->language == language_cplus
13229 || cu->language == language_fortran
c44af4eb
TT
13230 || cu->language == language_d
13231 || cu->language == language_rust)
4d4ec4e5 13232 && cu->processing_has_namespace_info)
195a3f6c
TT
13233 block_set_scope (block, determine_prefix (die, cu),
13234 &objfile->objfile_obstack);
df8a16a1 13235
801e3a5b
JB
13236 /* If we have address ranges, record them. */
13237 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13238
a60f3166 13239 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13240
34eaf542 13241 /* Attach template arguments to function. */
2f4732b0 13242 if (!template_args.empty ())
34eaf542
TT
13243 {
13244 gdb_assert (templ_func != NULL);
13245
2f4732b0 13246 templ_func->n_template_arguments = template_args.size ();
34eaf542 13247 templ_func->template_arguments
8d749320
SM
13248 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13249 templ_func->n_template_arguments);
34eaf542 13250 memcpy (templ_func->template_arguments,
2f4732b0 13251 template_args.data (),
34eaf542 13252 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13253
13254 /* Make sure that the symtab is set on the new symbols. Even
13255 though they don't appear in this symtab directly, other parts
13256 of gdb assume that symbols do, and this is reasonably
13257 true. */
8634679f 13258 for (symbol *sym : template_args)
3e1d3d8c 13259 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13260 }
13261
208d8187
JB
13262 /* In C++, we can have functions nested inside functions (e.g., when
13263 a function declares a class that has methods). This means that
13264 when we finish processing a function scope, we may need to go
13265 back to building a containing block's symbol lists. */
c24bdb02
KS
13266 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13267 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13268
921e78cf
JB
13269 /* If we've finished processing a top-level function, subsequent
13270 symbols go in the file symbol list. */
c24bdb02
KS
13271 if (cu->get_builder ()->outermost_context_p ())
13272 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13273}
13274
13275/* Process all the DIES contained within a lexical block scope. Start
13276 a new scope, process the dies, and then close the scope. */
13277
13278static void
e7c27a73 13279read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13280{
5e22e966 13281 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13282 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13283 CORE_ADDR lowpc, highpc;
13284 struct die_info *child_die;
e142c38c
DJ
13285 CORE_ADDR baseaddr;
13286
b3b3bada 13287 baseaddr = objfile->text_section_offset ();
c906108c
SS
13288
13289 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13290 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13291 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13292 be nasty. Might be easier to properly extend generic blocks to
af34e669 13293 describe ranges. */
e385593e
JK
13294 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13295 {
13296 case PC_BOUNDS_NOT_PRESENT:
13297 /* DW_TAG_lexical_block has no attributes, process its children as if
13298 there was no wrapping by that DW_TAG_lexical_block.
13299 GCC does no longer produces such DWARF since GCC r224161. */
13300 for (child_die = die->child;
13301 child_die != NULL && child_die->tag;
436c571c 13302 child_die = child_die->sibling)
4f7bc5ed
TT
13303 {
13304 /* We might already be processing this DIE. This can happen
13305 in an unusual circumstance -- where a subroutine A
13306 appears lexically in another subroutine B, but A actually
13307 inlines B. The recursion is broken here, rather than in
13308 inherit_abstract_dies, because it seems better to simply
13309 drop concrete children here. */
13310 if (!child_die->in_process)
13311 process_die (child_die, cu);
13312 }
e385593e
JK
13313 return;
13314 case PC_BOUNDS_INVALID:
13315 return;
13316 }
3e29f34a
MR
13317 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13318 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13319
c24bdb02 13320 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13321 if (die->child != NULL)
c906108c 13322 {
639d11d3 13323 child_die = die->child;
c906108c
SS
13324 while (child_die && child_die->tag)
13325 {
e7c27a73 13326 process_die (child_die, cu);
436c571c 13327 child_die = child_die->sibling;
c906108c
SS
13328 }
13329 }
3ea89b92 13330 inherit_abstract_dies (die, cu);
c24bdb02 13331 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13332
c24bdb02
KS
13333 if (*cu->get_builder ()->get_local_symbols () != NULL
13334 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13335 {
801e3a5b 13336 struct block *block
c24bdb02 13337 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13338 cstk.start_addr, highpc);
801e3a5b
JB
13339
13340 /* Note that recording ranges after traversing children, as we
13341 do here, means that recording a parent's ranges entails
13342 walking across all its children's ranges as they appear in
13343 the address map, which is quadratic behavior.
13344
13345 It would be nicer to record the parent's ranges before
13346 traversing its children, simply overriding whatever you find
13347 there. But since we don't even decide whether to create a
13348 block until after we've traversed its children, that's hard
13349 to do. */
13350 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13351 }
c24bdb02
KS
13352 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13353 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13354}
13355
216f72a1 13356/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13357
13358static void
13359read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13360{
5e22e966 13361 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13362 struct objfile *objfile = per_objfile->objfile;
08feed99 13363 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13364 CORE_ADDR pc, baseaddr;
13365 struct attribute *attr;
13366 struct call_site *call_site, call_site_local;
13367 void **slot;
13368 int nparams;
13369 struct die_info *child_die;
13370
b3b3bada 13371 baseaddr = objfile->text_section_offset ();
96408a79 13372
216f72a1
JK
13373 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13374 if (attr == NULL)
13375 {
13376 /* This was a pre-DWARF-5 GNU extension alias
13377 for DW_AT_call_return_pc. */
13378 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13379 }
96408a79
SA
13380 if (!attr)
13381 {
b98664d3 13382 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13383 "DIE %s [in module %s]"),
13384 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13385 return;
13386 }
cd6c91b4 13387 pc = attr->value_as_address () + baseaddr;
3e29f34a 13388 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13389
13390 if (cu->call_site_htab == NULL)
13391 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13392 NULL, &objfile->objfile_obstack,
13393 hashtab_obstack_allocate, NULL);
13394 call_site_local.pc = pc;
13395 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13396 if (*slot != NULL)
13397 {
b98664d3 13398 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13399 "DIE %s [in module %s]"),
13400 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13401 objfile_name (objfile));
96408a79
SA
13402 return;
13403 }
13404
13405 /* Count parameters at the caller. */
13406
13407 nparams = 0;
13408 for (child_die = die->child; child_die && child_die->tag;
436c571c 13409 child_die = child_die->sibling)
96408a79 13410 {
216f72a1
JK
13411 if (child_die->tag != DW_TAG_call_site_parameter
13412 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13413 {
b98664d3 13414 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13415 "DW_TAG_call_site child DIE %s [in module %s]"),
13416 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13417 objfile_name (objfile));
96408a79
SA
13418 continue;
13419 }
13420
13421 nparams++;
13422 }
13423
224c3ddb
SM
13424 call_site
13425 = ((struct call_site *)
13426 obstack_alloc (&objfile->objfile_obstack,
13427 sizeof (*call_site)
13428 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13429 *slot = call_site;
13430 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13431 call_site->pc = pc;
13432
216f72a1
JK
13433 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13434 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13435 {
13436 struct die_info *func_die;
13437
13438 /* Skip also over DW_TAG_inlined_subroutine. */
13439 for (func_die = die->parent;
13440 func_die && func_die->tag != DW_TAG_subprogram
13441 && func_die->tag != DW_TAG_subroutine_type;
13442 func_die = func_die->parent);
13443
216f72a1
JK
13444 /* DW_AT_call_all_calls is a superset
13445 of DW_AT_call_all_tail_calls. */
96408a79 13446 if (func_die
216f72a1 13447 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13448 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13449 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13450 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13451 {
13452 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13453 not complete. But keep CALL_SITE for look ups via call_site_htab,
13454 both the initial caller containing the real return address PC and
13455 the final callee containing the current PC of a chain of tail
13456 calls do not need to have the tail call list complete. But any
13457 function candidate for a virtual tail call frame searched via
13458 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13459 determined unambiguously. */
13460 }
13461 else
13462 {
13463 struct type *func_type = NULL;
13464
13465 if (func_die)
13466 func_type = get_die_type (func_die, cu);
13467 if (func_type != NULL)
13468 {
78134374 13469 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13470
13471 /* Enlist this call site to the function. */
13472 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13473 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13474 }
13475 else
b98664d3 13476 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13477 "DIE %s [in module %s]"),
13478 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13479 }
13480 }
13481
216f72a1
JK
13482 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13483 if (attr == NULL)
13484 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13485 if (attr == NULL)
13486 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13487 if (attr == NULL)
216f72a1
JK
13488 {
13489 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13490 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13491 }
96408a79 13492 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13493 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13494 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13495 else if (attr->form_is_block ())
96408a79
SA
13496 {
13497 struct dwarf2_locexpr_baton *dlbaton;
13498
8d749320 13499 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13500 dlbaton->data = DW_BLOCK (attr)->data;
13501 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13502 dlbaton->per_objfile = per_objfile;
96408a79
SA
13503 dlbaton->per_cu = cu->per_cu;
13504
13505 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13506 }
cd6c91b4 13507 else if (attr->form_is_ref ())
96408a79 13508 {
96408a79
SA
13509 struct dwarf2_cu *target_cu = cu;
13510 struct die_info *target_die;
13511
ac9ec31b 13512 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13513 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13514 if (die_is_declaration (target_die, target_cu))
13515 {
7d45c7c3 13516 const char *target_physname;
9112db09
JK
13517
13518 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13519 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13520 if (target_physname == NULL)
9112db09 13521 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13522 if (target_physname == NULL)
b98664d3 13523 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13524 "physname, for referencing DIE %s [in module %s]"),
13525 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13526 else
7d455152 13527 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13528 }
13529 else
13530 {
13531 CORE_ADDR lowpc;
13532
13533 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13534 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13535 <= PC_BOUNDS_INVALID)
b98664d3 13536 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13537 "low pc, for referencing DIE %s [in module %s]"),
13538 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13539 else
3e29f34a
MR
13540 {
13541 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13542 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13543 }
96408a79
SA
13544 }
13545 }
13546 else
b98664d3 13547 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13548 "block nor reference, for DIE %s [in module %s]"),
13549 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13550
13551 call_site->per_cu = cu->per_cu;
9f47c707 13552 call_site->per_objfile = per_objfile;
96408a79
SA
13553
13554 for (child_die = die->child;
13555 child_die && child_die->tag;
436c571c 13556 child_die = child_die->sibling)
96408a79 13557 {
96408a79 13558 struct call_site_parameter *parameter;
1788b2d3 13559 struct attribute *loc, *origin;
96408a79 13560
216f72a1
JK
13561 if (child_die->tag != DW_TAG_call_site_parameter
13562 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13563 {
13564 /* Already printed the complaint above. */
13565 continue;
13566 }
13567
13568 gdb_assert (call_site->parameter_count < nparams);
13569 parameter = &call_site->parameter[call_site->parameter_count];
13570
1788b2d3
JK
13571 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13572 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13573 register is contained in DW_AT_call_value. */
96408a79 13574
24c5c679 13575 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13576 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13577 if (origin == NULL)
13578 {
13579 /* This was a pre-DWARF-5 GNU extension alias
13580 for DW_AT_call_parameter. */
13581 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13582 }
cd6c91b4 13583 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13584 {
1788b2d3 13585 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13586
0826b30a 13587 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13588 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13589 {
13590 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13591 binding can be done only inside one CU. Such referenced DIE
13592 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13593 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13594 "DW_TAG_call_site child DIE %s [in module %s]"),
13595 sect_offset_str (child_die->sect_off),
9c541725 13596 objfile_name (objfile));
d76b7dbc
JK
13597 continue;
13598 }
9c541725
PA
13599 parameter->u.param_cu_off
13600 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13601 }
4fc6c0d5 13602 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13603 {
b98664d3 13604 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13605 "DW_TAG_call_site child DIE %s [in module %s]"),
13606 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13607 continue;
13608 }
24c5c679 13609 else
96408a79 13610 {
24c5c679
JK
13611 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13612 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13613 if (parameter->u.dwarf_reg != -1)
13614 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13615 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13616 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13617 &parameter->u.fb_offset))
13618 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13619 else
13620 {
b98664d3 13621 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13622 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13623 "DW_TAG_call_site child DIE %s "
24c5c679 13624 "[in module %s]"),
9d8780f0 13625 sect_offset_str (child_die->sect_off),
9c541725 13626 objfile_name (objfile));
24c5c679
JK
13627 continue;
13628 }
96408a79
SA
13629 }
13630
216f72a1
JK
13631 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13632 if (attr == NULL)
13633 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13634 if (attr == NULL || !attr->form_is_block ())
96408a79 13635 {
b98664d3 13636 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13637 "DW_TAG_call_site child DIE %s [in module %s]"),
13638 sect_offset_str (child_die->sect_off),
9c541725 13639 objfile_name (objfile));
96408a79
SA
13640 continue;
13641 }
13642 parameter->value = DW_BLOCK (attr)->data;
13643 parameter->value_size = DW_BLOCK (attr)->size;
13644
13645 /* Parameters are not pre-cleared by memset above. */
13646 parameter->data_value = NULL;
13647 parameter->data_value_size = 0;
13648 call_site->parameter_count++;
13649
216f72a1
JK
13650 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13651 if (attr == NULL)
13652 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13653 if (attr != nullptr)
96408a79 13654 {
4fc6c0d5 13655 if (!attr->form_is_block ())
b98664d3 13656 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13657 "DW_TAG_call_site child DIE %s [in module %s]"),
13658 sect_offset_str (child_die->sect_off),
9c541725 13659 objfile_name (objfile));
96408a79
SA
13660 else
13661 {
13662 parameter->data_value = DW_BLOCK (attr)->data;
13663 parameter->data_value_size = DW_BLOCK (attr)->size;
13664 }
13665 }
13666 }
13667}
13668
71a3c369
TT
13669/* Helper function for read_variable. If DIE represents a virtual
13670 table, then return the type of the concrete object that is
13671 associated with the virtual table. Otherwise, return NULL. */
13672
13673static struct type *
13674rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13675{
13676 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13677 if (attr == NULL)
13678 return NULL;
13679
13680 /* Find the type DIE. */
13681 struct die_info *type_die = NULL;
13682 struct dwarf2_cu *type_cu = cu;
13683
cd6c91b4 13684 if (attr->form_is_ref ())
71a3c369
TT
13685 type_die = follow_die_ref (die, attr, &type_cu);
13686 if (type_die == NULL)
13687 return NULL;
13688
13689 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13690 return NULL;
13691 return die_containing_type (type_die, type_cu);
13692}
13693
13694/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13695
13696static void
13697read_variable (struct die_info *die, struct dwarf2_cu *cu)
13698{
13699 struct rust_vtable_symbol *storage = NULL;
13700
13701 if (cu->language == language_rust)
13702 {
13703 struct type *containing_type = rust_containing_type (die, cu);
13704
13705 if (containing_type != NULL)
13706 {
5e22e966 13707 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13708
8c14c3a3 13709 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13710 storage->concrete_type = containing_type;
cf724bc9 13711 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13712 }
13713 }
13714
e4a62c65
TV
13715 struct symbol *res = new_symbol (die, NULL, cu, storage);
13716 struct attribute *abstract_origin
13717 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13718 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13719 if (res == NULL && loc && abstract_origin)
13720 {
13721 /* We have a variable without a name, but with a location and an abstract
13722 origin. This may be a concrete instance of an abstract variable
13723 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13724 later. */
13725 struct dwarf2_cu *origin_cu = cu;
13726 struct die_info *origin_die
13727 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13728 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13729 per_objfile->per_bfd->abstract_to_concrete
13730 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13731 }
71a3c369
TT
13732}
13733
43988095
JK
13734/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13735 reading .debug_rnglists.
13736 Callback's type should be:
13737 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13738 Return true if the attributes are present and valid, otherwise,
13739 return false. */
13740
13741template <typename Callback>
13742static bool
13743dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13744 Callback &&callback)
13745{
976ca316
SM
13746 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13747 struct objfile *objfile = per_objfile->objfile;
43988095 13748 bfd *obfd = objfile->obfd;
43988095 13749 /* Base address selection entry. */
2b24b6e4 13750 gdb::optional<CORE_ADDR> base;
43988095 13751 const gdb_byte *buffer;
43988095
JK
13752 CORE_ADDR baseaddr;
13753 bool overflow = false;
13754
43988095
JK
13755 base = cu->base_address;
13756
976ca316
SM
13757 per_objfile->per_bfd->rnglists.read (objfile);
13758 if (offset >= per_objfile->per_bfd->rnglists.size)
43988095 13759 {
b98664d3 13760 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13761 offset);
13762 return false;
13763 }
976ca316 13764 buffer = per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13765
b3b3bada 13766 baseaddr = objfile->text_section_offset ();
43988095
JK
13767
13768 while (1)
13769 {
7814882a
JK
13770 /* Initialize it due to a false compiler warning. */
13771 CORE_ADDR range_beginning = 0, range_end = 0;
976ca316
SM
13772 const gdb_byte *buf_end = (per_objfile->per_bfd->rnglists.buffer
13773 + per_objfile->per_bfd->rnglists.size);
43988095
JK
13774 unsigned int bytes_read;
13775
13776 if (buffer == buf_end)
13777 {
13778 overflow = true;
13779 break;
13780 }
13781 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13782 switch (rlet)
13783 {
13784 case DW_RLE_end_of_list:
13785 break;
13786 case DW_RLE_base_address:
13787 if (buffer + cu->header.addr_size > buf_end)
13788 {
13789 overflow = true;
13790 break;
13791 }
c8a7a66f 13792 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13793 buffer += bytes_read;
13794 break;
13795 case DW_RLE_start_length:
13796 if (buffer + cu->header.addr_size > buf_end)
13797 {
13798 overflow = true;
13799 break;
13800 }
c8a7a66f
TT
13801 range_beginning = cu->header.read_address (obfd, buffer,
13802 &bytes_read);
43988095
JK
13803 buffer += bytes_read;
13804 range_end = (range_beginning
13805 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13806 buffer += bytes_read;
13807 if (buffer > buf_end)
13808 {
13809 overflow = true;
13810 break;
13811 }
13812 break;
13813 case DW_RLE_offset_pair:
13814 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13815 buffer += bytes_read;
13816 if (buffer > buf_end)
13817 {
13818 overflow = true;
13819 break;
13820 }
13821 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13822 buffer += bytes_read;
13823 if (buffer > buf_end)
13824 {
13825 overflow = true;
13826 break;
13827 }
13828 break;
13829 case DW_RLE_start_end:
13830 if (buffer + 2 * cu->header.addr_size > buf_end)
13831 {
13832 overflow = true;
13833 break;
13834 }
c8a7a66f
TT
13835 range_beginning = cu->header.read_address (obfd, buffer,
13836 &bytes_read);
43988095 13837 buffer += bytes_read;
c8a7a66f 13838 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13839 buffer += bytes_read;
13840 break;
13841 default:
b98664d3 13842 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13843 return false;
13844 }
13845 if (rlet == DW_RLE_end_of_list || overflow)
13846 break;
13847 if (rlet == DW_RLE_base_address)
13848 continue;
13849
2b24b6e4 13850 if (!base.has_value ())
43988095
JK
13851 {
13852 /* We have no valid base address for the ranges
13853 data. */
b98664d3 13854 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13855 return false;
13856 }
13857
13858 if (range_beginning > range_end)
13859 {
13860 /* Inverted range entries are invalid. */
b98664d3 13861 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13862 return false;
13863 }
13864
13865 /* Empty range entries have no effect. */
13866 if (range_beginning == range_end)
13867 continue;
13868
2b24b6e4
TT
13869 range_beginning += *base;
13870 range_end += *base;
43988095
JK
13871
13872 /* A not-uncommon case of bad debug info.
13873 Don't pollute the addrmap with bad data. */
13874 if (range_beginning + baseaddr == 0
976ca316 13875 && !per_objfile->per_bfd->has_section_at_zero)
43988095 13876 {
b98664d3 13877 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13878 " [in module %s]"), objfile_name (objfile));
13879 continue;
13880 }
13881
13882 callback (range_beginning, range_end);
13883 }
13884
13885 if (overflow)
13886 {
b98664d3 13887 complaint (_("Offset %d is not terminated "
43988095
JK
13888 "for DW_AT_ranges attribute"),
13889 offset);
13890 return false;
13891 }
13892
13893 return true;
13894}
13895
13896/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13897 Callback's type should be:
13898 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13899 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13900
43988095 13901template <typename Callback>
43039443 13902static int
5f46c5a5 13903dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13904 Callback &&callback)
43039443 13905{
5e22e966
SM
13906 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13907 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13908 struct comp_unit_head *cu_header = &cu->header;
13909 bfd *obfd = objfile->obfd;
13910 unsigned int addr_size = cu_header->addr_size;
13911 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13912 /* Base address selection entry. */
2b24b6e4 13913 gdb::optional<CORE_ADDR> base;
43039443 13914 unsigned int dummy;
d521ce57 13915 const gdb_byte *buffer;
ff013f42 13916 CORE_ADDR baseaddr;
43039443 13917
43988095
JK
13918 if (cu_header->version >= 5)
13919 return dwarf2_rnglists_process (offset, cu, callback);
13920
d00adf39 13921 base = cu->base_address;
43039443 13922
5e22e966
SM
13923 per_objfile->per_bfd->ranges.read (objfile);
13924 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13925 {
b98664d3 13926 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13927 offset);
13928 return 0;
13929 }
5e22e966 13930 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13931
b3b3bada 13932 baseaddr = objfile->text_section_offset ();
ff013f42 13933
43039443
JK
13934 while (1)
13935 {
13936 CORE_ADDR range_beginning, range_end;
13937
c8a7a66f 13938 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13939 buffer += addr_size;
c8a7a66f 13940 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13941 buffer += addr_size;
13942 offset += 2 * addr_size;
13943
13944 /* An end of list marker is a pair of zero addresses. */
13945 if (range_beginning == 0 && range_end == 0)
13946 /* Found the end of list entry. */
13947 break;
13948
13949 /* Each base address selection entry is a pair of 2 values.
13950 The first is the largest possible address, the second is
13951 the base address. Check for a base address here. */
13952 if ((range_beginning & mask) == mask)
13953 {
28d2bfb9
AB
13954 /* If we found the largest possible address, then we already
13955 have the base address in range_end. */
13956 base = range_end;
43039443
JK
13957 continue;
13958 }
13959
2b24b6e4 13960 if (!base.has_value ())
43039443
JK
13961 {
13962 /* We have no valid base address for the ranges
13963 data. */
b98664d3 13964 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13965 return 0;
13966 }
13967
9277c30c
UW
13968 if (range_beginning > range_end)
13969 {
13970 /* Inverted range entries are invalid. */
b98664d3 13971 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13972 return 0;
13973 }
13974
13975 /* Empty range entries have no effect. */
13976 if (range_beginning == range_end)
13977 continue;
13978
2b24b6e4
TT
13979 range_beginning += *base;
13980 range_end += *base;
43039443 13981
01093045
DE
13982 /* A not-uncommon case of bad debug info.
13983 Don't pollute the addrmap with bad data. */
13984 if (range_beginning + baseaddr == 0
5e22e966 13985 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13986 {
b98664d3 13987 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13988 " [in module %s]"), objfile_name (objfile));
01093045
DE
13989 continue;
13990 }
13991
5f46c5a5
JK
13992 callback (range_beginning, range_end);
13993 }
13994
13995 return 1;
13996}
13997
13998/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13999 Return 1 if the attributes are present and valid, otherwise, return 0.
14000 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14001
14002static int
14003dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14004 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14005 dwarf2_psymtab *ranges_pst)
5f46c5a5 14006{
5e22e966 14007 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14008 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14009 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14010 int low_set = 0;
14011 CORE_ADDR low = 0;
14012 CORE_ADDR high = 0;
14013 int retval;
14014
14015 retval = dwarf2_ranges_process (offset, cu,
14016 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14017 {
9277c30c 14018 if (ranges_pst != NULL)
3e29f34a
MR
14019 {
14020 CORE_ADDR lowpc;
14021 CORE_ADDR highpc;
14022
79748972
TT
14023 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14024 range_beginning + baseaddr)
14025 - baseaddr);
14026 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14027 range_end + baseaddr)
14028 - baseaddr);
d320c2b5
TT
14029 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14030 lowpc, highpc - 1, ranges_pst);
3e29f34a 14031 }
ff013f42 14032
43039443
JK
14033 /* FIXME: This is recording everything as a low-high
14034 segment of consecutive addresses. We should have a
14035 data structure for discontiguous block ranges
14036 instead. */
14037 if (! low_set)
14038 {
14039 low = range_beginning;
14040 high = range_end;
14041 low_set = 1;
14042 }
14043 else
14044 {
14045 if (range_beginning < low)
14046 low = range_beginning;
14047 if (range_end > high)
14048 high = range_end;
14049 }
5f46c5a5
JK
14050 });
14051 if (!retval)
14052 return 0;
43039443
JK
14053
14054 if (! low_set)
14055 /* If the first entry is an end-of-list marker, the range
14056 describes an empty scope, i.e. no instructions. */
14057 return 0;
14058
14059 if (low_return)
14060 *low_return = low;
14061 if (high_return)
14062 *high_return = high;
14063 return 1;
14064}
14065
3a2b436a
JK
14066/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14067 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14068 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14069
3a2b436a 14070static enum pc_bounds_kind
af34e669 14071dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14072 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14073 dwarf2_psymtab *pst)
c906108c 14074{
976ca316 14075 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14076 struct attribute *attr;
91da1414 14077 struct attribute *attr_high;
af34e669
DJ
14078 CORE_ADDR low = 0;
14079 CORE_ADDR high = 0;
e385593e 14080 enum pc_bounds_kind ret;
c906108c 14081
91da1414
MW
14082 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14083 if (attr_high)
af34e669 14084 {
e142c38c 14085 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14086 if (attr != nullptr)
91da1414 14087 {
cd6c91b4
TT
14088 low = attr->value_as_address ();
14089 high = attr_high->value_as_address ();
14090 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14091 high += low;
91da1414 14092 }
af34e669
DJ
14093 else
14094 /* Found high w/o low attribute. */
e385593e 14095 return PC_BOUNDS_INVALID;
af34e669
DJ
14096
14097 /* Found consecutive range of addresses. */
3a2b436a 14098 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14099 }
c906108c 14100 else
af34e669 14101 {
e142c38c 14102 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14103 if (attr != NULL)
14104 {
18a8505e 14105 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14106 We take advantage of the fact that DW_AT_ranges does not appear
14107 in DW_TAG_compile_unit of DWO files. */
14108 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14109 unsigned int ranges_offset = (DW_UNSND (attr)
14110 + (need_ranges_base
14111 ? cu->ranges_base
14112 : 0));
2e3cf129 14113
af34e669 14114 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14115 .debug_ranges section. */
2e3cf129 14116 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14117 return PC_BOUNDS_INVALID;
43039443 14118 /* Found discontinuous range of addresses. */
3a2b436a 14119 ret = PC_BOUNDS_RANGES;
af34e669 14120 }
e385593e
JK
14121 else
14122 return PC_BOUNDS_NOT_PRESENT;
af34e669 14123 }
c906108c 14124
48fbe735 14125 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14126 if (high <= low)
e385593e 14127 return PC_BOUNDS_INVALID;
c906108c
SS
14128
14129 /* When using the GNU linker, .gnu.linkonce. sections are used to
14130 eliminate duplicate copies of functions and vtables and such.
14131 The linker will arbitrarily choose one and discard the others.
14132 The AT_*_pc values for such functions refer to local labels in
14133 these sections. If the section from that file was discarded, the
14134 labels are not in the output, so the relocs get a value of 0.
14135 If this is a discarded function, mark the pc bounds as invalid,
14136 so that GDB will ignore it. */
976ca316 14137 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14138 return PC_BOUNDS_INVALID;
c906108c
SS
14139
14140 *lowpc = low;
96408a79
SA
14141 if (highpc)
14142 *highpc = high;
af34e669 14143 return ret;
c906108c
SS
14144}
14145
b084d499
JB
14146/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14147 its low and high PC addresses. Do nothing if these addresses could not
14148 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14149 and HIGHPC to the high address if greater than HIGHPC. */
14150
14151static void
14152dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14153 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14154 struct dwarf2_cu *cu)
14155{
14156 CORE_ADDR low, high;
14157 struct die_info *child = die->child;
14158
e385593e 14159 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14160 {
325fac50
PA
14161 *lowpc = std::min (*lowpc, low);
14162 *highpc = std::max (*highpc, high);
b084d499
JB
14163 }
14164
14165 /* If the language does not allow nested subprograms (either inside
14166 subprograms or lexical blocks), we're done. */
14167 if (cu->language != language_ada)
14168 return;
6e70227d 14169
b084d499
JB
14170 /* Check all the children of the given DIE. If it contains nested
14171 subprograms, then check their pc bounds. Likewise, we need to
14172 check lexical blocks as well, as they may also contain subprogram
14173 definitions. */
14174 while (child && child->tag)
14175 {
14176 if (child->tag == DW_TAG_subprogram
14177 || child->tag == DW_TAG_lexical_block)
14178 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14179 child = child->sibling;
b084d499
JB
14180 }
14181}
14182
fae299cd
DC
14183/* Get the low and high pc's represented by the scope DIE, and store
14184 them in *LOWPC and *HIGHPC. If the correct values can't be
14185 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14186
14187static void
14188get_scope_pc_bounds (struct die_info *die,
14189 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14190 struct dwarf2_cu *cu)
14191{
14192 CORE_ADDR best_low = (CORE_ADDR) -1;
14193 CORE_ADDR best_high = (CORE_ADDR) 0;
14194 CORE_ADDR current_low, current_high;
14195
3a2b436a 14196 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14197 >= PC_BOUNDS_RANGES)
fae299cd
DC
14198 {
14199 best_low = current_low;
14200 best_high = current_high;
14201 }
14202 else
14203 {
14204 struct die_info *child = die->child;
14205
14206 while (child && child->tag)
14207 {
14208 switch (child->tag) {
14209 case DW_TAG_subprogram:
b084d499 14210 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14211 break;
14212 case DW_TAG_namespace:
f55ee35c 14213 case DW_TAG_module:
fae299cd
DC
14214 /* FIXME: carlton/2004-01-16: Should we do this for
14215 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14216 that current GCC's always emit the DIEs corresponding
14217 to definitions of methods of classes as children of a
14218 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14219 the DIEs giving the declarations, which could be
14220 anywhere). But I don't see any reason why the
14221 standards says that they have to be there. */
14222 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14223
14224 if (current_low != ((CORE_ADDR) -1))
14225 {
325fac50
PA
14226 best_low = std::min (best_low, current_low);
14227 best_high = std::max (best_high, current_high);
fae299cd
DC
14228 }
14229 break;
14230 default:
0963b4bd 14231 /* Ignore. */
fae299cd
DC
14232 break;
14233 }
14234
436c571c 14235 child = child->sibling;
fae299cd
DC
14236 }
14237 }
14238
14239 *lowpc = best_low;
14240 *highpc = best_high;
14241}
14242
801e3a5b
JB
14243/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14244 in DIE. */
380bca97 14245
801e3a5b
JB
14246static void
14247dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14248 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14249{
5e22e966 14250 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14251 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14252 struct attribute *attr;
91da1414 14253 struct attribute *attr_high;
801e3a5b 14254
91da1414
MW
14255 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14256 if (attr_high)
801e3a5b 14257 {
801e3a5b 14258 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14259 if (attr != nullptr)
801e3a5b 14260 {
cd6c91b4
TT
14261 CORE_ADDR low = attr->value_as_address ();
14262 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14263
cd6c91b4 14264 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14265 high += low;
9a619af0 14266
3e29f34a
MR
14267 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14268 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14269 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14270 }
14271 }
14272
14273 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14274 if (attr != nullptr)
801e3a5b 14275 {
18a8505e 14276 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14277 We take advantage of the fact that DW_AT_ranges does not appear
14278 in DW_TAG_compile_unit of DWO files. */
14279 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14280
14281 /* The value of the DW_AT_ranges attribute is the offset of the
14282 address range list in the .debug_ranges section. */
ab435259
DE
14283 unsigned long offset = (DW_UNSND (attr)
14284 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14285
2d5f09ec 14286 std::vector<blockrange> blockvec;
5f46c5a5
JK
14287 dwarf2_ranges_process (offset, cu,
14288 [&] (CORE_ADDR start, CORE_ADDR end)
14289 {
58fdfd2c
JK
14290 start += baseaddr;
14291 end += baseaddr;
5f46c5a5
JK
14292 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14293 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14294 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14295 blockvec.emplace_back (start, end);
5f46c5a5 14296 });
2d5f09ec
KB
14297
14298 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14299 }
14300}
14301
685b1105
JK
14302/* Check whether the producer field indicates either of GCC < 4.6, or the
14303 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14304
685b1105
JK
14305static void
14306check_producer (struct dwarf2_cu *cu)
60d5a603 14307{
38360086 14308 int major, minor;
60d5a603
JK
14309
14310 if (cu->producer == NULL)
14311 {
14312 /* For unknown compilers expect their behavior is DWARF version
14313 compliant.
14314
14315 GCC started to support .debug_types sections by -gdwarf-4 since
14316 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14317 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14318 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14319 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14320 }
b1ffba5a 14321 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14322 {
38360086
MW
14323 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14324 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14325 }
5230b05a 14326 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14327 {
14328 cu->producer_is_icc = true;
14329 cu->producer_is_icc_lt_14 = major < 14;
14330 }
c258c396
JD
14331 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14332 cu->producer_is_codewarrior = true;
685b1105
JK
14333 else
14334 {
14335 /* For other non-GCC compilers, expect their behavior is DWARF version
14336 compliant. */
60d5a603
JK
14337 }
14338
9068261f 14339 cu->checked_producer = true;
685b1105 14340}
ba919b58 14341
685b1105
JK
14342/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14343 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14344 during 4.6.0 experimental. */
14345
9068261f 14346static bool
685b1105
JK
14347producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14348{
14349 if (!cu->checked_producer)
14350 check_producer (cu);
14351
14352 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14353}
14354
c258c396
JD
14355
14356/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14357 with incorrect is_stmt attributes. */
14358
14359static bool
14360producer_is_codewarrior (struct dwarf2_cu *cu)
14361{
14362 if (!cu->checked_producer)
14363 check_producer (cu);
14364
14365 return cu->producer_is_codewarrior;
14366}
14367
405feb71 14368/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14369 DW_AT_accessibility. */
14370
14371static enum dwarf_access_attribute
14372dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14373{
14374 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14375 {
14376 /* The default DWARF 2 accessibility for members is public, the default
14377 accessibility for inheritance is private. */
14378
14379 if (die->tag != DW_TAG_inheritance)
14380 return DW_ACCESS_public;
14381 else
14382 return DW_ACCESS_private;
14383 }
14384 else
14385 {
14386 /* DWARF 3+ defines the default accessibility a different way. The same
14387 rules apply now for DW_TAG_inheritance as for the members and it only
14388 depends on the container kind. */
14389
14390 if (die->parent->tag == DW_TAG_class_type)
14391 return DW_ACCESS_private;
14392 else
14393 return DW_ACCESS_public;
14394 }
14395}
14396
74ac6d43
TT
14397/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14398 offset. If the attribute was not found return 0, otherwise return
14399 1. If it was found but could not properly be handled, set *OFFSET
14400 to 0. */
14401
14402static int
14403handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14404 LONGEST *offset)
14405{
14406 struct attribute *attr;
14407
14408 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14409 if (attr != NULL)
14410 {
14411 *offset = 0;
14412
14413 /* Note that we do not check for a section offset first here.
14414 This is because DW_AT_data_member_location is new in DWARF 4,
14415 so if we see it, we can assume that a constant form is really
14416 a constant and not a section offset. */
cd6c91b4 14417 if (attr->form_is_constant ())
0826b30a 14418 *offset = attr->constant_value (0);
cd6c91b4 14419 else if (attr->form_is_section_offset ())
74ac6d43 14420 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14421 else if (attr->form_is_block ())
74ac6d43
TT
14422 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14423 else
14424 dwarf2_complex_location_expr_complaint ();
14425
14426 return 1;
14427 }
14428
14429 return 0;
14430}
14431
7d79de9a
TT
14432/* Look for DW_AT_data_member_location and store the results in FIELD. */
14433
14434static void
14435handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14436 struct field *field)
14437{
14438 struct attribute *attr;
14439
14440 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14441 if (attr != NULL)
14442 {
14443 if (attr->form_is_constant ())
14444 {
14445 LONGEST offset = attr->constant_value (0);
14446 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14447 }
14448 else if (attr->form_is_section_offset ())
14449 dwarf2_complex_location_expr_complaint ();
14450 else if (attr->form_is_block ())
14451 {
14452 bool handled;
14453 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14454 if (handled)
14455 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14456 else
14457 {
5e22e966
SM
14458 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14459 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14460 struct dwarf2_locexpr_baton *dlbaton
14461 = XOBNEW (&objfile->objfile_obstack,
14462 struct dwarf2_locexpr_baton);
14463 dlbaton->data = DW_BLOCK (attr)->data;
14464 dlbaton->size = DW_BLOCK (attr)->size;
14465 /* When using this baton, we want to compute the address
14466 of the field, not the value. This is why
14467 is_reference is set to false here. */
14468 dlbaton->is_reference = false;
5e22e966 14469 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14470 dlbaton->per_cu = cu->per_cu;
14471
14472 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14473 }
14474 }
14475 else
14476 dwarf2_complex_location_expr_complaint ();
14477 }
14478}
14479
c906108c
SS
14480/* Add an aggregate field to the field list. */
14481
14482static void
107d2387 14483dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14484 struct dwarf2_cu *cu)
6e70227d 14485{
5e22e966 14486 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14487 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14488 struct nextfield *new_field;
14489 struct attribute *attr;
14490 struct field *fp;
15d034d0 14491 const char *fieldname = "";
c906108c 14492
7d0ccb61
DJ
14493 if (die->tag == DW_TAG_inheritance)
14494 {
be2daae6
TT
14495 fip->baseclasses.emplace_back ();
14496 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14497 }
14498 else
14499 {
be2daae6
TT
14500 fip->fields.emplace_back ();
14501 new_field = &fip->fields.back ();
7d0ccb61 14502 }
be2daae6 14503
9c6a1327
TT
14504 new_field->offset = die->sect_off;
14505
e142c38c 14506 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14507 if (attr != nullptr)
c906108c 14508 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14509 else
14510 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14511 if (new_field->accessibility != DW_ACCESS_public)
14512 fip->non_public_fields = 1;
60d5a603 14513
e142c38c 14514 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14515 if (attr != nullptr)
c906108c 14516 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14517 else
14518 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14519
14520 fp = &new_field->field;
a9a9bd0f 14521
e142c38c 14522 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14523 {
a9a9bd0f 14524 /* Data member other than a C++ static data member. */
6e70227d 14525
c906108c 14526 /* Get type of field. */
e7c27a73 14527 fp->type = die_type (die, cu);
c906108c 14528
d6a843b5 14529 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14530
c906108c 14531 /* Get bit size of field (zero if none). */
e142c38c 14532 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14533 if (attr != nullptr)
c906108c
SS
14534 {
14535 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14536 }
14537 else
14538 {
14539 FIELD_BITSIZE (*fp) = 0;
14540 }
14541
14542 /* Get bit offset of field. */
7d79de9a 14543 handle_data_member_location (die, cu, fp);
e142c38c 14544 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14545 if (attr != nullptr)
c906108c 14546 {
d5a22e77 14547 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14548 {
14549 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14550 additional bit offset from the MSB of the containing
14551 anonymous object to the MSB of the field. We don't
14552 have to do anything special since we don't need to
14553 know the size of the anonymous object. */
f41f5e61 14554 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14555 }
14556 else
14557 {
14558 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14559 MSB of the anonymous object, subtract off the number of
14560 bits from the MSB of the field to the MSB of the
14561 object, and then subtract off the number of bits of
14562 the field itself. The result is the bit offset of
14563 the LSB of the field. */
c906108c
SS
14564 int anonymous_size;
14565 int bit_offset = DW_UNSND (attr);
14566
e142c38c 14567 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14568 if (attr != nullptr)
c906108c
SS
14569 {
14570 /* The size of the anonymous object containing
14571 the bit field is explicit, so use the
14572 indicated size (in bytes). */
14573 anonymous_size = DW_UNSND (attr);
14574 }
14575 else
14576 {
14577 /* The size of the anonymous object containing
14578 the bit field must be inferred from the type
14579 attribute of the data member containing the
14580 bit field. */
14581 anonymous_size = TYPE_LENGTH (fp->type);
14582 }
f41f5e61
PA
14583 SET_FIELD_BITPOS (*fp,
14584 (FIELD_BITPOS (*fp)
14585 + anonymous_size * bits_per_byte
14586 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14587 }
14588 }
da5b30da
AA
14589 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14590 if (attr != NULL)
14591 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14592 + attr->constant_value (0)));
c906108c
SS
14593
14594 /* Get name of field. */
39cbfefa
DJ
14595 fieldname = dwarf2_name (die, cu);
14596 if (fieldname == NULL)
14597 fieldname = "";
d8151005
DJ
14598
14599 /* The name is already allocated along with this objfile, so we don't
14600 need to duplicate it for the type. */
14601 fp->name = fieldname;
c906108c
SS
14602
14603 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14604 pointer or virtual base class pointer) to private. */
e142c38c 14605 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14606 {
d48cc9dd 14607 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14608 new_field->accessibility = DW_ACCESS_private;
14609 fip->non_public_fields = 1;
14610 }
14611 }
a9a9bd0f 14612 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14613 {
a9a9bd0f
DC
14614 /* C++ static member. */
14615
14616 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14617 is a declaration, but all versions of G++ as of this writing
14618 (so through at least 3.2.1) incorrectly generate
14619 DW_TAG_variable tags. */
6e70227d 14620
ff355380 14621 const char *physname;
c906108c 14622
a9a9bd0f 14623 /* Get name of field. */
39cbfefa
DJ
14624 fieldname = dwarf2_name (die, cu);
14625 if (fieldname == NULL)
c906108c
SS
14626 return;
14627
254e6b9e 14628 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14629 if (attr
14630 /* Only create a symbol if this is an external value.
14631 new_symbol checks this and puts the value in the global symbol
14632 table, which we want. If it is not external, new_symbol
14633 will try to put the value in cu->list_in_scope which is wrong. */
14634 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14635 {
14636 /* A static const member, not much different than an enum as far as
14637 we're concerned, except that we can support more types. */
14638 new_symbol (die, NULL, cu);
14639 }
14640
2df3850c 14641 /* Get physical name. */
ff355380 14642 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14643
d8151005
DJ
14644 /* The name is already allocated along with this objfile, so we don't
14645 need to duplicate it for the type. */
14646 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14647 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14648 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14649 }
14650 else if (die->tag == DW_TAG_inheritance)
14651 {
74ac6d43 14652 /* C++ base class field. */
7d79de9a 14653 handle_data_member_location (die, cu, fp);
c906108c 14654 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14655 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14656 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14657 }
2ddeaf8a
TT
14658 else
14659 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14660}
14661
883fd55a
KS
14662/* Can the type given by DIE define another type? */
14663
14664static bool
14665type_can_define_types (const struct die_info *die)
14666{
14667 switch (die->tag)
14668 {
14669 case DW_TAG_typedef:
14670 case DW_TAG_class_type:
14671 case DW_TAG_structure_type:
14672 case DW_TAG_union_type:
14673 case DW_TAG_enumeration_type:
14674 return true;
14675
14676 default:
14677 return false;
14678 }
14679}
14680
14681/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14682
14683static void
883fd55a
KS
14684dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14685 struct dwarf2_cu *cu)
6e70227d 14686{
be2daae6
TT
14687 struct decl_field fp;
14688 memset (&fp, 0, sizeof (fp));
98751a41 14689
883fd55a 14690 gdb_assert (type_can_define_types (die));
98751a41 14691
883fd55a 14692 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14693 fp.name = dwarf2_name (die, cu);
14694 fp.type = read_type_die (die, cu);
98751a41 14695
c191a687
KS
14696 /* Save accessibility. */
14697 enum dwarf_access_attribute accessibility;
14698 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14699 if (attr != NULL)
14700 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14701 else
14702 accessibility = dwarf2_default_access_attribute (die, cu);
14703 switch (accessibility)
14704 {
14705 case DW_ACCESS_public:
14706 /* The assumed value if neither private nor protected. */
14707 break;
14708 case DW_ACCESS_private:
be2daae6 14709 fp.is_private = 1;
c191a687
KS
14710 break;
14711 case DW_ACCESS_protected:
be2daae6 14712 fp.is_protected = 1;
c191a687
KS
14713 break;
14714 default:
b98664d3 14715 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14716 }
14717
883fd55a 14718 if (die->tag == DW_TAG_typedef)
be2daae6 14719 fip->typedef_field_list.push_back (fp);
883fd55a 14720 else
be2daae6 14721 fip->nested_types_list.push_back (fp);
98751a41
JK
14722}
14723
9c6a1327
TT
14724/* A convenience typedef that's used when finding the discriminant
14725 field for a variant part. */
1b95cdb7
SM
14726typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14727 offset_map_type;
9c6a1327
TT
14728
14729/* Compute the discriminant range for a given variant. OBSTACK is
14730 where the results will be stored. VARIANT is the variant to
14731 process. IS_UNSIGNED indicates whether the discriminant is signed
14732 or unsigned. */
14733
14734static const gdb::array_view<discriminant_range>
14735convert_variant_range (struct obstack *obstack, const variant_field &variant,
14736 bool is_unsigned)
14737{
14738 std::vector<discriminant_range> ranges;
14739
14740 if (variant.default_branch)
14741 return {};
14742
14743 if (variant.discr_list_data == nullptr)
14744 {
14745 discriminant_range r
14746 = {variant.discriminant_value, variant.discriminant_value};
14747 ranges.push_back (r);
14748 }
14749 else
14750 {
14751 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14752 variant.discr_list_data->size);
14753 while (!data.empty ())
14754 {
14755 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14756 {
14757 complaint (_("invalid discriminant marker: %d"), data[0]);
14758 break;
14759 }
14760 bool is_range = data[0] == DW_DSC_range;
14761 data = data.slice (1);
14762
14763 ULONGEST low, high;
14764 unsigned int bytes_read;
14765
14766 if (data.empty ())
14767 {
14768 complaint (_("DW_AT_discr_list missing low value"));
14769 break;
14770 }
14771 if (is_unsigned)
14772 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14773 else
14774 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14775 &bytes_read);
14776 data = data.slice (bytes_read);
14777
14778 if (is_range)
14779 {
14780 if (data.empty ())
14781 {
14782 complaint (_("DW_AT_discr_list missing high value"));
14783 break;
14784 }
14785 if (is_unsigned)
14786 high = read_unsigned_leb128 (nullptr, data.data (),
14787 &bytes_read);
14788 else
14789 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14790 &bytes_read);
14791 data = data.slice (bytes_read);
14792 }
14793 else
14794 high = low;
14795
14796 ranges.push_back ({ low, high });
14797 }
14798 }
14799
14800 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14801 ranges.size ());
14802 std::copy (ranges.begin (), ranges.end (), result);
14803 return gdb::array_view<discriminant_range> (result, ranges.size ());
14804}
14805
14806static const gdb::array_view<variant_part> create_variant_parts
14807 (struct obstack *obstack,
14808 const offset_map_type &offset_map,
14809 struct field_info *fi,
14810 const std::vector<variant_part_builder> &variant_parts);
14811
14812/* Fill in a "struct variant" for a given variant field. RESULT is
14813 the variant to fill in. OBSTACK is where any needed allocations
14814 will be done. OFFSET_MAP holds the mapping from section offsets to
14815 fields for the type. FI describes the fields of the type we're
14816 processing. FIELD is the variant field we're converting. */
14817
14818static void
14819create_one_variant (variant &result, struct obstack *obstack,
14820 const offset_map_type &offset_map,
14821 struct field_info *fi, const variant_field &field)
14822{
14823 result.discriminants = convert_variant_range (obstack, field, false);
14824 result.first_field = field.first_field + fi->baseclasses.size ();
14825 result.last_field = field.last_field + fi->baseclasses.size ();
14826 result.parts = create_variant_parts (obstack, offset_map, fi,
14827 field.variant_parts);
14828}
14829
14830/* Fill in a "struct variant_part" for a given variant part. RESULT
14831 is the variant part to fill in. OBSTACK is where any needed
14832 allocations will be done. OFFSET_MAP holds the mapping from
14833 section offsets to fields for the type. FI describes the fields of
14834 the type we're processing. BUILDER is the variant part to be
14835 converted. */
14836
14837static void
14838create_one_variant_part (variant_part &result,
14839 struct obstack *obstack,
14840 const offset_map_type &offset_map,
14841 struct field_info *fi,
14842 const variant_part_builder &builder)
14843{
14844 auto iter = offset_map.find (builder.discriminant_offset);
14845 if (iter == offset_map.end ())
14846 {
14847 result.discriminant_index = -1;
14848 /* Doesn't matter. */
14849 result.is_unsigned = false;
14850 }
14851 else
14852 {
14853 result.discriminant_index = iter->second;
14854 result.is_unsigned
14855 = TYPE_UNSIGNED (FIELD_TYPE
14856 (fi->fields[result.discriminant_index].field));
14857 }
14858
14859 size_t n = builder.variants.size ();
14860 variant *output = new (obstack) variant[n];
14861 for (size_t i = 0; i < n; ++i)
14862 create_one_variant (output[i], obstack, offset_map, fi,
14863 builder.variants[i]);
14864
14865 result.variants = gdb::array_view<variant> (output, n);
14866}
14867
14868/* Create a vector of variant parts that can be attached to a type.
14869 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14870 holds the mapping from section offsets to fields for the type. FI
14871 describes the fields of the type we're processing. VARIANT_PARTS
14872 is the vector to convert. */
14873
14874static const gdb::array_view<variant_part>
14875create_variant_parts (struct obstack *obstack,
14876 const offset_map_type &offset_map,
14877 struct field_info *fi,
14878 const std::vector<variant_part_builder> &variant_parts)
14879{
14880 if (variant_parts.empty ())
14881 return {};
14882
14883 size_t n = variant_parts.size ();
14884 variant_part *result = new (obstack) variant_part[n];
14885 for (size_t i = 0; i < n; ++i)
14886 create_one_variant_part (result[i], obstack, offset_map, fi,
14887 variant_parts[i]);
14888
14889 return gdb::array_view<variant_part> (result, n);
14890}
14891
14892/* Compute the variant part vector for FIP, attaching it to TYPE when
14893 done. */
14894
14895static void
14896add_variant_property (struct field_info *fip, struct type *type,
14897 struct dwarf2_cu *cu)
14898{
14899 /* Map section offsets of fields to their field index. Note the
14900 field index here does not take the number of baseclasses into
14901 account. */
14902 offset_map_type offset_map;
14903 for (int i = 0; i < fip->fields.size (); ++i)
14904 offset_map[fip->fields[i].offset] = i;
14905
5e22e966 14906 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14907 gdb::array_view<variant_part> parts
14908 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14909 fip->variant_parts);
14910
14911 struct dynamic_prop prop;
14912 prop.kind = PROP_VARIANT_PARTS;
14913 prop.data.variant_parts
14914 = ((gdb::array_view<variant_part> *)
14915 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14916
5c54719c 14917 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14918}
14919
c906108c
SS
14920/* Create the vector of fields, and attach it to the type. */
14921
14922static void
fba45db2 14923dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14924 struct dwarf2_cu *cu)
c906108c 14925{
317f7127 14926 int nfields = fip->nfields ();
c906108c
SS
14927
14928 /* Record the field count, allocate space for the array of fields,
14929 and create blank accessibility bitfields if necessary. */
5e33d5f4 14930 type->set_num_fields (nfields);
3cabb6b0
SM
14931 type->set_fields
14932 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14933
b4ba55a1 14934 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14935 {
14936 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14937
14938 TYPE_FIELD_PRIVATE_BITS (type) =
14939 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14940 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14941
14942 TYPE_FIELD_PROTECTED_BITS (type) =
14943 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14944 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14945
774b6a14
TT
14946 TYPE_FIELD_IGNORE_BITS (type) =
14947 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14948 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14949 }
14950
14951 /* If the type has baseclasses, allocate and clear a bit vector for
14952 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14953 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14954 {
be2daae6 14955 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14956 unsigned char *pointer;
c906108c
SS
14957
14958 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14959 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14960 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14961 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14962 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14963 }
14964
9c6a1327
TT
14965 if (!fip->variant_parts.empty ())
14966 add_variant_property (fip, type, cu);
2ddeaf8a 14967
be2daae6
TT
14968 /* Copy the saved-up fields into the field vector. */
14969 for (int i = 0; i < nfields; ++i)
c906108c 14970 {
be2daae6
TT
14971 struct nextfield &field
14972 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14973 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14974
ceacbf6e 14975 type->field (i) = field.field;
be2daae6 14976 switch (field.accessibility)
c906108c 14977 {
c5aa993b 14978 case DW_ACCESS_private:
b4ba55a1 14979 if (cu->language != language_ada)
be2daae6 14980 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14981 break;
c906108c 14982
c5aa993b 14983 case DW_ACCESS_protected:
b4ba55a1 14984 if (cu->language != language_ada)
be2daae6 14985 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14986 break;
c906108c 14987
c5aa993b
JM
14988 case DW_ACCESS_public:
14989 break;
c906108c 14990
c5aa993b
JM
14991 default:
14992 /* Unknown accessibility. Complain and treat it as public. */
14993 {
b98664d3 14994 complaint (_("unsupported accessibility %d"),
be2daae6 14995 field.accessibility);
c5aa993b
JM
14996 }
14997 break;
c906108c 14998 }
be2daae6 14999 if (i < fip->baseclasses.size ())
c906108c 15000 {
be2daae6 15001 switch (field.virtuality)
c906108c 15002 {
c5aa993b
JM
15003 case DW_VIRTUALITY_virtual:
15004 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15005 if (cu->language == language_ada)
a73c6dcd 15006 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15007 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15008 break;
c906108c
SS
15009 }
15010 }
c906108c
SS
15011 }
15012}
15013
7d27a96d
TT
15014/* Return true if this member function is a constructor, false
15015 otherwise. */
15016
15017static int
15018dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15019{
15020 const char *fieldname;
fe978cb0 15021 const char *type_name;
7d27a96d
TT
15022 int len;
15023
15024 if (die->parent == NULL)
15025 return 0;
15026
15027 if (die->parent->tag != DW_TAG_structure_type
15028 && die->parent->tag != DW_TAG_union_type
15029 && die->parent->tag != DW_TAG_class_type)
15030 return 0;
15031
15032 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15033 type_name = dwarf2_name (die->parent, cu);
15034 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15035 return 0;
15036
15037 len = strlen (fieldname);
fe978cb0
PA
15038 return (strncmp (fieldname, type_name, len) == 0
15039 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15040}
15041
e35000a7
TBA
15042/* Check if the given VALUE is a recognized enum
15043 dwarf_defaulted_attribute constant according to DWARF5 spec,
15044 Table 7.24. */
15045
15046static bool
15047is_valid_DW_AT_defaulted (ULONGEST value)
15048{
15049 switch (value)
15050 {
15051 case DW_DEFAULTED_no:
15052 case DW_DEFAULTED_in_class:
15053 case DW_DEFAULTED_out_of_class:
15054 return true;
15055 }
15056
3142e908 15057 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15058 return false;
15059}
15060
c906108c
SS
15061/* Add a member function to the proper fieldlist. */
15062
15063static void
107d2387 15064dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15065 struct type *type, struct dwarf2_cu *cu)
c906108c 15066{
5e22e966 15067 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15068 struct attribute *attr;
c906108c 15069 int i;
be2daae6 15070 struct fnfieldlist *flp = nullptr;
c906108c 15071 struct fn_field *fnp;
15d034d0 15072 const char *fieldname;
f792889a 15073 struct type *this_type;
60d5a603 15074 enum dwarf_access_attribute accessibility;
c906108c 15075
b4ba55a1 15076 if (cu->language == language_ada)
a73c6dcd 15077 error (_("unexpected member function in Ada type"));
b4ba55a1 15078
2df3850c 15079 /* Get name of member function. */
39cbfefa
DJ
15080 fieldname = dwarf2_name (die, cu);
15081 if (fieldname == NULL)
2df3850c 15082 return;
c906108c 15083
c906108c 15084 /* Look up member function name in fieldlist. */
be2daae6 15085 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15086 {
27bfe10e 15087 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15088 {
15089 flp = &fip->fnfieldlists[i];
15090 break;
15091 }
c906108c
SS
15092 }
15093
be2daae6
TT
15094 /* Create a new fnfieldlist if necessary. */
15095 if (flp == nullptr)
c906108c 15096 {
be2daae6
TT
15097 fip->fnfieldlists.emplace_back ();
15098 flp = &fip->fnfieldlists.back ();
c906108c 15099 flp->name = fieldname;
be2daae6 15100 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15101 }
15102
be2daae6
TT
15103 /* Create a new member function field and add it to the vector of
15104 fnfieldlists. */
15105 flp->fnfields.emplace_back ();
15106 fnp = &flp->fnfields.back ();
3da10d80
KS
15107
15108 /* Delay processing of the physname until later. */
9c37b5ae 15109 if (cu->language == language_cplus)
be2daae6
TT
15110 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15111 die, cu);
3da10d80
KS
15112 else
15113 {
1d06ead6 15114 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15115 fnp->physname = physname ? physname : "";
15116 }
15117
c906108c 15118 fnp->type = alloc_type (objfile);
f792889a 15119 this_type = read_type_die (die, cu);
78134374 15120 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15121 {
1f704f76 15122 int nparams = this_type->num_fields ();
c906108c 15123
f792889a 15124 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15125 of the method itself (TYPE_CODE_METHOD). */
15126 smash_to_method_type (fnp->type, type,
f792889a 15127 TYPE_TARGET_TYPE (this_type),
80fc5e77 15128 this_type->fields (),
1f704f76 15129 this_type->num_fields (),
f792889a 15130 TYPE_VARARGS (this_type));
c906108c
SS
15131
15132 /* Handle static member functions.
c5aa993b 15133 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15134 member functions. G++ helps GDB by marking the first
15135 parameter for non-static member functions (which is the this
15136 pointer) as artificial. We obtain this information from
15137 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15138 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15139 fnp->voffset = VOFFSET_STATIC;
15140 }
15141 else
b98664d3 15142 complaint (_("member function type missing for '%s'"),
3da10d80 15143 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15144
15145 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15146 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15147 fnp->fcontext = die_containing_type (die, cu);
c906108c 15148
3e43a32a
MS
15149 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15150 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15151
15152 /* Get accessibility. */
e142c38c 15153 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15154 if (attr != nullptr)
aead7601 15155 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15156 else
15157 accessibility = dwarf2_default_access_attribute (die, cu);
15158 switch (accessibility)
c906108c 15159 {
60d5a603
JK
15160 case DW_ACCESS_private:
15161 fnp->is_private = 1;
15162 break;
15163 case DW_ACCESS_protected:
15164 fnp->is_protected = 1;
15165 break;
c906108c
SS
15166 }
15167
b02dede2 15168 /* Check for artificial methods. */
e142c38c 15169 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15170 if (attr && DW_UNSND (attr) != 0)
15171 fnp->is_artificial = 1;
15172
e35000a7
TBA
15173 /* Check for defaulted methods. */
15174 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15175 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15176 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15177
15178 /* Check for deleted methods. */
15179 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15180 if (attr != nullptr && DW_UNSND (attr) != 0)
15181 fnp->is_deleted = 1;
15182
7d27a96d
TT
15183 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15184
0d564a31 15185 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15186 function. For older versions of GCC, this is an offset in the
15187 appropriate virtual table, as specified by DW_AT_containing_type.
15188 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15189 to the object address. */
15190
e142c38c 15191 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15192 if (attr != nullptr)
8e19ed76 15193 {
4fc6c0d5 15194 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15195 {
aec5aa8b
TT
15196 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15197 {
15198 /* Old-style GCC. */
15199 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15200 }
15201 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15202 || (DW_BLOCK (attr)->size > 1
15203 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15204 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15205 {
aec5aa8b
TT
15206 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15207 if ((fnp->voffset % cu->header.addr_size) != 0)
15208 dwarf2_complex_location_expr_complaint ();
15209 else
15210 fnp->voffset /= cu->header.addr_size;
15211 fnp->voffset += 2;
15212 }
15213 else
15214 dwarf2_complex_location_expr_complaint ();
15215
15216 if (!fnp->fcontext)
7e993ebf
KS
15217 {
15218 /* If there is no `this' field and no DW_AT_containing_type,
15219 we cannot actually find a base class context for the
15220 vtable! */
1f704f76 15221 if (this_type->num_fields () == 0
7e993ebf
KS
15222 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15223 {
b98664d3 15224 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15225 "function \"%s\" (offset %s)"),
15226 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15227 }
15228 else
15229 {
15230 fnp->fcontext
15231 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15232 }
15233 }
aec5aa8b 15234 }
cd6c91b4 15235 else if (attr->form_is_section_offset ())
8e19ed76 15236 {
4d3c2250 15237 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15238 }
15239 else
15240 {
4d3c2250
KB
15241 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15242 fieldname);
8e19ed76 15243 }
0d564a31 15244 }
d48cc9dd
DJ
15245 else
15246 {
15247 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15248 if (attr && DW_UNSND (attr))
15249 {
15250 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15251 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15252 "but the vtable offset is not specified"),
9d8780f0 15253 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15254 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15255 TYPE_CPLUS_DYNAMIC (type) = 1;
15256 }
15257 }
c906108c
SS
15258}
15259
15260/* Create the vector of member function fields, and attach it to the type. */
15261
15262static void
fba45db2 15263dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15264 struct dwarf2_cu *cu)
c906108c 15265{
b4ba55a1 15266 if (cu->language == language_ada)
a73c6dcd 15267 error (_("unexpected member functions in Ada type"));
b4ba55a1 15268
c906108c
SS
15269 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15270 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15271 TYPE_ALLOC (type,
15272 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15273
be2daae6 15274 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15275 {
be2daae6 15276 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15277 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15278
be2daae6
TT
15279 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15280 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15281 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15282 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15283
15284 for (int k = 0; k < nf.fnfields.size (); ++k)
15285 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15286 }
15287
be2daae6 15288 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15289}
15290
1168df01
JB
15291/* Returns non-zero if NAME is the name of a vtable member in CU's
15292 language, zero otherwise. */
15293static int
15294is_vtable_name (const char *name, struct dwarf2_cu *cu)
15295{
15296 static const char vptr[] = "_vptr";
15297
9c37b5ae
TT
15298 /* Look for the C++ form of the vtable. */
15299 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15300 return 1;
15301
15302 return 0;
15303}
15304
c0dd20ea 15305/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15306 functions, with the ABI-specified layout. If TYPE describes
15307 such a structure, smash it into a member function type.
61049d3b
DJ
15308
15309 GCC shouldn't do this; it should just output pointer to member DIEs.
15310 This is GCC PR debug/28767. */
c0dd20ea 15311
0b92b5bb
TT
15312static void
15313quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15314{
09e2d7c7 15315 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15316
15317 /* Check for a structure with no name and two children. */
1f704f76 15318 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15319 return;
c0dd20ea
DJ
15320
15321 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15322 if (TYPE_FIELD_NAME (type, 0) == NULL
15323 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15324 || TYPE_FIELD_NAME (type, 1) == NULL
15325 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15326 return;
c0dd20ea
DJ
15327
15328 /* Find the type of the method. */
0b92b5bb 15329 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15330 if (pfn_type == NULL
78134374
SM
15331 || pfn_type->code () != TYPE_CODE_PTR
15332 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15333 return;
c0dd20ea
DJ
15334
15335 /* Look for the "this" argument. */
15336 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15337 if (pfn_type->num_fields () == 0
0b92b5bb 15338 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15339 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15340 return;
c0dd20ea 15341
09e2d7c7 15342 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15343 new_type = alloc_type (objfile);
09e2d7c7 15344 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15345 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15346 TYPE_VARARGS (pfn_type));
0b92b5bb 15347 smash_to_methodptr_type (type, new_type);
c0dd20ea 15348}
1168df01 15349
2b4424c3
TT
15350/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15351 appropriate error checking and issuing complaints if there is a
15352 problem. */
15353
15354static ULONGEST
15355get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15356{
15357 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15358
15359 if (attr == nullptr)
15360 return 0;
15361
cd6c91b4 15362 if (!attr->form_is_constant ())
2b4424c3 15363 {
b98664d3 15364 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15365 " - DIE at %s [in module %s]"),
15366 sect_offset_str (die->sect_off),
5e22e966 15367 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15368 return 0;
15369 }
15370
15371 ULONGEST align;
15372 if (attr->form == DW_FORM_sdata)
15373 {
15374 LONGEST val = DW_SND (attr);
15375 if (val < 0)
15376 {
b98664d3 15377 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15378 " - DIE at %s [in module %s]"),
15379 sect_offset_str (die->sect_off),
5e22e966 15380 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15381 return 0;
15382 }
15383 align = val;
15384 }
15385 else
15386 align = DW_UNSND (attr);
15387
15388 if (align == 0)
15389 {
b98664d3 15390 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15391 " - DIE at %s [in module %s]"),
15392 sect_offset_str (die->sect_off),
5e22e966 15393 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15394 return 0;
15395 }
15396 if ((align & (align - 1)) != 0)
15397 {
b98664d3 15398 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15399 " - DIE at %s [in module %s]"),
15400 sect_offset_str (die->sect_off),
5e22e966 15401 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15402 return 0;
15403 }
15404
15405 return align;
15406}
15407
15408/* If the DIE has a DW_AT_alignment attribute, use its value to set
15409 the alignment for TYPE. */
15410
15411static void
15412maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15413 struct type *type)
15414{
15415 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15416 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15417 " - DIE at %s [in module %s]"),
15418 sect_offset_str (die->sect_off),
5e22e966 15419 objfile_name (cu->per_objfile->objfile));
2b4424c3 15420}
685b1105 15421
e35000a7
TBA
15422/* Check if the given VALUE is a valid enum dwarf_calling_convention
15423 constant for a type, according to DWARF5 spec, Table 5.5. */
15424
15425static bool
15426is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15427{
15428 switch (value)
15429 {
15430 case DW_CC_normal:
15431 case DW_CC_pass_by_reference:
15432 case DW_CC_pass_by_value:
15433 return true;
15434
15435 default:
15436 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15437 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15438 return false;
15439 }
15440}
15441
d0922fcf
TBA
15442/* Check if the given VALUE is a valid enum dwarf_calling_convention
15443 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15444 also according to GNU-specific values (see include/dwarf2.h). */
15445
15446static bool
15447is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15448{
15449 switch (value)
15450 {
15451 case DW_CC_normal:
15452 case DW_CC_program:
15453 case DW_CC_nocall:
15454 return true;
15455
15456 case DW_CC_GNU_renesas_sh:
15457 case DW_CC_GNU_borland_fastcall_i386:
15458 case DW_CC_GDB_IBM_OpenCL:
15459 return true;
15460
15461 default:
15462 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15463 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15464 return false;
15465 }
15466}
15467
c906108c 15468/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15469 (definition) to create a type for the structure or union. Fill in
15470 the type's name and general properties; the members will not be
83655187
DE
15471 processed until process_structure_scope. A symbol table entry for
15472 the type will also not be done until process_structure_scope (assuming
15473 the type has a name).
c906108c 15474
c767944b
DJ
15475 NOTE: we need to call these functions regardless of whether or not the
15476 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15477 structure or union. This gets the type entered into our set of
83655187 15478 user defined types. */
c906108c 15479
f792889a 15480static struct type *
134d01f1 15481read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15482{
5e22e966 15483 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15484 struct type *type;
15485 struct attribute *attr;
15d034d0 15486 const char *name;
c906108c 15487
348e048f
DE
15488 /* If the definition of this type lives in .debug_types, read that type.
15489 Don't follow DW_AT_specification though, that will take us back up
15490 the chain and we want to go down. */
052c8bb8 15491 attr = die->attr (DW_AT_signature);
435d3d88 15492 if (attr != nullptr)
348e048f 15493 {
ac9ec31b 15494 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15495
ac9ec31b 15496 /* The type's CU may not be the same as CU.
02142a6c 15497 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15498 return set_die_type (die, type, cu);
15499 }
15500
c0dd20ea 15501 type = alloc_type (objfile);
c906108c 15502 INIT_CPLUS_SPECIFIC (type);
93311388 15503
39cbfefa
DJ
15504 name = dwarf2_name (die, cu);
15505 if (name != NULL)
c906108c 15506 {
987504bb 15507 if (cu->language == language_cplus
c44af4eb
TT
15508 || cu->language == language_d
15509 || cu->language == language_rust)
63d06c5c 15510 {
15d034d0 15511 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15512
15513 /* dwarf2_full_name might have already finished building the DIE's
15514 type. If so, there is no need to continue. */
15515 if (get_die_type (die, cu) != NULL)
15516 return get_die_type (die, cu);
15517
d0e39ea2 15518 type->set_name (full_name);
63d06c5c
DC
15519 }
15520 else
15521 {
d8151005
DJ
15522 /* The name is already allocated along with this objfile, so
15523 we don't need to duplicate it for the type. */
d0e39ea2 15524 type->set_name (name);
63d06c5c 15525 }
c906108c
SS
15526 }
15527
15528 if (die->tag == DW_TAG_structure_type)
15529 {
67607e24 15530 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15531 }
15532 else if (die->tag == DW_TAG_union_type)
15533 {
67607e24 15534 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15535 }
15536 else
15537 {
67607e24 15538 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15539 }
15540
0cc2414c
TT
15541 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15542 TYPE_DECLARED_CLASS (type) = 1;
15543
e35000a7
TBA
15544 /* Store the calling convention in the type if it's available in
15545 the die. Otherwise the calling convention remains set to
15546 the default value DW_CC_normal. */
15547 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15548 if (attr != nullptr
15549 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15550 {
15551 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15552 TYPE_CPLUS_CALLING_CONVENTION (type)
15553 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15554 }
15555
e142c38c 15556 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15557 if (attr != nullptr)
c906108c 15558 {
cd6c91b4 15559 if (attr->form_is_constant ())
155bfbd3
JB
15560 TYPE_LENGTH (type) = DW_UNSND (attr);
15561 else
15562 {
f8e89861 15563 struct dynamic_prop prop;
293e7e51 15564 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15565 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15566 TYPE_LENGTH (type) = 0;
15567 }
c906108c
SS
15568 }
15569 else
15570 {
15571 TYPE_LENGTH (type) = 0;
15572 }
15573
2b4424c3
TT
15574 maybe_set_alignment (cu, die, type);
15575
5230b05a 15576 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15577 {
5230b05a
WT
15578 /* ICC<14 does not output the required DW_AT_declaration on
15579 incomplete types, but gives them a size of zero. */
422b1cb0 15580 TYPE_STUB (type) = 1;
685b1105
JK
15581 }
15582 else
15583 TYPE_STUB_SUPPORTED (type) = 1;
15584
dc718098 15585 if (die_is_declaration (die, cu))
876cecd0 15586 TYPE_STUB (type) = 1;
a6c727b2
DJ
15587 else if (attr == NULL && die->child == NULL
15588 && producer_is_realview (cu->producer))
15589 /* RealView does not output the required DW_AT_declaration
15590 on incomplete types. */
15591 TYPE_STUB (type) = 1;
dc718098 15592
c906108c
SS
15593 /* We need to add the type field to the die immediately so we don't
15594 infinitely recurse when dealing with pointers to the structure
0963b4bd 15595 type within the structure itself. */
1c379e20 15596 set_die_type (die, type, cu);
c906108c 15597
7e314c57
JK
15598 /* set_die_type should be already done. */
15599 set_descriptive_type (type, die, cu);
15600
c767944b
DJ
15601 return type;
15602}
15603
9c6a1327
TT
15604static void handle_struct_member_die
15605 (struct die_info *child_die,
15606 struct type *type,
15607 struct field_info *fi,
15608 std::vector<struct symbol *> *template_args,
15609 struct dwarf2_cu *cu);
15610
15611/* A helper for handle_struct_member_die that handles
15612 DW_TAG_variant_part. */
15613
15614static void
15615handle_variant_part (struct die_info *die, struct type *type,
15616 struct field_info *fi,
15617 std::vector<struct symbol *> *template_args,
15618 struct dwarf2_cu *cu)
15619{
15620 variant_part_builder *new_part;
15621 if (fi->current_variant_part == nullptr)
15622 {
15623 fi->variant_parts.emplace_back ();
15624 new_part = &fi->variant_parts.back ();
15625 }
15626 else if (!fi->current_variant_part->processing_variant)
15627 {
15628 complaint (_("nested DW_TAG_variant_part seen "
15629 "- DIE at %s [in module %s]"),
15630 sect_offset_str (die->sect_off),
5e22e966 15631 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15632 return;
15633 }
15634 else
15635 {
15636 variant_field &current = fi->current_variant_part->variants.back ();
15637 current.variant_parts.emplace_back ();
15638 new_part = &current.variant_parts.back ();
15639 }
15640
15641 /* When we recurse, we want callees to add to this new variant
15642 part. */
15643 scoped_restore save_current_variant_part
15644 = make_scoped_restore (&fi->current_variant_part, new_part);
15645
15646 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15647 if (discr == NULL)
15648 {
15649 /* It's a univariant form, an extension we support. */
15650 }
15651 else if (discr->form_is_ref ())
15652 {
15653 struct dwarf2_cu *target_cu = cu;
15654 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15655
15656 new_part->discriminant_offset = target_die->sect_off;
15657 }
15658 else
15659 {
15660 complaint (_("DW_AT_discr does not have DIE reference form"
15661 " - DIE at %s [in module %s]"),
15662 sect_offset_str (die->sect_off),
5e22e966 15663 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15664 }
15665
15666 for (die_info *child_die = die->child;
15667 child_die != NULL;
15668 child_die = child_die->sibling)
15669 handle_struct_member_die (child_die, type, fi, template_args, cu);
15670}
15671
15672/* A helper for handle_struct_member_die that handles
15673 DW_TAG_variant. */
15674
15675static void
15676handle_variant (struct die_info *die, struct type *type,
15677 struct field_info *fi,
15678 std::vector<struct symbol *> *template_args,
15679 struct dwarf2_cu *cu)
15680{
15681 if (fi->current_variant_part == nullptr)
15682 {
15683 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15684 "- DIE at %s [in module %s]"),
15685 sect_offset_str (die->sect_off),
5e22e966 15686 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15687 return;
15688 }
15689 if (fi->current_variant_part->processing_variant)
15690 {
15691 complaint (_("nested DW_TAG_variant seen "
15692 "- DIE at %s [in module %s]"),
15693 sect_offset_str (die->sect_off),
5e22e966 15694 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15695 return;
15696 }
15697
15698 scoped_restore save_processing_variant
15699 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15700 true);
15701
15702 fi->current_variant_part->variants.emplace_back ();
15703 variant_field &variant = fi->current_variant_part->variants.back ();
15704 variant.first_field = fi->fields.size ();
15705
15706 /* In a variant we want to get the discriminant and also add a
15707 field for our sole member child. */
15708 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15709 if (discr == nullptr)
15710 {
15711 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15712 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15713 variant.default_branch = true;
15714 else
15715 variant.discr_list_data = DW_BLOCK (discr);
15716 }
15717 else
15718 variant.discriminant_value = DW_UNSND (discr);
15719
15720 for (die_info *variant_child = die->child;
15721 variant_child != NULL;
15722 variant_child = variant_child->sibling)
15723 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15724
15725 variant.last_field = fi->fields.size ();
15726}
15727
2ddeaf8a
TT
15728/* A helper for process_structure_scope that handles a single member
15729 DIE. */
15730
15731static void
15732handle_struct_member_die (struct die_info *child_die, struct type *type,
15733 struct field_info *fi,
15734 std::vector<struct symbol *> *template_args,
15735 struct dwarf2_cu *cu)
15736{
15737 if (child_die->tag == DW_TAG_member
9c6a1327 15738 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15739 {
15740 /* NOTE: carlton/2002-11-05: A C++ static data member
15741 should be a DW_TAG_member that is a declaration, but
15742 all versions of G++ as of this writing (so through at
15743 least 3.2.1) incorrectly generate DW_TAG_variable
15744 tags for them instead. */
15745 dwarf2_add_field (fi, child_die, cu);
15746 }
15747 else if (child_die->tag == DW_TAG_subprogram)
15748 {
15749 /* Rust doesn't have member functions in the C++ sense.
15750 However, it does emit ordinary functions as children
15751 of a struct DIE. */
15752 if (cu->language == language_rust)
15753 read_func_scope (child_die, cu);
15754 else
15755 {
15756 /* C++ member function. */
15757 dwarf2_add_member_fn (fi, child_die, type, cu);
15758 }
15759 }
15760 else if (child_die->tag == DW_TAG_inheritance)
15761 {
15762 /* C++ base class field. */
15763 dwarf2_add_field (fi, child_die, cu);
15764 }
15765 else if (type_can_define_types (child_die))
15766 dwarf2_add_type_defn (fi, child_die, cu);
15767 else if (child_die->tag == DW_TAG_template_type_param
15768 || child_die->tag == DW_TAG_template_value_param)
15769 {
15770 struct symbol *arg = new_symbol (child_die, NULL, cu);
15771
15772 if (arg != NULL)
15773 template_args->push_back (arg);
15774 }
9c6a1327
TT
15775 else if (child_die->tag == DW_TAG_variant_part)
15776 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15777 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15778 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15779}
15780
c767944b
DJ
15781/* Finish creating a structure or union type, including filling in
15782 its members and creating a symbol for it. */
15783
15784static void
15785process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15786{
5e22e966 15787 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15788 struct die_info *child_die;
c767944b
DJ
15789 struct type *type;
15790
15791 type = get_die_type (die, cu);
15792 if (type == NULL)
15793 type = read_structure_type (die, cu);
15794
3e1d3d8c 15795 bool has_template_parameters = false;
e142c38c 15796 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15797 {
15798 struct field_info fi;
2f4732b0 15799 std::vector<struct symbol *> template_args;
c906108c 15800
639d11d3 15801 child_die = die->child;
c906108c
SS
15802
15803 while (child_die && child_die->tag)
15804 {
2ddeaf8a 15805 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15806 child_die = child_die->sibling;
c906108c
SS
15807 }
15808
34eaf542 15809 /* Attach template arguments to type. */
2f4732b0 15810 if (!template_args.empty ())
34eaf542 15811 {
3e1d3d8c 15812 has_template_parameters = true;
34eaf542 15813 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15814 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15815 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15816 = XOBNEWVEC (&objfile->objfile_obstack,
15817 struct symbol *,
15818 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15819 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15820 template_args.data (),
34eaf542
TT
15821 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15822 * sizeof (struct symbol *)));
34eaf542
TT
15823 }
15824
c906108c 15825 /* Attach fields and member functions to the type. */
317f7127 15826 if (fi.nfields () > 0)
e7c27a73 15827 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15828 if (!fi.fnfieldlists.empty ())
c906108c 15829 {
e7c27a73 15830 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15831
c5aa993b 15832 /* Get the type which refers to the base class (possibly this
c906108c 15833 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15834 class from the DW_AT_containing_type attribute. This use of
15835 DW_AT_containing_type is a GNU extension. */
c906108c 15836
e142c38c 15837 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15838 {
e7c27a73 15839 struct type *t = die_containing_type (die, cu);
c906108c 15840
ae6ae975 15841 set_type_vptr_basetype (type, t);
c906108c
SS
15842 if (type == t)
15843 {
c906108c
SS
15844 int i;
15845
15846 /* Our own class provides vtbl ptr. */
1f704f76 15847 for (i = t->num_fields () - 1;
c906108c
SS
15848 i >= TYPE_N_BASECLASSES (t);
15849 --i)
15850 {
0d5cff50 15851 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15852
1168df01 15853 if (is_vtable_name (fieldname, cu))
c906108c 15854 {
ae6ae975 15855 set_type_vptr_fieldno (type, i);
c906108c
SS
15856 break;
15857 }
15858 }
15859
15860 /* Complain if virtual function table field not found. */
15861 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15862 complaint (_("virtual function table pointer "
3e43a32a 15863 "not found when defining class '%s'"),
7d93a1e0 15864 type->name () ? type->name () : "");
c906108c
SS
15865 }
15866 else
15867 {
ae6ae975 15868 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15869 }
15870 }
f6235d4c 15871 else if (cu->producer
61012eef 15872 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15873 {
15874 /* The IBM XLC compiler does not provide direct indication
15875 of the containing type, but the vtable pointer is
15876 always named __vfp. */
15877
15878 int i;
15879
1f704f76 15880 for (i = type->num_fields () - 1;
f6235d4c
EZ
15881 i >= TYPE_N_BASECLASSES (type);
15882 --i)
15883 {
15884 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15885 {
ae6ae975
DE
15886 set_type_vptr_fieldno (type, i);
15887 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15888 break;
15889 }
15890 }
15891 }
c906108c 15892 }
98751a41
JK
15893
15894 /* Copy fi.typedef_field_list linked list elements content into the
15895 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15896 if (!fi.typedef_field_list.empty ())
98751a41 15897 {
be2daae6 15898 int count = fi.typedef_field_list.size ();
98751a41 15899
a0d7a4ff 15900 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15901 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15902 = ((struct decl_field *)
be2daae6
TT
15903 TYPE_ALLOC (type,
15904 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15905 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15906
be2daae6
TT
15907 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15908 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15909 }
c767944b 15910
883fd55a
KS
15911 /* Copy fi.nested_types_list linked list elements content into the
15912 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15913 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15914 {
be2daae6 15915 int count = fi.nested_types_list.size ();
883fd55a
KS
15916
15917 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15918 TYPE_NESTED_TYPES_ARRAY (type)
15919 = ((struct decl_field *)
be2daae6
TT
15920 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15921 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15922
be2daae6
TT
15923 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15924 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15925 }
c906108c 15926 }
63d06c5c 15927
bb5ed363 15928 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15929 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15930 cu->rust_unions.push_back (type);
0b92b5bb 15931
90aeadfc
DC
15932 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15933 snapshots) has been known to create a die giving a declaration
15934 for a class that has, as a child, a die giving a definition for a
15935 nested class. So we have to process our children even if the
15936 current die is a declaration. Normally, of course, a declaration
15937 won't have any children at all. */
134d01f1 15938
ca040673
DE
15939 child_die = die->child;
15940
90aeadfc
DC
15941 while (child_die != NULL && child_die->tag)
15942 {
15943 if (child_die->tag == DW_TAG_member
15944 || child_die->tag == DW_TAG_variable
34eaf542
TT
15945 || child_die->tag == DW_TAG_inheritance
15946 || child_die->tag == DW_TAG_template_value_param
15947 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15948 {
90aeadfc 15949 /* Do nothing. */
134d01f1 15950 }
90aeadfc
DC
15951 else
15952 process_die (child_die, cu);
134d01f1 15953
436c571c 15954 child_die = child_die->sibling;
134d01f1
DJ
15955 }
15956
fa4028e9
JB
15957 /* Do not consider external references. According to the DWARF standard,
15958 these DIEs are identified by the fact that they have no byte_size
15959 attribute, and a declaration attribute. */
15960 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15961 || !die_is_declaration (die, cu)
15962 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15963 {
15964 struct symbol *sym = new_symbol (die, type, cu);
15965
15966 if (has_template_parameters)
15967 {
a776957c
TT
15968 struct symtab *symtab;
15969 if (sym != nullptr)
15970 symtab = symbol_symtab (sym);
15971 else if (cu->line_header != nullptr)
15972 {
15973 /* Any related symtab will do. */
15974 symtab
7ba99d21 15975 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15976 }
15977 else
15978 {
15979 symtab = nullptr;
15980 complaint (_("could not find suitable "
15981 "symtab for template parameter"
15982 " - DIE at %s [in module %s]"),
15983 sect_offset_str (die->sect_off),
15984 objfile_name (objfile));
15985 }
15986
15987 if (symtab != nullptr)
15988 {
15989 /* Make sure that the symtab is set on the new symbols.
15990 Even though they don't appear in this symtab directly,
15991 other parts of gdb assume that symbols do, and this is
15992 reasonably true. */
15993 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15994 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15995 }
3e1d3d8c
TT
15996 }
15997 }
134d01f1
DJ
15998}
15999
ed6acedd
TT
16000/* Assuming DIE is an enumeration type, and TYPE is its associated
16001 type, update TYPE using some information only available in DIE's
16002 children. In particular, the fields are computed. */
55426c9d
JB
16003
16004static void
16005update_enumeration_type_from_children (struct die_info *die,
16006 struct type *type,
16007 struct dwarf2_cu *cu)
16008{
60f7655a 16009 struct die_info *child_die;
55426c9d
JB
16010 int unsigned_enum = 1;
16011 int flag_enum = 1;
55426c9d 16012
8268c778 16013 auto_obstack obstack;
ed6acedd 16014 std::vector<struct field> fields;
55426c9d 16015
60f7655a
DE
16016 for (child_die = die->child;
16017 child_die != NULL && child_die->tag;
436c571c 16018 child_die = child_die->sibling)
55426c9d
JB
16019 {
16020 struct attribute *attr;
16021 LONGEST value;
16022 const gdb_byte *bytes;
16023 struct dwarf2_locexpr_baton *baton;
16024 const char *name;
60f7655a 16025
55426c9d
JB
16026 if (child_die->tag != DW_TAG_enumerator)
16027 continue;
16028
16029 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16030 if (attr == NULL)
16031 continue;
16032
16033 name = dwarf2_name (child_die, cu);
16034 if (name == NULL)
16035 name = "<anonymous enumerator>";
16036
16037 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16038 &value, &bytes, &baton);
16039 if (value < 0)
16040 {
16041 unsigned_enum = 0;
16042 flag_enum = 0;
16043 }
55426c9d 16044 else
edd45eb0
SM
16045 {
16046 if (count_one_bits_ll (value) >= 2)
16047 flag_enum = 0;
edd45eb0 16048 }
55426c9d 16049
ed6acedd
TT
16050 fields.emplace_back ();
16051 struct field &field = fields.back ();
16052 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16053 SET_FIELD_ENUMVAL (field, value);
16054 }
16055
16056 if (!fields.empty ())
16057 {
5e33d5f4 16058 type->set_num_fields (fields.size ());
3cabb6b0
SM
16059 type->set_fields
16060 ((struct field *)
16061 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16062 memcpy (type->fields (), fields.data (),
ed6acedd 16063 sizeof (struct field) * fields.size ());
55426c9d
JB
16064 }
16065
16066 if (unsigned_enum)
16067 TYPE_UNSIGNED (type) = 1;
16068 if (flag_enum)
16069 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16070}
16071
134d01f1
DJ
16072/* Given a DW_AT_enumeration_type die, set its type. We do not
16073 complete the type's fields yet, or create any symbols. */
c906108c 16074
f792889a 16075static struct type *
134d01f1 16076read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16077{
5e22e966 16078 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16079 struct type *type;
c906108c 16080 struct attribute *attr;
0114d602 16081 const char *name;
134d01f1 16082
348e048f
DE
16083 /* If the definition of this type lives in .debug_types, read that type.
16084 Don't follow DW_AT_specification though, that will take us back up
16085 the chain and we want to go down. */
052c8bb8 16086 attr = die->attr (DW_AT_signature);
435d3d88 16087 if (attr != nullptr)
348e048f 16088 {
ac9ec31b 16089 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16090
ac9ec31b 16091 /* The type's CU may not be the same as CU.
02142a6c 16092 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16093 return set_die_type (die, type, cu);
16094 }
16095
c906108c
SS
16096 type = alloc_type (objfile);
16097
67607e24 16098 type->set_code (TYPE_CODE_ENUM);
94af9270 16099 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16100 if (name != NULL)
d0e39ea2 16101 type->set_name (name);
c906108c 16102
0626fc76
TT
16103 attr = dwarf2_attr (die, DW_AT_type, cu);
16104 if (attr != NULL)
16105 {
16106 struct type *underlying_type = die_type (die, cu);
16107
16108 TYPE_TARGET_TYPE (type) = underlying_type;
16109 }
16110
e142c38c 16111 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16112 if (attr != nullptr)
c906108c
SS
16113 {
16114 TYPE_LENGTH (type) = DW_UNSND (attr);
16115 }
16116 else
16117 {
16118 TYPE_LENGTH (type) = 0;
16119 }
16120
2b4424c3
TT
16121 maybe_set_alignment (cu, die, type);
16122
137033e9
JB
16123 /* The enumeration DIE can be incomplete. In Ada, any type can be
16124 declared as private in the package spec, and then defined only
16125 inside the package body. Such types are known as Taft Amendment
16126 Types. When another package uses such a type, an incomplete DIE
16127 may be generated by the compiler. */
02eb380e 16128 if (die_is_declaration (die, cu))
876cecd0 16129 TYPE_STUB (type) = 1;
02eb380e 16130
0626fc76
TT
16131 /* If this type has an underlying type that is not a stub, then we
16132 may use its attributes. We always use the "unsigned" attribute
16133 in this situation, because ordinarily we guess whether the type
16134 is unsigned -- but the guess can be wrong and the underlying type
16135 can tell us the reality. However, we defer to a local size
16136 attribute if one exists, because this lets the compiler override
16137 the underlying type if needed. */
16138 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16139 {
9e7c9a03
HD
16140 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16141 underlying_type = check_typedef (underlying_type);
16142 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16143 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16144 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16145 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16146 && TYPE_RAW_ALIGN (underlying_type) != 0)
16147 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16148 }
16149
3d567982
TT
16150 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16151
ed6acedd
TT
16152 set_die_type (die, type, cu);
16153
16154 /* Finish the creation of this type by using the enum's children.
16155 Note that, as usual, this must come after set_die_type to avoid
16156 infinite recursion when trying to compute the names of the
16157 enumerators. */
16158 update_enumeration_type_from_children (die, type, cu);
16159
16160 return type;
134d01f1
DJ
16161}
16162
16163/* Given a pointer to a die which begins an enumeration, process all
16164 the dies that define the members of the enumeration, and create the
16165 symbol for the enumeration type.
16166
16167 NOTE: We reverse the order of the element list. */
16168
16169static void
16170process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16171{
f792889a 16172 struct type *this_type;
134d01f1 16173
f792889a
DJ
16174 this_type = get_die_type (die, cu);
16175 if (this_type == NULL)
16176 this_type = read_enumeration_type (die, cu);
9dc481d3 16177
639d11d3 16178 if (die->child != NULL)
c906108c 16179 {
9dc481d3 16180 struct die_info *child_die;
15d034d0 16181 const char *name;
9dc481d3 16182
639d11d3 16183 child_die = die->child;
c906108c
SS
16184 while (child_die && child_die->tag)
16185 {
16186 if (child_die->tag != DW_TAG_enumerator)
16187 {
e7c27a73 16188 process_die (child_die, cu);
c906108c
SS
16189 }
16190 else
16191 {
39cbfefa
DJ
16192 name = dwarf2_name (child_die, cu);
16193 if (name)
ed6acedd 16194 new_symbol (child_die, this_type, cu);
c906108c
SS
16195 }
16196
436c571c 16197 child_die = child_die->sibling;
c906108c 16198 }
c906108c 16199 }
134d01f1 16200
6c83ed52
TT
16201 /* If we are reading an enum from a .debug_types unit, and the enum
16202 is a declaration, and the enum is not the signatured type in the
16203 unit, then we do not want to add a symbol for it. Adding a
16204 symbol would in some cases obscure the true definition of the
16205 enum, giving users an incomplete type when the definition is
16206 actually available. Note that we do not want to do this for all
16207 enums which are just declarations, because C++0x allows forward
16208 enum declarations. */
3019eac3 16209 if (cu->per_cu->is_debug_types
6c83ed52
TT
16210 && die_is_declaration (die, cu))
16211 {
52dc124a 16212 struct signatured_type *sig_type;
6c83ed52 16213
c0f78cd4 16214 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16215 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16216 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16217 return;
16218 }
16219
f792889a 16220 new_symbol (die, this_type, cu);
c906108c
SS
16221}
16222
16223/* Extract all information from a DW_TAG_array_type DIE and put it in
16224 the DIE's type field. For now, this only handles one dimensional
16225 arrays. */
16226
f792889a 16227static struct type *
e7c27a73 16228read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16229{
5e22e966 16230 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16231 struct die_info *child_die;
7e314c57 16232 struct type *type;
c906108c 16233 struct type *element_type, *range_type, *index_type;
c906108c 16234 struct attribute *attr;
15d034d0 16235 const char *name;
a405673c 16236 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16237 unsigned int bit_stride = 0;
c906108c 16238
e7c27a73 16239 element_type = die_type (die, cu);
c906108c 16240
7e314c57
JK
16241 /* The die_type call above may have already set the type for this DIE. */
16242 type = get_die_type (die, cu);
16243 if (type)
16244 return type;
16245
dc53a7ad
JB
16246 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16247 if (attr != NULL)
a405673c
JB
16248 {
16249 int stride_ok;
293e7e51 16250 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16251
16252 byte_stride_prop
16253 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16254 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16255 prop_type);
a405673c
JB
16256 if (!stride_ok)
16257 {
b98664d3 16258 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16259 " - DIE at %s [in module %s]"),
16260 sect_offset_str (die->sect_off),
5e22e966 16261 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16262 /* Ignore this attribute. We will likely not be able to print
16263 arrays of this type correctly, but there is little we can do
16264 to help if we cannot read the attribute's value. */
16265 byte_stride_prop = NULL;
16266 }
16267 }
dc53a7ad
JB
16268
16269 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16270 if (attr != NULL)
16271 bit_stride = DW_UNSND (attr);
16272
c906108c
SS
16273 /* Irix 6.2 native cc creates array types without children for
16274 arrays with unspecified length. */
639d11d3 16275 if (die->child == NULL)
c906108c 16276 {
46bf5051 16277 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16278 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16279 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16280 byte_stride_prop, bit_stride);
f792889a 16281 return set_die_type (die, type, cu);
c906108c
SS
16282 }
16283
791afaa2 16284 std::vector<struct type *> range_types;
639d11d3 16285 child_die = die->child;
c906108c
SS
16286 while (child_die && child_die->tag)
16287 {
16288 if (child_die->tag == DW_TAG_subrange_type)
16289 {
f792889a 16290 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16291
f792889a 16292 if (child_type != NULL)
a02abb62 16293 {
0963b4bd
MS
16294 /* The range type was succesfully read. Save it for the
16295 array type creation. */
791afaa2 16296 range_types.push_back (child_type);
a02abb62 16297 }
c906108c 16298 }
436c571c 16299 child_die = child_die->sibling;
c906108c
SS
16300 }
16301
16302 /* Dwarf2 dimensions are output from left to right, create the
16303 necessary array types in backwards order. */
7ca2d3a3 16304
c906108c 16305 type = element_type;
7ca2d3a3
DL
16306
16307 if (read_array_order (die, cu) == DW_ORD_col_major)
16308 {
16309 int i = 0;
9a619af0 16310
791afaa2 16311 while (i < range_types.size ())
dc53a7ad 16312 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16313 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16314 }
16315 else
16316 {
791afaa2 16317 size_t ndim = range_types.size ();
7ca2d3a3 16318 while (ndim-- > 0)
dc53a7ad 16319 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16320 byte_stride_prop, bit_stride);
7ca2d3a3 16321 }
c906108c 16322
f5f8a009
EZ
16323 /* Understand Dwarf2 support for vector types (like they occur on
16324 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16325 array type. This is not part of the Dwarf2/3 standard yet, but a
16326 custom vendor extension. The main difference between a regular
16327 array and the vector variant is that vectors are passed by value
16328 to functions. */
e142c38c 16329 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16330 if (attr != nullptr)
ea37ba09 16331 make_vector_type (type);
f5f8a009 16332
dbc98a8b
KW
16333 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16334 implementation may choose to implement triple vectors using this
16335 attribute. */
16336 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16337 if (attr != nullptr)
dbc98a8b
KW
16338 {
16339 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16340 TYPE_LENGTH (type) = DW_UNSND (attr);
16341 else
b98664d3 16342 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16343 "than the total size of elements"));
dbc98a8b
KW
16344 }
16345
39cbfefa
DJ
16346 name = dwarf2_name (die, cu);
16347 if (name)
d0e39ea2 16348 type->set_name (name);
6e70227d 16349
2b4424c3
TT
16350 maybe_set_alignment (cu, die, type);
16351
0963b4bd 16352 /* Install the type in the die. */
7e314c57
JK
16353 set_die_type (die, type, cu);
16354
16355 /* set_die_type should be already done. */
b4ba55a1
JB
16356 set_descriptive_type (type, die, cu);
16357
7e314c57 16358 return type;
c906108c
SS
16359}
16360
7ca2d3a3 16361static enum dwarf_array_dim_ordering
6e70227d 16362read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16363{
16364 struct attribute *attr;
16365
16366 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16367
435d3d88 16368 if (attr != nullptr)
aead7601 16369 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16370
0963b4bd
MS
16371 /* GNU F77 is a special case, as at 08/2004 array type info is the
16372 opposite order to the dwarf2 specification, but data is still
16373 laid out as per normal fortran.
7ca2d3a3 16374
0963b4bd
MS
16375 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16376 version checking. */
7ca2d3a3 16377
905e0470
PM
16378 if (cu->language == language_fortran
16379 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16380 {
16381 return DW_ORD_row_major;
16382 }
16383
6e70227d 16384 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16385 {
16386 case array_column_major:
16387 return DW_ORD_col_major;
16388 case array_row_major:
16389 default:
16390 return DW_ORD_row_major;
16391 };
16392}
16393
72019c9c 16394/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16395 the DIE's type field. */
72019c9c 16396
f792889a 16397static struct type *
72019c9c
GM
16398read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16399{
7e314c57
JK
16400 struct type *domain_type, *set_type;
16401 struct attribute *attr;
f792889a 16402
7e314c57
JK
16403 domain_type = die_type (die, cu);
16404
16405 /* The die_type call above may have already set the type for this DIE. */
16406 set_type = get_die_type (die, cu);
16407 if (set_type)
16408 return set_type;
16409
16410 set_type = create_set_type (NULL, domain_type);
16411
16412 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16413 if (attr != nullptr)
d09039dd 16414 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16415
2b4424c3
TT
16416 maybe_set_alignment (cu, die, set_type);
16417
f792889a 16418 return set_die_type (die, set_type, cu);
72019c9c 16419}
7ca2d3a3 16420
0971de02
TT
16421/* A helper for read_common_block that creates a locexpr baton.
16422 SYM is the symbol which we are marking as computed.
16423 COMMON_DIE is the DIE for the common block.
16424 COMMON_LOC is the location expression attribute for the common
16425 block itself.
16426 MEMBER_LOC is the location expression attribute for the particular
16427 member of the common block that we are processing.
16428 CU is the CU from which the above come. */
16429
16430static void
16431mark_common_block_symbol_computed (struct symbol *sym,
16432 struct die_info *common_die,
16433 struct attribute *common_loc,
16434 struct attribute *member_loc,
16435 struct dwarf2_cu *cu)
16436{
5e22e966 16437 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16438 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16439 struct dwarf2_locexpr_baton *baton;
16440 gdb_byte *ptr;
16441 unsigned int cu_off;
08feed99 16442 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16443 LONGEST offset = 0;
16444
16445 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16446 gdb_assert (common_loc->form_is_block ());
16447 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16448 || member_loc->form_is_constant ());
0971de02 16449
8d749320 16450 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16451 baton->per_objfile = per_objfile;
0971de02
TT
16452 baton->per_cu = cu->per_cu;
16453 gdb_assert (baton->per_cu);
16454
16455 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16456
cd6c91b4 16457 if (member_loc->form_is_constant ())
0971de02 16458 {
0826b30a 16459 offset = member_loc->constant_value (0);
0971de02
TT
16460 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16461 }
16462 else
16463 baton->size += DW_BLOCK (member_loc)->size;
16464
224c3ddb 16465 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16466 baton->data = ptr;
16467
16468 *ptr++ = DW_OP_call4;
9c541725 16469 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16470 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16471 ptr += 4;
16472
cd6c91b4 16473 if (member_loc->form_is_constant ())
0971de02
TT
16474 {
16475 *ptr++ = DW_OP_addr;
16476 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16477 ptr += cu->header.addr_size;
16478 }
16479 else
16480 {
16481 /* We have to copy the data here, because DW_OP_call4 will only
16482 use a DW_AT_location attribute. */
16483 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16484 ptr += DW_BLOCK (member_loc)->size;
16485 }
16486
16487 *ptr++ = DW_OP_plus;
16488 gdb_assert (ptr - baton->data == baton->size);
16489
0971de02 16490 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16491 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16492}
16493
4357ac6c
TT
16494/* Create appropriate locally-scoped variables for all the
16495 DW_TAG_common_block entries. Also create a struct common_block
16496 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16497 is used to separate the common blocks name namespace from regular
4357ac6c 16498 variable names. */
c906108c
SS
16499
16500static void
e7c27a73 16501read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16502{
0971de02
TT
16503 struct attribute *attr;
16504
16505 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16506 if (attr != nullptr)
0971de02
TT
16507 {
16508 /* Support the .debug_loc offsets. */
4fc6c0d5 16509 if (attr->form_is_block ())
0971de02
TT
16510 {
16511 /* Ok. */
16512 }
cd6c91b4 16513 else if (attr->form_is_section_offset ())
0971de02
TT
16514 {
16515 dwarf2_complex_location_expr_complaint ();
16516 attr = NULL;
16517 }
16518 else
16519 {
16520 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16521 "common block member");
16522 attr = NULL;
16523 }
16524 }
16525
639d11d3 16526 if (die->child != NULL)
c906108c 16527 {
5e22e966 16528 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16529 struct die_info *child_die;
16530 size_t n_entries = 0, size;
16531 struct common_block *common_block;
16532 struct symbol *sym;
74ac6d43 16533
4357ac6c
TT
16534 for (child_die = die->child;
16535 child_die && child_die->tag;
436c571c 16536 child_die = child_die->sibling)
4357ac6c
TT
16537 ++n_entries;
16538
16539 size = (sizeof (struct common_block)
16540 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16541 common_block
16542 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16543 size);
4357ac6c
TT
16544 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16545 common_block->n_entries = 0;
16546
16547 for (child_die = die->child;
16548 child_die && child_die->tag;
436c571c 16549 child_die = child_die->sibling)
4357ac6c
TT
16550 {
16551 /* Create the symbol in the DW_TAG_common_block block in the current
16552 symbol scope. */
e7c27a73 16553 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16554 if (sym != NULL)
16555 {
16556 struct attribute *member_loc;
16557
16558 common_block->contents[common_block->n_entries++] = sym;
16559
16560 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16561 cu);
16562 if (member_loc)
16563 {
16564 /* GDB has handled this for a long time, but it is
16565 not specified by DWARF. It seems to have been
16566 emitted by gfortran at least as recently as:
16567 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16568 complaint (_("Variable in common block has "
0971de02 16569 "DW_AT_data_member_location "
9d8780f0
SM
16570 "- DIE at %s [in module %s]"),
16571 sect_offset_str (child_die->sect_off),
518817b3 16572 objfile_name (objfile));
0971de02 16573
cd6c91b4 16574 if (member_loc->form_is_section_offset ())
0971de02 16575 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16576 else if (member_loc->form_is_constant ()
4fc6c0d5 16577 || member_loc->form_is_block ())
0971de02 16578 {
435d3d88 16579 if (attr != nullptr)
0971de02
TT
16580 mark_common_block_symbol_computed (sym, die, attr,
16581 member_loc, cu);
16582 }
16583 else
16584 dwarf2_complex_location_expr_complaint ();
16585 }
16586 }
c906108c 16587 }
4357ac6c
TT
16588
16589 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16590 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16591 }
16592}
16593
0114d602 16594/* Create a type for a C++ namespace. */
d9fa45fe 16595
0114d602
DJ
16596static struct type *
16597read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16598{
5e22e966 16599 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16600 const char *previous_prefix, *name;
9219021c 16601 int is_anonymous;
0114d602
DJ
16602 struct type *type;
16603
16604 /* For extensions, reuse the type of the original namespace. */
16605 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16606 {
16607 struct die_info *ext_die;
16608 struct dwarf2_cu *ext_cu = cu;
9a619af0 16609
0114d602
DJ
16610 ext_die = dwarf2_extension (die, &ext_cu);
16611 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16612
16613 /* EXT_CU may not be the same as CU.
02142a6c 16614 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16615 return set_die_type (die, type, cu);
16616 }
9219021c 16617
e142c38c 16618 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16619
16620 /* Now build the name of the current namespace. */
16621
0114d602
DJ
16622 previous_prefix = determine_prefix (die, cu);
16623 if (previous_prefix[0] != '\0')
16624 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16625 previous_prefix, name, 0, cu);
0114d602
DJ
16626
16627 /* Create the type. */
19f392bc 16628 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16629
60531b24 16630 return set_die_type (die, type, cu);
0114d602
DJ
16631}
16632
22cee43f 16633/* Read a namespace scope. */
0114d602
DJ
16634
16635static void
16636read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16637{
5e22e966 16638 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16639 int is_anonymous;
9219021c 16640
5c4e30ca
DC
16641 /* Add a symbol associated to this if we haven't seen the namespace
16642 before. Also, add a using directive if it's an anonymous
16643 namespace. */
9219021c 16644
f2f0e013 16645 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16646 {
16647 struct type *type;
16648
0114d602 16649 type = read_type_die (die, cu);
e7c27a73 16650 new_symbol (die, type, cu);
5c4e30ca 16651
e8e80198 16652 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16653 if (is_anonymous)
0114d602
DJ
16654 {
16655 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16656
eb1e02fd 16657 std::vector<const char *> excludes;
804d2729 16658 add_using_directive (using_directives (cu),
7d93a1e0 16659 previous_prefix, type->name (), NULL,
eb1e02fd 16660 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16661 }
5c4e30ca 16662 }
9219021c 16663
639d11d3 16664 if (die->child != NULL)
d9fa45fe 16665 {
639d11d3 16666 struct die_info *child_die = die->child;
6e70227d 16667
d9fa45fe
DC
16668 while (child_die && child_die->tag)
16669 {
e7c27a73 16670 process_die (child_die, cu);
436c571c 16671 child_die = child_die->sibling;
d9fa45fe
DC
16672 }
16673 }
38d518c9
EZ
16674}
16675
f55ee35c
JK
16676/* Read a Fortran module as type. This DIE can be only a declaration used for
16677 imported module. Still we need that type as local Fortran "use ... only"
16678 declaration imports depend on the created type in determine_prefix. */
16679
16680static struct type *
16681read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16682{
5e22e966 16683 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16684 const char *module_name;
f55ee35c
JK
16685 struct type *type;
16686
16687 module_name = dwarf2_name (die, cu);
19f392bc 16688 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16689
f55ee35c
JK
16690 return set_die_type (die, type, cu);
16691}
16692
5d7cb8df
JK
16693/* Read a Fortran module. */
16694
16695static void
16696read_module (struct die_info *die, struct dwarf2_cu *cu)
16697{
16698 struct die_info *child_die = die->child;
530e8392
KB
16699 struct type *type;
16700
16701 type = read_type_die (die, cu);
16702 new_symbol (die, type, cu);
5d7cb8df 16703
5d7cb8df
JK
16704 while (child_die && child_die->tag)
16705 {
16706 process_die (child_die, cu);
436c571c 16707 child_die = child_die->sibling;
5d7cb8df
JK
16708 }
16709}
16710
38d518c9
EZ
16711/* Return the name of the namespace represented by DIE. Set
16712 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16713 namespace. */
16714
16715static const char *
e142c38c 16716namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16717{
16718 struct die_info *current_die;
16719 const char *name = NULL;
16720
16721 /* Loop through the extensions until we find a name. */
16722
16723 for (current_die = die;
16724 current_die != NULL;
f2f0e013 16725 current_die = dwarf2_extension (die, &cu))
38d518c9 16726 {
96553a0c
DE
16727 /* We don't use dwarf2_name here so that we can detect the absence
16728 of a name -> anonymous namespace. */
7d45c7c3 16729 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16730
38d518c9
EZ
16731 if (name != NULL)
16732 break;
16733 }
16734
16735 /* Is it an anonymous namespace? */
16736
16737 *is_anonymous = (name == NULL);
16738 if (*is_anonymous)
2b1dbab0 16739 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16740
16741 return name;
d9fa45fe
DC
16742}
16743
c906108c
SS
16744/* Extract all information from a DW_TAG_pointer_type DIE and add to
16745 the user defined type vector. */
16746
f792889a 16747static struct type *
e7c27a73 16748read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16749{
5e22e966 16750 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16751 struct comp_unit_head *cu_header = &cu->header;
c906108c 16752 struct type *type;
8b2dbe47
KB
16753 struct attribute *attr_byte_size;
16754 struct attribute *attr_address_class;
16755 int byte_size, addr_class;
7e314c57
JK
16756 struct type *target_type;
16757
16758 target_type = die_type (die, cu);
c906108c 16759
7e314c57
JK
16760 /* The die_type call above may have already set the type for this DIE. */
16761 type = get_die_type (die, cu);
16762 if (type)
16763 return type;
16764
16765 type = lookup_pointer_type (target_type);
8b2dbe47 16766
e142c38c 16767 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16768 if (attr_byte_size)
16769 byte_size = DW_UNSND (attr_byte_size);
c906108c 16770 else
8b2dbe47
KB
16771 byte_size = cu_header->addr_size;
16772
e142c38c 16773 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16774 if (attr_address_class)
16775 addr_class = DW_UNSND (attr_address_class);
16776 else
16777 addr_class = DW_ADDR_none;
16778
2b4424c3
TT
16779 ULONGEST alignment = get_alignment (cu, die);
16780
16781 /* If the pointer size, alignment, or address class is different
16782 than the default, create a type variant marked as such and set
16783 the length accordingly. */
16784 if (TYPE_LENGTH (type) != byte_size
16785 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16786 && alignment != TYPE_RAW_ALIGN (type))
16787 || addr_class != DW_ADDR_none)
c906108c 16788 {
5e2b427d 16789 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16790 {
16791 int type_flags;
16792
849957d9 16793 type_flags = gdbarch_address_class_type_flags
5e2b427d 16794 (gdbarch, byte_size, addr_class);
876cecd0
TT
16795 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16796 == 0);
8b2dbe47
KB
16797 type = make_type_with_address_space (type, type_flags);
16798 }
16799 else if (TYPE_LENGTH (type) != byte_size)
16800 {
b98664d3 16801 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16802 }
2b4424c3
TT
16803 else if (TYPE_RAW_ALIGN (type) != alignment)
16804 {
b98664d3 16805 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16806 " - DIE at %s [in module %s]"),
16807 sect_offset_str (die->sect_off),
5e22e966 16808 objfile_name (cu->per_objfile->objfile));
2b4424c3 16809 }
6e70227d 16810 else
9a619af0
MS
16811 {
16812 /* Should we also complain about unhandled address classes? */
16813 }
c906108c 16814 }
8b2dbe47
KB
16815
16816 TYPE_LENGTH (type) = byte_size;
2b4424c3 16817 set_type_align (type, alignment);
f792889a 16818 return set_die_type (die, type, cu);
c906108c
SS
16819}
16820
16821/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16822 the user defined type vector. */
16823
f792889a 16824static struct type *
e7c27a73 16825read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16826{
16827 struct type *type;
16828 struct type *to_type;
16829 struct type *domain;
16830
e7c27a73
DJ
16831 to_type = die_type (die, cu);
16832 domain = die_containing_type (die, cu);
0d5de010 16833
7e314c57
JK
16834 /* The calls above may have already set the type for this DIE. */
16835 type = get_die_type (die, cu);
16836 if (type)
16837 return type;
16838
78134374 16839 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16840 type = lookup_methodptr_type (to_type);
78134374 16841 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16842 {
5e22e966 16843 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16844
16845 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16846 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16847 TYPE_VARARGS (to_type));
16848 type = lookup_methodptr_type (new_type);
16849 }
0d5de010
DJ
16850 else
16851 type = lookup_memberptr_type (to_type, domain);
c906108c 16852
f792889a 16853 return set_die_type (die, type, cu);
c906108c
SS
16854}
16855
4297a3f0 16856/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16857 the user defined type vector. */
16858
f792889a 16859static struct type *
4297a3f0
AV
16860read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16861 enum type_code refcode)
c906108c 16862{
e7c27a73 16863 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16864 struct type *type, *target_type;
c906108c
SS
16865 struct attribute *attr;
16866
4297a3f0
AV
16867 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16868
7e314c57
JK
16869 target_type = die_type (die, cu);
16870
16871 /* The die_type call above may have already set the type for this DIE. */
16872 type = get_die_type (die, cu);
16873 if (type)
16874 return type;
16875
4297a3f0 16876 type = lookup_reference_type (target_type, refcode);
e142c38c 16877 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16878 if (attr != nullptr)
c906108c
SS
16879 {
16880 TYPE_LENGTH (type) = DW_UNSND (attr);
16881 }
16882 else
16883 {
107d2387 16884 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16885 }
2b4424c3 16886 maybe_set_alignment (cu, die, type);
f792889a 16887 return set_die_type (die, type, cu);
c906108c
SS
16888}
16889
cf363f18
MW
16890/* Add the given cv-qualifiers to the element type of the array. GCC
16891 outputs DWARF type qualifiers that apply to an array, not the
16892 element type. But GDB relies on the array element type to carry
16893 the cv-qualifiers. This mimics section 6.7.3 of the C99
16894 specification. */
16895
16896static struct type *
16897add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16898 struct type *base_type, int cnst, int voltl)
16899{
16900 struct type *el_type, *inner_array;
16901
16902 base_type = copy_type (base_type);
16903 inner_array = base_type;
16904
78134374 16905 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16906 {
16907 TYPE_TARGET_TYPE (inner_array) =
16908 copy_type (TYPE_TARGET_TYPE (inner_array));
16909 inner_array = TYPE_TARGET_TYPE (inner_array);
16910 }
16911
16912 el_type = TYPE_TARGET_TYPE (inner_array);
16913 cnst |= TYPE_CONST (el_type);
16914 voltl |= TYPE_VOLATILE (el_type);
16915 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16916
16917 return set_die_type (die, base_type, cu);
16918}
16919
f792889a 16920static struct type *
e7c27a73 16921read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16922{
f792889a 16923 struct type *base_type, *cv_type;
c906108c 16924
e7c27a73 16925 base_type = die_type (die, cu);
7e314c57
JK
16926
16927 /* The die_type call above may have already set the type for this DIE. */
16928 cv_type = get_die_type (die, cu);
16929 if (cv_type)
16930 return cv_type;
16931
2f608a3a
KW
16932 /* In case the const qualifier is applied to an array type, the element type
16933 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16934 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16935 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16936
f792889a
DJ
16937 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16938 return set_die_type (die, cv_type, cu);
c906108c
SS
16939}
16940
f792889a 16941static struct type *
e7c27a73 16942read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16943{
f792889a 16944 struct type *base_type, *cv_type;
c906108c 16945
e7c27a73 16946 base_type = die_type (die, cu);
7e314c57
JK
16947
16948 /* The die_type call above may have already set the type for this DIE. */
16949 cv_type = get_die_type (die, cu);
16950 if (cv_type)
16951 return cv_type;
16952
cf363f18
MW
16953 /* In case the volatile qualifier is applied to an array type, the
16954 element type is so qualified, not the array type (section 6.7.3
16955 of C99). */
78134374 16956 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16957 return add_array_cv_type (die, cu, base_type, 0, 1);
16958
f792889a
DJ
16959 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16960 return set_die_type (die, cv_type, cu);
c906108c
SS
16961}
16962
06d66ee9
TT
16963/* Handle DW_TAG_restrict_type. */
16964
16965static struct type *
16966read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16967{
16968 struct type *base_type, *cv_type;
16969
16970 base_type = die_type (die, cu);
16971
16972 /* The die_type call above may have already set the type for this DIE. */
16973 cv_type = get_die_type (die, cu);
16974 if (cv_type)
16975 return cv_type;
16976
16977 cv_type = make_restrict_type (base_type);
16978 return set_die_type (die, cv_type, cu);
16979}
16980
a2c2acaf
MW
16981/* Handle DW_TAG_atomic_type. */
16982
16983static struct type *
16984read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16985{
16986 struct type *base_type, *cv_type;
16987
16988 base_type = die_type (die, cu);
16989
16990 /* The die_type call above may have already set the type for this DIE. */
16991 cv_type = get_die_type (die, cu);
16992 if (cv_type)
16993 return cv_type;
16994
16995 cv_type = make_atomic_type (base_type);
16996 return set_die_type (die, cv_type, cu);
16997}
16998
c906108c
SS
16999/* Extract all information from a DW_TAG_string_type DIE and add to
17000 the user defined type vector. It isn't really a user defined type,
17001 but it behaves like one, with other DIE's using an AT_user_def_type
17002 attribute to reference it. */
17003
f792889a 17004static struct type *
e7c27a73 17005read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17006{
5e22e966 17007 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17008 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17009 struct type *type, *range_type, *index_type, *char_type;
17010 struct attribute *attr;
216a7e6b
AB
17011 struct dynamic_prop prop;
17012 bool length_is_constant = true;
17013 LONGEST length;
17014
17015 /* There are a couple of places where bit sizes might be made use of
17016 when parsing a DW_TAG_string_type, however, no producer that we know
17017 of make use of these. Handling bit sizes that are a multiple of the
17018 byte size is easy enough, but what about other bit sizes? Lets deal
17019 with that problem when we have to. Warn about these attributes being
17020 unsupported, then parse the type and ignore them like we always
17021 have. */
17022 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17023 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17024 {
17025 static bool warning_printed = false;
17026 if (!warning_printed)
17027 {
17028 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17029 "currently supported on DW_TAG_string_type."));
17030 warning_printed = true;
17031 }
17032 }
c906108c 17033
e142c38c 17034 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17035 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17036 {
17037 /* The string length describes the location at which the length of
17038 the string can be found. The size of the length field can be
17039 specified with one of the attributes below. */
17040 struct type *prop_type;
17041 struct attribute *len
17042 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17043 if (len == nullptr)
17044 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17045 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17046 {
17047 /* Pass 0 as the default as we know this attribute is constant
17048 and the default value will not be returned. */
0826b30a 17049 LONGEST sz = len->constant_value (0);
293e7e51 17050 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17051 }
17052 else
17053 {
17054 /* If the size is not specified then we assume it is the size of
17055 an address on this target. */
293e7e51 17056 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17057 }
17058
17059 /* Convert the attribute into a dynamic property. */
17060 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17061 length = 1;
17062 else
17063 length_is_constant = false;
17064 }
17065 else if (attr != nullptr)
17066 {
17067 /* This DW_AT_string_length just contains the length with no
17068 indirection. There's no need to create a dynamic property in this
17069 case. Pass 0 for the default value as we know it will not be
17070 returned in this case. */
0826b30a 17071 length = attr->constant_value (0);
216a7e6b
AB
17072 }
17073 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17074 {
216a7e6b 17075 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17076 length = attr->constant_value (1);
c906108c
SS
17077 }
17078 else
17079 {
216a7e6b
AB
17080 /* Use 1 as a fallback length if we have nothing else. */
17081 length = 1;
c906108c 17082 }
6ccb9162 17083
46bf5051 17084 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17085 if (length_is_constant)
17086 range_type = create_static_range_type (NULL, index_type, 1, length);
17087 else
17088 {
17089 struct dynamic_prop low_bound;
17090
17091 low_bound.kind = PROP_CONST;
17092 low_bound.data.const_val = 1;
17093 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17094 }
3b7538c0
UW
17095 char_type = language_string_char_type (cu->language_defn, gdbarch);
17096 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17097
f792889a 17098 return set_die_type (die, type, cu);
c906108c
SS
17099}
17100
4d804846
JB
17101/* Assuming that DIE corresponds to a function, returns nonzero
17102 if the function is prototyped. */
17103
17104static int
17105prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17106{
17107 struct attribute *attr;
17108
17109 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17110 if (attr && (DW_UNSND (attr) != 0))
17111 return 1;
17112
17113 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17114 is only meaningful for C, but the concept also extends to other
4d804846
JB
17115 languages that allow unprototyped functions (Eg: Objective C).
17116 For all other languages, assume that functions are always
17117 prototyped. */
17118 if (cu->language != language_c
17119 && cu->language != language_objc
17120 && cu->language != language_opencl)
17121 return 1;
17122
17123 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17124 prototyped and unprototyped functions; default to prototyped,
17125 since that is more common in modern code (and RealView warns
17126 about unprototyped functions). */
17127 if (producer_is_realview (cu->producer))
17128 return 1;
17129
17130 return 0;
17131}
17132
c906108c
SS
17133/* Handle DIES due to C code like:
17134
17135 struct foo
c5aa993b
JM
17136 {
17137 int (*funcp)(int a, long l);
17138 int b;
17139 };
c906108c 17140
0963b4bd 17141 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17142
f792889a 17143static struct type *
e7c27a73 17144read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17145{
5e22e966 17146 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17147 struct type *type; /* Type that this function returns. */
17148 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17149 struct attribute *attr;
17150
e7c27a73 17151 type = die_type (die, cu);
7e314c57
JK
17152
17153 /* The die_type call above may have already set the type for this DIE. */
17154 ftype = get_die_type (die, cu);
17155 if (ftype)
17156 return ftype;
17157
0c8b41f1 17158 ftype = lookup_function_type (type);
c906108c 17159
4d804846 17160 if (prototyped_function_p (die, cu))
a6c727b2 17161 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17162
c055b101
CV
17163 /* Store the calling convention in the type if it's available in
17164 the subroutine die. Otherwise set the calling convention to
17165 the default value DW_CC_normal. */
17166 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17167 if (attr != nullptr
17168 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17169 TYPE_CALLING_CONVENTION (ftype)
17170 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17171 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17172 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17173 else
17174 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17175
743649fd
MW
17176 /* Record whether the function returns normally to its caller or not
17177 if the DWARF producer set that information. */
17178 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17179 if (attr && (DW_UNSND (attr) != 0))
17180 TYPE_NO_RETURN (ftype) = 1;
17181
76c10ea2
GM
17182 /* We need to add the subroutine type to the die immediately so
17183 we don't infinitely recurse when dealing with parameters
0963b4bd 17184 declared as the same subroutine type. */
76c10ea2 17185 set_die_type (die, ftype, cu);
6e70227d 17186
639d11d3 17187 if (die->child != NULL)
c906108c 17188 {
bb5ed363 17189 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17190 struct die_info *child_die;
8072405b 17191 int nparams, iparams;
c906108c
SS
17192
17193 /* Count the number of parameters.
17194 FIXME: GDB currently ignores vararg functions, but knows about
17195 vararg member functions. */
8072405b 17196 nparams = 0;
639d11d3 17197 child_die = die->child;
c906108c
SS
17198 while (child_die && child_die->tag)
17199 {
17200 if (child_die->tag == DW_TAG_formal_parameter)
17201 nparams++;
17202 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17203 TYPE_VARARGS (ftype) = 1;
436c571c 17204 child_die = child_die->sibling;
c906108c
SS
17205 }
17206
17207 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17208 ftype->set_num_fields (nparams);
3cabb6b0
SM
17209 ftype->set_fields
17210 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17211
8072405b
JK
17212 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17213 even if we error out during the parameters reading below. */
17214 for (iparams = 0; iparams < nparams; iparams++)
17215 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17216
17217 iparams = 0;
639d11d3 17218 child_die = die->child;
c906108c
SS
17219 while (child_die && child_die->tag)
17220 {
17221 if (child_die->tag == DW_TAG_formal_parameter)
17222 {
3ce3b1ba
PA
17223 struct type *arg_type;
17224
17225 /* DWARF version 2 has no clean way to discern C++
17226 static and non-static member functions. G++ helps
17227 GDB by marking the first parameter for non-static
17228 member functions (which is the this pointer) as
17229 artificial. We pass this information to
17230 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17231
17232 DWARF version 3 added DW_AT_object_pointer, which GCC
17233 4.5 does not yet generate. */
e142c38c 17234 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17235 if (attr != nullptr)
c906108c
SS
17236 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17237 else
9c37b5ae 17238 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17239 arg_type = die_type (child_die, cu);
17240
17241 /* RealView does not mark THIS as const, which the testsuite
17242 expects. GCC marks THIS as const in method definitions,
17243 but not in the class specifications (GCC PR 43053). */
17244 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17245 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17246 {
17247 int is_this = 0;
17248 struct dwarf2_cu *arg_cu = cu;
17249 const char *name = dwarf2_name (child_die, cu);
17250
17251 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17252 if (attr != nullptr)
3ce3b1ba
PA
17253 {
17254 /* If the compiler emits this, use it. */
17255 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17256 is_this = 1;
17257 }
17258 else if (name && strcmp (name, "this") == 0)
17259 /* Function definitions will have the argument names. */
17260 is_this = 1;
17261 else if (name == NULL && iparams == 0)
17262 /* Declarations may not have the names, so like
17263 elsewhere in GDB, assume an artificial first
17264 argument is "this". */
17265 is_this = 1;
17266
17267 if (is_this)
17268 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17269 arg_type, 0);
17270 }
17271
17272 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17273 iparams++;
17274 }
436c571c 17275 child_die = child_die->sibling;
c906108c
SS
17276 }
17277 }
17278
76c10ea2 17279 return ftype;
c906108c
SS
17280}
17281
f792889a 17282static struct type *
e7c27a73 17283read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17284{
5e22e966 17285 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17286 const char *name = NULL;
3c8e0968 17287 struct type *this_type, *target_type;
c906108c 17288
94af9270 17289 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17290 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17291 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17292 set_die_type (die, this_type, cu);
3c8e0968
DE
17293 target_type = die_type (die, cu);
17294 if (target_type != this_type)
17295 TYPE_TARGET_TYPE (this_type) = target_type;
17296 else
17297 {
17298 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17299 spec and cause infinite loops in GDB. */
b98664d3 17300 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17301 "- DIE at %s [in module %s]"),
17302 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17303 TYPE_TARGET_TYPE (this_type) = NULL;
17304 }
e4003a34
TV
17305 if (name == NULL)
17306 {
17307 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17308 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17309 Handle these by just returning the target type, rather than
17310 constructing an anonymous typedef type and trying to handle this
17311 elsewhere. */
17312 set_die_type (die, target_type, cu);
17313 return target_type;
17314 }
f792889a 17315 return this_type;
c906108c
SS
17316}
17317
9b790ce7
UW
17318/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17319 (which may be different from NAME) to the architecture back-end to allow
17320 it to guess the correct format if necessary. */
17321
17322static struct type *
17323dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17324 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17325{
08feed99 17326 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17327 const struct floatformat **format;
17328 struct type *type;
17329
17330 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17331 if (format)
103a685e 17332 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17333 else
77b7c781 17334 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17335
17336 return type;
17337}
17338
eb77c9df
AB
17339/* Allocate an integer type of size BITS and name NAME. */
17340
17341static struct type *
17342dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17343 int bits, int unsigned_p, const char *name)
17344{
17345 struct type *type;
17346
17347 /* Versions of Intel's C Compiler generate an integer type called "void"
17348 instead of using DW_TAG_unspecified_type. This has been seen on
17349 at least versions 14, 17, and 18. */
35ee2dc2
AB
17350 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17351 && strcmp (name, "void") == 0)
eb77c9df
AB
17352 type = objfile_type (objfile)->builtin_void;
17353 else
17354 type = init_integer_type (objfile, bits, unsigned_p, name);
17355
17356 return type;
17357}
17358
8bdc1658
AB
17359/* Initialise and return a floating point type of size BITS suitable for
17360 use as a component of a complex number. The NAME_HINT is passed through
17361 when initialising the floating point type and is the name of the complex
17362 type.
17363
17364 As DWARF doesn't currently provide an explicit name for the components
17365 of a complex number, but it can be helpful to have these components
17366 named, we try to select a suitable name based on the size of the
17367 component. */
17368static struct type *
17369dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17370 struct objfile *objfile,
103a685e
TT
17371 int bits, const char *name_hint,
17372 enum bfd_endian byte_order)
8bdc1658 17373{
08feed99 17374 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17375 struct type *tt = nullptr;
17376
35add35e
AB
17377 /* Try to find a suitable floating point builtin type of size BITS.
17378 We're going to use the name of this type as the name for the complex
17379 target type that we are about to create. */
1db455a7 17380 switch (cu->language)
8bdc1658 17381 {
1db455a7
AB
17382 case language_fortran:
17383 switch (bits)
17384 {
17385 case 32:
17386 tt = builtin_f_type (gdbarch)->builtin_real;
17387 break;
17388 case 64:
17389 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17390 break;
17391 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17392 case 128:
17393 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17394 break;
17395 }
8bdc1658 17396 break;
1db455a7
AB
17397 default:
17398 switch (bits)
17399 {
17400 case 32:
17401 tt = builtin_type (gdbarch)->builtin_float;
17402 break;
17403 case 64:
17404 tt = builtin_type (gdbarch)->builtin_double;
17405 break;
17406 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17407 case 128:
17408 tt = builtin_type (gdbarch)->builtin_long_double;
17409 break;
17410 }
8bdc1658
AB
17411 break;
17412 }
17413
35add35e
AB
17414 /* If the type we found doesn't match the size we were looking for, then
17415 pretend we didn't find a type at all, the complex target type we
17416 create will then be nameless. */
a12e5744 17417 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17418 tt = nullptr;
17419
7d93a1e0 17420 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17421 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17422}
17423
c906108c
SS
17424/* Find a representation of a given base type and install
17425 it in the TYPE field of the die. */
17426
f792889a 17427static struct type *
e7c27a73 17428read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17429{
5e22e966 17430 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17431 struct type *type;
17432 struct attribute *attr;
19f392bc 17433 int encoding = 0, bits = 0;
15d034d0 17434 const char *name;
34877895 17435 gdbarch *arch;
c906108c 17436
e142c38c 17437 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17438 if (attr != nullptr)
34877895 17439 encoding = DW_UNSND (attr);
e142c38c 17440 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17441 if (attr != nullptr)
34877895 17442 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17443 name = dwarf2_name (die, cu);
6ccb9162 17444 if (!name)
34877895 17445 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17446
08feed99 17447 arch = objfile->arch ();
103a685e
TT
17448 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17449
34877895
PJ
17450 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17451 if (attr)
103a685e
TT
17452 {
17453 int endianity = DW_UNSND (attr);
17454
17455 switch (endianity)
17456 {
17457 case DW_END_big:
17458 byte_order = BFD_ENDIAN_BIG;
17459 break;
17460 case DW_END_little:
17461 byte_order = BFD_ENDIAN_LITTLE;
17462 break;
17463 default:
17464 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17465 break;
17466 }
17467 }
6ccb9162
UW
17468
17469 switch (encoding)
c906108c 17470 {
6ccb9162
UW
17471 case DW_ATE_address:
17472 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17473 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17474 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17475 break;
17476 case DW_ATE_boolean:
19f392bc 17477 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17478 break;
17479 case DW_ATE_complex_float:
103a685e
TT
17480 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17481 byte_order);
78134374 17482 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17483 {
17484 if (name == nullptr)
17485 {
17486 struct obstack *obstack
5e22e966 17487 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17488 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17489 nullptr);
17490 }
17491 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17492 }
17493 else
17494 type = init_complex_type (name, type);
6ccb9162
UW
17495 break;
17496 case DW_ATE_decimal_float:
19f392bc 17497 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17498 break;
17499 case DW_ATE_float:
103a685e 17500 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17501 break;
17502 case DW_ATE_signed:
eb77c9df 17503 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17504 break;
17505 case DW_ATE_unsigned:
3b2b8fea
TT
17506 if (cu->language == language_fortran
17507 && name
61012eef 17508 && startswith (name, "character("))
19f392bc
UW
17509 type = init_character_type (objfile, bits, 1, name);
17510 else
eb77c9df 17511 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17512 break;
17513 case DW_ATE_signed_char:
6e70227d 17514 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17515 || cu->language == language_pascal
17516 || cu->language == language_fortran)
19f392bc
UW
17517 type = init_character_type (objfile, bits, 0, name);
17518 else
eb77c9df 17519 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17520 break;
17521 case DW_ATE_unsigned_char:
868a0084 17522 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17523 || cu->language == language_pascal
c44af4eb
TT
17524 || cu->language == language_fortran
17525 || cu->language == language_rust)
19f392bc
UW
17526 type = init_character_type (objfile, bits, 1, name);
17527 else
eb77c9df 17528 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17529 break;
75079b2b 17530 case DW_ATE_UTF:
53e710ac 17531 {
53e710ac
PA
17532 if (bits == 16)
17533 type = builtin_type (arch)->builtin_char16;
17534 else if (bits == 32)
17535 type = builtin_type (arch)->builtin_char32;
17536 else
17537 {
b98664d3 17538 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17539 bits);
eb77c9df 17540 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17541 }
17542 return set_die_type (die, type, cu);
17543 }
75079b2b
TT
17544 break;
17545
6ccb9162 17546 default:
b98664d3 17547 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17548 dwarf_type_encoding_name (encoding));
77b7c781 17549 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17550 break;
c906108c 17551 }
6ccb9162 17552
0114d602 17553 if (name && strcmp (name, "char") == 0)
876cecd0 17554 TYPE_NOSIGN (type) = 1;
0114d602 17555
2b4424c3
TT
17556 maybe_set_alignment (cu, die, type);
17557
103a685e 17558 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17559
f792889a 17560 return set_die_type (die, type, cu);
c906108c
SS
17561}
17562
80180f79
SA
17563/* Parse dwarf attribute if it's a block, reference or constant and put the
17564 resulting value of the attribute into struct bound_prop.
17565 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17566
17567static int
17568attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17569 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17570 struct type *default_type)
80180f79
SA
17571{
17572 struct dwarf2_property_baton *baton;
5e22e966 17573 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17574 struct objfile *objfile = per_objfile->objfile;
17575 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17576
9a49df9d
AB
17577 gdb_assert (default_type != NULL);
17578
80180f79
SA
17579 if (attr == NULL || prop == NULL)
17580 return 0;
17581
4fc6c0d5 17582 if (attr->form_is_block ())
80180f79 17583 {
8d749320 17584 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17585 baton->property_type = default_type;
80180f79 17586 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17587 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17588 baton->locexpr.size = DW_BLOCK (attr)->size;
17589 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17590 switch (attr->name)
17591 {
17592 case DW_AT_string_length:
17593 baton->locexpr.is_reference = true;
17594 break;
17595 default:
17596 baton->locexpr.is_reference = false;
17597 break;
17598 }
80180f79
SA
17599 prop->data.baton = baton;
17600 prop->kind = PROP_LOCEXPR;
17601 gdb_assert (prop->data.baton != NULL);
17602 }
cd6c91b4 17603 else if (attr->form_is_ref ())
80180f79
SA
17604 {
17605 struct dwarf2_cu *target_cu = cu;
17606 struct die_info *target_die;
17607 struct attribute *target_attr;
17608
17609 target_die = follow_die_ref (die, attr, &target_cu);
17610 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17611 if (target_attr == NULL)
17612 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17613 target_cu);
80180f79
SA
17614 if (target_attr == NULL)
17615 return 0;
17616
df25ebbd 17617 switch (target_attr->name)
80180f79 17618 {
df25ebbd 17619 case DW_AT_location:
cd6c91b4 17620 if (target_attr->form_is_section_offset ())
df25ebbd 17621 {
8d749320 17622 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17623 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17624 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17625 prop->data.baton = baton;
17626 prop->kind = PROP_LOCLIST;
17627 gdb_assert (prop->data.baton != NULL);
17628 }
4fc6c0d5 17629 else if (target_attr->form_is_block ())
df25ebbd 17630 {
8d749320 17631 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17632 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17633 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17634 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17635 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17636 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17637 baton->locexpr.is_reference = true;
df25ebbd
JB
17638 prop->data.baton = baton;
17639 prop->kind = PROP_LOCEXPR;
17640 gdb_assert (prop->data.baton != NULL);
17641 }
17642 else
17643 {
17644 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17645 "dynamic property");
17646 return 0;
17647 }
17648 break;
17649 case DW_AT_data_member_location:
17650 {
17651 LONGEST offset;
17652
17653 if (!handle_data_member_location (target_die, target_cu,
17654 &offset))
17655 return 0;
17656
8d749320 17657 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17658 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17659 target_cu);
df25ebbd
JB
17660 baton->offset_info.offset = offset;
17661 baton->offset_info.type = die_type (target_die, target_cu);
17662 prop->data.baton = baton;
17663 prop->kind = PROP_ADDR_OFFSET;
17664 break;
17665 }
80180f79
SA
17666 }
17667 }
cd6c91b4 17668 else if (attr->form_is_constant ())
80180f79 17669 {
0826b30a 17670 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17671 prop->kind = PROP_CONST;
17672 }
17673 else
17674 {
17675 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17676 dwarf2_name (die, cu));
17677 return 0;
17678 }
17679
17680 return 1;
17681}
17682
09ba997f 17683/* See read.h. */
9a49df9d 17684
09ba997f 17685struct type *
293e7e51 17686dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17687{
9a49df9d
AB
17688 struct type *int_type;
17689
17690 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17691#define TRY_TYPE(F) \
17692 int_type = (unsigned_p \
17693 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17694 : objfile_type (objfile)->builtin_ ## F); \
17695 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17696 return int_type
17697
17698 TRY_TYPE (char);
17699 TRY_TYPE (short);
17700 TRY_TYPE (int);
17701 TRY_TYPE (long);
17702 TRY_TYPE (long_long);
17703
17704#undef TRY_TYPE
17705
17706 gdb_assert_not_reached ("unable to find suitable integer type");
17707}
17708
09ba997f 17709/* See read.h. */
11a8b164 17710
09ba997f 17711struct type *
293e7e51 17712dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17713{
293e7e51
SM
17714 int addr_size = this->per_cu->addr_size ();
17715 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17716}
17717
b86352cf
AB
17718/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17719 present (which is valid) then compute the default type based on the
17720 compilation units address size. */
17721
17722static struct type *
17723read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17724{
17725 struct type *index_type = die_type (die, cu);
17726
17727 /* Dwarf-2 specifications explicitly allows to create subrange types
17728 without specifying a base type.
17729 In that case, the base type must be set to the type of
17730 the lower bound, upper bound or count, in that order, if any of these
17731 three attributes references an object that has a type.
17732 If no base type is found, the Dwarf-2 specifications say that
17733 a signed integer type of size equal to the size of an address should
17734 be used.
17735 For the following C code: `extern char gdb_int [];'
17736 GCC produces an empty range DIE.
17737 FIXME: muller/2010-05-28: Possible references to object for low bound,
17738 high bound or count are not yet handled by this code. */
78134374 17739 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17740 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17741
17742 return index_type;
17743}
17744
a02abb62
JB
17745/* Read the given DW_AT_subrange DIE. */
17746
f792889a 17747static struct type *
a02abb62
JB
17748read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17749{
4c9ad8c2 17750 struct type *base_type, *orig_base_type;
a02abb62
JB
17751 struct type *range_type;
17752 struct attribute *attr;
729efb13 17753 struct dynamic_prop low, high;
4fae6e18 17754 int low_default_is_valid;
c451ebe5 17755 int high_bound_is_count = 0;
15d034d0 17756 const char *name;
d359392f 17757 ULONGEST negative_mask;
e77813c8 17758
b86352cf
AB
17759 orig_base_type = read_subrange_index_type (die, cu);
17760
4c9ad8c2
TT
17761 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17762 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17763 creating the range type, but we use the result of check_typedef
17764 when examining properties of the type. */
17765 base_type = check_typedef (orig_base_type);
a02abb62 17766
7e314c57
JK
17767 /* The die_type call above may have already set the type for this DIE. */
17768 range_type = get_die_type (die, cu);
17769 if (range_type)
17770 return range_type;
17771
729efb13
SA
17772 low.kind = PROP_CONST;
17773 high.kind = PROP_CONST;
17774 high.data.const_val = 0;
17775
4fae6e18
JK
17776 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17777 omitting DW_AT_lower_bound. */
17778 switch (cu->language)
6e70227d 17779 {
4fae6e18
JK
17780 case language_c:
17781 case language_cplus:
729efb13 17782 low.data.const_val = 0;
4fae6e18
JK
17783 low_default_is_valid = 1;
17784 break;
17785 case language_fortran:
729efb13 17786 low.data.const_val = 1;
4fae6e18
JK
17787 low_default_is_valid = 1;
17788 break;
17789 case language_d:
4fae6e18 17790 case language_objc:
c44af4eb 17791 case language_rust:
729efb13 17792 low.data.const_val = 0;
4fae6e18
JK
17793 low_default_is_valid = (cu->header.version >= 4);
17794 break;
17795 case language_ada:
17796 case language_m2:
17797 case language_pascal:
729efb13 17798 low.data.const_val = 1;
4fae6e18
JK
17799 low_default_is_valid = (cu->header.version >= 4);
17800 break;
17801 default:
729efb13 17802 low.data.const_val = 0;
4fae6e18
JK
17803 low_default_is_valid = 0;
17804 break;
a02abb62
JB
17805 }
17806
e142c38c 17807 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17808 if (attr != nullptr)
9a49df9d 17809 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17810 else if (!low_default_is_valid)
b98664d3 17811 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17812 "- DIE at %s [in module %s]"),
17813 sect_offset_str (die->sect_off),
5e22e966 17814 objfile_name (cu->per_objfile->objfile));
a02abb62 17815
506f5c41
TV
17816 struct attribute *attr_ub, *attr_count;
17817 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17818 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17819 {
506f5c41 17820 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17821 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17822 {
c451ebe5
SA
17823 /* If bounds are constant do the final calculation here. */
17824 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17825 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17826 else
17827 high_bound_is_count = 1;
c2ff108b 17828 }
506f5c41
TV
17829 else
17830 {
17831 if (attr_ub != NULL)
17832 complaint (_("Unresolved DW_AT_upper_bound "
17833 "- DIE at %s [in module %s]"),
17834 sect_offset_str (die->sect_off),
5e22e966 17835 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17836 if (attr_count != NULL)
17837 complaint (_("Unresolved DW_AT_count "
17838 "- DIE at %s [in module %s]"),
17839 sect_offset_str (die->sect_off),
5e22e966 17840 objfile_name (cu->per_objfile->objfile));
506f5c41 17841 }
e77813c8 17842 }
a02abb62 17843
4e962e74
TT
17844 LONGEST bias = 0;
17845 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17846 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17847 bias = bias_attr->constant_value (0);
4e962e74 17848
dbb9c2b1
JB
17849 /* Normally, the DWARF producers are expected to use a signed
17850 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17851 But this is unfortunately not always the case, as witnessed
17852 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17853 is used instead. To work around that ambiguity, we treat
17854 the bounds as signed, and thus sign-extend their values, when
17855 the base type is signed. */
6e70227d 17856 negative_mask =
d359392f 17857 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17858 if (low.kind == PROP_CONST
17859 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17860 low.data.const_val |= negative_mask;
17861 if (high.kind == PROP_CONST
17862 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17863 high.data.const_val |= negative_mask;
43bbcdc2 17864
5bbd8269
AB
17865 /* Check for bit and byte strides. */
17866 struct dynamic_prop byte_stride_prop;
17867 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17868 if (attr_byte_stride != nullptr)
17869 {
293e7e51 17870 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17871 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17872 prop_type);
17873 }
17874
17875 struct dynamic_prop bit_stride_prop;
17876 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17877 if (attr_bit_stride != nullptr)
17878 {
17879 /* It only makes sense to have either a bit or byte stride. */
17880 if (attr_byte_stride != nullptr)
17881 {
17882 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17883 "- DIE at %s [in module %s]"),
17884 sect_offset_str (die->sect_off),
5e22e966 17885 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17886 attr_bit_stride = nullptr;
17887 }
17888 else
17889 {
293e7e51 17890 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17891 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17892 prop_type);
17893 }
17894 }
17895
17896 if (attr_byte_stride != nullptr
17897 || attr_bit_stride != nullptr)
17898 {
17899 bool byte_stride_p = (attr_byte_stride != nullptr);
17900 struct dynamic_prop *stride
17901 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17902
17903 range_type
17904 = create_range_type_with_stride (NULL, orig_base_type, &low,
17905 &high, bias, stride, byte_stride_p);
17906 }
17907 else
17908 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17909
c451ebe5
SA
17910 if (high_bound_is_count)
17911 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17912
c2ff108b
JK
17913 /* Ada expects an empty array on no boundary attributes. */
17914 if (attr == NULL && cu->language != language_ada)
729efb13 17915 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17916
39cbfefa
DJ
17917 name = dwarf2_name (die, cu);
17918 if (name)
d0e39ea2 17919 range_type->set_name (name);
6e70227d 17920
e142c38c 17921 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17922 if (attr != nullptr)
a02abb62
JB
17923 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17924
2b4424c3
TT
17925 maybe_set_alignment (cu, die, range_type);
17926
7e314c57
JK
17927 set_die_type (die, range_type, cu);
17928
17929 /* set_die_type should be already done. */
b4ba55a1
JB
17930 set_descriptive_type (range_type, die, cu);
17931
7e314c57 17932 return range_type;
a02abb62 17933}
6e70227d 17934
f792889a 17935static struct type *
81a17f79
JB
17936read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17937{
17938 struct type *type;
81a17f79 17939
5e22e966 17940 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17941 type->set_name (dwarf2_name (die, cu));
81a17f79 17942
74a2f8ff 17943 /* In Ada, an unspecified type is typically used when the description
85102364 17944 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17945 such a type, we treat it as a stub, and try to resolve it later on,
17946 when needed. */
17947 if (cu->language == language_ada)
17948 TYPE_STUB (type) = 1;
17949
f792889a 17950 return set_die_type (die, type, cu);
81a17f79 17951}
a02abb62 17952
639d11d3
DC
17953/* Read a single die and all its descendents. Set the die's sibling
17954 field to NULL; set other fields in the die correctly, and set all
17955 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17956 location of the info_ptr after reading all of those dies. PARENT
17957 is the parent of the die in question. */
17958
17959static struct die_info *
dee91e82 17960read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17961 const gdb_byte *info_ptr,
17962 const gdb_byte **new_info_ptr,
dee91e82 17963 struct die_info *parent)
639d11d3
DC
17964{
17965 struct die_info *die;
d521ce57 17966 const gdb_byte *cur_ptr;
639d11d3 17967
3e225074 17968 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17969 if (die == NULL)
17970 {
17971 *new_info_ptr = cur_ptr;
17972 return NULL;
17973 }
93311388 17974 store_in_ref_table (die, reader->cu);
639d11d3 17975
3e225074 17976 if (die->has_children)
bf6af496 17977 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17978 else
17979 {
17980 die->child = NULL;
17981 *new_info_ptr = cur_ptr;
17982 }
17983
17984 die->sibling = NULL;
17985 die->parent = parent;
17986 return die;
17987}
17988
17989/* Read a die, all of its descendents, and all of its siblings; set
17990 all of the fields of all of the dies correctly. Arguments are as
17991 in read_die_and_children. */
17992
17993static struct die_info *
bf6af496 17994read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17995 const gdb_byte *info_ptr,
17996 const gdb_byte **new_info_ptr,
bf6af496 17997 struct die_info *parent)
639d11d3
DC
17998{
17999 struct die_info *first_die, *last_sibling;
d521ce57 18000 const gdb_byte *cur_ptr;
639d11d3 18001
c906108c 18002 cur_ptr = info_ptr;
639d11d3
DC
18003 first_die = last_sibling = NULL;
18004
18005 while (1)
c906108c 18006 {
639d11d3 18007 struct die_info *die
dee91e82 18008 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18009
1d325ec1 18010 if (die == NULL)
c906108c 18011 {
639d11d3
DC
18012 *new_info_ptr = cur_ptr;
18013 return first_die;
c906108c 18014 }
1d325ec1
DJ
18015
18016 if (!first_die)
18017 first_die = die;
c906108c 18018 else
1d325ec1
DJ
18019 last_sibling->sibling = die;
18020
18021 last_sibling = die;
c906108c 18022 }
c906108c
SS
18023}
18024
bf6af496
DE
18025/* Read a die, all of its descendents, and all of its siblings; set
18026 all of the fields of all of the dies correctly. Arguments are as
18027 in read_die_and_children.
18028 This the main entry point for reading a DIE and all its children. */
18029
18030static struct die_info *
18031read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18032 const gdb_byte *info_ptr,
18033 const gdb_byte **new_info_ptr,
bf6af496
DE
18034 struct die_info *parent)
18035{
18036 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18037 new_info_ptr, parent);
18038
b4f54984 18039 if (dwarf_die_debug)
bf6af496
DE
18040 {
18041 fprintf_unfiltered (gdb_stdlog,
18042 "Read die from %s@0x%x of %s:\n",
96b79293 18043 reader->die_section->get_name (),
bf6af496
DE
18044 (unsigned) (info_ptr - reader->die_section->buffer),
18045 bfd_get_filename (reader->abfd));
b4f54984 18046 dump_die (die, dwarf_die_debug);
bf6af496
DE
18047 }
18048
18049 return die;
18050}
18051
3019eac3
DE
18052/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18053 attributes.
18054 The caller is responsible for filling in the extra attributes
18055 and updating (*DIEP)->num_attrs.
18056 Set DIEP to point to a newly allocated die with its information,
3e225074 18057 except for its child, sibling, and parent fields. */
93311388 18058
d521ce57 18059static const gdb_byte *
3019eac3 18060read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18061 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18062 int num_extra_attrs)
93311388 18063{
b64f50a1 18064 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18065 struct abbrev_info *abbrev;
18066 struct die_info *die;
18067 struct dwarf2_cu *cu = reader->cu;
18068 bfd *abfd = reader->abfd;
18069
9c541725 18070 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18071 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18072 info_ptr += bytes_read;
18073 if (!abbrev_number)
18074 {
18075 *diep = NULL;
93311388
DE
18076 return info_ptr;
18077 }
18078
685af9cd 18079 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18080 if (!abbrev)
348e048f
DE
18081 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18082 abbrev_number,
18083 bfd_get_filename (abfd));
18084
3019eac3 18085 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18086 die->sect_off = sect_off;
93311388
DE
18087 die->tag = abbrev->tag;
18088 die->abbrev = abbrev_number;
3e225074 18089 die->has_children = abbrev->has_children;
93311388 18090
3019eac3
DE
18091 /* Make the result usable.
18092 The caller needs to update num_attrs after adding the extra
18093 attributes. */
93311388
DE
18094 die->num_attrs = abbrev->num_attrs;
18095
18a8505e 18096 std::vector<int> indexes_that_need_reprocess;
93311388 18097 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18098 {
18099 bool need_reprocess;
18100 info_ptr =
18101 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18102 info_ptr, &need_reprocess);
18103 if (need_reprocess)
18104 indexes_that_need_reprocess.push_back (i);
18105 }
18106
052c8bb8 18107 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18108 if (attr != nullptr)
18109 cu->str_offsets_base = DW_UNSND (attr);
93311388 18110
41144253 18111 attr = die->attr (DW_AT_loclists_base);
18112 if (attr != nullptr)
18113 cu->loclist_base = DW_UNSND (attr);
18114
a39fdb41 18115 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18116 if (maybe_addr_base.has_value ())
18117 cu->addr_base = *maybe_addr_base;
18118 for (int index : indexes_that_need_reprocess)
18119 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18120 *diep = die;
93311388
DE
18121 return info_ptr;
18122}
18123
3019eac3
DE
18124/* Read a die and all its attributes.
18125 Set DIEP to point to a newly allocated die with its information,
3e225074 18126 except for its child, sibling, and parent fields. */
3019eac3 18127
d521ce57 18128static const gdb_byte *
3019eac3 18129read_full_die (const struct die_reader_specs *reader,
3e225074 18130 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18131{
d521ce57 18132 const gdb_byte *result;
bf6af496 18133
3e225074 18134 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18135
b4f54984 18136 if (dwarf_die_debug)
bf6af496
DE
18137 {
18138 fprintf_unfiltered (gdb_stdlog,
18139 "Read die from %s@0x%x of %s:\n",
96b79293 18140 reader->die_section->get_name (),
bf6af496
DE
18141 (unsigned) (info_ptr - reader->die_section->buffer),
18142 bfd_get_filename (reader->abfd));
b4f54984 18143 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18144 }
18145
18146 return result;
3019eac3 18147}
433df2d4 18148\f
c906108c 18149
72bf9492
DJ
18150/* Returns nonzero if TAG represents a type that we might generate a partial
18151 symbol for. */
18152
18153static int
18154is_type_tag_for_partial (int tag)
18155{
18156 switch (tag)
18157 {
18158#if 0
18159 /* Some types that would be reasonable to generate partial symbols for,
18160 that we don't at present. */
18161 case DW_TAG_array_type:
18162 case DW_TAG_file_type:
18163 case DW_TAG_ptr_to_member_type:
18164 case DW_TAG_set_type:
18165 case DW_TAG_string_type:
18166 case DW_TAG_subroutine_type:
18167#endif
18168 case DW_TAG_base_type:
18169 case DW_TAG_class_type:
680b30c7 18170 case DW_TAG_interface_type:
72bf9492
DJ
18171 case DW_TAG_enumeration_type:
18172 case DW_TAG_structure_type:
18173 case DW_TAG_subrange_type:
18174 case DW_TAG_typedef:
18175 case DW_TAG_union_type:
18176 return 1;
18177 default:
18178 return 0;
18179 }
18180}
18181
18182/* Load all DIEs that are interesting for partial symbols into memory. */
18183
18184static struct partial_die_info *
dee91e82 18185load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18186 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18187{
dee91e82 18188 struct dwarf2_cu *cu = reader->cu;
5e22e966 18189 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18190 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18191 unsigned int bytes_read;
5afb4e99 18192 unsigned int load_all = 0;
72bf9492
DJ
18193 int nesting_level = 1;
18194
18195 parent_die = NULL;
18196 last_die = NULL;
18197
7adf1e79
DE
18198 gdb_assert (cu->per_cu != NULL);
18199 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18200 load_all = 1;
18201
72bf9492
DJ
18202 cu->partial_dies
18203 = htab_create_alloc_ex (cu->header.length / 12,
18204 partial_die_hash,
18205 partial_die_eq,
18206 NULL,
18207 &cu->comp_unit_obstack,
18208 hashtab_obstack_allocate,
18209 dummy_obstack_deallocate);
18210
72bf9492
DJ
18211 while (1)
18212 {
685af9cd 18213 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18214
18215 /* A NULL abbrev means the end of a series of children. */
18216 if (abbrev == NULL)
18217 {
18218 if (--nesting_level == 0)
cd9983dd
YQ
18219 return first_die;
18220
72bf9492
DJ
18221 info_ptr += bytes_read;
18222 last_die = parent_die;
18223 parent_die = parent_die->die_parent;
18224 continue;
18225 }
18226
98bfdba5
PA
18227 /* Check for template arguments. We never save these; if
18228 they're seen, we just mark the parent, and go on our way. */
18229 if (parent_die != NULL
18230 && cu->language == language_cplus
18231 && (abbrev->tag == DW_TAG_template_type_param
18232 || abbrev->tag == DW_TAG_template_value_param))
18233 {
18234 parent_die->has_template_arguments = 1;
18235
18236 if (!load_all)
18237 {
18238 /* We don't need a partial DIE for the template argument. */
dee91e82 18239 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18240 continue;
18241 }
18242 }
18243
0d99eb77 18244 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18245 Skip their other children. */
18246 if (!load_all
18247 && cu->language == language_cplus
18248 && parent_die != NULL
18249 && parent_die->tag == DW_TAG_subprogram)
18250 {
dee91e82 18251 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18252 continue;
18253 }
18254
5afb4e99
DJ
18255 /* Check whether this DIE is interesting enough to save. Normally
18256 we would not be interested in members here, but there may be
18257 later variables referencing them via DW_AT_specification (for
18258 static members). */
18259 if (!load_all
18260 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18261 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18262 && abbrev->tag != DW_TAG_enumerator
18263 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18264 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18265 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18266 && abbrev->tag != DW_TAG_variable
5afb4e99 18267 && abbrev->tag != DW_TAG_namespace
f55ee35c 18268 && abbrev->tag != DW_TAG_module
95554aad 18269 && abbrev->tag != DW_TAG_member
74921315
KS
18270 && abbrev->tag != DW_TAG_imported_unit
18271 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18272 {
18273 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18274 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18275 continue;
18276 }
18277
6f06d47b
YQ
18278 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18279 abbrev);
cd9983dd 18280
48fbe735 18281 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18282
18283 /* This two-pass algorithm for processing partial symbols has a
18284 high cost in cache pressure. Thus, handle some simple cases
18285 here which cover the majority of C partial symbols. DIEs
18286 which neither have specification tags in them, nor could have
18287 specification tags elsewhere pointing at them, can simply be
18288 processed and discarded.
18289
18290 This segment is also optional; scan_partial_symbols and
18291 add_partial_symbol will handle these DIEs if we chain
18292 them in normally. When compilers which do not emit large
18293 quantities of duplicate debug information are more common,
18294 this code can probably be removed. */
18295
18296 /* Any complete simple types at the top level (pretty much all
18297 of them, for a language without namespaces), can be processed
18298 directly. */
18299 if (parent_die == NULL
cd9983dd
YQ
18300 && pdi.has_specification == 0
18301 && pdi.is_declaration == 0
18302 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18303 || pdi.tag == DW_TAG_base_type
18304 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18305 {
7d00ffec 18306 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
18307 add_partial_symbol (&pdi, cu);
18308
cd9983dd 18309 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18310 continue;
18311 }
18312
d8228535
JK
18313 /* The exception for DW_TAG_typedef with has_children above is
18314 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18315 type_name_or_error will error on such types later.
d8228535
JK
18316
18317 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18318 it could not find the child DIEs referenced later, this is checked
18319 above. In correct DWARF DW_TAG_typedef should have no children. */
18320
cd9983dd 18321 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18322 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18323 "- DIE at %s [in module %s]"),
cd9983dd 18324 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18325
72bf9492
DJ
18326 /* If we're at the second level, and we're an enumerator, and
18327 our parent has no specification (meaning possibly lives in a
18328 namespace elsewhere), then we can add the partial symbol now
18329 instead of queueing it. */
cd9983dd 18330 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18331 && parent_die != NULL
18332 && parent_die->die_parent == NULL
18333 && parent_die->tag == DW_TAG_enumeration_type
18334 && parent_die->has_specification == 0)
18335 {
7d00ffec 18336 if (pdi.raw_name == NULL)
b98664d3 18337 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18338 else if (building_psymtab)
f0fbb768 18339 add_partial_symbol (&pdi, cu);
72bf9492 18340
cd9983dd 18341 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18342 continue;
18343 }
18344
cd9983dd 18345 struct partial_die_info *part_die
6f06d47b 18346 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18347
72bf9492
DJ
18348 /* We'll save this DIE so link it in. */
18349 part_die->die_parent = parent_die;
18350 part_die->die_sibling = NULL;
18351 part_die->die_child = NULL;
18352
18353 if (last_die && last_die == parent_die)
18354 last_die->die_child = part_die;
18355 else if (last_die)
18356 last_die->die_sibling = part_die;
18357
18358 last_die = part_die;
18359
18360 if (first_die == NULL)
18361 first_die = part_die;
18362
18363 /* Maybe add the DIE to the hash table. Not all DIEs that we
18364 find interesting need to be in the hash table, because we
18365 also have the parent/sibling/child chains; only those that we
18366 might refer to by offset later during partial symbol reading.
18367
18368 For now this means things that might have be the target of a
18369 DW_AT_specification, DW_AT_abstract_origin, or
18370 DW_AT_extension. DW_AT_extension will refer only to
18371 namespaces; DW_AT_abstract_origin refers to functions (and
18372 many things under the function DIE, but we do not recurse
18373 into function DIEs during partial symbol reading) and
18374 possibly variables as well; DW_AT_specification refers to
18375 declarations. Declarations ought to have the DW_AT_declaration
18376 flag. It happens that GCC forgets to put it in sometimes, but
18377 only for functions, not for types.
18378
18379 Adding more things than necessary to the hash table is harmless
18380 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18381 wasted time in find_partial_die, when we reread the compilation
18382 unit with load_all_dies set. */
72bf9492 18383
5afb4e99 18384 if (load_all
72929c62 18385 || abbrev->tag == DW_TAG_constant
5afb4e99 18386 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18387 || abbrev->tag == DW_TAG_variable
18388 || abbrev->tag == DW_TAG_namespace
18389 || part_die->is_declaration)
18390 {
18391 void **slot;
18392
18393 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18394 to_underlying (part_die->sect_off),
18395 INSERT);
72bf9492
DJ
18396 *slot = part_die;
18397 }
18398
72bf9492 18399 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18400 we have no reason to follow the children of structures; for other
98bfdba5
PA
18401 languages we have to, so that we can get at method physnames
18402 to infer fully qualified class names, for DW_AT_specification,
18403 and for C++ template arguments. For C++, we also look one level
18404 inside functions to find template arguments (if the name of the
18405 function does not already contain the template arguments).
bc30ff58 18406
0a4b0913
AB
18407 For Ada and Fortran, we need to scan the children of subprograms
18408 and lexical blocks as well because these languages allow the
18409 definition of nested entities that could be interesting for the
18410 debugger, such as nested subprograms for instance. */
72bf9492 18411 if (last_die->has_children
5afb4e99
DJ
18412 && (load_all
18413 || last_die->tag == DW_TAG_namespace
f55ee35c 18414 || last_die->tag == DW_TAG_module
72bf9492 18415 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18416 || (cu->language == language_cplus
18417 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
18418 && (last_die->raw_name == NULL
18419 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
18420 || (cu->language != language_c
18421 && (last_die->tag == DW_TAG_class_type
680b30c7 18422 || last_die->tag == DW_TAG_interface_type
72bf9492 18423 || last_die->tag == DW_TAG_structure_type
bc30ff58 18424 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18425 || ((cu->language == language_ada
18426 || cu->language == language_fortran)
bc30ff58
JB
18427 && (last_die->tag == DW_TAG_subprogram
18428 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18429 {
18430 nesting_level++;
18431 parent_die = last_die;
18432 continue;
18433 }
18434
18435 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18436 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18437
18438 /* Back to the top, do it again. */
18439 }
18440}
18441
6f06d47b
YQ
18442partial_die_info::partial_die_info (sect_offset sect_off_,
18443 struct abbrev_info *abbrev)
18444 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18445{
18446}
18447
7d00ffec
TT
18448/* See class definition. */
18449
18450const char *
18451partial_die_info::name (dwarf2_cu *cu)
18452{
18453 if (!canonical_name && raw_name != nullptr)
18454 {
18455 struct objfile *objfile = cu->per_objfile->objfile;
18456 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18457 canonical_name = 1;
18458 }
18459
18460 return raw_name;
18461}
18462
35cc7ed7
YQ
18463/* Read a minimal amount of information into the minimal die structure.
18464 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18465
48fbe735
YQ
18466const gdb_byte *
18467partial_die_info::read (const struct die_reader_specs *reader,
18468 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18469{
dee91e82 18470 struct dwarf2_cu *cu = reader->cu;
976ca316 18471 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 18472 unsigned int i;
c5aa993b 18473 int has_low_pc_attr = 0;
c906108c 18474 int has_high_pc_attr = 0;
91da1414 18475 int high_pc_relative = 0;
c906108c 18476
fd0a254f 18477 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18478 {
e7da7f8f 18479 attribute attr;
18a8505e 18480 bool need_reprocess;
e7da7f8f 18481 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18482 info_ptr, &need_reprocess);
18483 /* String and address offsets that need to do the reprocessing have
18484 already been read at this point, so there is no need to wait until
18485 the loop terminates to do the reprocessing. */
18486 if (need_reprocess)
e7da7f8f 18487 read_attribute_reprocess (reader, &attr);
c906108c 18488 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18489 partial symbol table. */
c906108c
SS
18490 switch (attr.name)
18491 {
18492 case DW_AT_name:
48fbe735 18493 switch (tag)
71c25dea
TT
18494 {
18495 case DW_TAG_compile_unit:
95554aad 18496 case DW_TAG_partial_unit:
348e048f 18497 case DW_TAG_type_unit:
71c25dea
TT
18498 /* Compilation units have a DW_AT_name that is a filename, not
18499 a source language identifier. */
18500 case DW_TAG_enumeration_type:
18501 case DW_TAG_enumerator:
18502 /* These tags always have simple identifiers already; no need
18503 to canonicalize them. */
7d00ffec
TT
18504 canonical_name = 1;
18505 raw_name = DW_STRING (&attr);
71c25dea
TT
18506 break;
18507 default:
7d00ffec
TT
18508 canonical_name = 0;
18509 raw_name = DW_STRING (&attr);
71c25dea
TT
18510 break;
18511 }
c906108c 18512 break;
31ef98ae 18513 case DW_AT_linkage_name:
c906108c 18514 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18515 /* Note that both forms of linkage name might appear. We
18516 assume they will be the same, and we only store the last
18517 one we see. */
e61108c9 18518 linkage_name = attr.value_as_string ();
787de330
TT
18519 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18520 See https://github.com/rust-lang/rust/issues/32925. */
18521 if (cu->language == language_rust && linkage_name != NULL
18522 && strchr (linkage_name, '{') != NULL)
18523 linkage_name = NULL;
c906108c
SS
18524 break;
18525 case DW_AT_low_pc:
18526 has_low_pc_attr = 1;
cd6c91b4 18527 lowpc = attr.value_as_address ();
c906108c
SS
18528 break;
18529 case DW_AT_high_pc:
18530 has_high_pc_attr = 1;
cd6c91b4
TT
18531 highpc = attr.value_as_address ();
18532 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18533 high_pc_relative = 1;
c906108c
SS
18534 break;
18535 case DW_AT_location:
0963b4bd 18536 /* Support the .debug_loc offsets. */
4fc6c0d5 18537 if (attr.form_is_block ())
8e19ed76 18538 {
48fbe735 18539 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18540 }
cd6c91b4 18541 else if (attr.form_is_section_offset ())
8e19ed76 18542 {
4d3c2250 18543 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18544 }
18545 else
18546 {
4d3c2250
KB
18547 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18548 "partial symbol information");
8e19ed76 18549 }
c906108c 18550 break;
c906108c 18551 case DW_AT_external:
48fbe735 18552 is_external = DW_UNSND (&attr);
c906108c
SS
18553 break;
18554 case DW_AT_declaration:
48fbe735 18555 is_declaration = DW_UNSND (&attr);
c906108c
SS
18556 break;
18557 case DW_AT_type:
48fbe735 18558 has_type = 1;
c906108c
SS
18559 break;
18560 case DW_AT_abstract_origin:
18561 case DW_AT_specification:
72bf9492 18562 case DW_AT_extension:
48fbe735 18563 has_specification = 1;
0826b30a 18564 spec_offset = attr.get_ref_die_offset ();
48fbe735 18565 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18566 || cu->per_cu->is_dwz);
c906108c
SS
18567 break;
18568 case DW_AT_sibling:
18569 /* Ignore absolute siblings, they might point outside of
18570 the current compile unit. */
18571 if (attr.form == DW_FORM_ref_addr)
b98664d3 18572 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18573 else
b9502d3f 18574 {
48fbe735 18575 const gdb_byte *buffer = reader->buffer;
0826b30a 18576 sect_offset off = attr.get_ref_die_offset ();
9c541725 18577 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18578
18579 if (sibling_ptr < info_ptr)
b98664d3 18580 complaint (_("DW_AT_sibling points backwards"));
22869d73 18581 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18582 reader->die_section->overflow_complaint ();
b9502d3f 18583 else
48fbe735 18584 sibling = sibling_ptr;
b9502d3f 18585 }
c906108c 18586 break;
fa4028e9 18587 case DW_AT_byte_size:
48fbe735 18588 has_byte_size = 1;
fa4028e9 18589 break;
ff908ebf 18590 case DW_AT_const_value:
48fbe735 18591 has_const_value = 1;
ff908ebf 18592 break;
68511cec
CES
18593 case DW_AT_calling_convention:
18594 /* DWARF doesn't provide a way to identify a program's source-level
18595 entry point. DW_AT_calling_convention attributes are only meant
18596 to describe functions' calling conventions.
18597
18598 However, because it's a necessary piece of information in
0c1b455e
TT
18599 Fortran, and before DWARF 4 DW_CC_program was the only
18600 piece of debugging information whose definition refers to
18601 a 'main program' at all, several compilers marked Fortran
18602 main programs with DW_CC_program --- even when those
18603 functions use the standard calling conventions.
18604
18605 Although DWARF now specifies a way to provide this
18606 information, we support this practice for backward
18607 compatibility. */
68511cec 18608 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18609 && cu->language == language_fortran)
48fbe735 18610 main_subprogram = 1;
68511cec 18611 break;
481860b3
GB
18612 case DW_AT_inline:
18613 if (DW_UNSND (&attr) == DW_INL_inlined
18614 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18615 may_be_inlined = 1;
481860b3 18616 break;
95554aad
TT
18617
18618 case DW_AT_import:
48fbe735 18619 if (tag == DW_TAG_imported_unit)
36586728 18620 {
0826b30a 18621 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18622 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18623 || cu->per_cu->is_dwz);
18624 }
95554aad
TT
18625 break;
18626
0c1b455e 18627 case DW_AT_main_subprogram:
48fbe735 18628 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18629 break;
18630
05caa1d2
TT
18631 case DW_AT_ranges:
18632 {
18633 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18634 but that requires a full DIE, so instead we just
18635 reimplement it. */
18636 int need_ranges_base = tag != DW_TAG_compile_unit;
18637 unsigned int ranges_offset = (DW_UNSND (&attr)
18638 + (need_ranges_base
18639 ? cu->ranges_base
18640 : 0));
18641
18642 /* Value of the DW_AT_ranges attribute is the offset in the
18643 .debug_ranges section. */
18644 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18645 nullptr))
18646 has_pc_info = 1;
18647 }
18648 break;
18649
c906108c
SS
18650 default:
18651 break;
18652 }
18653 }
18654
10d06d82
TT
18655 /* For Ada, if both the name and the linkage name appear, we prefer
18656 the latter. This lets "catch exception" work better, regardless
18657 of the order in which the name and linkage name were emitted.
18658 Really, though, this is just a workaround for the fact that gdb
18659 doesn't store both the name and the linkage name. */
18660 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 18661 raw_name = linkage_name;
10d06d82 18662
91da1414 18663 if (high_pc_relative)
48fbe735 18664 highpc += lowpc;
91da1414 18665
9373cf26
JK
18666 if (has_low_pc_attr && has_high_pc_attr)
18667 {
18668 /* When using the GNU linker, .gnu.linkonce. sections are used to
18669 eliminate duplicate copies of functions and vtables and such.
18670 The linker will arbitrarily choose one and discard the others.
18671 The AT_*_pc values for such functions refer to local labels in
18672 these sections. If the section from that file was discarded, the
18673 labels are not in the output, so the relocs get a value of 0.
18674 If this is a discarded function, mark the pc bounds as invalid,
18675 so that GDB will ignore it. */
976ca316 18676 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 18677 {
976ca316 18678 struct objfile *objfile = per_objfile->objfile;
08feed99 18679 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18680
b98664d3 18681 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18682 "for DIE at %s [in module %s]"),
48fbe735
YQ
18683 paddress (gdbarch, lowpc),
18684 sect_offset_str (sect_off),
9d8780f0 18685 objfile_name (objfile));
9373cf26
JK
18686 }
18687 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18688 else if (lowpc >= highpc)
9373cf26 18689 {
976ca316 18690 struct objfile *objfile = per_objfile->objfile;
08feed99 18691 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18692
b98664d3 18693 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18694 "for DIE at %s [in module %s]"),
48fbe735
YQ
18695 paddress (gdbarch, lowpc),
18696 paddress (gdbarch, highpc),
18697 sect_offset_str (sect_off),
9c541725 18698 objfile_name (objfile));
9373cf26
JK
18699 }
18700 else
48fbe735 18701 has_pc_info = 1;
9373cf26 18702 }
85cbf3d3 18703
c906108c
SS
18704 return info_ptr;
18705}
18706
72bf9492
DJ
18707/* Find a cached partial DIE at OFFSET in CU. */
18708
d590ff25
YQ
18709struct partial_die_info *
18710dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18711{
18712 struct partial_die_info *lookup_die = NULL;
6f06d47b 18713 struct partial_die_info part_die (sect_off);
72bf9492 18714
9a3c8263 18715 lookup_die = ((struct partial_die_info *)
d590ff25 18716 htab_find_with_hash (partial_dies, &part_die,
9c541725 18717 to_underlying (sect_off)));
72bf9492 18718
72bf9492
DJ
18719 return lookup_die;
18720}
18721
348e048f
DE
18722/* Find a partial DIE at OFFSET, which may or may not be in CU,
18723 except in the case of .debug_types DIEs which do not reference
18724 outside their CU (they do however referencing other types via
55f1336d 18725 DW_FORM_ref_sig8). */
72bf9492 18726
122cf0f2 18727static const struct cu_partial_die_info
9c541725 18728find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18729{
976ca316
SM
18730 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18731 struct objfile *objfile = per_objfile->objfile;
5afb4e99 18732 struct partial_die_info *pd = NULL;
72bf9492 18733
36586728 18734 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18735 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18736 {
d590ff25 18737 pd = cu->find_partial_die (sect_off);
5afb4e99 18738 if (pd != NULL)
fb816e8b 18739 return { cu, pd };
0d99eb77
DE
18740 /* We missed recording what we needed.
18741 Load all dies and try again. */
5afb4e99 18742 }
0d99eb77
DE
18743 else
18744 {
18745 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18746 if (cu->per_cu->is_debug_types)
0d99eb77 18747 {
9d8780f0
SM
18748 error (_("Dwarf Error: Type Unit at offset %s contains"
18749 " external reference to offset %s [in module %s].\n"),
18750 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18751 bfd_get_filename (objfile->obfd));
18752 }
7188ed02
SM
18753 dwarf2_per_cu_data *per_cu
18754 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 18755 per_objfile);
72bf9492 18756
976ca316 18757 cu = per_objfile->get_cu (per_cu);
7188ed02 18758 if (cu == NULL || cu->partial_dies == NULL)
976ca316 18759 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 18760
976ca316 18761 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
18762
18763 cu->last_used = 0;
18764 pd = cu->find_partial_die (sect_off);
0d99eb77 18765 }
5afb4e99 18766
dee91e82
DE
18767 /* If we didn't find it, and not all dies have been loaded,
18768 load them all and try again. */
18769
7188ed02 18770 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 18771 {
7188ed02 18772 cu->per_cu->load_all_dies = 1;
fd820528
DE
18773
18774 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18775 THIS_CU->cu may already be in use. So we can't just free it and
18776 replace its DIEs with the ones we read in. Instead, we leave those
18777 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18778 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18779 set. */
976ca316 18780 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 18781
7188ed02 18782 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18783 }
18784
18785 if (pd == NULL)
18786 internal_error (__FILE__, __LINE__,
9d8780f0 18787 _("could not find partial DIE %s "
3e43a32a 18788 "in cache [from module %s]\n"),
9d8780f0 18789 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 18790 return { cu, pd };
72bf9492
DJ
18791}
18792
abc72ce4
DE
18793/* See if we can figure out if the class lives in a namespace. We do
18794 this by looking for a member function; its demangled name will
18795 contain namespace info, if there is any. */
18796
18797static void
18798guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18799 struct dwarf2_cu *cu)
18800{
18801 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18802 what template types look like, because the demangler
18803 frequently doesn't give the same name as the debug info. We
18804 could fix this by only using the demangled name to get the
18805 prefix (but see comment in read_structure_type). */
18806
18807 struct partial_die_info *real_pdi;
18808 struct partial_die_info *child_pdi;
18809
18810 /* If this DIE (this DIE's specification, if any) has a parent, then
18811 we should not do this. We'll prepend the parent's fully qualified
18812 name when we create the partial symbol. */
18813
18814 real_pdi = struct_pdi;
18815 while (real_pdi->has_specification)
fb816e8b 18816 {
122cf0f2
AB
18817 auto res = find_partial_die (real_pdi->spec_offset,
18818 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18819 real_pdi = res.pdi;
18820 cu = res.cu;
18821 }
abc72ce4
DE
18822
18823 if (real_pdi->die_parent != NULL)
18824 return;
18825
18826 for (child_pdi = struct_pdi->die_child;
18827 child_pdi != NULL;
18828 child_pdi = child_pdi->die_sibling)
18829 {
18830 if (child_pdi->tag == DW_TAG_subprogram
18831 && child_pdi->linkage_name != NULL)
18832 {
43816ebc
TT
18833 gdb::unique_xmalloc_ptr<char> actual_class_name
18834 (language_class_name_from_physname (cu->language_defn,
18835 child_pdi->linkage_name));
abc72ce4
DE
18836 if (actual_class_name != NULL)
18837 {
5e22e966 18838 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
18839 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
18840 struct_pdi->canonical_name = 1;
abc72ce4
DE
18841 }
18842 break;
18843 }
18844 }
18845}
18846
25c11aca
TV
18847/* Return true if a DIE with TAG may have the DW_AT_const_value
18848 attribute. */
18849
18850static bool
18851can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18852{
18853 switch (tag)
18854 {
18855 case DW_TAG_constant:
18856 case DW_TAG_enumerator:
18857 case DW_TAG_formal_parameter:
18858 case DW_TAG_template_value_param:
18859 case DW_TAG_variable:
18860 return true;
18861 }
18862
18863 return false;
18864}
18865
52356b79
YQ
18866void
18867partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18868{
abc72ce4
DE
18869 /* Once we've fixed up a die, there's no point in doing so again.
18870 This also avoids a memory leak if we were to call
18871 guess_partial_die_structure_name multiple times. */
52356b79 18872 if (fixup_called)
abc72ce4
DE
18873 return;
18874
72bf9492
DJ
18875 /* If we found a reference attribute and the DIE has no name, try
18876 to find a name in the referred to DIE. */
18877
7d00ffec 18878 if (raw_name == NULL && has_specification)
72bf9492
DJ
18879 {
18880 struct partial_die_info *spec_die;
72bf9492 18881
122cf0f2 18882 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18883 spec_die = res.pdi;
18884 cu = res.cu;
72bf9492 18885
52356b79 18886 spec_die->fixup (cu);
72bf9492 18887
7d00ffec 18888 if (spec_die->raw_name)
72bf9492 18889 {
7d00ffec
TT
18890 raw_name = spec_die->raw_name;
18891 canonical_name = spec_die->canonical_name;
72bf9492
DJ
18892
18893 /* Copy DW_AT_external attribute if it is set. */
18894 if (spec_die->is_external)
52356b79 18895 is_external = spec_die->is_external;
72bf9492
DJ
18896 }
18897 }
18898
25c11aca
TV
18899 if (!has_const_value && has_specification
18900 && can_have_DW_AT_const_value_p (tag))
18901 {
18902 struct partial_die_info *spec_die;
18903
18904 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18905 spec_die = res.pdi;
18906 cu = res.cu;
18907
18908 spec_die->fixup (cu);
18909
18910 if (spec_die->has_const_value)
18911 {
18912 /* Copy DW_AT_const_value attribute if it is set. */
18913 has_const_value = spec_die->has_const_value;
18914 }
18915 }
18916
72bf9492 18917 /* Set default names for some unnamed DIEs. */
72bf9492 18918
7d00ffec
TT
18919 if (raw_name == NULL && tag == DW_TAG_namespace)
18920 {
18921 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
18922 canonical_name = 1;
18923 }
72bf9492 18924
abc72ce4
DE
18925 /* If there is no parent die to provide a namespace, and there are
18926 children, see if we can determine the namespace from their linkage
122d1940 18927 name. */
abc72ce4 18928 if (cu->language == language_cplus
5e22e966 18929 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18930 && die_parent == NULL
18931 && has_children
18932 && (tag == DW_TAG_class_type
18933 || tag == DW_TAG_structure_type
18934 || tag == DW_TAG_union_type))
18935 guess_partial_die_structure_name (this, cu);
abc72ce4 18936
53832f31
TT
18937 /* GCC might emit a nameless struct or union that has a linkage
18938 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 18939 if (raw_name == NULL
52356b79
YQ
18940 && (tag == DW_TAG_class_type
18941 || tag == DW_TAG_interface_type
18942 || tag == DW_TAG_structure_type
18943 || tag == DW_TAG_union_type)
18944 && linkage_name != NULL)
53832f31 18945 {
43816ebc
TT
18946 gdb::unique_xmalloc_ptr<char> demangled
18947 (gdb_demangle (linkage_name, DMGL_TYPES));
18948 if (demangled != nullptr)
53832f31 18949 {
96408a79
SA
18950 const char *base;
18951
18952 /* Strip any leading namespaces/classes, keep only the base name.
18953 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18954 base = strrchr (demangled.get (), ':');
18955 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18956 base++;
18957 else
43816ebc 18958 base = demangled.get ();
96408a79 18959
5e22e966 18960 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
18961 raw_name = objfile->intern (base);
18962 canonical_name = 1;
53832f31
TT
18963 }
18964 }
18965
52356b79 18966 fixup_called = 1;
72bf9492
DJ
18967}
18968
41144253 18969/* Read the .debug_loclists header contents from the given SECTION in the
18970 HEADER. */
18971static void
18972read_loclist_header (struct loclist_header *header,
18973 struct dwarf2_section_info *section)
18974{
18975 unsigned int bytes_read;
18976 bfd *abfd = section->get_bfd_owner ();
18977 const gdb_byte *info_ptr = section->buffer;
18978 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18979 info_ptr += bytes_read;
18980 header->version = read_2_bytes (abfd, info_ptr);
18981 info_ptr += 2;
18982 header->addr_size = read_1_byte (abfd, info_ptr);
18983 info_ptr += 1;
18984 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18985 info_ptr += 1;
18986 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18987}
18988
18989/* Return the DW_AT_loclists_base value for the CU. */
18990static ULONGEST
18991lookup_loclist_base (struct dwarf2_cu *cu)
18992{
18993 /* For the .dwo unit, the loclist_base points to the first offset following
18994 the header. The header consists of the following entities-
18995 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18996 bit format)
18997 2. version (2 bytes)
18998 3. address size (1 byte)
18999 4. segment selector size (1 byte)
19000 5. offset entry count (4 bytes)
19001 These sizes are derived as per the DWARFv5 standard. */
19002 if (cu->dwo_unit != nullptr)
19003 {
19004 if (cu->header.initial_length_size == 4)
19005 return LOCLIST_HEADER_SIZE32;
19006 return LOCLIST_HEADER_SIZE64;
19007 }
19008 return cu->loclist_base;
19009}
19010
19011/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19012 array of offsets in the .debug_loclists section. */
19013static CORE_ADDR
19014read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19015{
976ca316
SM
19016 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19017 struct objfile *objfile = per_objfile->objfile;
41144253 19018 bfd *abfd = objfile->obfd;
19019 ULONGEST loclist_base = lookup_loclist_base (cu);
19020 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19021
19022 section->read (objfile);
19023 if (section->buffer == NULL)
19024 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19025 "section [in module %s]"), objfile_name (objfile));
19026 struct loclist_header header;
19027 read_loclist_header (&header, section);
19028 if (loclist_index >= header.offset_entry_count)
19029 complaint (_("DW_FORM_loclistx pointing outside of "
19030 ".debug_loclists offset array [in module %s]"),
19031 objfile_name (objfile));
19032 if (loclist_base + loclist_index * cu->header.offset_size
19033 >= section->size)
19034 complaint (_("DW_FORM_loclistx pointing outside of "
19035 ".debug_loclists section [in module %s]"),
19036 objfile_name (objfile));
19037 const gdb_byte *info_ptr
19038 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19039
19040 if (cu->header.offset_size == 4)
19041 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19042 else
19043 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19044}
19045
18a8505e
AT
19046/* Process the attributes that had to be skipped in the first round. These
19047 attributes are the ones that need str_offsets_base or addr_base attributes.
19048 They could not have been processed in the first round, because at the time
19049 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19050static void
19051read_attribute_reprocess (const struct die_reader_specs *reader,
19052 struct attribute *attr)
18a8505e
AT
19053{
19054 struct dwarf2_cu *cu = reader->cu;
19055 switch (attr->form)
19056 {
19057 case DW_FORM_addrx:
19058 case DW_FORM_GNU_addr_index:
19059 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19060 break;
41144253 19061 case DW_FORM_loclistx:
19062 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19063 break;
18a8505e
AT
19064 case DW_FORM_strx:
19065 case DW_FORM_strx1:
19066 case DW_FORM_strx2:
19067 case DW_FORM_strx3:
19068 case DW_FORM_strx4:
19069 case DW_FORM_GNU_str_index:
19070 {
19071 unsigned int str_index = DW_UNSND (attr);
19072 if (reader->dwo_file != NULL)
19073 {
19074 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19075 DW_STRING_IS_CANONICAL (attr) = 0;
19076 }
19077 else
19078 {
19079 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19080 DW_STRING_IS_CANONICAL (attr) = 0;
19081 }
19082 break;
19083 }
19084 default:
19085 gdb_assert_not_reached (_("Unexpected DWARF form."));
19086 }
19087}
19088
a8329558 19089/* Read an attribute value described by an attribute form. */
c906108c 19090
d521ce57 19091static const gdb_byte *
dee91e82
DE
19092read_attribute_value (const struct die_reader_specs *reader,
19093 struct attribute *attr, unsigned form,
18a8505e
AT
19094 LONGEST implicit_const, const gdb_byte *info_ptr,
19095 bool *need_reprocess)
c906108c 19096{
dee91e82 19097 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19098 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19099 struct objfile *objfile = per_objfile->objfile;
dee91e82 19100 bfd *abfd = reader->abfd;
e7c27a73 19101 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19102 unsigned int bytes_read;
19103 struct dwarf_block *blk;
18a8505e 19104 *need_reprocess = false;
c906108c 19105
aead7601 19106 attr->form = (enum dwarf_form) form;
a8329558 19107 switch (form)
c906108c 19108 {
c906108c 19109 case DW_FORM_ref_addr:
ae411497 19110 if (cu->header.version == 2)
c8a7a66f
TT
19111 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19112 &bytes_read);
ae411497 19113 else
8266302d
TT
19114 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19115 &bytes_read);
ae411497
TT
19116 info_ptr += bytes_read;
19117 break;
36586728 19118 case DW_FORM_GNU_ref_alt:
8266302d 19119 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19120 info_ptr += bytes_read;
19121 break;
ae411497 19122 case DW_FORM_addr:
08feed99
TT
19123 {
19124 struct gdbarch *gdbarch = objfile->arch ();
19125 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19126 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19127 info_ptr += bytes_read;
19128 }
c906108c
SS
19129 break;
19130 case DW_FORM_block2:
7b5a2f43 19131 blk = dwarf_alloc_block (cu);
c906108c
SS
19132 blk->size = read_2_bytes (abfd, info_ptr);
19133 info_ptr += 2;
19134 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19135 info_ptr += blk->size;
19136 DW_BLOCK (attr) = blk;
19137 break;
19138 case DW_FORM_block4:
7b5a2f43 19139 blk = dwarf_alloc_block (cu);
c906108c
SS
19140 blk->size = read_4_bytes (abfd, info_ptr);
19141 info_ptr += 4;
19142 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19143 info_ptr += blk->size;
19144 DW_BLOCK (attr) = blk;
19145 break;
19146 case DW_FORM_data2:
19147 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19148 info_ptr += 2;
19149 break;
19150 case DW_FORM_data4:
19151 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19152 info_ptr += 4;
19153 break;
19154 case DW_FORM_data8:
19155 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19156 info_ptr += 8;
19157 break;
0224619f
JK
19158 case DW_FORM_data16:
19159 blk = dwarf_alloc_block (cu);
19160 blk->size = 16;
19161 blk->data = read_n_bytes (abfd, info_ptr, 16);
19162 info_ptr += 16;
19163 DW_BLOCK (attr) = blk;
19164 break;
2dc7f7b3 19165 case DW_FORM_sec_offset:
8266302d 19166 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19167 info_ptr += bytes_read;
19168 break;
41144253 19169 case DW_FORM_loclistx:
19170 {
19171 *need_reprocess = true;
19172 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19173 info_ptr += bytes_read;
19174 }
19175 break;
c906108c 19176 case DW_FORM_string:
9b1c24c8 19177 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19178 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19179 info_ptr += bytes_read;
19180 break;
4bdf3d34 19181 case DW_FORM_strp:
36586728
TT
19182 if (!cu->per_cu->is_dwz)
19183 {
976ca316 19184 DW_STRING (attr) = read_indirect_string (per_objfile,
ed2dc618 19185 abfd, info_ptr, cu_header,
36586728
TT
19186 &bytes_read);
19187 DW_STRING_IS_CANONICAL (attr) = 0;
19188 info_ptr += bytes_read;
19189 break;
19190 }
19191 /* FALLTHROUGH */
43988095
JK
19192 case DW_FORM_line_strp:
19193 if (!cu->per_cu->is_dwz)
19194 {
976ca316
SM
19195 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19196 &bytes_read);
43988095
JK
19197 DW_STRING_IS_CANONICAL (attr) = 0;
19198 info_ptr += bytes_read;
19199 break;
19200 }
19201 /* FALLTHROUGH */
36586728
TT
19202 case DW_FORM_GNU_strp_alt:
19203 {
976ca316 19204 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
19205 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19206 &bytes_read);
36586728 19207
0314b390 19208 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19209 DW_STRING_IS_CANONICAL (attr) = 0;
19210 info_ptr += bytes_read;
19211 }
4bdf3d34 19212 break;
2dc7f7b3 19213 case DW_FORM_exprloc:
c906108c 19214 case DW_FORM_block:
7b5a2f43 19215 blk = dwarf_alloc_block (cu);
c906108c
SS
19216 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19217 info_ptr += bytes_read;
19218 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19219 info_ptr += blk->size;
19220 DW_BLOCK (attr) = blk;
19221 break;
19222 case DW_FORM_block1:
7b5a2f43 19223 blk = dwarf_alloc_block (cu);
c906108c
SS
19224 blk->size = read_1_byte (abfd, info_ptr);
19225 info_ptr += 1;
19226 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19227 info_ptr += blk->size;
19228 DW_BLOCK (attr) = blk;
19229 break;
19230 case DW_FORM_data1:
19231 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19232 info_ptr += 1;
19233 break;
19234 case DW_FORM_flag:
19235 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19236 info_ptr += 1;
19237 break;
2dc7f7b3
TT
19238 case DW_FORM_flag_present:
19239 DW_UNSND (attr) = 1;
19240 break;
c906108c
SS
19241 case DW_FORM_sdata:
19242 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19243 info_ptr += bytes_read;
19244 break;
19245 case DW_FORM_udata:
18a8505e 19246 case DW_FORM_rnglistx:
c906108c
SS
19247 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19248 info_ptr += bytes_read;
19249 break;
19250 case DW_FORM_ref1:
9c541725 19251 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19252 + read_1_byte (abfd, info_ptr));
c906108c
SS
19253 info_ptr += 1;
19254 break;
19255 case DW_FORM_ref2:
9c541725 19256 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19257 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19258 info_ptr += 2;
19259 break;
19260 case DW_FORM_ref4:
9c541725 19261 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19262 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19263 info_ptr += 4;
19264 break;
613e1657 19265 case DW_FORM_ref8:
9c541725 19266 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19267 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19268 info_ptr += 8;
19269 break;
55f1336d 19270 case DW_FORM_ref_sig8:
ac9ec31b 19271 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19272 info_ptr += 8;
19273 break;
c906108c 19274 case DW_FORM_ref_udata:
9c541725 19275 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19276 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19277 info_ptr += bytes_read;
19278 break;
c906108c 19279 case DW_FORM_indirect:
a8329558
KW
19280 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19281 info_ptr += bytes_read;
43988095
JK
19282 if (form == DW_FORM_implicit_const)
19283 {
19284 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19285 info_ptr += bytes_read;
19286 }
19287 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19288 info_ptr, need_reprocess);
43988095
JK
19289 break;
19290 case DW_FORM_implicit_const:
19291 DW_SND (attr) = implicit_const;
a8329558 19292 break;
336d760d 19293 case DW_FORM_addrx:
3019eac3 19294 case DW_FORM_GNU_addr_index:
18a8505e
AT
19295 *need_reprocess = true;
19296 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19297 info_ptr += bytes_read;
19298 break;
cf532bd1 19299 case DW_FORM_strx:
15f18d14
AT
19300 case DW_FORM_strx1:
19301 case DW_FORM_strx2:
19302 case DW_FORM_strx3:
19303 case DW_FORM_strx4:
3019eac3 19304 case DW_FORM_GNU_str_index:
3019eac3 19305 {
15f18d14
AT
19306 ULONGEST str_index;
19307 if (form == DW_FORM_strx1)
19308 {
19309 str_index = read_1_byte (abfd, info_ptr);
19310 info_ptr += 1;
19311 }
19312 else if (form == DW_FORM_strx2)
19313 {
19314 str_index = read_2_bytes (abfd, info_ptr);
19315 info_ptr += 2;
19316 }
19317 else if (form == DW_FORM_strx3)
19318 {
19319 str_index = read_3_bytes (abfd, info_ptr);
19320 info_ptr += 3;
19321 }
19322 else if (form == DW_FORM_strx4)
19323 {
19324 str_index = read_4_bytes (abfd, info_ptr);
19325 info_ptr += 4;
19326 }
19327 else
19328 {
19329 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19330 info_ptr += bytes_read;
19331 }
18a8505e
AT
19332 *need_reprocess = true;
19333 DW_UNSND (attr) = str_index;
19334 }
3019eac3 19335 break;
c906108c 19336 default:
8a3fe4f8 19337 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19338 dwarf_form_name (form),
19339 bfd_get_filename (abfd));
c906108c 19340 }
28e94949 19341
36586728 19342 /* Super hack. */
cd6c91b4 19343 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19344 attr->form = DW_FORM_GNU_ref_alt;
19345
28e94949
JB
19346 /* We have seen instances where the compiler tried to emit a byte
19347 size attribute of -1 which ended up being encoded as an unsigned
19348 0xffffffff. Although 0xffffffff is technically a valid size value,
19349 an object of this size seems pretty unlikely so we can relatively
19350 safely treat these cases as if the size attribute was invalid and
19351 treat them as zero by default. */
19352 if (attr->name == DW_AT_byte_size
19353 && form == DW_FORM_data4
19354 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19355 {
19356 complaint
b98664d3 19357 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19358 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19359 DW_UNSND (attr) = 0;
19360 }
28e94949 19361
c906108c
SS
19362 return info_ptr;
19363}
19364
a8329558
KW
19365/* Read an attribute described by an abbreviated attribute. */
19366
d521ce57 19367static const gdb_byte *
dee91e82
DE
19368read_attribute (const struct die_reader_specs *reader,
19369 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19370 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19371{
19372 attr->name = abbrev->name;
43988095 19373 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19374 abbrev->implicit_const, info_ptr,
19375 need_reprocess);
a8329558
KW
19376}
19377
43988095
JK
19378/* Return pointer to string at .debug_str offset STR_OFFSET. */
19379
19380static const char *
976ca316 19381read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 19382 LONGEST str_offset)
43988095 19383{
976ca316
SM
19384 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19385 str_offset, "DW_FORM_strp");
c906108c
SS
19386}
19387
43988095
JK
19388/* Return pointer to string at .debug_str offset as read from BUF.
19389 BUF is assumed to be in a compilation unit described by CU_HEADER.
19390 Return *BYTES_READ_PTR count of bytes read from BUF. */
19391
d521ce57 19392static const char *
976ca316 19393read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 19394 const gdb_byte *buf,
cf2c3c16
TT
19395 const struct comp_unit_head *cu_header,
19396 unsigned int *bytes_read_ptr)
19397{
8266302d 19398 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19399
976ca316 19400 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
19401}
19402
86c0bb4c 19403/* See read.h. */
43988095 19404
86c0bb4c
TT
19405const char *
19406dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19407 const struct comp_unit_head *cu_header,
19408 unsigned int *bytes_read_ptr)
43988095 19409{
86c0bb4c 19410 bfd *abfd = objfile->obfd;
8266302d 19411 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19412
5989a64e 19413 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19414}
19415
3019eac3 19416/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19417 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19418 ADDR_SIZE is the size of addresses from the CU header. */
19419
19420static CORE_ADDR
976ca316
SM
19421read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19422 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 19423{
976ca316 19424 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
19425 bfd *abfd = objfile->obfd;
19426 const gdb_byte *info_ptr;
18a8505e 19427 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19428
976ca316
SM
19429 per_objfile->per_bfd->addr.read (objfile);
19430 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19431 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19432 objfile_name (objfile));
18a8505e 19433 if (addr_base_or_zero + addr_index * addr_size
976ca316 19434 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
19435 error (_("DW_FORM_addr_index pointing outside of "
19436 ".debug_addr section [in module %s]"),
4262abfb 19437 objfile_name (objfile));
976ca316
SM
19438 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19439 + addr_index * addr_size);
3019eac3
DE
19440 if (addr_size == 4)
19441 return bfd_get_32 (abfd, info_ptr);
19442 else
19443 return bfd_get_64 (abfd, info_ptr);
19444}
19445
19446/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19447
19448static CORE_ADDR
19449read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19450{
5e22e966 19451 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19452 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19453}
19454
19455/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19456
19457static CORE_ADDR
d521ce57 19458read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19459 unsigned int *bytes_read)
19460{
5e22e966 19461 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19462 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19463
19464 return read_addr_index (cu, addr_index);
19465}
19466
450a1bfc 19467/* See read.h. */
3019eac3
DE
19468
19469CORE_ADDR
82ca3f51 19470dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 19471 dwarf2_per_objfile *per_objfile,
82ca3f51 19472 unsigned int addr_index)
3019eac3 19473{
976ca316 19474 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 19475 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19476 int addr_size;
19477
3019eac3
DE
19478 /* We need addr_base and addr_size.
19479 If we don't have PER_CU->cu, we have to get it.
19480 Nasty, but the alternative is storing the needed info in PER_CU,
19481 which at this point doesn't seem justified: it's not clear how frequently
19482 it would get used and it would increase the size of every PER_CU.
19483 Entry points like dwarf2_per_cu_addr_size do a similar thing
19484 so we're not in uncharted territory here.
19485 Alas we need to be a bit more complicated as addr_base is contained
19486 in the DIE.
19487
19488 We don't need to read the entire CU(/TU).
19489 We just need the header and top level die.
a1b64ce1 19490
3019eac3 19491 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19492 For now we skip this optimization. */
3019eac3
DE
19493
19494 if (cu != NULL)
19495 {
19496 addr_base = cu->addr_base;
19497 addr_size = cu->header.addr_size;
19498 }
19499 else
19500 {
976ca316 19501 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
19502 addr_base = reader.cu->addr_base;
19503 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19504 }
19505
976ca316 19506 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
19507}
19508
18a8505e
AT
19509/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19510 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19511 DWO file. */
3019eac3 19512
d521ce57 19513static const char *
18a8505e
AT
19514read_str_index (struct dwarf2_cu *cu,
19515 struct dwarf2_section_info *str_section,
19516 struct dwarf2_section_info *str_offsets_section,
19517 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19518{
976ca316
SM
19519 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19520 struct objfile *objfile = per_objfile->objfile;
c5164cbc 19521 const char *objf_name = objfile_name (objfile);
3019eac3 19522 bfd *abfd = objfile->obfd;
d521ce57 19523 const gdb_byte *info_ptr;
3019eac3 19524 ULONGEST str_offset;
cf532bd1 19525 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19526
96b79293
TT
19527 str_section->read (objfile);
19528 str_offsets_section->read (objfile);
73869dc2 19529 if (str_section->buffer == NULL)
18a8505e 19530 error (_("%s used without %s section"
9d8780f0 19531 " in CU at offset %s [in module %s]"),
96b79293 19532 form_name, str_section->get_name (),
18a8505e 19533 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19534 if (str_offsets_section->buffer == NULL)
18a8505e 19535 error (_("%s used without %s section"
9d8780f0 19536 " in CU at offset %s [in module %s]"),
96b79293 19537 form_name, str_section->get_name (),
18a8505e 19538 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19539 info_ptr = (str_offsets_section->buffer
18a8505e 19540 + str_offsets_base
3019eac3
DE
19541 + str_index * cu->header.offset_size);
19542 if (cu->header.offset_size == 4)
19543 str_offset = bfd_get_32 (abfd, info_ptr);
19544 else
19545 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19546 if (str_offset >= str_section->size)
57d63ce2 19547 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19548 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19549 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19550 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19551}
19552
18a8505e
AT
19553/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19554
19555static const char *
19556read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19557{
19558 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19559 ? reader->cu->header.addr_size : 0;
19560 return read_str_index (reader->cu,
19561 &reader->dwo_file->sections.str,
19562 &reader->dwo_file->sections.str_offsets,
19563 str_offsets_base, str_index);
19564}
19565
19566/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19567
19568static const char *
19569read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19570{
5e22e966 19571 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19572 const char *objf_name = objfile_name (objfile);
19573 static const char form_name[] = "DW_FORM_GNU_str_index";
19574 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19575
19576 if (!cu->str_offsets_base.has_value ())
19577 error (_("%s used in Fission stub without %s"
19578 " in CU at offset 0x%lx [in module %s]"),
19579 form_name, str_offsets_attr_name,
19580 (long) cu->header.offset_size, objf_name);
19581
19582 return read_str_index (cu,
5e22e966
SM
19583 &cu->per_objfile->per_bfd->str,
19584 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19585 *cu->str_offsets_base, str_index);
19586}
19587
3019eac3
DE
19588/* Return the length of an LEB128 number in BUF. */
19589
19590static int
19591leb128_size (const gdb_byte *buf)
19592{
19593 const gdb_byte *begin = buf;
19594 gdb_byte byte;
19595
19596 while (1)
19597 {
19598 byte = *buf++;
19599 if ((byte & 128) == 0)
19600 return buf - begin;
19601 }
19602}
19603
c906108c 19604static void
e142c38c 19605set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19606{
19607 switch (lang)
19608 {
19609 case DW_LANG_C89:
76bee0cc 19610 case DW_LANG_C99:
0cfd832f 19611 case DW_LANG_C11:
c906108c 19612 case DW_LANG_C:
d1be3247 19613 case DW_LANG_UPC:
e142c38c 19614 cu->language = language_c;
c906108c 19615 break;
9c37b5ae 19616 case DW_LANG_Java:
c906108c 19617 case DW_LANG_C_plus_plus:
0cfd832f
MW
19618 case DW_LANG_C_plus_plus_11:
19619 case DW_LANG_C_plus_plus_14:
e142c38c 19620 cu->language = language_cplus;
c906108c 19621 break;
6aecb9c2
JB
19622 case DW_LANG_D:
19623 cu->language = language_d;
19624 break;
c906108c
SS
19625 case DW_LANG_Fortran77:
19626 case DW_LANG_Fortran90:
b21b22e0 19627 case DW_LANG_Fortran95:
f7de9aab
MW
19628 case DW_LANG_Fortran03:
19629 case DW_LANG_Fortran08:
e142c38c 19630 cu->language = language_fortran;
c906108c 19631 break;
a766d390
DE
19632 case DW_LANG_Go:
19633 cu->language = language_go;
19634 break;
c906108c 19635 case DW_LANG_Mips_Assembler:
e142c38c 19636 cu->language = language_asm;
c906108c
SS
19637 break;
19638 case DW_LANG_Ada83:
8aaf0b47 19639 case DW_LANG_Ada95:
bc5f45f8
JB
19640 cu->language = language_ada;
19641 break;
72019c9c
GM
19642 case DW_LANG_Modula2:
19643 cu->language = language_m2;
19644 break;
fe8e67fd
PM
19645 case DW_LANG_Pascal83:
19646 cu->language = language_pascal;
19647 break;
22566fbd
DJ
19648 case DW_LANG_ObjC:
19649 cu->language = language_objc;
19650 break;
c44af4eb
TT
19651 case DW_LANG_Rust:
19652 case DW_LANG_Rust_old:
19653 cu->language = language_rust;
19654 break;
c906108c
SS
19655 case DW_LANG_Cobol74:
19656 case DW_LANG_Cobol85:
c906108c 19657 default:
e142c38c 19658 cu->language = language_minimal;
c906108c
SS
19659 break;
19660 }
e142c38c 19661 cu->language_defn = language_def (cu->language);
c906108c
SS
19662}
19663
19664/* Return the named attribute or NULL if not there. */
19665
19666static struct attribute *
e142c38c 19667dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19668{
a48e046c 19669 for (;;)
c906108c 19670 {
a48e046c
TT
19671 unsigned int i;
19672 struct attribute *spec = NULL;
19673
19674 for (i = 0; i < die->num_attrs; ++i)
19675 {
19676 if (die->attrs[i].name == name)
19677 return &die->attrs[i];
19678 if (die->attrs[i].name == DW_AT_specification
19679 || die->attrs[i].name == DW_AT_abstract_origin)
19680 spec = &die->attrs[i];
19681 }
19682
19683 if (!spec)
19684 break;
c906108c 19685
f2f0e013 19686 die = follow_die_ref (die, spec, &cu);
f2f0e013 19687 }
c5aa993b 19688
c906108c
SS
19689 return NULL;
19690}
19691
7d45c7c3
KB
19692/* Return the string associated with a string-typed attribute, or NULL if it
19693 is either not found or is of an incorrect type. */
19694
19695static const char *
19696dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19697{
19698 struct attribute *attr;
19699 const char *str = NULL;
19700
19701 attr = dwarf2_attr (die, name, cu);
19702
19703 if (attr != NULL)
19704 {
e61108c9
TT
19705 str = attr->value_as_string ();
19706 if (str == nullptr)
b98664d3 19707 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19708 "DIE at %s in module %s"),
19709 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19710 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19711 }
19712
19713 return str;
19714}
19715
a084a2a6 19716/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19717 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19718static const char *
19719dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19720{
19721 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19722 if (dwo_name == nullptr)
19723 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19724 return dwo_name;
19725}
19726
05cf31d1
JB
19727/* Return non-zero iff the attribute NAME is defined for the given DIE,
19728 and holds a non-zero value. This function should only be used for
2dc7f7b3 19729 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19730
19731static int
19732dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19733{
19734 struct attribute *attr = dwarf2_attr (die, name, cu);
19735
19736 return (attr && DW_UNSND (attr));
19737}
19738
3ca72b44 19739static int
e142c38c 19740die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19741{
05cf31d1
JB
19742 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19743 which value is non-zero. However, we have to be careful with
19744 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19745 (via dwarf2_flag_true_p) follows this attribute. So we may
19746 end up accidently finding a declaration attribute that belongs
19747 to a different DIE referenced by the specification attribute,
19748 even though the given DIE does not have a declaration attribute. */
19749 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19750 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19751}
19752
63d06c5c 19753/* Return the die giving the specification for DIE, if there is
f2f0e013 19754 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19755 containing the return value on output. If there is no
19756 specification, but there is an abstract origin, that is
19757 returned. */
63d06c5c
DC
19758
19759static struct die_info *
f2f0e013 19760die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19761{
f2f0e013
DJ
19762 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19763 *spec_cu);
63d06c5c 19764
edb3359d
DJ
19765 if (spec_attr == NULL)
19766 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19767
63d06c5c
DC
19768 if (spec_attr == NULL)
19769 return NULL;
19770 else
f2f0e013 19771 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19772}
c906108c 19773
527f3840
JK
19774/* Stub for free_line_header to match void * callback types. */
19775
19776static void
19777free_line_header_voidp (void *arg)
19778{
9a3c8263 19779 struct line_header *lh = (struct line_header *) arg;
527f3840 19780
fff8551c 19781 delete lh;
527f3840
JK
19782}
19783
83769d0b 19784/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19785
19786static struct dwarf2_section_info *
19787get_debug_line_section (struct dwarf2_cu *cu)
19788{
19789 struct dwarf2_section_info *section;
976ca316 19790 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
19791
19792 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19793 DWO file. */
19794 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19795 section = &cu->dwo_unit->dwo_file->sections.line;
19796 else if (cu->per_cu->is_dwz)
19797 {
976ca316 19798 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
19799
19800 section = &dwz->line;
19801 }
19802 else
976ca316 19803 section = &per_objfile->per_bfd->line;
36586728
TT
19804
19805 return section;
19806}
19807
debd256d 19808/* Read the statement program header starting at OFFSET in
3019eac3 19809 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19810 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19811 Returns NULL if there is a problem reading the header, e.g., if it
19812 has a version we don't understand.
debd256d
JB
19813
19814 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19815 the returned object point into the dwarf line section buffer,
19816 and must not be freed. */
ae2de4f8 19817
fff8551c 19818static line_header_up
9c541725 19819dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19820{
3019eac3 19821 struct dwarf2_section_info *section;
976ca316 19822 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 19823
36586728 19824 section = get_debug_line_section (cu);
976ca316 19825 section->read (per_objfile->objfile);
3019eac3 19826 if (section->buffer == NULL)
debd256d 19827 {
3019eac3 19828 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19829 complaint (_("missing .debug_line.dwo section"));
3019eac3 19830 else
b98664d3 19831 complaint (_("missing .debug_line section"));
debd256d
JB
19832 return 0;
19833 }
19834
0df7ad3a 19835 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 19836 per_objfile, section, &cu->header);
debd256d 19837}
c906108c 19838
c6da4cef 19839/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19840 Return the file name of the psymtab for the given file_entry.
c6da4cef 19841 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19842 If space for the result is malloc'd, *NAME_HOLDER will be set.
19843 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19844
d521ce57 19845static const char *
7ba99d21 19846psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19847 const dwarf2_psymtab *pst,
c89b44cd
TT
19848 const char *comp_dir,
19849 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19850{
d521ce57
TT
19851 const char *include_name = fe.name;
19852 const char *include_name_to_compare = include_name;
72b9f47f 19853 const char *pst_filename;
c6da4cef
DE
19854 int file_is_pst;
19855
8c43009f 19856 const char *dir_name = fe.include_dir (lh);
c6da4cef 19857
c89b44cd 19858 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19859 if (!IS_ABSOLUTE_PATH (include_name)
19860 && (dir_name != NULL || comp_dir != NULL))
19861 {
19862 /* Avoid creating a duplicate psymtab for PST.
19863 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19864 Before we do the comparison, however, we need to account
19865 for DIR_NAME and COMP_DIR.
19866 First prepend dir_name (if non-NULL). If we still don't
19867 have an absolute path prepend comp_dir (if non-NULL).
19868 However, the directory we record in the include-file's
19869 psymtab does not contain COMP_DIR (to match the
19870 corresponding symtab(s)).
19871
19872 Example:
19873
19874 bash$ cd /tmp
19875 bash$ gcc -g ./hello.c
19876 include_name = "hello.c"
19877 dir_name = "."
19878 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19879 DW_AT_name = "./hello.c"
19880
19881 */
c6da4cef
DE
19882
19883 if (dir_name != NULL)
19884 {
c89b44cd
TT
19885 name_holder->reset (concat (dir_name, SLASH_STRING,
19886 include_name, (char *) NULL));
19887 include_name = name_holder->get ();
c6da4cef 19888 include_name_to_compare = include_name;
c6da4cef
DE
19889 }
19890 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19891 {
c89b44cd
TT
19892 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19893 include_name, (char *) NULL));
19894 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19895 }
19896 }
19897
19898 pst_filename = pst->filename;
c89b44cd 19899 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19900 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19901 {
c89b44cd
TT
19902 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19903 pst_filename, (char *) NULL));
19904 pst_filename = copied_name.get ();
c6da4cef
DE
19905 }
19906
1e3fad37 19907 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19908
c6da4cef
DE
19909 if (file_is_pst)
19910 return NULL;
19911 return include_name;
19912}
19913
d9b3de22
DE
19914/* State machine to track the state of the line number program. */
19915
6f77053d 19916class lnp_state_machine
d9b3de22 19917{
6f77053d
PA
19918public:
19919 /* Initialize a machine state for the start of a line number
19920 program. */
804d2729
TT
19921 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19922 bool record_lines_p);
6f77053d 19923
8c43009f
PA
19924 file_entry *current_file ()
19925 {
19926 /* lh->file_names is 0-based, but the file name numbers in the
19927 statement program are 1-based. */
6f77053d
PA
19928 return m_line_header->file_name_at (m_file);
19929 }
19930
19931 /* Record the line in the state machine. END_SEQUENCE is true if
19932 we're processing the end of a sequence. */
19933 void record_line (bool end_sequence);
19934
7ab6656f
OJ
19935 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19936 nop-out rest of the lines in this sequence. */
6f77053d
PA
19937 void check_line_address (struct dwarf2_cu *cu,
19938 const gdb_byte *line_ptr,
7ab6656f 19939 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19940
19941 void handle_set_discriminator (unsigned int discriminator)
19942 {
19943 m_discriminator = discriminator;
19944 m_line_has_non_zero_discriminator |= discriminator != 0;
19945 }
19946
19947 /* Handle DW_LNE_set_address. */
19948 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19949 {
19950 m_op_index = 0;
19951 address += baseaddr;
19952 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19953 }
19954
19955 /* Handle DW_LNS_advance_pc. */
19956 void handle_advance_pc (CORE_ADDR adjust);
19957
19958 /* Handle a special opcode. */
19959 void handle_special_opcode (unsigned char op_code);
19960
19961 /* Handle DW_LNS_advance_line. */
19962 void handle_advance_line (int line_delta)
19963 {
19964 advance_line (line_delta);
19965 }
19966
19967 /* Handle DW_LNS_set_file. */
19968 void handle_set_file (file_name_index file);
19969
19970 /* Handle DW_LNS_negate_stmt. */
19971 void handle_negate_stmt ()
19972 {
19973 m_is_stmt = !m_is_stmt;
19974 }
19975
19976 /* Handle DW_LNS_const_add_pc. */
19977 void handle_const_add_pc ();
19978
19979 /* Handle DW_LNS_fixed_advance_pc. */
19980 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19981 {
19982 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19983 m_op_index = 0;
19984 }
19985
19986 /* Handle DW_LNS_copy. */
19987 void handle_copy ()
19988 {
19989 record_line (false);
19990 m_discriminator = 0;
19991 }
19992
19993 /* Handle DW_LNE_end_sequence. */
19994 void handle_end_sequence ()
19995 {
804d2729 19996 m_currently_recording_lines = true;
6f77053d
PA
19997 }
19998
19999private:
20000 /* Advance the line by LINE_DELTA. */
20001 void advance_line (int line_delta)
20002 {
20003 m_line += line_delta;
20004
20005 if (line_delta != 0)
20006 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20007 }
20008
804d2729
TT
20009 struct dwarf2_cu *m_cu;
20010
6f77053d
PA
20011 gdbarch *m_gdbarch;
20012
20013 /* True if we're recording lines.
20014 Otherwise we're building partial symtabs and are just interested in
20015 finding include files mentioned by the line number program. */
20016 bool m_record_lines_p;
20017
8c43009f 20018 /* The line number header. */
6f77053d 20019 line_header *m_line_header;
8c43009f 20020
6f77053d
PA
20021 /* These are part of the standard DWARF line number state machine,
20022 and initialized according to the DWARF spec. */
d9b3de22 20023
6f77053d 20024 unsigned char m_op_index = 0;
7ba99d21
AT
20025 /* The line table index of the current file. */
20026 file_name_index m_file = 1;
6f77053d
PA
20027 unsigned int m_line = 1;
20028
20029 /* These are initialized in the constructor. */
20030
20031 CORE_ADDR m_address;
20032 bool m_is_stmt;
20033 unsigned int m_discriminator;
d9b3de22
DE
20034
20035 /* Additional bits of state we need to track. */
20036
20037 /* The last file that we called dwarf2_start_subfile for.
20038 This is only used for TLLs. */
6f77053d 20039 unsigned int m_last_file = 0;
d9b3de22 20040 /* The last file a line number was recorded for. */
6f77053d 20041 struct subfile *m_last_subfile = NULL;
d9b3de22 20042
804d2729
TT
20043 /* When true, record the lines we decode. */
20044 bool m_currently_recording_lines = false;
d9b3de22
DE
20045
20046 /* The last line number that was recorded, used to coalesce
20047 consecutive entries for the same line. This can happen, for
20048 example, when discriminators are present. PR 17276. */
6f77053d
PA
20049 unsigned int m_last_line = 0;
20050 bool m_line_has_non_zero_discriminator = false;
8c43009f 20051};
d9b3de22 20052
6f77053d
PA
20053void
20054lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20055{
20056 CORE_ADDR addr_adj = (((m_op_index + adjust)
20057 / m_line_header->maximum_ops_per_instruction)
20058 * m_line_header->minimum_instruction_length);
20059 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20060 m_op_index = ((m_op_index + adjust)
20061 % m_line_header->maximum_ops_per_instruction);
20062}
d9b3de22 20063
6f77053d
PA
20064void
20065lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20066{
6f77053d 20067 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20068 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20069 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20070 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20071 / m_line_header->maximum_ops_per_instruction)
20072 * m_line_header->minimum_instruction_length);
20073 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20074 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20075 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20076
258bf0ee 20077 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20078 advance_line (line_delta);
20079 record_line (false);
20080 m_discriminator = 0;
20081}
d9b3de22 20082
6f77053d
PA
20083void
20084lnp_state_machine::handle_set_file (file_name_index file)
20085{
20086 m_file = file;
20087
20088 const file_entry *fe = current_file ();
20089 if (fe == NULL)
20090 dwarf2_debug_line_missing_file_complaint ();
20091 else if (m_record_lines_p)
20092 {
20093 const char *dir = fe->include_dir (m_line_header);
20094
c24bdb02 20095 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20096 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20097 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20098 }
20099}
20100
20101void
20102lnp_state_machine::handle_const_add_pc ()
20103{
20104 CORE_ADDR adjust
20105 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20106
20107 CORE_ADDR addr_adj
20108 = (((m_op_index + adjust)
20109 / m_line_header->maximum_ops_per_instruction)
20110 * m_line_header->minimum_instruction_length);
20111
20112 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20113 m_op_index = ((m_op_index + adjust)
20114 % m_line_header->maximum_ops_per_instruction);
20115}
d9b3de22 20116
a05a36a5
DE
20117/* Return non-zero if we should add LINE to the line number table.
20118 LINE is the line to add, LAST_LINE is the last line that was added,
20119 LAST_SUBFILE is the subfile for LAST_LINE.
20120 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20121 had a non-zero discriminator.
20122
20123 We have to be careful in the presence of discriminators.
20124 E.g., for this line:
20125
20126 for (i = 0; i < 100000; i++);
20127
20128 clang can emit four line number entries for that one line,
20129 each with a different discriminator.
20130 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20131
20132 However, we want gdb to coalesce all four entries into one.
20133 Otherwise the user could stepi into the middle of the line and
20134 gdb would get confused about whether the pc really was in the
20135 middle of the line.
20136
20137 Things are further complicated by the fact that two consecutive
20138 line number entries for the same line is a heuristic used by gcc
20139 to denote the end of the prologue. So we can't just discard duplicate
20140 entries, we have to be selective about it. The heuristic we use is
20141 that we only collapse consecutive entries for the same line if at least
20142 one of those entries has a non-zero discriminator. PR 17276.
20143
20144 Note: Addresses in the line number state machine can never go backwards
20145 within one sequence, thus this coalescing is ok. */
20146
20147static int
804d2729
TT
20148dwarf_record_line_p (struct dwarf2_cu *cu,
20149 unsigned int line, unsigned int last_line,
a05a36a5
DE
20150 int line_has_non_zero_discriminator,
20151 struct subfile *last_subfile)
20152{
c24bdb02 20153 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20154 return 1;
20155 if (line != last_line)
20156 return 1;
20157 /* Same line for the same file that we've seen already.
20158 As a last check, for pr 17276, only record the line if the line
20159 has never had a non-zero discriminator. */
20160 if (!line_has_non_zero_discriminator)
20161 return 1;
20162 return 0;
20163}
20164
804d2729
TT
20165/* Use the CU's builder to record line number LINE beginning at
20166 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20167
20168static void
d9b3de22 20169dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20170 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20171 struct dwarf2_cu *cu)
252a6764
DE
20172{
20173 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20174
27e0867f
DE
20175 if (dwarf_line_debug)
20176 {
20177 fprintf_unfiltered (gdb_stdlog,
20178 "Recording line %u, file %s, address %s\n",
20179 line, lbasename (subfile->name),
20180 paddress (gdbarch, address));
20181 }
20182
804d2729 20183 if (cu != nullptr)
8c95582d 20184 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20185}
20186
20187/* Subroutine of dwarf_decode_lines_1 to simplify it.
20188 Mark the end of a set of line number records.
d9b3de22 20189 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20190 If SUBFILE is NULL the request is ignored. */
20191
20192static void
20193dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20194 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20195{
27e0867f
DE
20196 if (subfile == NULL)
20197 return;
20198
20199 if (dwarf_line_debug)
20200 {
20201 fprintf_unfiltered (gdb_stdlog,
20202 "Finishing current line, file %s, address %s\n",
20203 lbasename (subfile->name),
20204 paddress (gdbarch, address));
20205 }
20206
8c95582d 20207 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20208}
20209
6f77053d
PA
20210void
20211lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20212{
d9b3de22
DE
20213 if (dwarf_line_debug)
20214 {
20215 fprintf_unfiltered (gdb_stdlog,
20216 "Processing actual line %u: file %u,"
94a72be7 20217 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20218 m_line, m_file,
6f77053d 20219 paddress (m_gdbarch, m_address),
94a72be7
AB
20220 m_is_stmt, m_discriminator,
20221 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20222 }
20223
6f77053d 20224 file_entry *fe = current_file ();
8c43009f
PA
20225
20226 if (fe == NULL)
d9b3de22
DE
20227 dwarf2_debug_line_missing_file_complaint ();
20228 /* For now we ignore lines not starting on an instruction boundary.
20229 But not when processing end_sequence for compatibility with the
20230 previous version of the code. */
6f77053d 20231 else if (m_op_index == 0 || end_sequence)
d9b3de22 20232 {
8c43009f 20233 fe->included_p = 1;
8c95582d 20234 if (m_record_lines_p)
d9b3de22 20235 {
c24bdb02 20236 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20237 || end_sequence)
d9b3de22 20238 {
804d2729
TT
20239 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20240 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20241 }
20242
20243 if (!end_sequence)
20244 {
8c95582d
AB
20245 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20246
804d2729 20247 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20248 m_line_has_non_zero_discriminator,
20249 m_last_subfile))
d9b3de22 20250 {
c24bdb02 20251 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20252 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20253 builder->get_current_subfile (),
8c95582d 20254 m_line, m_address, is_stmt,
804d2729 20255 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20256 }
c24bdb02 20257 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20258 m_last_line = m_line;
d9b3de22
DE
20259 }
20260 }
20261 }
20262}
20263
804d2729
TT
20264lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20265 line_header *lh, bool record_lines_p)
d9b3de22 20266{
804d2729 20267 m_cu = cu;
6f77053d
PA
20268 m_gdbarch = arch;
20269 m_record_lines_p = record_lines_p;
20270 m_line_header = lh;
d9b3de22 20271
804d2729 20272 m_currently_recording_lines = true;
d9b3de22 20273
d9b3de22
DE
20274 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20275 was a line entry for it so that the backend has a chance to adjust it
20276 and also record it in case it needs it. This is currently used by MIPS
20277 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20278 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20279 m_is_stmt = lh->default_is_stmt;
20280 m_discriminator = 0;
252a6764
DE
20281}
20282
6f77053d
PA
20283void
20284lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20285 const gdb_byte *line_ptr,
7ab6656f 20286 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20287{
7ab6656f
OJ
20288 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20289 the pc range of the CU. However, we restrict the test to only ADDRESS
20290 values of zero to preserve GDB's previous behaviour which is to handle
20291 the specific case of a function being GC'd by the linker. */
924c2928 20292
7ab6656f 20293 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20294 {
20295 /* This line table is for a function which has been
20296 GCd by the linker. Ignore it. PR gdb/12528 */
20297
5e22e966 20298 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20299 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20300
b98664d3 20301 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20302 line_offset, objfile_name (objfile));
804d2729
TT
20303 m_currently_recording_lines = false;
20304 /* Note: m_currently_recording_lines is left as false until we see
20305 DW_LNE_end_sequence. */
924c2928
DE
20306 }
20307}
20308
f3f5162e 20309/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20310 Process the line number information in LH.
20311 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20312 program in order to set included_p for every referenced header. */
debd256d 20313
c906108c 20314static void
43f3e411
DE
20315dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20316 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20317{
d521ce57
TT
20318 const gdb_byte *line_ptr, *extended_end;
20319 const gdb_byte *line_end;
a8c50c1f 20320 unsigned int bytes_read, extended_len;
699ca60a 20321 unsigned char op_code, extended_op;
e142c38c 20322 CORE_ADDR baseaddr;
5e22e966 20323 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20324 bfd *abfd = objfile->obfd;
08feed99 20325 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20326 /* True if we're recording line info (as opposed to building partial
20327 symtabs and just interested in finding include files mentioned by
20328 the line number program). */
20329 bool record_lines_p = !decode_for_pst_p;
e142c38c 20330
b3b3bada 20331 baseaddr = objfile->text_section_offset ();
c906108c 20332
debd256d
JB
20333 line_ptr = lh->statement_program_start;
20334 line_end = lh->statement_program_end;
c906108c
SS
20335
20336 /* Read the statement sequences until there's nothing left. */
20337 while (line_ptr < line_end)
20338 {
6f77053d
PA
20339 /* The DWARF line number program state machine. Reset the state
20340 machine at the start of each sequence. */
804d2729 20341 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20342 bool end_sequence = false;
d9b3de22 20343
8c43009f 20344 if (record_lines_p)
c906108c 20345 {
8c43009f
PA
20346 /* Start a subfile for the current file of the state
20347 machine. */
20348 const file_entry *fe = state_machine.current_file ();
20349
20350 if (fe != NULL)
804d2729 20351 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20352 }
20353
a738430d 20354 /* Decode the table. */
d9b3de22 20355 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20356 {
20357 op_code = read_1_byte (abfd, line_ptr);
20358 line_ptr += 1;
9aa1fe7e 20359
debd256d 20360 if (op_code >= lh->opcode_base)
6e70227d 20361 {
8e07a239 20362 /* Special opcode. */
6f77053d 20363 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20364 }
20365 else switch (op_code)
c906108c
SS
20366 {
20367 case DW_LNS_extended_op:
3e43a32a
MS
20368 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20369 &bytes_read);
473b7be6 20370 line_ptr += bytes_read;
a8c50c1f 20371 extended_end = line_ptr + extended_len;
c906108c
SS
20372 extended_op = read_1_byte (abfd, line_ptr);
20373 line_ptr += 1;
20374 switch (extended_op)
20375 {
20376 case DW_LNE_end_sequence:
6f77053d
PA
20377 state_machine.handle_end_sequence ();
20378 end_sequence = true;
c906108c
SS
20379 break;
20380 case DW_LNE_set_address:
d9b3de22
DE
20381 {
20382 CORE_ADDR address
c8a7a66f 20383 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20384 line_ptr += bytes_read;
6f77053d
PA
20385
20386 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20387 lowpc - baseaddr, address);
6f77053d 20388 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20389 }
c906108c
SS
20390 break;
20391 case DW_LNE_define_file:
debd256d 20392 {
d521ce57 20393 const char *cur_file;
ecfb656c
PA
20394 unsigned int mod_time, length;
20395 dir_index dindex;
6e70227d 20396
3e43a32a
MS
20397 cur_file = read_direct_string (abfd, line_ptr,
20398 &bytes_read);
debd256d 20399 line_ptr += bytes_read;
ecfb656c 20400 dindex = (dir_index)
debd256d
JB
20401 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20402 line_ptr += bytes_read;
20403 mod_time =
20404 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20405 line_ptr += bytes_read;
20406 length =
20407 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20408 line_ptr += bytes_read;
ecfb656c 20409 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20410 }
c906108c 20411 break;
d0c6ba3d 20412 case DW_LNE_set_discriminator:
6f77053d
PA
20413 {
20414 /* The discriminator is not interesting to the
20415 debugger; just ignore it. We still need to
20416 check its value though:
20417 if there are consecutive entries for the same
20418 (non-prologue) line we want to coalesce them.
20419 PR 17276. */
20420 unsigned int discr
20421 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20422 line_ptr += bytes_read;
20423
20424 state_machine.handle_set_discriminator (discr);
20425 }
d0c6ba3d 20426 break;
c906108c 20427 default:
b98664d3 20428 complaint (_("mangled .debug_line section"));
debd256d 20429 return;
c906108c 20430 }
a8c50c1f
DJ
20431 /* Make sure that we parsed the extended op correctly. If e.g.
20432 we expected a different address size than the producer used,
20433 we may have read the wrong number of bytes. */
20434 if (line_ptr != extended_end)
20435 {
b98664d3 20436 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20437 return;
20438 }
c906108c
SS
20439 break;
20440 case DW_LNS_copy:
6f77053d 20441 state_machine.handle_copy ();
c906108c
SS
20442 break;
20443 case DW_LNS_advance_pc:
2dc7f7b3
TT
20444 {
20445 CORE_ADDR adjust
20446 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20447 line_ptr += bytes_read;
6f77053d
PA
20448
20449 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20450 }
c906108c
SS
20451 break;
20452 case DW_LNS_advance_line:
a05a36a5
DE
20453 {
20454 int line_delta
20455 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20456 line_ptr += bytes_read;
6f77053d
PA
20457
20458 state_machine.handle_advance_line (line_delta);
a05a36a5 20459 }
c906108c
SS
20460 break;
20461 case DW_LNS_set_file:
d9b3de22 20462 {
6f77053d 20463 file_name_index file
ecfb656c
PA
20464 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20465 &bytes_read);
d9b3de22 20466 line_ptr += bytes_read;
8c43009f 20467
6f77053d 20468 state_machine.handle_set_file (file);
d9b3de22 20469 }
c906108c
SS
20470 break;
20471 case DW_LNS_set_column:
0ad93d4f 20472 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20473 line_ptr += bytes_read;
20474 break;
20475 case DW_LNS_negate_stmt:
6f77053d 20476 state_machine.handle_negate_stmt ();
c906108c
SS
20477 break;
20478 case DW_LNS_set_basic_block:
c906108c 20479 break;
c2c6d25f
JM
20480 /* Add to the address register of the state machine the
20481 address increment value corresponding to special opcode
a738430d
MK
20482 255. I.e., this value is scaled by the minimum
20483 instruction length since special opcode 255 would have
b021a221 20484 scaled the increment. */
c906108c 20485 case DW_LNS_const_add_pc:
6f77053d 20486 state_machine.handle_const_add_pc ();
c906108c
SS
20487 break;
20488 case DW_LNS_fixed_advance_pc:
3e29f34a 20489 {
6f77053d 20490 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20491 line_ptr += 2;
6f77053d
PA
20492
20493 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20494 }
c906108c 20495 break;
9aa1fe7e 20496 default:
a738430d
MK
20497 {
20498 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20499 int i;
a738430d 20500
debd256d 20501 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20502 {
20503 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20504 line_ptr += bytes_read;
20505 }
20506 }
c906108c
SS
20507 }
20508 }
d9b3de22
DE
20509
20510 if (!end_sequence)
20511 dwarf2_debug_line_missing_end_sequence_complaint ();
20512
20513 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20514 in which case we still finish recording the last line). */
6f77053d 20515 state_machine.record_line (true);
c906108c 20516 }
f3f5162e
DE
20517}
20518
20519/* Decode the Line Number Program (LNP) for the given line_header
20520 structure and CU. The actual information extracted and the type
20521 of structures created from the LNP depends on the value of PST.
20522
20523 1. If PST is NULL, then this procedure uses the data from the program
20524 to create all necessary symbol tables, and their linetables.
20525
20526 2. If PST is not NULL, this procedure reads the program to determine
20527 the list of files included by the unit represented by PST, and
20528 builds all the associated partial symbol tables.
20529
20530 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20531 It is used for relative paths in the line table.
20532 NOTE: When processing partial symtabs (pst != NULL),
20533 comp_dir == pst->dirname.
20534
20535 NOTE: It is important that psymtabs have the same file name (via strcmp)
20536 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20537 symtab we don't use it in the name of the psymtabs we create.
20538 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20539 A good testcase for this is mb-inline.exp.
20540
527f3840
JK
20541 LOWPC is the lowest address in CU (or 0 if not known).
20542
20543 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20544 for its PC<->lines mapping information. Otherwise only the filename
20545 table is read in. */
f3f5162e
DE
20546
20547static void
20548dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20549 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20550 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20551{
5e22e966 20552 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20553 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20554
527f3840
JK
20555 if (decode_mapping)
20556 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20557
20558 if (decode_for_pst_p)
20559 {
aaa75496
JB
20560 /* Now that we're done scanning the Line Header Program, we can
20561 create the psymtab of each included file. */
7ba99d21
AT
20562 for (auto &file_entry : lh->file_names ())
20563 if (file_entry.included_p == 1)
aaa75496 20564 {
c89b44cd 20565 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20566 const char *include_name =
7ba99d21
AT
20567 psymtab_include_file_name (lh, file_entry, pst,
20568 comp_dir, &name_holder);
c6da4cef 20569 if (include_name != NULL)
aaa75496
JB
20570 dwarf2_create_include_psymtab (include_name, pst, objfile);
20571 }
20572 }
cb1df416
DJ
20573 else
20574 {
20575 /* Make sure a symtab is created for every file, even files
20576 which contain only variables (i.e. no code with associated
20577 line numbers). */
c24bdb02
KS
20578 buildsym_compunit *builder = cu->get_builder ();
20579 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20580
7ba99d21 20581 for (auto &fe : lh->file_names ())
cb1df416 20582 {
804d2729 20583 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20584 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20585 {
c24bdb02 20586 builder->get_current_subfile ()->symtab
804d2729 20587 = allocate_symtab (cust,
c24bdb02 20588 builder->get_current_subfile ()->name);
43f3e411 20589 }
c24bdb02 20590 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20591 }
20592 }
c906108c
SS
20593}
20594
20595/* Start a subfile for DWARF. FILENAME is the name of the file and
20596 DIRNAME the name of the source directory which contains FILENAME
4d663531 20597 or NULL if not known.
c906108c
SS
20598 This routine tries to keep line numbers from identical absolute and
20599 relative file names in a common subfile.
20600
20601 Using the `list' example from the GDB testsuite, which resides in
20602 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20603 of /srcdir/list0.c yields the following debugging information for list0.c:
20604
c5aa993b 20605 DW_AT_name: /srcdir/list0.c
4d663531 20606 DW_AT_comp_dir: /compdir
357e46e7 20607 files.files[0].name: list0.h
c5aa993b 20608 files.files[0].dir: /srcdir
357e46e7 20609 files.files[1].name: list0.c
c5aa993b 20610 files.files[1].dir: /srcdir
c906108c
SS
20611
20612 The line number information for list0.c has to end up in a single
4f1520fb
FR
20613 subfile, so that `break /srcdir/list0.c:1' works as expected.
20614 start_subfile will ensure that this happens provided that we pass the
20615 concatenation of files.files[1].dir and files.files[1].name as the
20616 subfile's name. */
c906108c
SS
20617
20618static void
804d2729
TT
20619dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20620 const char *dirname)
c906108c 20621{
43816ebc 20622 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20623
4d663531 20624 /* In order not to lose the line information directory,
4f1520fb
FR
20625 we concatenate it to the filename when it makes sense.
20626 Note that the Dwarf3 standard says (speaking of filenames in line
20627 information): ``The directory index is ignored for file names
20628 that represent full path names''. Thus ignoring dirname in the
20629 `else' branch below isn't an issue. */
c906108c 20630
d5166ae1 20631 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20632 {
43816ebc
TT
20633 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20634 filename = copy.get ();
d521ce57 20635 }
c906108c 20636
c24bdb02 20637 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20638}
20639
804d2729
TT
20640/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20641 buildsym_compunit constructor. */
f4dc4d17 20642
c24bdb02
KS
20643struct compunit_symtab *
20644dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20645 CORE_ADDR low_pc)
f4dc4d17 20646{
c24bdb02 20647 gdb_assert (m_builder == nullptr);
43f3e411 20648
c24bdb02 20649 m_builder.reset (new struct buildsym_compunit
f6e649dd 20650 (this->per_objfile->objfile,
c24bdb02 20651 name, comp_dir, language, low_pc));
93b8bea4 20652
c24bdb02 20653 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20654
c24bdb02
KS
20655 get_builder ()->record_debugformat ("DWARF 2");
20656 get_builder ()->record_producer (producer);
f4dc4d17 20657
c24bdb02 20658 processing_has_namespace_info = false;
43f3e411 20659
c24bdb02 20660 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20661}
20662
4c2df51b
DJ
20663static void
20664var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20665 struct dwarf2_cu *cu)
4c2df51b 20666{
5e22e966 20667 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20668 struct comp_unit_head *cu_header = &cu->header;
20669
4c2df51b
DJ
20670 /* NOTE drow/2003-01-30: There used to be a comment and some special
20671 code here to turn a symbol with DW_AT_external and a
20672 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20673 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20674 with some versions of binutils) where shared libraries could have
20675 relocations against symbols in their debug information - the
20676 minimal symbol would have the right address, but the debug info
20677 would not. It's no longer necessary, because we will explicitly
20678 apply relocations when we read in the debug information now. */
20679
20680 /* A DW_AT_location attribute with no contents indicates that a
20681 variable has been optimized away. */
4fc6c0d5 20682 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20683 {
f1e6e072 20684 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20685 return;
20686 }
20687
20688 /* Handle one degenerate form of location expression specially, to
20689 preserve GDB's previous behavior when section offsets are
336d760d
AT
20690 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20691 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20692
4fc6c0d5 20693 if (attr->form_is_block ()
3019eac3
DE
20694 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20695 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20696 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20697 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20698 && (DW_BLOCK (attr)->size
20699 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20700 {
891d2f0b 20701 unsigned int dummy;
4c2df51b 20702
3019eac3 20703 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20704 SET_SYMBOL_VALUE_ADDRESS
20705 (sym, cu->header.read_address (objfile->obfd,
20706 DW_BLOCK (attr)->data + 1,
20707 &dummy));
3019eac3 20708 else
38583298
TT
20709 SET_SYMBOL_VALUE_ADDRESS
20710 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20711 &dummy));
f1e6e072 20712 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20713 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20714 SET_SYMBOL_VALUE_ADDRESS
20715 (sym,
20716 SYMBOL_VALUE_ADDRESS (sym)
20717 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20718 return;
20719 }
20720
20721 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20722 expression evaluator, and use LOC_COMPUTED only when necessary
20723 (i.e. when the value of a register or memory location is
20724 referenced, or a thread-local block, etc.). Then again, it might
20725 not be worthwhile. I'm assuming that it isn't unless performance
20726 or memory numbers show me otherwise. */
20727
f1e6e072 20728 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20729
f1e6e072 20730 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20731 cu->has_loclist = true;
4c2df51b
DJ
20732}
20733
c906108c
SS
20734/* Given a pointer to a DWARF information entry, figure out if we need
20735 to make a symbol table entry for it, and if so, create a new entry
20736 and return a pointer to it.
20737 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20738 used the passed type.
20739 If SPACE is not NULL, use it to hold the new symbol. If it is
20740 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20741
20742static struct symbol *
5e2db402
TT
20743new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20744 struct symbol *space)
c906108c 20745{
976ca316
SM
20746 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20747 struct objfile *objfile = per_objfile->objfile;
08feed99 20748 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20749 struct symbol *sym = NULL;
15d034d0 20750 const char *name;
c906108c
SS
20751 struct attribute *attr = NULL;
20752 struct attribute *attr2 = NULL;
e142c38c 20753 CORE_ADDR baseaddr;
e37fd15a
SW
20754 struct pending **list_to_add = NULL;
20755
edb3359d 20756 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20757
b3b3bada 20758 baseaddr = objfile->text_section_offset ();
c906108c 20759
94af9270 20760 name = dwarf2_name (die, cu);
c906108c
SS
20761 if (name)
20762 {
34eaf542 20763 int suppress_add = 0;
94af9270 20764
34eaf542
TT
20765 if (space)
20766 sym = space;
20767 else
8c14c3a3 20768 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20769 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20770
20771 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20772 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20773 /* Fortran does not have mangling standard and the mangling does differ
20774 between gfortran, iFort etc. */
bcfe6157
TT
20775 const char *physname
20776 = (cu->language == language_fortran
20777 ? dwarf2_full_name (name, die, cu)
20778 : dwarf2_physname (name, die, cu));
20779 const char *linkagename = dw2_linkage_name (die, cu);
20780
20781 if (linkagename == nullptr || cu->language == language_ada)
20782 sym->set_linkage_name (physname);
20783 else
20784 {
20785 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20786 sym->set_linkage_name (linkagename);
20787 }
f55ee35c 20788
c906108c 20789 /* Default assumptions.
c5aa993b 20790 Use the passed type or decode it from the die. */
176620f1 20791 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20792 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20793 if (type != NULL)
20794 SYMBOL_TYPE (sym) = type;
20795 else
e7c27a73 20796 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20797 attr = dwarf2_attr (die,
20798 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20799 cu);
435d3d88 20800 if (attr != nullptr)
c906108c
SS
20801 {
20802 SYMBOL_LINE (sym) = DW_UNSND (attr);
20803 }
cb1df416 20804
edb3359d
DJ
20805 attr = dwarf2_attr (die,
20806 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20807 cu);
435d3d88 20808 if (attr != nullptr)
cb1df416 20809 {
ecfb656c 20810 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20811 struct file_entry *fe;
9a619af0 20812
ecfb656c
PA
20813 if (cu->line_header != NULL)
20814 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20815 else
20816 fe = NULL;
20817
20818 if (fe == NULL)
b98664d3 20819 complaint (_("file index out of range"));
8c43009f
PA
20820 else
20821 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20822 }
20823
c906108c
SS
20824 switch (die->tag)
20825 {
20826 case DW_TAG_label:
e142c38c 20827 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20828 if (attr != nullptr)
3e29f34a
MR
20829 {
20830 CORE_ADDR addr;
20831
cd6c91b4 20832 addr = attr->value_as_address ();
3e29f34a 20833 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20834 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20835 }
0f5238ed
TT
20836 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20837 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20838 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20839 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20840 break;
20841 case DW_TAG_subprogram:
20842 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20843 finish_block. */
f1e6e072 20844 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20845 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20846 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20847 || cu->language == language_ada
20848 || cu->language == language_fortran)
c906108c 20849 {
2cfa0c8d 20850 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20851 Ada and Fortran subprograms, whether marked external or
20852 not, are always stored as a global symbol, because we want
20853 to be able to access them globally. For instance, we want
20854 to be able to break on a nested subprogram without having
20855 to specify the context. */
c24bdb02 20856 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20857 }
20858 else
20859 {
e37fd15a 20860 list_to_add = cu->list_in_scope;
c906108c
SS
20861 }
20862 break;
edb3359d
DJ
20863 case DW_TAG_inlined_subroutine:
20864 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20865 finish_block. */
f1e6e072 20866 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20867 SYMBOL_INLINED (sym) = 1;
481860b3 20868 list_to_add = cu->list_in_scope;
edb3359d 20869 break;
34eaf542
TT
20870 case DW_TAG_template_value_param:
20871 suppress_add = 1;
20872 /* Fall through. */
72929c62 20873 case DW_TAG_constant:
c906108c 20874 case DW_TAG_variable:
254e6b9e 20875 case DW_TAG_member:
0963b4bd
MS
20876 /* Compilation with minimal debug info may result in
20877 variables with missing type entries. Change the
20878 misleading `void' type to something sensible. */
78134374 20879 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20880 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20881
e142c38c 20882 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20883 /* In the case of DW_TAG_member, we should only be called for
20884 static const members. */
20885 if (die->tag == DW_TAG_member)
20886 {
3863f96c
DE
20887 /* dwarf2_add_field uses die_is_declaration,
20888 so we do the same. */
254e6b9e
DE
20889 gdb_assert (die_is_declaration (die, cu));
20890 gdb_assert (attr);
20891 }
435d3d88 20892 if (attr != nullptr)
c906108c 20893 {
e7c27a73 20894 dwarf2_const_value (attr, sym, cu);
e142c38c 20895 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20896 if (!suppress_add)
34eaf542
TT
20897 {
20898 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20899 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20900 else
e37fd15a 20901 list_to_add = cu->list_in_scope;
34eaf542 20902 }
c906108c
SS
20903 break;
20904 }
e142c38c 20905 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20906 if (attr != nullptr)
c906108c 20907 {
e7c27a73 20908 var_decode_location (attr, sym, cu);
e142c38c 20909 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20910
20911 /* Fortran explicitly imports any global symbols to the local
20912 scope by DW_TAG_common_block. */
20913 if (cu->language == language_fortran && die->parent
20914 && die->parent->tag == DW_TAG_common_block)
20915 attr2 = NULL;
20916
caac4577
JG
20917 if (SYMBOL_CLASS (sym) == LOC_STATIC
20918 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 20919 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20920 {
20921 /* When a static variable is eliminated by the linker,
20922 the corresponding debug information is not stripped
20923 out, but the variable address is set to null;
20924 do not add such variables into symbol table. */
20925 }
20926 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20927 {
4b610737
TT
20928 if (SYMBOL_CLASS (sym) == LOC_STATIC
20929 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 20930 && per_objfile->per_bfd->can_copy)
4b610737
TT
20931 {
20932 /* A global static variable might be subject to
20933 copy relocation. We first check for a local
20934 minsym, though, because maybe the symbol was
20935 marked hidden, in which case this would not
20936 apply. */
20937 bound_minimal_symbol found
20938 = (lookup_minimal_symbol_linkage
987012b8 20939 (sym->linkage_name (), objfile));
4b610737
TT
20940 if (found.minsym != nullptr)
20941 sym->maybe_copied = 1;
20942 }
f55ee35c 20943
1c809c68
TT
20944 /* A variable with DW_AT_external is never static,
20945 but it may be block-scoped. */
804d2729 20946 list_to_add
c24bdb02
KS
20947 = ((cu->list_in_scope
20948 == cu->get_builder ()->get_file_symbols ())
20949 ? cu->get_builder ()->get_global_symbols ()
804d2729 20950 : cu->list_in_scope);
1c809c68 20951 }
c906108c 20952 else
e37fd15a 20953 list_to_add = cu->list_in_scope;
c906108c
SS
20954 }
20955 else
20956 {
20957 /* We do not know the address of this symbol.
c5aa993b
JM
20958 If it is an external symbol and we have type information
20959 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20960 The address of the variable will then be determined from
20961 the minimal symbol table whenever the variable is
20962 referenced. */
e142c38c 20963 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20964
20965 /* Fortran explicitly imports any global symbols to the local
20966 scope by DW_TAG_common_block. */
20967 if (cu->language == language_fortran && die->parent
20968 && die->parent->tag == DW_TAG_common_block)
20969 {
20970 /* SYMBOL_CLASS doesn't matter here because
20971 read_common_block is going to reset it. */
20972 if (!suppress_add)
20973 list_to_add = cu->list_in_scope;
20974 }
20975 else if (attr2 && (DW_UNSND (attr2) != 0)
20976 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20977 {
0fe7935b
DJ
20978 /* A variable with DW_AT_external is never static, but it
20979 may be block-scoped. */
804d2729 20980 list_to_add
c24bdb02
KS
20981 = ((cu->list_in_scope
20982 == cu->get_builder ()->get_file_symbols ())
20983 ? cu->get_builder ()->get_global_symbols ()
804d2729 20984 : cu->list_in_scope);
0fe7935b 20985
f1e6e072 20986 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20987 }
442ddf59
JK
20988 else if (!die_is_declaration (die, cu))
20989 {
20990 /* Use the default LOC_OPTIMIZED_OUT class. */
20991 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20992 if (!suppress_add)
20993 list_to_add = cu->list_in_scope;
442ddf59 20994 }
c906108c
SS
20995 }
20996 break;
20997 case DW_TAG_formal_parameter:
a60f3166
TT
20998 {
20999 /* If we are inside a function, mark this as an argument. If
21000 not, we might be looking at an argument to an inlined function
21001 when we do not have enough information to show inlined frames;
21002 pretend it's a local variable in that case so that the user can
21003 still see it. */
804d2729 21004 struct context_stack *curr
c24bdb02 21005 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21006 if (curr != nullptr && curr->name != nullptr)
21007 SYMBOL_IS_ARGUMENT (sym) = 1;
21008 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21009 if (attr != nullptr)
a60f3166
TT
21010 {
21011 var_decode_location (attr, sym, cu);
21012 }
21013 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21014 if (attr != nullptr)
a60f3166
TT
21015 {
21016 dwarf2_const_value (attr, sym, cu);
21017 }
f346a30d 21018
a60f3166
TT
21019 list_to_add = cu->list_in_scope;
21020 }
c906108c
SS
21021 break;
21022 case DW_TAG_unspecified_parameters:
21023 /* From varargs functions; gdb doesn't seem to have any
21024 interest in this information, so just ignore it for now.
21025 (FIXME?) */
21026 break;
34eaf542
TT
21027 case DW_TAG_template_type_param:
21028 suppress_add = 1;
21029 /* Fall through. */
c906108c 21030 case DW_TAG_class_type:
680b30c7 21031 case DW_TAG_interface_type:
c906108c
SS
21032 case DW_TAG_structure_type:
21033 case DW_TAG_union_type:
72019c9c 21034 case DW_TAG_set_type:
c906108c 21035 case DW_TAG_enumeration_type:
f1e6e072 21036 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21037 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21038
63d06c5c 21039 {
9c37b5ae 21040 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21041 really ever be static objects: otherwise, if you try
21042 to, say, break of a class's method and you're in a file
21043 which doesn't mention that class, it won't work unless
21044 the check for all static symbols in lookup_symbol_aux
21045 saves you. See the OtherFileClass tests in
21046 gdb.c++/namespace.exp. */
21047
e37fd15a 21048 if (!suppress_add)
34eaf542 21049 {
c24bdb02 21050 buildsym_compunit *builder = cu->get_builder ();
804d2729 21051 list_to_add
c24bdb02 21052 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21053 && cu->language == language_cplus
c24bdb02 21054 ? builder->get_global_symbols ()
804d2729 21055 : cu->list_in_scope);
63d06c5c 21056
64382290 21057 /* The semantics of C++ state that "struct foo {
9c37b5ae 21058 ... }" also defines a typedef for "foo". */
64382290 21059 if (cu->language == language_cplus
45280282 21060 || cu->language == language_ada
c44af4eb
TT
21061 || cu->language == language_d
21062 || cu->language == language_rust)
64382290
TT
21063 {
21064 /* The symbol's name is already allocated along
21065 with this objfile, so we don't need to
21066 duplicate it for the type. */
7d93a1e0 21067 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21068 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21069 }
63d06c5c
DC
21070 }
21071 }
c906108c
SS
21072 break;
21073 case DW_TAG_typedef:
f1e6e072 21074 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21075 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21076 list_to_add = cu->list_in_scope;
63d06c5c 21077 break;
c906108c 21078 case DW_TAG_base_type:
a02abb62 21079 case DW_TAG_subrange_type:
f1e6e072 21080 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21081 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21082 list_to_add = cu->list_in_scope;
c906108c
SS
21083 break;
21084 case DW_TAG_enumerator:
e142c38c 21085 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21086 if (attr != nullptr)
c906108c 21087 {
e7c27a73 21088 dwarf2_const_value (attr, sym, cu);
c906108c 21089 }
63d06c5c
DC
21090 {
21091 /* NOTE: carlton/2003-11-10: See comment above in the
21092 DW_TAG_class_type, etc. block. */
21093
804d2729 21094 list_to_add
c24bdb02 21095 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21096 && cu->language == language_cplus
c24bdb02 21097 ? cu->get_builder ()->get_global_symbols ()
804d2729 21098 : cu->list_in_scope);
63d06c5c 21099 }
c906108c 21100 break;
74921315 21101 case DW_TAG_imported_declaration:
5c4e30ca 21102 case DW_TAG_namespace:
f1e6e072 21103 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21104 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21105 break;
530e8392
KB
21106 case DW_TAG_module:
21107 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21108 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21109 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21110 break;
4357ac6c 21111 case DW_TAG_common_block:
f1e6e072 21112 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21113 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21114 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21115 break;
c906108c
SS
21116 default:
21117 /* Not a tag we recognize. Hopefully we aren't processing
21118 trash data, but since we must specifically ignore things
21119 we don't recognize, there is nothing else we should do at
0963b4bd 21120 this point. */
b98664d3 21121 complaint (_("unsupported tag: '%s'"),
4d3c2250 21122 dwarf_tag_name (die->tag));
c906108c
SS
21123 break;
21124 }
df8a16a1 21125
e37fd15a
SW
21126 if (suppress_add)
21127 {
21128 sym->hash_next = objfile->template_symbols;
21129 objfile->template_symbols = sym;
21130 list_to_add = NULL;
21131 }
21132
21133 if (list_to_add != NULL)
d3cb6808 21134 add_symbol_to_list (sym, list_to_add);
e37fd15a 21135
df8a16a1
DJ
21136 /* For the benefit of old versions of GCC, check for anonymous
21137 namespaces based on the demangled name. */
4d4ec4e5 21138 if (!cu->processing_has_namespace_info
94af9270 21139 && cu->language == language_cplus)
c24bdb02 21140 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21141 }
21142 return (sym);
21143}
21144
98bfdba5
PA
21145/* Given an attr with a DW_FORM_dataN value in host byte order,
21146 zero-extend it as appropriate for the symbol's type. The DWARF
21147 standard (v4) is not entirely clear about the meaning of using
21148 DW_FORM_dataN for a constant with a signed type, where the type is
21149 wider than the data. The conclusion of a discussion on the DWARF
21150 list was that this is unspecified. We choose to always zero-extend
21151 because that is the interpretation long in use by GCC. */
c906108c 21152
98bfdba5 21153static gdb_byte *
ff39bb5e 21154dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21155 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21156{
5e22e966 21157 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21158 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21159 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21160 LONGEST l = DW_UNSND (attr);
21161
21162 if (bits < sizeof (*value) * 8)
21163 {
21164 l &= ((LONGEST) 1 << bits) - 1;
21165 *value = l;
21166 }
21167 else if (bits == sizeof (*value) * 8)
21168 *value = l;
21169 else
21170 {
224c3ddb 21171 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21172 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21173 return bytes;
21174 }
21175
21176 return NULL;
21177}
21178
21179/* Read a constant value from an attribute. Either set *VALUE, or if
21180 the value does not fit in *VALUE, set *BYTES - either already
21181 allocated on the objfile obstack, or newly allocated on OBSTACK,
21182 or, set *BATON, if we translated the constant to a location
21183 expression. */
21184
21185static void
ff39bb5e 21186dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21187 const char *name, struct obstack *obstack,
21188 struct dwarf2_cu *cu,
d521ce57 21189 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21190 struct dwarf2_locexpr_baton **baton)
21191{
5e22e966 21192 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21193 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21194 struct comp_unit_head *cu_header = &cu->header;
c906108c 21195 struct dwarf_block *blk;
98bfdba5
PA
21196 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21197 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21198
21199 *value = 0;
21200 *bytes = NULL;
21201 *baton = NULL;
c906108c
SS
21202
21203 switch (attr->form)
21204 {
21205 case DW_FORM_addr:
336d760d 21206 case DW_FORM_addrx:
3019eac3 21207 case DW_FORM_GNU_addr_index:
ac56253d 21208 {
ac56253d
TT
21209 gdb_byte *data;
21210
98bfdba5
PA
21211 if (TYPE_LENGTH (type) != cu_header->addr_size)
21212 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21213 cu_header->addr_size,
98bfdba5 21214 TYPE_LENGTH (type));
ac56253d
TT
21215 /* Symbols of this form are reasonably rare, so we just
21216 piggyback on the existing location code rather than writing
21217 a new implementation of symbol_computed_ops. */
8d749320 21218 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21219 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21220 (*baton)->per_cu = cu->per_cu;
21221 gdb_assert ((*baton)->per_cu);
ac56253d 21222
98bfdba5 21223 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21224 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21225 (*baton)->data = data;
ac56253d
TT
21226
21227 data[0] = DW_OP_addr;
21228 store_unsigned_integer (&data[1], cu_header->addr_size,
21229 byte_order, DW_ADDR (attr));
21230 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21231 }
c906108c 21232 break;
4ac36638 21233 case DW_FORM_string:
93b5768b 21234 case DW_FORM_strp:
cf532bd1 21235 case DW_FORM_strx:
3019eac3 21236 case DW_FORM_GNU_str_index:
36586728 21237 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21238 /* DW_STRING is already allocated on the objfile obstack, point
21239 directly to it. */
d521ce57 21240 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21241 break;
c906108c
SS
21242 case DW_FORM_block1:
21243 case DW_FORM_block2:
21244 case DW_FORM_block4:
21245 case DW_FORM_block:
2dc7f7b3 21246 case DW_FORM_exprloc:
0224619f 21247 case DW_FORM_data16:
c906108c 21248 blk = DW_BLOCK (attr);
98bfdba5
PA
21249 if (TYPE_LENGTH (type) != blk->size)
21250 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21251 TYPE_LENGTH (type));
21252 *bytes = blk->data;
c906108c 21253 break;
2df3850c
JM
21254
21255 /* The DW_AT_const_value attributes are supposed to carry the
21256 symbol's value "represented as it would be on the target
21257 architecture." By the time we get here, it's already been
21258 converted to host endianness, so we just need to sign- or
21259 zero-extend it as appropriate. */
21260 case DW_FORM_data1:
3aef2284 21261 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21262 break;
c906108c 21263 case DW_FORM_data2:
3aef2284 21264 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21265 break;
c906108c 21266 case DW_FORM_data4:
3aef2284 21267 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21268 break;
c906108c 21269 case DW_FORM_data8:
3aef2284 21270 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21271 break;
21272
c906108c 21273 case DW_FORM_sdata:
663c44ac 21274 case DW_FORM_implicit_const:
98bfdba5 21275 *value = DW_SND (attr);
2df3850c
JM
21276 break;
21277
c906108c 21278 case DW_FORM_udata:
98bfdba5 21279 *value = DW_UNSND (attr);
c906108c 21280 break;
2df3850c 21281
c906108c 21282 default:
b98664d3 21283 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21284 dwarf_form_name (attr->form));
98bfdba5 21285 *value = 0;
c906108c
SS
21286 break;
21287 }
21288}
21289
2df3850c 21290
98bfdba5
PA
21291/* Copy constant value from an attribute to a symbol. */
21292
2df3850c 21293static void
ff39bb5e 21294dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21295 struct dwarf2_cu *cu)
2df3850c 21296{
5e22e966 21297 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21298 LONGEST value;
d521ce57 21299 const gdb_byte *bytes;
98bfdba5 21300 struct dwarf2_locexpr_baton *baton;
2df3850c 21301
98bfdba5 21302 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21303 sym->print_name (),
98bfdba5
PA
21304 &objfile->objfile_obstack, cu,
21305 &value, &bytes, &baton);
2df3850c 21306
98bfdba5
PA
21307 if (baton != NULL)
21308 {
98bfdba5 21309 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21310 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21311 }
21312 else if (bytes != NULL)
21313 {
21314 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21315 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21316 }
21317 else
21318 {
21319 SYMBOL_VALUE (sym) = value;
f1e6e072 21320 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21321 }
2df3850c
JM
21322}
21323
c906108c
SS
21324/* Return the type of the die in question using its DW_AT_type attribute. */
21325
21326static struct type *
e7c27a73 21327die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21328{
c906108c 21329 struct attribute *type_attr;
c906108c 21330
e142c38c 21331 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21332 if (!type_attr)
21333 {
5e22e966 21334 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21335 /* A missing DW_AT_type represents a void type. */
518817b3 21336 return objfile_type (objfile)->builtin_void;
c906108c 21337 }
348e048f 21338
673bfd45 21339 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21340}
21341
b4ba55a1
JB
21342/* True iff CU's producer generates GNAT Ada auxiliary information
21343 that allows to find parallel types through that information instead
21344 of having to do expensive parallel lookups by type name. */
21345
21346static int
21347need_gnat_info (struct dwarf2_cu *cu)
21348{
de4cb04a
JB
21349 /* Assume that the Ada compiler was GNAT, which always produces
21350 the auxiliary information. */
21351 return (cu->language == language_ada);
b4ba55a1
JB
21352}
21353
b4ba55a1
JB
21354/* Return the auxiliary type of the die in question using its
21355 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21356 attribute is not present. */
21357
21358static struct type *
21359die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21360{
b4ba55a1 21361 struct attribute *type_attr;
b4ba55a1
JB
21362
21363 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21364 if (!type_attr)
21365 return NULL;
21366
673bfd45 21367 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21368}
21369
21370/* If DIE has a descriptive_type attribute, then set the TYPE's
21371 descriptive type accordingly. */
21372
21373static void
21374set_descriptive_type (struct type *type, struct die_info *die,
21375 struct dwarf2_cu *cu)
21376{
21377 struct type *descriptive_type = die_descriptive_type (die, cu);
21378
21379 if (descriptive_type)
21380 {
21381 ALLOCATE_GNAT_AUX_TYPE (type);
21382 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21383 }
21384}
21385
c906108c
SS
21386/* Return the containing type of the die in question using its
21387 DW_AT_containing_type attribute. */
21388
21389static struct type *
e7c27a73 21390die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21391{
c906108c 21392 struct attribute *type_attr;
5e22e966 21393 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21394
e142c38c 21395 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21396 if (!type_attr)
21397 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21398 "[in module %s]"), objfile_name (objfile));
33ac96f0 21399
673bfd45 21400 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21401}
21402
ac9ec31b
DE
21403/* Return an error marker type to use for the ill formed type in DIE/CU. */
21404
21405static struct type *
21406build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21407{
976ca316
SM
21408 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21409 struct objfile *objfile = per_objfile->objfile;
528e1572 21410 char *saved;
ac9ec31b 21411
528e1572
SM
21412 std::string message
21413 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21414 objfile_name (objfile),
21415 sect_offset_str (cu->header.sect_off),
21416 sect_offset_str (die->sect_off));
efba19b0 21417 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21418
19f392bc 21419 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21420}
21421
673bfd45 21422/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21423 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21424 DW_AT_containing_type.
673bfd45
DE
21425 If there is no type substitute an error marker. */
21426
c906108c 21427static struct type *
ff39bb5e 21428lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21429 struct dwarf2_cu *cu)
c906108c 21430{
976ca316
SM
21431 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21432 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
21433 struct type *this_type;
21434
ac9ec31b
DE
21435 gdb_assert (attr->name == DW_AT_type
21436 || attr->name == DW_AT_GNAT_descriptive_type
21437 || attr->name == DW_AT_containing_type);
21438
673bfd45
DE
21439 /* First see if we have it cached. */
21440
36586728
TT
21441 if (attr->form == DW_FORM_GNU_ref_alt)
21442 {
21443 struct dwarf2_per_cu_data *per_cu;
0826b30a 21444 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21445
976ca316
SM
21446 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
21447 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 21448 }
cd6c91b4 21449 else if (attr->form_is_ref ())
673bfd45 21450 {
0826b30a 21451 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21452
976ca316 21453 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 21454 }
55f1336d 21455 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21456 {
ac9ec31b 21457 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21458
ac9ec31b 21459 return get_signatured_type (die, signature, cu);
673bfd45
DE
21460 }
21461 else
21462 {
b98664d3 21463 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21464 " at %s [in module %s]"),
21465 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21466 objfile_name (objfile));
ac9ec31b 21467 return build_error_marker_type (cu, die);
673bfd45
DE
21468 }
21469
21470 /* If not cached we need to read it in. */
21471
21472 if (this_type == NULL)
21473 {
ac9ec31b 21474 struct die_info *type_die = NULL;
673bfd45
DE
21475 struct dwarf2_cu *type_cu = cu;
21476
cd6c91b4 21477 if (attr->form_is_ref ())
ac9ec31b
DE
21478 type_die = follow_die_ref (die, attr, &type_cu);
21479 if (type_die == NULL)
21480 return build_error_marker_type (cu, die);
21481 /* If we find the type now, it's probably because the type came
3019eac3
DE
21482 from an inter-CU reference and the type's CU got expanded before
21483 ours. */
ac9ec31b 21484 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21485 }
21486
21487 /* If we still don't have a type use an error marker. */
21488
21489 if (this_type == NULL)
ac9ec31b 21490 return build_error_marker_type (cu, die);
673bfd45 21491
f792889a 21492 return this_type;
c906108c
SS
21493}
21494
673bfd45
DE
21495/* Return the type in DIE, CU.
21496 Returns NULL for invalid types.
21497
02142a6c 21498 This first does a lookup in die_type_hash,
673bfd45
DE
21499 and only reads the die in if necessary.
21500
21501 NOTE: This can be called when reading in partial or full symbols. */
21502
f792889a 21503static struct type *
e7c27a73 21504read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21505{
f792889a
DJ
21506 struct type *this_type;
21507
21508 this_type = get_die_type (die, cu);
21509 if (this_type)
21510 return this_type;
21511
673bfd45
DE
21512 return read_type_die_1 (die, cu);
21513}
21514
21515/* Read the type in DIE, CU.
21516 Returns NULL for invalid types. */
21517
21518static struct type *
21519read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21520{
21521 struct type *this_type = NULL;
21522
c906108c
SS
21523 switch (die->tag)
21524 {
21525 case DW_TAG_class_type:
680b30c7 21526 case DW_TAG_interface_type:
c906108c
SS
21527 case DW_TAG_structure_type:
21528 case DW_TAG_union_type:
f792889a 21529 this_type = read_structure_type (die, cu);
c906108c
SS
21530 break;
21531 case DW_TAG_enumeration_type:
f792889a 21532 this_type = read_enumeration_type (die, cu);
c906108c
SS
21533 break;
21534 case DW_TAG_subprogram:
21535 case DW_TAG_subroutine_type:
edb3359d 21536 case DW_TAG_inlined_subroutine:
f792889a 21537 this_type = read_subroutine_type (die, cu);
c906108c
SS
21538 break;
21539 case DW_TAG_array_type:
f792889a 21540 this_type = read_array_type (die, cu);
c906108c 21541 break;
72019c9c 21542 case DW_TAG_set_type:
f792889a 21543 this_type = read_set_type (die, cu);
72019c9c 21544 break;
c906108c 21545 case DW_TAG_pointer_type:
f792889a 21546 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21547 break;
21548 case DW_TAG_ptr_to_member_type:
f792889a 21549 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21550 break;
21551 case DW_TAG_reference_type:
4297a3f0
AV
21552 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21553 break;
21554 case DW_TAG_rvalue_reference_type:
21555 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21556 break;
21557 case DW_TAG_const_type:
f792889a 21558 this_type = read_tag_const_type (die, cu);
c906108c
SS
21559 break;
21560 case DW_TAG_volatile_type:
f792889a 21561 this_type = read_tag_volatile_type (die, cu);
c906108c 21562 break;
06d66ee9
TT
21563 case DW_TAG_restrict_type:
21564 this_type = read_tag_restrict_type (die, cu);
21565 break;
c906108c 21566 case DW_TAG_string_type:
f792889a 21567 this_type = read_tag_string_type (die, cu);
c906108c
SS
21568 break;
21569 case DW_TAG_typedef:
f792889a 21570 this_type = read_typedef (die, cu);
c906108c 21571 break;
a02abb62 21572 case DW_TAG_subrange_type:
f792889a 21573 this_type = read_subrange_type (die, cu);
a02abb62 21574 break;
c906108c 21575 case DW_TAG_base_type:
f792889a 21576 this_type = read_base_type (die, cu);
c906108c 21577 break;
81a17f79 21578 case DW_TAG_unspecified_type:
f792889a 21579 this_type = read_unspecified_type (die, cu);
81a17f79 21580 break;
0114d602
DJ
21581 case DW_TAG_namespace:
21582 this_type = read_namespace_type (die, cu);
21583 break;
f55ee35c
JK
21584 case DW_TAG_module:
21585 this_type = read_module_type (die, cu);
21586 break;
a2c2acaf
MW
21587 case DW_TAG_atomic_type:
21588 this_type = read_tag_atomic_type (die, cu);
21589 break;
c906108c 21590 default:
b98664d3 21591 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21592 dwarf_tag_name (die->tag));
c906108c
SS
21593 break;
21594 }
63d06c5c 21595
f792889a 21596 return this_type;
63d06c5c
DC
21597}
21598
abc72ce4
DE
21599/* See if we can figure out if the class lives in a namespace. We do
21600 this by looking for a member function; its demangled name will
21601 contain namespace info, if there is any.
21602 Return the computed name or NULL.
21603 Space for the result is allocated on the objfile's obstack.
21604 This is the full-die version of guess_partial_die_structure_name.
21605 In this case we know DIE has no useful parent. */
21606
43816ebc 21607static const char *
abc72ce4
DE
21608guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21609{
21610 struct die_info *spec_die;
21611 struct dwarf2_cu *spec_cu;
21612 struct die_info *child;
5e22e966 21613 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21614
21615 spec_cu = cu;
21616 spec_die = die_specification (die, &spec_cu);
21617 if (spec_die != NULL)
21618 {
21619 die = spec_die;
21620 cu = spec_cu;
21621 }
21622
21623 for (child = die->child;
21624 child != NULL;
21625 child = child->sibling)
21626 {
21627 if (child->tag == DW_TAG_subprogram)
21628 {
73b9be8b 21629 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21630
7d45c7c3 21631 if (linkage_name != NULL)
abc72ce4 21632 {
43816ebc
TT
21633 gdb::unique_xmalloc_ptr<char> actual_name
21634 (language_class_name_from_physname (cu->language_defn,
21635 linkage_name));
21636 const char *name = NULL;
abc72ce4
DE
21637
21638 if (actual_name != NULL)
21639 {
15d034d0 21640 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21641
21642 if (die_name != NULL
43816ebc 21643 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21644 {
21645 /* Strip off the class name from the full name.
21646 We want the prefix. */
21647 int die_name_len = strlen (die_name);
43816ebc
TT
21648 int actual_name_len = strlen (actual_name.get ());
21649 const char *ptr = actual_name.get ();
abc72ce4
DE
21650
21651 /* Test for '::' as a sanity check. */
21652 if (actual_name_len > die_name_len + 2
43816ebc 21653 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21654 name = obstack_strndup (
e3b94546 21655 &objfile->per_bfd->storage_obstack,
43816ebc 21656 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21657 }
21658 }
abc72ce4
DE
21659 return name;
21660 }
21661 }
21662 }
21663
21664 return NULL;
21665}
21666
96408a79
SA
21667/* GCC might emit a nameless typedef that has a linkage name. Determine the
21668 prefix part in such case. See
21669 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21670
a121b7c1 21671static const char *
96408a79
SA
21672anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21673{
21674 struct attribute *attr;
e6a959d6 21675 const char *base;
96408a79
SA
21676
21677 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21678 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21679 return NULL;
21680
7d45c7c3 21681 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21682 return NULL;
21683
73b9be8b 21684 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21685 if (attr == NULL || DW_STRING (attr) == NULL)
21686 return NULL;
21687
21688 /* dwarf2_name had to be already called. */
21689 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21690
21691 /* Strip the base name, keep any leading namespaces/classes. */
21692 base = strrchr (DW_STRING (attr), ':');
21693 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21694 return "";
21695
5e22e966 21696 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21697 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21698 DW_STRING (attr),
21699 &base[-1] - DW_STRING (attr));
96408a79
SA
21700}
21701
fdde2d81 21702/* Return the name of the namespace/class that DIE is defined within,
0114d602 21703 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21704
0114d602
DJ
21705 For example, if we're within the method foo() in the following
21706 code:
21707
21708 namespace N {
21709 class C {
21710 void foo () {
21711 }
21712 };
21713 }
21714
21715 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21716
0d5cff50 21717static const char *
e142c38c 21718determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21719{
976ca316 21720 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
21721 struct die_info *parent, *spec_die;
21722 struct dwarf2_cu *spec_cu;
21723 struct type *parent_type;
a121b7c1 21724 const char *retval;
63d06c5c 21725
9c37b5ae 21726 if (cu->language != language_cplus
c44af4eb
TT
21727 && cu->language != language_fortran && cu->language != language_d
21728 && cu->language != language_rust)
0114d602
DJ
21729 return "";
21730
96408a79
SA
21731 retval = anonymous_struct_prefix (die, cu);
21732 if (retval)
21733 return retval;
21734
0114d602
DJ
21735 /* We have to be careful in the presence of DW_AT_specification.
21736 For example, with GCC 3.4, given the code
21737
21738 namespace N {
21739 void foo() {
21740 // Definition of N::foo.
21741 }
21742 }
21743
21744 then we'll have a tree of DIEs like this:
21745
21746 1: DW_TAG_compile_unit
21747 2: DW_TAG_namespace // N
21748 3: DW_TAG_subprogram // declaration of N::foo
21749 4: DW_TAG_subprogram // definition of N::foo
21750 DW_AT_specification // refers to die #3
21751
21752 Thus, when processing die #4, we have to pretend that we're in
21753 the context of its DW_AT_specification, namely the contex of die
21754 #3. */
21755 spec_cu = cu;
21756 spec_die = die_specification (die, &spec_cu);
21757 if (spec_die == NULL)
21758 parent = die->parent;
21759 else
63d06c5c 21760 {
0114d602
DJ
21761 parent = spec_die->parent;
21762 cu = spec_cu;
63d06c5c 21763 }
0114d602
DJ
21764
21765 if (parent == NULL)
21766 return "";
98bfdba5
PA
21767 else if (parent->building_fullname)
21768 {
21769 const char *name;
21770 const char *parent_name;
21771
21772 /* It has been seen on RealView 2.2 built binaries,
21773 DW_TAG_template_type_param types actually _defined_ as
21774 children of the parent class:
21775
21776 enum E {};
21777 template class <class Enum> Class{};
21778 Class<enum E> class_e;
21779
21780 1: DW_TAG_class_type (Class)
21781 2: DW_TAG_enumeration_type (E)
21782 3: DW_TAG_enumerator (enum1:0)
21783 3: DW_TAG_enumerator (enum2:1)
21784 ...
21785 2: DW_TAG_template_type_param
21786 DW_AT_type DW_FORM_ref_udata (E)
21787
21788 Besides being broken debug info, it can put GDB into an
21789 infinite loop. Consider:
21790
21791 When we're building the full name for Class<E>, we'll start
21792 at Class, and go look over its template type parameters,
21793 finding E. We'll then try to build the full name of E, and
21794 reach here. We're now trying to build the full name of E,
21795 and look over the parent DIE for containing scope. In the
21796 broken case, if we followed the parent DIE of E, we'd again
21797 find Class, and once again go look at its template type
21798 arguments, etc., etc. Simply don't consider such parent die
21799 as source-level parent of this die (it can't be, the language
21800 doesn't allow it), and break the loop here. */
21801 name = dwarf2_name (die, cu);
21802 parent_name = dwarf2_name (parent, cu);
b98664d3 21803 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21804 name ? name : "<unknown>",
21805 parent_name ? parent_name : "<unknown>");
21806 return "";
21807 }
63d06c5c 21808 else
0114d602
DJ
21809 switch (parent->tag)
21810 {
63d06c5c 21811 case DW_TAG_namespace:
0114d602 21812 parent_type = read_type_die (parent, cu);
acebe513
UW
21813 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21814 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21815 Work around this problem here. */
21816 if (cu->language == language_cplus
7d93a1e0 21817 && strcmp (parent_type->name (), "::") == 0)
acebe513 21818 return "";
0114d602 21819 /* We give a name to even anonymous namespaces. */
7d93a1e0 21820 return parent_type->name ();
63d06c5c 21821 case DW_TAG_class_type:
680b30c7 21822 case DW_TAG_interface_type:
63d06c5c 21823 case DW_TAG_structure_type:
0114d602 21824 case DW_TAG_union_type:
f55ee35c 21825 case DW_TAG_module:
0114d602 21826 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21827 if (parent_type->name () != NULL)
21828 return parent_type->name ();
0114d602
DJ
21829 else
21830 /* An anonymous structure is only allowed non-static data
21831 members; no typedefs, no member functions, et cetera.
21832 So it does not need a prefix. */
21833 return "";
abc72ce4 21834 case DW_TAG_compile_unit:
95554aad 21835 case DW_TAG_partial_unit:
abc72ce4
DE
21836 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21837 if (cu->language == language_cplus
976ca316 21838 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21839 && die->child != NULL
21840 && (die->tag == DW_TAG_class_type
21841 || die->tag == DW_TAG_structure_type
21842 || die->tag == DW_TAG_union_type))
21843 {
43816ebc 21844 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21845 if (name != NULL)
21846 return name;
21847 }
21848 return "";
0a4b0913
AB
21849 case DW_TAG_subprogram:
21850 /* Nested subroutines in Fortran get a prefix with the name
21851 of the parent's subroutine. */
21852 if (cu->language == language_fortran)
21853 {
21854 if ((die->tag == DW_TAG_subprogram)
21855 && (dwarf2_name (parent, cu) != NULL))
21856 return dwarf2_name (parent, cu);
21857 }
21858 return determine_prefix (parent, cu);
3d567982
TT
21859 case DW_TAG_enumeration_type:
21860 parent_type = read_type_die (parent, cu);
21861 if (TYPE_DECLARED_CLASS (parent_type))
21862 {
7d93a1e0
SM
21863 if (parent_type->name () != NULL)
21864 return parent_type->name ();
3d567982
TT
21865 return "";
21866 }
21867 /* Fall through. */
63d06c5c 21868 default:
8176b9b8 21869 return determine_prefix (parent, cu);
63d06c5c 21870 }
63d06c5c
DC
21871}
21872
3e43a32a
MS
21873/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21874 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21875 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21876 an obconcat, otherwise allocate storage for the result. The CU argument is
21877 used to determine the language and hence, the appropriate separator. */
987504bb 21878
f55ee35c 21879#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21880
21881static char *
f55ee35c
JK
21882typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21883 int physname, struct dwarf2_cu *cu)
63d06c5c 21884{
f55ee35c 21885 const char *lead = "";
5c315b68 21886 const char *sep;
63d06c5c 21887
3e43a32a
MS
21888 if (suffix == NULL || suffix[0] == '\0'
21889 || prefix == NULL || prefix[0] == '\0')
987504bb 21890 sep = "";
45280282
IB
21891 else if (cu->language == language_d)
21892 {
21893 /* For D, the 'main' function could be defined in any module, but it
21894 should never be prefixed. */
21895 if (strcmp (suffix, "D main") == 0)
21896 {
21897 prefix = "";
21898 sep = "";
21899 }
21900 else
21901 sep = ".";
21902 }
f55ee35c
JK
21903 else if (cu->language == language_fortran && physname)
21904 {
21905 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21906 DW_AT_MIPS_linkage_name is preferred and used instead. */
21907
21908 lead = "__";
21909 sep = "_MOD_";
21910 }
987504bb
JJ
21911 else
21912 sep = "::";
63d06c5c 21913
6dd47d34
DE
21914 if (prefix == NULL)
21915 prefix = "";
21916 if (suffix == NULL)
21917 suffix = "";
21918
987504bb
JJ
21919 if (obs == NULL)
21920 {
3e43a32a 21921 char *retval
224c3ddb
SM
21922 = ((char *)
21923 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21924
f55ee35c
JK
21925 strcpy (retval, lead);
21926 strcat (retval, prefix);
6dd47d34
DE
21927 strcat (retval, sep);
21928 strcat (retval, suffix);
63d06c5c
DC
21929 return retval;
21930 }
987504bb
JJ
21931 else
21932 {
21933 /* We have an obstack. */
f55ee35c 21934 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21935 }
63d06c5c
DC
21936}
21937
71c25dea
TT
21938/* Get name of a die, return NULL if not found. */
21939
15d034d0
TT
21940static const char *
21941dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21942 struct objfile *objfile)
71c25dea
TT
21943{
21944 if (name && cu->language == language_cplus)
21945 {
596dc4ad
TT
21946 gdb::unique_xmalloc_ptr<char> canon_name
21947 = cp_canonicalize_string (name);
71c25dea 21948
596dc4ad
TT
21949 if (canon_name != nullptr)
21950 name = objfile->intern (canon_name.get ());
71c25dea
TT
21951 }
21952
21953 return name;
c906108c
SS
21954}
21955
96553a0c
DE
21956/* Get name of a die, return NULL if not found.
21957 Anonymous namespaces are converted to their magic string. */
9219021c 21958
15d034d0 21959static const char *
e142c38c 21960dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21961{
21962 struct attribute *attr;
5e22e966 21963 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 21964
e142c38c 21965 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21966 if ((!attr || !DW_STRING (attr))
96553a0c 21967 && die->tag != DW_TAG_namespace
53832f31
TT
21968 && die->tag != DW_TAG_class_type
21969 && die->tag != DW_TAG_interface_type
21970 && die->tag != DW_TAG_structure_type
21971 && die->tag != DW_TAG_union_type)
71c25dea
TT
21972 return NULL;
21973
21974 switch (die->tag)
21975 {
21976 case DW_TAG_compile_unit:
95554aad 21977 case DW_TAG_partial_unit:
71c25dea
TT
21978 /* Compilation units have a DW_AT_name that is a filename, not
21979 a source language identifier. */
21980 case DW_TAG_enumeration_type:
21981 case DW_TAG_enumerator:
21982 /* These tags always have simple identifiers already; no need
21983 to canonicalize them. */
21984 return DW_STRING (attr);
907af001 21985
96553a0c
DE
21986 case DW_TAG_namespace:
21987 if (attr != NULL && DW_STRING (attr) != NULL)
21988 return DW_STRING (attr);
21989 return CP_ANONYMOUS_NAMESPACE_STR;
21990
907af001
UW
21991 case DW_TAG_class_type:
21992 case DW_TAG_interface_type:
21993 case DW_TAG_structure_type:
21994 case DW_TAG_union_type:
21995 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21996 structures or unions. These were of the form "._%d" in GCC 4.1,
21997 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21998 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21999 if (attr && DW_STRING (attr)
61012eef
GB
22000 && (startswith (DW_STRING (attr), "._")
22001 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22002 return NULL;
53832f31
TT
22003
22004 /* GCC might emit a nameless typedef that has a linkage name. See
22005 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22006 if (!attr || DW_STRING (attr) == NULL)
22007 {
73b9be8b 22008 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22009 if (attr == NULL || DW_STRING (attr) == NULL)
22010 return NULL;
22011
df5c6c50
JK
22012 /* Avoid demangling DW_STRING (attr) the second time on a second
22013 call for the same DIE. */
22014 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 22015 {
43816ebc
TT
22016 gdb::unique_xmalloc_ptr<char> demangled
22017 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
22018 if (demangled == nullptr)
22019 return nullptr;
43816ebc 22020
be1e3d3e 22021 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 22022 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 22023 }
67430cd0
TT
22024
22025 /* Strip any leading namespaces/classes, keep only the base name.
22026 DW_AT_name for named DIEs does not contain the prefixes. */
22027 const char *base = strrchr (DW_STRING (attr), ':');
22028 if (base && base > DW_STRING (attr) && base[-1] == ':')
22029 return &base[1];
22030 else
22031 return DW_STRING (attr);
53832f31 22032 }
907af001
UW
22033 break;
22034
71c25dea 22035 default:
907af001
UW
22036 break;
22037 }
22038
22039 if (!DW_STRING_IS_CANONICAL (attr))
22040 {
be1e3d3e
TT
22041 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22042 objfile);
907af001 22043 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22044 }
907af001 22045 return DW_STRING (attr);
9219021c
DC
22046}
22047
22048/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22049 is none. *EXT_CU is the CU containing DIE on input, and the CU
22050 containing the return value on output. */
9219021c
DC
22051
22052static struct die_info *
f2f0e013 22053dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22054{
22055 struct attribute *attr;
9219021c 22056
f2f0e013 22057 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22058 if (attr == NULL)
22059 return NULL;
22060
f2f0e013 22061 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22062}
22063
f9aca02d 22064static void
d97bc12b 22065dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22066{
22067 unsigned int i;
22068
d97bc12b 22069 print_spaces (indent, f);
9d8780f0 22070 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22071 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22072 sect_offset_str (die->sect_off));
d97bc12b
DE
22073
22074 if (die->parent != NULL)
22075 {
22076 print_spaces (indent, f);
9d8780f0
SM
22077 fprintf_unfiltered (f, " parent at offset: %s\n",
22078 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22079 }
22080
22081 print_spaces (indent, f);
22082 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22083 dwarf_bool_name (die->child != NULL));
c906108c 22084
d97bc12b
DE
22085 print_spaces (indent, f);
22086 fprintf_unfiltered (f, " attributes:\n");
22087
c906108c
SS
22088 for (i = 0; i < die->num_attrs; ++i)
22089 {
d97bc12b
DE
22090 print_spaces (indent, f);
22091 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22092 dwarf_attr_name (die->attrs[i].name),
22093 dwarf_form_name (die->attrs[i].form));
d97bc12b 22094
c906108c
SS
22095 switch (die->attrs[i].form)
22096 {
c906108c 22097 case DW_FORM_addr:
336d760d 22098 case DW_FORM_addrx:
3019eac3 22099 case DW_FORM_GNU_addr_index:
d97bc12b 22100 fprintf_unfiltered (f, "address: ");
5af949e3 22101 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22102 break;
22103 case DW_FORM_block2:
22104 case DW_FORM_block4:
22105 case DW_FORM_block:
22106 case DW_FORM_block1:
56eb65bd
SP
22107 fprintf_unfiltered (f, "block: size %s",
22108 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22109 break;
2dc7f7b3 22110 case DW_FORM_exprloc:
56eb65bd
SP
22111 fprintf_unfiltered (f, "expression: size %s",
22112 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22113 break;
0224619f
JK
22114 case DW_FORM_data16:
22115 fprintf_unfiltered (f, "constant of 16 bytes");
22116 break;
4568ecf9
DE
22117 case DW_FORM_ref_addr:
22118 fprintf_unfiltered (f, "ref address: ");
22119 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22120 break;
36586728
TT
22121 case DW_FORM_GNU_ref_alt:
22122 fprintf_unfiltered (f, "alt ref address: ");
22123 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22124 break;
10b3939b
DJ
22125 case DW_FORM_ref1:
22126 case DW_FORM_ref2:
22127 case DW_FORM_ref4:
4568ecf9
DE
22128 case DW_FORM_ref8:
22129 case DW_FORM_ref_udata:
d97bc12b 22130 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22131 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22132 break;
c906108c
SS
22133 case DW_FORM_data1:
22134 case DW_FORM_data2:
22135 case DW_FORM_data4:
ce5d95e1 22136 case DW_FORM_data8:
c906108c
SS
22137 case DW_FORM_udata:
22138 case DW_FORM_sdata:
43bbcdc2
PH
22139 fprintf_unfiltered (f, "constant: %s",
22140 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22141 break;
2dc7f7b3
TT
22142 case DW_FORM_sec_offset:
22143 fprintf_unfiltered (f, "section offset: %s",
22144 pulongest (DW_UNSND (&die->attrs[i])));
22145 break;
55f1336d 22146 case DW_FORM_ref_sig8:
ac9ec31b
DE
22147 fprintf_unfiltered (f, "signature: %s",
22148 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22149 break;
c906108c 22150 case DW_FORM_string:
4bdf3d34 22151 case DW_FORM_strp:
43988095 22152 case DW_FORM_line_strp:
cf532bd1 22153 case DW_FORM_strx:
3019eac3 22154 case DW_FORM_GNU_str_index:
36586728 22155 case DW_FORM_GNU_strp_alt:
8285870a 22156 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22157 DW_STRING (&die->attrs[i])
8285870a
JK
22158 ? DW_STRING (&die->attrs[i]) : "",
22159 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22160 break;
22161 case DW_FORM_flag:
22162 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22163 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22164 else
d97bc12b 22165 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22166 break;
2dc7f7b3
TT
22167 case DW_FORM_flag_present:
22168 fprintf_unfiltered (f, "flag: TRUE");
22169 break;
a8329558 22170 case DW_FORM_indirect:
0963b4bd
MS
22171 /* The reader will have reduced the indirect form to
22172 the "base form" so this form should not occur. */
5f48f8f3 22173 fprintf_unfiltered (f,
3e43a32a 22174 "unexpected attribute form: DW_FORM_indirect");
a8329558 22175 break;
663c44ac
JK
22176 case DW_FORM_implicit_const:
22177 fprintf_unfiltered (f, "constant: %s",
22178 plongest (DW_SND (&die->attrs[i])));
22179 break;
c906108c 22180 default:
d97bc12b 22181 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22182 die->attrs[i].form);
d97bc12b 22183 break;
c906108c 22184 }
d97bc12b 22185 fprintf_unfiltered (f, "\n");
c906108c
SS
22186 }
22187}
22188
f9aca02d 22189static void
d97bc12b 22190dump_die_for_error (struct die_info *die)
c906108c 22191{
d97bc12b
DE
22192 dump_die_shallow (gdb_stderr, 0, die);
22193}
22194
22195static void
22196dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22197{
22198 int indent = level * 4;
22199
22200 gdb_assert (die != NULL);
22201
22202 if (level >= max_level)
22203 return;
22204
22205 dump_die_shallow (f, indent, die);
22206
22207 if (die->child != NULL)
c906108c 22208 {
d97bc12b
DE
22209 print_spaces (indent, f);
22210 fprintf_unfiltered (f, " Children:");
22211 if (level + 1 < max_level)
22212 {
22213 fprintf_unfiltered (f, "\n");
22214 dump_die_1 (f, level + 1, max_level, die->child);
22215 }
22216 else
22217 {
3e43a32a
MS
22218 fprintf_unfiltered (f,
22219 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22220 }
22221 }
22222
22223 if (die->sibling != NULL && level > 0)
22224 {
22225 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22226 }
22227}
22228
d97bc12b
DE
22229/* This is called from the pdie macro in gdbinit.in.
22230 It's not static so gcc will keep a copy callable from gdb. */
22231
22232void
22233dump_die (struct die_info *die, int max_level)
22234{
22235 dump_die_1 (gdb_stdlog, 0, max_level, die);
22236}
22237
f9aca02d 22238static void
51545339 22239store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22240{
51545339 22241 void **slot;
c906108c 22242
9c541725
PA
22243 slot = htab_find_slot_with_hash (cu->die_hash, die,
22244 to_underlying (die->sect_off),
b64f50a1 22245 INSERT);
51545339
DJ
22246
22247 *slot = die;
c906108c
SS
22248}
22249
348e048f
DE
22250/* Follow reference or signature attribute ATTR of SRC_DIE.
22251 On entry *REF_CU is the CU of SRC_DIE.
22252 On exit *REF_CU is the CU of the result. */
22253
22254static struct die_info *
ff39bb5e 22255follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22256 struct dwarf2_cu **ref_cu)
22257{
22258 struct die_info *die;
22259
cd6c91b4 22260 if (attr->form_is_ref ())
348e048f 22261 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22262 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22263 die = follow_die_sig (src_die, attr, ref_cu);
22264 else
22265 {
22266 dump_die_for_error (src_die);
22267 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22268 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22269 }
22270
22271 return die;
03dd20cc
DJ
22272}
22273
5c631832 22274/* Follow reference OFFSET.
673bfd45
DE
22275 On entry *REF_CU is the CU of the source die referencing OFFSET.
22276 On exit *REF_CU is the CU of the result.
22277 Returns NULL if OFFSET is invalid. */
f504f079 22278
f9aca02d 22279static struct die_info *
9c541725 22280follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22281 struct dwarf2_cu **ref_cu)
c906108c 22282{
10b3939b 22283 struct die_info temp_die;
f2f0e013 22284 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 22285 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 22286
348e048f
DE
22287 gdb_assert (cu->per_cu != NULL);
22288
98bfdba5
PA
22289 target_cu = cu;
22290
3019eac3 22291 if (cu->per_cu->is_debug_types)
348e048f
DE
22292 {
22293 /* .debug_types CUs cannot reference anything outside their CU.
22294 If they need to, they have to reference a signatured type via
55f1336d 22295 DW_FORM_ref_sig8. */
4057dfde 22296 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22297 return NULL;
348e048f 22298 }
36586728 22299 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22300 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22301 {
22302 struct dwarf2_per_cu_data *per_cu;
9a619af0 22303
9c541725 22304 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 22305 per_objfile);
03dd20cc
DJ
22306
22307 /* If necessary, add it to the queue and load its DIEs. */
976ca316
SM
22308 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22309 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
03dd20cc 22310
976ca316 22311 target_cu = per_objfile->get_cu (per_cu);
10b3939b 22312 }
98bfdba5
PA
22313 else if (cu->dies == NULL)
22314 {
22315 /* We're loading full DIEs during partial symbol reading. */
976ca316
SM
22316 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22317 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
98bfdba5 22318 }
c906108c 22319
f2f0e013 22320 *ref_cu = target_cu;
9c541725 22321 temp_die.sect_off = sect_off;
c24bdb02
KS
22322
22323 if (target_cu != cu)
22324 target_cu->ancestor = cu;
22325
9a3c8263 22326 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22327 &temp_die,
22328 to_underlying (sect_off));
5c631832 22329}
10b3939b 22330
5c631832
JK
22331/* Follow reference attribute ATTR of SRC_DIE.
22332 On entry *REF_CU is the CU of SRC_DIE.
22333 On exit *REF_CU is the CU of the result. */
22334
22335static struct die_info *
ff39bb5e 22336follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22337 struct dwarf2_cu **ref_cu)
22338{
0826b30a 22339 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22340 struct dwarf2_cu *cu = *ref_cu;
22341 struct die_info *die;
22342
9c541725 22343 die = follow_die_offset (sect_off,
36586728
TT
22344 (attr->form == DW_FORM_GNU_ref_alt
22345 || cu->per_cu->is_dwz),
22346 ref_cu);
5c631832 22347 if (!die)
9d8780f0
SM
22348 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22349 "at %s [in module %s]"),
22350 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22351 objfile_name (cu->per_objfile->objfile));
348e048f 22352
5c631832
JK
22353 return die;
22354}
22355
d4c9a4f8 22356/* See read.h. */
5c631832
JK
22357
22358struct dwarf2_locexpr_baton
9c541725 22359dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22360 dwarf2_per_cu_data *per_cu,
976ca316 22361 dwarf2_per_objfile *per_objfile,
8b9737bf 22362 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22363 void *baton, bool resolve_abstract_p)
5c631832 22364{
5c631832
JK
22365 struct die_info *die;
22366 struct attribute *attr;
22367 struct dwarf2_locexpr_baton retval;
976ca316 22368 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 22369
976ca316 22370 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 22371 if (cu == nullptr)
976ca316 22372 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
22373
22374 if (cu == nullptr)
cc12ce38
DE
22375 {
22376 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22377 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22378 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22379 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22380 }
918dd910 22381
9c541725 22382 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22383 if (!die)
9d8780f0
SM
22384 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22385 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22386
22387 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22388 if (!attr && resolve_abstract_p
976ca316
SM
22389 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22390 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22391 {
22392 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22393 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22394 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22395
3360b6e7 22396 for (const auto &cand_off
976ca316 22397 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22398 {
3360b6e7
TV
22399 struct dwarf2_cu *cand_cu = cu;
22400 struct die_info *cand
22401 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22402 if (!cand
22403 || !cand->parent
e4a62c65
TV
22404 || cand->parent->tag != DW_TAG_subprogram)
22405 continue;
22406
22407 CORE_ADDR pc_low, pc_high;
22408 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22409 if (pc_low == ((CORE_ADDR) -1))
22410 continue;
22411 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22412 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22413 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22414 continue;
22415
22416 die = cand;
22417 attr = dwarf2_attr (die, DW_AT_location, cu);
22418 break;
22419 }
22420 }
22421
5c631832
JK
22422 if (!attr)
22423 {
e103e986
JK
22424 /* DWARF: "If there is no such attribute, then there is no effect.".
22425 DATA is ignored if SIZE is 0. */
5c631832 22426
e103e986 22427 retval.data = NULL;
5c631832
JK
22428 retval.size = 0;
22429 }
cd6c91b4 22430 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22431 {
22432 struct dwarf2_loclist_baton loclist_baton;
22433 CORE_ADDR pc = (*get_frame_pc) (baton);
22434 size_t size;
22435
22436 fill_in_loclist_baton (cu, &loclist_baton, attr);
22437
22438 retval.data = dwarf2_find_location_expression (&loclist_baton,
22439 &size, pc);
22440 retval.size = size;
22441 }
5c631832
JK
22442 else
22443 {
4fc6c0d5 22444 if (!attr->form_is_block ())
9d8780f0 22445 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22446 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22447 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22448
22449 retval.data = DW_BLOCK (attr)->data;
22450 retval.size = DW_BLOCK (attr)->size;
22451 }
976ca316 22452 retval.per_objfile = per_objfile;
5c631832 22453 retval.per_cu = cu->per_cu;
918dd910 22454
976ca316 22455 per_objfile->age_comp_units ();
918dd910 22456
5c631832 22457 return retval;
348e048f
DE
22458}
22459
d4c9a4f8 22460/* See read.h. */
8b9737bf
TT
22461
22462struct dwarf2_locexpr_baton
22463dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22464 dwarf2_per_cu_data *per_cu,
14095eb3 22465 dwarf2_per_objfile *per_objfile,
8b9737bf
TT
22466 CORE_ADDR (*get_frame_pc) (void *baton),
22467 void *baton)
22468{
9c541725 22469 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22470
14095eb3
SM
22471 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22472 get_frame_pc, baton);
8b9737bf
TT
22473}
22474
b6807d98
TT
22475/* Write a constant of a given type as target-ordered bytes into
22476 OBSTACK. */
22477
22478static const gdb_byte *
22479write_constant_as_bytes (struct obstack *obstack,
22480 enum bfd_endian byte_order,
22481 struct type *type,
22482 ULONGEST value,
22483 LONGEST *len)
22484{
22485 gdb_byte *result;
22486
22487 *len = TYPE_LENGTH (type);
224c3ddb 22488 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22489 store_unsigned_integer (result, *len, byte_order, value);
22490
22491 return result;
22492}
22493
d4c9a4f8 22494/* See read.h. */
b6807d98
TT
22495
22496const gdb_byte *
9c541725 22497dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 22498 dwarf2_per_cu_data *per_cu,
14095eb3 22499 dwarf2_per_objfile *per_objfile,
d4c9a4f8 22500 obstack *obstack,
b6807d98
TT
22501 LONGEST *len)
22502{
b6807d98
TT
22503 struct die_info *die;
22504 struct attribute *attr;
22505 const gdb_byte *result = NULL;
22506 struct type *type;
22507 LONGEST value;
22508 enum bfd_endian byte_order;
14095eb3 22509 struct objfile *objfile = per_objfile->objfile;
b6807d98 22510
7188ed02 22511 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22512 if (cu == nullptr)
22513 cu = load_cu (per_cu, per_objfile, false);
22514
22515 if (cu == nullptr)
cc12ce38
DE
22516 {
22517 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22518 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22519 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22520 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22521 }
b6807d98 22522
9c541725 22523 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22524 if (!die)
9d8780f0
SM
22525 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22526 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22527
22528 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22529 if (attr == NULL)
22530 return NULL;
22531
e3b94546 22532 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22533 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22534
22535 switch (attr->form)
22536 {
22537 case DW_FORM_addr:
336d760d 22538 case DW_FORM_addrx:
b6807d98
TT
22539 case DW_FORM_GNU_addr_index:
22540 {
22541 gdb_byte *tem;
22542
22543 *len = cu->header.addr_size;
224c3ddb 22544 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22545 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22546 result = tem;
22547 }
22548 break;
22549 case DW_FORM_string:
22550 case DW_FORM_strp:
cf532bd1 22551 case DW_FORM_strx:
b6807d98
TT
22552 case DW_FORM_GNU_str_index:
22553 case DW_FORM_GNU_strp_alt:
22554 /* DW_STRING is already allocated on the objfile obstack, point
22555 directly to it. */
22556 result = (const gdb_byte *) DW_STRING (attr);
22557 *len = strlen (DW_STRING (attr));
22558 break;
22559 case DW_FORM_block1:
22560 case DW_FORM_block2:
22561 case DW_FORM_block4:
22562 case DW_FORM_block:
22563 case DW_FORM_exprloc:
0224619f 22564 case DW_FORM_data16:
b6807d98
TT
22565 result = DW_BLOCK (attr)->data;
22566 *len = DW_BLOCK (attr)->size;
22567 break;
22568
22569 /* The DW_AT_const_value attributes are supposed to carry the
22570 symbol's value "represented as it would be on the target
22571 architecture." By the time we get here, it's already been
22572 converted to host endianness, so we just need to sign- or
22573 zero-extend it as appropriate. */
22574 case DW_FORM_data1:
22575 type = die_type (die, cu);
22576 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22577 if (result == NULL)
22578 result = write_constant_as_bytes (obstack, byte_order,
22579 type, value, len);
22580 break;
22581 case DW_FORM_data2:
22582 type = die_type (die, cu);
22583 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22584 if (result == NULL)
22585 result = write_constant_as_bytes (obstack, byte_order,
22586 type, value, len);
22587 break;
22588 case DW_FORM_data4:
22589 type = die_type (die, cu);
22590 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22591 if (result == NULL)
22592 result = write_constant_as_bytes (obstack, byte_order,
22593 type, value, len);
22594 break;
22595 case DW_FORM_data8:
22596 type = die_type (die, cu);
22597 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22598 if (result == NULL)
22599 result = write_constant_as_bytes (obstack, byte_order,
22600 type, value, len);
22601 break;
22602
22603 case DW_FORM_sdata:
663c44ac 22604 case DW_FORM_implicit_const:
b6807d98
TT
22605 type = die_type (die, cu);
22606 result = write_constant_as_bytes (obstack, byte_order,
22607 type, DW_SND (attr), len);
22608 break;
22609
22610 case DW_FORM_udata:
22611 type = die_type (die, cu);
22612 result = write_constant_as_bytes (obstack, byte_order,
22613 type, DW_UNSND (attr), len);
22614 break;
22615
22616 default:
b98664d3 22617 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22618 dwarf_form_name (attr->form));
22619 break;
22620 }
22621
22622 return result;
22623}
22624
d4c9a4f8 22625/* See read.h. */
7942e96e
AA
22626
22627struct type *
9c541725 22628dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
22629 dwarf2_per_cu_data *per_cu,
22630 dwarf2_per_objfile *per_objfile)
7942e96e 22631{
7942e96e
AA
22632 struct die_info *die;
22633
7188ed02 22634 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22635 if (cu == nullptr)
22636 cu = load_cu (per_cu, per_objfile, false);
22637
22638 if (cu == nullptr)
22639 return nullptr;
7942e96e 22640
9c541725 22641 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22642 if (!die)
22643 return NULL;
22644
22645 return die_type (die, cu);
22646}
22647
8cb5117c 22648/* See read.h. */
8a9b8146
TT
22649
22650struct type *
b64f50a1 22651dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
22652 dwarf2_per_cu_data *per_cu,
22653 dwarf2_per_objfile *per_objfile)
8a9b8146 22654{
9c541725 22655 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 22656 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
22657}
22658
ac9ec31b 22659/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22660 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22661 On exit *REF_CU is the CU of the result.
22662 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22663
22664static struct die_info *
ac9ec31b
DE
22665follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22666 struct dwarf2_cu **ref_cu)
348e048f 22667{
348e048f 22668 struct die_info temp_die;
c24bdb02 22669 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 22670 struct die_info *die;
976ca316 22671 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 22672
348e048f 22673
ac9ec31b
DE
22674 /* While it might be nice to assert sig_type->type == NULL here,
22675 we can get here for DW_AT_imported_declaration where we need
22676 the DIE not the type. */
348e048f
DE
22677
22678 /* If necessary, add it to the queue and load its DIEs. */
22679
976ca316 22680 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
120ce1b5 22681 language_minimal))
976ca316 22682 read_signatured_type (sig_type, per_objfile);
348e048f 22683
976ca316 22684 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 22685 gdb_assert (sig_cu != NULL);
9c541725
PA
22686 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22687 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22688 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22689 to_underlying (temp_die.sect_off));
348e048f
DE
22690 if (die)
22691 {
796a7ff8
DE
22692 /* For .gdb_index version 7 keep track of included TUs.
22693 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
22694 if (per_objfile->per_bfd->index_table != NULL
22695 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22696 {
ae640021 22697 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22698 }
22699
348e048f 22700 *ref_cu = sig_cu;
c24bdb02
KS
22701 if (sig_cu != cu)
22702 sig_cu->ancestor = cu;
22703
348e048f
DE
22704 return die;
22705 }
22706
ac9ec31b
DE
22707 return NULL;
22708}
22709
22710/* Follow signatured type referenced by ATTR in SRC_DIE.
22711 On entry *REF_CU is the CU of SRC_DIE.
22712 On exit *REF_CU is the CU of the result.
22713 The result is the DIE of the type.
22714 If the referenced type cannot be found an error is thrown. */
22715
22716static struct die_info *
ff39bb5e 22717follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22718 struct dwarf2_cu **ref_cu)
22719{
22720 ULONGEST signature = DW_SIGNATURE (attr);
22721 struct signatured_type *sig_type;
22722 struct die_info *die;
22723
22724 gdb_assert (attr->form == DW_FORM_ref_sig8);
22725
a2ce51a0 22726 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22727 /* sig_type will be NULL if the signatured type is missing from
22728 the debug info. */
22729 if (sig_type == NULL)
22730 {
22731 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22732 " from DIE at %s [in module %s]"),
22733 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22734 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22735 }
22736
22737 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22738 if (die == NULL)
22739 {
22740 dump_die_for_error (src_die);
22741 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22742 " from DIE at %s [in module %s]"),
22743 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22744 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22745 }
22746
22747 return die;
22748}
22749
22750/* Get the type specified by SIGNATURE referenced in DIE/CU,
22751 reading in and processing the type unit if necessary. */
22752
22753static struct type *
22754get_signatured_type (struct die_info *die, ULONGEST signature,
22755 struct dwarf2_cu *cu)
22756{
976ca316 22757 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
22758 struct signatured_type *sig_type;
22759 struct dwarf2_cu *type_cu;
22760 struct die_info *type_die;
22761 struct type *type;
22762
a2ce51a0 22763 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22764 /* sig_type will be NULL if the signatured type is missing from
22765 the debug info. */
22766 if (sig_type == NULL)
22767 {
b98664d3 22768 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22769 " from DIE at %s [in module %s]"),
22770 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22771 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22772 return build_error_marker_type (cu, die);
22773 }
22774
22775 /* If we already know the type we're done. */
976ca316 22776 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
22777 if (type != nullptr)
22778 return type;
ac9ec31b
DE
22779
22780 type_cu = cu;
22781 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22782 if (type_die != NULL)
22783 {
22784 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22785 is created. This is important, for example, because for c++ classes
22786 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22787 type = read_type_die (type_die, type_cu);
22788 if (type == NULL)
22789 {
b98664d3 22790 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22791 " referenced from DIE at %s [in module %s]"),
22792 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22793 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22794 type = build_error_marker_type (cu, die);
22795 }
22796 }
22797 else
22798 {
b98664d3 22799 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22800 " from DIE at %s [in module %s]"),
22801 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22802 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22803 type = build_error_marker_type (cu, die);
22804 }
e286671b 22805
976ca316 22806 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
22807
22808 return type;
22809}
22810
22811/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22812 reading in and processing the type unit if necessary. */
22813
22814static struct type *
ff39bb5e 22815get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22816 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22817{
22818 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22819 if (attr->form_is_ref ())
ac9ec31b
DE
22820 {
22821 struct dwarf2_cu *type_cu = cu;
22822 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22823
22824 return read_type_die (type_die, type_cu);
22825 }
22826 else if (attr->form == DW_FORM_ref_sig8)
22827 {
22828 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22829 }
22830 else
22831 {
976ca316 22832 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 22833
b98664d3 22834 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22835 " at %s [in module %s]"),
22836 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 22837 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22838 return build_error_marker_type (cu, die);
22839 }
348e048f
DE
22840}
22841
e5fe5e75 22842/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22843
22844static void
ab432490
SM
22845load_full_type_unit (dwarf2_per_cu_data *per_cu,
22846 dwarf2_per_objfile *per_objfile)
348e048f 22847{
52dc124a 22848 struct signatured_type *sig_type;
348e048f 22849
f4dc4d17 22850 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22851 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22852
6721b2ec
DE
22853 /* We have the per_cu, but we need the signatured_type.
22854 Fortunately this is an easy translation. */
22855 gdb_assert (per_cu->is_debug_types);
22856 sig_type = (struct signatured_type *) per_cu;
348e048f 22857
7188ed02 22858 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22859
ab432490 22860 read_signatured_type (sig_type, per_objfile);
348e048f 22861
7188ed02 22862 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
22863}
22864
3019eac3
DE
22865/* Read in a signatured type and build its CU and DIEs.
22866 If the type is a stub for the real type in a DWO file,
22867 read in the real type from the DWO file as well. */
dee91e82
DE
22868
22869static void
ab432490
SM
22870read_signatured_type (signatured_type *sig_type,
22871 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22872{
22873 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22874
3019eac3 22875 gdb_assert (per_cu->is_debug_types);
7188ed02 22876 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22877
2e671100 22878 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
22879
22880 if (!reader.dummy_p)
22881 {
22882 struct dwarf2_cu *cu = reader.cu;
22883 const gdb_byte *info_ptr = reader.info_ptr;
22884
22885 gdb_assert (cu->die_hash == NULL);
22886 cu->die_hash =
22887 htab_create_alloc_ex (cu->header.length / 12,
22888 die_hash,
22889 die_eq,
22890 NULL,
22891 &cu->comp_unit_obstack,
22892 hashtab_obstack_allocate,
22893 dummy_obstack_deallocate);
22894
3e225074 22895 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22896 reader.comp_unit_die->child
22897 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22898 reader.comp_unit_die);
22899 cu->dies = reader.comp_unit_die;
22900 /* comp_unit_die is not stored in die_hash, no need. */
22901
22902 /* We try not to read any attributes in this function, because
22903 not all CUs needed for references have been loaded yet, and
22904 symbol table processing isn't initialized. But we have to
22905 set the CU language, or we won't be able to build types
22906 correctly. Similarly, if we do not read the producer, we can
22907 not apply producer-specific interpretation. */
22908 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22909
22910 reader.keep ();
c0ab21c2
TT
22911 }
22912
7ee85ab1 22913 sig_type->per_cu.tu_read = 1;
c906108c
SS
22914}
22915
c906108c
SS
22916/* Decode simple location descriptions.
22917 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22918 the location and return the value. If COMPUTED is non-null, it is
22919 set to true to indicate that decoding was successful, and false
22920 otherwise. If COMPUTED is null, then this function may emit a
22921 complaint. */
c906108c
SS
22922
22923static CORE_ADDR
7d79de9a 22924decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22925{
5e22e966 22926 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22927 size_t i;
22928 size_t size = blk->size;
d521ce57 22929 const gdb_byte *data = blk->data;
21ae7a4d
JK
22930 CORE_ADDR stack[64];
22931 int stacki;
22932 unsigned int bytes_read, unsnd;
22933 gdb_byte op;
c906108c 22934
7d79de9a
TT
22935 if (computed != nullptr)
22936 *computed = false;
22937
21ae7a4d
JK
22938 i = 0;
22939 stacki = 0;
22940 stack[stacki] = 0;
22941 stack[++stacki] = 0;
22942
22943 while (i < size)
22944 {
22945 op = data[i++];
22946 switch (op)
22947 {
22948 case DW_OP_lit0:
22949 case DW_OP_lit1:
22950 case DW_OP_lit2:
22951 case DW_OP_lit3:
22952 case DW_OP_lit4:
22953 case DW_OP_lit5:
22954 case DW_OP_lit6:
22955 case DW_OP_lit7:
22956 case DW_OP_lit8:
22957 case DW_OP_lit9:
22958 case DW_OP_lit10:
22959 case DW_OP_lit11:
22960 case DW_OP_lit12:
22961 case DW_OP_lit13:
22962 case DW_OP_lit14:
22963 case DW_OP_lit15:
22964 case DW_OP_lit16:
22965 case DW_OP_lit17:
22966 case DW_OP_lit18:
22967 case DW_OP_lit19:
22968 case DW_OP_lit20:
22969 case DW_OP_lit21:
22970 case DW_OP_lit22:
22971 case DW_OP_lit23:
22972 case DW_OP_lit24:
22973 case DW_OP_lit25:
22974 case DW_OP_lit26:
22975 case DW_OP_lit27:
22976 case DW_OP_lit28:
22977 case DW_OP_lit29:
22978 case DW_OP_lit30:
22979 case DW_OP_lit31:
22980 stack[++stacki] = op - DW_OP_lit0;
22981 break;
f1bea926 22982
21ae7a4d
JK
22983 case DW_OP_reg0:
22984 case DW_OP_reg1:
22985 case DW_OP_reg2:
22986 case DW_OP_reg3:
22987 case DW_OP_reg4:
22988 case DW_OP_reg5:
22989 case DW_OP_reg6:
22990 case DW_OP_reg7:
22991 case DW_OP_reg8:
22992 case DW_OP_reg9:
22993 case DW_OP_reg10:
22994 case DW_OP_reg11:
22995 case DW_OP_reg12:
22996 case DW_OP_reg13:
22997 case DW_OP_reg14:
22998 case DW_OP_reg15:
22999 case DW_OP_reg16:
23000 case DW_OP_reg17:
23001 case DW_OP_reg18:
23002 case DW_OP_reg19:
23003 case DW_OP_reg20:
23004 case DW_OP_reg21:
23005 case DW_OP_reg22:
23006 case DW_OP_reg23:
23007 case DW_OP_reg24:
23008 case DW_OP_reg25:
23009 case DW_OP_reg26:
23010 case DW_OP_reg27:
23011 case DW_OP_reg28:
23012 case DW_OP_reg29:
23013 case DW_OP_reg30:
23014 case DW_OP_reg31:
23015 stack[++stacki] = op - DW_OP_reg0;
23016 if (i < size)
7d79de9a
TT
23017 {
23018 if (computed == nullptr)
23019 dwarf2_complex_location_expr_complaint ();
23020 else
23021 return 0;
23022 }
21ae7a4d 23023 break;
c906108c 23024
21ae7a4d
JK
23025 case DW_OP_regx:
23026 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23027 i += bytes_read;
23028 stack[++stacki] = unsnd;
23029 if (i < size)
7d79de9a
TT
23030 {
23031 if (computed == nullptr)
23032 dwarf2_complex_location_expr_complaint ();
23033 else
23034 return 0;
23035 }
21ae7a4d 23036 break;
c906108c 23037
21ae7a4d 23038 case DW_OP_addr:
c8a7a66f
TT
23039 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23040 &bytes_read);
21ae7a4d
JK
23041 i += bytes_read;
23042 break;
d53d4ac5 23043
21ae7a4d
JK
23044 case DW_OP_const1u:
23045 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23046 i += 1;
23047 break;
23048
23049 case DW_OP_const1s:
23050 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23051 i += 1;
23052 break;
23053
23054 case DW_OP_const2u:
23055 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23056 i += 2;
23057 break;
23058
23059 case DW_OP_const2s:
23060 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23061 i += 2;
23062 break;
d53d4ac5 23063
21ae7a4d
JK
23064 case DW_OP_const4u:
23065 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23066 i += 4;
23067 break;
23068
23069 case DW_OP_const4s:
23070 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23071 i += 4;
23072 break;
23073
585861ea
JK
23074 case DW_OP_const8u:
23075 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23076 i += 8;
23077 break;
23078
21ae7a4d
JK
23079 case DW_OP_constu:
23080 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23081 &bytes_read);
23082 i += bytes_read;
23083 break;
23084
23085 case DW_OP_consts:
23086 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23087 i += bytes_read;
23088 break;
23089
23090 case DW_OP_dup:
23091 stack[stacki + 1] = stack[stacki];
23092 stacki++;
23093 break;
23094
23095 case DW_OP_plus:
23096 stack[stacki - 1] += stack[stacki];
23097 stacki--;
23098 break;
23099
23100 case DW_OP_plus_uconst:
23101 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23102 &bytes_read);
23103 i += bytes_read;
23104 break;
23105
23106 case DW_OP_minus:
23107 stack[stacki - 1] -= stack[stacki];
23108 stacki--;
23109 break;
23110
23111 case DW_OP_deref:
23112 /* If we're not the last op, then we definitely can't encode
23113 this using GDB's address_class enum. This is valid for partial
23114 global symbols, although the variable's address will be bogus
23115 in the psymtab. */
23116 if (i < size)
7d79de9a
TT
23117 {
23118 if (computed == nullptr)
23119 dwarf2_complex_location_expr_complaint ();
23120 else
23121 return 0;
23122 }
21ae7a4d
JK
23123 break;
23124
23125 case DW_OP_GNU_push_tls_address:
4aa4e28b 23126 case DW_OP_form_tls_address:
21ae7a4d
JK
23127 /* The top of the stack has the offset from the beginning
23128 of the thread control block at which the variable is located. */
23129 /* Nothing should follow this operator, so the top of stack would
23130 be returned. */
23131 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23132 address will be bogus in the psymtab. Make it always at least
23133 non-zero to not look as a variable garbage collected by linker
23134 which have DW_OP_addr 0. */
21ae7a4d 23135 if (i < size)
7d79de9a
TT
23136 {
23137 if (computed == nullptr)
23138 dwarf2_complex_location_expr_complaint ();
23139 else
23140 return 0;
23141 }
585861ea 23142 stack[stacki]++;
21ae7a4d
JK
23143 break;
23144
23145 case DW_OP_GNU_uninit:
7d79de9a
TT
23146 if (computed != nullptr)
23147 return 0;
21ae7a4d
JK
23148 break;
23149
336d760d 23150 case DW_OP_addrx:
3019eac3 23151 case DW_OP_GNU_addr_index:
49f6c839 23152 case DW_OP_GNU_const_index:
3019eac3
DE
23153 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23154 &bytes_read);
23155 i += bytes_read;
23156 break;
23157
21ae7a4d 23158 default:
7d79de9a
TT
23159 if (computed == nullptr)
23160 {
23161 const char *name = get_DW_OP_name (op);
21ae7a4d 23162
7d79de9a
TT
23163 if (name)
23164 complaint (_("unsupported stack op: '%s'"),
23165 name);
23166 else
23167 complaint (_("unsupported stack op: '%02x'"),
23168 op);
23169 }
21ae7a4d
JK
23170
23171 return (stack[stacki]);
d53d4ac5 23172 }
3c6e0cb3 23173
21ae7a4d
JK
23174 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23175 outside of the allocated space. Also enforce minimum>0. */
23176 if (stacki >= ARRAY_SIZE (stack) - 1)
23177 {
7d79de9a
TT
23178 if (computed == nullptr)
23179 complaint (_("location description stack overflow"));
21ae7a4d
JK
23180 return 0;
23181 }
23182
23183 if (stacki <= 0)
23184 {
7d79de9a
TT
23185 if (computed == nullptr)
23186 complaint (_("location description stack underflow"));
21ae7a4d
JK
23187 return 0;
23188 }
23189 }
7d79de9a
TT
23190
23191 if (computed != nullptr)
23192 *computed = true;
21ae7a4d 23193 return (stack[stacki]);
c906108c
SS
23194}
23195
23196/* memory allocation interface */
23197
c906108c 23198static struct dwarf_block *
7b5a2f43 23199dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23200{
8d749320 23201 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23202}
23203
c906108c 23204static struct die_info *
b60c80d6 23205dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23206{
23207 struct die_info *die;
b60c80d6
DJ
23208 size_t size = sizeof (struct die_info);
23209
23210 if (num_attrs > 1)
23211 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23212
b60c80d6 23213 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23214 memset (die, 0, sizeof (struct die_info));
23215 return (die);
23216}
2e276125
JB
23217
23218\f
a036ba48 23219
c90ec28a 23220/* Macro support. */
cf2c3c16 23221
9eac9650
TT
23222/* An overload of dwarf_decode_macros that finds the correct section
23223 and ensures it is read in before calling the other overload. */
23224
23225static void
23226dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23227 int section_is_gnu)
23228{
976ca316
SM
23229 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23230 struct objfile *objfile = per_objfile->objfile;
5a0e026f 23231 const struct line_header *lh = cu->line_header;
9eac9650
TT
23232 unsigned int offset_size = cu->header.offset_size;
23233 struct dwarf2_section_info *section;
23234 const char *section_name;
23235
23236 if (cu->dwo_unit != nullptr)
23237 {
23238 if (section_is_gnu)
23239 {
23240 section = &cu->dwo_unit->dwo_file->sections.macro;
23241 section_name = ".debug_macro.dwo";
23242 }
23243 else
23244 {
23245 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23246 section_name = ".debug_macinfo.dwo";
23247 }
23248 }
23249 else
23250 {
23251 if (section_is_gnu)
23252 {
976ca316 23253 section = &per_objfile->per_bfd->macro;
9eac9650
TT
23254 section_name = ".debug_macro";
23255 }
23256 else
23257 {
976ca316 23258 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
23259 section_name = ".debug_macinfo";
23260 }
23261 }
23262
23263 section->read (objfile);
23264 if (section->buffer == nullptr)
23265 {
23266 complaint (_("missing %s section"), section_name);
23267 return;
23268 }
23269
23270 buildsym_compunit *builder = cu->get_builder ();
23271
976ca316 23272 dwarf_decode_macros (per_objfile, builder, section, lh,
9eac9650
TT
23273 offset_size, offset, section_is_gnu);
23274}
23275
3019eac3
DE
23276/* Return the .debug_loc section to use for CU.
23277 For DWO files use .debug_loc.dwo. */
23278
23279static struct dwarf2_section_info *
23280cu_debug_loc_section (struct dwarf2_cu *cu)
23281{
976ca316 23282 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23283
3019eac3 23284 if (cu->dwo_unit)
43988095
JK
23285 {
23286 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23287
43988095
JK
23288 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23289 }
976ca316
SM
23290 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23291 : &per_objfile->per_bfd->loc);
3019eac3
DE
23292}
23293
8cf6f0b1
TT
23294/* A helper function that fills in a dwarf2_loclist_baton. */
23295
23296static void
23297fill_in_loclist_baton (struct dwarf2_cu *cu,
23298 struct dwarf2_loclist_baton *baton,
ff39bb5e 23299 const struct attribute *attr)
8cf6f0b1 23300{
976ca316 23301 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
23302 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23303
976ca316 23304 section->read (per_objfile->objfile);
8cf6f0b1 23305
976ca316 23306 baton->per_objfile = per_objfile;
8cf6f0b1
TT
23307 baton->per_cu = cu->per_cu;
23308 gdb_assert (baton->per_cu);
23309 /* We don't know how long the location list is, but make sure we
23310 don't run off the edge of the section. */
3019eac3
DE
23311 baton->size = section->size - DW_UNSND (attr);
23312 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23313 if (cu->base_address.has_value ())
23314 baton->base_address = *cu->base_address;
23315 else
23316 baton->base_address = 0;
f664829e 23317 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23318}
23319
4c2df51b 23320static void
ff39bb5e 23321dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23322 struct dwarf2_cu *cu, int is_block)
4c2df51b 23323{
976ca316
SM
23324 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23325 struct objfile *objfile = per_objfile->objfile;
3019eac3 23326 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23327
cd6c91b4 23328 if (attr->form_is_section_offset ()
3019eac3 23329 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23330 the section. If so, fall through to the complaint in the
23331 other branch. */
2c7d5afc 23332 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23333 {
0d53c4c4 23334 struct dwarf2_loclist_baton *baton;
4c2df51b 23335
8d749320 23336 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23337
8cf6f0b1 23338 fill_in_loclist_baton (cu, baton, attr);
be391dca 23339
2b24b6e4 23340 if (!cu->base_address.has_value ())
b98664d3 23341 complaint (_("Location list used without "
3e43a32a 23342 "specifying the CU base address."));
4c2df51b 23343
f1e6e072
TT
23344 SYMBOL_ACLASS_INDEX (sym) = (is_block
23345 ? dwarf2_loclist_block_index
23346 : dwarf2_loclist_index);
0d53c4c4
DJ
23347 SYMBOL_LOCATION_BATON (sym) = baton;
23348 }
23349 else
23350 {
23351 struct dwarf2_locexpr_baton *baton;
23352
8d749320 23353 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 23354 baton->per_objfile = per_objfile;
ae0d2f24
UW
23355 baton->per_cu = cu->per_cu;
23356 gdb_assert (baton->per_cu);
0d53c4c4 23357
4fc6c0d5 23358 if (attr->form_is_block ())
0d53c4c4
DJ
23359 {
23360 /* Note that we're just copying the block's data pointer
23361 here, not the actual data. We're still pointing into the
6502dd73
DJ
23362 info_buffer for SYM's objfile; right now we never release
23363 that buffer, but when we do clean up properly this may
23364 need to change. */
0d53c4c4
DJ
23365 baton->size = DW_BLOCK (attr)->size;
23366 baton->data = DW_BLOCK (attr)->data;
23367 }
23368 else
23369 {
23370 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23371 sym->natural_name ());
0d53c4c4 23372 baton->size = 0;
0d53c4c4 23373 }
6e70227d 23374
f1e6e072
TT
23375 SYMBOL_ACLASS_INDEX (sym) = (is_block
23376 ? dwarf2_locexpr_block_index
23377 : dwarf2_locexpr_index);
0d53c4c4
DJ
23378 SYMBOL_LOCATION_BATON (sym) = baton;
23379 }
4c2df51b 23380}
6502dd73 23381
2e6a9f79 23382/* See read.h. */
96408a79 23383
2e6a9f79
SM
23384const comp_unit_head *
23385dwarf2_per_cu_data::get_header () const
96408a79 23386{
2e6a9f79
SM
23387 if (!m_header_read_in)
23388 {
23389 const gdb_byte *info_ptr
23390 = this->section->buffer + to_underlying (this->sect_off);
96408a79 23391
2e6a9f79 23392 memset (&m_header, 0, sizeof (m_header));
96408a79 23393
2e6a9f79
SM
23394 read_comp_unit_head (&m_header, info_ptr, this->section,
23395 rcuh_kind::COMPILE);
23396 }
96408a79 23397
2e6a9f79 23398 return &m_header;
96408a79
SA
23399}
23400
09ba997f 23401/* See read.h. */
ae0d2f24 23402
98714339 23403int
09ba997f 23404dwarf2_per_cu_data::addr_size () const
ae0d2f24 23405{
2e6a9f79 23406 return this->get_header ()->addr_size;
ae0d2f24
UW
23407}
23408
09ba997f 23409/* See read.h. */
9eae7c52
TT
23410
23411int
09ba997f 23412dwarf2_per_cu_data::offset_size () const
9eae7c52 23413{
2e6a9f79 23414 return this->get_header ()->offset_size;
96408a79
SA
23415}
23416
09ba997f 23417/* See read.h. */
96408a79
SA
23418
23419int
09ba997f 23420dwarf2_per_cu_data::ref_addr_size () const
96408a79 23421{
2e6a9f79 23422 const comp_unit_head *header = this->get_header ();
96408a79 23423
2e6a9f79
SM
23424 if (header->version == 2)
23425 return header->addr_size;
96408a79 23426 else
2e6a9f79 23427 return header->offset_size;
181cebd4
JK
23428}
23429
09ba997f 23430/* See read.h. */
9aa1f1e3 23431
09ba997f 23432struct type *
293e7e51 23433dwarf2_cu::addr_type () const
9a49df9d 23434{
293e7e51 23435 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23436 struct type *void_type = objfile_type (objfile)->builtin_void;
23437 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23438 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23439
23440 if (TYPE_LENGTH (addr_type) == addr_size)
23441 return addr_type;
23442
09ba997f 23443 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23444 return addr_type;
23445}
23446
22b6cd70
TT
23447/* A helper function for dwarf2_find_containing_comp_unit that returns
23448 the index of the result, and that searches a vector. It will
23449 return a result even if the offset in question does not actually
23450 occur in any CU. This is separate so that it can be unit
23451 tested. */
ae038cb0 23452
22b6cd70
TT
23453static int
23454dwarf2_find_containing_comp_unit
23455 (sect_offset sect_off,
23456 unsigned int offset_in_dwz,
23457 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23458{
ae038cb0
DJ
23459 int low, high;
23460
ae038cb0 23461 low = 0;
22b6cd70 23462 high = all_comp_units.size () - 1;
ae038cb0
DJ
23463 while (high > low)
23464 {
36586728 23465 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23466 int mid = low + (high - low) / 2;
9a619af0 23467
22b6cd70 23468 mid_cu = all_comp_units[mid];
36586728 23469 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23470 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23471 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23472 high = mid;
23473 else
23474 low = mid + 1;
23475 }
23476 gdb_assert (low == high);
22b6cd70
TT
23477 return low;
23478}
23479
23480/* Locate the .debug_info compilation unit from CU's objfile which contains
23481 the DIE at OFFSET. Raises an error on failure. */
23482
23483static struct dwarf2_per_cu_data *
23484dwarf2_find_containing_comp_unit (sect_offset sect_off,
23485 unsigned int offset_in_dwz,
976ca316 23486 dwarf2_per_objfile *per_objfile)
22b6cd70 23487{
976ca316
SM
23488 int low = dwarf2_find_containing_comp_unit
23489 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
23490 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23491
45b8ae0c 23492 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23493 {
36586728 23494 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23495 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23496 "offset %s [in module %s]"),
23497 sect_offset_str (sect_off),
976ca316 23498 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 23499
976ca316 23500 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23501 <= sect_off);
976ca316 23502 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23503 }
23504 else
23505 {
976ca316 23506 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23507 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23508 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23509 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23510 return this_cu;
23511 }
23512}
23513
22b6cd70
TT
23514#if GDB_SELF_TEST
23515
23516namespace selftests {
23517namespace find_containing_comp_unit {
23518
23519static void
23520run_test ()
23521{
23522 struct dwarf2_per_cu_data one {};
23523 struct dwarf2_per_cu_data two {};
23524 struct dwarf2_per_cu_data three {};
23525 struct dwarf2_per_cu_data four {};
23526
23527 one.length = 5;
23528 two.sect_off = sect_offset (one.length);
23529 two.length = 7;
23530
23531 three.length = 5;
23532 three.is_dwz = 1;
23533 four.sect_off = sect_offset (three.length);
23534 four.length = 7;
23535 four.is_dwz = 1;
23536
23537 std::vector<dwarf2_per_cu_data *> units;
23538 units.push_back (&one);
23539 units.push_back (&two);
23540 units.push_back (&three);
23541 units.push_back (&four);
23542
23543 int result;
23544
23545 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23546 SELF_CHECK (units[result] == &one);
23547 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23548 SELF_CHECK (units[result] == &one);
23549 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23550 SELF_CHECK (units[result] == &two);
23551
23552 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23553 SELF_CHECK (units[result] == &three);
23554 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23555 SELF_CHECK (units[result] == &three);
23556 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23557 SELF_CHECK (units[result] == &four);
23558}
23559
23560}
23561}
23562
23563#endif /* GDB_SELF_TEST */
23564
9e021579 23565/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23566
9e021579
SM
23567dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23568 dwarf2_per_objfile *per_objfile)
23569 : per_cu (per_cu),
23570 per_objfile (per_objfile),
9068261f
AB
23571 mark (false),
23572 has_loclist (false),
23573 checked_producer (false),
23574 producer_is_gxx_lt_4_6 (false),
23575 producer_is_gcc_lt_4_3 (false),
eb77c9df 23576 producer_is_icc (false),
9068261f 23577 producer_is_icc_lt_14 (false),
c258c396 23578 producer_is_codewarrior (false),
9068261f 23579 processing_has_namespace_info (false)
93311388 23580{
9816fde3
JK
23581}
23582
23583/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23584
23585static void
95554aad
TT
23586prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23587 enum language pretend_language)
9816fde3
JK
23588{
23589 struct attribute *attr;
23590
23591 /* Set the language we're debugging. */
23592 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23593 if (attr != nullptr)
9816fde3
JK
23594 set_cu_language (DW_UNSND (attr), cu);
23595 else
9cded63f 23596 {
95554aad 23597 cu->language = pretend_language;
9cded63f
TT
23598 cu->language_defn = language_def (cu->language);
23599 }
dee91e82 23600
7d45c7c3 23601 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23602}
23603
7188ed02 23604/* See read.h. */
ae038cb0 23605
7188ed02
SM
23606dwarf2_cu *
23607dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23608{
7188ed02
SM
23609 auto it = m_dwarf2_cus.find (per_cu);
23610 if (it == m_dwarf2_cus.end ())
23611 return nullptr;
ae038cb0 23612
7188ed02
SM
23613 return it->second;
23614}
23615
23616/* See read.h. */
23617
23618void
23619dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23620{
23621 gdb_assert (this->get_cu (per_cu) == nullptr);
23622
23623 m_dwarf2_cus[per_cu] = cu;
23624}
23625
23626/* See read.h. */
23627
23628void
23629dwarf2_per_objfile::age_comp_units ()
23630{
23631 /* Start by clearing all marks. */
23632 for (auto pair : m_dwarf2_cus)
23633 pair.second->mark = false;
23634
23635 /* Traverse all CUs, mark them and their dependencies if used recently
23636 enough. */
23637 for (auto pair : m_dwarf2_cus)
ae038cb0 23638 {
7188ed02
SM
23639 dwarf2_cu *cu = pair.second;
23640
23641 cu->last_used++;
23642 if (cu->last_used <= dwarf_max_cache_age)
23643 dwarf2_mark (cu);
ae038cb0
DJ
23644 }
23645
7188ed02
SM
23646 /* Delete all CUs still not marked. */
23647 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 23648 {
7188ed02 23649 dwarf2_cu *cu = it->second;
ae038cb0 23650
7188ed02 23651 if (!cu->mark)
ae038cb0 23652 {
7188ed02
SM
23653 delete cu;
23654 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
23655 }
23656 else
7188ed02 23657 it++;
ae038cb0
DJ
23658 }
23659}
23660
7188ed02 23661/* See read.h. */
ae038cb0 23662
7188ed02
SM
23663void
23664dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23665{
7188ed02
SM
23666 auto it = m_dwarf2_cus.find (per_cu);
23667 if (it == m_dwarf2_cus.end ())
23668 return;
ae038cb0 23669
7188ed02 23670 delete it->second;
ae038cb0 23671
7188ed02
SM
23672 m_dwarf2_cus.erase (it);
23673}
ae038cb0 23674
7188ed02
SM
23675dwarf2_per_objfile::~dwarf2_per_objfile ()
23676{
23677 remove_all_cus ();
ae038cb0
DJ
23678}
23679
dee91e82
DE
23680/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23681 We store these in a hash table separate from the DIEs, and preserve them
23682 when the DIEs are flushed out of cache.
23683
23684 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23685 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23686 or the type may come from a DWO file. Furthermore, while it's more logical
23687 to use per_cu->section+offset, with Fission the section with the data is in
23688 the DWO file but we don't know that section at the point we need it.
23689 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23690 because we can enter the lookup routine, get_die_type_at_offset, from
23691 outside this file, and thus won't necessarily have PER_CU->cu.
23692 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23693
dee91e82 23694struct dwarf2_per_cu_offset_and_type
1c379e20 23695{
dee91e82 23696 const struct dwarf2_per_cu_data *per_cu;
9c541725 23697 sect_offset sect_off;
1c379e20
DJ
23698 struct type *type;
23699};
23700
dee91e82 23701/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23702
23703static hashval_t
dee91e82 23704per_cu_offset_and_type_hash (const void *item)
1c379e20 23705{
9a3c8263
SM
23706 const struct dwarf2_per_cu_offset_and_type *ofs
23707 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23708
9c541725 23709 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23710}
23711
dee91e82 23712/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23713
23714static int
dee91e82 23715per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23716{
9a3c8263
SM
23717 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23718 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23719 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23720 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23721
dee91e82 23722 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23723 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23724}
23725
23726/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23727 table if necessary. For convenience, return TYPE.
23728
23729 The DIEs reading must have careful ordering to:
85102364 23730 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23731 reading current DIE.
23732 * Not trying to dereference contents of still incompletely read in types
23733 while reading in other DIEs.
23734 * Enable referencing still incompletely read in types just by a pointer to
23735 the type without accessing its fields.
23736
23737 Therefore caller should follow these rules:
23738 * Try to fetch any prerequisite types we may need to build this DIE type
23739 before building the type and calling set_die_type.
e71ec853 23740 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23741 possible before fetching more types to complete the current type.
23742 * Make the type as complete as possible before fetching more types. */
1c379e20 23743
f792889a 23744static struct type *
1c379e20
DJ
23745set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23746{
976ca316 23747 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 23748 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 23749 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
23750 struct attribute *attr;
23751 struct dynamic_prop prop;
1c379e20 23752
b4ba55a1
JB
23753 /* For Ada types, make sure that the gnat-specific data is always
23754 initialized (if not already set). There are a few types where
23755 we should not be doing so, because the type-specific area is
23756 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23757 where the type-specific area is used to store the floatformat).
23758 But this is not a problem, because the gnat-specific information
23759 is actually not needed for these types. */
23760 if (need_gnat_info (cu)
78134374
SM
23761 && type->code () != TYPE_CODE_FUNC
23762 && type->code () != TYPE_CODE_FLT
23763 && type->code () != TYPE_CODE_METHODPTR
23764 && type->code () != TYPE_CODE_MEMBERPTR
23765 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23766 && !HAVE_GNAT_AUX_INFO (type))
23767 INIT_GNAT_SPECIFIC (type);
23768
3f2f83dd
KB
23769 /* Read DW_AT_allocated and set in type. */
23770 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23771 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23772 {
293e7e51 23773 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23774 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23775 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23776 }
23777 else if (attr != NULL)
23778 {
b98664d3 23779 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23780 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23781 sect_offset_str (die->sect_off));
3f2f83dd
KB
23782 }
23783
23784 /* Read DW_AT_associated and set in type. */
23785 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23786 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23787 {
293e7e51 23788 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23789 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23790 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23791 }
23792 else if (attr != NULL)
23793 {
b98664d3 23794 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23795 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23796 sect_offset_str (die->sect_off));
3f2f83dd
KB
23797 }
23798
3cdcd0ce
JB
23799 /* Read DW_AT_data_location and set in type. */
23800 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23801 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23802 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23803
976ca316
SM
23804 if (per_objfile->die_type_hash == NULL)
23805 per_objfile->die_type_hash
0335378b
TT
23806 = htab_up (htab_create_alloc (127,
23807 per_cu_offset_and_type_hash,
23808 per_cu_offset_and_type_eq,
23809 NULL, xcalloc, xfree));
1c379e20 23810
dee91e82 23811 ofs.per_cu = cu->per_cu;
9c541725 23812 ofs.sect_off = die->sect_off;
1c379e20 23813 ofs.type = type;
dee91e82 23814 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 23815 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23816 if (*slot)
b98664d3 23817 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23818 sect_offset_str (die->sect_off));
8d749320
SM
23819 *slot = XOBNEW (&objfile->objfile_obstack,
23820 struct dwarf2_per_cu_offset_and_type);
1c379e20 23821 **slot = ofs;
f792889a 23822 return type;
1c379e20
DJ
23823}
23824
9c541725 23825/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23826 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23827
23828static struct type *
9c541725 23829get_die_type_at_offset (sect_offset sect_off,
aa66c379 23830 dwarf2_per_cu_data *per_cu,
976ca316 23831 dwarf2_per_objfile *per_objfile)
1c379e20 23832{
dee91e82 23833 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23834
976ca316 23835 if (per_objfile->die_type_hash == NULL)
f792889a 23836 return NULL;
1c379e20 23837
dee91e82 23838 ofs.per_cu = per_cu;
9c541725 23839 ofs.sect_off = sect_off;
9a3c8263 23840 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 23841 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23842 if (slot)
23843 return slot->type;
23844 else
23845 return NULL;
23846}
23847
02142a6c 23848/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23849 or return NULL if DIE does not have a saved type. */
23850
23851static struct type *
23852get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23853{
aa66c379 23854 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
23855}
23856
10b3939b
DJ
23857/* Add a dependence relationship from CU to REF_PER_CU. */
23858
23859static void
23860dwarf2_add_dependence (struct dwarf2_cu *cu,
23861 struct dwarf2_per_cu_data *ref_per_cu)
23862{
23863 void **slot;
23864
23865 if (cu->dependencies == NULL)
23866 cu->dependencies
23867 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23868 NULL, &cu->comp_unit_obstack,
23869 hashtab_obstack_allocate,
23870 dummy_obstack_deallocate);
23871
23872 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23873 if (*slot == NULL)
23874 *slot = ref_per_cu;
23875}
1c379e20 23876
f504f079
DE
23877/* Subroutine of dwarf2_mark to pass to htab_traverse.
23878 Set the mark field in every compilation unit in the
7188ed02
SM
23879 cache that we must keep because we are keeping CU.
23880
23881 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 23882
10b3939b
DJ
23883static int
23884dwarf2_mark_helper (void **slot, void *data)
23885{
7188ed02
SM
23886 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23887 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23888 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
23889
23890 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23891 reading of the chain. As such dependencies remain valid it is not much
23892 useful to track and undo them during QUIT cleanups. */
7188ed02 23893 if (cu == nullptr)
d07ed419
JK
23894 return 1;
23895
7188ed02 23896 if (cu->mark)
10b3939b 23897 return 1;
10b3939b 23898
7188ed02
SM
23899 cu->mark = true;
23900
23901 if (cu->dependencies != nullptr)
23902 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
23903
23904 return 1;
23905}
23906
f504f079
DE
23907/* Set the mark field in CU and in every other compilation unit in the
23908 cache that we must keep because we are keeping CU. */
23909
ae038cb0
DJ
23910static void
23911dwarf2_mark (struct dwarf2_cu *cu)
23912{
23913 if (cu->mark)
23914 return;
7188ed02 23915
9068261f 23916 cu->mark = true;
ae038cb0 23917
7188ed02
SM
23918 if (cu->dependencies != nullptr)
23919 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
23920}
23921
72bf9492
DJ
23922/* Trivial hash function for partial_die_info: the hash value of a DIE
23923 is its offset in .debug_info for this objfile. */
23924
23925static hashval_t
23926partial_die_hash (const void *item)
23927{
9a3c8263
SM
23928 const struct partial_die_info *part_die
23929 = (const struct partial_die_info *) item;
9a619af0 23930
9c541725 23931 return to_underlying (part_die->sect_off);
72bf9492
DJ
23932}
23933
23934/* Trivial comparison function for partial_die_info structures: two DIEs
23935 are equal if they have the same offset. */
23936
23937static int
23938partial_die_eq (const void *item_lhs, const void *item_rhs)
23939{
9a3c8263
SM
23940 const struct partial_die_info *part_die_lhs
23941 = (const struct partial_die_info *) item_lhs;
23942 const struct partial_die_info *part_die_rhs
23943 = (const struct partial_die_info *) item_rhs;
9a619af0 23944
9c541725 23945 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23946}
23947
3c3bb058
AB
23948struct cmd_list_element *set_dwarf_cmdlist;
23949struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23950
9291a0cd 23951static void
cd4fb1b2
SM
23952show_check_physname (struct ui_file *file, int from_tty,
23953 struct cmd_list_element *c, const char *value)
9291a0cd 23954{
cd4fb1b2
SM
23955 fprintf_filtered (file,
23956 _("Whether to check \"physname\" is %s.\n"),
23957 value);
9291a0cd
TT
23958}
23959
6c265988 23960void _initialize_dwarf2_read ();
cd4fb1b2 23961void
6c265988 23962_initialize_dwarf2_read ()
9291a0cd 23963{
0743fc83 23964 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23965Set DWARF specific variables.\n\
590042fc 23966Configure DWARF variables such as the cache size."),
0743fc83
TT
23967 &set_dwarf_cmdlist, "maintenance set dwarf ",
23968 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23969
0743fc83 23970 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23971Show DWARF specific variables.\n\
23972Show DWARF variables such as the cache size."),
0743fc83
TT
23973 &show_dwarf_cmdlist, "maintenance show dwarf ",
23974 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23975
cd4fb1b2
SM
23976 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23977 &dwarf_max_cache_age, _("\
23978Set the upper bound on the age of cached DWARF compilation units."), _("\
23979Show the upper bound on the age of cached DWARF compilation units."), _("\
23980A higher limit means that cached compilation units will be stored\n\
23981in memory longer, and more total memory will be used. Zero disables\n\
23982caching, which can slow down startup."),
23983 NULL,
23984 show_dwarf_max_cache_age,
23985 &set_dwarf_cmdlist,
23986 &show_dwarf_cmdlist);
156942c7 23987
cd4fb1b2
SM
23988 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23989Set debugging of the DWARF reader."), _("\
23990Show debugging of the DWARF reader."), _("\
23991When enabled (non-zero), debugging messages are printed during DWARF\n\
23992reading and symtab expansion. A value of 1 (one) provides basic\n\
23993information. A value greater than 1 provides more verbose information."),
23994 NULL,
23995 NULL,
23996 &setdebuglist, &showdebuglist);
9291a0cd 23997
cd4fb1b2
SM
23998 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23999Set debugging of the DWARF DIE reader."), _("\
24000Show debugging of the DWARF DIE reader."), _("\
24001When enabled (non-zero), DIEs are dumped after they are read in.\n\
24002The value is the maximum depth to print."),
24003 NULL,
24004 NULL,
24005 &setdebuglist, &showdebuglist);
9291a0cd 24006
cd4fb1b2
SM
24007 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24008Set debugging of the dwarf line reader."), _("\
24009Show debugging of the dwarf line reader."), _("\
24010When enabled (non-zero), line number entries are dumped as they are read in.\n\
24011A value of 1 (one) provides basic information.\n\
24012A value greater than 1 provides more verbose information."),
24013 NULL,
24014 NULL,
24015 &setdebuglist, &showdebuglist);
437afbb8 24016
cd4fb1b2
SM
24017 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24018Set cross-checking of \"physname\" code against demangler."), _("\
24019Show cross-checking of \"physname\" code against demangler."), _("\
24020When enabled, GDB's internal \"physname\" code is checked against\n\
24021the demangler."),
24022 NULL, show_check_physname,
24023 &setdebuglist, &showdebuglist);
900e11f9 24024
e615022a
DE
24025 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24026 no_class, &use_deprecated_index_sections, _("\
24027Set whether to use deprecated gdb_index sections."), _("\
24028Show whether to use deprecated gdb_index sections."), _("\
24029When enabled, deprecated .gdb_index sections are used anyway.\n\
24030Normally they are ignored either because of a missing feature or\n\
24031performance issue.\n\
24032Warning: This option must be enabled before gdb reads the file."),
24033 NULL,
24034 NULL,
24035 &setlist, &showlist);
24036
f1e6e072
TT
24037 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24038 &dwarf2_locexpr_funcs);
24039 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24040 &dwarf2_loclist_funcs);
24041
24042 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24043 &dwarf2_block_frame_base_locexpr_funcs);
24044 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24045 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24046
24047#if GDB_SELF_TEST
24048 selftests::register_test ("dw2_expand_symtabs_matching",
24049 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24050 selftests::register_test ("dwarf2_find_containing_comp_unit",
24051 selftests::find_containing_comp_unit::run_test);
c62446b1 24052#endif
6502dd73 24053}
This page took 5.352673 seconds and 4 git commands to generate.