Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
88b9d363 3 Copyright (C) 1994-2022 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"
cd53fa40 35#include "dwarf2/comp-unit-head.h"
8ae78a44 36#include "dwarf2/cu.h"
82ca8957
TT
37#include "dwarf2/index-cache.h"
38#include "dwarf2/index-common.h"
f4382c45 39#include "dwarf2/leb.h"
8fdd972c 40#include "dwarf2/line-header.h"
9fda78b6 41#include "dwarf2/dwz.h"
c90ec28a 42#include "dwarf2/macro.h"
c2d50fd0 43#include "dwarf2/die.h"
c2a62a3d 44#include "dwarf2/sect-names.h"
2b2558bf 45#include "dwarf2/stringify.h"
70182375 46#include "dwarf2/public.h"
4de283e4
TT
47#include "bfd.h"
48#include "elf-bfd.h"
49#include "symtab.h"
50#include "gdbtypes.h"
51#include "objfiles.h"
d55e5aa6 52#include "dwarf2.h"
4de283e4
TT
53#include "demangle.h"
54#include "gdb-demangle.h"
4de283e4 55#include "filenames.h" /* for DOSish file names */
4de283e4
TT
56#include "language.h"
57#include "complaints.h"
82ca8957
TT
58#include "dwarf2/expr.h"
59#include "dwarf2/loc.h"
4de283e4
TT
60#include "cp-support.h"
61#include "hashtab.h"
62#include "command.h"
d55e5aa6 63#include "gdbcmd.h"
4de283e4
TT
64#include "block.h"
65#include "addrmap.h"
66#include "typeprint.h"
67#include "psympriv.h"
4de283e4 68#include "c-lang.h"
d55e5aa6 69#include "go-lang.h"
4de283e4
TT
70#include "valprint.h"
71#include "gdbcore.h" /* for gnutarget */
72#include "gdb/gdb-index.h"
4de283e4
TT
73#include "gdb_bfd.h"
74#include "f-lang.h"
75#include "source.h"
4de283e4 76#include "build-id.h"
d55e5aa6 77#include "namespace.h"
268a13a5
TT
78#include "gdbsupport/function-view.h"
79#include "gdbsupport/gdb_optional.h"
80#include "gdbsupport/underlying.h"
268a13a5 81#include "gdbsupport/hash_enum.h"
4de283e4 82#include "filename-seen-cache.h"
b32b108a 83#include "producer.h"
4de283e4 84#include <fcntl.h>
4de283e4 85#include <algorithm>
4de283e4 86#include <unordered_map>
268a13a5 87#include "gdbsupport/selftest.h"
c9317f21 88#include "rust-lang.h"
268a13a5 89#include "gdbsupport/pathstuff.h"
edd45eb0 90#include "count-one-bits.h"
9fc29955 91#include <unordered_set>
437afbb8 92
73be47f5
DE
93/* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
b4f54984
DE
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96static unsigned int dwarf_read_debug = 0;
45cfd468 97
6f738b01
SM
98/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
99
100#define dwarf_read_debug_printf(fmt, ...) \
74b773fc
SM
101 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
102 ##__VA_ARGS__)
6f738b01
SM
103
104/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
105
106#define dwarf_read_debug_printf_v(fmt, ...) \
74b773fc
SM
107 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
108 ##__VA_ARGS__)
6f738b01 109
3a706c17
SM
110/* When non-zero, dump DIEs after they are read in. */
111static unsigned int dwarf_die_debug = 0;
d97bc12b 112
27e0867f 113/* When non-zero, dump line number entries as they are read in. */
8fdd972c 114unsigned int dwarf_line_debug = 0;
27e0867f 115
491144b5
CB
116/* When true, cross-check physname against demangler. */
117static bool check_physname = false;
900e11f9 118
491144b5
CB
119/* When true, do not reject deprecated .gdb_index sections. */
120static bool use_deprecated_index_sections = false;
481860b3 121
17ee85fc
TT
122/* This is used to store the data that is always per objfile. */
123static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
124
125/* These are used to store the dwarf2_per_bfd objects.
126
127 objfiles having the same BFD, which doesn't require relocations, are going to
128 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
129
130 Other objfiles are not going to share a dwarf2_per_bfd with any other
131 objfiles, so they'll have their own version kept in the _objfile_data_key
132 version. */
133static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
134static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 135
f1e6e072
TT
136/* The "aclass" indices for various kinds of computed DWARF symbols. */
137
138static int dwarf2_locexpr_index;
139static int dwarf2_loclist_index;
140static int dwarf2_locexpr_block_index;
141static int dwarf2_loclist_block_index;
142
41144253 143/* Size of .debug_loclists section header for 32-bit DWARF format. */
144#define LOCLIST_HEADER_SIZE32 12
145
146/* Size of .debug_loclists section header for 64-bit DWARF format. */
147#define LOCLIST_HEADER_SIZE64 20
148
d0ce17d8
CT
149/* Size of .debug_rnglists section header for 32-bit DWARF format. */
150#define RNGLIST_HEADER_SIZE32 12
151
152/* Size of .debug_rnglists section header for 64-bit DWARF format. */
153#define RNGLIST_HEADER_SIZE64 20
154
3f563c84
PA
155/* An index into a (C++) symbol name component in a symbol name as
156 recorded in the mapped_index's symbol table. For each C++ symbol
157 in the symbol table, we record one entry for the start of each
158 component in the symbol in a table of name components, and then
159 sort the table, in order to be able to binary search symbol names,
160 ignoring leading namespaces, both completion and regular look up.
161 For example, for symbol "A::B::C", we'll have an entry that points
162 to "A::B::C", another that points to "B::C", and another for "C".
163 Note that function symbols in GDB index have no parameter
164 information, just the function/method names. You can convert a
165 name_component to a "const char *" using the
166 'mapped_index::symbol_name_at(offset_type)' method. */
167
168struct name_component
169{
170 /* Offset in the symbol name where the component starts. Stored as
171 a (32-bit) offset instead of a pointer to save memory and improve
172 locality on 64-bit architectures. */
173 offset_type name_offset;
174
175 /* The symbol's index in the symbol and constant pool tables of a
176 mapped_index. */
177 offset_type idx;
178};
179
44ed8f3e
PA
180/* Base class containing bits shared by both .gdb_index and
181 .debug_name indexes. */
182
183struct mapped_index_base
184{
22ca247e
TT
185 mapped_index_base () = default;
186 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
187
44ed8f3e
PA
188 /* The name_component table (a sorted vector). See name_component's
189 description above. */
190 std::vector<name_component> name_components;
191
192 /* How NAME_COMPONENTS is sorted. */
193 enum case_sensitivity name_components_casing;
194
195 /* Return the number of names in the symbol table. */
196 virtual size_t symbol_name_count () const = 0;
197
198 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
199 virtual const char *symbol_name_at
200 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
201
202 /* Return whether the name at IDX in the symbol table should be
203 ignored. */
204 virtual bool symbol_name_slot_invalid (offset_type idx) const
205 {
206 return false;
207 }
208
209 /* Build the symbol name component sorted vector, if we haven't
210 yet. */
fcf23d5b 211 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
212
213 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
214 possible matches for LN_NO_PARAMS in the name component
215 vector. */
216 std::pair<std::vector<name_component>::const_iterator,
217 std::vector<name_component>::const_iterator>
3b00ef10 218 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
219 enum language lang,
220 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
221
222 /* Prevent deleting/destroying via a base class pointer. */
223protected:
224 ~mapped_index_base() = default;
225};
226
42c2c694
TT
227/* This is a view into the index that converts from bytes to an
228 offset_type, and allows indexing. Unaligned bytes are specifically
229 allowed here, and handled via unpacking. */
230
231class offset_view
232{
233public:
234 offset_view () = default;
235
236 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
237 : m_bytes (bytes)
238 {
239 }
240
241 /* Extract the INDEXth offset_type from the array. */
242 offset_type operator[] (size_t index) const
243 {
244 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
245 return (offset_type) extract_unsigned_integer (bytes,
246 sizeof (offset_type),
247 BFD_ENDIAN_LITTLE);
248 }
249
250 /* Return the number of offset_types in this array. */
251 size_t size () const
252 {
253 return m_bytes.size () / sizeof (offset_type);
254 }
255
256 /* Return true if this view is empty. */
257 bool empty () const
258 {
259 return m_bytes.empty ();
260 }
261
262private:
263 /* The underlying bytes. */
264 gdb::array_view<const gdb_byte> m_bytes;
265};
266
9291a0cd
TT
267/* A description of the mapped index. The file format is described in
268 a comment by the code that writes the index. */
fc898b42 269struct mapped_index final : public mapped_index_base
9291a0cd 270{
559a7a62 271 /* Index data format version. */
3063847f 272 int version = 0;
559a7a62 273
f00a2de2
PA
274 /* The address table data. */
275 gdb::array_view<const gdb_byte> address_table;
b11b1f88 276
3876f04e 277 /* The symbol table, implemented as a hash table. */
42c2c694 278 offset_view symbol_table;
b11b1f88 279
9291a0cd 280 /* A pointer to the constant pool. */
42c2c694
TT
281 gdb::array_view<const gdb_byte> constant_pool;
282
283 /* Return the index into the constant pool of the name of the IDXth
284 symbol in the symbol table. */
285 offset_type symbol_name_index (offset_type idx) const
286 {
287 return symbol_table[2 * idx];
288 }
289
290 /* Return the index into the constant pool of the CU vector of the
291 IDXth symbol in the symbol table. */
292 offset_type symbol_vec_index (offset_type idx) const
293 {
294 return symbol_table[2 * idx + 1];
295 }
3f563c84 296
44ed8f3e
PA
297 bool symbol_name_slot_invalid (offset_type idx) const override
298 {
42c2c694
TT
299 return (symbol_name_index (idx) == 0
300 && symbol_vec_index (idx) == 0);
44ed8f3e 301 }
5c58de74 302
3f563c84
PA
303 /* Convenience method to get at the name of the symbol at IDX in the
304 symbol table. */
fcf23d5b
SM
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
42c2c694
TT
307 {
308 return (const char *) (this->constant_pool.data ()
309 + symbol_name_index (idx));
310 }
5c58de74 311
44ed8f3e 312 size_t symbol_name_count () const override
42c2c694 313 { return this->symbol_table.size () / 2; }
9291a0cd
TT
314};
315
927aa2e7
JK
316/* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
fc898b42 318struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
319{
320 bfd_endian dwarf5_byte_order;
321 bool dwarf5_is_dwarf64;
322 bool augmentation_is_gdb;
323 uint8_t offset_size;
324 uint32_t cu_count = 0;
325 uint32_t tu_count, bucket_count, name_count;
326 const gdb_byte *cu_table_reordered, *tu_table_reordered;
327 const uint32_t *bucket_table_reordered, *hash_table_reordered;
328 const gdb_byte *name_table_string_offs_reordered;
329 const gdb_byte *name_table_entry_offs_reordered;
330 const gdb_byte *entry_pool;
331
332 struct index_val
333 {
334 ULONGEST dwarf_tag;
335 struct attr
336 {
337 /* Attribute name DW_IDX_*. */
338 ULONGEST dw_idx;
339
340 /* Attribute form DW_FORM_*. */
341 ULONGEST form;
342
343 /* Value if FORM is DW_FORM_implicit_const. */
344 LONGEST implicit_const;
345 };
346 std::vector<attr> attr_vec;
347 };
348
349 std::unordered_map<ULONGEST, index_val> abbrev_map;
350
fcf23d5b
SM
351 const char *namei_to_name
352 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
353
354 /* Implementation of the mapped_index_base virtual interface, for
355 the name_components cache. */
356
fcf23d5b
SM
357 const char *symbol_name_at
358 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
359 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
360
361 size_t symbol_name_count () const override
362 { return this->name_count; }
927aa2e7
JK
363};
364
cd4fb1b2 365/* See dwarf2read.h. */
ed2dc618 366
cd4fb1b2 367dwarf2_per_objfile *
ed2dc618
SM
368get_dwarf2_per_objfile (struct objfile *objfile)
369{
5bfd760d 370 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 371}
c906108c 372
251d32d9 373/* Default names of the debugging sections. */
c906108c 374
233a11ab
CS
375/* Note that if the debugging section has been compressed, it might
376 have a name like .zdebug_info. */
377
9938d15a 378const struct dwarf2_debug_sections dwarf2_elf_names =
9cdd5dbd 379{
251d32d9
TG
380 { ".debug_info", ".zdebug_info" },
381 { ".debug_abbrev", ".zdebug_abbrev" },
382 { ".debug_line", ".zdebug_line" },
383 { ".debug_loc", ".zdebug_loc" },
43988095 384 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 385 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 386 { ".debug_macro", ".zdebug_macro" },
251d32d9 387 { ".debug_str", ".zdebug_str" },
18a8505e 388 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 389 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 390 { ".debug_ranges", ".zdebug_ranges" },
43988095 391 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 392 { ".debug_types", ".zdebug_types" },
3019eac3 393 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
394 { ".debug_frame", ".zdebug_frame" },
395 { ".eh_frame", NULL },
24d3216f 396 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
397 { ".debug_names", ".zdebug_names" },
398 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 399 23
251d32d9 400};
c906108c 401
80626a55 402/* List of DWO/DWP sections. */
3019eac3 403
80626a55 404static const struct dwop_section_names
3019eac3
DE
405{
406 struct dwarf2_section_names abbrev_dwo;
407 struct dwarf2_section_names info_dwo;
408 struct dwarf2_section_names line_dwo;
409 struct dwarf2_section_names loc_dwo;
43988095 410 struct dwarf2_section_names loclists_dwo;
09262596
DE
411 struct dwarf2_section_names macinfo_dwo;
412 struct dwarf2_section_names macro_dwo;
d0ce17d8 413 struct dwarf2_section_names rnglists_dwo;
3019eac3
DE
414 struct dwarf2_section_names str_dwo;
415 struct dwarf2_section_names str_offsets_dwo;
416 struct dwarf2_section_names types_dwo;
80626a55
DE
417 struct dwarf2_section_names cu_index;
418 struct dwarf2_section_names tu_index;
3019eac3 419}
80626a55 420dwop_section_names =
3019eac3
DE
421{
422 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
423 { ".debug_info.dwo", ".zdebug_info.dwo" },
424 { ".debug_line.dwo", ".zdebug_line.dwo" },
425 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 426 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
427 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
428 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
d0ce17d8 429 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
3019eac3
DE
430 { ".debug_str.dwo", ".zdebug_str.dwo" },
431 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
432 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
433 { ".debug_cu_index", ".zdebug_cu_index" },
434 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
435};
436
c906108c
SS
437/* local data types */
438
d0ce17d8
CT
439/* The location list and range list sections (.debug_loclists & .debug_rnglists)
440 begin with a header, which contains the following information. */
441struct loclists_rnglists_header
41144253 442{
443 /* A 4-byte or 12-byte length containing the length of the
444 set of entries for this compilation unit, not including the
445 length field itself. */
446 unsigned int length;
447
448 /* A 2-byte version identifier. */
449 short version;
450
451 /* A 1-byte unsigned integer containing the size in bytes of an address on
452 the target system. */
453 unsigned char addr_size;
454
455 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
456 on the target system. */
457 unsigned char segment_collector_size;
458
459 /* A 4-byte count of the number of offsets that follow the header. */
460 unsigned int offset_entry_count;
461};
462
094b34ac
DE
463/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
464 This includes type_unit_group and quick_file_names. */
465
466struct stmt_list_hash
467{
468 /* The DWO unit this table is from or NULL if there is none. */
469 struct dwo_unit *dwo_unit;
470
471 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 472 sect_offset line_sect_off;
094b34ac
DE
473};
474
5989a64e 475/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
476 an object of this type. This contains elements of type unit groups
477 that can be shared across objfiles. The non-shareable parts are in
478 type_unit_group_unshareable. */
f4dc4d17 479
0d305d5c 480struct type_unit_group : public dwarf2_per_cu_data
f4dc4d17 481{
0186c6a7
DE
482 /* The TUs that share this DW_AT_stmt_list entry.
483 This is added to while parsing type units to build partial symtabs,
484 and is deleted afterwards and not used again. */
0d305d5c 485 std::vector<signatured_type *> *tus = nullptr;
f4dc4d17 486
094b34ac 487 /* The data used to construct the hash key. */
0d305d5c 488 struct stmt_list_hash hash {};
f4dc4d17
DE
489};
490
73869dc2 491/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
492
493struct dwo_sections
494{
495 struct dwarf2_section_info abbrev;
3019eac3
DE
496 struct dwarf2_section_info line;
497 struct dwarf2_section_info loc;
43988095 498 struct dwarf2_section_info loclists;
09262596
DE
499 struct dwarf2_section_info macinfo;
500 struct dwarf2_section_info macro;
d0ce17d8 501 struct dwarf2_section_info rnglists;
3019eac3
DE
502 struct dwarf2_section_info str;
503 struct dwarf2_section_info str_offsets;
80626a55
DE
504 /* In the case of a virtual DWO file, these two are unused. */
505 struct dwarf2_section_info info;
fd5866f6 506 std::vector<dwarf2_section_info> types;
3019eac3
DE
507};
508
c88ee1f0 509/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
510
511struct dwo_unit
512{
513 /* Backlink to the containing struct dwo_file. */
514 struct dwo_file *dwo_file;
515
516 /* The "id" that distinguishes this CU/TU.
517 .debug_info calls this "dwo_id", .debug_types calls this "signature".
518 Since signatures came first, we stick with it for consistency. */
519 ULONGEST signature;
520
521 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 522 struct dwarf2_section_info *section;
3019eac3 523
9c541725
PA
524 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
525 sect_offset sect_off;
3019eac3
DE
526 unsigned int length;
527
528 /* For types, offset in the type's DIE of the type defined by this TU. */
529 cu_offset type_offset_in_tu;
530};
531
73869dc2
DE
532/* include/dwarf2.h defines the DWP section codes.
533 It defines a max value but it doesn't define a min value, which we
534 use for error checking, so provide one. */
535
536enum dwp_v2_section_ids
537{
538 DW_SECT_MIN = 1
539};
540
80626a55 541/* Data for one DWO file.
57d63ce2
DE
542
543 This includes virtual DWO files (a virtual DWO file is a DWO file as it
544 appears in a DWP file). DWP files don't really have DWO files per se -
545 comdat folding of types "loses" the DWO file they came from, and from
546 a high level view DWP files appear to contain a mass of random types.
547 However, to maintain consistency with the non-DWP case we pretend DWP
548 files contain virtual DWO files, and we assign each TU with one virtual
549 DWO file (generally based on the line and abbrev section offsets -
550 a heuristic that seems to work in practice). */
3019eac3
DE
551
552struct dwo_file
553{
51ac9db5
SM
554 dwo_file () = default;
555 DISABLE_COPY_AND_ASSIGN (dwo_file);
556
18a8505e 557 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
558 For virtual DWO files the name is constructed from the section offsets
559 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
560 from related CU+TUs. */
51ac9db5 561 const char *dwo_name = nullptr;
0ac5b59e
DE
562
563 /* The DW_AT_comp_dir attribute. */
51ac9db5 564 const char *comp_dir = nullptr;
3019eac3 565
80626a55
DE
566 /* The bfd, when the file is open. Otherwise this is NULL.
567 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 568 gdb_bfd_ref_ptr dbfd;
3019eac3 569
73869dc2 570 /* The sections that make up this DWO file.
d2854d8d 571 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 572 sections (for lack of a better name). */
51ac9db5 573 struct dwo_sections sections {};
3019eac3 574
33c5cd75
DB
575 /* The CUs in the file.
576 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
577 an extension to handle LLVM's Link Time Optimization output (where
578 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 579 htab_up cus;
3019eac3
DE
580
581 /* Table of TUs in the file.
582 Each element is a struct dwo_unit. */
b0b6a987 583 htab_up tus;
3019eac3
DE
584};
585
80626a55
DE
586/* These sections are what may appear in a DWP file. */
587
588struct dwp_sections
589{
d2854d8d 590 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
591 struct dwarf2_section_info str;
592 struct dwarf2_section_info cu_index;
593 struct dwarf2_section_info tu_index;
73869dc2 594
d2854d8d 595 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
596 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
597 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
598 In DWP version 2 or 5 there is at most one copy of all these sections,
599 each section being (effectively) comprised of the concatenation of all of
600 the individual sections that exist in the version 1 format.
73869dc2
DE
601 To keep the code simple we treat each of these concatenated pieces as a
602 section itself (a virtual section?). */
603 struct dwarf2_section_info abbrev;
604 struct dwarf2_section_info info;
605 struct dwarf2_section_info line;
606 struct dwarf2_section_info loc;
d2854d8d 607 struct dwarf2_section_info loclists;
73869dc2
DE
608 struct dwarf2_section_info macinfo;
609 struct dwarf2_section_info macro;
d2854d8d 610 struct dwarf2_section_info rnglists;
73869dc2
DE
611 struct dwarf2_section_info str_offsets;
612 struct dwarf2_section_info types;
80626a55
DE
613};
614
73869dc2
DE
615/* These sections are what may appear in a virtual DWO file in DWP version 1.
616 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 617
73869dc2 618struct virtual_v1_dwo_sections
80626a55
DE
619{
620 struct dwarf2_section_info abbrev;
621 struct dwarf2_section_info line;
622 struct dwarf2_section_info loc;
623 struct dwarf2_section_info macinfo;
624 struct dwarf2_section_info macro;
625 struct dwarf2_section_info str_offsets;
626 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 627 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
628 struct dwarf2_section_info info_or_types;
629};
630
d2854d8d 631/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
632 In version 2, the sections of the DWO files are concatenated together
633 and stored in one section of that name. Thus each ELF section contains
634 several "virtual" sections. */
635
d2854d8d 636struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
637{
638 bfd_size_type abbrev_offset;
639 bfd_size_type abbrev_size;
640
641 bfd_size_type line_offset;
642 bfd_size_type line_size;
643
644 bfd_size_type loc_offset;
645 bfd_size_type loc_size;
646
d2854d8d
CT
647 bfd_size_type loclists_offset;
648 bfd_size_type loclists_size;
649
73869dc2
DE
650 bfd_size_type macinfo_offset;
651 bfd_size_type macinfo_size;
652
653 bfd_size_type macro_offset;
654 bfd_size_type macro_size;
655
d2854d8d
CT
656 bfd_size_type rnglists_offset;
657 bfd_size_type rnglists_size;
658
73869dc2
DE
659 bfd_size_type str_offsets_offset;
660 bfd_size_type str_offsets_size;
661
662 /* Each DWP hash table entry records one CU or one TU.
663 That is recorded here, and copied to dwo_unit.section. */
664 bfd_size_type info_or_types_offset;
665 bfd_size_type info_or_types_size;
666};
667
80626a55
DE
668/* Contents of DWP hash tables. */
669
670struct dwp_hash_table
671{
73869dc2 672 uint32_t version, nr_columns;
80626a55 673 uint32_t nr_units, nr_slots;
73869dc2
DE
674 const gdb_byte *hash_table, *unit_table;
675 union
676 {
677 struct
678 {
679 const gdb_byte *indices;
680 } v1;
681 struct
682 {
683 /* This is indexed by column number and gives the id of the section
684 in that column. */
685#define MAX_NR_V2_DWO_SECTIONS \
686 (1 /* .debug_info or .debug_types */ \
687 + 1 /* .debug_abbrev */ \
688 + 1 /* .debug_line */ \
689 + 1 /* .debug_loc */ \
690 + 1 /* .debug_str_offsets */ \
691 + 1 /* .debug_macro or .debug_macinfo */)
692 int section_ids[MAX_NR_V2_DWO_SECTIONS];
693 const gdb_byte *offsets;
694 const gdb_byte *sizes;
695 } v2;
d2854d8d
CT
696 struct
697 {
698 /* This is indexed by column number and gives the id of the section
699 in that column. */
700#define MAX_NR_V5_DWO_SECTIONS \
701 (1 /* .debug_info */ \
702 + 1 /* .debug_abbrev */ \
703 + 1 /* .debug_line */ \
704 + 1 /* .debug_loclists */ \
705 + 1 /* .debug_str_offsets */ \
706 + 1 /* .debug_macro */ \
707 + 1 /* .debug_rnglists */)
708 int section_ids[MAX_NR_V5_DWO_SECTIONS];
709 const gdb_byte *offsets;
710 const gdb_byte *sizes;
711 } v5;
73869dc2 712 } section_pool;
80626a55
DE
713};
714
715/* Data for one DWP file. */
716
717struct dwp_file
718{
400174b1
TT
719 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
720 : name (name_),
721 dbfd (std::move (abfd))
722 {
723 }
724
80626a55
DE
725 /* Name of the file. */
726 const char *name;
727
73869dc2 728 /* File format version. */
400174b1 729 int version = 0;
73869dc2 730
93417882 731 /* The bfd. */
400174b1 732 gdb_bfd_ref_ptr dbfd;
80626a55
DE
733
734 /* Section info for this file. */
400174b1 735 struct dwp_sections sections {};
80626a55 736
57d63ce2 737 /* Table of CUs in the file. */
400174b1 738 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
739
740 /* Table of TUs in the file. */
400174b1 741 const struct dwp_hash_table *tus = nullptr;
80626a55 742
19ac8c2e 743 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
744 htab_up loaded_cus;
745 htab_up loaded_tus;
80626a55 746
73869dc2
DE
747 /* Table to map ELF section numbers to their sections.
748 This is only needed for the DWP V1 file format. */
400174b1
TT
749 unsigned int num_sections = 0;
750 asection **elf_sections = nullptr;
80626a55
DE
751};
752
0963b4bd
MS
753/* Struct used to pass misc. parameters to read_die_and_children, et
754 al. which are used for both .debug_info and .debug_types dies.
755 All parameters here are unchanging for the life of the call. This
dee91e82 756 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
757
758struct die_reader_specs
759{
a32a8923 760 /* The bfd of die_section. */
0280fdcc 761 bfd *abfd;
93311388
DE
762
763 /* The CU of the DIE we are parsing. */
764 struct dwarf2_cu *cu;
765
80626a55 766 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
767 struct dwo_file *dwo_file;
768
dee91e82 769 /* The section the die comes from.
3019eac3 770 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
771 struct dwarf2_section_info *die_section;
772
773 /* die_section->buffer. */
d521ce57 774 const gdb_byte *buffer;
f664829e
DE
775
776 /* The end of the buffer. */
777 const gdb_byte *buffer_end;
a2ce51a0 778
685af9cd
TT
779 /* The abbreviation table to use when reading the DIEs. */
780 struct abbrev_table *abbrev_table;
93311388
DE
781};
782
c0ab21c2
TT
783/* A subclass of die_reader_specs that holds storage and has complex
784 constructor and destructor behavior. */
785
786class cutu_reader : public die_reader_specs
787{
788public:
789
ab432490
SM
790 cutu_reader (dwarf2_per_cu_data *this_cu,
791 dwarf2_per_objfile *per_objfile,
c0ab21c2 792 struct abbrev_table *abbrev_table,
2e671100 793 dwarf2_cu *existing_cu,
c0ab21c2
TT
794 bool skip_partial);
795
796 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 797 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
798 struct dwarf2_cu *parent_cu = nullptr,
799 struct dwo_file *dwo_file = nullptr);
800
c0ab21c2
TT
801 DISABLE_COPY_AND_ASSIGN (cutu_reader);
802
803 const gdb_byte *info_ptr = nullptr;
804 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
805 bool dummy_p = false;
806
6751ebae
TT
807 /* Release the new CU, putting it on the chain. This cannot be done
808 for dummy CUs. */
809 void keep ();
810
c0ab21c2 811private:
9e021579
SM
812 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
813 dwarf2_per_objfile *per_objfile,
2e671100 814 dwarf2_cu *existing_cu);
c0ab21c2
TT
815
816 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
817 std::unique_ptr<dwarf2_cu> m_new_cu;
818
819 /* The ordinary abbreviation table. */
820 abbrev_table_up m_abbrev_table_holder;
821
822 /* The DWO abbreviation table. */
823 abbrev_table_up m_dwo_abbrev_table;
824};
dee91e82 825
c906108c 826/* When we construct a partial symbol table entry we only
0963b4bd 827 need this much information. */
6f06d47b 828struct partial_die_info : public allocate_on_obstack
c906108c 829 {
7c32eebb 830 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
6f06d47b
YQ
831
832 /* Disable assign but still keep copy ctor, which is needed
833 load_partial_dies. */
834 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1b453aed 835 partial_die_info (const partial_die_info &) = default;
6f06d47b 836
52356b79
YQ
837 /* Adjust the partial die before generating a symbol for it. This
838 function may set the is_external flag or change the DIE's
839 name. */
840 void fixup (struct dwarf2_cu *cu);
841
48fbe735
YQ
842 /* Read a minimal amount of information into the minimal die
843 structure. */
844 const gdb_byte *read (const struct die_reader_specs *reader,
845 const struct abbrev_info &abbrev,
846 const gdb_byte *info_ptr);
847
7d00ffec
TT
848 /* Compute the name of this partial DIE. This memoizes the
849 result, so it is safe to call multiple times. */
850 const char *name (dwarf2_cu *cu);
851
72bf9492 852 /* Offset of this DIE. */
6f06d47b 853 const sect_offset sect_off;
72bf9492
DJ
854
855 /* DWARF-2 tag for this DIE. */
6f06d47b 856 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 857
72bf9492 858 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
859 const unsigned int has_children : 1;
860
72bf9492
DJ
861 unsigned int is_external : 1;
862 unsigned int is_declaration : 1;
863 unsigned int has_type : 1;
864 unsigned int has_specification : 1;
865 unsigned int has_pc_info : 1;
481860b3 866 unsigned int may_be_inlined : 1;
72bf9492 867
0c1b455e
TT
868 /* This DIE has been marked DW_AT_main_subprogram. */
869 unsigned int main_subprogram : 1;
870
72bf9492
DJ
871 /* Flag set if the SCOPE field of this structure has been
872 computed. */
873 unsigned int scope_set : 1;
874
fa4028e9
JB
875 /* Flag set if the DIE has a byte_size attribute. */
876 unsigned int has_byte_size : 1;
877
ff908ebf
AW
878 /* Flag set if the DIE has a DW_AT_const_value attribute. */
879 unsigned int has_const_value : 1;
880
98bfdba5
PA
881 /* Flag set if any of the DIE's children are template arguments. */
882 unsigned int has_template_arguments : 1;
883
52356b79 884 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
885 unsigned int fixup_called : 1;
886
36586728
TT
887 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
888 unsigned int is_dwz : 1;
889
890 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
891 unsigned int spec_is_dwz : 1;
892
7d00ffec
TT
893 unsigned int canonical_name : 1;
894
72bf9492 895 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 896 sometimes a default name for unnamed DIEs. */
7d00ffec 897 const char *raw_name = nullptr;
72bf9492 898
abc72ce4 899 /* The linkage name, if present. */
6f06d47b 900 const char *linkage_name = nullptr;
abc72ce4 901
72bf9492
DJ
902 /* The scope to prepend to our children. This is generally
903 allocated on the comp_unit_obstack, so will disappear
904 when this compilation unit leaves the cache. */
6f06d47b 905 const char *scope = nullptr;
72bf9492 906
95554aad
TT
907 /* Some data associated with the partial DIE. The tag determines
908 which field is live. */
909 union
910 {
911 /* The location description associated with this DIE, if any. */
912 struct dwarf_block *locdesc;
913 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 914 sect_offset sect_off;
6f06d47b 915 } d {};
72bf9492
DJ
916
917 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
918 CORE_ADDR lowpc = 0;
919 CORE_ADDR highpc = 0;
72bf9492 920
93311388 921 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 922 DW_AT_sibling, if any. */
48fbe735
YQ
923 /* NOTE: This member isn't strictly necessary, partial_die_info::read
924 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 925 const gdb_byte *sibling = nullptr;
72bf9492
DJ
926
927 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
928 DW_AT_specification (or DW_AT_abstract_origin or
929 DW_AT_extension). */
6f06d47b 930 sect_offset spec_offset {};
72bf9492
DJ
931
932 /* Pointers to this DIE's parent, first child, and next sibling,
933 if any. */
6f06d47b
YQ
934 struct partial_die_info *die_parent = nullptr;
935 struct partial_die_info *die_child = nullptr;
936 struct partial_die_info *die_sibling = nullptr;
937
938 friend struct partial_die_info *
939 dwarf2_cu::find_partial_die (sect_offset sect_off);
940
941 private:
942 /* Only need to do look up in dwarf2_cu::find_partial_die. */
943 partial_die_info (sect_offset sect_off)
944 : partial_die_info (sect_off, DW_TAG_padding, 0)
945 {
946 }
947
948 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
949 int has_children_)
950 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
951 {
952 is_external = 0;
953 is_declaration = 0;
954 has_type = 0;
955 has_specification = 0;
956 has_pc_info = 0;
957 may_be_inlined = 0;
958 main_subprogram = 0;
959 scope_set = 0;
960 has_byte_size = 0;
961 has_const_value = 0;
962 has_template_arguments = 0;
963 fixup_called = 0;
964 is_dwz = 0;
965 spec_is_dwz = 0;
7d00ffec 966 canonical_name = 0;
6f06d47b 967 }
c906108c
SS
968 };
969
c906108c
SS
970/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
971 but this would require a corresponding change in unpack_field_as_long
972 and friends. */
973static int bits_per_byte = 8;
974
9c6a1327
TT
975struct variant_part_builder;
976
977/* When reading a variant, we track a bit more information about the
978 field, and store it in an object of this type. */
2ddeaf8a
TT
979
980struct variant_field
981{
9c6a1327
TT
982 int first_field = -1;
983 int last_field = -1;
984
985 /* A variant can contain other variant parts. */
986 std::vector<variant_part_builder> variant_parts;
987
2ddeaf8a
TT
988 /* If we see a DW_TAG_variant, then this will be set if this is the
989 default branch. */
9c6a1327
TT
990 bool default_branch = false;
991 /* If we see a DW_AT_discr_value, then this will be the discriminant
992 value. */
993 ULONGEST discriminant_value = 0;
994 /* If we see a DW_AT_discr_list, then this is a pointer to the list
995 data. */
996 struct dwarf_block *discr_list_data = nullptr;
997};
998
999/* This represents a DW_TAG_variant_part. */
1000
1001struct variant_part_builder
1002{
1003 /* The offset of the discriminant field. */
1004 sect_offset discriminant_offset {};
1005
1006 /* Variants that are direct children of this variant part. */
1007 std::vector<variant_field> variants;
1008
1009 /* True if we're currently reading a variant. */
1010 bool processing_variant = false;
2ddeaf8a
TT
1011};
1012
52059ffd
TT
1013struct nextfield
1014{
be2daae6
TT
1015 int accessibility = 0;
1016 int virtuality = 0;
9c6a1327
TT
1017 /* Variant parts need to find the discriminant, which is a DIE
1018 reference. We track the section offset of each field to make
1019 this link. */
1020 sect_offset offset;
be2daae6 1021 struct field field {};
52059ffd
TT
1022};
1023
1024struct fnfieldlist
1025{
be2daae6
TT
1026 const char *name = nullptr;
1027 std::vector<struct fn_field> fnfields;
52059ffd
TT
1028};
1029
c906108c
SS
1030/* The routines that read and process dies for a C struct or C++ class
1031 pass lists of data member fields and lists of member function fields
1032 in an instance of a field_info structure, as defined below. */
1033struct field_info
2de01bdb
SM
1034{
1035 /* List of data member and baseclasses fields. */
1036 std::vector<struct nextfield> fields;
1037 std::vector<struct nextfield> baseclasses;
1038
1039 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1040 bool non_public_fields = false;
2de01bdb
SM
1041
1042 /* Member function fieldlist array, contains name of possibly overloaded
1043 member function, number of overloaded member functions and a pointer
1044 to the head of the member function field chain. */
1045 std::vector<struct fnfieldlist> fnfieldlists;
1046
1047 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1048 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1049 std::vector<struct decl_field> typedef_field_list;
1050
1051 /* Nested types defined by this class and the number of elements in this
1052 list. */
1053 std::vector<struct decl_field> nested_types_list;
1054
1055 /* If non-null, this is the variant part we are currently
1056 reading. */
1057 variant_part_builder *current_variant_part = nullptr;
1058 /* This holds all the top-level variant parts attached to the type
1059 we're reading. */
1060 std::vector<variant_part_builder> variant_parts;
1061
1062 /* Return the total number of fields (including baseclasses). */
1063 int nfields () const
c5aa993b 1064 {
2de01bdb
SM
1065 return fields.size () + baseclasses.size ();
1066 }
1067};
c906108c 1068
ae038cb0
DJ
1069/* Loaded secondary compilation units are kept in memory until they
1070 have not been referenced for the processing of this many
1071 compilation units. Set this to zero to disable caching. Cache
1072 sizes of up to at least twenty will improve startup time for
1073 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1074static int dwarf_max_cache_age = 5;
920d2a44 1075static void
b4f54984
DE
1076show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1077 struct cmd_list_element *c, const char *value)
920d2a44 1078{
3e43a32a 1079 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1080 "DWARF compilation units is %s.\n"),
920d2a44
AC
1081 value);
1082}
4390d890 1083\f
c906108c
SS
1084/* local function prototypes */
1085
918dd910
JK
1086static void dwarf2_find_base_address (struct die_info *die,
1087 struct dwarf2_cu *cu);
1088
891813be 1089static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1090 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1091 const char *name);
0018ea6f 1092
f1902523
JK
1093static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1094 const gdb_byte *info_ptr,
3e225074 1095 struct die_info *type_unit_die);
f1902523 1096
976ca316 1097static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1098
72bf9492
DJ
1099static void scan_partial_symbols (struct partial_die_info *,
1100 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1101 int, struct dwarf2_cu *);
c906108c 1102
72bf9492
DJ
1103static void add_partial_symbol (struct partial_die_info *,
1104 struct dwarf2_cu *);
63d06c5c 1105
72bf9492
DJ
1106static void add_partial_namespace (struct partial_die_info *pdi,
1107 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1108 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1109
5d7cb8df 1110static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1111 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1112 struct dwarf2_cu *cu);
1113
72bf9492
DJ
1114static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1115 struct dwarf2_cu *cu);
91c24f0a 1116
bc30ff58
JB
1117static void add_partial_subprogram (struct partial_die_info *pdi,
1118 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1119 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1120
d521ce57 1121static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1122
dee91e82 1123static struct partial_die_info *load_partial_dies
d521ce57 1124 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1125
fb816e8b
TV
1126/* A pair of partial_die_info and compilation unit. */
1127struct cu_partial_die_info
1128{
1129 /* The compilation unit of the partial_die_info. */
1130 struct dwarf2_cu *cu;
1131 /* A partial_die_info. */
1132 struct partial_die_info *pdi;
122cf0f2
AB
1133
1134 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1135 : cu (cu),
1136 pdi (pdi)
405feb71 1137 { /* Nothing. */ }
122cf0f2
AB
1138
1139private:
1140 cu_partial_die_info () = delete;
fb816e8b
TV
1141};
1142
122cf0f2
AB
1143static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1144 struct dwarf2_cu *);
72bf9492 1145
d521ce57 1146static const gdb_byte *read_attribute (const struct die_reader_specs *,
4444f407
TT
1147 struct attribute *,
1148 const struct attr_abbrev *,
7a5f294d 1149 const gdb_byte *);
18a8505e
AT
1150
1151static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1152 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1153
1154static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1155
976ca316
SM
1156static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1157 dwarf2_section_info *, sect_offset);
f4dc4d17 1158
ed2dc618 1159static const char *read_indirect_string
976ca316 1160 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1161 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1162
ed2dc618 1163static const char *read_indirect_string_at_offset
976ca316 1164 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1165
d521ce57
TT
1166static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1167 const gdb_byte *,
3019eac3
DE
1168 unsigned int *);
1169
18a8505e
AT
1170static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1171 ULONGEST str_index);
1172
1173static const char *read_stub_str_index (struct dwarf2_cu *cu,
1174 ULONGEST str_index);
3019eac3 1175
e142c38c
DJ
1176static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1177 struct dwarf2_cu *);
c906108c 1178
7d45c7c3 1179static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1180 struct dwarf2_cu *cu);
7d45c7c3 1181
a084a2a6
AT
1182static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1183
05cf31d1 1184static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1185 struct dwarf2_cu *cu);
05cf31d1 1186
e142c38c 1187static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1188
e142c38c 1189static struct die_info *die_specification (struct die_info *die,
f2f0e013 1190 struct dwarf2_cu **);
63d06c5c 1191
9c541725 1192static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1193 struct dwarf2_cu *cu);
debd256d 1194
f3f5162e 1195static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1196 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1197 CORE_ADDR, int decode_mapping);
c906108c 1198
804d2729
TT
1199static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1200 const char *);
c906108c 1201
a14ed312 1202static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1203 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1204
ff39bb5e 1205static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1206 struct dwarf2_cu *);
c906108c 1207
ff39bb5e 1208static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1209 struct type *type,
1210 const char *name,
1211 struct obstack *obstack,
12df843f 1212 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1213 const gdb_byte **bytes,
98bfdba5 1214 struct dwarf2_locexpr_baton **baton);
2df3850c 1215
57567375
TT
1216static struct type *read_subrange_index_type (struct die_info *die,
1217 struct dwarf2_cu *cu);
1218
e7c27a73 1219static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1220
b4ba55a1
JB
1221static int need_gnat_info (struct dwarf2_cu *);
1222
3e43a32a
MS
1223static struct type *die_descriptive_type (struct die_info *,
1224 struct dwarf2_cu *);
b4ba55a1
JB
1225
1226static void set_descriptive_type (struct type *, struct die_info *,
1227 struct dwarf2_cu *);
1228
e7c27a73
DJ
1229static struct type *die_containing_type (struct die_info *,
1230 struct dwarf2_cu *);
c906108c 1231
ff39bb5e 1232static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1233 struct dwarf2_cu *);
c906108c 1234
f792889a 1235static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1236
673bfd45
DE
1237static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1238
0d5cff50 1239static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1240
6e70227d 1241static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1242 const char *suffix, int physname,
1243 struct dwarf2_cu *cu);
63d06c5c 1244
e7c27a73 1245static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1246
348e048f
DE
1247static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1248
e7c27a73 1249static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1250
e7c27a73 1251static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1252
96408a79
SA
1253static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1254
71a3c369
TT
1255static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1256
41144253 1257/* Return the .debug_loclists section to use for cu. */
1258static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1259
d0ce17d8
CT
1260/* Return the .debug_rnglists section to use for cu. */
1261static struct dwarf2_section_info *cu_debug_rnglists_section
1262 (struct dwarf2_cu *cu, dwarf_tag tag);
1263
3a2b436a 1264/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1265 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1266enum pc_bounds_kind
1267{
e385593e 1268 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1269 PC_BOUNDS_NOT_PRESENT,
1270
e385593e
JK
1271 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1272 were present but they do not form a valid range of PC addresses. */
1273 PC_BOUNDS_INVALID,
1274
3a2b436a
JK
1275 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1276 PC_BOUNDS_RANGES,
1277
1278 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1279 PC_BOUNDS_HIGH_LOW,
1280};
1281
1282static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1283 CORE_ADDR *, CORE_ADDR *,
1284 struct dwarf2_cu *,
891813be 1285 dwarf2_psymtab *);
c906108c 1286
fae299cd
DC
1287static void get_scope_pc_bounds (struct die_info *,
1288 CORE_ADDR *, CORE_ADDR *,
1289 struct dwarf2_cu *);
1290
801e3a5b 1291static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1292 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1293
a14ed312 1294static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1295 struct dwarf2_cu *);
c906108c 1296
a14ed312 1297static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1298 struct type *, struct dwarf2_cu *);
c906108c 1299
a14ed312 1300static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1301 struct die_info *, struct type *,
e7c27a73 1302 struct dwarf2_cu *);
c906108c 1303
a14ed312 1304static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1305 struct type *,
1306 struct dwarf2_cu *);
c906108c 1307
134d01f1 1308static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1309
e7c27a73 1310static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1311
e7c27a73 1312static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1313
5d7cb8df
JK
1314static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1315
804d2729 1316static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1317
27aa8d6a
SW
1318static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1319
74921315
KS
1320static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1321
f55ee35c
JK
1322static struct type *read_module_type (struct die_info *die,
1323 struct dwarf2_cu *cu);
1324
38d518c9 1325static const char *namespace_name (struct die_info *die,
e142c38c 1326 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1327
134d01f1 1328static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1329
7d79de9a
TT
1330static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1331 bool * = nullptr);
c906108c 1332
6e70227d 1333static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1334 struct dwarf2_cu *);
1335
bf6af496 1336static struct die_info *read_die_and_siblings_1
d521ce57 1337 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1338 struct die_info *);
639d11d3 1339
dee91e82 1340static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1341 const gdb_byte *info_ptr,
1342 const gdb_byte **new_info_ptr,
639d11d3
DC
1343 struct die_info *parent);
1344
d521ce57
TT
1345static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1346 struct die_info **, const gdb_byte *,
3e225074 1347 int);
3019eac3 1348
d521ce57 1349static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1350 struct die_info **, const gdb_byte *);
93311388 1351
e7c27a73 1352static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1353
15d034d0 1354static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1355 struct objfile *);
71c25dea 1356
15d034d0 1357static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1358
15d034d0 1359static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1360 struct die_info *die,
1361 struct dwarf2_cu *cu);
1362
ca69b9e6
DE
1363static const char *dwarf2_physname (const char *name, struct die_info *die,
1364 struct dwarf2_cu *cu);
1365
e142c38c 1366static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1367 struct dwarf2_cu **);
9219021c 1368
d97bc12b
DE
1369static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1370
1371static void dump_die_for_error (struct die_info *);
1372
1373static void dump_die_1 (struct ui_file *, int level, int max_level,
1374 struct die_info *);
c906108c 1375
d97bc12b 1376/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1377
51545339 1378static void store_in_ref_table (struct die_info *,
10b3939b 1379 struct dwarf2_cu *);
c906108c 1380
348e048f 1381static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1382 const struct attribute *,
348e048f
DE
1383 struct dwarf2_cu **);
1384
10b3939b 1385static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1386 const struct attribute *,
f2f0e013 1387 struct dwarf2_cu **);
c906108c 1388
348e048f 1389static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1390 const struct attribute *,
348e048f
DE
1391 struct dwarf2_cu **);
1392
ac9ec31b
DE
1393static struct type *get_signatured_type (struct die_info *, ULONGEST,
1394 struct dwarf2_cu *);
1395
1396static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1397 const struct attribute *,
ac9ec31b
DE
1398 struct dwarf2_cu *);
1399
ab432490
SM
1400static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1401 dwarf2_per_objfile *per_objfile);
348e048f 1402
ab432490
SM
1403static void read_signatured_type (signatured_type *sig_type,
1404 dwarf2_per_objfile *per_objfile);
348e048f 1405
63e43d3a
PMR
1406static int attr_to_dynamic_prop (const struct attribute *attr,
1407 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1408 struct dynamic_prop *prop, struct type *type);
63e43d3a 1409
c906108c
SS
1410/* memory allocation interface */
1411
7b5a2f43 1412static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1413
b60c80d6 1414static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1415
43f3e411 1416static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1417
8cf6f0b1
TT
1418static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1419 struct dwarf2_loclist_baton *baton,
ff39bb5e 1420 const struct attribute *attr);
8cf6f0b1 1421
ff39bb5e 1422static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1423 struct symbol *sym,
f1e6e072
TT
1424 struct dwarf2_cu *cu,
1425 int is_block);
4c2df51b 1426
d521ce57
TT
1427static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1428 const gdb_byte *info_ptr,
7c32eebb 1429 const struct abbrev_info *abbrev);
4bb7a0a7 1430
72bf9492
DJ
1431static hashval_t partial_die_hash (const void *item);
1432
1433static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1434
ae038cb0 1435static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1436 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1437 dwarf2_per_objfile *per_objfile);
ae038cb0 1438
9816fde3 1439static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1440 struct die_info *comp_unit_die,
1441 enum language pretend_language);
93311388 1442
f792889a 1443static struct type *set_die_type (struct die_info *, struct type *,
57567375 1444 struct dwarf2_cu *, bool = false);
1c379e20 1445
976ca316 1446static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1447
ab432490
SM
1448static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1449 dwarf2_per_objfile *per_objfile,
4a636814 1450 dwarf2_cu *existing_cu,
ab432490
SM
1451 bool skip_partial,
1452 enum language pretend_language);
10b3939b 1453
8fc0b21d 1454static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1455 enum language pretend_language);
10b3939b 1456
8fc0b21d 1457static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1458 enum language pretend_language);
f4dc4d17 1459
b64f50a1 1460static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1461 dwarf2_per_cu_data *per_cu,
1462 dwarf2_per_objfile *per_objfile);
673bfd45 1463
f792889a 1464static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1465
120ce1b5
SM
1466static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1467 dwarf2_per_objfile *per_objfile,
95554aad
TT
1468 enum language pretend_language);
1469
976ca316 1470static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1471
b303c6f6
AB
1472/* Class, the destructor of which frees all allocated queue entries. This
1473 will only have work to do if an error was thrown while processing the
1474 dwarf. If no error was thrown then the queue entries should have all
1475 been processed, and freed, as we went along. */
1476
1477class dwarf2_queue_guard
1478{
1479public:
39856def
TT
1480 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1481 : m_per_objfile (per_objfile)
1482 {
08ac5771
SM
1483 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1484
1485 m_per_objfile->per_bfd->queue.emplace ();
39856def 1486 }
b303c6f6
AB
1487
1488 /* Free any entries remaining on the queue. There should only be
1489 entries left if we hit an error while processing the dwarf. */
1490 ~dwarf2_queue_guard ()
1491 {
08ac5771
SM
1492 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1493
1494 m_per_objfile->per_bfd->queue.reset ();
39856def 1495 }
b303c6f6 1496
39856def 1497 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1498
39856def
TT
1499private:
1500 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1501};
1502
39856def
TT
1503dwarf2_queue_item::~dwarf2_queue_item ()
1504{
1505 /* Anything still marked queued is likely to be in an
1506 inconsistent state, so discard it. */
1507 if (per_cu->queued)
1508 {
7188ed02 1509 per_objfile->remove_cu (per_cu);
39856def
TT
1510 per_cu->queued = 0;
1511 }
1512}
1513
473ab964
TT
1514/* See dwarf2/read.h. */
1515
1516void
1517dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1518{
1519 if (data->is_debug_types)
1520 delete static_cast<signatured_type *> (data);
1521 else
1522 delete data;
1523}
1524
d721ba37
PA
1525/* The return type of find_file_and_directory. Note, the enclosed
1526 string pointers are only valid while this object is valid. */
1527
1528struct file_and_directory
1529{
1530 /* The filename. This is never NULL. */
1531 const char *name;
1532
1533 /* The compilation directory. NULL if not known. If we needed to
1534 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1535 points directly to the DW_AT_comp_dir string attribute owned by
1536 the obstack that owns the DIE. */
1537 const char *comp_dir;
1538
1539 /* If we needed to build a new string for comp_dir, this is what
1540 owns the storage. */
1541 std::string comp_dir_storage;
1542};
1543
1544static file_and_directory find_file_and_directory (struct die_info *die,
1545 struct dwarf2_cu *cu);
9291a0cd 1546
298e9637 1547static htab_up allocate_signatured_type_table ();
1fd400ff 1548
298e9637 1549static htab_up allocate_dwo_unit_table ();
3019eac3 1550
57d63ce2 1551static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1552 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1553 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1554
976ca316 1555static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1556
3019eac3 1557static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1558 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1559 ULONGEST signature);
3019eac3
DE
1560
1561static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1562 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1563
1b555f17 1564static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1565
263db9a1
TT
1566/* A unique pointer to a dwo_file. */
1567
51ac9db5 1568typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1569
976ca316 1570static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1571
1b80a9fa 1572static void check_producer (struct dwarf2_cu *cu);
4390d890
DE
1573\f
1574/* Various complaints about symbol reading that don't abort the process. */
1575
4390d890
DE
1576static void
1577dwarf2_debug_line_missing_file_complaint (void)
1578{
b98664d3 1579 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1580}
1581
1582static void
1583dwarf2_debug_line_missing_end_sequence_complaint (void)
1584{
b98664d3 1585 complaint (_(".debug_line section has line "
4390d890
DE
1586 "program sequence without an end"));
1587}
1588
1589static void
1590dwarf2_complex_location_expr_complaint (void)
1591{
b98664d3 1592 complaint (_("location expression too complex"));
4390d890
DE
1593}
1594
1595static void
1596dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1597 int arg3)
1598{
b98664d3 1599 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1600 arg1, arg2, arg3);
1601}
1602
4390d890
DE
1603static void
1604dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1605{
b98664d3 1606 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1607 arg1, arg2);
1608}
527f3840
JK
1609
1610/* Hash function for line_header_hash. */
1611
1612static hashval_t
1613line_header_hash (const struct line_header *ofs)
1614{
9c541725 1615 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1616}
1617
1618/* Hash function for htab_create_alloc_ex for line_header_hash. */
1619
1620static hashval_t
1621line_header_hash_voidp (const void *item)
1622{
9a3c8263 1623 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1624
1625 return line_header_hash (ofs);
1626}
1627
1628/* Equality function for line_header_hash. */
1629
1630static int
1631line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1632{
9a3c8263
SM
1633 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1634 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1635
9c541725 1636 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1637 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1638}
1639
4390d890 1640\f
9291a0cd 1641
de2143d6
TT
1642/* An iterator for all_comp_units that is based on index. This
1643 approach makes it possible to iterate over all_comp_units safely,
1644 when some caller in the loop may add new units. */
1645
1646class all_comp_units_iterator
1647{
1648public:
1649
1650 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1651 : m_per_bfd (per_bfd),
1652 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1653 {
1654 }
1655
1656 all_comp_units_iterator &operator++ ()
1657 {
1658 ++m_index;
1659 return *this;
1660 }
1661
1662 dwarf2_per_cu_data *operator* () const
1663 {
1664 return m_per_bfd->get_cu (m_index);
1665 }
1666
1667 bool operator== (const all_comp_units_iterator &other) const
1668 {
1669 return m_index == other.m_index;
1670 }
1671
1672
1673 bool operator!= (const all_comp_units_iterator &other) const
1674 {
1675 return m_index != other.m_index;
1676 }
1677
1678private:
1679
1680 dwarf2_per_bfd *m_per_bfd;
1681 size_t m_index;
1682};
1683
1684/* A range adapter for the all_comp_units_iterator. */
1685class all_comp_units_range
1686{
1687public:
1688
1689 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1690 : m_per_bfd (per_bfd)
1691 {
1692 }
1693
1694 all_comp_units_iterator begin ()
1695 {
1696 return all_comp_units_iterator (m_per_bfd, true);
1697 }
1698
1699 all_comp_units_iterator end ()
1700 {
1701 return all_comp_units_iterator (m_per_bfd, false);
1702 }
1703
1704private:
1705
1706 dwarf2_per_bfd *m_per_bfd;
1707};
1708
330cdd98
PA
1709/* See declaration. */
1710
5989a64e
SM
1711dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1712 bool can_copy_)
c3699833
SM
1713 : obfd (obfd),
1714 can_copy (can_copy_)
330cdd98
PA
1715{
1716 if (names == NULL)
1717 names = &dwarf2_elf_names;
1718
330cdd98
PA
1719 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1720 locate_sections (obfd, sec, *names);
1721}
1722
5989a64e 1723dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1724{
0d305d5c 1725 for (auto &per_cu : all_comp_units)
ae640021 1726 per_cu->imported_symtabs_free ();
fc8e7e75 1727
5989a64e 1728 /* Everything else should be on this->obstack. */
330cdd98
PA
1729}
1730
7188ed02 1731/* See read.h. */
330cdd98
PA
1732
1733void
7188ed02 1734dwarf2_per_objfile::remove_all_cus ()
330cdd98 1735{
08ac5771
SM
1736 gdb_assert (!this->per_bfd->queue.has_value ());
1737
7188ed02
SM
1738 for (auto pair : m_dwarf2_cus)
1739 delete pair.second;
330cdd98 1740
7188ed02 1741 m_dwarf2_cus.clear ();
330cdd98
PA
1742}
1743
11ed8cad
TT
1744/* A helper class that calls free_cached_comp_units on
1745 destruction. */
1746
1747class free_cached_comp_units
1748{
1749public:
1750
1751 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1752 : m_per_objfile (per_objfile)
1753 {
1754 }
1755
1756 ~free_cached_comp_units ()
1757 {
7188ed02 1758 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1759 }
1760
1761 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1762
1763private:
1764
1765 dwarf2_per_objfile *m_per_objfile;
1766};
1767
af758d11
SM
1768/* See read.h. */
1769
1770bool
1771dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1772{
de2143d6
TT
1773 if (per_cu->index < this->m_symtabs.size ())
1774 return this->m_symtabs[per_cu->index] != nullptr;
1775 return false;
af758d11
SM
1776}
1777
1778/* See read.h. */
1779
1780compunit_symtab *
1781dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1782{
de2143d6
TT
1783 if (per_cu->index < this->m_symtabs.size ())
1784 return this->m_symtabs[per_cu->index];
1785 return nullptr;
af758d11
SM
1786}
1787
1788/* See read.h. */
1789
1790void
1791dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1792 compunit_symtab *symtab)
1793{
de2143d6
TT
1794 if (per_cu->index >= this->m_symtabs.size ())
1795 this->m_symtabs.resize (per_cu->index + 1);
af758d11 1796 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
af758d11
SM
1797 this->m_symtabs[per_cu->index] = symtab;
1798}
1799
c906108c 1800/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1801 information and return true if we have enough to do something.
1802 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1803 ELF names are used. CAN_COPY is true for formats where symbol
1804 interposition is possible and so symbol values must follow copy
1805 relocation rules. */
c906108c
SS
1806
1807int
251d32d9 1808dwarf2_has_info (struct objfile *objfile,
dda83cd7 1809 const struct dwarf2_debug_sections *names,
4b610737 1810 bool can_copy)
c906108c 1811{
97cbe998
SDJ
1812 if (objfile->flags & OBJF_READNEVER)
1813 return 0;
1814
976ca316 1815 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1816
976ca316 1817 if (per_objfile == NULL)
5989a64e 1818 {
17ee85fc
TT
1819 dwarf2_per_bfd *per_bfd;
1820
8a91fbdf
SM
1821 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1822 BFD doesn't require relocations.
1823
1824 We don't share with objfiles for which -readnow was requested,
1825 because it would complicate things when loading the same BFD with
1826 -readnow and then without -readnow. */
1827 if (!gdb_bfd_requires_relocations (objfile->obfd)
1828 && (objfile->flags & OBJF_READNOW) == 0)
17ee85fc
TT
1829 {
1830 /* See if one has been created for this BFD yet. */
1831 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1832
1833 if (per_bfd == nullptr)
1834 {
1835 /* No, create it now. */
1836 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1837 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1838 }
1839 }
1840 else
1841 {
1842 /* No sharing possible, create one specifically for this objfile. */
1843 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1844 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1845 }
5989a64e 1846
976ca316 1847 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1848 }
5bfd760d 1849
976ca316
SM
1850 return (!per_objfile->per_bfd->info.is_virtual
1851 && per_objfile->per_bfd->info.s.section != NULL
1852 && !per_objfile->per_bfd->abbrev.is_virtual
1853 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1854}
1855
330cdd98 1856/* See declaration. */
c906108c 1857
330cdd98 1858void
5989a64e
SM
1859dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1860 const dwarf2_debug_sections &names)
c906108c 1861{
fd361982 1862 flagword aflag = bfd_section_flags (sectp);
251d32d9 1863
dc7650b8
JK
1864 if ((aflag & SEC_HAS_CONTENTS) == 0)
1865 {
1866 }
950b7495
KS
1867 else if (elf_section_data (sectp)->this_hdr.sh_size
1868 > bfd_get_file_size (abfd))
1869 {
1870 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1871 warning (_("Discarding section %s which has a section size (%s"
1872 ") larger than the file size [in module %s]"),
1873 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1874 bfd_get_filename (abfd));
1875 }
fbedd546 1876 else if (names.info.matches (sectp->name))
c906108c 1877 {
330cdd98 1878 this->info.s.section = sectp;
fd361982 1879 this->info.size = bfd_section_size (sectp);
c906108c 1880 }
fbedd546 1881 else if (names.abbrev.matches (sectp->name))
c906108c 1882 {
330cdd98 1883 this->abbrev.s.section = sectp;
fd361982 1884 this->abbrev.size = bfd_section_size (sectp);
c906108c 1885 }
fbedd546 1886 else if (names.line.matches (sectp->name))
c906108c 1887 {
330cdd98 1888 this->line.s.section = sectp;
fd361982 1889 this->line.size = bfd_section_size (sectp);
c906108c 1890 }
fbedd546 1891 else if (names.loc.matches (sectp->name))
c906108c 1892 {
330cdd98 1893 this->loc.s.section = sectp;
fd361982 1894 this->loc.size = bfd_section_size (sectp);
c906108c 1895 }
fbedd546 1896 else if (names.loclists.matches (sectp->name))
43988095 1897 {
330cdd98 1898 this->loclists.s.section = sectp;
fd361982 1899 this->loclists.size = bfd_section_size (sectp);
43988095 1900 }
fbedd546 1901 else if (names.macinfo.matches (sectp->name))
c906108c 1902 {
330cdd98 1903 this->macinfo.s.section = sectp;
fd361982 1904 this->macinfo.size = bfd_section_size (sectp);
c906108c 1905 }
fbedd546 1906 else if (names.macro.matches (sectp->name))
cf2c3c16 1907 {
330cdd98 1908 this->macro.s.section = sectp;
fd361982 1909 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1910 }
fbedd546 1911 else if (names.str.matches (sectp->name))
c906108c 1912 {
330cdd98 1913 this->str.s.section = sectp;
fd361982 1914 this->str.size = bfd_section_size (sectp);
c906108c 1915 }
fbedd546 1916 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
1917 {
1918 this->str_offsets.s.section = sectp;
1919 this->str_offsets.size = bfd_section_size (sectp);
1920 }
fbedd546 1921 else if (names.line_str.matches (sectp->name))
43988095 1922 {
330cdd98 1923 this->line_str.s.section = sectp;
fd361982 1924 this->line_str.size = bfd_section_size (sectp);
43988095 1925 }
fbedd546 1926 else if (names.addr.matches (sectp->name))
3019eac3 1927 {
330cdd98 1928 this->addr.s.section = sectp;
fd361982 1929 this->addr.size = bfd_section_size (sectp);
3019eac3 1930 }
fbedd546 1931 else if (names.frame.matches (sectp->name))
b6af0555 1932 {
330cdd98 1933 this->frame.s.section = sectp;
fd361982 1934 this->frame.size = bfd_section_size (sectp);
b6af0555 1935 }
fbedd546 1936 else if (names.eh_frame.matches (sectp->name))
b6af0555 1937 {
330cdd98 1938 this->eh_frame.s.section = sectp;
fd361982 1939 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1940 }
fbedd546 1941 else if (names.ranges.matches (sectp->name))
af34e669 1942 {
330cdd98 1943 this->ranges.s.section = sectp;
fd361982 1944 this->ranges.size = bfd_section_size (sectp);
af34e669 1945 }
fbedd546 1946 else if (names.rnglists.matches (sectp->name))
43988095 1947 {
330cdd98 1948 this->rnglists.s.section = sectp;
fd361982 1949 this->rnglists.size = bfd_section_size (sectp);
43988095 1950 }
fbedd546 1951 else if (names.types.matches (sectp->name))
348e048f 1952 {
8b70b953
TT
1953 struct dwarf2_section_info type_section;
1954
1955 memset (&type_section, 0, sizeof (type_section));
049412e3 1956 type_section.s.section = sectp;
fd361982 1957 type_section.size = bfd_section_size (sectp);
8b70b953 1958
fd5866f6 1959 this->types.push_back (type_section);
348e048f 1960 }
fbedd546 1961 else if (names.gdb_index.matches (sectp->name))
9291a0cd 1962 {
330cdd98 1963 this->gdb_index.s.section = sectp;
fd361982 1964 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1965 }
fbedd546 1966 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
1967 {
1968 this->debug_names.s.section = sectp;
fd361982 1969 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 1970 }
fbedd546 1971 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
1972 {
1973 this->debug_aranges.s.section = sectp;
fd361982 1974 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1975 }
dce234bc 1976
fd361982
AM
1977 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1978 && bfd_section_vma (sectp) == 0)
330cdd98 1979 this->has_section_at_zero = true;
c906108c
SS
1980}
1981
dce234bc 1982/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1983 SECTION_NAME. */
af34e669 1984
dce234bc 1985void
3017a003 1986dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
1987 enum dwarf2_section_enum sect,
1988 asection **sectp, const gdb_byte **bufp,
1989 bfd_size_type *sizep)
dce234bc 1990{
976ca316 1991 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 1992 struct dwarf2_section_info *info;
a3b2a86b
TT
1993
1994 /* We may see an objfile without any DWARF, in which case we just
1995 return nothing. */
976ca316 1996 if (per_objfile == NULL)
a3b2a86b
TT
1997 {
1998 *sectp = NULL;
1999 *bufp = NULL;
2000 *sizep = 0;
2001 return;
2002 }
3017a003
TG
2003 switch (sect)
2004 {
2005 case DWARF2_DEBUG_FRAME:
976ca316 2006 info = &per_objfile->per_bfd->frame;
3017a003
TG
2007 break;
2008 case DWARF2_EH_FRAME:
976ca316 2009 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2010 break;
2011 default:
2012 gdb_assert_not_reached ("unexpected section");
2013 }
dce234bc 2014
96b79293 2015 info->read (objfile);
dce234bc 2016
96b79293 2017 *sectp = info->get_bfd_section ();
dce234bc
PP
2018 *bufp = info->buffer;
2019 *sizep = info->size;
2020}
2021
9291a0cd 2022\f
39298a5d 2023/* DWARF quick_symbol_functions support. */
7b9f3c50
DE
2024
2025/* TUs can share .debug_line entries, and there can be a lot more TUs than
2026 unique line tables, so we maintain a separate table of all .debug_line
2027 derived entries to support the sharing.
2028 All the quick functions need is the list of file names. We discard the
2029 line_header when we're done and don't need to record it here. */
2030struct quick_file_names
2031{
094b34ac
DE
2032 /* The data used to construct the hash key. */
2033 struct stmt_list_hash hash;
7b9f3c50
DE
2034
2035 /* The number of entries in file_names, real_names. */
2036 unsigned int num_file_names;
2037
2038 /* The file names from the line table, after being run through
2039 file_full_name. */
2040 const char **file_names;
2041
2042 /* The file names from the line table after being run through
2043 gdb_realpath. These are computed lazily. */
2044 const char **real_names;
2045};
2046
2047/* When using the index (and thus not using psymtabs), each CU has an
2048 object of this type. This is used to hold information needed by
2049 the various "quick" methods. */
2050struct dwarf2_per_cu_quick_data
2051{
2052 /* The file table. This can be NULL if there was no file table
2053 or it's currently not read in.
5989a64e 2054 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2055 struct quick_file_names *file_names;
2056
7b9f3c50
DE
2057 /* A temporary mark bit used when iterating over all CUs in
2058 expand_symtabs_matching. */
2059 unsigned int mark : 1;
2060
2061 /* True if we've tried to read the file table and found there isn't one.
2062 There will be no point in trying to read it again next time. */
2063 unsigned int no_file_data : 1;
2064};
2065
edc02ceb
TT
2066/* A subclass of psymbol_functions that arranges to read the DWARF
2067 partial symbols when needed. */
2068struct lazy_dwarf_reader : public psymbol_functions
2069{
2070 using psymbol_functions::psymbol_functions;
2071
2072 bool can_lazily_read_symbols () override
2073 {
2074 return true;
2075 }
2076
2077 void read_partial_symbols (struct objfile *objfile) override
2078 {
2079 if (dwarf2_has_info (objfile, nullptr))
2080 dwarf2_build_psymtabs (objfile, this);
2081 }
2082};
2083
2084static quick_symbol_functions_up
2085make_lazy_dwarf_reader ()
2086{
2087 return quick_symbol_functions_up (new lazy_dwarf_reader);
2088}
2089
39298a5d
TT
2090struct dwarf2_base_index_functions : public quick_symbol_functions
2091{
2092 bool has_symbols (struct objfile *objfile) override;
2093
fc4d5ebf
AB
2094 bool has_unexpanded_symtabs (struct objfile *objfile) override;
2095
39298a5d
TT
2096 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2097
2098 void forget_cached_source_info (struct objfile *objfile) override;
2099
39298a5d
TT
2100 enum language lookup_global_symbol_language (struct objfile *objfile,
2101 const char *name,
2102 domain_enum domain,
2103 bool *symbol_found_p) override
2104 {
2105 *symbol_found_p = false;
2106 return language_unknown;
2107 }
2108
4829711b 2109 void print_stats (struct objfile *objfile, bool print_bcache) override;
39298a5d
TT
2110
2111 void expand_all_symtabs (struct objfile *objfile) override;
2112
39298a5d
TT
2113 struct compunit_symtab *find_pc_sect_compunit_symtab
2114 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2115 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2116
2117 struct compunit_symtab *find_compunit_symtab_by_address
2118 (struct objfile *objfile, CORE_ADDR address) override
2119 {
2120 return nullptr;
2121 }
2122
2123 void map_symbol_filenames (struct objfile *objfile,
f4655dee
TT
2124 gdb::function_view<symbol_filename_ftype> fun,
2125 bool need_fullname) override;
39298a5d
TT
2126};
2127
2128struct dwarf2_gdb_index : public dwarf2_base_index_functions
2129{
39298a5d
TT
2130 void dump (struct objfile *objfile) override;
2131
0b7b2c2a 2132 void expand_matching_symbols
39298a5d
TT
2133 (struct objfile *,
2134 const lookup_name_info &lookup_name,
2135 domain_enum domain,
2136 int global,
39298a5d
TT
2137 symbol_compare_ftype *ordered_compare) override;
2138
df35e626 2139 bool expand_symtabs_matching
39298a5d
TT
2140 (struct objfile *objfile,
2141 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2142 const lookup_name_info *lookup_name,
2143 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2144 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2145 block_search_flags search_flags,
3bfa51a7 2146 domain_enum domain,
39298a5d
TT
2147 enum search_domain kind) override;
2148};
2149
2150struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2151{
39298a5d
TT
2152 void dump (struct objfile *objfile) override;
2153
0b7b2c2a 2154 void expand_matching_symbols
39298a5d
TT
2155 (struct objfile *,
2156 const lookup_name_info &lookup_name,
2157 domain_enum domain,
2158 int global,
39298a5d
TT
2159 symbol_compare_ftype *ordered_compare) override;
2160
df35e626 2161 bool expand_symtabs_matching
39298a5d
TT
2162 (struct objfile *objfile,
2163 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2164 const lookup_name_info *lookup_name,
2165 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2166 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2167 block_search_flags search_flags,
3bfa51a7 2168 domain_enum domain,
39298a5d
TT
2169 enum search_domain kind) override;
2170};
2171
edc02ceb 2172static quick_symbol_functions_up
39298a5d
TT
2173make_dwarf_gdb_index ()
2174{
2175 return quick_symbol_functions_up (new dwarf2_gdb_index);
2176}
2177
edc02ceb 2178static quick_symbol_functions_up
39298a5d
TT
2179make_dwarf_debug_names ()
2180{
2181 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2182}
2183
094b34ac
DE
2184/* Utility hash function for a stmt_list_hash. */
2185
2186static hashval_t
2187hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2188{
2189 hashval_t v = 0;
2190
2191 if (stmt_list_hash->dwo_unit != NULL)
2192 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2193 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2194 return v;
2195}
2196
2197/* Utility equality function for a stmt_list_hash. */
2198
2199static int
2200eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2201 const struct stmt_list_hash *rhs)
2202{
2203 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2204 return 0;
2205 if (lhs->dwo_unit != NULL
2206 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2207 return 0;
2208
9c541725 2209 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2210}
2211
7b9f3c50
DE
2212/* Hash function for a quick_file_names. */
2213
2214static hashval_t
2215hash_file_name_entry (const void *e)
2216{
9a3c8263
SM
2217 const struct quick_file_names *file_data
2218 = (const struct quick_file_names *) e;
7b9f3c50 2219
094b34ac 2220 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2221}
2222
2223/* Equality function for a quick_file_names. */
2224
2225static int
2226eq_file_name_entry (const void *a, const void *b)
2227{
9a3c8263
SM
2228 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2229 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2230
094b34ac 2231 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2232}
2233
2234/* Delete function for a quick_file_names. */
2235
2236static void
2237delete_file_name_entry (void *e)
2238{
9a3c8263 2239 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2240 int i;
2241
2242 for (i = 0; i < file_data->num_file_names; ++i)
2243 {
2244 xfree ((void*) file_data->file_names[i]);
2245 if (file_data->real_names)
2246 xfree ((void*) file_data->real_names[i]);
2247 }
2248
45940949
TT
2249 /* The space for the struct itself lives on the obstack, so we don't
2250 free it here. */
7b9f3c50
DE
2251}
2252
2253/* Create a quick_file_names hash table. */
2254
5895093f 2255static htab_up
7b9f3c50
DE
2256create_quick_file_names_table (unsigned int nr_initial_entries)
2257{
5895093f
TT
2258 return htab_up (htab_create_alloc (nr_initial_entries,
2259 hash_file_name_entry, eq_file_name_entry,
2260 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2261}
9291a0cd 2262
ab432490
SM
2263/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2264 function is unrelated to symtabs, symtab would have to be created afterwards.
2265 You should call age_cached_comp_units after processing the CU. */
918dd910 2266
1b555f17 2267static dwarf2_cu *
ab432490
SM
2268load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2269 bool skip_partial)
918dd910 2270{
3019eac3 2271 if (per_cu->is_debug_types)
ab432490 2272 load_full_type_unit (per_cu, per_objfile);
918dd910 2273 else
4a636814
SM
2274 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2275 skip_partial, language_minimal);
918dd910 2276
7188ed02
SM
2277 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2278 if (cu == nullptr)
1b555f17 2279 return nullptr; /* Dummy CU. */
2dc860c0 2280
7188ed02 2281 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2282
7188ed02 2283 return cu;
918dd910
JK
2284}
2285
1350c3b4 2286/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2287
9291a0cd 2288static void
97a1449a 2289dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2290 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2291{
f4dc4d17
DE
2292 /* Skip type_unit_groups, reading the type units they contain
2293 is handled elsewhere. */
197400e8 2294 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2295 return;
2296
08ac5771
SM
2297 {
2298 /* The destructor of dwarf2_queue_guard frees any entries left on
2299 the queue. After this point we're guaranteed to leave this function
2300 with the dwarf queue empty. */
2301 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2302
08ac5771
SM
2303 if (!per_objfile->symtab_set_p (per_cu))
2304 {
2305 queue_comp_unit (per_cu, per_objfile, language_minimal);
2306 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2307
2308 /* If we just loaded a CU from a DWO, and we're working with an index
2309 that may badly handle TUs, load all the TUs in that DWO as well.
2310 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2311 if (!per_cu->is_debug_types
2312 && cu != NULL
2313 && cu->dwo_unit != NULL
2314 && per_objfile->per_bfd->index_table != NULL
2315 && per_objfile->per_bfd->index_table->version <= 7
2316 /* DWP files aren't supported yet. */
2317 && get_dwp_file (per_objfile) == NULL)
2318 queue_and_load_all_dwo_tus (cu);
2319 }
9291a0cd 2320
08ac5771
SM
2321 process_queue (per_objfile);
2322 }
9291a0cd
TT
2323
2324 /* Age the cache, releasing compilation units that have not
2325 been used recently. */
976ca316 2326 per_objfile->age_comp_units ();
9291a0cd
TT
2327}
2328
97a1449a
SM
2329/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2330 the per-objfile for which this symtab is instantiated.
2331
2332 Returns the resulting symbol table. */
2fdf6df6 2333
43f3e411 2334static struct compunit_symtab *
97a1449a 2335dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2336 dwarf2_per_objfile *per_objfile,
97a1449a 2337 bool skip_partial)
9291a0cd 2338{
976ca316 2339 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2340
976ca316 2341 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2342 {
976ca316 2343 free_cached_comp_units freer (per_objfile);
c83dd867 2344 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2345 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2346 process_cu_includes (per_objfile);
9291a0cd 2347 }
f194fefb 2348
976ca316 2349 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2350}
2351
d3473f0c
TT
2352/* See read.h. */
2353
473ab964 2354dwarf2_per_cu_data_up
5989a64e 2355dwarf2_per_bfd::allocate_per_cu ()
d3473f0c 2356{
473ab964 2357 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
1859c670 2358 result->per_bfd = this;
c96e8b04 2359 result->index = all_comp_units.size ();
d3473f0c
TT
2360 return result;
2361}
2362
2363/* See read.h. */
2364
46c6bcf6 2365signatured_type_up
9ea36493 2366dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
d3473f0c 2367{
9ea36493 2368 signatured_type_up result (new signatured_type (signature));
0d305d5c 2369 result->per_bfd = this;
c96e8b04 2370 result->index = all_comp_units.size ();
cc653233 2371 result->is_debug_types = true;
91eea9cc 2372 tu_stats.nr_tus++;
d3473f0c
TT
2373 return result;
2374}
2375
168c9250 2376/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2377 obstack, and constructed with the specified field values. */
4b514bc8 2378
473ab964 2379static dwarf2_per_cu_data_up
168c9250
SM
2380create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2381 struct dwarf2_section_info *section,
2382 int is_dwz,
2383 sect_offset sect_off, ULONGEST length)
4b514bc8 2384{
473ab964 2385 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2386 the_cu->sect_off = sect_off;
2387 the_cu->length = length;
4b514bc8 2388 the_cu->section = section;
168c9250 2389 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2390 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2391 the_cu->is_dwz = is_dwz;
2392 return the_cu;
2393}
2394
2ec9a5e0
TT
2395/* A helper for create_cus_from_index that handles a given list of
2396 CUs. */
2fdf6df6 2397
74a0d9f6 2398static void
168c9250 2399create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2400 const gdb_byte *cu_list, offset_type n_elements,
2401 struct dwarf2_section_info *section,
b76e467d 2402 int is_dwz)
9291a0cd 2403{
12359b5e 2404 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2405 {
74a0d9f6 2406 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2407
2408 sect_offset sect_off
2409 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2410 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2411 cu_list += 2 * 8;
2412
473ab964 2413 dwarf2_per_cu_data_up per_cu
168c9250
SM
2414 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2415 length);
0d305d5c 2416 per_bfd->all_comp_units.push_back (std::move (per_cu));
9291a0cd 2417 }
9291a0cd
TT
2418}
2419
2ec9a5e0 2420/* Read the CU list from the mapped index, and use it to create all
168c9250 2421 the CU objects for PER_BFD. */
2ec9a5e0 2422
74a0d9f6 2423static void
168c9250 2424create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2425 const gdb_byte *cu_list, offset_type cu_list_elements,
2426 const gdb_byte *dwz_list, offset_type dwz_elements)
2427{
168c9250
SM
2428 gdb_assert (per_bfd->all_comp_units.empty ());
2429 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2430
168c9250
SM
2431 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2432 &per_bfd->info, 0);
2ec9a5e0
TT
2433
2434 if (dwz_elements == 0)
74a0d9f6 2435 return;
2ec9a5e0 2436
168c9250
SM
2437 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2438 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2439 &dwz->info, 1);
2ec9a5e0
TT
2440}
2441
1fd400ff 2442/* Create the signatured type hash table from the index. */
673bfd45 2443
74a0d9f6 2444static void
12359b5e 2445create_signatured_type_table_from_index
168c9250
SM
2446 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2447 const gdb_byte *bytes, offset_type elements)
1fd400ff 2448{
298e9637 2449 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2450
12359b5e 2451 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2452 {
46c6bcf6 2453 signatured_type_up sig_type;
9c541725 2454 ULONGEST signature;
1fd400ff 2455 void **slot;
9c541725 2456 cu_offset type_offset_in_tu;
1fd400ff 2457
74a0d9f6 2458 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2459 sect_offset sect_off
2460 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2461 type_offset_in_tu
2462 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2463 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2464 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2465 bytes += 3 * 8;
2466
9ea36493 2467 sig_type = per_bfd->allocate_signatured_type (signature);
9c541725 2468 sig_type->type_offset_in_tu = type_offset_in_tu;
0d305d5c
TT
2469 sig_type->section = section;
2470 sig_type->sect_off = sect_off;
2471 sig_type->v.quick
168c9250 2472 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2473 struct dwarf2_per_cu_quick_data);
2474
0d305d5c
TT
2475 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2476 *slot = sig_type.get ();
1fd400ff 2477
473ab964 2478 per_bfd->all_comp_units.emplace_back (sig_type.release ());
1fd400ff
TT
2479 }
2480
168c9250 2481 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2482}
2483
927aa2e7
JK
2484/* Create the signatured type hash table from .debug_names. */
2485
2486static void
2487create_signatured_type_table_from_debug_names
976ca316 2488 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2489 const mapped_debug_names &map,
2490 struct dwarf2_section_info *section,
2491 struct dwarf2_section_info *abbrev_section)
2492{
976ca316 2493 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2494
96b79293
TT
2495 section->read (objfile);
2496 abbrev_section->read (objfile);
927aa2e7 2497
298e9637 2498 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2499
2500 for (uint32_t i = 0; i < map.tu_count; ++i)
2501 {
46c6bcf6 2502 signatured_type_up sig_type;
927aa2e7 2503 void **slot;
927aa2e7
JK
2504
2505 sect_offset sect_off
2506 = (sect_offset) (extract_unsigned_integer
2507 (map.tu_table_reordered + i * map.offset_size,
2508 map.offset_size,
2509 map.dwarf5_byte_order));
2510
2511 comp_unit_head cu_header;
976ca316 2512 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2513 abbrev_section,
927aa2e7
JK
2514 section->buffer + to_underlying (sect_off),
2515 rcuh_kind::TYPE);
2516
9ea36493
SM
2517 sig_type = per_objfile->per_bfd->allocate_signatured_type
2518 (cu_header.signature);
927aa2e7 2519 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
0d305d5c
TT
2520 sig_type->section = section;
2521 sig_type->sect_off = sect_off;
2522 sig_type->v.quick
976ca316 2523 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2524 struct dwarf2_per_cu_quick_data);
2525
0d305d5c
TT
2526 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2527 *slot = sig_type.get ();
927aa2e7 2528
473ab964 2529 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
927aa2e7
JK
2530 }
2531
976ca316 2532 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2533}
2534
9291a0cd 2535/* Read the address map data from the mapped index, and use it to
efd7398e 2536 populate the psymtabs_addrmap. */
2fdf6df6 2537
9291a0cd 2538static void
976ca316 2539create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2540 struct mapped_index *index)
9291a0cd 2541{
976ca316 2542 struct objfile *objfile = per_objfile->objfile;
efd7398e 2543 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2544 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2545 const gdb_byte *iter, *end;
9291a0cd 2546 struct addrmap *mutable_map;
9291a0cd
TT
2547 CORE_ADDR baseaddr;
2548
8268c778
PA
2549 auto_obstack temp_obstack;
2550
9291a0cd
TT
2551 mutable_map = addrmap_create_mutable (&temp_obstack);
2552
f00a2de2
PA
2553 iter = index->address_table.data ();
2554 end = iter + index->address_table.size ();
9291a0cd 2555
b3b3bada 2556 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2557
2558 while (iter < end)
2559 {
2560 ULONGEST hi, lo, cu_index;
2561 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2562 iter += 8;
2563 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2564 iter += 8;
2565 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2566 iter += 4;
f652bce2 2567
24a55014 2568 if (lo > hi)
f652bce2 2569 {
b98664d3 2570 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2571 hex_string (lo), hex_string (hi));
24a55014 2572 continue;
f652bce2 2573 }
24a55014 2574
efd7398e 2575 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2576 {
b98664d3 2577 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2578 (unsigned) cu_index);
24a55014 2579 continue;
f652bce2 2580 }
24a55014 2581
79748972
TT
2582 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2583 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2584 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2585 per_bfd->get_cu (cu_index));
9291a0cd
TT
2586 }
2587
efd7398e
TT
2588 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2589 &per_bfd->obstack);
9291a0cd
TT
2590}
2591
927aa2e7 2592/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2593 populate the psymtabs_addrmap. */
927aa2e7
JK
2594
2595static void
976ca316 2596create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2597 struct dwarf2_section_info *section)
2598{
976ca316 2599 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2600 bfd *abfd = objfile->obfd;
08feed99 2601 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2602 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2603 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2604
2605 auto_obstack temp_obstack;
2606 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2607
2608 std::unordered_map<sect_offset,
2609 dwarf2_per_cu_data *,
2610 gdb::hash_enum<sect_offset>>
2611 debug_info_offset_to_per_cu;
0d305d5c 2612 for (const auto &per_cu : per_bfd->all_comp_units)
927aa2e7 2613 {
927aa2e7 2614 const auto insertpair
0d305d5c
TT
2615 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2616 per_cu.get ());
927aa2e7
JK
2617 if (!insertpair.second)
2618 {
2619 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2620 "debug_info_offset %s, ignoring .debug_aranges."),
2621 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2622 return;
2623 }
2624 }
2625
96b79293 2626 section->read (objfile);
927aa2e7
JK
2627
2628 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2629
2630 const gdb_byte *addr = section->buffer;
2631
2632 while (addr < section->buffer + section->size)
2633 {
2634 const gdb_byte *const entry_addr = addr;
2635 unsigned int bytes_read;
2636
2637 const LONGEST entry_length = read_initial_length (abfd, addr,
2638 &bytes_read);
2639 addr += bytes_read;
2640
2641 const gdb_byte *const entry_end = addr + entry_length;
2642 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2643 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2644 if (addr + entry_length > section->buffer + section->size)
2645 {
47e3f474 2646 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2647 "length %s exceeds section length %s, "
927aa2e7 2648 "ignoring .debug_aranges."),
47e3f474
TV
2649 objfile_name (objfile),
2650 plongest (entry_addr - section->buffer),
927aa2e7
JK
2651 plongest (bytes_read + entry_length),
2652 pulongest (section->size));
2653 return;
2654 }
2655
2656 /* The version number. */
2657 const uint16_t version = read_2_bytes (abfd, addr);
2658 addr += 2;
2659 if (version != 2)
2660 {
47e3f474 2661 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2662 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2663 objfile_name (objfile),
2664 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2665 return;
2666 }
2667
2668 const uint64_t debug_info_offset
2669 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2670 addr += offset_size;
2671 const auto per_cu_it
2672 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2673 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2674 {
47e3f474 2675 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2676 "debug_info_offset %s does not exists, "
2677 "ignoring .debug_aranges."),
47e3f474
TV
2678 objfile_name (objfile),
2679 plongest (entry_addr - section->buffer),
927aa2e7
JK
2680 pulongest (debug_info_offset));
2681 return;
2682 }
2683 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2684
2685 const uint8_t address_size = *addr++;
2686 if (address_size < 1 || address_size > 8)
2687 {
47e3f474 2688 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2689 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2690 objfile_name (objfile),
2691 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2692 return;
2693 }
2694
2695 const uint8_t segment_selector_size = *addr++;
2696 if (segment_selector_size != 0)
2697 {
47e3f474 2698 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2699 "segment_selector_size %u is not supported, "
2700 "ignoring .debug_aranges."),
47e3f474
TV
2701 objfile_name (objfile),
2702 plongest (entry_addr - section->buffer),
927aa2e7
JK
2703 segment_selector_size);
2704 return;
2705 }
2706
2707 /* Must pad to an alignment boundary that is twice the address
dda83cd7 2708 size. It is undocumented by the DWARF standard but GCC does
3da4c644
TT
2709 use it. However, not every compiler does this. We can see
2710 whether it has happened by looking at the total length of the
2711 contents of the aranges for this CU -- it if isn't a multiple
2712 of twice the address size, then we skip any leftover
2713 bytes. */
2714 addr += (entry_end - addr) % (2 * address_size);
927aa2e7
JK
2715
2716 for (;;)
2717 {
2718 if (addr + 2 * address_size > entry_end)
2719 {
47e3f474 2720 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2721 "address list is not properly terminated, "
2722 "ignoring .debug_aranges."),
47e3f474
TV
2723 objfile_name (objfile),
2724 plongest (entry_addr - section->buffer));
927aa2e7
JK
2725 return;
2726 }
2727 ULONGEST start = extract_unsigned_integer (addr, address_size,
2728 dwarf5_byte_order);
2729 addr += address_size;
2730 ULONGEST length = extract_unsigned_integer (addr, address_size,
2731 dwarf5_byte_order);
2732 addr += address_size;
2733 if (start == 0 && length == 0)
2734 break;
efd7398e 2735 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2736 {
2737 /* Symbol was eliminated due to a COMDAT group. */
2738 continue;
2739 }
2740 ULONGEST end = start + length;
79748972
TT
2741 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2742 - baseaddr);
2743 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2744 - baseaddr);
927aa2e7
JK
2745 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2746 }
2747 }
2748
efd7398e
TT
2749 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2750 &per_bfd->obstack);
927aa2e7
JK
2751}
2752
4485a1c1
SM
2753/* A helper function that reads the .gdb_index from BUFFER and fills
2754 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2755 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2756 ok to use deprecated sections.
2757
2758 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2759 out parameters that are filled in with information about the CU and
2760 TU lists in the section.
2761
4485a1c1 2762 Returns true if all went well, false otherwise. */
2fdf6df6 2763
d33bc52e 2764static bool
3810f182 2765read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2766 bool deprecated_ok,
2767 gdb::array_view<const gdb_byte> buffer,
2768 struct mapped_index *map,
2769 const gdb_byte **cu_list,
2770 offset_type *cu_list_elements,
2771 const gdb_byte **types_list,
2772 offset_type *types_list_elements)
2773{
2774 const gdb_byte *addr = &buffer[0];
42c2c694 2775 offset_view metadata (buffer);
82430852 2776
9291a0cd 2777 /* Version check. */
42c2c694 2778 offset_type version = metadata[0];
987d643c 2779 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2780 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2781 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2782 indices. */
831adc1f 2783 if (version < 4)
481860b3
GB
2784 {
2785 static int warning_printed = 0;
2786 if (!warning_printed)
2787 {
2788 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2789 filename);
481860b3
GB
2790 warning_printed = 1;
2791 }
2792 return 0;
2793 }
2794 /* Index version 4 uses a different hash function than index version
2795 5 and later.
2796
2797 Versions earlier than 6 did not emit psymbols for inlined
2798 functions. Using these files will cause GDB not to be able to
2799 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2800 indices unless the user has done
2801 "set use-deprecated-index-sections on". */
2ec9a5e0 2802 if (version < 6 && !deprecated_ok)
481860b3
GB
2803 {
2804 static int warning_printed = 0;
2805 if (!warning_printed)
2806 {
e615022a
DE
2807 warning (_("\
2808Skipping deprecated .gdb_index section in %s.\n\
2809Do \"set use-deprecated-index-sections on\" before the file is read\n\
2810to use the section anyway."),
2ec9a5e0 2811 filename);
481860b3
GB
2812 warning_printed = 1;
2813 }
2814 return 0;
2815 }
796a7ff8 2816 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2817 of the TU (for symbols coming from TUs),
2818 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2819 Plus gold-generated indices can have duplicate entries for global symbols,
2820 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2821 These are just performance bugs, and we can't distinguish gdb-generated
2822 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2823
481860b3 2824 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2825 longer backward compatible. */
796a7ff8 2826 if (version > 8)
594e8718 2827 return 0;
9291a0cd 2828
559a7a62 2829 map->version = version;
9291a0cd 2830
42c2c694
TT
2831 int i = 1;
2832 *cu_list = addr + metadata[i];
2833 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
1fd400ff
TT
2834 ++i;
2835
42c2c694
TT
2836 *types_list = addr + metadata[i];
2837 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
987d643c 2838 ++i;
1fd400ff 2839
42c2c694
TT
2840 const gdb_byte *address_table = addr + metadata[i];
2841 const gdb_byte *address_table_end = addr + metadata[i + 1];
f00a2de2
PA
2842 map->address_table
2843 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2844 ++i;
2845
42c2c694
TT
2846 const gdb_byte *symbol_table = addr + metadata[i];
2847 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
f00a2de2 2848 map->symbol_table
42c2c694
TT
2849 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2850 symbol_table_end));
9291a0cd 2851
f00a2de2 2852 ++i;
42c2c694 2853 map->constant_pool = buffer.slice (metadata[i]);
1fd400ff 2854
2ec9a5e0
TT
2855 return 1;
2856}
2857
4485a1c1
SM
2858/* Callback types for dwarf2_read_gdb_index. */
2859
2860typedef gdb::function_view
5989a64e 2861 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
2862 get_gdb_index_contents_ftype;
2863typedef gdb::function_view
2864 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2865 get_gdb_index_contents_dwz_ftype;
2866
927aa2e7 2867/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2868 elements of all the CUs and return 1. Otherwise, return 0. */
2869
2870static int
4485a1c1 2871dwarf2_read_gdb_index
976ca316 2872 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
2873 get_gdb_index_contents_ftype get_gdb_index_contents,
2874 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 2875{
2ec9a5e0
TT
2876 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2877 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2878 struct dwz_file *dwz;
976ca316 2879 struct objfile *objfile = per_objfile->objfile;
f8c41851 2880 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 2881
4485a1c1 2882 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 2883 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
2884
2885 if (main_index_contents.empty ())
2886 return 0;
2887
3063847f 2888 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 2889 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
2890 use_deprecated_index_sections,
2891 main_index_contents, map.get (), &cu_list,
2892 &cu_list_elements, &types_list,
2893 &types_list_elements))
2ec9a5e0
TT
2894 return 0;
2895
0fefef59 2896 /* Don't use the index if it's empty. */
3063847f 2897 if (map->symbol_table.empty ())
0fefef59
DE
2898 return 0;
2899
2ec9a5e0
TT
2900 /* If there is a .dwz file, read it so we can get its CU list as
2901 well. */
f8c41851 2902 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 2903 if (dwz != NULL)
2ec9a5e0 2904 {
2ec9a5e0
TT
2905 struct mapped_index dwz_map;
2906 const gdb_byte *dwz_types_ignore;
2907 offset_type dwz_types_elements_ignore;
2908
4485a1c1
SM
2909 gdb::array_view<const gdb_byte> dwz_index_content
2910 = get_gdb_index_contents_dwz (objfile, dwz);
2911
2912 if (dwz_index_content.empty ())
2913 return 0;
2914
3810f182 2915 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 2916 1, dwz_index_content, &dwz_map,
4485a1c1
SM
2917 &dwz_list, &dwz_list_elements,
2918 &dwz_types_ignore,
2919 &dwz_types_elements_ignore))
2ec9a5e0
TT
2920 {
2921 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 2922 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
2923 return 0;
2924 }
2925 }
2926
f8c41851
SM
2927 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2928 dwz_list_elements);
1fd400ff 2929
8b70b953
TT
2930 if (types_list_elements)
2931 {
8b70b953
TT
2932 /* We can only handle a single .debug_types when we have an
2933 index. */
f8c41851 2934 if (per_bfd->types.size () != 1)
8b70b953
TT
2935 return 0;
2936
f8c41851 2937 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 2938
f8c41851 2939 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 2940 types_list_elements);
8b70b953 2941 }
9291a0cd 2942
976ca316 2943 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 2944
f8c41851
SM
2945 per_bfd->index_table = std::move (map);
2946 per_bfd->using_index = 1;
2947 per_bfd->quick_file_names_table =
2948 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2949
9291a0cd
TT
2950 return 1;
2951}
2952
dee91e82 2953/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2954
dee91e82
DE
2955static void
2956dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 2957 struct die_info *comp_unit_die)
9291a0cd 2958{
dee91e82 2959 struct dwarf2_cu *cu = reader->cu;
ed2dc618 2960 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 2961 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 2962 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 2963 struct attribute *attr;
7b9f3c50
DE
2964 void **slot;
2965 struct quick_file_names *qfn;
9291a0cd 2966
0186c6a7
DE
2967 gdb_assert (! this_cu->is_debug_types);
2968
07261596
TT
2969 /* Our callers never want to match partial units -- instead they
2970 will match the enclosing full CU. */
2971 if (comp_unit_die->tag == DW_TAG_partial_unit)
2972 {
2973 this_cu->v.quick->no_file_data = 1;
2974 return;
2975 }
2976
0186c6a7 2977 lh_cu = this_cu;
7b9f3c50 2978 slot = NULL;
dee91e82 2979
fff8551c 2980 line_header_up lh;
9c541725 2981 sect_offset line_offset {};
fff8551c 2982
dee91e82 2983 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 2984 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 2985 {
7b9f3c50
DE
2986 struct quick_file_names find_entry;
2987
d4df075e 2988 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
2989
2990 /* We may have already read in this line header (TU line header sharing).
2991 If we have we're done. */
094b34ac 2992 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 2993 find_entry.hash.line_sect_off = line_offset;
976ca316 2994 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
2995 &find_entry, INSERT);
2996 if (*slot != NULL)
2997 {
9a3c8263 2998 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 2999 return;
7b9f3c50
DE
3000 }
3001
3019eac3 3002 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3003 }
3004 if (lh == NULL)
3005 {
094b34ac 3006 lh_cu->v.quick->no_file_data = 1;
dee91e82 3007 return;
9291a0cd
TT
3008 }
3009
976ca316 3010 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3011 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3012 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3013 gdb_assert (slot != NULL);
3014 *slot = qfn;
9291a0cd 3015
d721ba37 3016 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3017
aa391654
TT
3018 int offset = 0;
3019 if (strcmp (fnd.name, "<unknown>") != 0)
3020 ++offset;
3021
7ba99d21 3022 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3023 qfn->file_names =
976ca316 3024 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3025 qfn->num_file_names);
aa391654
TT
3026 if (offset != 0)
3027 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3028 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3029 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3030 fnd.comp_dir).release ();
7b9f3c50 3031 qfn->real_names = NULL;
9291a0cd 3032
094b34ac 3033 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3034}
3035
3036/* A helper for the "quick" functions which attempts to read the line
3037 table for THIS_CU. */
3038
3039static struct quick_file_names *
ab432490
SM
3040dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3041 dwarf2_per_objfile *per_objfile)
dee91e82 3042{
0186c6a7
DE
3043 /* This should never be called for TUs. */
3044 gdb_assert (! this_cu->is_debug_types);
3045 /* Nor type unit groups. */
197400e8 3046 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3047
dee91e82
DE
3048 if (this_cu->v.quick->file_names != NULL)
3049 return this_cu->v.quick->file_names;
3050 /* If we know there is no line data, no point in looking again. */
3051 if (this_cu->v.quick->no_file_data)
3052 return NULL;
3053
ab432490 3054 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3055 if (!reader.dummy_p)
eb5dd737 3056 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
3057
3058 if (this_cu->v.quick->no_file_data)
3059 return NULL;
3060 return this_cu->v.quick->file_names;
9291a0cd
TT
3061}
3062
3063/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3064 real path for a given file name from the line table. */
2fdf6df6 3065
9291a0cd 3066static const char *
976ca316 3067dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3068 struct quick_file_names *qfn, int index)
9291a0cd 3069{
7b9f3c50 3070 if (qfn->real_names == NULL)
976ca316 3071 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3072 qfn->num_file_names, const char *);
9291a0cd 3073
7b9f3c50 3074 if (qfn->real_names[index] == NULL)
14278e1f 3075 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3076
7b9f3c50 3077 return qfn->real_names[index];
9291a0cd
TT
3078}
3079
39298a5d
TT
3080struct symtab *
3081dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3082{
976ca316 3083 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
0d305d5c
TT
3084 dwarf2_per_cu_data *dwarf_cu
3085 = per_objfile->per_bfd->all_comp_units.back ().get ();
976ca316 3086 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3087
43f3e411
DE
3088 if (cust == NULL)
3089 return NULL;
ed2dc618 3090
43f3e411 3091 return compunit_primary_filetab (cust);
9291a0cd
TT
3092}
3093
7b9f3c50
DE
3094/* Traversal function for dw2_forget_cached_source_info. */
3095
3096static int
3097dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3098{
7b9f3c50 3099 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3100
7b9f3c50 3101 if (file_data->real_names)
9291a0cd 3102 {
7b9f3c50 3103 int i;
9291a0cd 3104
7b9f3c50 3105 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3106 {
7b9f3c50
DE
3107 xfree ((void*) file_data->real_names[i]);
3108 file_data->real_names[i] = NULL;
9291a0cd
TT
3109 }
3110 }
7b9f3c50
DE
3111
3112 return 1;
3113}
3114
39298a5d
TT
3115void
3116dwarf2_base_index_functions::forget_cached_source_info
3117 (struct objfile *objfile)
7b9f3c50 3118{
976ca316 3119 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3120
976ca316 3121 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3122 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3123}
3124
da51c347
DE
3125/* Struct used to manage iterating over all CUs looking for a symbol. */
3126
3127struct dw2_symtab_iterator
9291a0cd 3128{
ed2dc618 3129 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3130 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3131 /* If set, only look for symbols that match that block. Valid values are
3132 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3133 gdb::optional<block_enum> block_index;
da51c347
DE
3134 /* The kind of symbol we're looking for. */
3135 domain_enum domain;
3136 /* The list of CUs from the index entry of the symbol,
3137 or NULL if not found. */
42c2c694 3138 offset_view vec;
da51c347
DE
3139 /* The next element in VEC to look at. */
3140 int next;
3141 /* The number of elements in VEC, or zero if there is no match. */
3142 int length;
8943b874
DE
3143 /* Have we seen a global version of the symbol?
3144 If so we can ignore all further global instances.
3145 This is to work around gold/15646, inefficient gold-generated
3146 indices. */
3147 int global_seen;
da51c347 3148};
9291a0cd 3149
7089bd88 3150/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2fdf6df6 3151
9291a0cd 3152static void
7089bd88
TT
3153dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3154 dwarf2_per_objfile *per_objfile,
3155 gdb::optional<block_enum> block_index,
3156 domain_enum domain, offset_type namei)
da51c347 3157{
976ca316 3158 iter->per_objfile = per_objfile;
da51c347
DE
3159 iter->block_index = block_index;
3160 iter->domain = domain;
3161 iter->next = 0;
8943b874 3162 iter->global_seen = 0;
42c2c694 3163 iter->vec = {};
e5f3ece2 3164 iter->length = 0;
e5f3ece2
TV
3165
3166 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3167 /* index is NULL if OBJF_READNOW. */
3168 if (index == NULL)
3169 return;
3170
3171 gdb_assert (!index->symbol_name_slot_invalid (namei));
42c2c694 3172 offset_type vec_idx = index->symbol_vec_index (namei);
e5f3ece2 3173
42c2c694
TT
3174 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3175 iter->length = iter->vec[0];
da51c347
DE
3176}
3177
3178/* Return the next matching CU or NULL if there are no more. */
3179
3180static struct dwarf2_per_cu_data *
3181dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3182{
976ca316 3183 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3184
da51c347
DE
3185 for ( ; iter->next < iter->length; ++iter->next)
3186 {
42c2c694 3187 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
da51c347 3188 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3189 gdb_index_symbol_kind symbol_kind =
3190 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3191 /* Only check the symbol attributes if they're present.
3192 Indices prior to version 7 don't record them,
3193 and indices >= 7 may elide them for certain symbols
3194 (gold does this). */
3195 int attrs_valid =
976ca316 3196 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3197 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3198
3190f0c6 3199 /* Don't crash on bad data. */
91eea9cc 3200 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3190f0c6 3201 {
b98664d3 3202 complaint (_(".gdb_index entry has bad CU index"
976ca316 3203 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3204 continue;
3205 }
3206
91eea9cc 3207 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3190f0c6 3208
da51c347 3209 /* Skip if already read in. */
976ca316 3210 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3211 continue;
3212
8943b874
DE
3213 /* Check static vs global. */
3214 if (attrs_valid)
3215 {
2b79f376
SM
3216 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3217
3218 if (iter->block_index.has_value ())
3219 {
3220 bool want_static = *iter->block_index == STATIC_BLOCK;
3221
3222 if (is_static != want_static)
3223 continue;
3224 }
3225
8943b874 3226 /* Work around gold/15646. */
f030440d
TV
3227 if (!is_static
3228 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3229 {
3230 if (iter->global_seen)
3231 continue;
3232
3233 iter->global_seen = 1;
3234 }
8943b874 3235 }
da51c347
DE
3236
3237 /* Only check the symbol's kind if it has one. */
3238 if (attrs_valid)
3239 {
3240 switch (iter->domain)
3241 {
3242 case VAR_DOMAIN:
3243 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3244 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3245 /* Some types are also in VAR_DOMAIN. */
3246 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3247 continue;
3248 break;
3249 case STRUCT_DOMAIN:
3250 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3251 continue;
3252 break;
3253 case LABEL_DOMAIN:
3254 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3255 continue;
3256 break;
59c35742
AB
3257 case MODULE_DOMAIN:
3258 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3259 continue;
3260 break;
da51c347
DE
3261 default:
3262 break;
3263 }
3264 }
3265
3266 ++iter->next;
3267 return per_cu;
3268 }
3269
3270 return NULL;
3271}
3272
39298a5d 3273void
4829711b
TT
3274dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3275 bool print_bcache)
9291a0cd 3276{
4829711b
TT
3277 if (print_bcache)
3278 return;
3279
976ca316 3280 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3281 int total = per_objfile->per_bfd->all_comp_units.size ();
ed2dc618 3282 int count = 0;
9291a0cd 3283
ed2dc618 3284 for (int i = 0; i < total; ++i)
9291a0cd 3285 {
91eea9cc 3286 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3287
976ca316 3288 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3289 ++count;
3290 }
e4a48d9d 3291 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3292 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3293}
3294
779bd270
DE
3295/* This dumps minimal information about the index.
3296 It is called via "mt print objfiles".
3297 One use is to verify .gdb_index has been loaded by the
3298 gdb.dwarf2/gdb-index.exp testcase. */
3299
39298a5d
TT
3300void
3301dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3302{
976ca316 3303 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3304
976ca316 3305 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3306 printf_filtered (".gdb_index:");
976ca316 3307 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3308 {
3309 printf_filtered (" version %d\n",
976ca316 3310 per_objfile->per_bfd->index_table->version);
779bd270
DE
3311 }
3312 else
3313 printf_filtered (" faked for \"readnow\"\n");
3314 printf_filtered ("\n");
9291a0cd
TT
3315}
3316
39298a5d
TT
3317void
3318dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3319{
976ca316 3320 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3321 int total_units = per_objfile->per_bfd->all_comp_units.size ();
9291a0cd 3322
ed2dc618 3323 for (int i = 0; i < total_units; ++i)
9291a0cd 3324 {
91eea9cc 3325 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3326
58f0c718
TT
3327 /* We don't want to directly expand a partial CU, because if we
3328 read it with the wrong language, then assertion failures can
3329 be triggered later on. See PR symtab/23010. So, tell
3330 dw2_instantiate_symtab to skip partial CUs -- any important
3331 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3332 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3333 }
3334}
3335
df35e626 3336static bool
9a0bacfb
TV
3337dw2_expand_symtabs_matching_symbol
3338 (mapped_index_base &index,
3339 const lookup_name_info &lookup_name_in,
3340 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3341 gdb::function_view<bool (offset_type)> match_callback,
3342 dwarf2_per_objfile *per_objfile);
9a0bacfb 3343
df35e626 3344static bool
9a0bacfb 3345dw2_expand_symtabs_matching_one
97a1449a
SM
3346 (dwarf2_per_cu_data *per_cu,
3347 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3348 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3349 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3350
af82f89d 3351void
0b7b2c2a 3352dwarf2_gdb_index::expand_matching_symbols
199b4314 3353 (struct objfile *objfile,
b054970d 3354 const lookup_name_info &name, domain_enum domain,
199b4314 3355 int global,
199b4314 3356 symbol_compare_ftype *ordered_compare)
9291a0cd 3357{
1aa98955 3358 /* Used for Ada. */
976ca316 3359 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3360
9a0bacfb
TV
3361 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3362
976ca316 3363 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3364 {
976ca316 3365 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3366
9a0bacfb
TV
3367 const char *match_name = name.ada ().lookup_name ().c_str ();
3368 auto matcher = [&] (const char *symname)
3369 {
3370 if (ordered_compare == nullptr)
3371 return true;
3372 return ordered_compare (symname, match_name) == 0;
3373 };
3374
b64f703b 3375 dw2_expand_symtabs_matching_symbol (index, name, matcher,
9a0bacfb
TV
3376 [&] (offset_type namei)
3377 {
3378 struct dw2_symtab_iterator iter;
3379 struct dwarf2_per_cu_data *per_cu;
3380
976ca316 3381 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3382 namei);
9a0bacfb 3383 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3384 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3385 nullptr);
9a0bacfb 3386 return true;
976ca316 3387 }, per_objfile);
9a0bacfb
TV
3388 }
3389 else
3390 {
3391 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3392 proceed assuming all symtabs have been read in. */
3393 }
9291a0cd
TT
3394}
3395
e1ef7d7a
PA
3396/* Starting from a search name, return the string that finds the upper
3397 bound of all strings that start with SEARCH_NAME in a sorted name
3398 list. Returns the empty string to indicate that the upper bound is
3399 the end of the list. */
3400
3401static std::string
3402make_sort_after_prefix_name (const char *search_name)
3403{
3404 /* When looking to complete "func", we find the upper bound of all
3405 symbols that start with "func" by looking for where we'd insert
3406 the closest string that would follow "func" in lexicographical
3407 order. Usually, that's "func"-with-last-character-incremented,
3408 i.e. "fund". Mind non-ASCII characters, though. Usually those
3409 will be UTF-8 multi-byte sequences, but we can't be certain.
3410 Especially mind the 0xff character, which is a valid character in
3411 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3412 rule out compilers allowing it in identifiers. Note that
3413 conveniently, strcmp/strcasecmp are specified to compare
3414 characters interpreted as unsigned char. So what we do is treat
3415 the whole string as a base 256 number composed of a sequence of
3416 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3417 to 0, and carries 1 to the following more-significant position.
3418 If the very first character in SEARCH_NAME ends up incremented
3419 and carries/overflows, then the upper bound is the end of the
3420 list. The string after the empty string is also the empty
3421 string.
3422
3423 Some examples of this operation:
3424
3425 SEARCH_NAME => "+1" RESULT
3426
3427 "abc" => "abd"
3428 "ab\xff" => "ac"
3429 "\xff" "a" "\xff" => "\xff" "b"
3430 "\xff" => ""
3431 "\xff\xff" => ""
3432 "" => ""
3433
3434 Then, with these symbols for example:
3435
3436 func
3437 func1
3438 fund
3439
3440 completing "func" looks for symbols between "func" and
3441 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3442 which finds "func" and "func1", but not "fund".
3443
3444 And with:
3445
3446 funcÿ (Latin1 'ÿ' [0xff])
3447 funcÿ1
3448 fund
3449
3450 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3451 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3452
3453 And with:
3454
3455 ÿÿ (Latin1 'ÿ' [0xff])
3456 ÿÿ1
3457
3458 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3459 the end of the list.
3460 */
3461 std::string after = search_name;
3462 while (!after.empty () && (unsigned char) after.back () == 0xff)
3463 after.pop_back ();
3464 if (!after.empty ())
3465 after.back () = (unsigned char) after.back () + 1;
3466 return after;
3467}
3468
5c58de74 3469/* See declaration. */
61d96d7e 3470
5c58de74
PA
3471std::pair<std::vector<name_component>::const_iterator,
3472 std::vector<name_component>::const_iterator>
44ed8f3e 3473mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3474 (const lookup_name_info &lookup_name_without_params, language lang,
3475 dwarf2_per_objfile *per_objfile) const
3f563c84 3476{
5c58de74
PA
3477 auto *name_cmp
3478 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3479
3b00ef10 3480 const char *lang_name
e0802d59 3481 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3482
3f563c84
PA
3483 /* Comparison function object for lower_bound that matches against a
3484 given symbol name. */
3485 auto lookup_compare_lower = [&] (const name_component &elem,
3486 const char *name)
3487 {
fcf23d5b 3488 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3489 const char *elem_name = elem_qualified + elem.name_offset;
3490 return name_cmp (elem_name, name) < 0;
3491 };
3492
3493 /* Comparison function object for upper_bound that matches against a
3494 given symbol name. */
3495 auto lookup_compare_upper = [&] (const char *name,
3496 const name_component &elem)
3497 {
fcf23d5b 3498 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3499 const char *elem_name = elem_qualified + elem.name_offset;
3500 return name_cmp (name, elem_name) < 0;
3501 };
3502
5c58de74
PA
3503 auto begin = this->name_components.begin ();
3504 auto end = this->name_components.end ();
3f563c84
PA
3505
3506 /* Find the lower bound. */
3507 auto lower = [&] ()
3508 {
3b00ef10 3509 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3510 return begin;
3511 else
3b00ef10 3512 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3513 } ();
3514
3515 /* Find the upper bound. */
3516 auto upper = [&] ()
3517 {
5c58de74 3518 if (lookup_name_without_params.completion_mode ())
3f563c84 3519 {
e1ef7d7a
PA
3520 /* In completion mode, we want UPPER to point past all
3521 symbols names that have the same prefix. I.e., with
3522 these symbols, and completing "func":
3523
3524 function << lower bound
3525 function1
3526 other_function << upper bound
3527
3528 We find the upper bound by looking for the insertion
3529 point of "func"-with-last-character-incremented,
3530 i.e. "fund". */
3b00ef10 3531 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3532 if (after.empty ())
3f563c84 3533 return end;
e6b2f5ef
PA
3534 return std::lower_bound (lower, end, after.c_str (),
3535 lookup_compare_lower);
3f563c84
PA
3536 }
3537 else
3b00ef10 3538 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3539 } ();
3540
5c58de74
PA
3541 return {lower, upper};
3542}
3543
3544/* See declaration. */
3545
3546void
fcf23d5b 3547mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3548{
3549 if (!this->name_components.empty ())
3550 return;
3551
3552 this->name_components_casing = case_sensitivity;
3553 auto *name_cmp
3554 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3555
3556 /* The code below only knows how to break apart components of C++
3557 symbol names (and other languages that use '::' as
3b00ef10 3558 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3559 auto count = this->symbol_name_count ();
3560 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3561 {
44ed8f3e 3562 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3563 continue;
3564
fcf23d5b 3565 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3566
3567 /* Add each name component to the name component table. */
3568 unsigned int previous_len = 0;
3b00ef10
TT
3569
3570 if (strstr (name, "::") != nullptr)
3571 {
3572 for (unsigned int current_len = cp_find_first_component (name);
3573 name[current_len] != '\0';
3574 current_len += cp_find_first_component (name + current_len))
3575 {
3576 gdb_assert (name[current_len] == ':');
3577 this->name_components.push_back ({previous_len, idx});
3578 /* Skip the '::'. */
3579 current_len += 2;
3580 previous_len = current_len;
3581 }
3582 }
3583 else
5c58de74 3584 {
3b00ef10
TT
3585 /* Handle the Ada encoded (aka mangled) form here. */
3586 for (const char *iter = strstr (name, "__");
3587 iter != nullptr;
3588 iter = strstr (iter, "__"))
3589 {
3590 this->name_components.push_back ({previous_len, idx});
3591 iter += 2;
3592 previous_len = iter - name;
3593 }
5c58de74 3594 }
3b00ef10 3595
5c58de74
PA
3596 this->name_components.push_back ({previous_len, idx});
3597 }
3598
3599 /* Sort name_components elements by name. */
3600 auto name_comp_compare = [&] (const name_component &left,
3601 const name_component &right)
3602 {
fcf23d5b
SM
3603 const char *left_qualified
3604 = this->symbol_name_at (left.idx, per_objfile);
3605 const char *right_qualified
3606 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
3607
3608 const char *left_name = left_qualified + left.name_offset;
3609 const char *right_name = right_qualified + right.name_offset;
3610
3611 return name_cmp (left_name, right_name) < 0;
3612 };
3613
3614 std::sort (this->name_components.begin (),
3615 this->name_components.end (),
3616 name_comp_compare);
3617}
3618
3619/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3620 mapped_index_base instead of the containing objfile. This is split
3621 to a separate function in order to be able to unit test the
3622 name_components matching using a mock mapped_index_base. For each
5c58de74 3623 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3624 symbol's index in the mapped_index_base symbol table. */
5c58de74 3625
df35e626 3626static bool
5c58de74 3627dw2_expand_symtabs_matching_symbol
44ed8f3e 3628 (mapped_index_base &index,
5c58de74
PA
3629 const lookup_name_info &lookup_name_in,
3630 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3631 gdb::function_view<bool (offset_type)> match_callback,
3632 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3633{
3634 lookup_name_info lookup_name_without_params
3635 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3636
3637 /* Build the symbol name component sorted vector, if we haven't
3638 yet. */
fcf23d5b 3639 index.build_name_components (per_objfile);
5c58de74 3640
3f563c84
PA
3641 /* The same symbol may appear more than once in the range though.
3642 E.g., if we're looking for symbols that complete "w", and we have
3643 a symbol named "w1::w2", we'll find the two name components for
3644 that same symbol in the range. To be sure we only call the
3645 callback once per symbol, we first collect the symbol name
3646 indexes that matched in a temporary vector and ignore
3647 duplicates. */
3648 std::vector<offset_type> matches;
3f563c84 3649
3b00ef10
TT
3650 struct name_and_matcher
3651 {
3652 symbol_name_matcher_ftype *matcher;
ecc6c606 3653 const char *name;
3b00ef10
TT
3654
3655 bool operator== (const name_and_matcher &other) const
3f563c84 3656 {
ecc6c606 3657 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3658 }
3659 };
3660
3661 /* A vector holding all the different symbol name matchers, for all
3662 languages. */
3663 std::vector<name_and_matcher> matchers;
3664
3665 for (int i = 0; i < nr_languages; i++)
3666 {
3667 enum language lang_e = (enum language) i;
3668
3669 const language_defn *lang = language_def (lang_e);
3670 symbol_name_matcher_ftype *name_matcher
c9debfb9 3671 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 3672
3b00ef10 3673 name_and_matcher key {
dda83cd7 3674 name_matcher,
3b00ef10
TT
3675 lookup_name_without_params.language_lookup_name (lang_e)
3676 };
3677
3678 /* Don't insert the same comparison routine more than once.
3679 Note that we do this linear walk. This is not a problem in
3680 practice because the number of supported languages is
3681 low. */
3682 if (std::find (matchers.begin (), matchers.end (), key)
3683 != matchers.end ())
9291a0cd 3684 continue;
3b00ef10
TT
3685 matchers.push_back (std::move (key));
3686
3687 auto bounds
3688 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 3689 lang_e, per_objfile);
3b00ef10
TT
3690
3691 /* Now for each symbol name in range, check to see if we have a name
3692 match, and if so, call the MATCH_CALLBACK callback. */
3693
3694 for (; bounds.first != bounds.second; ++bounds.first)
3695 {
fcf23d5b
SM
3696 const char *qualified
3697 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
3698
3699 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3700 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3701 continue;
9291a0cd 3702
3b00ef10
TT
3703 matches.push_back (bounds.first->idx);
3704 }
3f563c84
PA
3705 }
3706
3707 std::sort (matches.begin (), matches.end ());
3708
3709 /* Finally call the callback, once per match. */
3710 ULONGEST prev = -1;
df35e626 3711 bool result = true;
3f563c84
PA
3712 for (offset_type idx : matches)
3713 {
3714 if (prev != idx)
3715 {
3b00ef10 3716 if (!match_callback (idx))
df35e626
TT
3717 {
3718 result = false;
3719 break;
3720 }
3f563c84
PA
3721 prev = idx;
3722 }
3723 }
3724
3725 /* Above we use a type wider than idx's for 'prev', since 0 and
3726 (offset_type)-1 are both possible values. */
3727 static_assert (sizeof (prev) > sizeof (offset_type), "");
df35e626
TT
3728
3729 return result;
3f563c84
PA
3730}
3731
c62446b1
PA
3732#if GDB_SELF_TEST
3733
3734namespace selftests { namespace dw2_expand_symtabs_matching {
3735
a3c5fafd
PA
3736/* A mock .gdb_index/.debug_names-like name index table, enough to
3737 exercise dw2_expand_symtabs_matching_symbol, which works with the
3738 mapped_index_base interface. Builds an index from the symbol list
3739 passed as parameter to the constructor. */
3740class mock_mapped_index : public mapped_index_base
c62446b1
PA
3741{
3742public:
a3c5fafd
PA
3743 mock_mapped_index (gdb::array_view<const char *> symbols)
3744 : m_symbol_table (symbols)
c62446b1
PA
3745 {}
3746
a3c5fafd 3747 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3748
a3c5fafd 3749 /* Return the number of names in the symbol table. */
632e107b 3750 size_t symbol_name_count () const override
c62446b1 3751 {
a3c5fafd 3752 return m_symbol_table.size ();
c62446b1
PA
3753 }
3754
a3c5fafd 3755 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
3756 const char *symbol_name_at
3757 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
3758 {
3759 return m_symbol_table[idx];
3760 }
c62446b1 3761
a3c5fafd
PA
3762private:
3763 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
3764};
3765
3766/* Convenience function that converts a NULL pointer to a "<null>"
3767 string, to pass to print routines. */
3768
3769static const char *
3770string_or_null (const char *str)
3771{
3772 return str != NULL ? str : "<null>";
3773}
3774
3775/* Check if a lookup_name_info built from
3776 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3777 index. EXPECTED_LIST is the list of expected matches, in expected
3778 matching order. If no match expected, then an empty list is
3779 specified. Returns true on success. On failure prints a warning
3780 indicating the file:line that failed, and returns false. */
3781
3782static bool
3783check_match (const char *file, int line,
3784 mock_mapped_index &mock_index,
3785 const char *name, symbol_name_match_type match_type,
3786 bool completion_mode,
fcf23d5b
SM
3787 std::initializer_list<const char *> expected_list,
3788 dwarf2_per_objfile *per_objfile)
c62446b1
PA
3789{
3790 lookup_name_info lookup_name (name, match_type, completion_mode);
3791
3792 bool matched = true;
3793
3794 auto mismatch = [&] (const char *expected_str,
3795 const char *got)
3796 {
3797 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3798 "expected=\"%s\", got=\"%s\"\n"),
3799 file, line,
3800 (match_type == symbol_name_match_type::FULL
3801 ? "FULL" : "WILD"),
3802 name, string_or_null (expected_str), string_or_null (got));
3803 matched = false;
3804 };
3805
3806 auto expected_it = expected_list.begin ();
3807 auto expected_end = expected_list.end ();
3808
a3c5fafd 3809 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
b64f703b 3810 nullptr,
c62446b1
PA
3811 [&] (offset_type idx)
3812 {
fcf23d5b 3813 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
3814 const char *expected_str
3815 = expected_it == expected_end ? NULL : *expected_it++;
3816
3817 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3818 mismatch (expected_str, matched_name);
3b00ef10 3819 return true;
fcf23d5b 3820 }, per_objfile);
c62446b1
PA
3821
3822 const char *expected_str
3823 = expected_it == expected_end ? NULL : *expected_it++;
3824 if (expected_str != NULL)
3825 mismatch (expected_str, NULL);
3826
3827 return matched;
3828}
3829
3830/* The symbols added to the mock mapped_index for testing (in
3831 canonical form). */
3832static const char *test_symbols[] = {
3833 "function",
3834 "std::bar",
3835 "std::zfunction",
3836 "std::zfunction2",
3837 "w1::w2",
3838 "ns::foo<char*>",
3839 "ns::foo<int>",
3840 "ns::foo<long>",
a20714ff
PA
3841 "ns2::tmpl<int>::foo2",
3842 "(anonymous namespace)::A::B::C",
c62446b1 3843
e1ef7d7a
PA
3844 /* These are used to check that the increment-last-char in the
3845 matching algorithm for completion doesn't match "t1_fund" when
3846 completing "t1_func". */
3847 "t1_func",
3848 "t1_func1",
3849 "t1_fund",
3850 "t1_fund1",
3851
3852 /* A UTF-8 name with multi-byte sequences to make sure that
3853 cp-name-parser understands this as a single identifier ("função"
3854 is "function" in PT). */
3855 u8"u8função",
3856
3857 /* \377 (0xff) is Latin1 'ÿ'. */
3858 "yfunc\377",
3859
3860 /* \377 (0xff) is Latin1 'ÿ'. */
3861 "\377",
3862 "\377\377123",
3863
c62446b1
PA
3864 /* A name with all sorts of complications. Starts with "z" to make
3865 it easier for the completion tests below. */
3866#define Z_SYM_NAME \
3867 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3868 "::tuple<(anonymous namespace)::ui*, " \
3869 "std::default_delete<(anonymous namespace)::ui>, void>"
3870
3871 Z_SYM_NAME
3872};
3873
a3c5fafd
PA
3874/* Returns true if the mapped_index_base::find_name_component_bounds
3875 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3876 in completion mode. */
5c58de74
PA
3877
3878static bool
a3c5fafd 3879check_find_bounds_finds (mapped_index_base &index,
5c58de74 3880 const char *search_name,
fcf23d5b
SM
3881 gdb::array_view<const char *> expected_syms,
3882 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3883{
3884 lookup_name_info lookup_name (search_name,
3885 symbol_name_match_type::FULL, true);
3886
3b00ef10 3887 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
3888 language_cplus,
3889 per_objfile);
5c58de74
PA
3890
3891 size_t distance = std::distance (bounds.first, bounds.second);
3892 if (distance != expected_syms.size ())
3893 return false;
3894
3895 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3896 {
3897 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 3898 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
3899 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3900 return false;
3901 }
3902
3903 return true;
3904}
3905
3906/* Test the lower-level mapped_index::find_name_component_bounds
3907 method. */
3908
c62446b1 3909static void
5c58de74
PA
3910test_mapped_index_find_name_component_bounds ()
3911{
3912 mock_mapped_index mock_index (test_symbols);
3913
fcf23d5b 3914 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
3915
3916 /* Test the lower-level mapped_index::find_name_component_bounds
3917 method in completion mode. */
3918 {
3919 static const char *expected_syms[] = {
3920 "t1_func",
3921 "t1_func1",
5c58de74
PA
3922 };
3923
fcf23d5b
SM
3924 SELF_CHECK (check_find_bounds_finds
3925 (mock_index, "t1_func", expected_syms,
3926 NULL /* per_objfile */));
5c58de74
PA
3927 }
3928
3929 /* Check that the increment-last-char in the name matching algorithm
3930 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3931 {
3932 static const char *expected_syms1[] = {
3933 "\377",
3934 "\377\377123",
3935 };
fcf23d5b
SM
3936 SELF_CHECK (check_find_bounds_finds
3937 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
3938
3939 static const char *expected_syms2[] = {
3940 "\377\377123",
3941 };
fcf23d5b
SM
3942 SELF_CHECK (check_find_bounds_finds
3943 (mock_index, "\377\377", expected_syms2,
3944 NULL /* per_objfile */));
5c58de74
PA
3945 }
3946}
3947
3948/* Test dw2_expand_symtabs_matching_symbol. */
3949
3950static void
3951test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
3952{
3953 mock_mapped_index mock_index (test_symbols);
3954
3955 /* We let all tests run until the end even if some fails, for debug
3956 convenience. */
3957 bool any_mismatch = false;
3958
3959 /* Create the expected symbols list (an initializer_list). Needed
3960 because lists have commas, and we need to pass them to CHECK,
3961 which is a macro. */
3962#define EXPECT(...) { __VA_ARGS__ }
3963
3964 /* Wrapper for check_match that passes down the current
3965 __FILE__/__LINE__. */
3966#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3967 any_mismatch |= !check_match (__FILE__, __LINE__, \
3968 mock_index, \
3969 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 3970 EXPECTED_LIST, NULL)
c62446b1
PA
3971
3972 /* Identity checks. */
3973 for (const char *sym : test_symbols)
3974 {
3975 /* Should be able to match all existing symbols. */
3976 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3977 EXPECT (sym));
3978
3979 /* Should be able to match all existing symbols with
3980 parameters. */
3981 std::string with_params = std::string (sym) + "(int)";
3982 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3983 EXPECT (sym));
3984
3985 /* Should be able to match all existing symbols with
3986 parameters and qualifiers. */
3987 with_params = std::string (sym) + " ( int ) const";
3988 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3989 EXPECT (sym));
3990
3991 /* This should really find sym, but cp-name-parser.y doesn't
3992 know about lvalue/rvalue qualifiers yet. */
3993 with_params = std::string (sym) + " ( int ) &&";
3994 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3995 {});
3996 }
3997
e1ef7d7a
PA
3998 /* Check that the name matching algorithm for completion doesn't get
3999 confused with Latin1 'ÿ' / 0xff. */
4000 {
4001 static const char str[] = "\377";
4002 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4003 EXPECT ("\377", "\377\377123"));
4004 }
4005
4006 /* Check that the increment-last-char in the matching algorithm for
4007 completion doesn't match "t1_fund" when completing "t1_func". */
4008 {
4009 static const char str[] = "t1_func";
4010 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4011 EXPECT ("t1_func", "t1_func1"));
4012 }
4013
c62446b1
PA
4014 /* Check that completion mode works at each prefix of the expected
4015 symbol name. */
4016 {
4017 static const char str[] = "function(int)";
4018 size_t len = strlen (str);
4019 std::string lookup;
4020
4021 for (size_t i = 1; i < len; i++)
4022 {
4023 lookup.assign (str, i);
4024 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4025 EXPECT ("function"));
4026 }
4027 }
4028
4029 /* While "w" is a prefix of both components, the match function
4030 should still only be called once. */
4031 {
4032 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4033 EXPECT ("w1::w2"));
a20714ff
PA
4034 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4035 EXPECT ("w1::w2"));
c62446b1
PA
4036 }
4037
4038 /* Same, with a "complicated" symbol. */
4039 {
4040 static const char str[] = Z_SYM_NAME;
4041 size_t len = strlen (str);
4042 std::string lookup;
4043
4044 for (size_t i = 1; i < len; i++)
4045 {
4046 lookup.assign (str, i);
4047 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4048 EXPECT (Z_SYM_NAME));
4049 }
4050 }
4051
4052 /* In FULL mode, an incomplete symbol doesn't match. */
4053 {
4054 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4055 {});
4056 }
4057
4058 /* A complete symbol with parameters matches any overload, since the
4059 index has no overload info. */
4060 {
4061 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4062 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4063 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4064 EXPECT ("std::zfunction", "std::zfunction2"));
4065 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4066 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4067 }
4068
4069 /* Check that whitespace is ignored appropriately. A symbol with a
4070 template argument list. */
4071 {
4072 static const char expected[] = "ns::foo<int>";
4073 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4074 EXPECT (expected));
a20714ff
PA
4075 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4076 EXPECT (expected));
c62446b1
PA
4077 }
4078
4079 /* Check that whitespace is ignored appropriately. A symbol with a
4080 template argument list that includes a pointer. */
4081 {
4082 static const char expected[] = "ns::foo<char*>";
4083 /* Try both completion and non-completion modes. */
4084 static const bool completion_mode[2] = {false, true};
4085 for (size_t i = 0; i < 2; i++)
4086 {
4087 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4088 completion_mode[i], EXPECT (expected));
a20714ff
PA
4089 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4090 completion_mode[i], EXPECT (expected));
c62446b1
PA
4091
4092 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4093 completion_mode[i], EXPECT (expected));
a20714ff
PA
4094 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4095 completion_mode[i], EXPECT (expected));
c62446b1
PA
4096 }
4097 }
4098
4099 {
4100 /* Check method qualifiers are ignored. */
4101 static const char expected[] = "ns::foo<char*>";
4102 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4103 symbol_name_match_type::FULL, true, EXPECT (expected));
4104 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4105 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4106 CHECK_MATCH ("foo < char * > ( int ) const",
4107 symbol_name_match_type::WILD, true, EXPECT (expected));
4108 CHECK_MATCH ("foo < char * > ( int ) &&",
4109 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4110 }
4111
4112 /* Test lookup names that don't match anything. */
4113 {
a20714ff
PA
4114 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4115 {});
4116
c62446b1
PA
4117 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4118 {});
4119 }
4120
a20714ff
PA
4121 /* Some wild matching tests, exercising "(anonymous namespace)",
4122 which should not be confused with a parameter list. */
4123 {
4124 static const char *syms[] = {
4125 "A::B::C",
4126 "B::C",
4127 "C",
4128 "A :: B :: C ( int )",
4129 "B :: C ( int )",
4130 "C ( int )",
4131 };
4132
4133 for (const char *s : syms)
4134 {
4135 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4136 EXPECT ("(anonymous namespace)::A::B::C"));
4137 }
4138 }
4139
4140 {
4141 static const char expected[] = "ns2::tmpl<int>::foo2";
4142 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4143 EXPECT (expected));
4144 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4145 EXPECT (expected));
4146 }
4147
c62446b1
PA
4148 SELF_CHECK (!any_mismatch);
4149
4150#undef EXPECT
4151#undef CHECK_MATCH
4152}
4153
5c58de74
PA
4154static void
4155run_test ()
4156{
4157 test_mapped_index_find_name_component_bounds ();
4158 test_dw2_expand_symtabs_matching_symbol ();
4159}
4160
c62446b1
PA
4161}} // namespace selftests::dw2_expand_symtabs_matching
4162
4163#endif /* GDB_SELF_TEST */
4164
4b514bc8
JK
4165/* If FILE_MATCHER is NULL or if PER_CU has
4166 dwarf2_per_cu_quick_data::MARK set (see
4167 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4168 EXPANSION_NOTIFY on it. */
4169
df35e626 4170static bool
4b514bc8 4171dw2_expand_symtabs_matching_one
97a1449a
SM
4172 (dwarf2_per_cu_data *per_cu,
4173 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4174 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4175 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4176{
4177 if (file_matcher == NULL || per_cu->v.quick->mark)
4178 {
af758d11 4179 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4180
97a1449a
SM
4181 compunit_symtab *symtab
4182 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4183 gdb_assert (symtab != nullptr);
4b514bc8 4184
af758d11 4185 if (expansion_notify != NULL && symtab_was_null)
df35e626 4186 return expansion_notify (symtab);
4b514bc8 4187 }
df35e626 4188 return true;
4b514bc8
JK
4189}
4190
3f563c84
PA
4191/* Helper for dw2_expand_matching symtabs. Called on each symbol
4192 matched, to expand corresponding CUs that were marked. IDX is the
4193 index of the symbol name that matched. */
4194
df35e626 4195static bool
3f563c84 4196dw2_expand_marked_cus
976ca316 4197 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4198 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4199 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4200 block_search_flags search_flags,
3f563c84
PA
4201 search_domain kind)
4202{
42c2c694 4203 offset_type vec_len, vec_idx;
3f563c84 4204 bool global_seen = false;
976ca316 4205 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4206
42c2c694
TT
4207 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4208 vec_len = vec[0];
61920122
PA
4209 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4210 {
42c2c694 4211 offset_type cu_index_and_attrs = vec[vec_idx + 1];
61920122
PA
4212 /* This value is only valid for index versions >= 7. */
4213 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4214 gdb_index_symbol_kind symbol_kind =
4215 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4216 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4217 /* Only check the symbol attributes if they're present.
4218 Indices prior to version 7 don't record them,
4219 and indices >= 7 may elide them for certain symbols
4220 (gold does this). */
4221 int attrs_valid =
4222 (index.version >= 7
4223 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4224
4225 /* Work around gold/15646. */
f030440d
TV
4226 if (attrs_valid
4227 && !is_static
4228 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4229 {
f030440d 4230 if (global_seen)
61920122 4231 continue;
f030440d
TV
4232
4233 global_seen = true;
61920122 4234 }
3190f0c6 4235
61920122
PA
4236 /* Only check the symbol's kind if it has one. */
4237 if (attrs_valid)
4238 {
03a8ea51
TT
4239 if (is_static)
4240 {
4241 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4242 continue;
4243 }
4244 else
4245 {
4246 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4247 continue;
4248 }
4249
61920122 4250 switch (kind)
8943b874 4251 {
61920122
PA
4252 case VARIABLES_DOMAIN:
4253 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4254 continue;
4255 break;
4256 case FUNCTIONS_DOMAIN:
4257 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4258 continue;
61920122
PA
4259 break;
4260 case TYPES_DOMAIN:
4261 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4262 continue;
4263 break;
59c35742
AB
4264 case MODULES_DOMAIN:
4265 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4266 continue;
4267 break;
61920122
PA
4268 default:
4269 break;
8943b874 4270 }
61920122 4271 }
8943b874 4272
61920122 4273 /* Don't crash on bad data. */
91eea9cc 4274 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
61920122 4275 {
b98664d3 4276 complaint (_(".gdb_index entry has bad CU index"
976ca316 4277 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4278 continue;
4279 }
4280
91eea9cc 4281 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
df35e626
TT
4282 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4283 expansion_notify))
4284 return false;
61920122 4285 }
df35e626
TT
4286
4287 return true;
61920122
PA
4288}
4289
4b514bc8
JK
4290/* If FILE_MATCHER is non-NULL, set all the
4291 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4292 that match FILE_MATCHER. */
4293
61920122 4294static void
4b514bc8 4295dw_expand_symtabs_matching_file_matcher
976ca316 4296 (dwarf2_per_objfile *per_objfile,
ed2dc618 4297 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4298{
4b514bc8 4299 if (file_matcher == NULL)
61920122
PA
4300 return;
4301
4b514bc8
JK
4302 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4303 htab_eq_pointer,
4304 NULL, xcalloc, xfree));
4305 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4306 htab_eq_pointer,
4307 NULL, xcalloc, xfree));
61920122 4308
4b514bc8
JK
4309 /* The rule is CUs specify all the files, including those used by
4310 any TU, so there's no need to scan TUs here. */
61920122 4311
0d305d5c 4312 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4313 {
927aa2e7
JK
4314 QUIT;
4315
91eea9cc
TT
4316 if (per_cu->is_debug_types)
4317 continue;
927aa2e7
JK
4318 per_cu->v.quick->mark = 0;
4319
4320 /* We only need to look at symtabs not already expanded. */
0d305d5c 4321 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7
JK
4322 continue;
4323
0d305d5c
TT
4324 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4325 per_objfile);
927aa2e7
JK
4326 if (file_data == NULL)
4327 continue;
4328
4329 if (htab_find (visited_not_found.get (), file_data) != NULL)
4330 continue;
4331 else if (htab_find (visited_found.get (), file_data) != NULL)
4332 {
4333 per_cu->v.quick->mark = 1;
4334 continue;
4335 }
4336
b76e467d 4337 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4338 {
4339 const char *this_real_name;
4340
4341 if (file_matcher (file_data->file_names[j], false))
4342 {
4343 per_cu->v.quick->mark = 1;
4344 break;
4345 }
4346
4347 /* Before we invoke realpath, which can get expensive when many
4348 files are involved, do a quick comparison of the basenames. */
4349 if (!basenames_may_differ
4350 && !file_matcher (lbasename (file_data->file_names[j]),
4351 true))
4352 continue;
4353
976ca316 4354 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4355 if (file_matcher (this_real_name, false))
4356 {
4357 per_cu->v.quick->mark = 1;
4358 break;
4359 }
4360 }
4361
b76e467d
SM
4362 void **slot = htab_find_slot (per_cu->v.quick->mark
4363 ? visited_found.get ()
4364 : visited_not_found.get (),
4365 file_data, INSERT);
927aa2e7
JK
4366 *slot = file_data;
4367 }
4368}
4369
df35e626 4370bool
af82f89d
TT
4371dwarf2_gdb_index::expand_symtabs_matching
4372 (struct objfile *objfile,
4373 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4374 const lookup_name_info *lookup_name,
4375 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4376 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4377 block_search_flags search_flags,
3bfa51a7 4378 domain_enum domain,
af82f89d 4379 enum search_domain kind)
927aa2e7 4380{
976ca316 4381 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4382
4383 /* index_table is NULL if OBJF_READNOW. */
976ca316 4384 if (!per_objfile->per_bfd->index_table)
df35e626 4385 return true;
927aa2e7 4386
976ca316 4387 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4388
c1a66c06
TV
4389 if (symbol_matcher == NULL && lookup_name == NULL)
4390 {
de2143d6
TT
4391 for (dwarf2_per_cu_data *per_cu
4392 : all_comp_units_range (per_objfile->per_bfd))
c1a66c06
TV
4393 {
4394 QUIT;
4395
de2143d6 4396 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
df35e626
TT
4397 file_matcher,
4398 expansion_notify))
4399 return false;
c1a66c06 4400 }
df35e626 4401 return true;
c1a66c06
TV
4402 }
4403
976ca316 4404 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4405
df35e626
TT
4406 bool result
4407 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4408 symbol_matcher,
4409 [&] (offset_type idx)
927aa2e7 4410 {
df35e626 4411 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
03a8ea51 4412 expansion_notify, search_flags, kind))
df35e626 4413 return false;
3b00ef10 4414 return true;
976ca316 4415 }, per_objfile);
df35e626
TT
4416
4417 return result;
927aa2e7
JK
4418}
4419
4420/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4421 symtab. */
4422
4423static struct compunit_symtab *
4424recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4425 CORE_ADDR pc)
4426{
4427 int i;
4428
4429 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4430 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4431 return cust;
4432
4433 if (cust->includes == NULL)
4434 return NULL;
4435
4436 for (i = 0; cust->includes[i]; ++i)
4437 {
4438 struct compunit_symtab *s = cust->includes[i];
4439
4440 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4441 if (s != NULL)
4442 return s;
4443 }
4444
4445 return NULL;
4446}
4447
39298a5d
TT
4448struct compunit_symtab *
4449dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4450 (struct objfile *objfile,
4451 struct bound_minimal_symbol msymbol,
4452 CORE_ADDR pc,
4453 struct obj_section *section,
4454 int warn_if_readin)
927aa2e7
JK
4455{
4456 struct dwarf2_per_cu_data *data;
4457 struct compunit_symtab *result;
4458
efd7398e
TT
4459 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4460 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4461 return NULL;
4462
b3b3bada 4463 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4464 data = ((struct dwarf2_per_cu_data *)
4465 addrmap_find (per_objfile->per_bfd->index_addrmap,
4466 pc - baseaddr));
927aa2e7
JK
4467 if (!data)
4468 return NULL;
4469
af758d11 4470 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4471 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4472 paddress (objfile->arch (), pc));
927aa2e7 4473
97a1449a
SM
4474 result = recursively_find_pc_sect_compunit_symtab
4475 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4476
927aa2e7
JK
4477 gdb_assert (result != NULL);
4478 return result;
4479}
4480
39298a5d 4481void
f4655dee
TT
4482dwarf2_base_index_functions::map_symbol_filenames
4483 (struct objfile *objfile,
4484 gdb::function_view<symbol_filename_ftype> fun,
4485 bool need_fullname)
927aa2e7 4486{
976ca316 4487 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4488
9fc29955
AB
4489 /* Use caches to ensure we only call FUN once for each filename. */
4490 filename_seen_cache filenames_cache;
4491 std::unordered_set<quick_file_names *> qfn_cache;
927aa2e7 4492
9fc29955
AB
4493 /* The rule is CUs specify all the files, including those used by any TU,
4494 so there's no need to scan TUs here. We can ignore file names coming
4495 from already-expanded CUs. It is possible that an expanded CU might
4496 reuse the file names data from a currently unexpanded CU, in this
4497 case we don't want to report the files from the unexpanded CU. */
927aa2e7 4498
0d305d5c 4499 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955 4500 {
0d305d5c 4501 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7 4502 {
9fc29955
AB
4503 if (per_cu->v.quick->file_names != nullptr)
4504 qfn_cache.insert (per_cu->v.quick->file_names);
927aa2e7 4505 }
9fc29955 4506 }
927aa2e7 4507
de2143d6
TT
4508 for (dwarf2_per_cu_data *per_cu
4509 : all_comp_units_range (per_objfile->per_bfd))
9fc29955
AB
4510 {
4511 /* We only need to look at symtabs not already expanded. */
de2143d6 4512 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
9fc29955 4513 continue;
927aa2e7 4514
de2143d6 4515 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
9fc29955
AB
4516 if (file_data == nullptr
4517 || qfn_cache.find (file_data) != qfn_cache.end ())
4518 continue;
927aa2e7 4519
9fc29955
AB
4520 for (int j = 0; j < file_data->num_file_names; ++j)
4521 {
4522 const char *filename = file_data->file_names[j];
4523 filenames_cache.seen (filename);
927aa2e7
JK
4524 }
4525 }
4526
9fc29955 4527 filenames_cache.traverse ([&] (const char *filename)
927aa2e7
JK
4528 {
4529 gdb::unique_xmalloc_ptr<char> this_real_name;
4530
4531 if (need_fullname)
4532 this_real_name = gdb_realpath (filename);
f4655dee 4533 fun (filename, this_real_name.get ());
927aa2e7
JK
4534 });
4535}
4536
39298a5d
TT
4537bool
4538dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 4539{
fae2120b 4540 return true;
927aa2e7
JK
4541}
4542
fc4d5ebf
AB
4543/* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4544
4545bool
4546dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4547{
4548 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4549
4550 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4551 {
4552 /* Is this already expanded? */
4553 if (per_objfile->symtab_set_p (per_cu.get ()))
4554 continue;
4555
4556 /* It has not yet been expanded. */
4557 return true;
4558 }
4559
4560 return false;
4561}
4562
927aa2e7
JK
4563/* DWARF-5 debug_names reader. */
4564
4565/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4566static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4567
4568/* A helper function that reads the .debug_names section in SECTION
4569 and fills in MAP. FILENAME is the name of the file containing the
4570 section; it is used for error reporting.
4571
4572 Returns true if all went well, false otherwise. */
4573
4574static bool
4575read_debug_names_from_section (struct objfile *objfile,
4576 const char *filename,
4577 struct dwarf2_section_info *section,
4578 mapped_debug_names &map)
4579{
96b79293 4580 if (section->empty ())
927aa2e7
JK
4581 return false;
4582
4583 /* Older elfutils strip versions could keep the section in the main
4584 executable while splitting it for the separate debug info file. */
96b79293 4585 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4586 return false;
4587
96b79293 4588 section->read (objfile);
927aa2e7 4589
08feed99 4590 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4591
4592 const gdb_byte *addr = section->buffer;
4593
96b79293 4594 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4595
4596 unsigned int bytes_read;
4597 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4598 addr += bytes_read;
4599
4600 map.dwarf5_is_dwarf64 = bytes_read != 4;
4601 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4602 if (bytes_read + length != section->size)
4603 {
4604 /* There may be multiple per-CU indices. */
4605 warning (_("Section .debug_names in %s length %s does not match "
4606 "section length %s, ignoring .debug_names."),
4607 filename, plongest (bytes_read + length),
4608 pulongest (section->size));
4609 return false;
4610 }
4611
4612 /* The version number. */
4613 uint16_t version = read_2_bytes (abfd, addr);
4614 addr += 2;
4615 if (version != 5)
4616 {
4617 warning (_("Section .debug_names in %s has unsupported version %d, "
4618 "ignoring .debug_names."),
4619 filename, version);
4620 return false;
4621 }
4622
4623 /* Padding. */
4624 uint16_t padding = read_2_bytes (abfd, addr);
4625 addr += 2;
4626 if (padding != 0)
4627 {
4628 warning (_("Section .debug_names in %s has unsupported padding %d, "
4629 "ignoring .debug_names."),
4630 filename, padding);
4631 return false;
4632 }
4633
4634 /* comp_unit_count - The number of CUs in the CU list. */
4635 map.cu_count = read_4_bytes (abfd, addr);
4636 addr += 4;
4637
4638 /* local_type_unit_count - The number of TUs in the local TU
4639 list. */
4640 map.tu_count = read_4_bytes (abfd, addr);
4641 addr += 4;
4642
4643 /* foreign_type_unit_count - The number of TUs in the foreign TU
4644 list. */
4645 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4646 addr += 4;
4647 if (foreign_tu_count != 0)
4648 {
4649 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4650 "ignoring .debug_names."),
4651 filename, static_cast<unsigned long> (foreign_tu_count));
4652 return false;
4653 }
4654
4655 /* bucket_count - The number of hash buckets in the hash lookup
4656 table. */
4657 map.bucket_count = read_4_bytes (abfd, addr);
4658 addr += 4;
4659
4660 /* name_count - The number of unique names in the index. */
4661 map.name_count = read_4_bytes (abfd, addr);
4662 addr += 4;
4663
4664 /* abbrev_table_size - The size in bytes of the abbreviations
4665 table. */
4666 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4667 addr += 4;
4668
4669 /* augmentation_string_size - The size in bytes of the augmentation
4670 string. This value is rounded up to a multiple of 4. */
4671 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4672 addr += 4;
4673 map.augmentation_is_gdb = ((augmentation_string_size
4674 == sizeof (dwarf5_augmentation))
4675 && memcmp (addr, dwarf5_augmentation,
4676 sizeof (dwarf5_augmentation)) == 0);
4677 augmentation_string_size += (-augmentation_string_size) & 3;
4678 addr += augmentation_string_size;
4679
4680 /* List of CUs */
4681 map.cu_table_reordered = addr;
4682 addr += map.cu_count * map.offset_size;
4683
4684 /* List of Local TUs */
4685 map.tu_table_reordered = addr;
4686 addr += map.tu_count * map.offset_size;
4687
4688 /* Hash Lookup Table */
4689 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4690 addr += map.bucket_count * 4;
4691 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4692 addr += map.name_count * 4;
4693
4694 /* Name Table */
4695 map.name_table_string_offs_reordered = addr;
4696 addr += map.name_count * map.offset_size;
4697 map.name_table_entry_offs_reordered = addr;
4698 addr += map.name_count * map.offset_size;
4699
4700 const gdb_byte *abbrev_table_start = addr;
4701 for (;;)
4702 {
927aa2e7
JK
4703 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4704 addr += bytes_read;
4705 if (index_num == 0)
4706 break;
4707
4708 const auto insertpair
4709 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4710 if (!insertpair.second)
4711 {
4712 warning (_("Section .debug_names in %s has duplicate index %s, "
4713 "ignoring .debug_names."),
4714 filename, pulongest (index_num));
4715 return false;
4716 }
4717 mapped_debug_names::index_val &indexval = insertpair.first->second;
4718 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4719 addr += bytes_read;
4720
4721 for (;;)
4722 {
4723 mapped_debug_names::index_val::attr attr;
4724 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4725 addr += bytes_read;
4726 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4727 addr += bytes_read;
4728 if (attr.form == DW_FORM_implicit_const)
4729 {
4730 attr.implicit_const = read_signed_leb128 (abfd, addr,
4731 &bytes_read);
4732 addr += bytes_read;
4733 }
4734 if (attr.dw_idx == 0 && attr.form == 0)
4735 break;
4736 indexval.attr_vec.push_back (std::move (attr));
4737 }
4738 }
4739 if (addr != abbrev_table_start + abbrev_table_size)
4740 {
4741 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4742 "of size %s vs. written as %u, ignoring .debug_names."),
4743 filename, plongest (addr - abbrev_table_start),
4744 abbrev_table_size);
927aa2e7
JK
4745 return false;
4746 }
4747 map.entry_pool = addr;
4748
4749 return true;
4750}
4751
4752/* A helper for create_cus_from_debug_names that handles the MAP's CU
4753 list. */
4754
4755static void
168c9250 4756create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4757 const mapped_debug_names &map,
4758 dwarf2_section_info &section,
b76e467d 4759 bool is_dwz)
927aa2e7 4760{
3ee6bb11
TV
4761 if (!map.augmentation_is_gdb)
4762 {
fc9a13fb
TV
4763 for (uint32_t i = 0; i < map.cu_count; ++i)
4764 {
4765 sect_offset sect_off
4766 = (sect_offset) (extract_unsigned_integer
4767 (map.cu_table_reordered + i * map.offset_size,
4768 map.offset_size,
4769 map.dwarf5_byte_order));
4770 /* We don't know the length of the CU, because the CU list in a
4771 .debug_names index can be incomplete, so we can't use the start
4772 of the next CU as end of this CU. We create the CUs here with
4773 length 0, and in cutu_reader::cutu_reader we'll fill in the
4774 actual length. */
473ab964 4775 dwarf2_per_cu_data_up per_cu
fc9a13fb
TV
4776 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4777 sect_off, 0);
0d305d5c 4778 per_bfd->all_comp_units.push_back (std::move (per_cu));
fc9a13fb 4779 }
d3b54e63 4780 return;
3ee6bb11
TV
4781 }
4782
927aa2e7
JK
4783 sect_offset sect_off_prev;
4784 for (uint32_t i = 0; i <= map.cu_count; ++i)
4785 {
4786 sect_offset sect_off_next;
4787 if (i < map.cu_count)
4788 {
4789 sect_off_next
4790 = (sect_offset) (extract_unsigned_integer
4791 (map.cu_table_reordered + i * map.offset_size,
4792 map.offset_size,
4793 map.dwarf5_byte_order));
4794 }
4795 else
4796 sect_off_next = (sect_offset) section.size;
4797 if (i >= 1)
4798 {
4799 const ULONGEST length = sect_off_next - sect_off_prev;
473ab964 4800 dwarf2_per_cu_data_up per_cu
168c9250 4801 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 4802 sect_off_prev, length);
0d305d5c 4803 per_bfd->all_comp_units.push_back (std::move (per_cu));
927aa2e7
JK
4804 }
4805 sect_off_prev = sect_off_next;
4806 }
4807}
4808
4809/* Read the CU list from the mapped index, and use it to create all
ed2dc618 4810 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
4811
4812static void
168c9250 4813create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4814 const mapped_debug_names &map,
4815 const mapped_debug_names &dwz_map)
4816{
168c9250
SM
4817 gdb_assert (per_bfd->all_comp_units.empty ());
4818 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 4819
168c9250 4820 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 4821 false /* is_dwz */);
927aa2e7
JK
4822
4823 if (dwz_map.cu_count == 0)
4824 return;
4825
168c9250
SM
4826 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4827 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 4828 true /* is_dwz */);
927aa2e7
JK
4829}
4830
4831/* Read .debug_names. If everything went ok, initialize the "quick"
4832 elements of all the CUs and return true. Otherwise, return false. */
4833
4834static bool
976ca316 4835dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 4836{
fcf23d5b
SM
4837 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4838 mapped_debug_names dwz_map;
976ca316 4839 struct objfile *objfile = per_objfile->objfile;
f8c41851 4840 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
4841
4842 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
bab287cd 4843 &per_bfd->debug_names, *map))
927aa2e7
JK
4844 return false;
4845
4846 /* Don't use the index if it's empty. */
22ca247e 4847 if (map->name_count == 0)
927aa2e7
JK
4848 return false;
4849
4850 /* If there is a .dwz file, read it so we can get its CU list as
4851 well. */
f8c41851 4852 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
4853 if (dwz != NULL)
4854 {
4855 if (!read_debug_names_from_section (objfile,
00f93c44 4856 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
4857 &dwz->debug_names, dwz_map))
4858 {
4859 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 4860 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
4861 return false;
4862 }
4863 }
4864
f8c41851 4865 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 4866
22ca247e 4867 if (map->tu_count != 0)
927aa2e7
JK
4868 {
4869 /* We can only handle a single .debug_types when we have an
4870 index. */
f8c41851 4871 if (per_bfd->types.size () != 1)
927aa2e7
JK
4872 return false;
4873
f8c41851 4874 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
4875
4876 create_signatured_type_table_from_debug_names
f8c41851 4877 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
4878 }
4879
f8c41851 4880 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 4881
f8c41851
SM
4882 per_bfd->debug_names_table = std::move (map);
4883 per_bfd->using_index = 1;
4884 per_bfd->quick_file_names_table =
bab287cd 4885 create_quick_file_names_table (per_bfd->all_comp_units.size ());
927aa2e7
JK
4886
4887 return true;
4888}
4889
927aa2e7
JK
4890/* Type used to manage iterating over all CUs looking for a symbol for
4891 .debug_names. */
4892
4893class dw2_debug_names_iterator
4894{
4895public:
927aa2e7 4896 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4897 block_search_flags block_index,
2b79f376 4898 domain_enum domain,
fcf23d5b 4899 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 4900 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
4901 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4902 m_per_objfile (per_objfile)
927aa2e7
JK
4903 {}
4904
4905 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4906 search_domain search, uint32_t namei,
3bfa51a7
TT
4907 dwarf2_per_objfile *per_objfile,
4908 domain_enum domain = UNDEF_DOMAIN)
927aa2e7 4909 : m_map (map),
3bfa51a7 4910 m_domain (domain),
927aa2e7 4911 m_search (search),
fcf23d5b
SM
4912 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4913 m_per_objfile (per_objfile)
927aa2e7
JK
4914 {}
4915
3b00ef10 4916 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4917 block_search_flags block_index, domain_enum domain,
fcf23d5b 4918 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 4919 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
4920 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4921 m_per_objfile (per_objfile)
3b00ef10
TT
4922 {}
4923
927aa2e7
JK
4924 /* Return the next matching CU or NULL if there are no more. */
4925 dwarf2_per_cu_data *next ();
4926
4927private:
4928 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
4929 const char *name,
4930 dwarf2_per_objfile *per_objfile);
927aa2e7 4931 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
4932 uint32_t namei,
4933 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
4934
4935 /* The internalized form of .debug_names. */
4936 const mapped_debug_names &m_map;
4937
03a8ea51
TT
4938 /* Restrict the search to these blocks. */
4939 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4940 | SEARCH_STATIC_BLOCK);
927aa2e7
JK
4941
4942 /* The kind of symbol we're looking for. */
4943 const domain_enum m_domain = UNDEF_DOMAIN;
4944 const search_domain m_search = ALL_DOMAIN;
4945
4946 /* The list of CUs from the index entry of the symbol, or NULL if
4947 not found. */
4948 const gdb_byte *m_addr;
fcf23d5b
SM
4949
4950 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
4951};
4952
4953const char *
fcf23d5b 4954mapped_debug_names::namei_to_name
976ca316 4955 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
4956{
4957 const ULONGEST namei_string_offs
4958 = extract_unsigned_integer ((name_table_string_offs_reordered
4959 + namei * offset_size),
4960 offset_size,
4961 dwarf5_byte_order);
976ca316 4962 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
4963}
4964
4965/* Find a slot in .debug_names for the object named NAME. If NAME is
4966 found, return pointer to its pool data. If NAME cannot be found,
4967 return NULL. */
4968
4969const gdb_byte *
4970dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
4971 (const mapped_debug_names &map, const char *name,
4972 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
4973{
4974 int (*cmp) (const char *, const char *);
4975
54ee4252 4976 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
4977 if (current_language->la_language == language_cplus
4978 || current_language->la_language == language_fortran
4979 || current_language->la_language == language_d)
4980 {
4981 /* NAME is already canonical. Drop any qualifiers as
4982 .debug_names does not contain any. */
4983
4984 if (strchr (name, '(') != NULL)
4985 {
54ee4252 4986 without_params = cp_remove_params (name);
927aa2e7 4987 if (without_params != NULL)
54ee4252 4988 name = without_params.get ();
927aa2e7
JK
4989 }
4990 }
4991
4992 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4993
4994 const uint32_t full_hash = dwarf5_djb_hash (name);
4995 uint32_t namei
4996 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4997 (map.bucket_table_reordered
4998 + (full_hash % map.bucket_count)), 4,
4999 map.dwarf5_byte_order);
5000 if (namei == 0)
5001 return NULL;
5002 --namei;
5003 if (namei >= map.name_count)
5004 {
b98664d3 5005 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5006 "[in module %s]"),
5007 namei, map.name_count,
fcf23d5b 5008 objfile_name (per_objfile->objfile));
927aa2e7
JK
5009 return NULL;
5010 }
5011
5012 for (;;)
5013 {
5014 const uint32_t namei_full_hash
5015 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5016 (map.hash_table_reordered + namei), 4,
5017 map.dwarf5_byte_order);
5018 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5019 return NULL;
5020
5021 if (full_hash == namei_full_hash)
5022 {
fcf23d5b 5023 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5024
5025#if 0 /* An expensive sanity check. */
5026 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5027 {
b98664d3 5028 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5029 "[in module %s]"),
5030 namei, objfile_name (dwarf2_per_objfile->objfile));
5031 return NULL;
5032 }
5033#endif
5034
5035 if (cmp (namei_string, name) == 0)
5036 {
5037 const ULONGEST namei_entry_offs
5038 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5039 + namei * map.offset_size),
5040 map.offset_size, map.dwarf5_byte_order);
5041 return map.entry_pool + namei_entry_offs;
5042 }
5043 }
5044
5045 ++namei;
5046 if (namei >= map.name_count)
5047 return NULL;
5048 }
5049}
5050
5051const gdb_byte *
5052dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5053 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5054{
5055 if (namei >= map.name_count)
5056 {
b98664d3 5057 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5058 "[in module %s]"),
5059 namei, map.name_count,
fcf23d5b 5060 objfile_name (per_objfile->objfile));
927aa2e7
JK
5061 return NULL;
5062 }
5063
5064 const ULONGEST namei_entry_offs
5065 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5066 + namei * map.offset_size),
5067 map.offset_size, map.dwarf5_byte_order);
5068 return map.entry_pool + namei_entry_offs;
5069}
5070
5071/* See dw2_debug_names_iterator. */
5072
5073dwarf2_per_cu_data *
5074dw2_debug_names_iterator::next ()
5075{
5076 if (m_addr == NULL)
5077 return NULL;
5078
fcf23d5b
SM
5079 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5080 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5081 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5082
5083 again:
5084
5085 unsigned int bytes_read;
5086 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5087 m_addr += bytes_read;
5088 if (abbrev == 0)
5089 return NULL;
5090
5091 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5092 if (indexval_it == m_map.abbrev_map.cend ())
5093 {
b98664d3 5094 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5095 "[in module %s]"),
ed2dc618 5096 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5097 return NULL;
5098 }
5099 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5100 enum class symbol_linkage {
5101 unknown,
5102 static_,
5103 extern_,
23c13d42 5104 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5105 dwarf2_per_cu_data *per_cu = NULL;
5106 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5107 {
5108 ULONGEST ull;
5109 switch (attr.form)
5110 {
5111 case DW_FORM_implicit_const:
5112 ull = attr.implicit_const;
5113 break;
5114 case DW_FORM_flag_present:
5115 ull = 1;
5116 break;
5117 case DW_FORM_udata:
5118 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5119 m_addr += bytes_read;
5120 break;
6dc55ce9 5121 case DW_FORM_ref4:
5122 ull = read_4_bytes (abfd, m_addr);
5123 m_addr += 4;
5124 break;
5125 case DW_FORM_ref8:
5126 ull = read_8_bytes (abfd, m_addr);
5127 m_addr += 8;
5128 break;
5129 case DW_FORM_ref_sig8:
5130 ull = read_8_bytes (abfd, m_addr);
5131 m_addr += 8;
5132 break;
927aa2e7 5133 default:
b98664d3 5134 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5135 dwarf_form_name (attr.form),
ed2dc618 5136 objfile_name (objfile));
927aa2e7
JK
5137 return NULL;
5138 }
5139 switch (attr.dw_idx)
5140 {
5141 case DW_IDX_compile_unit:
5142 /* Don't crash on bad data. */
bab287cd 5143 if (ull >= per_bfd->all_comp_units.size ())
927aa2e7 5144 {
b98664d3 5145 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5146 " [in module %s]"),
5147 pulongest (ull),
fcf23d5b 5148 objfile_name (objfile));
927aa2e7
JK
5149 continue;
5150 }
91eea9cc 5151 per_cu = per_bfd->get_cu (ull);
927aa2e7 5152 break;
8af5c486
JK
5153 case DW_IDX_type_unit:
5154 /* Don't crash on bad data. */
91eea9cc 5155 if (ull >= per_bfd->tu_stats.nr_tus)
8af5c486 5156 {
b98664d3 5157 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5158 " [in module %s]"),
5159 pulongest (ull),
fcf23d5b 5160 objfile_name (objfile));
8af5c486
JK
5161 continue;
5162 }
91eea9cc 5163 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
8af5c486 5164 break;
6dc55ce9 5165 case DW_IDX_die_offset:
5166 /* In a per-CU index (as opposed to a per-module index), index
5167 entries without CU attribute implicitly refer to the single CU. */
5168 if (per_cu == NULL)
fcf23d5b 5169 per_cu = per_bfd->get_cu (0);
6dc55ce9 5170 break;
927aa2e7
JK
5171 case DW_IDX_GNU_internal:
5172 if (!m_map.augmentation_is_gdb)
5173 break;
23c13d42 5174 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5175 break;
5176 case DW_IDX_GNU_external:
5177 if (!m_map.augmentation_is_gdb)
5178 break;
23c13d42 5179 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5180 break;
5181 }
5182 }
5183
5184 /* Skip if already read in. */
fcf23d5b 5185 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5186 goto again;
5187
5188 /* Check static vs global. */
03a8ea51 5189 if (symbol_linkage_ != symbol_linkage::unknown)
927aa2e7 5190 {
03a8ea51
TT
5191 if (symbol_linkage_ == symbol_linkage::static_)
5192 {
5193 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5194 goto again;
5195 }
5196 else
5197 {
5198 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5199 goto again;
5200 }
927aa2e7
JK
5201 }
5202
5203 /* Match dw2_symtab_iter_next, symbol_kind
5204 and debug_names::psymbol_tag. */
5205 switch (m_domain)
5206 {
5207 case VAR_DOMAIN:
5208 switch (indexval.dwarf_tag)
5209 {
5210 case DW_TAG_variable:
5211 case DW_TAG_subprogram:
5212 /* Some types are also in VAR_DOMAIN. */
5213 case DW_TAG_typedef:
5214 case DW_TAG_structure_type:
5215 break;
5216 default:
5217 goto again;
5218 }
5219 break;
5220 case STRUCT_DOMAIN:
5221 switch (indexval.dwarf_tag)
5222 {
5223 case DW_TAG_typedef:
5224 case DW_TAG_structure_type:
5225 break;
5226 default:
5227 goto again;
5228 }
5229 break;
5230 case LABEL_DOMAIN:
5231 switch (indexval.dwarf_tag)
5232 {
5233 case 0:
5234 case DW_TAG_variable:
5235 break;
5236 default:
5237 goto again;
5238 }
5239 break;
59c35742
AB
5240 case MODULE_DOMAIN:
5241 switch (indexval.dwarf_tag)
5242 {
5243 case DW_TAG_module:
5244 break;
5245 default:
5246 goto again;
5247 }
5248 break;
927aa2e7
JK
5249 default:
5250 break;
5251 }
5252
5253 /* Match dw2_expand_symtabs_matching, symbol_kind and
5254 debug_names::psymbol_tag. */
5255 switch (m_search)
4b514bc8 5256 {
927aa2e7
JK
5257 case VARIABLES_DOMAIN:
5258 switch (indexval.dwarf_tag)
4b514bc8 5259 {
927aa2e7
JK
5260 case DW_TAG_variable:
5261 break;
5262 default:
5263 goto again;
4b514bc8 5264 }
927aa2e7
JK
5265 break;
5266 case FUNCTIONS_DOMAIN:
5267 switch (indexval.dwarf_tag)
4b514bc8 5268 {
927aa2e7
JK
5269 case DW_TAG_subprogram:
5270 break;
5271 default:
5272 goto again;
4b514bc8 5273 }
927aa2e7
JK
5274 break;
5275 case TYPES_DOMAIN:
5276 switch (indexval.dwarf_tag)
5277 {
5278 case DW_TAG_typedef:
5279 case DW_TAG_structure_type:
5280 break;
5281 default:
5282 goto again;
5283 }
5284 break;
59c35742
AB
5285 case MODULES_DOMAIN:
5286 switch (indexval.dwarf_tag)
5287 {
5288 case DW_TAG_module:
5289 break;
5290 default:
5291 goto again;
5292 }
927aa2e7
JK
5293 default:
5294 break;
4b514bc8 5295 }
927aa2e7
JK
5296
5297 return per_cu;
4b514bc8 5298}
61920122 5299
927aa2e7
JK
5300/* This dumps minimal information about .debug_names. It is called
5301 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5302 uses this to verify that .debug_names has been loaded. */
9291a0cd 5303
39298a5d
TT
5304void
5305dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5306{
976ca316 5307 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5308
976ca316 5309 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5310 printf_filtered (".debug_names:");
976ca316 5311 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5312 printf_filtered (" exists\n");
5313 else
5314 printf_filtered (" faked for \"readnow\"\n");
5315 printf_filtered ("\n");
9291a0cd
TT
5316}
5317
39298a5d 5318void
0b7b2c2a 5319dwarf2_debug_names_index::expand_matching_symbols
3b00ef10
TT
5320 (struct objfile *objfile,
5321 const lookup_name_info &name, domain_enum domain,
5322 int global,
3b00ef10
TT
5323 symbol_compare_ftype *ordered_compare)
5324{
976ca316 5325 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5326
5327 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5328 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5329 return;
5330
976ca316 5331 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
03a8ea51
TT
5332 const block_search_flags block_flags
5333 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
3b00ef10
TT
5334
5335 const char *match_name = name.ada ().lookup_name ().c_str ();
5336 auto matcher = [&] (const char *symname)
5337 {
5338 if (ordered_compare == nullptr)
5339 return true;
5340 return ordered_compare (symname, match_name) == 0;
5341 };
5342
b64f703b 5343 dw2_expand_symtabs_matching_symbol (map, name, matcher,
3b00ef10
TT
5344 [&] (offset_type namei)
5345 {
5346 /* The name was matched, now expand corresponding CUs that were
5347 marked. */
03a8ea51 5348 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
976ca316 5349 per_objfile);
3b00ef10
TT
5350
5351 struct dwarf2_per_cu_data *per_cu;
5352 while ((per_cu = iter.next ()) != NULL)
976ca316 5353 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5354 nullptr);
3b00ef10 5355 return true;
976ca316 5356 }, per_objfile);
3b00ef10
TT
5357}
5358
df35e626 5359bool
39298a5d 5360dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5361 (struct objfile *objfile,
5362 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5363 const lookup_name_info *lookup_name,
927aa2e7
JK
5364 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5365 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 5366 block_search_flags search_flags,
3bfa51a7 5367 domain_enum domain,
927aa2e7
JK
5368 enum search_domain kind)
5369{
976ca316 5370 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5371
927aa2e7 5372 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5373 if (!per_objfile->per_bfd->debug_names_table)
df35e626 5374 return true;
9291a0cd 5375
976ca316 5376 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5377
c1a66c06
TV
5378 if (symbol_matcher == NULL && lookup_name == NULL)
5379 {
de2143d6
TT
5380 for (dwarf2_per_cu_data *per_cu
5381 : all_comp_units_range (per_objfile->per_bfd))
c1a66c06
TV
5382 {
5383 QUIT;
5384
de2143d6 5385 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
df35e626
TT
5386 file_matcher,
5387 expansion_notify))
5388 return false;
c1a66c06 5389 }
df35e626 5390 return true;
c1a66c06
TV
5391 }
5392
976ca316 5393 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5394
df35e626
TT
5395 bool result
5396 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5397 symbol_matcher,
5398 [&] (offset_type namei)
927aa2e7 5399 {
927aa2e7
JK
5400 /* The name was matched, now expand corresponding CUs that were
5401 marked. */
3bfa51a7 5402 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
bbf2f4df 5403
927aa2e7
JK
5404 struct dwarf2_per_cu_data *per_cu;
5405 while ((per_cu = iter.next ()) != NULL)
df35e626
TT
5406 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5407 file_matcher,
5408 expansion_notify))
5409 return false;
3b00ef10 5410 return true;
976ca316 5411 }, per_objfile);
df35e626
TT
5412
5413 return result;
9291a0cd
TT
5414}
5415
4485a1c1 5416/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5417 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5418
5419template <typename T>
5420static gdb::array_view<const gdb_byte>
5421get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5422{
5423 dwarf2_section_info *section = &section_owner->gdb_index;
5424
96b79293 5425 if (section->empty ())
4485a1c1
SM
5426 return {};
5427
5428 /* Older elfutils strip versions could keep the section in the main
5429 executable while splitting it for the separate debug info file. */
96b79293 5430 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5431 return {};
5432
96b79293 5433 section->read (obj);
4485a1c1 5434
8bebfcda
PA
5435 /* dwarf2_section_info::size is a bfd_size_type, while
5436 gdb::array_view works with size_t. On 32-bit hosts, with
5437 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5438 is 32-bit. So we need an explicit narrowing conversion here.
5439 This is fine, because it's impossible to allocate or mmap an
5440 array/buffer larger than what size_t can represent. */
5441 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5442}
5443
87d6a7aa
SM
5444/* Lookup the index cache for the contents of the index associated to
5445 DWARF2_OBJ. */
5446
5447static gdb::array_view<const gdb_byte>
5989a64e 5448get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5449{
5450 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5451 if (build_id == nullptr)
5452 return {};
5453
5454 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5455 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5456}
5457
5458/* Same as the above, but for DWZ. */
5459
5460static gdb::array_view<const gdb_byte>
5461get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5462{
5463 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5464 if (build_id == nullptr)
5465 return {};
5466
5467 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5468}
5469
edc02ceb 5470/* See dwarf2/public.h. */
9291a0cd 5471
edc02ceb
TT
5472void
5473dwarf2_initialize_objfile (struct objfile *objfile)
9291a0cd 5474{
976ca316
SM
5475 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5476 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5477
850ed749
SM
5478 dwarf_read_debug_printf ("called");
5479
9291a0cd
TT
5480 /* If we're about to read full symbols, don't bother with the
5481 indices. In this case we also don't care if some other debug
5482 format is making psymtabs, because they are all about to be
5483 expanded anyway. */
5484 if ((objfile->flags & OBJF_READNOW))
5485 {
850ed749
SM
5486 dwarf_read_debug_printf ("readnow requested");
5487
17ee85fc
TT
5488 /* When using READNOW, the using_index flag (set below) indicates that
5489 PER_BFD was already initialized, when we loaded some other objfile. */
5490 if (per_bfd->using_index)
5491 {
850ed749 5492 dwarf_read_debug_printf ("using_index already set");
edc02ceb
TT
5493 objfile->qf.push_front (make_dwarf_gdb_index ());
5494 return;
17ee85fc
TT
5495 }
5496
5497 per_bfd->using_index = 1;
976ca316 5498 create_all_comp_units (per_objfile);
17ee85fc
TT
5499 per_bfd->quick_file_names_table
5500 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
9291a0cd 5501
91eea9cc 5502 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
9291a0cd 5503 {
91eea9cc 5504 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
9291a0cd 5505
17ee85fc 5506 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5507 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5508 }
5509
edc02ceb
TT
5510 /* Arrange for gdb to see the "quick" functions. However, these
5511 functions will be no-ops because we will have expanded all
5512 symtabs. */
5513 objfile->qf.push_front (make_dwarf_gdb_index ());
5514 return;
9291a0cd
TT
5515 }
5516
17ee85fc
TT
5517 /* Was a debug names index already read when we processed an objfile sharing
5518 PER_BFD? */
5519 if (per_bfd->debug_names_table != nullptr)
5520 {
850ed749 5521 dwarf_read_debug_printf ("re-using shared debug names table");
edc02ceb
TT
5522 objfile->qf.push_front (make_dwarf_debug_names ());
5523 return;
17ee85fc
TT
5524 }
5525
5526 /* Was a GDB index already read when we processed an objfile sharing
5527 PER_BFD? */
5528 if (per_bfd->index_table != nullptr)
5529 {
850ed749 5530 dwarf_read_debug_printf ("re-using shared index table");
edc02ceb
TT
5531 objfile->qf.push_front (make_dwarf_gdb_index ());
5532 return;
17ee85fc
TT
5533 }
5534
efb763a5
SM
5535 /* There might already be partial symtabs built for this BFD. This happens
5536 when loading the same binary twice with the index-cache enabled. If so,
5537 don't try to read an index. The objfile / per_objfile initialization will
5538 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5539 code path. */
5540 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
5541 {
5542 dwarf_read_debug_printf ("re-using shared partial symtabs");
edc02ceb
TT
5543 objfile->qf.push_front (make_lazy_dwarf_reader ());
5544 return;
850ed749 5545 }
efb763a5 5546
976ca316 5547 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 5548 {
850ed749 5549 dwarf_read_debug_printf ("found debug names");
edc02ceb
TT
5550 objfile->qf.push_front (make_dwarf_debug_names ());
5551 return;
3c0aa29a 5552 }
927aa2e7 5553
976ca316 5554 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5555 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5556 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 5557 {
850ed749 5558 dwarf_read_debug_printf ("found gdb index from file");
edc02ceb
TT
5559 objfile->qf.push_front (make_dwarf_gdb_index ());
5560 return;
3c0aa29a 5561 }
9291a0cd 5562
87d6a7aa 5563 /* ... otherwise, try to find the index in the index cache. */
976ca316 5564 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
5565 get_gdb_index_contents_from_cache,
5566 get_gdb_index_contents_from_cache_dwz))
5567 {
850ed749 5568 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa 5569 global_index_cache.hit ();
edc02ceb
TT
5570 objfile->qf.push_front (make_dwarf_gdb_index ());
5571 return;
87d6a7aa
SM
5572 }
5573
5574 global_index_cache.miss ();
edc02ceb 5575 objfile->qf.push_front (make_lazy_dwarf_reader ());
9291a0cd
TT
5576}
5577
5578\f
5579
dce234bc
PP
5580/* Build a partial symbol table. */
5581
5582void
eb36a3eb 5583dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
dce234bc 5584{
976ca316
SM
5585 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5586 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
5587
5588 if (per_bfd->partial_symtabs != nullptr)
5589 {
5590 /* Partial symbols were already read, so now we can simply
5591 attach them. */
eb36a3eb
TT
5592 if (psf == nullptr)
5593 {
5594 psf = new psymbol_functions (per_bfd->partial_symtabs);
5595 objfile->qf.emplace_front (psf);
5596 }
5597 else
5598 psf->set_partial_symtabs (per_bfd->partial_symtabs);
17ee85fc
TT
5599 return;
5600 }
c9bf0622 5601
eb36a3eb
TT
5602 if (psf == nullptr)
5603 {
5604 psf = new psymbol_functions;
5605 objfile->qf.emplace_front (psf);
5606 }
5607 const std::shared_ptr<psymtab_storage> &partial_symtabs
5608 = psf->get_partial_symtabs ();
5609
51962708
TT
5610 /* Set the local reference to partial symtabs, so that we don't try
5611 to read them again if reading another objfile with the same BFD.
5612 If we can't in fact share, this won't make a difference anyway as
5613 the dwarf2_per_bfd object won't be shared. */
eb36a3eb 5614 per_bfd->partial_symtabs = partial_symtabs;
51962708 5615
a70b8144 5616 try
c9bf0622
TT
5617 {
5618 /* This isn't really ideal: all the data we allocate on the
5619 objfile's obstack is still uselessly kept around. However,
5620 freeing it seems unsafe. */
eb36a3eb 5621 psymtab_discarder psymtabs (partial_symtabs.get ());
976ca316 5622 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 5623 psymtabs.keep ();
87d6a7aa
SM
5624
5625 /* (maybe) store an index in the cache. */
976ca316 5626 global_index_cache.store (per_objfile);
c9bf0622 5627 }
230d2906 5628 catch (const gdb_exception_error &except)
492d29ea
PA
5629 {
5630 exception_print (gdb_stderr, except);
5631 }
c906108c 5632}
c906108c 5633
3b80fe9b
DE
5634/* Find the base address of the compilation unit for range lists and
5635 location lists. It will normally be specified by DW_AT_low_pc.
5636 In DWARF-3 draft 4, the base address could be overridden by
5637 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5638 compilation units with discontinuous ranges. */
5639
5640static void
5641dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5642{
5643 struct attribute *attr;
5644
2b24b6e4 5645 cu->base_address.reset ();
3b80fe9b
DE
5646
5647 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5648 if (attr != nullptr)
95f982e5 5649 cu->base_address = attr->as_address ();
3b80fe9b
DE
5650 else
5651 {
5652 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5653 if (attr != nullptr)
95f982e5 5654 cu->base_address = attr->as_address ();
3b80fe9b
DE
5655 }
5656}
5657
36586728
TT
5658/* Helper function that returns the proper abbrev section for
5659 THIS_CU. */
5660
5661static struct dwarf2_section_info *
5662get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5663{
5664 struct dwarf2_section_info *abbrev;
c3699833 5665 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
5666
5667 if (this_cu->is_dwz)
a7308ce0 5668 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 5669 else
c3699833 5670 abbrev = &per_bfd->abbrev;
36586728
TT
5671
5672 return abbrev;
5673}
5674
f4dc4d17
DE
5675/* Fetch the abbreviation table offset from a comp or type unit header. */
5676
5677static sect_offset
976ca316 5678read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 5679 struct dwarf2_section_info *section,
9c541725 5680 sect_offset sect_off)
f4dc4d17 5681{
96b79293 5682 bfd *abfd = section->get_bfd_owner ();
d521ce57 5683 const gdb_byte *info_ptr;
ac298888 5684 unsigned int initial_length_size, offset_size;
43988095 5685 uint16_t version;
f4dc4d17 5686
976ca316 5687 section->read (per_objfile->objfile);
9c541725 5688 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5689 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5690 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5691 info_ptr += initial_length_size;
5692
5693 version = read_2_bytes (abfd, info_ptr);
5694 info_ptr += 2;
5695 if (version >= 5)
5696 {
5697 /* Skip unit type and address size. */
5698 info_ptr += 2;
5699 }
5700
24aa364d 5701 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5702}
5703
b83470bf
TT
5704/* A partial symtab that is used only for include files. */
5705struct dwarf2_include_psymtab : public partial_symtab
5706{
7b249e47
TT
5707 dwarf2_include_psymtab (const char *filename,
5708 psymtab_storage *partial_symtabs,
0072c873
SM
5709 objfile_per_bfd_storage *objfile_per_bfd)
5710 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
b83470bf
TT
5711 {
5712 }
5713
5714 void read_symtab (struct objfile *objfile) override
5715 {
194d088f
TV
5716 /* It's an include file, no symbols to read for it.
5717 Everything is in the includer symtab. */
5718
5719 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5720 expansion of the includer psymtab. We use the dependencies[0] field to
5721 model the includer. But if we go the regular route of calling
5722 expand_psymtab here, and having expand_psymtab call expand_dependencies
5723 to expand the includer, we'll only use expand_psymtab on the includer
5724 (making it a non-toplevel psymtab), while if we expand the includer via
5725 another path, we'll use read_symtab (making it a toplevel psymtab).
5726 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5727 psymtab, and trigger read_symtab on the includer here directly. */
5728 includer ()->read_symtab (objfile);
b83470bf
TT
5729 }
5730
5731 void expand_psymtab (struct objfile *objfile) override
5732 {
194d088f
TV
5733 /* This is not called by read_symtab, and should not be called by any
5734 expand_dependencies. */
5735 gdb_assert (false);
b83470bf
TT
5736 }
5737
5717c425 5738 bool readin_p (struct objfile *objfile) const override
b83470bf 5739 {
5717c425 5740 return includer ()->readin_p (objfile);
b83470bf
TT
5741 }
5742
5717c425 5743 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
5744 {
5745 return nullptr;
5746 }
5747
5748private:
194d088f
TV
5749 partial_symtab *includer () const
5750 {
5751 /* An include psymtab has exactly one dependency: the psymtab that
5752 includes it. */
5753 gdb_assert (this->number_of_dependencies == 1);
5754 return this->dependencies[0];
5755 }
b83470bf
TT
5756};
5757
aaa75496
JB
5758/* Allocate a new partial symtab for file named NAME and mark this new
5759 partial symtab as being an include of PST. */
5760
5761static void
84685904 5762dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
7b249e47
TT
5763 const char *name,
5764 dwarf2_psymtab *pst,
5765 psymtab_storage *partial_symtabs,
0072c873 5766 objfile_per_bfd_storage *objfile_per_bfd)
aaa75496 5767{
7b249e47 5768 dwarf2_include_psymtab *subpst
0072c873 5769 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
aaa75496 5770
fbd9ab74 5771 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 5772 subpst->dirname = pst->dirname;
fbd9ab74 5773
84685904 5774 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5775 subpst->dependencies[0] = pst;
5776 subpst->number_of_dependencies = 1;
aaa75496
JB
5777}
5778
5779/* Read the Line Number Program data and extract the list of files
5780 included by the source file represented by PST. Build an include
d85a05f0 5781 partial symtab for each of these included files. */
aaa75496
JB
5782
5783static void
5784dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5785 struct die_info *die,
891813be 5786 dwarf2_psymtab *pst)
aaa75496 5787{
fff8551c 5788 line_header_up lh;
d85a05f0 5789 struct attribute *attr;
aaa75496 5790
d85a05f0 5791 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
5792 if (attr != nullptr && attr->form_is_unsigned ())
5793 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
5794 if (lh == NULL)
5795 return; /* No linetable, so no includes. */
5796
79748972
TT
5797 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5798 that we pass in the raw text_low here; that is ok because we're
5799 only decoding the line table to make include partial symtabs, and
5800 so the addresses aren't really used. */
4ae976d1 5801 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5802 pst->raw_text_low (), 1);
aaa75496
JB
5803}
5804
348e048f 5805static hashval_t
52dc124a 5806hash_signatured_type (const void *item)
348e048f 5807{
9a3c8263
SM
5808 const struct signatured_type *sig_type
5809 = (const struct signatured_type *) item;
9a619af0 5810
348e048f 5811 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5812 return sig_type->signature;
348e048f
DE
5813}
5814
5815static int
52dc124a 5816eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5817{
9a3c8263
SM
5818 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5819 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5820
348e048f
DE
5821 return lhs->signature == rhs->signature;
5822}
5823
1fd400ff
TT
5824/* Allocate a hash table for signatured types. */
5825
b0b6a987 5826static htab_up
298e9637 5827allocate_signatured_type_table ()
1fd400ff 5828{
b0b6a987
TT
5829 return htab_up (htab_create_alloc (41,
5830 hash_signatured_type,
5831 eq_signatured_type,
5832 NULL, xcalloc, xfree));
1fd400ff
TT
5833}
5834
78d4d2c5 5835/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5836 and fill them into TYPES_HTAB. It will process only type units,
5837 therefore DW_UT_type. */
c88ee1f0 5838
78d4d2c5 5839static void
976ca316 5840create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 5841 struct dwo_file *dwo_file,
b0b6a987 5842 dwarf2_section_info *section, htab_up &types_htab,
43988095 5843 rcuh_kind section_kind)
348e048f 5844{
976ca316 5845 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 5846 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
5847 bfd *abfd;
5848 const gdb_byte *info_ptr, *end_ptr;
348e048f 5849
b8efb248 5850 abbrev_section = &dwo_file->sections.abbrev;
4bdcc0c1 5851
4800761a 5852 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
5853 section->get_name (),
5854 abbrev_section->get_file_name ());
09406207 5855
96b79293 5856 section->read (objfile);
78d4d2c5 5857 info_ptr = section->buffer;
348e048f 5858
78d4d2c5
JK
5859 if (info_ptr == NULL)
5860 return;
348e048f 5861
78d4d2c5
JK
5862 /* We can't set abfd until now because the section may be empty or
5863 not present, in which case the bfd is unknown. */
96b79293 5864 abfd = section->get_bfd_owner ();
348e048f 5865
c0ab21c2
TT
5866 /* We don't use cutu_reader here because we don't need to read
5867 any dies: the signature is in the header. */
3019eac3 5868
78d4d2c5
JK
5869 end_ptr = info_ptr + section->size;
5870 while (info_ptr < end_ptr)
5871 {
46c6bcf6 5872 signatured_type_up sig_type;
78d4d2c5
JK
5873 struct dwo_unit *dwo_tu;
5874 void **slot;
5875 const gdb_byte *ptr = info_ptr;
5876 struct comp_unit_head header;
5877 unsigned int length;
8b70b953 5878
9c541725 5879 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 5880
a49dd8dd
JK
5881 /* Initialize it due to a false compiler warning. */
5882 header.signature = -1;
9c541725 5883 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 5884
78d4d2c5
JK
5885 /* We need to read the type's signature in order to build the hash
5886 table, but we don't need anything else just yet. */
348e048f 5887
976ca316 5888 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 5889 abbrev_section, ptr, section_kind);
348e048f 5890
4057dfde 5891 length = header.get_length ();
6caca83c 5892
78d4d2c5
JK
5893 /* Skip dummy type units. */
5894 if (ptr >= info_ptr + length
43988095 5895 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
5896 || (header.unit_type != DW_UT_type
5897 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
5898 {
5899 info_ptr += length;
5900 continue;
5901 }
dee91e82 5902
78d4d2c5 5903 if (types_htab == NULL)
b8efb248 5904 types_htab = allocate_dwo_unit_table ();
8b70b953 5905
b8efb248
TT
5906 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5907 dwo_tu->dwo_file = dwo_file;
5908 dwo_tu->signature = header.signature;
5909 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5910 dwo_tu->section = section;
5911 dwo_tu->sect_off = sect_off;
5912 dwo_tu->length = length;
78d4d2c5 5913
b8efb248 5914 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
78d4d2c5
JK
5915 gdb_assert (slot != NULL);
5916 if (*slot != NULL)
b8efb248
TT
5917 complaint (_("debug type entry at offset %s is duplicate to"
5918 " the entry at offset %s, signature %s"),
5919 sect_offset_str (sect_off),
5920 sect_offset_str (dwo_tu->sect_off),
5921 hex_string (header.signature));
5922 *slot = dwo_tu;
3019eac3 5923
6f738b01
SM
5924 dwarf_read_debug_printf_v (" offset %s, signature %s",
5925 sect_offset_str (sect_off),
5926 hex_string (header.signature));
3019eac3 5927
78d4d2c5
JK
5928 info_ptr += length;
5929 }
5930}
3019eac3 5931
78d4d2c5
JK
5932/* Create the hash table of all entries in the .debug_types
5933 (or .debug_types.dwo) section(s).
b8efb248 5934 DWO_FILE is a pointer to the DWO file object.
b3c8eb43 5935
78d4d2c5 5936 The result is a pointer to the hash table or NULL if there are no types.
348e048f 5937
78d4d2c5 5938 Note: This function processes DWO files only, not DWP files. */
348e048f 5939
78d4d2c5 5940static void
976ca316 5941create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 5942 struct dwo_file *dwo_file,
fd5866f6 5943 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 5944 htab_up &types_htab)
78d4d2c5 5945{
fd5866f6 5946 for (dwarf2_section_info &section : type_sections)
976ca316
SM
5947 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5948 rcuh_kind::TYPE);
3019eac3
DE
5949}
5950
5989a64e 5951/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
5952 If SLOT is non-NULL, it is the entry to use in the hash table.
5953 Otherwise we find one. */
5954
5955static struct signatured_type *
976ca316 5956add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 5957{
91eea9cc
TT
5958 if (per_objfile->per_bfd->all_comp_units.size ()
5959 == per_objfile->per_bfd->all_comp_units.capacity ())
976ca316 5960 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 5961
46c6bcf6 5962 signatured_type_up sig_type_holder
9ea36493 5963 = per_objfile->per_bfd->allocate_signatured_type (sig);
0d305d5c 5964 signatured_type *sig_type = sig_type_holder.get ();
b2bdb8cf 5965
473ab964
TT
5966 per_objfile->per_bfd->all_comp_units.emplace_back
5967 (sig_type_holder.release ());
976ca316 5968 if (per_objfile->per_bfd->using_index)
6aa5f3a6 5969 {
0d305d5c 5970 sig_type->v.quick =
976ca316 5971 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
5972 struct dwarf2_per_cu_quick_data);
5973 }
5974
5975 if (slot == NULL)
5976 {
976ca316 5977 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
5978 sig_type, INSERT);
5979 }
5980 gdb_assert (*slot == NULL);
5981 *slot = sig_type;
5982 /* The rest of sig_type must be filled in by the caller. */
5983 return sig_type;
5984}
5985
a2ce51a0
DE
5986/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5987 Fill in SIG_ENTRY with DWO_ENTRY. */
5988
5989static void
976ca316 5990fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
5991 struct signatured_type *sig_entry,
5992 struct dwo_unit *dwo_entry)
5993{
976ca316 5994 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 5995
7ee85ab1 5996 /* Make sure we're not clobbering something we don't expect to. */
0d305d5c
TT
5997 gdb_assert (! sig_entry->queued);
5998 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
1859c670 5999 if (per_bfd->using_index)
6aa5f3a6 6000 {
0d305d5c
TT
6001 gdb_assert (sig_entry->v.quick != NULL);
6002 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6aa5f3a6
DE
6003 }
6004 else
0d305d5c 6005 gdb_assert (sig_entry->v.psymtab == NULL);
a2ce51a0 6006 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6007 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6008 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6009 gdb_assert (sig_entry->dwo_unit == NULL);
6010
0d305d5c
TT
6011 sig_entry->section = dwo_entry->section;
6012 sig_entry->sect_off = dwo_entry->sect_off;
6013 sig_entry->length = dwo_entry->length;
6014 sig_entry->reading_dwo_directly = 1;
6015 sig_entry->per_bfd = per_bfd;
a2ce51a0
DE
6016 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6017 sig_entry->dwo_unit = dwo_entry;
6018}
6019
6020/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6021 If we haven't read the TU yet, create the signatured_type data structure
6022 for a TU to be read in directly from a DWO file, bypassing the stub.
6023 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6024 using .gdb_index, then when reading a CU we want to stay in the DWO file
6025 containing that CU. Otherwise we could end up reading several other DWO
6026 files (due to comdat folding) to process the transitive closure of all the
6027 mentioned TUs, and that can be slow. The current DWO file will have every
6028 type signature that it needs.
a2ce51a0
DE
6029 We only do this for .gdb_index because in the psymtab case we already have
6030 to read all the DWOs to build the type unit groups. */
6031
6032static struct signatured_type *
6033lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6034{
976ca316 6035 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6036 struct dwo_file *dwo_file;
6037 struct dwo_unit find_dwo_entry, *dwo_entry;
6aa5f3a6 6038 void **slot;
a2ce51a0 6039
976ca316 6040 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6041
6aa5f3a6
DE
6042 /* If TU skeletons have been removed then we may not have read in any
6043 TUs yet. */
976ca316
SM
6044 if (per_objfile->per_bfd->signatured_types == NULL)
6045 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6046
6047 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6048 Use the global signatured_types array to do our own comdat-folding
6049 of types. If this is the first time we're reading this TU, and
6050 the TU has an entry in .gdb_index, replace the recorded data from
6051 .gdb_index with this TU. */
a2ce51a0 6052
9ea36493 6053 signatured_type find_sig_entry (sig);
976ca316 6054 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6055 &find_sig_entry, INSERT);
9ea36493 6056 signatured_type *sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6057
6058 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6059 read. Don't reassign the global entry to point to this DWO if that's
6060 the case. Also note that if the TU is already being read, it may not
6061 have come from a DWO, the program may be a mix of Fission-compiled
6062 code and non-Fission-compiled code. */
6063
6064 /* Have we already tried to read this TU?
6065 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6066 needn't exist in the global table yet). */
0d305d5c 6067 if (sig_entry != NULL && sig_entry->tu_read)
a2ce51a0
DE
6068 return sig_entry;
6069
6aa5f3a6
DE
6070 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6071 dwo_unit of the TU itself. */
6072 dwo_file = cu->dwo_unit->dwo_file;
6073
a2ce51a0
DE
6074 /* Ok, this is the first time we're reading this TU. */
6075 if (dwo_file->tus == NULL)
6076 return NULL;
6077 find_dwo_entry.signature = sig;
b0b6a987
TT
6078 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6079 &find_dwo_entry);
a2ce51a0
DE
6080 if (dwo_entry == NULL)
6081 return NULL;
6082
6aa5f3a6
DE
6083 /* If the global table doesn't have an entry for this TU, add one. */
6084 if (sig_entry == NULL)
976ca316 6085 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6086
976ca316 6087 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
0d305d5c 6088 sig_entry->tu_read = 1;
a2ce51a0
DE
6089 return sig_entry;
6090}
6091
a2ce51a0
DE
6092/* Subroutine of lookup_signatured_type.
6093 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6094 then try the DWP file. If the TU stub (skeleton) has been removed then
6095 it won't be in .gdb_index. */
a2ce51a0
DE
6096
6097static struct signatured_type *
6098lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6099{
976ca316
SM
6100 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6101 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0 6102 struct dwo_unit *dwo_entry;
6aa5f3a6 6103 void **slot;
a2ce51a0 6104
976ca316 6105 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6106 gdb_assert (dwp_file != NULL);
6107
6aa5f3a6
DE
6108 /* If TU skeletons have been removed then we may not have read in any
6109 TUs yet. */
976ca316
SM
6110 if (per_objfile->per_bfd->signatured_types == NULL)
6111 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6112
9ea36493 6113 signatured_type find_sig_entry (sig);
976ca316 6114 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6115 &find_sig_entry, INSERT);
9ea36493 6116 signatured_type *sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6117
6118 /* Have we already tried to read this TU?
6119 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6120 needn't exist in the global table yet). */
6121 if (sig_entry != NULL)
6122 return sig_entry;
6123
a2ce51a0
DE
6124 if (dwp_file->tus == NULL)
6125 return NULL;
976ca316
SM
6126 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6127 1 /* is_debug_types */);
a2ce51a0
DE
6128 if (dwo_entry == NULL)
6129 return NULL;
6130
976ca316
SM
6131 sig_entry = add_type_unit (per_objfile, sig, slot);
6132 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6133
a2ce51a0
DE
6134 return sig_entry;
6135}
6136
380bca97 6137/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6138 Returns NULL if signature SIG is not present in the table.
6139 It is up to the caller to complain about this. */
348e048f
DE
6140
6141static struct signatured_type *
a2ce51a0 6142lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6143{
976ca316 6144 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6145
976ca316 6146 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6147 {
6148 /* We're in a DWO/DWP file, and we're using .gdb_index.
6149 These cases require special processing. */
976ca316 6150 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6151 return lookup_dwo_signatured_type (cu, sig);
6152 else
6153 return lookup_dwp_signatured_type (cu, sig);
6154 }
6155 else
6156 {
976ca316 6157 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0 6158 return NULL;
9ea36493
SM
6159 signatured_type find_entry (sig);
6160 return ((struct signatured_type *)
6161 htab_find (per_objfile->per_bfd->signatured_types.get (),
6162 &find_entry));
a2ce51a0 6163 }
348e048f 6164}
18a8505e 6165
42e7ad6c 6166/* Low level DIE reading support. */
348e048f 6167
d85a05f0
DJ
6168/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6169
6170static void
6171init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6172 struct dwarf2_cu *cu,
3019eac3 6173 struct dwarf2_section_info *section,
685af9cd
TT
6174 struct dwo_file *dwo_file,
6175 struct abbrev_table *abbrev_table)
d85a05f0 6176{
fceca515 6177 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6178 reader->abfd = section->get_bfd_owner ();
d85a05f0 6179 reader->cu = cu;
3019eac3 6180 reader->dwo_file = dwo_file;
dee91e82
DE
6181 reader->die_section = section;
6182 reader->buffer = section->buffer;
f664829e 6183 reader->buffer_end = section->buffer + section->size;
685af9cd 6184 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6185}
6186
c0ab21c2 6187/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6188 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6189 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6190 already.
6191
6192 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6193 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6194 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6195 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6196 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6197 STUB_COMP_DIR may be non-NULL.
3e225074 6198 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6199 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6200 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6201 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6202 kept around for at least as long as *RESULT_READER.
6203
b0c7bfa9
DE
6204 The result is non-zero if a valid (non-dummy) DIE was found. */
6205
6206static int
4ab09049 6207read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6208 struct dwo_unit *dwo_unit,
b0c7bfa9 6209 struct die_info *stub_comp_unit_die,
a2ce51a0 6210 const char *stub_comp_dir,
b0c7bfa9 6211 struct die_reader_specs *result_reader,
d521ce57 6212 const gdb_byte **result_info_ptr,
b0c7bfa9 6213 struct die_info **result_comp_unit_die,
685af9cd 6214 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6215{
976ca316 6216 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6217 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6218 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6219 bfd *abfd;
d521ce57 6220 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6221 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6222 int i,num_extra_attrs;
6223 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6224 struct die_info *comp_unit_die;
6225
b0aeadb3
DE
6226 /* At most one of these may be provided. */
6227 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6228
b0c7bfa9
DE
6229 /* These attributes aren't processed until later:
6230 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6231 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6232 referenced later. However, these attributes are found in the stub
6233 which we won't have later. In order to not impose this complication
6234 on the rest of the code, we read them here and copy them to the
6235 DWO CU/TU die. */
b0c7bfa9
DE
6236
6237 stmt_list = NULL;
6238 low_pc = NULL;
6239 high_pc = NULL;
6240 ranges = NULL;
6241 comp_dir = NULL;
6242
6243 if (stub_comp_unit_die != NULL)
6244 {
6245 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6246 DWO file. */
4ab09049 6247 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6248 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6249 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6250 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6251 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6252 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6253
a39fdb41 6254 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6255
2b0c7f41
SM
6256 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6257 We need the value before we can process DW_AT_ranges values from the
6258 DWO. */
6259 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6260
6261 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6262 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6263 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6264 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6265 section. */
6266 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6267 }
a2ce51a0
DE
6268 else if (stub_comp_dir != NULL)
6269 {
6270 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6271 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6272 comp_dir->name = DW_AT_comp_dir;
6273 comp_dir->form = DW_FORM_string;
c6481205 6274 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6275 }
b0c7bfa9
DE
6276
6277 /* Set up for reading the DWO CU/TU. */
6278 cu->dwo_unit = dwo_unit;
685af9cd 6279 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6280 section->read (objfile);
6281 abfd = section->get_bfd_owner ();
9c541725
PA
6282 begin_info_ptr = info_ptr = (section->buffer
6283 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6284 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6285
4ab09049 6286 if (per_cu->is_debug_types)
b0c7bfa9 6287 {
4ab09049 6288 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6289
976ca316
SM
6290 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6291 section, dwo_abbrev_section,
43988095 6292 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6293 /* This is not an assert because it can be caused by bad debug info. */
43988095 6294 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6295 {
6296 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6297 " TU at offset %s [in module %s]"),
a2ce51a0 6298 hex_string (sig_type->signature),
43988095 6299 hex_string (cu->header.signature),
9d8780f0 6300 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6301 bfd_get_filename (abfd));
6302 }
9c541725 6303 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6304 /* For DWOs coming from DWP files, we don't know the CU length
6305 nor the type's offset in the TU until now. */
4057dfde 6306 dwo_unit->length = cu->header.get_length ();
9c541725 6307 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6308
6309 /* Establish the type offset that can be used to lookup the type.
6310 For DWO files, we don't know it until now. */
9c541725
PA
6311 sig_type->type_offset_in_section
6312 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6313 }
6314 else
6315 {
976ca316
SM
6316 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6317 section, dwo_abbrev_section,
43988095 6318 info_ptr, rcuh_kind::COMPILE);
9c541725 6319 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6320 /* For DWOs coming from DWP files, we don't know the CU length
6321 until now. */
4057dfde 6322 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6323 }
6324
606decb2 6325 dwo_abbrev_section->read (objfile);
685af9cd 6326 *result_dwo_abbrev_table
606decb2 6327 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6328 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6329 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6330
6331 /* Read in the die, but leave space to copy over the attributes
6332 from the stub. This has the benefit of simplifying the rest of
6333 the code - all the work to maintain the illusion of a single
6334 DW_TAG_{compile,type}_unit DIE is done here. */
6335 num_extra_attrs = ((stmt_list != NULL)
6336 + (low_pc != NULL)
6337 + (high_pc != NULL)
6338 + (ranges != NULL)
6339 + (comp_dir != NULL));
6340 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6341 num_extra_attrs);
b0c7bfa9
DE
6342
6343 /* Copy over the attributes from the stub to the DIE we just read in. */
6344 comp_unit_die = *result_comp_unit_die;
6345 i = comp_unit_die->num_attrs;
6346 if (stmt_list != NULL)
6347 comp_unit_die->attrs[i++] = *stmt_list;
6348 if (low_pc != NULL)
6349 comp_unit_die->attrs[i++] = *low_pc;
6350 if (high_pc != NULL)
6351 comp_unit_die->attrs[i++] = *high_pc;
6352 if (ranges != NULL)
6353 comp_unit_die->attrs[i++] = *ranges;
6354 if (comp_dir != NULL)
6355 comp_unit_die->attrs[i++] = *comp_dir;
6356 comp_unit_die->num_attrs += num_extra_attrs;
6357
b4f54984 6358 if (dwarf_die_debug)
bf6af496
DE
6359 {
6360 fprintf_unfiltered (gdb_stdlog,
6361 "Read die from %s@0x%x of %s:\n",
96b79293 6362 section->get_name (),
bf6af496
DE
6363 (unsigned) (begin_info_ptr - section->buffer),
6364 bfd_get_filename (abfd));
b4f54984 6365 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6366 }
6367
b0c7bfa9
DE
6368 /* Skip dummy compilation units. */
6369 if (info_ptr >= begin_info_ptr + dwo_unit->length
6370 || peek_abbrev_code (abfd, info_ptr) == 0)
6371 return 0;
6372
6373 *result_info_ptr = info_ptr;
6374 return 1;
6375}
6376
a084a2a6
AT
6377/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6378 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6379 signature is part of the header. */
6380static gdb::optional<ULONGEST>
6381lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6382{
6383 if (cu->header.version >= 5)
6384 return cu->header.signature;
6385 struct attribute *attr;
6386 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 6387 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 6388 return gdb::optional<ULONGEST> ();
529908cb 6389 return attr->as_unsigned ();
a084a2a6
AT
6390}
6391
c0ab21c2 6392/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6393 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6394 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6395
6396static struct dwo_unit *
4ab09049 6397lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6398{
4ab09049 6399 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6400 struct dwo_unit *dwo_unit;
c0ab21c2 6401 const char *comp_dir;
b0c7bfa9 6402
a2ce51a0
DE
6403 gdb_assert (cu != NULL);
6404
b0c7bfa9 6405 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6406 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6407 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6408
4ab09049
SM
6409 if (per_cu->is_debug_types)
6410 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6411 else
6412 {
a084a2a6 6413 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6414
a084a2a6 6415 if (!signature.has_value ())
b0c7bfa9
DE
6416 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6417 " [in module %s]"),
4ab09049
SM
6418 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6419
6420 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6421 }
6422
b0c7bfa9
DE
6423 return dwo_unit;
6424}
6425
c0ab21c2 6426/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6427 See it for a description of the parameters.
fcd3b13d 6428 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6429
c0ab21c2 6430void
9e021579
SM
6431cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6432 dwarf2_per_objfile *per_objfile,
2e671100 6433 dwarf2_cu *existing_cu)
a2ce51a0 6434{
a2ce51a0 6435 struct signatured_type *sig_type;
a2ce51a0
DE
6436
6437 /* Verify we can do the following downcast, and that we have the
6438 data we need. */
6439 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6440 sig_type = (struct signatured_type *) this_cu;
6441 gdb_assert (sig_type->dwo_unit != NULL);
6442
2e671100
SM
6443 dwarf2_cu *cu;
6444
6445 if (existing_cu != nullptr)
6aa5f3a6 6446 {
2e671100
SM
6447 cu = existing_cu;
6448 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6449 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6450 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6451 }
6452 else
6453 {
7188ed02 6454 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6455 in per_objfile yet. */
7188ed02 6456 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6457 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6458 cu = m_new_cu.get ();
6aa5f3a6
DE
6459 }
6460
6461 /* A future optimization, if needed, would be to use an existing
6462 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6463 could share abbrev tables. */
a2ce51a0 6464
2e671100 6465 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6466 NULL /* stub_comp_unit_die */,
6467 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6468 this, &info_ptr,
3e225074 6469 &comp_unit_die,
c0ab21c2 6470 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6471 {
6472 /* Dummy die. */
c0ab21c2 6473 dummy_p = true;
a2ce51a0 6474 }
a2ce51a0
DE
6475}
6476
fd820528 6477/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6478 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6479
f4dc4d17
DE
6480 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6481 Otherwise the table specified in the comp unit header is read in and used.
6482 This is an optimization for when we already have the abbrev table.
6483
2e671100
SM
6484 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6485 allocated. */
aaa75496 6486
ab432490 6487cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6488 dwarf2_per_objfile *per_objfile,
c0ab21c2 6489 struct abbrev_table *abbrev_table,
2e671100 6490 dwarf2_cu *existing_cu,
c0ab21c2
TT
6491 bool skip_partial)
6492 : die_reader_specs {},
6751ebae 6493 m_this_cu (this_cu)
c906108c 6494{
976ca316 6495 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6496 struct dwarf2_section_info *section = this_cu->section;
96b79293 6497 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 6498 const gdb_byte *begin_info_ptr;
dee91e82 6499 struct signatured_type *sig_type = NULL;
4bdcc0c1 6500 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6501 /* Non-zero if CU currently points to a DWO file and we need to
6502 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6503 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6504 int rereading_dwo_cu = 0;
c906108c 6505
b4f54984 6506 if (dwarf_die_debug)
9d8780f0 6507 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6508 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6509 sect_offset_str (this_cu->sect_off));
09406207 6510
a2ce51a0
DE
6511 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6512 file (instead of going through the stub), short-circuit all of this. */
6513 if (this_cu->reading_dwo_directly)
6514 {
6515 /* Narrow down the scope of possibilities to have to understand. */
6516 gdb_assert (this_cu->is_debug_types);
6517 gdb_assert (abbrev_table == NULL);
976ca316 6518 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
6519 return;
6520 }
6521
dee91e82 6522 /* This is cheap if the section is already read in. */
96b79293 6523 section->read (objfile);
dee91e82 6524
9c541725 6525 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6526
6527 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 6528
2e671100
SM
6529 dwarf2_cu *cu;
6530
6531 if (existing_cu != nullptr)
dee91e82 6532 {
2e671100 6533 cu = existing_cu;
42e7ad6c
DE
6534 /* If this CU is from a DWO file we need to start over, we need to
6535 refetch the attributes from the skeleton CU.
6536 This could be optimized by retrieving those attributes from when we
6537 were here the first time: the previous comp_unit_die was stored in
6538 comp_unit_obstack. But there's no data yet that we need this
6539 optimization. */
6540 if (cu->dwo_unit != NULL)
6541 rereading_dwo_cu = 1;
dee91e82
DE
6542 }
6543 else
6544 {
7188ed02 6545 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6546 in per_objfile yet. */
976ca316
SM
6547 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6548 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 6549 cu = m_new_cu.get ();
42e7ad6c 6550 }
dee91e82 6551
b0c7bfa9 6552 /* Get the header. */
9c541725 6553 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6554 {
6555 /* We already have the header, there's no need to read it in again. */
9c541725 6556 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6557 }
6558 else
6559 {
3019eac3 6560 if (this_cu->is_debug_types)
dee91e82 6561 {
976ca316
SM
6562 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6563 section, abbrev_section,
6564 info_ptr, rcuh_kind::TYPE);
dee91e82 6565
42e7ad6c
DE
6566 /* Since per_cu is the first member of struct signatured_type,
6567 we can go from a pointer to one to a pointer to the other. */
6568 sig_type = (struct signatured_type *) this_cu;
43988095 6569 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6570 gdb_assert (sig_type->type_offset_in_tu
6571 == cu->header.type_cu_offset_in_tu);
6572 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6573
42e7ad6c
DE
6574 /* LENGTH has not been set yet for type units if we're
6575 using .gdb_index. */
4057dfde 6576 this_cu->length = cu->header.get_length ();
3019eac3
DE
6577
6578 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6579 sig_type->type_offset_in_section =
6580 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6581
6582 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6583 }
6584 else
6585 {
976ca316
SM
6586 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6587 section, abbrev_section,
43988095
JK
6588 info_ptr,
6589 rcuh_kind::COMPILE);
dee91e82 6590
9c541725 6591 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
6592 if (this_cu->length == 0)
6593 this_cu->length = cu->header.get_length ();
6594 else
6595 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6596 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6597 }
6598 }
10b3939b 6599
6caca83c 6600 /* Skip dummy compilation units. */
dee91e82 6601 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6602 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6603 {
6604 dummy_p = true;
6605 return;
6606 }
6caca83c 6607
433df2d4
DE
6608 /* If we don't have them yet, read the abbrevs for this compilation unit.
6609 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6610 done. */
f4dc4d17 6611 if (abbrev_table != NULL)
685af9cd
TT
6612 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6613 else
f4dc4d17 6614 {
606decb2 6615 abbrev_section->read (objfile);
c0ab21c2 6616 m_abbrev_table_holder
606decb2 6617 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 6618 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6619 }
af703f96 6620
dee91e82 6621 /* Read the top level CU/TU die. */
c0ab21c2 6622 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6623 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6624
58f0c718 6625 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6626 {
6627 dummy_p = true;
6628 return;
6629 }
58f0c718 6630
b0c7bfa9 6631 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6632 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6633 table from the DWO file and pass the ownership over to us. It will be
6634 referenced from READER, so we must make sure to free it after we're done
6635 with READER.
6636
b0c7bfa9
DE
6637 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6638 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6639 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6640 if (dwo_name != nullptr)
3019eac3 6641 {
3019eac3 6642 struct dwo_unit *dwo_unit;
b0c7bfa9 6643 struct die_info *dwo_comp_unit_die;
3019eac3 6644
3e225074 6645 if (comp_unit_die->has_children)
6a506a2d 6646 {
b98664d3 6647 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6648 " has children (offset %s) [in module %s]"),
6649 sect_offset_str (this_cu->sect_off),
6650 bfd_get_filename (abfd));
6a506a2d 6651 }
4ab09049 6652 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 6653 if (dwo_unit != NULL)
3019eac3 6654 {
4ab09049 6655 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 6656 comp_unit_die, NULL,
c0ab21c2 6657 this, &info_ptr,
3e225074 6658 &dwo_comp_unit_die,
c0ab21c2 6659 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6660 {
6661 /* Dummy die. */
c0ab21c2 6662 dummy_p = true;
6a506a2d
DE
6663 return;
6664 }
6665 comp_unit_die = dwo_comp_unit_die;
6666 }
6667 else
6668 {
6669 /* Yikes, we couldn't find the rest of the DIE, we only have
6670 the stub. A complaint has already been logged. There's
6671 not much more we can do except pass on the stub DIE to
6672 die_reader_func. We don't want to throw an error on bad
6673 debug info. */
3019eac3
DE
6674 }
6675 }
c0ab21c2 6676}
3019eac3 6677
6751ebae
TT
6678void
6679cutu_reader::keep ()
c0ab21c2 6680{
b0c7bfa9 6681 /* Done, clean up. */
6751ebae
TT
6682 gdb_assert (!dummy_p);
6683 if (m_new_cu != NULL)
348e048f 6684 {
7188ed02 6685 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 6686 now. */
7188ed02
SM
6687 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6688 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 6689 }
dee91e82
DE
6690}
6691
18a8505e
AT
6692/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6693 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6694 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6695
6696 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6697 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6698
6699 We fill in THIS_CU->length.
6700
dee91e82 6701 THIS_CU->cu is always freed when done.
3019eac3 6702 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6703 to care whether it refers to the "main" CU or the DWO CU.
6704
6705 When parent_cu is passed, it is used to provide a default value for
6706 str_offsets_base and addr_base from the parent. */
dee91e82 6707
ab432490 6708cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6709 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
6710 struct dwarf2_cu *parent_cu,
6711 struct dwo_file *dwo_file)
6712 : die_reader_specs {},
6713 m_this_cu (this_cu)
dee91e82 6714{
976ca316 6715 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6716 struct dwarf2_section_info *section = this_cu->section;
96b79293 6717 bfd *abfd = section->get_bfd_owner ();
33e80786 6718 struct dwarf2_section_info *abbrev_section;
d521ce57 6719 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6720
b4f54984 6721 if (dwarf_die_debug)
9d8780f0 6722 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6723 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6724 sect_offset_str (this_cu->sect_off));
09406207 6725
976ca316 6726 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 6727
33e80786
DE
6728 abbrev_section = (dwo_file != NULL
6729 ? &dwo_file->sections.abbrev
6730 : get_abbrev_section_for_cu (this_cu));
6731
dee91e82 6732 /* This is cheap if the section is already read in. */
96b79293 6733 section->read (objfile);
dee91e82 6734
976ca316 6735 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 6736
9c541725 6737 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
6738 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6739 section, abbrev_section, info_ptr,
43988095
JK
6740 (this_cu->is_debug_types
6741 ? rcuh_kind::TYPE
6742 : rcuh_kind::COMPILE));
dee91e82 6743
18a8505e
AT
6744 if (parent_cu != nullptr)
6745 {
c0ab21c2
TT
6746 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6747 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 6748 }
4057dfde 6749 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
6750
6751 /* Skip dummy compilation units. */
6752 if (info_ptr >= begin_info_ptr + this_cu->length
6753 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6754 {
6755 dummy_p = true;
6756 return;
6757 }
72bf9492 6758
606decb2 6759 abbrev_section->read (objfile);
c0ab21c2 6760 m_abbrev_table_holder
606decb2 6761 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 6762
c0ab21c2
TT
6763 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6764 m_abbrev_table_holder.get ());
3e225074 6765 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
6766}
6767
0018ea6f
DE
6768\f
6769/* Type Unit Groups.
dee91e82 6770
0018ea6f
DE
6771 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6772 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6773 so that all types coming from the same compilation (.o file) are grouped
6774 together. A future step could be to put the types in the same symtab as
6775 the CU the types ultimately came from. */
ff013f42 6776
f4dc4d17
DE
6777static hashval_t
6778hash_type_unit_group (const void *item)
6779{
9a3c8263
SM
6780 const struct type_unit_group *tu_group
6781 = (const struct type_unit_group *) item;
f4dc4d17 6782
094b34ac 6783 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6784}
348e048f
DE
6785
6786static int
f4dc4d17 6787eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6788{
9a3c8263
SM
6789 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6790 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6791
094b34ac 6792 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6793}
348e048f 6794
f4dc4d17
DE
6795/* Allocate a hash table for type unit groups. */
6796
eaa5fa8b 6797static htab_up
298e9637 6798allocate_type_unit_groups_table ()
f4dc4d17 6799{
eaa5fa8b
TT
6800 return htab_up (htab_create_alloc (3,
6801 hash_type_unit_group,
6802 eq_type_unit_group,
ef5f598c 6803 htab_delete_entry<type_unit_group>,
0d305d5c 6804 xcalloc, xfree));
f4dc4d17 6805}
dee91e82 6806
f4dc4d17
DE
6807/* Type units that don't have DW_AT_stmt_list are grouped into their own
6808 partial symtabs. We combine several TUs per psymtab to not let the size
6809 of any one psymtab grow too big. */
6810#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6811#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6812
094b34ac 6813/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6814 Create the type_unit_group object used to hold one or more TUs. */
6815
0d305d5c 6816static std::unique_ptr<type_unit_group>
094b34ac 6817create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 6818{
976ca316
SM
6819 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6820 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 6821
0d305d5c
TT
6822 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6823 tu_group->per_bfd = per_bfd;
f4dc4d17 6824
1859c670 6825 if (per_bfd->using_index)
094b34ac 6826 {
0d305d5c
TT
6827 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6828 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6829 }
6830 else
6831 {
9c541725 6832 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 6833 dwarf2_psymtab *pst;
528e1572 6834 std::string name;
094b34ac
DE
6835
6836 /* Give the symtab a useful name for debug purposes. */
6837 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
6838 name = string_printf ("<type_units_%d>",
6839 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 6840 else
528e1572 6841 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 6842
0d305d5c
TT
6843 pst = create_partial_symtab (tu_group.get (), per_objfile,
6844 name.c_str ());
6d94535f 6845 pst->anonymous = true;
094b34ac 6846 }
f4dc4d17 6847
094b34ac 6848 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6849 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6850
6851 return tu_group;
6852}
6853
094b34ac
DE
6854/* Look up the type_unit_group for type unit CU, and create it if necessary.
6855 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6856
6857static struct type_unit_group *
ff39bb5e 6858get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 6859{
976ca316
SM
6860 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6861 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
6862 struct type_unit_group *tu_group;
6863 void **slot;
6864 unsigned int line_offset;
6865 struct type_unit_group type_unit_group_for_lookup;
6866
976ca316
SM
6867 if (per_objfile->per_bfd->type_unit_groups == NULL)
6868 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
6869
6870 /* Do we need to create a new group, or can we use an existing one? */
6871
529908cb 6872 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 6873 {
529908cb 6874 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
6875 ++tu_stats->nr_symtab_sharers;
6876 }
6877 else
6878 {
6879 /* Ugh, no stmt_list. Rare, but we have to handle it.
6880 We can do various things here like create one group per TU or
6881 spread them over multiple groups to split up the expansion work.
6882 To avoid worst case scenarios (too many groups or too large groups)
6883 we, umm, group them in bunches. */
6884 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6885 | (tu_stats->nr_stmt_less_type_units
6886 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6887 ++tu_stats->nr_stmt_less_type_units;
6888 }
6889
094b34ac 6890 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6891 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 6892 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17 6893 &type_unit_group_for_lookup, INSERT);
0d305d5c 6894 if (*slot == nullptr)
f4dc4d17 6895 {
9c541725 6896 sect_offset line_offset_struct = (sect_offset) line_offset;
0d305d5c
TT
6897 std::unique_ptr<type_unit_group> grp
6898 = create_type_unit_group (cu, line_offset_struct);
6899 *slot = grp.release ();
f4dc4d17
DE
6900 ++tu_stats->nr_symtabs;
6901 }
6902
0d305d5c
TT
6903 tu_group = (struct type_unit_group *) *slot;
6904 gdb_assert (tu_group != nullptr);
f4dc4d17
DE
6905 return tu_group;
6906}
0018ea6f
DE
6907\f
6908/* Partial symbol tables. */
6909
6910/* Create a psymtab named NAME and assign it to PER_CU.
6911
6912 The caller must fill in the following details:
6913 dirname, textlow, texthigh. */
6914
891813be 6915static dwarf2_psymtab *
7aa104c4
SM
6916create_partial_symtab (dwarf2_per_cu_data *per_cu,
6917 dwarf2_per_objfile *per_objfile,
6918 const char *name)
0018ea6f 6919{
0072c873
SM
6920 dwarf2_psymtab *pst
6921 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6922 per_objfile->objfile->per_bfd, per_cu);
0018ea6f 6923
6d94535f 6924 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
6925
6926 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
6927 per_cu->v.psymtab = pst;
6928
6929 return pst;
6930}
6931
c0ab21c2 6932/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
6933
6934static void
6935process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6936 const gdb_byte *info_ptr,
0018ea6f 6937 struct die_info *comp_unit_die,
c0ab21c2 6938 enum language pretend_language)
0018ea6f
DE
6939{
6940 struct dwarf2_cu *cu = reader->cu;
7aa104c4 6941 dwarf2_per_objfile *per_objfile = cu->per_objfile;
84685904 6942 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7aa104c4 6943 struct objfile *objfile = per_objfile->objfile;
08feed99 6944 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 6945 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6946 CORE_ADDR baseaddr;
6947 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 6948 dwarf2_psymtab *pst;
3a2b436a 6949 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6950 const char *filename;
0018ea6f 6951
0018ea6f
DE
6952 gdb_assert (! per_cu->is_debug_types);
6953
c0ab21c2 6954 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 6955
0018ea6f 6956 /* Allocate a new partial symbol table structure. */
2e927613
TV
6957 gdb::unique_xmalloc_ptr<char> debug_filename;
6958 static const char artificial[] = "<artificial>";
7d45c7c3
KB
6959 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6960 if (filename == NULL)
0018ea6f 6961 filename = "";
2e927613
TV
6962 else if (strcmp (filename, artificial) == 0)
6963 {
6964 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
6965 sect_offset_str (per_cu->sect_off),
6966 (char *) NULL));
2e927613
TV
6967 filename = debug_filename.get ();
6968 }
0018ea6f 6969
7aa104c4 6970 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
6971
6972 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6973 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 6974
b3b3bada 6975 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
6976
6977 dwarf2_find_base_address (comp_unit_die, cu);
6978
6979 /* Possibly set the default values of LOWPC and HIGHPC from
6980 `DW_AT_ranges'. */
3a2b436a
JK
6981 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6982 &best_highpc, cu, pst);
6983 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
6984 {
6985 CORE_ADDR low
6986 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
6987 - baseaddr);
6988 CORE_ADDR high
6989 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
6990 - baseaddr - 1);
6991 /* Store the contiguous range if it is not empty; it can be
6992 empty for CUs with no code. */
84685904 6993 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 6994 low, high, pst);
79748972 6995 }
0018ea6f
DE
6996
6997 /* Check if comp unit has_children.
6998 If so, read the rest of the partial symbols from this comp unit.
6999 If not, there's no more debug_info for this comp unit. */
3e225074 7000 if (comp_unit_die->has_children)
0018ea6f
DE
7001 {
7002 struct partial_die_info *first_die;
7003 CORE_ADDR lowpc, highpc;
7004
7005 lowpc = ((CORE_ADDR) -1);
7006 highpc = ((CORE_ADDR) 0);
7007
7008 first_die = load_partial_dies (reader, info_ptr, 1);
7009
7010 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7011 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7012
7013 /* If we didn't find a lowpc, set it to highpc to avoid
7014 complaints from `maint check'. */
7015 if (lowpc == ((CORE_ADDR) -1))
7016 lowpc = highpc;
7017
7018 /* If the compilation unit didn't have an explicit address range,
7019 then use the information extracted from its child dies. */
e385593e 7020 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7021 {
7022 best_lowpc = lowpc;
7023 best_highpc = highpc;
7024 }
7025 }
4ae976d1 7026 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7027 best_lowpc + baseaddr)
7028 - baseaddr);
4ae976d1 7029 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7030 best_highpc + baseaddr)
7031 - baseaddr);
0018ea6f 7032
ae7754b2 7033 pst->end ();
0018ea6f 7034
ae640021 7035 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7036 {
7037 int i;
ae640021 7038 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7039
7040 /* Fill in 'dependencies' here; we fill in 'users' in a
7041 post-pass. */
7042 pst->number_of_dependencies = len;
a9342b62 7043 pst->dependencies
84685904 7044 = per_bfd->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7045 for (i = 0; i < len; ++i)
7046 {
7047 pst->dependencies[i]
7048 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7049 }
0018ea6f 7050
ae640021 7051 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7052 }
7053
7054 /* Get the list of files included in the current compilation unit,
7055 and build a psymtab for each of them. */
7056 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7057
6f738b01
SM
7058 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7059 ", %d global, %d static syms",
7060 per_cu->is_debug_types ? "type" : "comp",
7061 sect_offset_str (per_cu->sect_off),
7062 paddress (gdbarch, pst->text_low (objfile)),
7063 paddress (gdbarch, pst->text_high (objfile)),
7064 (int) pst->global_psymbols.size (),
7065 (int) pst->static_psymbols.size ());
0018ea6f
DE
7066}
7067
7068/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7069 Process compilation unit THIS_CU for a psymtab. */
7070
7071static void
ab432490
SM
7072process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7073 dwarf2_per_objfile *per_objfile,
135f5437 7074 bool want_partial_unit,
b93601f3 7075 enum language pretend_language)
0018ea6f
DE
7076{
7077 /* If this compilation unit was already read in, free the
7078 cached copy in order to read it in again. This is
7079 necessary because we skipped some symbols when we first
7080 read in the compilation unit (see load_partial_dies).
7081 This problem could be avoided, but the benefit is unclear. */
7188ed02 7082 per_objfile->remove_cu (this_cu);
0018ea6f 7083
2e671100 7084 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7085
248f7165
TV
7086 if (reader.comp_unit_die == nullptr)
7087 return;
7088
58990295
TV
7089 switch (reader.comp_unit_die->tag)
7090 {
7091 case DW_TAG_compile_unit:
7092 this_cu->unit_type = DW_UT_compile;
7093 break;
7094 case DW_TAG_partial_unit:
7095 this_cu->unit_type = DW_UT_partial;
7096 break;
e77b0004
TV
7097 case DW_TAG_type_unit:
7098 this_cu->unit_type = DW_UT_type;
7099 break;
58990295 7100 default:
702cf3f5
AB
7101 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7102 dwarf_tag_name (reader.comp_unit_die->tag),
7103 sect_offset_str (reader.cu->per_cu->sect_off),
7104 objfile_name (per_objfile->objfile));
58990295
TV
7105 }
7106
c0ab21c2 7107 if (reader.dummy_p)
f1902523 7108 {
c0ab21c2 7109 /* Nothing. */
f1902523 7110 }
c0ab21c2 7111 else if (this_cu->is_debug_types)
3e225074
TT
7112 build_type_psymtabs_reader (&reader, reader.info_ptr,
7113 reader.comp_unit_die);
135f5437
TT
7114 else if (want_partial_unit
7115 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7116 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7117 reader.comp_unit_die,
c0ab21c2 7118 pretend_language);
0018ea6f
DE
7119
7120 /* Age out any secondary CUs. */
7188ed02 7121 per_objfile->age_comp_units ();
0018ea6f 7122}
f4dc4d17
DE
7123
7124/* Reader function for build_type_psymtabs. */
7125
7126static void
7127build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7128 const gdb_byte *info_ptr,
3e225074 7129 struct die_info *type_unit_die)
f4dc4d17 7130{
976ca316 7131 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7132 struct dwarf2_cu *cu = reader->cu;
7133 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7134 struct signatured_type *sig_type;
f4dc4d17
DE
7135 struct type_unit_group *tu_group;
7136 struct attribute *attr;
7137 struct partial_die_info *first_die;
7138 CORE_ADDR lowpc, highpc;
891813be 7139 dwarf2_psymtab *pst;
f4dc4d17 7140
0186c6a7
DE
7141 gdb_assert (per_cu->is_debug_types);
7142 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7143
3e225074 7144 if (! type_unit_die->has_children)
f4dc4d17
DE
7145 return;
7146
052c8bb8 7147 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7148 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7149
df07e2c7 7150 if (tu_group->tus == nullptr)
a8b3b8e9 7151 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7152 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7153
7154 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7155 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7156 pst->anonymous = true;
f4dc4d17
DE
7157
7158 first_die = load_partial_dies (reader, info_ptr, 1);
7159
7160 lowpc = (CORE_ADDR) -1;
7161 highpc = (CORE_ADDR) 0;
7162 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7163
ae7754b2 7164 pst->end ();
f4dc4d17
DE
7165}
7166
73051182
DE
7167/* Struct used to sort TUs by their abbreviation table offset. */
7168
7169struct tu_abbrev_offset
7170{
b2bdb8cf
SM
7171 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7172 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7173 {}
7174
16e0020b 7175 /* This is used when sorting. */
5433e20e 7176 bool operator< (const tu_abbrev_offset &other) const
16e0020b
TT
7177 {
7178 return abbrev_offset < other.abbrev_offset;
7179 }
7180
b2bdb8cf 7181 signatured_type *sig_type;
73051182
DE
7182 sect_offset abbrev_offset;
7183};
7184
73051182 7185/* Efficiently read all the type units.
73051182
DE
7186
7187 The efficiency is because we sort TUs by the abbrev table they use and
7188 only read each abbrev table once. In one program there are 200K TUs
7189 sharing 8K abbrev tables.
7190
7191 The main purpose of this function is to support building the
5989a64e 7192 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7193 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7194 can collapse the search space by grouping them by stmt_list.
7195 The savings can be significant, in the same program from above the 200K TUs
7196 share 8K stmt_list tables.
7197
7198 FUNC is expected to call get_type_unit_group, which will create the
7199 struct type_unit_group if necessary and add it to
5989a64e 7200 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7201
7202static void
b8efb248 7203build_type_psymtabs (dwarf2_per_objfile *per_objfile)
73051182 7204{
976ca316 7205 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7206 abbrev_table_up abbrev_table;
73051182 7207 sect_offset abbrev_offset;
73051182
DE
7208
7209 /* It's up to the caller to not call us multiple times. */
976ca316 7210 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7211
91eea9cc 7212 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
73051182
DE
7213 return;
7214
7215 /* TUs typically share abbrev tables, and there can be way more TUs than
7216 abbrev tables. Sort by abbrev table to reduce the number of times we
7217 read each abbrev table in.
7218 Alternatives are to punt or to maintain a cache of abbrev tables.
7219 This is simpler and efficient enough for now.
7220
7221 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7222 symtab to use). Typically TUs with the same abbrev offset have the same
7223 stmt_list value too so in practice this should work well.
7224
7225 The basic algorithm here is:
7226
7227 sort TUs by abbrev table
7228 for each TU with same abbrev table:
7229 read abbrev table if first user
7230 read TU top level DIE
7231 [IWBN if DWO skeletons had DW_AT_stmt_list]
7232 call FUNC */
7233
6f738b01 7234 dwarf_read_debug_printf ("Building type unit groups ...");
73051182 7235
91eea9cc 7236 /* Sort in a separate table to maintain the order of all_comp_units
73051182 7237 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7238 std::vector<tu_abbrev_offset> sorted_by_abbrev;
91eea9cc 7239 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
b2bdb8cf 7240
91eea9cc
TT
7241 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7242 {
7243 if (cu->is_debug_types)
7244 {
7245 auto sig_type = static_cast<signatured_type *> (cu.get ());
7246 sorted_by_abbrev.emplace_back
7247 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7248 sig_type->sect_off));
7249 }
7250 }
73051182 7251
16e0020b 7252 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
73051182 7253
9c541725 7254 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7255
b2bdb8cf 7256 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7257 {
73051182
DE
7258 /* Switch to the next abbrev table if necessary. */
7259 if (abbrev_table == NULL
b2bdb8cf 7260 || tu.abbrev_offset != abbrev_offset)
73051182 7261 {
b2bdb8cf 7262 abbrev_offset = tu.abbrev_offset;
606decb2 7263 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7264 abbrev_table =
606decb2 7265 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7266 ++tu_stats->nr_uniq_abbrev_tables;
7267 }
7268
0d305d5c 7269 cutu_reader reader (tu.sig_type, per_objfile,
2e671100 7270 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7271 if (!reader.dummy_p)
7272 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7273 reader.comp_unit_die);
73051182 7274 }
6aa5f3a6 7275}
73051182 7276
6aa5f3a6
DE
7277/* Print collected type unit statistics. */
7278
7279static void
976ca316 7280print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7281{
976ca316 7282 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7283
6f738b01 7284 dwarf_read_debug_printf ("Type unit statistics:");
91eea9cc 7285 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
6f738b01
SM
7286 dwarf_read_debug_printf (" %d uniq abbrev tables",
7287 tu_stats->nr_uniq_abbrev_tables);
7288 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7289 tu_stats->nr_symtabs);
7290 dwarf_read_debug_printf (" %d symtab sharers",
7291 tu_stats->nr_symtab_sharers);
7292 dwarf_read_debug_printf (" %d type units without a stmt_list",
7293 tu_stats->nr_stmt_less_type_units);
7294 dwarf_read_debug_printf (" %d all_type_units reallocs",
7295 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7296}
7297
f4dc4d17
DE
7298/* Traversal function for build_type_psymtabs. */
7299
7300static int
7301build_type_psymtab_dependencies (void **slot, void *info)
7302{
976ca316 7303 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
84685904 7304 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 7305 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
0d305d5c 7306 dwarf2_psymtab *pst = tu_group->v.psymtab;
df07e2c7 7307 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7308 int i;
7309
7310 gdb_assert (len > 0);
0d305d5c 7311 gdb_assert (tu_group->type_unit_group_p ());
f4dc4d17
DE
7312
7313 pst->number_of_dependencies = len;
84685904 7314 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
df07e2c7 7315 for (i = 0; i < len; ++i)
f4dc4d17 7316 {
df07e2c7 7317 struct signatured_type *iter = tu_group->tus->at (i);
0d305d5c
TT
7318 gdb_assert (iter->is_debug_types);
7319 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 7320 iter->type_unit_group = tu_group;
f4dc4d17
DE
7321 }
7322
df07e2c7
AB
7323 delete tu_group->tus;
7324 tu_group->tus = nullptr;
348e048f
DE
7325
7326 return 1;
7327}
7328
6aa5f3a6
DE
7329/* Traversal function for process_skeletonless_type_unit.
7330 Read a TU in a DWO file and build partial symbols for it. */
7331
7332static int
7333process_skeletonless_type_unit (void **slot, void *info)
7334{
7335 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7336 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7337
7338 /* If this TU doesn't exist in the global table, add it and read it in. */
7339
976ca316
SM
7340 if (per_objfile->per_bfd->signatured_types == NULL)
7341 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6 7342
9ea36493 7343 signatured_type find_entry (dwo_unit->signature);
976ca316 7344 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7345 &find_entry, INSERT);
6aa5f3a6
DE
7346 /* If we've already seen this type there's nothing to do. What's happening
7347 is we're doing our own version of comdat-folding here. */
7348 if (*slot != NULL)
7349 return 1;
7350
b8efb248 7351 /* This does the job that create_all_comp_units would have done for
6aa5f3a6 7352 this TU. */
9ea36493
SM
7353 signatured_type *entry
7354 = add_type_unit (per_objfile, dwo_unit->signature, slot);
976ca316 7355 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7356 *slot = entry;
7357
b8efb248 7358 /* This does the job that build_type_psymtabs would have done. */
0d305d5c 7359 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7360 if (!reader.dummy_p)
7361 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7362 reader.comp_unit_die);
6aa5f3a6
DE
7363
7364 return 1;
7365}
7366
7367/* Traversal function for process_skeletonless_type_units. */
7368
7369static int
7370process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7371{
7372 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7373
7374 if (dwo_file->tus != NULL)
b0b6a987
TT
7375 htab_traverse_noresize (dwo_file->tus.get (),
7376 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7377
7378 return 1;
7379}
7380
7381/* Scan all TUs of DWO files, verifying we've processed them.
7382 This is needed in case a TU was emitted without its skeleton.
7383 Note: This can't be done until we know what all the DWO files are. */
7384
7385static void
976ca316 7386process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7387{
7388 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7389 if (get_dwp_file (per_objfile) == NULL
7390 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7391 {
976ca316 7392 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7393 process_dwo_file_for_skeletonless_type_units,
976ca316 7394 per_objfile);
6aa5f3a6 7395 }
348e048f
DE
7396}
7397
ed2dc618 7398/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7399
7400static void
976ca316 7401set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7402{
0d305d5c 7403 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7404 {
891813be 7405 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7406
36586728
TT
7407 if (pst == NULL)
7408 continue;
7409
b76e467d 7410 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7411 {
7412 /* Set the 'user' field only if it is not already set. */
7413 if (pst->dependencies[j]->user == NULL)
7414 pst->dependencies[j]->user = pst;
7415 }
7416 }
7417}
7418
93311388
DE
7419/* Build the partial symbol table by doing a quick pass through the
7420 .debug_info and .debug_abbrev sections. */
72bf9492 7421
93311388 7422static void
976ca316 7423dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7424{
976ca316 7425 struct objfile *objfile = per_objfile->objfile;
84685904 7426 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
93311388 7427
6f738b01
SM
7428 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7429 objfile_name (objfile));
45cfd468 7430
76935768 7431 scoped_restore restore_reading_psyms
bab287cd 7432 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
98bfdba5 7433
84685904 7434 per_bfd->info.read (objfile);
91c24f0a 7435
93311388
DE
7436 /* Any cached compilation units will be linked by the per-objfile
7437 read_in_chain. Make sure to free them when we're done. */
976ca316 7438 free_cached_comp_units freer (per_objfile);
72bf9492 7439
976ca316 7440 create_all_comp_units (per_objfile);
b8efb248 7441 build_type_psymtabs (per_objfile);
c906108c 7442
60606b2c
TT
7443 /* Create a temporary address map on a temporary obstack. We later
7444 copy this to the final obstack. */
8268c778 7445 auto_obstack temp_obstack;
791afaa2
TT
7446
7447 scoped_restore save_psymtabs_addrmap
84685904 7448 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
791afaa2 7449 addrmap_create_mutable (&temp_obstack));
72bf9492 7450
0d305d5c 7451 for (const auto &per_cu : per_bfd->all_comp_units)
3d5afab3
TV
7452 {
7453 if (per_cu->v.psymtab != NULL)
7454 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7455 continue;
0d305d5c 7456 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
ab432490 7457 language_minimal);
3d5afab3 7458 }
ff013f42 7459
6aa5f3a6 7460 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 7461 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
7462
7463 /* Now that all TUs have been processed we can fill in the dependencies. */
84685904 7464 if (per_bfd->type_unit_groups != NULL)
6aa5f3a6 7465 {
84685904 7466 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
976ca316 7467 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
7468 }
7469
6f738b01 7470 if (dwarf_read_debug > 0)
976ca316 7471 print_tu_stats (per_objfile);
6aa5f3a6 7472
976ca316 7473 set_partial_user (per_objfile);
95554aad 7474
84685904
TT
7475 per_bfd->partial_symtabs->psymtabs_addrmap
7476 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7477 per_bfd->partial_symtabs->obstack ());
791afaa2
TT
7478 /* At this point we want to keep the address map. */
7479 save_psymtabs_addrmap.release ();
ff013f42 7480
6f738b01
SM
7481 dwarf_read_debug_printf ("Done building psymtabs of %s",
7482 objfile_name (objfile));
ae038cb0
DJ
7483}
7484
dee91e82
DE
7485/* Load the partial DIEs for a secondary CU into memory.
7486 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7487
dee91e82 7488static void
ab432490 7489load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
7490 dwarf2_per_objfile *per_objfile,
7491 dwarf2_cu *existing_cu)
dee91e82 7492{
2e671100 7493 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
7494
7495 if (!reader.dummy_p)
7496 {
7497 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7498 language_minimal);
7499
7500 /* Check if comp unit has_children.
7501 If so, read the rest of the partial symbols from this comp unit.
7502 If not, there's no more debug_info for this comp unit. */
3e225074 7503 if (reader.comp_unit_die->has_children)
c0ab21c2 7504 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7505
7506 reader.keep ();
c0ab21c2 7507 }
ae038cb0
DJ
7508}
7509
ae038cb0 7510static void
976ca316 7511read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 7512 struct dwarf2_section_info *section,
f1902523 7513 struct dwarf2_section_info *abbrev_section,
b8efb248
TT
7514 unsigned int is_dwz,
7515 htab_up &types_htab,
7516 rcuh_kind section_kind)
ae038cb0 7517{
d521ce57 7518 const gdb_byte *info_ptr;
976ca316 7519 struct objfile *objfile = per_objfile->objfile;
be391dca 7520
6f738b01
SM
7521 dwarf_read_debug_printf ("Reading %s for %s",
7522 section->get_name (),
7523 section->get_file_name ());
bf6af496 7524
96b79293 7525 section->read (objfile);
ae038cb0 7526
36586728 7527 info_ptr = section->buffer;
6e70227d 7528
36586728 7529 while (info_ptr < section->buffer + section->size)
ae038cb0 7530 {
473ab964 7531 dwarf2_per_cu_data_up this_cu;
ae038cb0 7532
9c541725 7533 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7534
f1902523 7535 comp_unit_head cu_header;
976ca316 7536 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 7537 abbrev_section, info_ptr,
b8efb248 7538 section_kind);
ae038cb0
DJ
7539
7540 /* Save the compilation unit for later lookup. */
f1902523 7541 if (cu_header.unit_type != DW_UT_type)
976ca316 7542 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
7543 else
7544 {
b8efb248
TT
7545 if (types_htab == nullptr)
7546 types_htab = allocate_signatured_type_table ();
7547
9ea36493
SM
7548 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7549 (cu_header.signature);
b8efb248 7550 signatured_type *sig_ptr = sig_type.get ();
f1902523 7551 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
473ab964 7552 this_cu.reset (sig_type.release ());
b8efb248
TT
7553
7554 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7555 gdb_assert (slot != nullptr);
7556 if (*slot != nullptr)
7557 complaint (_("debug type entry at offset %s is duplicate to"
7558 " the entry at offset %s, signature %s"),
7559 sect_offset_str (sect_off),
7560 sect_offset_str (sig_ptr->sect_off),
7561 hex_string (sig_ptr->signature));
7562 *slot = sig_ptr;
f1902523 7563 }
9c541725 7564 this_cu->sect_off = sect_off;
f1902523 7565 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7566 this_cu->is_dwz = is_dwz;
8a0459fd 7567 this_cu->section = section;
ae038cb0 7568
ae038cb0 7569 info_ptr = info_ptr + this_cu->length;
0d305d5c 7570 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
ae038cb0 7571 }
36586728
TT
7572}
7573
7574/* Create a list of all compilation units in OBJFILE.
7575 This is only done for -readnow and building partial symtabs. */
7576
7577static void
976ca316 7578create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 7579{
b8efb248
TT
7580 htab_up types_htab;
7581
976ca316 7582 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
b8efb248
TT
7583 &per_objfile->per_bfd->abbrev, 0,
7584 types_htab, rcuh_kind::COMPILE);
7585 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7586 read_comp_units_from_section (per_objfile, &section,
7587 &per_objfile->per_bfd->abbrev, 0,
7588 types_htab, rcuh_kind::TYPE);
36586728 7589
976ca316 7590 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 7591 if (dwz != NULL)
b8efb248
TT
7592 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7593 types_htab, rcuh_kind::COMPILE);
7594
7595 per_objfile->per_bfd->signatured_types = std::move (types_htab);
c906108c
SS
7596}
7597
5734ee8b 7598/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7599 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7600 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7601 DW_AT_ranges). See the comments of add_partial_subprogram on how
7602 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7603
72bf9492
DJ
7604static void
7605scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7606 CORE_ADDR *highpc, int set_addrmap,
7607 struct dwarf2_cu *cu)
c906108c 7608{
72bf9492 7609 struct partial_die_info *pdi;
c906108c 7610
91c24f0a
DC
7611 /* Now, march along the PDI's, descending into ones which have
7612 interesting children but skipping the children of the other ones,
7613 until we reach the end of the compilation unit. */
c906108c 7614
72bf9492 7615 pdi = first_die;
91c24f0a 7616
72bf9492
DJ
7617 while (pdi != NULL)
7618 {
52356b79 7619 pdi->fixup (cu);
c906108c 7620
f55ee35c 7621 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7622 children, so we need to look at them. Ditto for anonymous
7623 enums. */
933c6fe4 7624
7d00ffec 7625 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7626 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7627 || pdi->tag == DW_TAG_imported_unit
7628 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7629 {
72bf9492 7630 switch (pdi->tag)
c906108c
SS
7631 {
7632 case DW_TAG_subprogram:
b1dc1806 7633 case DW_TAG_inlined_subroutine:
cdc07690 7634 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
3e9f1ca1 7635 if (cu->per_cu->lang == language_cplus)
f9b5d5ea
TV
7636 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7637 set_addrmap, cu);
c906108c 7638 break;
72929c62 7639 case DW_TAG_constant:
c906108c
SS
7640 case DW_TAG_variable:
7641 case DW_TAG_typedef:
91c24f0a 7642 case DW_TAG_union_type:
317d2668
TV
7643 if (!pdi->is_declaration
7644 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 7645 {
72bf9492 7646 add_partial_symbol (pdi, cu);
63d06c5c
DC
7647 }
7648 break;
c906108c 7649 case DW_TAG_class_type:
680b30c7 7650 case DW_TAG_interface_type:
c906108c 7651 case DW_TAG_structure_type:
72bf9492 7652 if (!pdi->is_declaration)
c906108c 7653 {
72bf9492 7654 add_partial_symbol (pdi, cu);
c906108c 7655 }
3e9f1ca1
TT
7656 if ((cu->per_cu->lang == language_rust
7657 || cu->per_cu->lang == language_cplus)
7658 && pdi->has_children)
e98c9e7c
TT
7659 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7660 set_addrmap, cu);
c906108c 7661 break;
91c24f0a 7662 case DW_TAG_enumeration_type:
72bf9492
DJ
7663 if (!pdi->is_declaration)
7664 add_partial_enumeration (pdi, cu);
c906108c
SS
7665 break;
7666 case DW_TAG_base_type:
dda83cd7 7667 case DW_TAG_subrange_type:
c906108c 7668 /* File scope base type definitions are added to the partial
dda83cd7 7669 symbol table. */
72bf9492 7670 add_partial_symbol (pdi, cu);
c906108c 7671 break;
d9fa45fe 7672 case DW_TAG_namespace:
cdc07690 7673 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7674 break;
5d7cb8df 7675 case DW_TAG_module:
59c35742
AB
7676 if (!pdi->is_declaration)
7677 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7678 break;
95554aad
TT
7679 case DW_TAG_imported_unit:
7680 {
7681 struct dwarf2_per_cu_data *per_cu;
7682
f4dc4d17
DE
7683 /* For now we don't handle imported units in type units. */
7684 if (cu->per_cu->is_debug_types)
7685 {
7686 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7687 " supported in type units [in module %s]"),
5e22e966 7688 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
7689 }
7690
e3b94546 7691 per_cu = dwarf2_find_containing_comp_unit
5e22e966 7692 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
7693
7694 /* Go read the partial unit, if needed. */
7695 if (per_cu->v.psymtab == NULL)
ab432490 7696 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
3e9f1ca1 7697 cu->per_cu->lang);
95554aad 7698
ae640021 7699 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7700 }
7701 break;
74921315
KS
7702 case DW_TAG_imported_declaration:
7703 add_partial_symbol (pdi, cu);
7704 break;
c906108c
SS
7705 default:
7706 break;
7707 }
7708 }
7709
72bf9492
DJ
7710 /* If the die has a sibling, skip to the sibling. */
7711
7712 pdi = pdi->die_sibling;
7713 }
7714}
7715
7716/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7717
72bf9492 7718 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7719 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7720 Enumerators are an exception; they use the scope of their parent
7721 enumeration type, i.e. the name of the enumeration type is not
7722 prepended to the enumerator.
91c24f0a 7723
72bf9492
DJ
7724 There are two complexities. One is DW_AT_specification; in this
7725 case "parent" means the parent of the target of the specification,
7726 instead of the direct parent of the DIE. The other is compilers
7727 which do not emit DW_TAG_namespace; in this case we try to guess
7728 the fully qualified name of structure types from their members'
7729 linkage names. This must be done using the DIE's children rather
7730 than the children of any DW_AT_specification target. We only need
7731 to do this for structures at the top level, i.e. if the target of
7732 any DW_AT_specification (if any; otherwise the DIE itself) does not
7733 have a parent. */
7734
7735/* Compute the scope prefix associated with PDI's parent, in
7736 compilation unit CU. The result will be allocated on CU's
7737 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7738 field. NULL is returned if no prefix is necessary. */
15d034d0 7739static const char *
72bf9492
DJ
7740partial_die_parent_scope (struct partial_die_info *pdi,
7741 struct dwarf2_cu *cu)
7742{
15d034d0 7743 const char *grandparent_scope;
72bf9492 7744 struct partial_die_info *parent, *real_pdi;
91c24f0a 7745
72bf9492
DJ
7746 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7747 then this means the parent of the specification DIE. */
7748
7749 real_pdi = pdi;
72bf9492 7750 while (real_pdi->has_specification)
fb816e8b 7751 {
122cf0f2
AB
7752 auto res = find_partial_die (real_pdi->spec_offset,
7753 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
7754 real_pdi = res.pdi;
7755 cu = res.cu;
7756 }
72bf9492
DJ
7757
7758 parent = real_pdi->die_parent;
7759 if (parent == NULL)
7760 return NULL;
7761
7762 if (parent->scope_set)
7763 return parent->scope;
7764
52356b79 7765 parent->fixup (cu);
72bf9492 7766
10b3939b 7767 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7768
acebe513
UW
7769 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7770 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7771 Work around this problem here. */
3e9f1ca1 7772 if (cu->per_cu->lang == language_cplus
6e70227d 7773 && parent->tag == DW_TAG_namespace
7d00ffec 7774 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
7775 && grandparent_scope == NULL)
7776 {
7777 parent->scope = NULL;
7778 parent->scope_set = 1;
7779 return NULL;
7780 }
7781
0a4b0913 7782 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
7783 if (pdi->tag == DW_TAG_enumerator)
7784 /* Enumerators should not get the name of the enumeration as a prefix. */
7785 parent->scope = grandparent_scope;
7786 else if (parent->tag == DW_TAG_namespace
f55ee35c 7787 || parent->tag == DW_TAG_module
72bf9492
DJ
7788 || parent->tag == DW_TAG_structure_type
7789 || parent->tag == DW_TAG_class_type
680b30c7 7790 || parent->tag == DW_TAG_interface_type
ceeb3d5a 7791 || parent->tag == DW_TAG_union_type
0a4b0913 7792 || parent->tag == DW_TAG_enumeration_type
3e9f1ca1 7793 || (cu->per_cu->lang == language_fortran
0a4b0913
AB
7794 && parent->tag == DW_TAG_subprogram
7795 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
7796 {
7797 if (grandparent_scope == NULL)
7d00ffec 7798 parent->scope = parent->name (cu);
72bf9492 7799 else
3e43a32a
MS
7800 parent->scope = typename_concat (&cu->comp_unit_obstack,
7801 grandparent_scope,
7d00ffec 7802 parent->name (cu), 0, cu);
72bf9492 7803 }
72bf9492
DJ
7804 else
7805 {
7806 /* FIXME drow/2004-04-01: What should we be doing with
7807 function-local names? For partial symbols, we should probably be
7808 ignoring them. */
fa9c3fa0
TT
7809 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7810 dwarf_tag_name (parent->tag),
7811 sect_offset_str (pdi->sect_off));
72bf9492 7812 parent->scope = grandparent_scope;
c906108c
SS
7813 }
7814
72bf9492
DJ
7815 parent->scope_set = 1;
7816 return parent->scope;
7817}
7818
7819/* Return the fully scoped name associated with PDI, from compilation unit
7820 CU. The result will be allocated with malloc. */
4568ecf9 7821
43816ebc 7822static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
7823partial_die_full_name (struct partial_die_info *pdi,
7824 struct dwarf2_cu *cu)
7825{
15d034d0 7826 const char *parent_scope;
72bf9492 7827
98bfdba5
PA
7828 /* If this is a template instantiation, we can not work out the
7829 template arguments from partial DIEs. So, unfortunately, we have
7830 to go through the full DIEs. At least any work we do building
7831 types here will be reused if full symbols are loaded later. */
7832 if (pdi->has_template_arguments)
7833 {
52356b79 7834 pdi->fixup (cu);
98bfdba5 7835
7d00ffec 7836 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
7837 {
7838 struct die_info *die;
7839 struct attribute attr;
7840 struct dwarf2_cu *ref_cu = cu;
7841
b64f50a1 7842 /* DW_FORM_ref_addr is using section offset. */
b4069958 7843 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7844 attr.form = DW_FORM_ref_addr;
9c541725 7845 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7846 die = follow_die_ref (NULL, &attr, &ref_cu);
7847
43816ebc 7848 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
7849 }
7850 }
7851
72bf9492
DJ
7852 parent_scope = partial_die_parent_scope (pdi, cu);
7853 if (parent_scope == NULL)
7854 return NULL;
7855 else
43816ebc 7856 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
7857 pdi->name (cu),
7858 0, cu));
c906108c
SS
7859}
7860
7861static void
72bf9492 7862add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7863{
976ca316
SM
7864 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7865 struct objfile *objfile = per_objfile->objfile;
08feed99 7866 struct gdbarch *gdbarch = objfile->arch ();
c906108c 7867 CORE_ADDR addr = 0;
15d034d0 7868 const char *actual_name = NULL;
e142c38c
DJ
7869 CORE_ADDR baseaddr;
7870
b3b3bada 7871 baseaddr = objfile->text_section_offset ();
c906108c 7872
43816ebc
TT
7873 gdb::unique_xmalloc_ptr<char> built_actual_name
7874 = partial_die_full_name (pdi, cu);
15d034d0 7875 if (built_actual_name != NULL)
43816ebc 7876 actual_name = built_actual_name.get ();
63d06c5c 7877
72bf9492 7878 if (actual_name == NULL)
7d00ffec 7879 actual_name = pdi->name (cu);
72bf9492 7880
76e288d1
TT
7881 partial_symbol psymbol;
7882 memset (&psymbol, 0, sizeof (psymbol));
3e9f1ca1
TT
7883 psymbol.ginfo.set_language (cu->per_cu->lang,
7884 &objfile->objfile_obstack);
a52d653e 7885 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
7886
7887 /* The code below indicates that the psymbol should be installed by
7888 setting this. */
7889 gdb::optional<psymbol_placement> where;
7890
c906108c
SS
7891 switch (pdi->tag)
7892 {
b1dc1806 7893 case DW_TAG_inlined_subroutine:
c906108c 7894 case DW_TAG_subprogram:
79748972
TT
7895 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7896 - baseaddr);
0a4b0913 7897 if (pdi->is_external
3e9f1ca1
TT
7898 || cu->per_cu->lang == language_ada
7899 || (cu->per_cu->lang == language_fortran
0a4b0913
AB
7900 && pdi->die_parent != NULL
7901 && pdi->die_parent->tag == DW_TAG_subprogram))
7902 {
dda83cd7
SM
7903 /* Normally, only "external" DIEs are part of the global scope.
7904 But in Ada and Fortran, we want to be able to access nested
7905 procedures globally. So all Ada and Fortran subprograms are
7906 stored in the global scope. */
76e288d1 7907 where = psymbol_placement::GLOBAL;
c906108c
SS
7908 }
7909 else
76e288d1
TT
7910 where = psymbol_placement::STATIC;
7911
7912 psymbol.domain = VAR_DOMAIN;
7913 psymbol.aclass = LOC_BLOCK;
a52d653e 7914 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 7915 psymbol.ginfo.value.address = addr;
0c1b455e
TT
7916
7917 if (pdi->main_subprogram && actual_name != NULL)
3e9f1ca1 7918 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
c906108c 7919 break;
72929c62 7920 case DW_TAG_constant:
76e288d1
TT
7921 psymbol.domain = VAR_DOMAIN;
7922 psymbol.aclass = LOC_STATIC;
7923 where = (pdi->is_external
7924 ? psymbol_placement::GLOBAL
7925 : psymbol_placement::STATIC);
72929c62 7926 break;
c906108c 7927 case DW_TAG_variable:
95554aad
TT
7928 if (pdi->d.locdesc)
7929 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7930
95554aad 7931 if (pdi->d.locdesc
caac4577 7932 && addr == 0
976ca316 7933 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
7934 {
7935 /* A global or static variable may also have been stripped
7936 out by the linker if unused, in which case its address
7937 will be nullified; do not add such variables into partial
7938 symbol table then. */
7939 }
7940 else if (pdi->is_external)
c906108c
SS
7941 {
7942 /* Global Variable.
7943 Don't enter into the minimal symbol tables as there is
7944 a minimal symbol table entry from the ELF symbols already.
7945 Enter into partial symbol table if it has a location
7946 descriptor or a type.
7947 If the location descriptor is missing, new_symbol will create
7948 a LOC_UNRESOLVED symbol, the address of the variable will then
7949 be determined from the minimal symbol table whenever the variable
7950 is referenced.
7951 The address for the partial symbol table entry is not
7952 used by GDB, but it comes in handy for debugging partial symbol
7953 table building. */
7954
95554aad 7955 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
7956 {
7957 psymbol.domain = VAR_DOMAIN;
7958 psymbol.aclass = LOC_STATIC;
a52d653e 7959 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
7960 psymbol.ginfo.value.address = addr;
7961 where = psymbol_placement::GLOBAL;
7962 }
c906108c
SS
7963 }
7964 else
7965 {
ff908ebf
AW
7966 int has_loc = pdi->d.locdesc != NULL;
7967
7968 /* Static Variable. Skip symbols whose value we cannot know (those
7969 without location descriptors or constant values). */
7970 if (!has_loc && !pdi->has_const_value)
43816ebc 7971 return;
ff908ebf 7972
76e288d1
TT
7973 psymbol.domain = VAR_DOMAIN;
7974 psymbol.aclass = LOC_STATIC;
a52d653e 7975 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
7976 if (has_loc)
7977 psymbol.ginfo.value.address = addr;
7978 where = psymbol_placement::STATIC;
c906108c
SS
7979 }
7980 break;
d8f62e84 7981 case DW_TAG_array_type:
c906108c
SS
7982 case DW_TAG_typedef:
7983 case DW_TAG_base_type:
a02abb62 7984 case DW_TAG_subrange_type:
76e288d1
TT
7985 psymbol.domain = VAR_DOMAIN;
7986 psymbol.aclass = LOC_TYPEDEF;
7987 where = psymbol_placement::STATIC;
c906108c 7988 break;
74921315 7989 case DW_TAG_imported_declaration:
72bf9492 7990 case DW_TAG_namespace:
76e288d1
TT
7991 psymbol.domain = VAR_DOMAIN;
7992 psymbol.aclass = LOC_TYPEDEF;
7993 where = psymbol_placement::GLOBAL;
72bf9492 7994 break;
530e8392 7995 case DW_TAG_module:
a5fd13a9 7996 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
7997 available without any name. If so, we skip the module as it
7998 doesn't bring any value. */
a5fd13a9 7999 if (actual_name != nullptr)
76e288d1
TT
8000 {
8001 psymbol.domain = MODULE_DOMAIN;
8002 psymbol.aclass = LOC_TYPEDEF;
8003 where = psymbol_placement::GLOBAL;
8004 }
530e8392 8005 break;
c906108c 8006 case DW_TAG_class_type:
680b30c7 8007 case DW_TAG_interface_type:
c906108c
SS
8008 case DW_TAG_structure_type:
8009 case DW_TAG_union_type:
8010 case DW_TAG_enumeration_type:
fa4028e9 8011 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8012 about "Structure, Union, and Class Type Entries": "An incomplete
8013 structure, union or class type is represented by a structure,
8014 union or class entry that does not have a byte size attribute
8015 and that has a DW_AT_declaration attribute." */
fa4028e9 8016 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8017 return;
fa4028e9 8018
63d06c5c
DC
8019 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8020 static vs. global. */
76e288d1
TT
8021 psymbol.domain = STRUCT_DOMAIN;
8022 psymbol.aclass = LOC_TYPEDEF;
3e9f1ca1 8023 where = (cu->per_cu->lang == language_cplus
76e288d1
TT
8024 ? psymbol_placement::GLOBAL
8025 : psymbol_placement::STATIC);
c906108c
SS
8026 break;
8027 case DW_TAG_enumerator:
76e288d1
TT
8028 psymbol.domain = VAR_DOMAIN;
8029 psymbol.aclass = LOC_CONST;
3e9f1ca1 8030 where = (cu->per_cu->lang == language_cplus
76e288d1
TT
8031 ? psymbol_placement::GLOBAL
8032 : psymbol_placement::STATIC);
c906108c
SS
8033 break;
8034 default:
8035 break;
8036 }
76e288d1
TT
8037
8038 if (where.has_value ())
8039 {
f049a313
TT
8040 if (built_actual_name != nullptr)
8041 actual_name = objfile->intern (actual_name);
3e9f1ca1
TT
8042 if (pdi->linkage_name == nullptr
8043 || cu->per_cu->lang == language_ada)
bcfe6157
TT
8044 psymbol.ginfo.set_linkage_name (actual_name);
8045 else
8046 {
8047 psymbol.ginfo.set_demangled_name (actual_name,
8048 &objfile->objfile_obstack);
8049 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8050 }
7b249e47
TT
8051 cu->per_cu->v.psymtab->add_psymbol
8052 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8053 objfile);
76e288d1 8054 }
c906108c
SS
8055}
8056
5c4e30ca
DC
8057/* Read a partial die corresponding to a namespace; also, add a symbol
8058 corresponding to that namespace to the symbol table. NAMESPACE is
8059 the name of the enclosing namespace. */
91c24f0a 8060
72bf9492
DJ
8061static void
8062add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8063 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8064 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8065{
72bf9492 8066 /* Add a symbol for the namespace. */
e7c27a73 8067
72bf9492 8068 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8069
8070 /* Now scan partial symbols in that namespace. */
8071
91c24f0a 8072 if (pdi->has_children)
cdc07690 8073 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8074}
8075
5d7cb8df
JK
8076/* Read a partial die corresponding to a Fortran module. */
8077
8078static void
8079add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8080 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8081{
530e8392
KB
8082 /* Add a symbol for the namespace. */
8083
8084 add_partial_symbol (pdi, cu);
8085
f55ee35c 8086 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8087
8088 if (pdi->has_children)
cdc07690 8089 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8090}
8091
b1dc1806
XR
8092/* Read a partial die corresponding to a subprogram or an inlined
8093 subprogram and create a partial symbol for that subprogram.
8094 When the CU language allows it, this routine also defines a partial
8095 symbol for each nested subprogram that this subprogram contains.
8096 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8097 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8098
cdc07690
YQ
8099 PDI may also be a lexical block, in which case we simply search
8100 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8101 Again, this is only performed when the CU language allows this
8102 type of definitions. */
8103
8104static void
8105add_partial_subprogram (struct partial_die_info *pdi,
8106 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8107 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8108{
b1dc1806 8109 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8110 {
8111 if (pdi->has_pc_info)
dda83cd7
SM
8112 {
8113 if (pdi->lowpc < *lowpc)
8114 *lowpc = pdi->lowpc;
8115 if (pdi->highpc > *highpc)
8116 *highpc = pdi->highpc;
cdc07690 8117 if (set_addrmap)
5734ee8b 8118 {
5e22e966 8119 struct objfile *objfile = cu->per_objfile->objfile;
84685904 8120 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 8121 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8122 CORE_ADDR baseaddr;
b926417a
TT
8123 CORE_ADDR this_highpc;
8124 CORE_ADDR this_lowpc;
5734ee8b 8125
b3b3bada 8126 baseaddr = objfile->text_section_offset ();
b926417a
TT
8127 this_lowpc
8128 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8129 pdi->lowpc + baseaddr)
8130 - baseaddr);
8131 this_highpc
8132 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8133 pdi->highpc + baseaddr)
8134 - baseaddr);
84685904 8135 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
b926417a 8136 this_lowpc, this_highpc - 1,
9291a0cd 8137 cu->per_cu->v.psymtab);
5734ee8b 8138 }
dda83cd7 8139 }
481860b3
GB
8140
8141 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8142 {
dda83cd7 8143 if (!pdi->is_declaration)
e8d05480
JB
8144 /* Ignore subprogram DIEs that do not have a name, they are
8145 illegal. Do not emit a complaint at this point, we will
8146 do so when we convert this psymtab into a symtab. */
7d00ffec 8147 if (pdi->name (cu))
e8d05480 8148 add_partial_symbol (pdi, cu);
dda83cd7 8149 }
bc30ff58 8150 }
6e70227d 8151
bc30ff58
JB
8152 if (! pdi->has_children)
8153 return;
8154
3e9f1ca1
TT
8155 if (cu->per_cu->lang == language_ada
8156 || cu->per_cu->lang == language_fortran)
bc30ff58
JB
8157 {
8158 pdi = pdi->die_child;
8159 while (pdi != NULL)
8160 {
52356b79 8161 pdi->fixup (cu);
bc30ff58 8162 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8163 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8164 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8165 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8166 pdi = pdi->die_sibling;
8167 }
8168 }
8169}
8170
91c24f0a
DC
8171/* Read a partial die corresponding to an enumeration type. */
8172
72bf9492
DJ
8173static void
8174add_partial_enumeration (struct partial_die_info *enum_pdi,
8175 struct dwarf2_cu *cu)
91c24f0a 8176{
72bf9492 8177 struct partial_die_info *pdi;
91c24f0a 8178
7d00ffec 8179 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8180 add_partial_symbol (enum_pdi, cu);
8181
8182 pdi = enum_pdi->die_child;
8183 while (pdi)
91c24f0a 8184 {
7d00ffec 8185 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8186 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8187 else
72bf9492
DJ
8188 add_partial_symbol (pdi, cu);
8189 pdi = pdi->die_sibling;
91c24f0a 8190 }
91c24f0a
DC
8191}
8192
6caca83c
CC
8193/* Return the initial uleb128 in the die at INFO_PTR. */
8194
8195static unsigned int
d521ce57 8196peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8197{
8198 unsigned int bytes_read;
8199
8200 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8201}
8202
685af9cd
TT
8203/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8204 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8205
4bb7a0a7
DJ
8206 Return the corresponding abbrev, or NULL if the number is zero (indicating
8207 an empty DIE). In either case *BYTES_READ will be set to the length of
8208 the initial number. */
8209
7c32eebb 8210static const struct abbrev_info *
685af9cd
TT
8211peek_die_abbrev (const die_reader_specs &reader,
8212 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8213{
685af9cd 8214 dwarf2_cu *cu = reader.cu;
0280fdcc 8215 bfd *abfd = reader.abfd;
685af9cd
TT
8216 unsigned int abbrev_number
8217 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8218
8219 if (abbrev_number == 0)
8220 return NULL;
8221
7c32eebb
TT
8222 const abbrev_info *abbrev
8223 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8224 if (!abbrev)
8225 {
422b9917 8226 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8227 " at offset %s [in module %s]"),
422b9917 8228 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8229 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8230 }
8231
8232 return abbrev;
8233}
8234
93311388
DE
8235/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8236 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8237 DIE. Any children of the skipped DIEs will also be skipped. */
8238
d521ce57
TT
8239static const gdb_byte *
8240skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8241{
4bb7a0a7
DJ
8242 while (1)
8243 {
685af9cd 8244 unsigned int bytes_read;
7c32eebb
TT
8245 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8246 &bytes_read);
685af9cd 8247
4bb7a0a7
DJ
8248 if (abbrev == NULL)
8249 return info_ptr + bytes_read;
8250 else
dee91e82 8251 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8252 }
8253}
8254
93311388
DE
8255/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8256 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8257 abbrev corresponding to that skipped uleb128 should be passed in
8258 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8259 children. */
8260
d521ce57
TT
8261static const gdb_byte *
8262skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8263 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8264{
8265 unsigned int bytes_read;
8266 struct attribute attr;
dee91e82
DE
8267 bfd *abfd = reader->abfd;
8268 struct dwarf2_cu *cu = reader->cu;
d521ce57 8269 const gdb_byte *buffer = reader->buffer;
f664829e 8270 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8271 unsigned int form, i;
8272
8273 for (i = 0; i < abbrev->num_attrs; i++)
8274 {
8275 /* The only abbrev we care about is DW_AT_sibling. */
8276 if (abbrev->attrs[i].name == DW_AT_sibling)
8277 {
7a5f294d 8278 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8279 if (attr.form == DW_FORM_ref_addr)
b98664d3 8280 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8281 else
b9502d3f 8282 {
0826b30a 8283 sect_offset off = attr.get_ref_die_offset ();
9c541725 8284 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8285
8286 if (sibling_ptr < info_ptr)
b98664d3 8287 complaint (_("DW_AT_sibling points backwards"));
22869d73 8288 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8289 reader->die_section->overflow_complaint ();
b9502d3f
WN
8290 else
8291 return sibling_ptr;
8292 }
4bb7a0a7
DJ
8293 }
8294
8295 /* If it isn't DW_AT_sibling, skip this attribute. */
8296 form = abbrev->attrs[i].form;
8297 skip_attribute:
8298 switch (form)
8299 {
4bb7a0a7 8300 case DW_FORM_ref_addr:
ae411497
TT
8301 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8302 and later it is offset sized. */
8303 if (cu->header.version == 2)
8304 info_ptr += cu->header.addr_size;
8305 else
8306 info_ptr += cu->header.offset_size;
8307 break;
36586728
TT
8308 case DW_FORM_GNU_ref_alt:
8309 info_ptr += cu->header.offset_size;
8310 break;
ae411497 8311 case DW_FORM_addr:
4bb7a0a7
DJ
8312 info_ptr += cu->header.addr_size;
8313 break;
8314 case DW_FORM_data1:
8315 case DW_FORM_ref1:
8316 case DW_FORM_flag:
8fe0f950 8317 case DW_FORM_strx1:
4bb7a0a7
DJ
8318 info_ptr += 1;
8319 break;
2dc7f7b3 8320 case DW_FORM_flag_present:
43988095 8321 case DW_FORM_implicit_const:
2dc7f7b3 8322 break;
4bb7a0a7
DJ
8323 case DW_FORM_data2:
8324 case DW_FORM_ref2:
8fe0f950 8325 case DW_FORM_strx2:
4bb7a0a7
DJ
8326 info_ptr += 2;
8327 break;
8fe0f950
AT
8328 case DW_FORM_strx3:
8329 info_ptr += 3;
8330 break;
4bb7a0a7
DJ
8331 case DW_FORM_data4:
8332 case DW_FORM_ref4:
8fe0f950 8333 case DW_FORM_strx4:
4bb7a0a7
DJ
8334 info_ptr += 4;
8335 break;
8336 case DW_FORM_data8:
8337 case DW_FORM_ref8:
55f1336d 8338 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8339 info_ptr += 8;
8340 break;
0224619f
JK
8341 case DW_FORM_data16:
8342 info_ptr += 16;
8343 break;
4bb7a0a7 8344 case DW_FORM_string:
9b1c24c8 8345 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8346 info_ptr += bytes_read;
8347 break;
2dc7f7b3 8348 case DW_FORM_sec_offset:
4bb7a0a7 8349 case DW_FORM_strp:
36586728 8350 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8351 info_ptr += cu->header.offset_size;
8352 break;
2dc7f7b3 8353 case DW_FORM_exprloc:
4bb7a0a7
DJ
8354 case DW_FORM_block:
8355 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8356 info_ptr += bytes_read;
8357 break;
8358 case DW_FORM_block1:
8359 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8360 break;
8361 case DW_FORM_block2:
8362 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8363 break;
8364 case DW_FORM_block4:
8365 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8366 break;
336d760d 8367 case DW_FORM_addrx:
cf532bd1 8368 case DW_FORM_strx:
4bb7a0a7
DJ
8369 case DW_FORM_sdata:
8370 case DW_FORM_udata:
8371 case DW_FORM_ref_udata:
3019eac3
DE
8372 case DW_FORM_GNU_addr_index:
8373 case DW_FORM_GNU_str_index:
18a8505e 8374 case DW_FORM_rnglistx:
41144253 8375 case DW_FORM_loclistx:
d521ce57 8376 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8377 break;
8378 case DW_FORM_indirect:
8379 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8380 info_ptr += bytes_read;
8381 /* We need to continue parsing from here, so just go back to
8382 the top. */
8383 goto skip_attribute;
8384
8385 default:
3e43a32a
MS
8386 error (_("Dwarf Error: Cannot handle %s "
8387 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8388 dwarf_form_name (form),
8389 bfd_get_filename (abfd));
8390 }
8391 }
8392
8393 if (abbrev->has_children)
dee91e82 8394 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8395 else
8396 return info_ptr;
8397}
8398
93311388 8399/* Locate ORIG_PDI's sibling.
dee91e82 8400 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8401
d521ce57 8402static const gdb_byte *
dee91e82
DE
8403locate_pdi_sibling (const struct die_reader_specs *reader,
8404 struct partial_die_info *orig_pdi,
d521ce57 8405 const gdb_byte *info_ptr)
91c24f0a
DC
8406{
8407 /* Do we know the sibling already? */
72bf9492 8408
91c24f0a
DC
8409 if (orig_pdi->sibling)
8410 return orig_pdi->sibling;
8411
8412 /* Are there any children to deal with? */
8413
8414 if (!orig_pdi->has_children)
8415 return info_ptr;
8416
4bb7a0a7 8417 /* Skip the children the long way. */
91c24f0a 8418
dee91e82 8419 return skip_children (reader, info_ptr);
91c24f0a
DC
8420}
8421
257e7a09 8422/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8423 not NULL. */
c906108c 8424
891813be
TT
8425void
8426dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8427{
976ca316 8428 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8429
976ca316 8430 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8431
077cbab2
TT
8432 /* If this psymtab is constructed from a debug-only objfile, the
8433 has_section_at_zero flag will not necessarily be correct. We
8434 can get the correct value for this flag by looking at the data
8435 associated with the (presumably stripped) associated objfile. */
8436 if (objfile->separate_debug_objfile_backlink)
c906108c 8437 {
976ca316 8438 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8439 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8440
976ca316
SM
8441 per_objfile->per_bfd->has_section_at_zero
8442 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8443 }
98bfdba5 8444
8566b89b 8445 expand_psymtab (objfile);
95554aad 8446
976ca316 8447 process_cu_includes (per_objfile);
c906108c 8448}
9cdd5dbd
DE
8449\f
8450/* Reading in full CUs. */
c906108c 8451
10b3939b
DJ
8452/* Add PER_CU to the queue. */
8453
8454static void
120ce1b5
SM
8455queue_comp_unit (dwarf2_per_cu_data *per_cu,
8456 dwarf2_per_objfile *per_objfile,
95554aad 8457 enum language pretend_language)
10b3939b 8458{
10b3939b 8459 per_cu->queued = 1;
08ac5771
SM
8460
8461 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8462 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8463}
8464
616c069a
SM
8465/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8466
89e63ee4
DE
8467 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8468 dependency.
69d751e3 8469
616c069a
SM
8470 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8471 DIEs, false otherwise.
8472
8473 Explanation: there is an invariant that if a CU is queued for expansion
8474 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8475 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8476 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8477 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8478 invariant is respected.
8479
8480 The caller is therefore not required to load the CU's DIEs (we return false)
8481 if:
8482
8483 - the CU is already expanded, and therefore does not get enqueued
8484 - the CU gets enqueued for expansion, but its DIEs are already loaded
8485
8486 Note that the caller should not use this function's return value as an
8487 indicator of whether the CU's DIEs are loaded right now, it should check
8488 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
8489
8490static int
89e63ee4 8491maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8492 dwarf2_per_cu_data *per_cu,
8493 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8494 enum language pretend_language)
8495{
8496 /* We may arrive here during partial symbol reading, if we need full
8497 DIEs to process an unusual case (e.g. template arguments). Do
8498 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8499 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8500 {
7188ed02
SM
8501 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8502
8503 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8504 return 1;
8505 return 0;
8506 }
8507
8508 /* Mark the dependence relation so that we don't flush PER_CU
8509 too early. */
89e63ee4 8510 if (dependent_cu != NULL)
347212b8 8511 dependent_cu->add_dependence (per_cu);
0907af0c
DE
8512
8513 /* If it's already on the queue, we have nothing to do. */
8514 if (per_cu->queued)
de53369b
SM
8515 {
8516 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8517 loaded. */
8518 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
8519
8520 /* If the CU is queued for expansion, it should not already be
8521 expanded. */
8522 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8523
8524 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
8525 return 0;
8526 }
0907af0c 8527
616c069a
SM
8528 bool queued = false;
8529 if (!per_objfile->symtab_set_p (per_cu))
8530 {
8531 /* Add it to the queue. */
8532 queue_comp_unit (per_cu, per_objfile, pretend_language);
8533 queued = true;
8534 }
8535
0907af0c
DE
8536 /* If the compilation unit is already loaded, just mark it as
8537 used. */
7188ed02
SM
8538 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8539 if (cu != nullptr)
616c069a 8540 cu->last_used = 0;
0907af0c 8541
616c069a
SM
8542 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8543 and the DIEs are not already loaded. */
8544 return queued && cu == nullptr;
0907af0c
DE
8545}
8546
10b3939b
DJ
8547/* Process the queue. */
8548
8549static void
976ca316 8550process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 8551{
6f738b01
SM
8552 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8553 objfile_name (per_objfile->objfile));
45cfd468 8554
03dd20cc
DJ
8555 /* The queue starts out with one item, but following a DIE reference
8556 may load a new CU, adding it to the end of the queue. */
08ac5771 8557 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 8558 {
08ac5771 8559 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 8560 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 8561
976ca316 8562 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 8563 {
976ca316 8564 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 8565
7188ed02
SM
8566 /* Skip dummy CUs. */
8567 if (cu != nullptr)
73be47f5 8568 {
7188ed02
SM
8569 unsigned int debug_print_threshold;
8570 char buf[100];
8571
8572 if (per_cu->is_debug_types)
8573 {
8574 struct signatured_type *sig_type =
8575 (struct signatured_type *) per_cu;
8576
8577 sprintf (buf, "TU %s at offset %s",
8578 hex_string (sig_type->signature),
8579 sect_offset_str (per_cu->sect_off));
8580 /* There can be 100s of TUs.
8581 Only print them in verbose mode. */
8582 debug_print_threshold = 2;
8583 }
8584 else
8585 {
8586 sprintf (buf, "CU at offset %s",
8587 sect_offset_str (per_cu->sect_off));
8588 debug_print_threshold = 1;
8589 }
247f5c4f 8590
7188ed02 8591 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8592 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 8593
7188ed02
SM
8594 if (per_cu->is_debug_types)
8595 process_full_type_unit (cu, item.pretend_language);
8596 else
8597 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 8598
7188ed02 8599 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8600 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 8601 }
f4dc4d17 8602 }
10b3939b 8603
7188ed02 8604 per_cu->queued = 0;
08ac5771 8605 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
8606 }
8607
6f738b01
SM
8608 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8609 objfile_name (per_objfile->objfile));
10b3939b
DJ
8610}
8611
10b3939b
DJ
8612/* Read in full symbols for PST, and anything it depends on. */
8613
8566b89b
TT
8614void
8615dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8616{
af758d11 8617 gdb_assert (!readin_p (objfile));
95554aad 8618
17ee85fc
TT
8619 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8620 free_cached_comp_units freer (per_objfile);
48993951 8621 expand_dependencies (objfile);
aaa75496 8622
97a1449a 8623 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 8624 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
8625}
8626
af758d11
SM
8627/* See psympriv.h. */
8628
8629bool
8630dwarf2_psymtab::readin_p (struct objfile *objfile) const
8631{
8632 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8633 return per_objfile->symtab_set_p (per_cu_data);
8634}
8635
8636/* See psympriv.h. */
8637
8638compunit_symtab *
8639dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8640{
8641 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8642 return per_objfile->get_symtab (per_cu_data);
8643}
8644
dee91e82
DE
8645/* Trivial hash function for die_info: the hash value of a DIE
8646 is its offset in .debug_info for this objfile. */
10b3939b 8647
dee91e82
DE
8648static hashval_t
8649die_hash (const void *item)
10b3939b 8650{
9a3c8263 8651 const struct die_info *die = (const struct die_info *) item;
6502dd73 8652
9c541725 8653 return to_underlying (die->sect_off);
dee91e82 8654}
63d06c5c 8655
dee91e82
DE
8656/* Trivial comparison function for die_info structures: two DIEs
8657 are equal if they have the same offset. */
98bfdba5 8658
dee91e82
DE
8659static int
8660die_eq (const void *item_lhs, const void *item_rhs)
8661{
9a3c8263
SM
8662 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8663 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8664
9c541725 8665 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8666}
c906108c 8667
4a636814
SM
8668/* Load the DIEs associated with PER_CU into memory.
8669
8670 In some cases, the caller, while reading partial symbols, will need to load
8671 the full symbols for the CU for some reason. It will already have a
8672 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8673 rather than creating a new one. */
c906108c 8674
dee91e82 8675static void
ab432490
SM
8676load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8677 dwarf2_per_objfile *per_objfile,
4a636814 8678 dwarf2_cu *existing_cu,
c0ab21c2
TT
8679 bool skip_partial,
8680 enum language pretend_language)
dee91e82 8681{
c0ab21c2
TT
8682 gdb_assert (! this_cu->is_debug_types);
8683
7188ed02 8684 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
8685 if (reader.dummy_p)
8686 return;
8687
8688 struct dwarf2_cu *cu = reader.cu;
8689 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8690
dee91e82
DE
8691 gdb_assert (cu->die_hash == NULL);
8692 cu->die_hash =
8693 htab_create_alloc_ex (cu->header.length / 12,
8694 die_hash,
8695 die_eq,
8696 NULL,
8697 &cu->comp_unit_obstack,
8698 hashtab_obstack_allocate,
8699 dummy_obstack_deallocate);
e142c38c 8700
3e225074 8701 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8702 reader.comp_unit_die->child
8703 = read_die_and_siblings (&reader, reader.info_ptr,
8704 &info_ptr, reader.comp_unit_die);
8705 cu->dies = reader.comp_unit_die;
dee91e82 8706 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8707
8708 /* We try not to read any attributes in this function, because not
9cdd5dbd 8709 all CUs needed for references have been loaded yet, and symbol
10b3939b 8710 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8711 or we won't be able to build types correctly.
8712 Similarly, if we do not read the producer, we can not apply
8713 producer-specific interpretation. */
c0ab21c2 8714 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8715
8716 reader.keep ();
10b3939b
DJ
8717}
8718
3da10d80
KS
8719/* Add a DIE to the delayed physname list. */
8720
8721static void
8722add_to_method_list (struct type *type, int fnfield_index, int index,
8723 const char *name, struct die_info *die,
8724 struct dwarf2_cu *cu)
8725{
8726 struct delayed_method_info mi;
8727 mi.type = type;
8728 mi.fnfield_index = fnfield_index;
8729 mi.index = index;
8730 mi.name = name;
8731 mi.die = die;
c89b44cd 8732 cu->method_list.push_back (mi);
3da10d80
KS
8733}
8734
3693fdb3
PA
8735/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8736 "const" / "volatile". If so, decrements LEN by the length of the
8737 modifier and return true. Otherwise return false. */
8738
8739template<size_t N>
8740static bool
8741check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8742{
8743 size_t mod_len = sizeof (mod) - 1;
8744 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8745 {
8746 len -= mod_len;
8747 return true;
8748 }
8749 return false;
8750}
8751
3da10d80
KS
8752/* Compute the physnames of any methods on the CU's method list.
8753
8754 The computation of method physnames is delayed in order to avoid the
8755 (bad) condition that one of the method's formal parameters is of an as yet
8756 incomplete type. */
8757
8758static void
8759compute_delayed_physnames (struct dwarf2_cu *cu)
8760{
3693fdb3 8761 /* Only C++ delays computing physnames. */
c89b44cd 8762 if (cu->method_list.empty ())
3693fdb3 8763 return;
3e9f1ca1 8764 gdb_assert (cu->per_cu->lang == language_cplus);
3693fdb3 8765
52941706 8766 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8767 {
1d06ead6 8768 const char *physname;
3da10d80 8769 struct fn_fieldlist *fn_flp
c89b44cd
TT
8770 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8771 physname = dwarf2_physname (mi.name, mi.die, cu);
8772 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8773 = physname ? physname : "";
3693fdb3
PA
8774
8775 /* Since there's no tag to indicate whether a method is a
8776 const/volatile overload, extract that information out of the
8777 demangled name. */
8778 if (physname != NULL)
8779 {
8780 size_t len = strlen (physname);
8781
8782 while (1)
8783 {
8784 if (physname[len] == ')') /* shortcut */
8785 break;
8786 else if (check_modifier (physname, len, " const"))
c89b44cd 8787 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8788 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8789 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8790 else
8791 break;
8792 }
8793 }
3da10d80 8794 }
c89b44cd
TT
8795
8796 /* The list is no longer needed. */
8797 cu->method_list.clear ();
3da10d80
KS
8798}
8799
a766d390
DE
8800/* Go objects should be embedded in a DW_TAG_module DIE,
8801 and it's not clear if/how imported objects will appear.
8802 To keep Go support simple until that's worked out,
8803 go back through what we've read and create something usable.
8804 We could do this while processing each DIE, and feels kinda cleaner,
8805 but that way is more invasive.
8806 This is to, for example, allow the user to type "p var" or "b main"
8807 without having to specify the package name, and allow lookups
8808 of module.object to work in contexts that use the expression
8809 parser. */
8810
8811static void
8812fixup_go_packaging (struct dwarf2_cu *cu)
8813{
421d1616 8814 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
8815 struct pending *list;
8816 int i;
8817
c24bdb02 8818 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
8819 list != NULL;
8820 list = list->next)
a766d390
DE
8821 {
8822 for (i = 0; i < list->nsyms; ++i)
8823 {
8824 struct symbol *sym = list->symbol[i];
8825
c1b5c1eb 8826 if (sym->language () == language_go
a766d390
DE
8827 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8828 {
421d1616
TT
8829 gdb::unique_xmalloc_ptr<char> this_package_name
8830 (go_symbol_package_name (sym));
a766d390
DE
8831
8832 if (this_package_name == NULL)
8833 continue;
8834 if (package_name == NULL)
421d1616 8835 package_name = std::move (this_package_name);
a766d390
DE
8836 else
8837 {
5e22e966 8838 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 8839 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 8840 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8841 (symbol_symtab (sym) != NULL
8842 ? symtab_to_filename_for_display
8843 (symbol_symtab (sym))
e3b94546 8844 : objfile_name (objfile)),
421d1616 8845 this_package_name.get (), package_name.get ());
a766d390
DE
8846 }
8847 }
8848 }
8849 }
8850
8851 if (package_name != NULL)
8852 {
5e22e966 8853 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 8854 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
8855 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8856 saved_package_name);
a766d390
DE
8857 struct symbol *sym;
8858
8c14c3a3 8859 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 8860 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 8861 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
8862 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8863 e.g., "main" finds the "main" module and not C's main(). */
8864 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8865 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8866 SYMBOL_TYPE (sym) = type;
8867
c24bdb02 8868 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
8869 }
8870}
8871
c9317f21
TT
8872/* Allocate a fully-qualified name consisting of the two parts on the
8873 obstack. */
8874
8875static const char *
8876rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8877{
8878 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8879}
8880
9c6a1327
TT
8881/* A helper that allocates a variant part to attach to a Rust enum
8882 type. OBSTACK is where the results should be allocated. TYPE is
8883 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
8884 discriminant. It must be the index of one of the fields of TYPE,
8885 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
8886 DEFAULT_INDEX is the index of the default field; or -1 if there is
8887 no default. RANGES is indexed by "effective" field number (the
8888 field index, but omitting the discriminant and default fields) and
8889 must hold the discriminant values used by the variants. Note that
8890 RANGES must have a lifetime at least as long as OBSTACK -- either
8891 already allocated on it, or static. */
c9317f21 8892
9c6a1327
TT
8893static void
8894alloc_rust_variant (struct obstack *obstack, struct type *type,
8895 int discriminant_index, int default_index,
8896 gdb::array_view<discriminant_range> ranges)
8897{
57d02173
TT
8898 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8899 gdb_assert (discriminant_index == -1
8900 || (discriminant_index >= 0
8901 && discriminant_index < type->num_fields ()));
c9317f21 8902 gdb_assert (default_index == -1
1f704f76 8903 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 8904
9c6a1327 8905 /* We have one variant for each non-discriminant field. */
57d02173
TT
8906 int n_variants = type->num_fields ();
8907 if (discriminant_index != -1)
8908 --n_variants;
c9317f21 8909
9c6a1327
TT
8910 variant *variants = new (obstack) variant[n_variants];
8911 int var_idx = 0;
8912 int range_idx = 0;
1f704f76 8913 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
8914 {
8915 if (i == discriminant_index)
8916 continue;
c9317f21 8917
9c6a1327
TT
8918 variants[var_idx].first_field = i;
8919 variants[var_idx].last_field = i + 1;
8920
8921 /* The default field does not need a range, but other fields do.
8922 We skipped the discriminant above. */
8923 if (i != default_index)
8924 {
8925 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8926 ++range_idx;
8927 }
c9317f21 8928
9c6a1327
TT
8929 ++var_idx;
8930 }
8931
8932 gdb_assert (range_idx == ranges.size ());
8933 gdb_assert (var_idx == n_variants);
8934
8935 variant_part *part = new (obstack) variant_part;
8936 part->discriminant_index = discriminant_index;
57d02173
TT
8937 /* If there is no discriminant, then whether it is signed is of no
8938 consequence. */
8939 part->is_unsigned
8940 = (discriminant_index == -1
8941 ? false
c6d940a9 8942 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
8943 part->variants = gdb::array_view<variant> (variants, n_variants);
8944
8945 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
8946 gdb::array_view<variant_part> *prop_value
8947 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 8948
9c6a1327 8949 struct dynamic_prop prop;
8c2e4e06 8950 prop.set_variant_parts (prop_value);
9c6a1327 8951
5c54719c 8952 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
8953}
8954
8955/* Some versions of rustc emitted enums in an unusual way.
8956
8957 Ordinary enums were emitted as unions. The first element of each
8958 structure in the union was named "RUST$ENUM$DISR". This element
8959 held the discriminant.
8960
8961 These versions of Rust also implemented the "non-zero"
8962 optimization. When the enum had two values, and one is empty and
8963 the other holds a pointer that cannot be zero, the pointer is used
8964 as the discriminant, with a zero value meaning the empty variant.
8965 Here, the union's first member is of the form
8966 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8967 where the fieldnos are the indices of the fields that should be
8968 traversed in order to find the field (which may be several fields deep)
8969 and the variantname is the name of the variant of the case when the
8970 field is zero.
8971
8972 This function recognizes whether TYPE is of one of these forms,
8973 and, if so, smashes it to be a variant type. */
8974
8975static void
8976quirk_rust_enum (struct type *type, struct objfile *objfile)
8977{
78134374 8978 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
8979
8980 /* We don't need to deal with empty enums. */
1f704f76 8981 if (type->num_fields () == 0)
c9317f21
TT
8982 return;
8983
8984#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 8985 if (type->num_fields () == 1
c9317f21
TT
8986 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
8987 {
8988 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
8989
8990 /* Decode the field name to find the offset of the
8991 discriminant. */
8992 ULONGEST bit_offset = 0;
940da03e 8993 struct type *field_type = type->field (0).type ();
c9317f21
TT
8994 while (name[0] >= '0' && name[0] <= '9')
8995 {
8996 char *tail;
8997 unsigned long index = strtoul (name, &tail, 10);
8998 name = tail;
8999 if (*name != '$'
1f704f76 9000 || index >= field_type->num_fields ()
c9317f21
TT
9001 || (TYPE_FIELD_LOC_KIND (field_type, index)
9002 != FIELD_LOC_KIND_BITPOS))
9003 {
b98664d3 9004 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9005 "[in module %s]"),
9006 TYPE_FIELD_NAME (type, 0),
9007 objfile_name (objfile));
9008 return;
9009 }
9010 ++name;
9011
9012 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9013 field_type = field_type->field (index).type ();
c9317f21
TT
9014 }
9015
9c6a1327
TT
9016 /* Smash this type to be a structure type. We have to do this
9017 because the type has already been recorded. */
67607e24 9018 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9019 type->set_num_fields (3);
9c6a1327 9020 /* Save the field we care about. */
ceacbf6e 9021 struct field saved_field = type->field (0);
3cabb6b0
SM
9022 type->set_fields
9023 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9024
9c6a1327 9025 /* Put the discriminant at index 0. */
5d14b6e5 9026 type->field (0).set_type (field_type);
9c6a1327
TT
9027 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9028 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9029 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9030
9031 /* The order of fields doesn't really matter, so put the real
9032 field at index 1 and the data-less field at index 2. */
ceacbf6e 9033 type->field (1) = saved_field;
9c6a1327 9034 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9035 = rust_last_path_segment (type->field (1).type ()->name ());
9036 type->field (1).type ()->set_name
7d93a1e0 9037 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9038 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9039
9040 const char *dataless_name
7d93a1e0 9041 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9042 name);
9043 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9044 dataless_name);
5d14b6e5 9045 type->field (2).set_type (dataless_type);
c9317f21
TT
9046 /* NAME points into the original discriminant name, which
9047 already has the correct lifetime. */
9c6a1327 9048 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9049 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9050
9c6a1327
TT
9051 /* Indicate that this is a variant type. */
9052 static discriminant_range ranges[1] = { { 0, 0 } };
9053 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9054 }
77c2dba3
TT
9055 /* A union with a single anonymous field is probably an old-style
9056 univariant enum. */
1f704f76 9057 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9058 {
c9317f21
TT
9059 /* Smash this type to be a structure type. We have to do this
9060 because the type has already been recorded. */
67607e24 9061 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9062
940da03e 9063 struct type *field_type = type->field (0).type ();
c9317f21 9064 const char *variant_name
7d93a1e0 9065 = rust_last_path_segment (field_type->name ());
9c6a1327 9066 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9067 field_type->set_name
9068 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9069 type->name (), variant_name));
57d02173
TT
9070
9071 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9072 }
9073 else
9074 {
9075 struct type *disr_type = nullptr;
1f704f76 9076 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9077 {
940da03e 9078 disr_type = type->field (i).type ();
c9317f21 9079
78134374 9080 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9081 {
9082 /* All fields of a true enum will be structs. */
9083 return;
9084 }
1f704f76 9085 else if (disr_type->num_fields () == 0)
c9317f21
TT
9086 {
9087 /* Could be data-less variant, so keep going. */
a037790e 9088 disr_type = nullptr;
c9317f21
TT
9089 }
9090 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9091 "RUST$ENUM$DISR") != 0)
9092 {
9093 /* Not a Rust enum. */
9094 return;
9095 }
9096 else
9097 {
9098 /* Found one. */
9099 break;
9100 }
9101 }
9102
9103 /* If we got here without a discriminant, then it's probably
9104 just a union. */
9105 if (disr_type == nullptr)
9106 return;
9107
9108 /* Smash this type to be a structure type. We have to do this
9109 because the type has already been recorded. */
67607e24 9110 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9111
9c6a1327 9112 /* Make space for the discriminant field. */
ceacbf6e 9113 struct field *disr_field = &disr_type->field (0);
9c6a1327 9114 field *new_fields
1f704f76 9115 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9116 * sizeof (struct field)));
80fc5e77 9117 memcpy (new_fields + 1, type->fields (),
1f704f76 9118 type->num_fields () * sizeof (struct field));
3cabb6b0 9119 type->set_fields (new_fields);
1f704f76 9120 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9121
9122 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9123 type->field (0) = *disr_field;
9c6a1327
TT
9124 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9125 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9126
9127 /* We need a way to find the correct discriminant given a
9128 variant name. For convenience we build a map here. */
b6cdac4b 9129 struct type *enum_type = disr_field->type ();
c9317f21 9130 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9131 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9132 {
9133 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9134 {
9135 const char *name
9136 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9137 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9138 }
9139 }
9140
1f704f76 9141 int n_fields = type->num_fields ();
9c6a1327
TT
9142 /* We don't need a range entry for the discriminant, but we do
9143 need one for every other field, as there is no default
9144 variant. */
9145 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9146 discriminant_range,
9147 n_fields - 1);
c9317f21
TT
9148 /* Skip the discriminant here. */
9149 for (int i = 1; i < n_fields; ++i)
9150 {
9151 /* Find the final word in the name of this variant's type.
9152 That name can be used to look up the correct
9153 discriminant. */
9154 const char *variant_name
940da03e 9155 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9156
9157 auto iter = discriminant_map.find (variant_name);
9158 if (iter != discriminant_map.end ())
9c6a1327 9159 {
57d02173
TT
9160 ranges[i - 1].low = iter->second;
9161 ranges[i - 1].high = iter->second;
9c6a1327 9162 }
c9317f21 9163
57d02173
TT
9164 /* In Rust, each element should have the size of the
9165 enclosing enum. */
9166 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9167
bedda9ac 9168 /* Remove the discriminant field, if it exists. */
940da03e 9169 struct type *sub_type = type->field (i).type ();
1f704f76 9170 if (sub_type->num_fields () > 0)
bedda9ac 9171 {
5e33d5f4 9172 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9173 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9174 }
9c6a1327 9175 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9176 sub_type->set_name
9177 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9178 type->name (), variant_name));
c9317f21 9179 }
9c6a1327
TT
9180
9181 /* Indicate that this is a variant type. */
a1520ad8 9182 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9183 gdb::array_view<discriminant_range> (ranges,
9184 n_fields - 1));
c9317f21
TT
9185 }
9186}
9187
9188/* Rewrite some Rust unions to be structures with variants parts. */
9189
9190static void
9191rust_union_quirks (struct dwarf2_cu *cu)
9192{
3e9f1ca1 9193 gdb_assert (cu->per_cu->lang == language_rust);
52941706 9194 for (type *type_ : cu->rust_unions)
5e22e966 9195 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9196 /* We don't need this any more. */
9197 cu->rust_unions.clear ();
c9317f21
TT
9198}
9199
8adb8487
TT
9200/* See read.h. */
9201
9202type_unit_group_unshareable *
9203dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9204{
9205 auto iter = this->m_type_units.find (tu_group);
9206 if (iter != this->m_type_units.end ())
9207 return iter->second.get ();
9208
9209 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9210 type_unit_group_unshareable *result = uniq.get ();
9211 this->m_type_units[tu_group] = std::move (uniq);
9212 return result;
9213}
9214
e286671b
TT
9215struct type *
9216dwarf2_per_objfile::get_type_for_signatured_type
9217 (signatured_type *sig_type) const
9218{
9219 auto iter = this->m_type_map.find (sig_type);
9220 if (iter == this->m_type_map.end ())
9221 return nullptr;
9222
9223 return iter->second;
9224}
9225
9226void dwarf2_per_objfile::set_type_for_signatured_type
9227 (signatured_type *sig_type, struct type *type)
9228{
9229 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9230
9231 this->m_type_map[sig_type] = type;
9232}
9233
95554aad
TT
9234/* A helper function for computing the list of all symbol tables
9235 included by PER_CU. */
9236
9237static void
4c39bc03 9238recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9239 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9240 dwarf2_per_cu_data *per_cu,
9241 dwarf2_per_objfile *per_objfile,
43f3e411 9242 struct compunit_symtab *immediate_parent)
95554aad 9243{
af758d11 9244 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9245 if (*slot != NULL)
9246 {
9247 /* This inclusion and its children have been processed. */
9248 return;
9249 }
9250
9251 *slot = per_cu;
af758d11 9252
95554aad 9253 /* Only add a CU if it has a symbol table. */
43182c09 9254 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9255 if (cust != NULL)
ec94af83
DE
9256 {
9257 /* If this is a type unit only add its symbol table if we haven't
9258 seen it yet (type unit per_cu's can share symtabs). */
9259 if (per_cu->is_debug_types)
9260 {
43f3e411 9261 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9262 if (*slot == NULL)
9263 {
43f3e411 9264 *slot = cust;
4c39bc03 9265 result->push_back (cust);
43f3e411
DE
9266 if (cust->user == NULL)
9267 cust->user = immediate_parent;
ec94af83
DE
9268 }
9269 }
9270 else
f9125b6c 9271 {
4c39bc03 9272 result->push_back (cust);
43f3e411
DE
9273 if (cust->user == NULL)
9274 cust->user = immediate_parent;
f9125b6c 9275 }
ec94af83 9276 }
95554aad 9277
ae640021
AB
9278 if (!per_cu->imported_symtabs_empty ())
9279 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9280 {
9281 recursively_compute_inclusions (result, all_children,
43182c09
SM
9282 all_type_symtabs, ptr, per_objfile,
9283 cust);
ae640021 9284 }
95554aad
TT
9285}
9286
43f3e411 9287/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9288 PER_CU. */
9289
9290static void
43182c09
SM
9291compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9292 dwarf2_per_objfile *per_objfile)
95554aad 9293{
f4dc4d17
DE
9294 gdb_assert (! per_cu->is_debug_types);
9295
ae640021 9296 if (!per_cu->imported_symtabs_empty ())
95554aad 9297 {
ae640021 9298 int len;
4c39bc03 9299 std::vector<compunit_symtab *> result_symtabs;
43182c09 9300 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9301
9302 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9303 if (cust == NULL)
95554aad
TT
9304 return;
9305
280a9412
TT
9306 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9307 htab_eq_pointer,
9308 NULL, xcalloc, xfree));
9309 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9310 htab_eq_pointer,
9311 NULL, xcalloc, xfree));
95554aad 9312
ae640021 9313 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9314 {
280a9412
TT
9315 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9316 all_type_symtabs.get (), ptr,
9317 per_objfile, cust);
ec94af83 9318 }
95554aad 9319
ec94af83 9320 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9321 len = result_symtabs.size ();
43f3e411 9322 cust->includes
f6e649dd 9323 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9324 struct compunit_symtab *, len + 1);
4c39bc03
TT
9325 memcpy (cust->includes, result_symtabs.data (),
9326 len * sizeof (compunit_symtab *));
43f3e411 9327 cust->includes[len] = NULL;
95554aad
TT
9328 }
9329}
9330
9331/* Compute the 'includes' field for the symtabs of all the CUs we just
9332 read. */
9333
9334static void
976ca316 9335process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9336{
976ca316 9337 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9338 {
9339 if (! iter->is_debug_types)
976ca316 9340 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9341 }
95554aad 9342
976ca316 9343 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9344}
9345
8fc0b21d 9346/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9347 already been loaded into memory. */
9348
9349static void
8fc0b21d 9350process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9351{
976ca316
SM
9352 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9353 struct objfile *objfile = per_objfile->objfile;
08feed99 9354 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9355 CORE_ADDR lowpc, highpc;
43f3e411 9356 struct compunit_symtab *cust;
10b3939b 9357 CORE_ADDR baseaddr;
4359dff1 9358 struct block *static_block;
3e29f34a 9359 CORE_ADDR addr;
10b3939b 9360
b3b3bada 9361 baseaddr = objfile->text_section_offset ();
10b3939b 9362
c89b44cd
TT
9363 /* Clear the list here in case something was left over. */
9364 cu->method_list.clear ();
10b3939b 9365
1c47ec3e
TV
9366 dwarf2_find_base_address (cu->dies, cu);
9367
702cf3f5
AB
9368 /* Before we start reading the top-level DIE, ensure it has a valid tag
9369 type. */
9370 switch (cu->dies->tag)
9371 {
9372 case DW_TAG_compile_unit:
9373 case DW_TAG_partial_unit:
9374 case DW_TAG_type_unit:
9375 break;
9376 default:
9377 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9378 dwarf_tag_name (cu->dies->tag),
9379 sect_offset_str (cu->per_cu->sect_off),
9380 objfile_name (per_objfile->objfile));
9381 }
9382
c906108c 9383 /* Do line number decoding in read_file_scope () */
10b3939b 9384 process_die (cu->dies, cu);
c906108c 9385
a766d390 9386 /* For now fudge the Go package. */
3e9f1ca1 9387 if (cu->per_cu->lang == language_go)
a766d390
DE
9388 fixup_go_packaging (cu);
9389
5f48f8f3 9390 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9391 should be complete, and it should now be safe to compute all of the
9392 physnames. */
9393 compute_delayed_physnames (cu);
3da10d80 9394
3e9f1ca1 9395 if (cu->per_cu->lang == language_rust)
c9317f21
TT
9396 rust_union_quirks (cu);
9397
fae299cd
DC
9398 /* Some compilers don't define a DW_AT_high_pc attribute for the
9399 compilation unit. If the DW_AT_high_pc is missing, synthesize
9400 it, by scanning the DIE's below the compilation unit. */
10b3939b 9401 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9402
3e29f34a 9403 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9404 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9405
9406 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9407 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9408 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9409 addrmap to help ensure it has an accurate map of pc values belonging to
9410 this comp unit. */
9411 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9412
c24bdb02 9413 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9414 SECT_OFF_TEXT (objfile),
9415 0);
c906108c 9416
43f3e411 9417 if (cust != NULL)
c906108c 9418 {
df15bd07 9419 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9420
8be455d7
JK
9421 /* Set symtab language to language from DW_AT_language. If the
9422 compilation is from a C file generated by language preprocessors, do
9423 not set the language if it was already deduced by start_subfile. */
3e9f1ca1 9424 if (!(cu->per_cu->lang == language_c
40e3ad0e 9425 && COMPUNIT_FILETABS (cust)->language != language_unknown))
3e9f1ca1 9426 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
8be455d7
JK
9427
9428 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9429 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9430 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9431 there were bugs in prologue debug info, fixed later in GCC-4.5
9432 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9433
9434 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9435 needed, it would be wrong due to missing DW_AT_producer there.
9436
9437 Still one can confuse GDB by using non-standard GCC compilation
9438 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9439 */
ab260dad 9440 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9441 cust->locations_valid = 1;
e0d00bc7
JK
9442
9443 if (gcc_4_minor >= 5)
43f3e411 9444 cust->epilogue_unwind_valid = 1;
96408a79 9445
43f3e411 9446 cust->call_site_htab = cu->call_site_htab;
c906108c 9447 }
9291a0cd 9448
976ca316 9449 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9450
95554aad 9451 /* Push it for inclusion processing later. */
976ca316 9452 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9453
9454 /* Not needed any more. */
c24bdb02 9455 cu->reset_builder ();
f4dc4d17 9456}
45cfd468 9457
8fc0b21d 9458/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9459 already been loaded into memory. */
9460
9461static void
8fc0b21d 9462process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9463 enum language pretend_language)
9464{
976ca316
SM
9465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9466 struct objfile *objfile = per_objfile->objfile;
43f3e411 9467 struct compunit_symtab *cust;
0186c6a7
DE
9468 struct signatured_type *sig_type;
9469
8fc0b21d
SM
9470 gdb_assert (cu->per_cu->is_debug_types);
9471 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9472
c89b44cd
TT
9473 /* Clear the list here in case something was left over. */
9474 cu->method_list.clear ();
f4dc4d17 9475
f4dc4d17
DE
9476 /* The symbol tables are set up in read_type_unit_scope. */
9477 process_die (cu->dies, cu);
9478
9479 /* For now fudge the Go package. */
3e9f1ca1 9480 if (cu->per_cu->lang == language_go)
f4dc4d17
DE
9481 fixup_go_packaging (cu);
9482
5f48f8f3 9483 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9484 should be complete, and it should now be safe to compute all of the
9485 physnames. */
9486 compute_delayed_physnames (cu);
f4dc4d17 9487
3e9f1ca1 9488 if (cu->per_cu->lang == language_rust)
c9317f21
TT
9489 rust_union_quirks (cu);
9490
f4dc4d17
DE
9491 /* TUs share symbol tables.
9492 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9493 of it with end_expandable_symtab. Otherwise, complete the addition of
9494 this TU's symbols to the existing symtab. */
8adb8487 9495 type_unit_group_unshareable *tug_unshare =
976ca316 9496 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9497 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9498 {
c24bdb02
KS
9499 buildsym_compunit *builder = cu->get_builder ();
9500 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9501 tug_unshare->compunit_symtab = cust;
f4dc4d17 9502
43f3e411 9503 if (cust != NULL)
f4dc4d17
DE
9504 {
9505 /* Set symtab language to language from DW_AT_language. If the
9506 compilation is from a C file generated by language preprocessors,
9507 do not set the language if it was already deduced by
9508 start_subfile. */
3e9f1ca1 9509 if (!(cu->per_cu->lang == language_c
43f3e411 9510 && COMPUNIT_FILETABS (cust)->language != language_c))
3e9f1ca1 9511 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
f4dc4d17
DE
9512 }
9513 }
9514 else
9515 {
c24bdb02 9516 cu->get_builder ()->augment_type_symtab ();
8adb8487 9517 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9518 }
9519
976ca316 9520 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9521
9522 /* Not needed any more. */
c24bdb02 9523 cu->reset_builder ();
c906108c
SS
9524}
9525
95554aad
TT
9526/* Process an imported unit DIE. */
9527
9528static void
9529process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9530{
9531 struct attribute *attr;
9532
f4dc4d17
DE
9533 /* For now we don't handle imported units in type units. */
9534 if (cu->per_cu->is_debug_types)
9535 {
9536 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9537 " supported in type units [in module %s]"),
5e22e966 9538 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9539 }
9540
95554aad
TT
9541 attr = dwarf2_attr (die, DW_AT_import, cu);
9542 if (attr != NULL)
9543 {
0826b30a 9544 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9545 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9546 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9547 dwarf2_per_cu_data *per_cu
ab432490 9548 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9549
58990295
TV
9550 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9551 into another compilation unit, at root level. Regard this as a hint,
9552 and ignore it. */
9553 if (die->parent && die->parent->parent == NULL
9554 && per_cu->unit_type == DW_UT_compile
9555 && per_cu->lang == language_cplus)
9556 return;
9557
69d751e3 9558 /* If necessary, add it to the queue and load its DIEs. */
3e9f1ca1
TT
9559 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
9560 cu->per_cu->lang))
4a636814 9561 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
3e9f1ca1 9562 false, cu->per_cu->lang);
95554aad 9563
ae640021 9564 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9565 }
9566}
9567
4c8aa72d
PA
9568/* RAII object that represents a process_die scope: i.e.,
9569 starts/finishes processing a DIE. */
9570class process_die_scope
adde2bff 9571{
4c8aa72d
PA
9572public:
9573 process_die_scope (die_info *die, dwarf2_cu *cu)
9574 : m_die (die), m_cu (cu)
9575 {
9576 /* We should only be processing DIEs not already in process. */
9577 gdb_assert (!m_die->in_process);
9578 m_die->in_process = true;
9579 }
8c3cb9fa 9580
4c8aa72d
PA
9581 ~process_die_scope ()
9582 {
9583 m_die->in_process = false;
9584
9585 /* If we're done processing the DIE for the CU that owns the line
9586 header, we don't need the line header anymore. */
9587 if (m_cu->line_header_die_owner == m_die)
9588 {
9589 delete m_cu->line_header;
9590 m_cu->line_header = NULL;
9591 m_cu->line_header_die_owner = NULL;
9592 }
9593 }
9594
9595private:
9596 die_info *m_die;
9597 dwarf2_cu *m_cu;
9598};
adde2bff 9599
c906108c
SS
9600/* Process a die and its children. */
9601
9602static void
e7c27a73 9603process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9604{
4c8aa72d 9605 process_die_scope scope (die, cu);
adde2bff 9606
c906108c
SS
9607 switch (die->tag)
9608 {
9609 case DW_TAG_padding:
9610 break;
9611 case DW_TAG_compile_unit:
95554aad 9612 case DW_TAG_partial_unit:
e7c27a73 9613 read_file_scope (die, cu);
c906108c 9614 break;
348e048f
DE
9615 case DW_TAG_type_unit:
9616 read_type_unit_scope (die, cu);
9617 break;
c906108c 9618 case DW_TAG_subprogram:
0a4b0913 9619 /* Nested subprograms in Fortran get a prefix. */
3e9f1ca1 9620 if (cu->per_cu->lang == language_fortran
0a4b0913
AB
9621 && die->parent != NULL
9622 && die->parent->tag == DW_TAG_subprogram)
9623 cu->processing_has_namespace_info = true;
9624 /* Fall through. */
c906108c 9625 case DW_TAG_inlined_subroutine:
edb3359d 9626 read_func_scope (die, cu);
c906108c
SS
9627 break;
9628 case DW_TAG_lexical_block:
14898363
L
9629 case DW_TAG_try_block:
9630 case DW_TAG_catch_block:
e7c27a73 9631 read_lexical_block_scope (die, cu);
c906108c 9632 break;
216f72a1 9633 case DW_TAG_call_site:
96408a79
SA
9634 case DW_TAG_GNU_call_site:
9635 read_call_site_scope (die, cu);
9636 break;
c906108c 9637 case DW_TAG_class_type:
680b30c7 9638 case DW_TAG_interface_type:
c906108c
SS
9639 case DW_TAG_structure_type:
9640 case DW_TAG_union_type:
134d01f1 9641 process_structure_scope (die, cu);
c906108c
SS
9642 break;
9643 case DW_TAG_enumeration_type:
134d01f1 9644 process_enumeration_scope (die, cu);
c906108c 9645 break;
134d01f1 9646
f792889a
DJ
9647 /* These dies have a type, but processing them does not create
9648 a symbol or recurse to process the children. Therefore we can
9649 read them on-demand through read_type_die. */
c906108c 9650 case DW_TAG_subroutine_type:
72019c9c 9651 case DW_TAG_set_type:
c906108c 9652 case DW_TAG_pointer_type:
c906108c 9653 case DW_TAG_ptr_to_member_type:
c906108c 9654 case DW_TAG_reference_type:
4297a3f0 9655 case DW_TAG_rvalue_reference_type:
c906108c 9656 case DW_TAG_string_type:
c906108c 9657 break;
134d01f1 9658
d8f62e84
TT
9659 case DW_TAG_array_type:
9660 /* We only need to handle this case for Ada -- in other
9661 languages, it's normal for the compiler to emit a typedef
9662 instead. */
3e9f1ca1 9663 if (cu->per_cu->lang != language_ada)
d8f62e84
TT
9664 break;
9665 /* FALLTHROUGH */
c906108c 9666 case DW_TAG_base_type:
a02abb62 9667 case DW_TAG_subrange_type:
cb249c71 9668 case DW_TAG_typedef:
134d01f1 9669 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 9670 DW_AT_name. */
f792889a 9671 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9672 break;
c906108c 9673 case DW_TAG_common_block:
e7c27a73 9674 read_common_block (die, cu);
c906108c
SS
9675 break;
9676 case DW_TAG_common_inclusion:
9677 break;
d9fa45fe 9678 case DW_TAG_namespace:
9068261f 9679 cu->processing_has_namespace_info = true;
e7c27a73 9680 read_namespace (die, cu);
d9fa45fe 9681 break;
5d7cb8df 9682 case DW_TAG_module:
9068261f 9683 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9684 read_module (die, cu);
9685 break;
d9fa45fe 9686 case DW_TAG_imported_declaration:
9068261f 9687 cu->processing_has_namespace_info = true;
74921315
KS
9688 if (read_namespace_alias (die, cu))
9689 break;
86a73007
TT
9690 /* The declaration is not a global namespace alias. */
9691 /* Fall through. */
d9fa45fe 9692 case DW_TAG_imported_module:
9068261f 9693 cu->processing_has_namespace_info = true;
27aa8d6a 9694 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
3e9f1ca1 9695 || cu->per_cu->lang != language_fortran))
b98664d3 9696 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9697 dwarf_tag_name (die->tag));
9698 read_import_statement (die, cu);
d9fa45fe 9699 break;
95554aad
TT
9700
9701 case DW_TAG_imported_unit:
9702 process_imported_unit_die (die, cu);
9703 break;
9704
71a3c369
TT
9705 case DW_TAG_variable:
9706 read_variable (die, cu);
9707 break;
9708
c906108c 9709 default:
e7c27a73 9710 new_symbol (die, NULL, cu);
c906108c
SS
9711 break;
9712 }
9713}
ca69b9e6
DE
9714\f
9715/* DWARF name computation. */
c906108c 9716
94af9270
KS
9717/* A helper function for dwarf2_compute_name which determines whether DIE
9718 needs to have the name of the scope prepended to the name listed in the
9719 die. */
9720
9721static int
9722die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9723{
1c809c68
TT
9724 struct attribute *attr;
9725
94af9270
KS
9726 switch (die->tag)
9727 {
9728 case DW_TAG_namespace:
9729 case DW_TAG_typedef:
9730 case DW_TAG_class_type:
9731 case DW_TAG_interface_type:
9732 case DW_TAG_structure_type:
9733 case DW_TAG_union_type:
9734 case DW_TAG_enumeration_type:
9735 case DW_TAG_enumerator:
9736 case DW_TAG_subprogram:
08a76f8a 9737 case DW_TAG_inlined_subroutine:
94af9270 9738 case DW_TAG_member:
74921315 9739 case DW_TAG_imported_declaration:
94af9270
KS
9740 return 1;
9741
9742 case DW_TAG_variable:
c2b0a229 9743 case DW_TAG_constant:
94af9270
KS
9744 /* We only need to prefix "globally" visible variables. These include
9745 any variable marked with DW_AT_external or any variable that
9746 lives in a namespace. [Variables in anonymous namespaces
9747 require prefixing, but they are not DW_AT_external.] */
9748
9749 if (dwarf2_attr (die, DW_AT_specification, cu))
9750 {
9751 struct dwarf2_cu *spec_cu = cu;
9a619af0 9752
94af9270
KS
9753 return die_needs_namespace (die_specification (die, &spec_cu),
9754 spec_cu);
9755 }
9756
1c809c68 9757 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9758 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9759 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9760 return 0;
9761 /* A variable in a lexical block of some kind does not need a
9762 namespace, even though in C++ such variables may be external
9763 and have a mangled name. */
9764 if (die->parent->tag == DW_TAG_lexical_block
9765 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9766 || die->parent->tag == DW_TAG_catch_block
9767 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9768 return 0;
9769 return 1;
94af9270
KS
9770
9771 default:
9772 return 0;
9773 }
9774}
9775
73b9be8b
KS
9776/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9777 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9778 defined for the given DIE. */
9779
9780static struct attribute *
9781dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9782{
9783 struct attribute *attr;
9784
9785 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9786 if (attr == NULL)
9787 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9788
9789 return attr;
9790}
9791
9792/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9793 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9794 defined for the given DIE. */
9795
9796static const char *
9797dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9798{
9799 const char *linkage_name;
9800
9801 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9802 if (linkage_name == NULL)
9803 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9804
787de330
TT
9805 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9806 See https://github.com/rust-lang/rust/issues/32925. */
3e9f1ca1 9807 if (cu->per_cu->lang == language_rust && linkage_name != NULL
787de330
TT
9808 && strchr (linkage_name, '{') != NULL)
9809 linkage_name = NULL;
9810
73b9be8b
KS
9811 return linkage_name;
9812}
9813
94af9270 9814/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9815 compute the physname for the object, which include a method's:
9c37b5ae 9816 - formal parameters (C++),
a766d390 9817 - receiver type (Go),
a766d390
DE
9818
9819 The term "physname" is a bit confusing.
9820 For C++, for example, it is the demangled name.
9821 For Go, for example, it's the mangled name.
94af9270 9822
af6b7be1
JB
9823 For Ada, return the DIE's linkage name rather than the fully qualified
9824 name. PHYSNAME is ignored..
9825
5989a64e 9826 The result is allocated on the objfile->per_bfd's obstack and
45940949 9827 canonicalized. */
94af9270
KS
9828
9829static const char *
15d034d0
TT
9830dwarf2_compute_name (const char *name,
9831 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9832 int physname)
9833{
5e22e966 9834 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 9835
94af9270
KS
9836 if (name == NULL)
9837 name = dwarf2_name (die, cu);
9838
3e9f1ca1
TT
9839 enum language lang = cu->per_cu->lang;
9840
2ee7123e
DE
9841 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9842 but otherwise compute it by typename_concat inside GDB.
9843 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 9844 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 9845 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
9846 will set the demangled name to the result of dwarf2_full_name, and it is
9847 the demangled name that GDB uses if it exists. */
3e9f1ca1
TT
9848 if (lang == language_ada
9849 || (lang == language_fortran && physname))
f55ee35c
JK
9850 {
9851 /* For Ada unit, we prefer the linkage name over the name, as
9852 the former contains the exported name, which the user expects
9853 to be able to reference. Ideally, we want the user to be able
9854 to reference this entity using either natural or linkage name,
9855 but we haven't started looking at this enhancement yet. */
73b9be8b 9856 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9857
2ee7123e
DE
9858 if (linkage_name != NULL)
9859 return linkage_name;
f55ee35c
JK
9860 }
9861
94af9270
KS
9862 /* These are the only languages we know how to qualify names in. */
9863 if (name != NULL
3e9f1ca1
TT
9864 && (lang == language_cplus
9865 || lang == language_fortran || lang == language_d
9866 || lang == language_rust))
94af9270
KS
9867 {
9868 if (die_needs_namespace (die, cu))
9869 {
0d5cff50 9870 const char *prefix;
34a68019 9871 const char *canonical_name = NULL;
94af9270 9872
d7e74731
PA
9873 string_file buf;
9874
94af9270 9875 prefix = determine_prefix (die, cu);
94af9270
KS
9876 if (*prefix != '\0')
9877 {
43816ebc
TT
9878 gdb::unique_xmalloc_ptr<char> prefixed_name
9879 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 9880
43816ebc 9881 buf.puts (prefixed_name.get ());
94af9270
KS
9882 }
9883 else
d7e74731 9884 buf.puts (name);
94af9270 9885
98bfdba5
PA
9886 /* Template parameters may be specified in the DIE's DW_AT_name, or
9887 as children with DW_TAG_template_type_param or
9888 DW_TAG_value_type_param. If the latter, add them to the name
9889 here. If the name already has template parameters, then
9890 skip this step; some versions of GCC emit both, and
9891 it is more efficient to use the pre-computed name.
9892
9893 Something to keep in mind about this process: it is very
9894 unlikely, or in some cases downright impossible, to produce
9895 something that will match the mangled name of a function.
9896 If the definition of the function has the same debug info,
9897 we should be able to match up with it anyway. But fallbacks
9898 using the minimal symbol, for instance to find a method
9899 implemented in a stripped copy of libstdc++, will not work.
9900 If we do not have debug info for the definition, we will have to
9901 match them up some other way.
9902
9903 When we do name matching there is a related problem with function
9904 templates; two instantiated function templates are allowed to
9905 differ only by their return types, which we do not add here. */
9906
3e9f1ca1 9907 if (lang == language_cplus && strchr (name, '<') == NULL)
98bfdba5
PA
9908 {
9909 struct attribute *attr;
9910 struct die_info *child;
9911 int first = 1;
9912
9913 die->building_fullname = 1;
9914
9915 for (child = die->child; child != NULL; child = child->sibling)
9916 {
9917 struct type *type;
12df843f 9918 LONGEST value;
d521ce57 9919 const gdb_byte *bytes;
98bfdba5
PA
9920 struct dwarf2_locexpr_baton *baton;
9921 struct value *v;
9922
9923 if (child->tag != DW_TAG_template_type_param
9924 && child->tag != DW_TAG_template_value_param)
9925 continue;
9926
9927 if (first)
9928 {
d7e74731 9929 buf.puts ("<");
98bfdba5
PA
9930 first = 0;
9931 }
9932 else
d7e74731 9933 buf.puts (", ");
98bfdba5
PA
9934
9935 attr = dwarf2_attr (child, DW_AT_type, cu);
9936 if (attr == NULL)
9937 {
b98664d3 9938 complaint (_("template parameter missing DW_AT_type"));
d7e74731 9939 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
9940 continue;
9941 }
9942 type = die_type (child, cu);
9943
9944 if (child->tag == DW_TAG_template_type_param)
9945 {
3e9f1ca1
TT
9946 cu->language_defn->print_type (type, "", &buf, -1, 0,
9947 &type_print_raw_options);
98bfdba5
PA
9948 continue;
9949 }
9950
9951 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9952 if (attr == NULL)
9953 {
b98664d3 9954 complaint (_("template parameter missing "
3e43a32a 9955 "DW_AT_const_value"));
d7e74731 9956 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
9957 continue;
9958 }
9959
9960 dwarf2_const_value_attr (attr, type, name,
9961 &cu->comp_unit_obstack, cu,
9962 &value, &bytes, &baton);
9963
20ce4123 9964 if (type->has_no_signedness ())
98bfdba5
PA
9965 /* GDB prints characters as NUMBER 'CHAR'. If that's
9966 changed, this can use value_print instead. */
3e9f1ca1 9967 cu->language_defn->printchar (value, type, &buf);
98bfdba5
PA
9968 else
9969 {
9970 struct value_print_options opts;
9971
9972 if (baton != NULL)
9973 v = dwarf2_evaluate_loc_desc (type, NULL,
9974 baton->data,
9975 baton->size,
9f47c707
SM
9976 baton->per_cu,
9977 baton->per_objfile);
98bfdba5
PA
9978 else if (bytes != NULL)
9979 {
9980 v = allocate_value (type);
9981 memcpy (value_contents_writeable (v), bytes,
9982 TYPE_LENGTH (type));
9983 }
9984 else
9985 v = value_from_longest (type, value);
9986
3e43a32a
MS
9987 /* Specify decimal so that we do not depend on
9988 the radix. */
98bfdba5
PA
9989 get_formatted_print_options (&opts, 'd');
9990 opts.raw = 1;
d7e74731 9991 value_print (v, &buf, &opts);
98bfdba5 9992 release_value (v);
98bfdba5
PA
9993 }
9994 }
9995
9996 die->building_fullname = 0;
9997
9998 if (!first)
9999 {
10000 /* Close the argument list, with a space if necessary
10001 (nested templates). */
d7e74731
PA
10002 if (!buf.empty () && buf.string ().back () == '>')
10003 buf.puts (" >");
98bfdba5 10004 else
d7e74731 10005 buf.puts (">");
98bfdba5
PA
10006 }
10007 }
10008
9c37b5ae 10009 /* For C++ methods, append formal parameter type
94af9270 10010 information, if PHYSNAME. */
6e70227d 10011
94af9270 10012 if (physname && die->tag == DW_TAG_subprogram
3e9f1ca1 10013 && lang == language_cplus)
94af9270
KS
10014 {
10015 struct type *type = read_type_die (die, cu);
10016
3e9f1ca1 10017 c_type_print_args (type, &buf, 1, lang,
79d43c61 10018 &type_print_raw_options);
94af9270 10019
3e9f1ca1 10020 if (lang == language_cplus)
94af9270 10021 {
60430eff
DJ
10022 /* Assume that an artificial first parameter is
10023 "this", but do not crash if it is not. RealView
10024 marks unnamed (and thus unused) parameters as
10025 artificial; there is no way to differentiate
10026 the two cases. */
1f704f76 10027 if (type->num_fields () > 0
94af9270 10028 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10029 && type->field (0).type ()->code () == TYPE_CODE_PTR
10030 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10031 buf.puts (" const");
94af9270
KS
10032 }
10033 }
10034
d7e74731 10035 const std::string &intermediate_name = buf.string ();
94af9270 10036
3e9f1ca1 10037 if (lang == language_cplus)
34a68019 10038 canonical_name
322a8516 10039 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10040 objfile);
34a68019
TT
10041
10042 /* If we only computed INTERMEDIATE_NAME, or if
10043 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10044 intern it. */
322a8516 10045 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10046 name = objfile->intern (intermediate_name);
34a68019
TT
10047 else
10048 name = canonical_name;
94af9270
KS
10049 }
10050 }
10051
10052 return name;
10053}
10054
0114d602
DJ
10055/* Return the fully qualified name of DIE, based on its DW_AT_name.
10056 If scope qualifiers are appropriate they will be added. The result
34a68019 10057 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10058 not have a name. NAME may either be from a previous call to
10059 dwarf2_name or NULL.
10060
9c37b5ae 10061 The output string will be canonicalized (if C++). */
0114d602
DJ
10062
10063static const char *
15d034d0 10064dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10065{
94af9270
KS
10066 return dwarf2_compute_name (name, die, cu, 0);
10067}
0114d602 10068
94af9270
KS
10069/* Construct a physname for the given DIE in CU. NAME may either be
10070 from a previous call to dwarf2_name or NULL. The result will be
10071 allocated on the objfile_objstack or NULL if the DIE does not have a
10072 name.
0114d602 10073
9c37b5ae 10074 The output string will be canonicalized (if C++). */
0114d602 10075
94af9270 10076static const char *
15d034d0 10077dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10078{
5e22e966 10079 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10080 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10081 int need_copy = 1;
10082
10083 /* In this case dwarf2_compute_name is just a shortcut not building anything
10084 on its own. */
10085 if (!die_needs_namespace (die, cu))
10086 return dwarf2_compute_name (name, die, cu, 1);
10087
3e9f1ca1 10088 if (cu->per_cu->lang != language_rust)
906bb4c5 10089 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10090
10091 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10092 has computed. */
791afaa2 10093 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10094 if (mangled != NULL)
900e11f9 10095 {
3e9f1ca1 10096 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10097 {
10098 /* Do nothing (do not demangle the symbol name). */
10099 }
a766d390
DE
10100 else
10101 {
0eb876f5
JB
10102 /* Use DMGL_RET_DROP for C++ template functions to suppress
10103 their return type. It is easier for GDB users to search
10104 for such functions as `name(params)' than `long name(params)'.
10105 In such case the minimal symbol names do not match the full
10106 symbol names but for template functions there is never a need
10107 to look up their definition from their declaration so
10108 the only disadvantage remains the minimal symbol variant
10109 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10110 demangled.reset (gdb_demangle (mangled,
10111 (DMGL_PARAMS | DMGL_ANSI
10112 | DMGL_RET_DROP)));
a766d390 10113 }
900e11f9 10114 if (demangled)
791afaa2 10115 canon = demangled.get ();
900e11f9
JK
10116 else
10117 {
10118 canon = mangled;
10119 need_copy = 0;
10120 }
10121 }
10122
10123 if (canon == NULL || check_physname)
10124 {
10125 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10126
10127 if (canon != NULL && strcmp (physname, canon) != 0)
10128 {
10129 /* It may not mean a bug in GDB. The compiler could also
10130 compute DW_AT_linkage_name incorrectly. But in such case
10131 GDB would need to be bug-to-bug compatible. */
10132
b98664d3 10133 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10134 "(from linkage <%s>) - DIE at %s [in module %s]"),
10135 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10136 objfile_name (objfile));
900e11f9
JK
10137
10138 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10139 is available here - over computed PHYSNAME. It is safer
10140 against both buggy GDB and buggy compilers. */
10141
10142 retval = canon;
10143 }
10144 else
10145 {
10146 retval = physname;
10147 need_copy = 0;
10148 }
10149 }
10150 else
10151 retval = canon;
10152
10153 if (need_copy)
be1e3d3e 10154 retval = objfile->intern (retval);
900e11f9 10155
900e11f9 10156 return retval;
0114d602
DJ
10157}
10158
74921315
KS
10159/* Inspect DIE in CU for a namespace alias. If one exists, record
10160 a new symbol for it.
10161
10162 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10163
10164static int
10165read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10166{
10167 struct attribute *attr;
10168
10169 /* If the die does not have a name, this is not a namespace
10170 alias. */
10171 attr = dwarf2_attr (die, DW_AT_name, cu);
10172 if (attr != NULL)
10173 {
10174 int num;
10175 struct die_info *d = die;
10176 struct dwarf2_cu *imported_cu = cu;
10177
10178 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10179 keep inspecting DIEs until we hit the underlying import. */
10180#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10181 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10182 {
10183 attr = dwarf2_attr (d, DW_AT_import, cu);
10184 if (attr == NULL)
10185 break;
10186
10187 d = follow_die_ref (d, attr, &imported_cu);
10188 if (d->tag != DW_TAG_imported_declaration)
10189 break;
10190 }
10191
10192 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10193 {
b98664d3 10194 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10195 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10196 return 0;
10197 }
10198
10199 if (attr != NULL)
10200 {
10201 struct type *type;
0826b30a 10202 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10203
aa66c379 10204 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10205 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10206 {
10207 /* This declaration is a global namespace alias. Add
10208 a symbol for it whose type is the aliased namespace. */
10209 new_symbol (die, type, cu);
10210 return 1;
10211 }
10212 }
10213 }
10214
10215 return 0;
10216}
10217
22cee43f 10218/* Return the using directives repository (global or local?) to use in the
804d2729 10219 current context for CU.
22cee43f
PMR
10220
10221 For Ada, imported declarations can materialize renamings, which *may* be
10222 global. However it is impossible (for now?) in DWARF to distinguish
10223 "external" imported declarations and "static" ones. As all imported
10224 declarations seem to be static in all other languages, make them all CU-wide
10225 global only in Ada. */
10226
10227static struct using_direct **
804d2729 10228using_directives (struct dwarf2_cu *cu)
22cee43f 10229{
3e9f1ca1 10230 if (cu->per_cu->lang == language_ada
c24bdb02
KS
10231 && cu->get_builder ()->outermost_context_p ())
10232 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10233 else
c24bdb02 10234 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10235}
10236
27aa8d6a
SW
10237/* Read the import statement specified by the given die and record it. */
10238
10239static void
10240read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10241{
5e22e966 10242 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10243 struct attribute *import_attr;
32019081 10244 struct die_info *imported_die, *child_die;
de4affc9 10245 struct dwarf2_cu *imported_cu;
27aa8d6a 10246 const char *imported_name;
794684b6 10247 const char *imported_name_prefix;
13387711
SW
10248 const char *canonical_name;
10249 const char *import_alias;
10250 const char *imported_declaration = NULL;
794684b6 10251 const char *import_prefix;
eb1e02fd 10252 std::vector<const char *> excludes;
13387711 10253
27aa8d6a
SW
10254 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10255 if (import_attr == NULL)
10256 {
b98664d3 10257 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10258 dwarf_tag_name (die->tag));
10259 return;
10260 }
10261
de4affc9
CC
10262 imported_cu = cu;
10263 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10264 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10265 if (imported_name == NULL)
10266 {
10267 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10268
dda83cd7
SM
10269 The import in the following code:
10270 namespace A
10271 {
10272 typedef int B;
10273 }
10274
10275 int main ()
10276 {
10277 using A::B;
10278 B b;
10279 return b;
10280 }
10281
10282 ...
10283 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10284 <52> DW_AT_decl_file : 1
10285 <53> DW_AT_decl_line : 6
10286 <54> DW_AT_import : <0x75>
10287 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10288 <59> DW_AT_name : B
10289 <5b> DW_AT_decl_file : 1
10290 <5c> DW_AT_decl_line : 2
10291 <5d> DW_AT_type : <0x6e>
10292 ...
10293 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10294 <76> DW_AT_byte_size : 4
10295 <77> DW_AT_encoding : 5 (signed)
10296
10297 imports the wrong die ( 0x75 instead of 0x58 ).
10298 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10299 return;
10300 }
10301
82856980
SW
10302 /* Figure out the local name after import. */
10303 import_alias = dwarf2_name (die, cu);
27aa8d6a 10304
794684b6
SW
10305 /* Figure out where the statement is being imported to. */
10306 import_prefix = determine_prefix (die, cu);
10307
10308 /* Figure out what the scope of the imported die is and prepend it
10309 to the name of the imported die. */
de4affc9 10310 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10311
f55ee35c
JK
10312 if (imported_die->tag != DW_TAG_namespace
10313 && imported_die->tag != DW_TAG_module)
794684b6 10314 {
13387711
SW
10315 imported_declaration = imported_name;
10316 canonical_name = imported_name_prefix;
794684b6 10317 }
13387711 10318 else if (strlen (imported_name_prefix) > 0)
12aaed36 10319 canonical_name = obconcat (&objfile->objfile_obstack,
45280282 10320 imported_name_prefix,
3e9f1ca1
TT
10321 (cu->per_cu->lang == language_d
10322 ? "."
10323 : "::"),
45280282 10324 imported_name, (char *) NULL);
13387711
SW
10325 else
10326 canonical_name = imported_name;
794684b6 10327
3e9f1ca1
TT
10328 if (die->tag == DW_TAG_imported_module
10329 && cu->per_cu->lang == language_fortran)
32019081 10330 for (child_die = die->child; child_die && child_die->tag;
436c571c 10331 child_die = child_die->sibling)
32019081
JK
10332 {
10333 /* DWARF-4: A Fortran use statement with a “rename list” may be
10334 represented by an imported module entry with an import attribute
10335 referring to the module and owned entries corresponding to those
10336 entities that are renamed as part of being imported. */
10337
10338 if (child_die->tag != DW_TAG_imported_declaration)
10339 {
b98664d3 10340 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10341 "- DIE at %s [in module %s]"),
10342 sect_offset_str (child_die->sect_off),
10343 objfile_name (objfile));
32019081
JK
10344 continue;
10345 }
10346
10347 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10348 if (import_attr == NULL)
10349 {
b98664d3 10350 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10351 dwarf_tag_name (child_die->tag));
10352 continue;
10353 }
10354
10355 imported_cu = cu;
10356 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10357 &imported_cu);
10358 imported_name = dwarf2_name (imported_die, imported_cu);
10359 if (imported_name == NULL)
10360 {
b98664d3 10361 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10362 "imported name - DIE at %s [in module %s]"),
10363 sect_offset_str (child_die->sect_off),
10364 objfile_name (objfile));
32019081
JK
10365 continue;
10366 }
10367
eb1e02fd 10368 excludes.push_back (imported_name);
32019081
JK
10369
10370 process_die (child_die, cu);
10371 }
10372
804d2729 10373 add_using_directive (using_directives (cu),
22cee43f
PMR
10374 import_prefix,
10375 canonical_name,
10376 import_alias,
10377 imported_declaration,
10378 excludes,
10379 0,
10380 &objfile->objfile_obstack);
27aa8d6a
SW
10381}
10382
5230b05a
WT
10383/* ICC<14 does not output the required DW_AT_declaration on incomplete
10384 types, but gives them a size of zero. Starting with version 14,
10385 ICC is compatible with GCC. */
10386
9068261f 10387static bool
5230b05a
WT
10388producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10389{
10390 if (!cu->checked_producer)
10391 check_producer (cu);
10392
10393 return cu->producer_is_icc_lt_14;
10394}
10395
eb77c9df
AB
10396/* ICC generates a DW_AT_type for C void functions. This was observed on
10397 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10398 which says that void functions should not have a DW_AT_type. */
10399
10400static bool
10401producer_is_icc (struct dwarf2_cu *cu)
10402{
10403 if (!cu->checked_producer)
10404 check_producer (cu);
10405
10406 return cu->producer_is_icc;
10407}
10408
1b80a9fa
JK
10409/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10410 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10411 this, it was first present in GCC release 4.3.0. */
10412
9068261f 10413static bool
1b80a9fa
JK
10414producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10415{
10416 if (!cu->checked_producer)
10417 check_producer (cu);
10418
10419 return cu->producer_is_gcc_lt_4_3;
10420}
10421
d721ba37
PA
10422static file_and_directory
10423find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10424{
d721ba37
PA
10425 file_and_directory res;
10426
9291a0cd
TT
10427 /* Find the filename. Do not use dwarf2_name here, since the filename
10428 is not a source language identifier. */
d721ba37
PA
10429 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10430 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10431
d721ba37
PA
10432 if (res.comp_dir == NULL
10433 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10434 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10435 {
d721ba37
PA
10436 res.comp_dir_storage = ldirname (res.name);
10437 if (!res.comp_dir_storage.empty ())
10438 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10439 }
d721ba37 10440 if (res.comp_dir != NULL)
9291a0cd
TT
10441 {
10442 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10443 directory, get rid of it. */
d721ba37 10444 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10445
d721ba37
PA
10446 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10447 res.comp_dir = cp + 1;
9291a0cd
TT
10448 }
10449
d721ba37
PA
10450 if (res.name == NULL)
10451 res.name = "<unknown>";
10452
10453 return res;
9291a0cd
TT
10454}
10455
f4dc4d17
DE
10456/* Handle DW_AT_stmt_list for a compilation unit.
10457 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10458 COMP_DIR is the compilation directory. LOWPC is passed to
10459 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10460
10461static void
10462handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10463 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10464{
976ca316 10465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10466 struct attribute *attr;
527f3840
JK
10467 struct line_header line_header_local;
10468 hashval_t line_header_local_hash;
527f3840
JK
10469 void **slot;
10470 int decode_mapping;
2ab95328 10471
f4dc4d17
DE
10472 gdb_assert (! cu->per_cu->is_debug_types);
10473
2ab95328 10474 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 10475 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
10476 return;
10477
d4df075e 10478 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
10479
10480 /* The line header hash table is only created if needed (it exists to
10481 prevent redundant reading of the line table for partial_units).
10482 If we're given a partial_unit, we'll need it. If we're given a
10483 compile_unit, then use the line header hash table if it's already
10484 created, but don't create one just yet. */
10485
976ca316 10486 if (per_objfile->line_header_hash == NULL
527f3840 10487 && die->tag == DW_TAG_partial_unit)
2ab95328 10488 {
976ca316 10489 per_objfile->line_header_hash
d15acc42
TT
10490 .reset (htab_create_alloc (127, line_header_hash_voidp,
10491 line_header_eq_voidp,
ef5f598c 10492 htab_delete_entry<line_header>,
d15acc42 10493 xcalloc, xfree));
527f3840 10494 }
2ab95328 10495
9c541725 10496 line_header_local.sect_off = line_offset;
527f3840
JK
10497 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10498 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10499 if (per_objfile->line_header_hash != NULL)
527f3840 10500 {
976ca316 10501 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10502 &line_header_local,
10503 line_header_local_hash, NO_INSERT);
10504
10505 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10506 is not present in *SLOT (since if there is something in *SLOT then
10507 it will be for a partial_unit). */
10508 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10509 {
527f3840 10510 gdb_assert (*slot != NULL);
9a3c8263 10511 cu->line_header = (struct line_header *) *slot;
527f3840 10512 return;
dee91e82 10513 }
2ab95328 10514 }
527f3840
JK
10515
10516 /* dwarf_decode_line_header does not yet provide sufficient information.
10517 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10518 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10519 if (lh == NULL)
527f3840 10520 return;
4c8aa72d
PA
10521
10522 cu->line_header = lh.release ();
10523 cu->line_header_die_owner = die;
527f3840 10524
976ca316 10525 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10526 slot = NULL;
10527 else
10528 {
976ca316 10529 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10530 &line_header_local,
10531 line_header_local_hash, INSERT);
10532 gdb_assert (slot != NULL);
10533 }
10534 if (slot != NULL && *slot == NULL)
10535 {
10536 /* This newly decoded line number information unit will be owned
10537 by line_header_hash hash table. */
10538 *slot = cu->line_header;
4c8aa72d 10539 cu->line_header_die_owner = NULL;
527f3840
JK
10540 }
10541 else
10542 {
10543 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 10544 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
10545 line_header for this CU - it may happen at most once for each line
10546 number information unit. And if we're not using line_header_hash
10547 then this is what we want as well. */
10548 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10549 }
10550 decode_mapping = (die->tag != DW_TAG_partial_unit);
10551 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10552 decode_mapping);
fff8551c 10553
2ab95328
TT
10554}
10555
95554aad 10556/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10557
c906108c 10558static void
e7c27a73 10559read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10560{
976ca316
SM
10561 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10562 struct objfile *objfile = per_objfile->objfile;
08feed99 10563 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10564 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10565 CORE_ADDR highpc = ((CORE_ADDR) 0);
10566 struct attribute *attr;
c906108c 10567 struct die_info *child_die;
e142c38c 10568 CORE_ADDR baseaddr;
6e70227d 10569
3e9f1ca1 10570 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
b3b3bada 10571 baseaddr = objfile->text_section_offset ();
c906108c 10572
fae299cd 10573 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10574
10575 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10576 from finish_block. */
2acceee2 10577 if (lowpc == ((CORE_ADDR) -1))
c906108c 10578 lowpc = highpc;
3e29f34a 10579 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10580
d721ba37 10581 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10582
c24bdb02 10583 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3 10584
8457e5ec
TV
10585 gdb_assert (per_objfile->sym_cu == nullptr);
10586 scoped_restore restore_sym_cu
10587 = make_scoped_restore (&per_objfile->sym_cu, cu);
10588
3019eac3
DE
10589 /* Decode line number information if present. We do this before
10590 processing child DIEs, so that the line header table is available
10591 for DW_AT_decl_file. */
d721ba37 10592 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10593
10594 /* Process all dies in compilation unit. */
10595 if (die->child != NULL)
10596 {
10597 child_die = die->child;
10598 while (child_die && child_die->tag)
10599 {
10600 process_die (child_die, cu);
436c571c 10601 child_die = child_die->sibling;
3019eac3
DE
10602 }
10603 }
8457e5ec 10604 per_objfile->sym_cu = nullptr;
3019eac3
DE
10605
10606 /* Decode macro information, if present. Dwarf 2 macro information
10607 refers to information in the line number info statement program
10608 header, so we can only read it if we've read the header
10609 successfully. */
0af92d60
JK
10610 attr = dwarf2_attr (die, DW_AT_macros, cu);
10611 if (attr == NULL)
10612 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 10613 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
10614 {
10615 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10616 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10617
529908cb 10618 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
10619 }
10620 else
10621 {
10622 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 10623 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 10624 {
529908cb 10625 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 10626
43f3e411 10627 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10628 }
10629 }
3019eac3
DE
10630}
10631
c24bdb02
KS
10632void
10633dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10634{
f4dc4d17
DE
10635 struct type_unit_group *tu_group;
10636 int first_time;
3019eac3 10637 struct attribute *attr;
9c541725 10638 unsigned int i;
0186c6a7 10639 struct signatured_type *sig_type;
3019eac3 10640
f4dc4d17 10641 gdb_assert (per_cu->is_debug_types);
0186c6a7 10642 sig_type = (struct signatured_type *) per_cu;
3019eac3 10643
c24bdb02 10644 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10645
f4dc4d17 10646 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10647 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10648 if (sig_type->type_unit_group == NULL)
c24bdb02 10649 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10650 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10651
10652 /* If we've already processed this stmt_list there's no real need to
10653 do it again, we could fake it and just recreate the part we need
10654 (file name,index -> symtab mapping). If data shows this optimization
10655 is useful we can do it then. */
8adb8487
TT
10656 type_unit_group_unshareable *tug_unshare
10657 = per_objfile->get_type_unit_group_unshareable (tu_group);
10658 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
10659
10660 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10661 debug info. */
fff8551c 10662 line_header_up lh;
d4df075e 10663 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 10664 {
d4df075e 10665 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 10666 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10667 }
10668 if (lh == NULL)
10669 {
10670 if (first_time)
c24bdb02 10671 start_symtab ("", NULL, 0);
f4dc4d17
DE
10672 else
10673 {
8adb8487 10674 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 10675 gdb_assert (m_builder == nullptr);
8adb8487 10676 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10677 m_builder.reset (new struct buildsym_compunit
10678 (COMPUNIT_OBJFILE (cust), "",
10679 COMPUNIT_DIRNAME (cust),
10680 compunit_language (cust),
10681 0, cust));
770479f2 10682 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10683 }
f4dc4d17 10684 return;
3019eac3
DE
10685 }
10686
c24bdb02
KS
10687 line_header = lh.release ();
10688 line_header_die_owner = die;
3019eac3 10689
f4dc4d17
DE
10690 if (first_time)
10691 {
c24bdb02 10692 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10693
1fd60fc0
DE
10694 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10695 still initializing it, and our caller (a few levels up)
10696 process_full_type_unit still needs to know if this is the first
10697 time. */
10698
8adb8487 10699 tug_unshare->symtabs
4ac93832
TT
10700 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10701 struct symtab *, line_header->file_names_size ());
3019eac3 10702
7ba99d21
AT
10703 auto &file_names = line_header->file_names ();
10704 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10705 {
7ba99d21 10706 file_entry &fe = file_names[i];
c24bdb02
KS
10707 dwarf2_start_subfile (this, fe.name,
10708 fe.include_dir (line_header));
10709 buildsym_compunit *b = get_builder ();
10710 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10711 {
4c8aa72d
PA
10712 /* NOTE: start_subfile will recognize when it's been
10713 passed a file it has already seen. So we can't
10714 assume there's a simple mapping from
10715 cu->line_header->file_names to subfiles, plus
10716 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10717 b->get_current_subfile ()->symtab
10718 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10719 }
10720
c24bdb02 10721 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 10722 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
10723 }
10724 }
10725 else
3019eac3 10726 {
c24bdb02 10727 gdb_assert (m_builder == nullptr);
8adb8487 10728 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10729 m_builder.reset (new struct buildsym_compunit
10730 (COMPUNIT_OBJFILE (cust), "",
10731 COMPUNIT_DIRNAME (cust),
10732 compunit_language (cust),
10733 0, cust));
770479f2 10734 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10735
7ba99d21
AT
10736 auto &file_names = line_header->file_names ();
10737 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10738 {
7ba99d21 10739 file_entry &fe = file_names[i];
8adb8487 10740 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 10741 }
3019eac3
DE
10742 }
10743
f4dc4d17
DE
10744 /* The main symtab is allocated last. Type units don't have DW_AT_name
10745 so they don't have a "real" (so to speak) symtab anyway.
10746 There is later code that will assign the main symtab to all symbols
10747 that don't have one. We need to handle the case of a symbol with a
10748 missing symtab (DW_AT_decl_file) anyway. */
10749}
3019eac3 10750
f4dc4d17
DE
10751/* Process DW_TAG_type_unit.
10752 For TUs we want to skip the first top level sibling if it's not the
10753 actual type being defined by this TU. In this case the first top
10754 level sibling is there to provide context only. */
3019eac3 10755
f4dc4d17
DE
10756static void
10757read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10758{
10759 struct die_info *child_die;
3019eac3 10760
f4dc4d17
DE
10761 prepare_one_comp_unit (cu, die, language_minimal);
10762
10763 /* Initialize (or reinitialize) the machinery for building symtabs.
10764 We do this before processing child DIEs, so that the line header table
10765 is available for DW_AT_decl_file. */
c24bdb02 10766 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10767
10768 if (die->child != NULL)
10769 {
10770 child_die = die->child;
10771 while (child_die && child_die->tag)
10772 {
10773 process_die (child_die, cu);
436c571c 10774 child_die = child_die->sibling;
f4dc4d17
DE
10775 }
10776 }
3019eac3
DE
10777}
10778\f
80626a55
DE
10779/* DWO/DWP files.
10780
10781 http://gcc.gnu.org/wiki/DebugFission
10782 http://gcc.gnu.org/wiki/DebugFissionDWP
10783
10784 To simplify handling of both DWO files ("object" files with the DWARF info)
10785 and DWP files (a file with the DWOs packaged up into one file), we treat
10786 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10787
10788static hashval_t
10789hash_dwo_file (const void *item)
10790{
9a3c8263 10791 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10792 hashval_t hash;
3019eac3 10793
a2ce51a0
DE
10794 hash = htab_hash_string (dwo_file->dwo_name);
10795 if (dwo_file->comp_dir != NULL)
10796 hash += htab_hash_string (dwo_file->comp_dir);
10797 return hash;
3019eac3
DE
10798}
10799
10800static int
10801eq_dwo_file (const void *item_lhs, const void *item_rhs)
10802{
9a3c8263
SM
10803 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10804 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10805
a2ce51a0
DE
10806 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10807 return 0;
10808 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10809 return lhs->comp_dir == rhs->comp_dir;
10810 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10811}
10812
10813/* Allocate a hash table for DWO files. */
10814
51ac9db5 10815static htab_up
298e9637 10816allocate_dwo_file_hash_table ()
3019eac3 10817{
bc68fb19
TT
10818 return htab_up (htab_create_alloc (41,
10819 hash_dwo_file,
10820 eq_dwo_file,
ef5f598c 10821 htab_delete_entry<dwo_file>,
bc68fb19 10822 xcalloc, xfree));
3019eac3
DE
10823}
10824
80626a55
DE
10825/* Lookup DWO file DWO_NAME. */
10826
10827static void **
976ca316 10828lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
10829 const char *dwo_name,
10830 const char *comp_dir)
80626a55
DE
10831{
10832 struct dwo_file find_entry;
10833 void **slot;
10834
976ca316
SM
10835 if (per_objfile->per_bfd->dwo_files == NULL)
10836 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 10837
0ac5b59e
DE
10838 find_entry.dwo_name = dwo_name;
10839 find_entry.comp_dir = comp_dir;
976ca316 10840 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 10841 INSERT);
80626a55
DE
10842
10843 return slot;
10844}
10845
3019eac3
DE
10846static hashval_t
10847hash_dwo_unit (const void *item)
10848{
9a3c8263 10849 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10850
10851 /* This drops the top 32 bits of the id, but is ok for a hash. */
10852 return dwo_unit->signature;
10853}
10854
10855static int
10856eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10857{
9a3c8263
SM
10858 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10859 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
10860
10861 /* The signature is assumed to be unique within the DWO file.
10862 So while object file CU dwo_id's always have the value zero,
10863 that's OK, assuming each object file DWO file has only one CU,
10864 and that's the rule for now. */
10865 return lhs->signature == rhs->signature;
10866}
10867
10868/* Allocate a hash table for DWO CUs,TUs.
10869 There is one of these tables for each of CUs,TUs for each DWO file. */
10870
b0b6a987 10871static htab_up
298e9637 10872allocate_dwo_unit_table ()
3019eac3
DE
10873{
10874 /* Start out with a pretty small number.
10875 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
10876 return htab_up (htab_create_alloc (3,
10877 hash_dwo_unit,
10878 eq_dwo_unit,
10879 NULL, xcalloc, xfree));
3019eac3
DE
10880}
10881
19c3d4c9 10882/* die_reader_func for create_dwo_cu. */
3019eac3
DE
10883
10884static void
19c3d4c9
DE
10885create_dwo_cu_reader (const struct die_reader_specs *reader,
10886 const gdb_byte *info_ptr,
10887 struct die_info *comp_unit_die,
c0ab21c2
TT
10888 struct dwo_file *dwo_file,
10889 struct dwo_unit *dwo_unit)
3019eac3
DE
10890{
10891 struct dwarf2_cu *cu = reader->cu;
9c541725 10892 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 10893 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 10894
a084a2a6
AT
10895 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10896 if (!signature.has_value ())
3019eac3 10897 {
b98664d3 10898 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 10899 " its dwo_id [in module %s]"),
9d8780f0 10900 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
10901 return;
10902 }
10903
3019eac3 10904 dwo_unit->dwo_file = dwo_file;
a084a2a6 10905 dwo_unit->signature = *signature;
8a0459fd 10906 dwo_unit->section = section;
9c541725 10907 dwo_unit->sect_off = sect_off;
3019eac3
DE
10908 dwo_unit->length = cu->per_cu->length;
10909
6f738b01
SM
10910 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10911 sect_offset_str (sect_off),
10912 hex_string (dwo_unit->signature));
3019eac3
DE
10913}
10914
33c5cd75 10915/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 10916 Note: This function processes DWO files only, not DWP files. */
3019eac3 10917
33c5cd75 10918static void
976ca316 10919create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 10920 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 10921 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 10922{
976ca316
SM
10923 struct objfile *objfile = per_objfile->objfile;
10924 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 10925 const gdb_byte *info_ptr, *end_ptr;
3019eac3 10926
96b79293 10927 section.read (objfile);
33c5cd75 10928 info_ptr = section.buffer;
3019eac3
DE
10929
10930 if (info_ptr == NULL)
33c5cd75 10931 return;
3019eac3 10932
6f738b01
SM
10933 dwarf_read_debug_printf ("Reading %s for %s:",
10934 section.get_name (),
10935 section.get_file_name ());
3019eac3 10936
33c5cd75 10937 end_ptr = info_ptr + section.size;
3019eac3
DE
10938 while (info_ptr < end_ptr)
10939 {
10940 struct dwarf2_per_cu_data per_cu;
c0ab21c2 10941 struct dwo_unit read_unit {};
33c5cd75
DB
10942 struct dwo_unit *dwo_unit;
10943 void **slot;
10944 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 10945
1859c670 10946 per_cu.per_bfd = per_bfd;
3019eac3 10947 per_cu.is_debug_types = 0;
33c5cd75
DB
10948 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10949 per_cu.section = &section;
10950
976ca316 10951 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
10952 if (!reader.dummy_p)
10953 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 10954 &dwo_file, &read_unit);
33c5cd75
DB
10955 info_ptr += per_cu.length;
10956
10957 // If the unit could not be parsed, skip it.
c0ab21c2 10958 if (read_unit.dwo_file == NULL)
33c5cd75 10959 continue;
3019eac3 10960
33c5cd75 10961 if (cus_htab == NULL)
298e9637 10962 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 10963
1859c670 10964 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 10965 struct dwo_unit);
c0ab21c2 10966 *dwo_unit = read_unit;
b0b6a987 10967 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
10968 gdb_assert (slot != NULL);
10969 if (*slot != NULL)
19c3d4c9 10970 {
33c5cd75
DB
10971 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10972 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 10973
b98664d3 10974 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
10975 " the entry at offset %s, signature %s"),
10976 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 10977 hex_string (dwo_unit->signature));
19c3d4c9 10978 }
33c5cd75 10979 *slot = (void *)dwo_unit;
3019eac3 10980 }
3019eac3
DE
10981}
10982
80626a55
DE
10983/* DWP file .debug_{cu,tu}_index section format:
10984 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
10985 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10986
10987 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10988 officially standard DWP format was published with DWARF v5 and is called
10989 Version 5. There are no versions 3 or 4.
80626a55 10990
d2415c6c
DE
10991 DWP Version 1:
10992
80626a55
DE
10993 Both index sections have the same format, and serve to map a 64-bit
10994 signature to a set of section numbers. Each section begins with a header,
10995 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10996 indexes, and a pool of 32-bit section numbers. The index sections will be
10997 aligned at 8-byte boundaries in the file.
10998
d2415c6c
DE
10999 The index section header consists of:
11000
11001 V, 32 bit version number
11002 -, 32 bits unused
11003 N, 32 bit number of compilation units or type units in the index
11004 M, 32 bit number of slots in the hash table
80626a55 11005
d2415c6c 11006 Numbers are recorded using the byte order of the application binary.
80626a55 11007
d2415c6c
DE
11008 The hash table begins at offset 16 in the section, and consists of an array
11009 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11010 order of the application binary). Unused slots in the hash table are 0.
11011 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11012
d2415c6c
DE
11013 The parallel table begins immediately after the hash table
11014 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11015 array of 32-bit indexes (using the byte order of the application binary),
11016 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11017 table contains a 32-bit index into the pool of section numbers. For unused
11018 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11019
73869dc2
DE
11020 The pool of section numbers begins immediately following the hash table
11021 (at offset 16 + 12 * M from the beginning of the section). The pool of
11022 section numbers consists of an array of 32-bit words (using the byte order
11023 of the application binary). Each item in the array is indexed starting
11024 from 0. The hash table entry provides the index of the first section
11025 number in the set. Additional section numbers in the set follow, and the
11026 set is terminated by a 0 entry (section number 0 is not used in ELF).
11027
11028 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11029 section must be the first entry in the set, and the .debug_abbrev.dwo must
11030 be the second entry. Other members of the set may follow in any order.
11031
11032 ---
11033
d2854d8d 11034 DWP Versions 2 and 5:
73869dc2 11035
d2854d8d 11036 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11037 and the entries in the index tables are now offsets into these sections.
11038 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11039 section.
11040
11041 Index Section Contents:
11042 Header
11043 Hash Table of Signatures dwp_hash_table.hash_table
11044 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11045 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11046 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11047
11048 The index section header consists of:
11049
11050 V, 32 bit version number
11051 L, 32 bit number of columns in the table of section offsets
11052 N, 32 bit number of compilation units or type units in the index
11053 M, 32 bit number of slots in the hash table
11054
11055 Numbers are recorded using the byte order of the application binary.
11056
11057 The hash table has the same format as version 1.
11058 The parallel table of indices has the same format as version 1,
11059 except that the entries are origin-1 indices into the table of sections
11060 offsets and the table of section sizes.
11061
11062 The table of offsets begins immediately following the parallel table
11063 (at offset 16 + 12 * M from the beginning of the section). The table is
11064 a two-dimensional array of 32-bit words (using the byte order of the
11065 application binary), with L columns and N+1 rows, in row-major order.
11066 Each row in the array is indexed starting from 0. The first row provides
11067 a key to the remaining rows: each column in this row provides an identifier
11068 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11069 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11070
11071 DW_SECT_INFO 1 .debug_info.dwo
11072 DW_SECT_TYPES 2 .debug_types.dwo
11073 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11074 DW_SECT_LINE 4 .debug_line.dwo
11075 DW_SECT_LOC 5 .debug_loc.dwo
11076 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11077 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11078 DW_SECT_MACRO 8 .debug_macro.dwo
11079
d2854d8d
CT
11080 The section identifiers for Version 5 are:
11081
11082 DW_SECT_INFO_V5 1 .debug_info.dwo
11083 DW_SECT_RESERVED_V5 2 --
11084 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11085 DW_SECT_LINE_V5 4 .debug_line.dwo
11086 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11087 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11088 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11089 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11090
73869dc2
DE
11091 The offsets provided by the CU and TU index sections are the base offsets
11092 for the contributions made by each CU or TU to the corresponding section
11093 in the package file. Each CU and TU header contains an abbrev_offset
11094 field, used to find the abbreviations table for that CU or TU within the
11095 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11096 be interpreted as relative to the base offset given in the index section.
11097 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11098 should be interpreted as relative to the base offset for .debug_line.dwo,
11099 and offsets into other debug sections obtained from DWARF attributes should
11100 also be interpreted as relative to the corresponding base offset.
11101
11102 The table of sizes begins immediately following the table of offsets.
11103 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11104 with L columns and N rows, in row-major order. Each row in the array is
11105 indexed starting from 1 (row 0 is shared by the two tables).
11106
11107 ---
11108
11109 Hash table lookup is handled the same in version 1 and 2:
11110
11111 We assume that N and M will not exceed 2^32 - 1.
11112 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11113
d2415c6c
DE
11114 Given a 64-bit compilation unit signature or a type signature S, an entry
11115 in the hash table is located as follows:
80626a55 11116
d2415c6c
DE
11117 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11118 the low-order k bits all set to 1.
80626a55 11119
d2415c6c 11120 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11121
d2415c6c
DE
11122 3) If the hash table entry at index H matches the signature, use that
11123 entry. If the hash table entry at index H is unused (all zeroes),
11124 terminate the search: the signature is not present in the table.
80626a55 11125
d2415c6c 11126 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11127
d2415c6c 11128 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11129 to stop at an unused slot or find the match. */
80626a55
DE
11130
11131/* Create a hash table to map DWO IDs to their CU/TU entry in
11132 .debug_{info,types}.dwo in DWP_FILE.
11133 Returns NULL if there isn't one.
11134 Note: This function processes DWP files only, not DWO files. */
11135
11136static struct dwp_hash_table *
976ca316 11137create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11138 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11139{
976ca316 11140 struct objfile *objfile = per_objfile->objfile;
400174b1 11141 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11142 const gdb_byte *index_ptr, *index_end;
80626a55 11143 struct dwarf2_section_info *index;
73869dc2 11144 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11145 struct dwp_hash_table *htab;
11146
11147 if (is_debug_types)
11148 index = &dwp_file->sections.tu_index;
11149 else
11150 index = &dwp_file->sections.cu_index;
11151
96b79293 11152 if (index->empty ())
80626a55 11153 return NULL;
96b79293 11154 index->read (objfile);
80626a55
DE
11155
11156 index_ptr = index->buffer;
11157 index_end = index_ptr + index->size;
11158
d2854d8d
CT
11159 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11160 For now it's safe to just read 4 bytes (particularly as it's difficult to
11161 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11162 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11163 index_ptr += 4;
d2854d8d 11164 if (version == 2 || version == 5)
73869dc2
DE
11165 nr_columns = read_4_bytes (dbfd, index_ptr);
11166 else
11167 nr_columns = 0;
11168 index_ptr += 4;
80626a55
DE
11169 nr_units = read_4_bytes (dbfd, index_ptr);
11170 index_ptr += 4;
11171 nr_slots = read_4_bytes (dbfd, index_ptr);
11172 index_ptr += 4;
11173
d2854d8d 11174 if (version != 1 && version != 2 && version != 5)
80626a55 11175 {
21aa081e 11176 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11177 " [in module %s]"),
21aa081e 11178 pulongest (version), dwp_file->name);
80626a55
DE
11179 }
11180 if (nr_slots != (nr_slots & -nr_slots))
11181 {
21aa081e 11182 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11183 " is not power of 2 [in module %s]"),
21aa081e 11184 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11185 }
11186
976ca316 11187 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11188 htab->version = version;
11189 htab->nr_columns = nr_columns;
80626a55
DE
11190 htab->nr_units = nr_units;
11191 htab->nr_slots = nr_slots;
11192 htab->hash_table = index_ptr;
11193 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11194
11195 /* Exit early if the table is empty. */
11196 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11197 || (version == 2 && nr_columns == 0)
11198 || (version == 5 && nr_columns == 0))
73869dc2
DE
11199 {
11200 /* All must be zero. */
11201 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11202 || (version == 2 && nr_columns != 0)
11203 || (version == 5 && nr_columns != 0))
73869dc2 11204 {
b98664d3 11205 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11206 " all zero [in modules %s]"),
11207 dwp_file->name);
11208 }
11209 return htab;
11210 }
11211
11212 if (version == 1)
11213 {
11214 htab->section_pool.v1.indices =
11215 htab->unit_table + sizeof (uint32_t) * nr_slots;
11216 /* It's harder to decide whether the section is too small in v1.
11217 V1 is deprecated anyway so we punt. */
11218 }
d2854d8d 11219 else if (version == 2)
73869dc2
DE
11220 {
11221 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11222 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11223 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11224 /* Reverse map for error checking. */
11225 int ids_seen[DW_SECT_MAX + 1];
11226 int i;
11227
11228 if (nr_columns < 2)
11229 {
11230 error (_("Dwarf Error: bad DWP hash table, too few columns"
11231 " in section table [in module %s]"),
11232 dwp_file->name);
11233 }
11234 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11235 {
11236 error (_("Dwarf Error: bad DWP hash table, too many columns"
11237 " in section table [in module %s]"),
11238 dwp_file->name);
11239 }
04fd5eed
GB
11240 memset (ids, 255, sizeof_ids);
11241 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11242 for (i = 0; i < nr_columns; ++i)
11243 {
11244 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11245
11246 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11247 {
11248 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11249 " in section table [in module %s]"),
11250 id, dwp_file->name);
11251 }
11252 if (ids_seen[id] != -1)
11253 {
11254 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11255 " id %d in section table [in module %s]"),
11256 id, dwp_file->name);
11257 }
11258 ids_seen[id] = i;
11259 ids[i] = id;
11260 }
11261 /* Must have exactly one info or types section. */
11262 if (((ids_seen[DW_SECT_INFO] != -1)
11263 + (ids_seen[DW_SECT_TYPES] != -1))
11264 != 1)
11265 {
11266 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11267 " DWO info/types section [in module %s]"),
11268 dwp_file->name);
11269 }
11270 /* Must have an abbrev section. */
11271 if (ids_seen[DW_SECT_ABBREV] == -1)
11272 {
11273 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11274 " section [in module %s]"),
11275 dwp_file->name);
11276 }
11277 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11278 htab->section_pool.v2.sizes =
11279 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11280 * nr_units * nr_columns);
11281 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11282 * nr_units * nr_columns))
11283 > index_end)
11284 {
11285 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11286 " [in module %s]"),
11287 dwp_file->name);
11288 }
11289 }
d2854d8d
CT
11290 else /* version == 5 */
11291 {
11292 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11293 int *ids = htab->section_pool.v5.section_ids;
11294 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11295 /* Reverse map for error checking. */
11296 int ids_seen[DW_SECT_MAX_V5 + 1];
11297
11298 if (nr_columns < 2)
11299 {
11300 error (_("Dwarf Error: bad DWP hash table, too few columns"
11301 " in section table [in module %s]"),
11302 dwp_file->name);
11303 }
11304 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11305 {
11306 error (_("Dwarf Error: bad DWP hash table, too many columns"
11307 " in section table [in module %s]"),
11308 dwp_file->name);
11309 }
11310 memset (ids, 255, sizeof_ids);
11311 memset (ids_seen, 255, sizeof (ids_seen));
11312 for (int i = 0; i < nr_columns; ++i)
11313 {
11314 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11315
11316 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11317 {
11318 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11319 " in section table [in module %s]"),
11320 id, dwp_file->name);
11321 }
11322 if (ids_seen[id] != -1)
11323 {
11324 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11325 " id %d in section table [in module %s]"),
11326 id, dwp_file->name);
11327 }
11328 ids_seen[id] = i;
11329 ids[i] = id;
11330 }
11331 /* Must have seen an info section. */
11332 if (ids_seen[DW_SECT_INFO_V5] == -1)
11333 {
11334 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11335 " DWO info/types section [in module %s]"),
11336 dwp_file->name);
11337 }
11338 /* Must have an abbrev section. */
11339 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11340 {
11341 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11342 " section [in module %s]"),
11343 dwp_file->name);
11344 }
11345 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11346 htab->section_pool.v5.sizes
11347 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11348 * nr_units * nr_columns);
11349 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11350 * nr_units * nr_columns))
11351 > index_end)
11352 {
11353 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11354 " [in module %s]"),
11355 dwp_file->name);
11356 }
11357 }
80626a55
DE
11358
11359 return htab;
11360}
11361
11362/* Update SECTIONS with the data from SECTP.
11363
5bb6e9dd
TT
11364 This function is like the other "locate" section routines, but in
11365 this context the sections to read comes from the DWP V1 hash table,
11366 not the full ELF section table.
80626a55
DE
11367
11368 The result is non-zero for success, or zero if an error was found. */
11369
11370static int
73869dc2
DE
11371locate_v1_virtual_dwo_sections (asection *sectp,
11372 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11373{
11374 const struct dwop_section_names *names = &dwop_section_names;
11375
fbedd546 11376 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11377 {
11378 /* There can be only one. */
049412e3 11379 if (sections->abbrev.s.section != NULL)
80626a55 11380 return 0;
049412e3 11381 sections->abbrev.s.section = sectp;
fd361982 11382 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11383 }
fbedd546
TT
11384 else if (names->info_dwo.matches (sectp->name)
11385 || names->types_dwo.matches (sectp->name))
80626a55
DE
11386 {
11387 /* There can be only one. */
049412e3 11388 if (sections->info_or_types.s.section != NULL)
80626a55 11389 return 0;
049412e3 11390 sections->info_or_types.s.section = sectp;
fd361982 11391 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11392 }
fbedd546 11393 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
11394 {
11395 /* There can be only one. */
049412e3 11396 if (sections->line.s.section != NULL)
80626a55 11397 return 0;
049412e3 11398 sections->line.s.section = sectp;
fd361982 11399 sections->line.size = bfd_section_size (sectp);
80626a55 11400 }
fbedd546 11401 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
11402 {
11403 /* There can be only one. */
049412e3 11404 if (sections->loc.s.section != NULL)
80626a55 11405 return 0;
049412e3 11406 sections->loc.s.section = sectp;
fd361982 11407 sections->loc.size = bfd_section_size (sectp);
80626a55 11408 }
fbedd546 11409 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
11410 {
11411 /* There can be only one. */
049412e3 11412 if (sections->macinfo.s.section != NULL)
80626a55 11413 return 0;
049412e3 11414 sections->macinfo.s.section = sectp;
fd361982 11415 sections->macinfo.size = bfd_section_size (sectp);
80626a55 11416 }
fbedd546 11417 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
11418 {
11419 /* There can be only one. */
049412e3 11420 if (sections->macro.s.section != NULL)
80626a55 11421 return 0;
049412e3 11422 sections->macro.s.section = sectp;
fd361982 11423 sections->macro.size = bfd_section_size (sectp);
80626a55 11424 }
fbedd546 11425 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
11426 {
11427 /* There can be only one. */
049412e3 11428 if (sections->str_offsets.s.section != NULL)
80626a55 11429 return 0;
049412e3 11430 sections->str_offsets.s.section = sectp;
fd361982 11431 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11432 }
11433 else
11434 {
11435 /* No other kind of section is valid. */
11436 return 0;
11437 }
11438
11439 return 1;
11440}
11441
73869dc2
DE
11442/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11443 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11444 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11445 This is for DWP version 1 files. */
80626a55
DE
11446
11447static struct dwo_unit *
976ca316 11448create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11449 struct dwp_file *dwp_file,
73869dc2
DE
11450 uint32_t unit_index,
11451 const char *comp_dir,
11452 ULONGEST signature, int is_debug_types)
80626a55 11453{
73869dc2
DE
11454 const struct dwp_hash_table *dwp_htab =
11455 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11456 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11457 const char *kind = is_debug_types ? "TU" : "CU";
11458 struct dwo_file *dwo_file;
11459 struct dwo_unit *dwo_unit;
73869dc2 11460 struct virtual_v1_dwo_sections sections;
80626a55 11461 void **dwo_file_slot;
80626a55
DE
11462 int i;
11463
73869dc2
DE
11464 gdb_assert (dwp_file->version == 1);
11465
6f738b01
SM
11466 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11467 kind, pulongest (unit_index), hex_string (signature),
11468 dwp_file->name);
80626a55 11469
19ac8c2e 11470 /* Fetch the sections of this DWO unit.
80626a55
DE
11471 Put a limit on the number of sections we look for so that bad data
11472 doesn't cause us to loop forever. */
11473
73869dc2 11474#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11475 (1 /* .debug_info or .debug_types */ \
11476 + 1 /* .debug_abbrev */ \
11477 + 1 /* .debug_line */ \
11478 + 1 /* .debug_loc */ \
11479 + 1 /* .debug_str_offsets */ \
19ac8c2e 11480 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11481 + 1 /* trailing zero */)
11482
11483 memset (&sections, 0, sizeof (sections));
80626a55 11484
73869dc2 11485 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11486 {
11487 asection *sectp;
11488 uint32_t section_nr =
11489 read_4_bytes (dbfd,
73869dc2
DE
11490 dwp_htab->section_pool.v1.indices
11491 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11492
11493 if (section_nr == 0)
11494 break;
11495 if (section_nr >= dwp_file->num_sections)
11496 {
11497 error (_("Dwarf Error: bad DWP hash table, section number too large"
11498 " [in module %s]"),
11499 dwp_file->name);
11500 }
11501
11502 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11503 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11504 {
11505 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11506 " [in module %s]"),
11507 dwp_file->name);
11508 }
11509 }
11510
11511 if (i < 2
96b79293
TT
11512 || sections.info_or_types.empty ()
11513 || sections.abbrev.empty ())
80626a55
DE
11514 {
11515 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11516 " [in module %s]"),
11517 dwp_file->name);
11518 }
73869dc2 11519 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11520 {
11521 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11522 " [in module %s]"),
11523 dwp_file->name);
11524 }
11525
11526 /* It's easier for the rest of the code if we fake a struct dwo_file and
11527 have dwo_unit "live" in that. At least for now.
11528
11529 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11530 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11531 file, we can combine them back into a virtual DWO file to save space
11532 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11533 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11534
791afaa2
TT
11535 std::string virtual_dwo_name =
11536 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11537 sections.abbrev.get_id (),
11538 sections.line.get_id (),
11539 sections.loc.get_id (),
11540 sections.str_offsets.get_id ());
80626a55 11541 /* Can we use an existing virtual DWO file? */
976ca316 11542 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11543 comp_dir);
80626a55
DE
11544 /* Create one if necessary. */
11545 if (*dwo_file_slot == NULL)
11546 {
6f738b01
SM
11547 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11548 virtual_dwo_name.c_str ());
11549
51ac9db5 11550 dwo_file = new struct dwo_file;
976ca316 11551 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11552 dwo_file->comp_dir = comp_dir;
80626a55
DE
11553 dwo_file->sections.abbrev = sections.abbrev;
11554 dwo_file->sections.line = sections.line;
11555 dwo_file->sections.loc = sections.loc;
11556 dwo_file->sections.macinfo = sections.macinfo;
11557 dwo_file->sections.macro = sections.macro;
11558 dwo_file->sections.str_offsets = sections.str_offsets;
11559 /* The "str" section is global to the entire DWP file. */
11560 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11561 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11562 there's no need to record it in dwo_file.
11563 Also, we can't simply record type sections in dwo_file because
11564 we record a pointer into the vector in dwo_unit. As we collect more
11565 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11566 for it, invalidating all copies of pointers into the previous
11567 contents. */
80626a55
DE
11568 *dwo_file_slot = dwo_file;
11569 }
11570 else
11571 {
6f738b01
SM
11572 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11573 virtual_dwo_name.c_str ());
11574
9a3c8263 11575 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11576 }
80626a55 11577
976ca316 11578 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11579 dwo_unit->dwo_file = dwo_file;
11580 dwo_unit->signature = signature;
8d749320 11581 dwo_unit->section =
976ca316 11582 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11583 *dwo_unit->section = sections.info_or_types;
57d63ce2 11584 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11585
11586 return dwo_unit;
11587}
11588
d2854d8d
CT
11589/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11590 simplify them. Given a pointer to the containing section SECTION, and
11591 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11592 virtual section of just that piece. */
73869dc2
DE
11593
11594static struct dwarf2_section_info
d2854d8d
CT
11595create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11596 struct dwarf2_section_info *section,
11597 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
11598{
11599 struct dwarf2_section_info result;
11600 asection *sectp;
11601
11602 gdb_assert (section != NULL);
11603 gdb_assert (!section->is_virtual);
11604
11605 memset (&result, 0, sizeof (result));
11606 result.s.containing_section = section;
dc4ccb6f 11607 result.is_virtual = true;
73869dc2
DE
11608
11609 if (size == 0)
11610 return result;
11611
96b79293 11612 sectp = section->get_bfd_section ();
73869dc2
DE
11613
11614 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11615 bounds of the real section. This is a pretty-rare event, so just
11616 flag an error (easier) instead of a warning and trying to cope. */
11617 if (sectp == NULL
fd361982 11618 || offset + size > bfd_section_size (sectp))
73869dc2 11619 {
d2854d8d 11620 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 11621 " in section %s [in module %s]"),
fd361982 11622 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 11623 objfile_name (per_objfile->objfile));
73869dc2
DE
11624 }
11625
11626 result.virtual_offset = offset;
11627 result.size = size;
11628 return result;
11629}
11630
11631/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11632 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11633 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11634 This is for DWP version 2 files. */
11635
11636static struct dwo_unit *
976ca316 11637create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 11638 struct dwp_file *dwp_file,
73869dc2
DE
11639 uint32_t unit_index,
11640 const char *comp_dir,
11641 ULONGEST signature, int is_debug_types)
11642{
73869dc2
DE
11643 const struct dwp_hash_table *dwp_htab =
11644 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11645 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11646 const char *kind = is_debug_types ? "TU" : "CU";
11647 struct dwo_file *dwo_file;
11648 struct dwo_unit *dwo_unit;
d2854d8d 11649 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 11650 void **dwo_file_slot;
73869dc2
DE
11651 int i;
11652
11653 gdb_assert (dwp_file->version == 2);
11654
6f738b01
SM
11655 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11656 kind, pulongest (unit_index), hex_string (signature),
11657 dwp_file->name);
73869dc2
DE
11658
11659 /* Fetch the section offsets of this DWO unit. */
11660
11661 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11662
11663 for (i = 0; i < dwp_htab->nr_columns; ++i)
11664 {
11665 uint32_t offset = read_4_bytes (dbfd,
11666 dwp_htab->section_pool.v2.offsets
11667 + (((unit_index - 1) * dwp_htab->nr_columns
11668 + i)
11669 * sizeof (uint32_t)));
11670 uint32_t size = read_4_bytes (dbfd,
11671 dwp_htab->section_pool.v2.sizes
11672 + (((unit_index - 1) * dwp_htab->nr_columns
11673 + i)
11674 * sizeof (uint32_t)));
11675
11676 switch (dwp_htab->section_pool.v2.section_ids[i])
11677 {
11678 case DW_SECT_INFO:
11679 case DW_SECT_TYPES:
11680 sections.info_or_types_offset = offset;
11681 sections.info_or_types_size = size;
11682 break;
11683 case DW_SECT_ABBREV:
11684 sections.abbrev_offset = offset;
11685 sections.abbrev_size = size;
11686 break;
11687 case DW_SECT_LINE:
11688 sections.line_offset = offset;
11689 sections.line_size = size;
11690 break;
11691 case DW_SECT_LOC:
11692 sections.loc_offset = offset;
11693 sections.loc_size = size;
11694 break;
11695 case DW_SECT_STR_OFFSETS:
11696 sections.str_offsets_offset = offset;
11697 sections.str_offsets_size = size;
11698 break;
11699 case DW_SECT_MACINFO:
11700 sections.macinfo_offset = offset;
11701 sections.macinfo_size = size;
11702 break;
11703 case DW_SECT_MACRO:
11704 sections.macro_offset = offset;
11705 sections.macro_size = size;
11706 break;
11707 }
11708 }
11709
11710 /* It's easier for the rest of the code if we fake a struct dwo_file and
11711 have dwo_unit "live" in that. At least for now.
11712
11713 The DWP file can be made up of a random collection of CUs and TUs.
11714 However, for each CU + set of TUs that came from the same original DWO
11715 file, we can combine them back into a virtual DWO file to save space
11716 (fewer struct dwo_file objects to allocate). Remember that for really
11717 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11718
791afaa2
TT
11719 std::string virtual_dwo_name =
11720 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11721 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11722 (long) (sections.line_size ? sections.line_offset : 0),
11723 (long) (sections.loc_size ? sections.loc_offset : 0),
11724 (long) (sections.str_offsets_size
11725 ? sections.str_offsets_offset : 0));
73869dc2 11726 /* Can we use an existing virtual DWO file? */
976ca316 11727 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11728 comp_dir);
73869dc2
DE
11729 /* Create one if necessary. */
11730 if (*dwo_file_slot == NULL)
11731 {
6f738b01
SM
11732 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11733 virtual_dwo_name.c_str ());
11734
51ac9db5 11735 dwo_file = new struct dwo_file;
976ca316 11736 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
11737 dwo_file->comp_dir = comp_dir;
11738 dwo_file->sections.abbrev =
d2854d8d
CT
11739 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11740 sections.abbrev_offset,
11741 sections.abbrev_size);
73869dc2 11742 dwo_file->sections.line =
d2854d8d
CT
11743 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11744 sections.line_offset,
11745 sections.line_size);
73869dc2 11746 dwo_file->sections.loc =
d2854d8d
CT
11747 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11748 sections.loc_offset, sections.loc_size);
73869dc2 11749 dwo_file->sections.macinfo =
d2854d8d
CT
11750 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11751 sections.macinfo_offset,
11752 sections.macinfo_size);
73869dc2 11753 dwo_file->sections.macro =
d2854d8d
CT
11754 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11755 sections.macro_offset,
11756 sections.macro_size);
73869dc2 11757 dwo_file->sections.str_offsets =
d2854d8d
CT
11758 create_dwp_v2_or_v5_section (per_objfile,
11759 &dwp_file->sections.str_offsets,
11760 sections.str_offsets_offset,
11761 sections.str_offsets_size);
73869dc2
DE
11762 /* The "str" section is global to the entire DWP file. */
11763 dwo_file->sections.str = dwp_file->sections.str;
11764 /* The info or types section is assigned below to dwo_unit,
11765 there's no need to record it in dwo_file.
11766 Also, we can't simply record type sections in dwo_file because
11767 we record a pointer into the vector in dwo_unit. As we collect more
11768 types we'll grow the vector and eventually have to reallocate space
11769 for it, invalidating all copies of pointers into the previous
11770 contents. */
11771 *dwo_file_slot = dwo_file;
11772 }
11773 else
11774 {
6f738b01
SM
11775 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11776 virtual_dwo_name.c_str ());
11777
9a3c8263 11778 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11779 }
73869dc2 11780
976ca316 11781 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
11782 dwo_unit->dwo_file = dwo_file;
11783 dwo_unit->signature = signature;
8d749320 11784 dwo_unit->section =
976ca316 11785 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 11786 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 11787 (per_objfile,
d2854d8d
CT
11788 is_debug_types
11789 ? &dwp_file->sections.types
11790 : &dwp_file->sections.info,
11791 sections.info_or_types_offset,
11792 sections.info_or_types_size);
11793 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11794
11795 return dwo_unit;
11796}
11797
11798/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11799 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11800 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11801 This is for DWP version 5 files. */
11802
11803static struct dwo_unit *
11804create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11805 struct dwp_file *dwp_file,
11806 uint32_t unit_index,
11807 const char *comp_dir,
11808 ULONGEST signature, int is_debug_types)
11809{
11810 const struct dwp_hash_table *dwp_htab
11811 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11812 bfd *dbfd = dwp_file->dbfd.get ();
11813 const char *kind = is_debug_types ? "TU" : "CU";
11814 struct dwo_file *dwo_file;
11815 struct dwo_unit *dwo_unit;
11816 struct virtual_v2_or_v5_dwo_sections sections {};
11817 void **dwo_file_slot;
11818
11819 gdb_assert (dwp_file->version == 5);
11820
6f738b01
SM
11821 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11822 kind, pulongest (unit_index), hex_string (signature),
11823 dwp_file->name);
d2854d8d
CT
11824
11825 /* Fetch the section offsets of this DWO unit. */
11826
11827 /* memset (&sections, 0, sizeof (sections)); */
11828
11829 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11830 {
11831 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
11832 dwp_htab->section_pool.v5.offsets
11833 + (((unit_index - 1)
11834 * dwp_htab->nr_columns
11835 + i)
11836 * sizeof (uint32_t)));
d2854d8d 11837 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
11838 dwp_htab->section_pool.v5.sizes
11839 + (((unit_index - 1) * dwp_htab->nr_columns
11840 + i)
11841 * sizeof (uint32_t)));
d2854d8d
CT
11842
11843 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
11844 {
11845 case DW_SECT_ABBREV_V5:
11846 sections.abbrev_offset = offset;
11847 sections.abbrev_size = size;
11848 break;
11849 case DW_SECT_INFO_V5:
11850 sections.info_or_types_offset = offset;
11851 sections.info_or_types_size = size;
11852 break;
11853 case DW_SECT_LINE_V5:
11854 sections.line_offset = offset;
11855 sections.line_size = size;
11856 break;
11857 case DW_SECT_LOCLISTS_V5:
11858 sections.loclists_offset = offset;
11859 sections.loclists_size = size;
11860 break;
11861 case DW_SECT_MACRO_V5:
11862 sections.macro_offset = offset;
11863 sections.macro_size = size;
11864 break;
11865 case DW_SECT_RNGLISTS_V5:
11866 sections.rnglists_offset = offset;
11867 sections.rnglists_size = size;
11868 break;
11869 case DW_SECT_STR_OFFSETS_V5:
11870 sections.str_offsets_offset = offset;
11871 sections.str_offsets_size = size;
11872 break;
11873 case DW_SECT_RESERVED_V5:
11874 default:
11875 break;
11876 }
d2854d8d
CT
11877 }
11878
11879 /* It's easier for the rest of the code if we fake a struct dwo_file and
11880 have dwo_unit "live" in that. At least for now.
11881
11882 The DWP file can be made up of a random collection of CUs and TUs.
11883 However, for each CU + set of TUs that came from the same original DWO
11884 file, we can combine them back into a virtual DWO file to save space
11885 (fewer struct dwo_file objects to allocate). Remember that for really
11886 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11887
11888 std::string virtual_dwo_name =
11889 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
11890 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11891 (long) (sections.line_size ? sections.line_offset : 0),
11892 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11893 (long) (sections.str_offsets_size
11894 ? sections.str_offsets_offset : 0),
11895 (long) (sections.macro_size ? sections.macro_offset : 0),
11896 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
11897 /* Can we use an existing virtual DWO file? */
11898 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
11899 virtual_dwo_name.c_str (),
11900 comp_dir);
d2854d8d
CT
11901 /* Create one if necessary. */
11902 if (*dwo_file_slot == NULL)
11903 {
6f738b01
SM
11904 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11905 virtual_dwo_name.c_str ());
11906
d2854d8d
CT
11907 dwo_file = new struct dwo_file;
11908 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11909 dwo_file->comp_dir = comp_dir;
11910 dwo_file->sections.abbrev =
dda83cd7
SM
11911 create_dwp_v2_or_v5_section (per_objfile,
11912 &dwp_file->sections.abbrev,
11913 sections.abbrev_offset,
11914 sections.abbrev_size);
d2854d8d 11915 dwo_file->sections.line =
dda83cd7
SM
11916 create_dwp_v2_or_v5_section (per_objfile,
11917 &dwp_file->sections.line,
11918 sections.line_offset, sections.line_size);
d2854d8d 11919 dwo_file->sections.macro =
dda83cd7
SM
11920 create_dwp_v2_or_v5_section (per_objfile,
11921 &dwp_file->sections.macro,
11922 sections.macro_offset,
11923 sections.macro_size);
d2854d8d 11924 dwo_file->sections.loclists =
dda83cd7
SM
11925 create_dwp_v2_or_v5_section (per_objfile,
11926 &dwp_file->sections.loclists,
11927 sections.loclists_offset,
11928 sections.loclists_size);
d2854d8d 11929 dwo_file->sections.rnglists =
dda83cd7
SM
11930 create_dwp_v2_or_v5_section (per_objfile,
11931 &dwp_file->sections.rnglists,
11932 sections.rnglists_offset,
11933 sections.rnglists_size);
d2854d8d 11934 dwo_file->sections.str_offsets =
dda83cd7
SM
11935 create_dwp_v2_or_v5_section (per_objfile,
11936 &dwp_file->sections.str_offsets,
11937 sections.str_offsets_offset,
11938 sections.str_offsets_size);
d2854d8d
CT
11939 /* The "str" section is global to the entire DWP file. */
11940 dwo_file->sections.str = dwp_file->sections.str;
11941 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
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
11946 for it, invalidating all copies of pointers into the previous
11947 contents. */
d2854d8d
CT
11948 *dwo_file_slot = dwo_file;
11949 }
11950 else
11951 {
6f738b01
SM
11952 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11953 virtual_dwo_name.c_str ());
11954
d2854d8d
CT
11955 dwo_file = (struct dwo_file *) *dwo_file_slot;
11956 }
11957
11958 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11959 dwo_unit->dwo_file = dwo_file;
11960 dwo_unit->signature = signature;
11961 dwo_unit->section
11962 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11963 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
11964 &dwp_file->sections.info,
11965 sections.info_or_types_offset,
11966 sections.info_or_types_size);
73869dc2
DE
11967 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11968
11969 return dwo_unit;
11970}
11971
57d63ce2
DE
11972/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11973 Returns NULL if the signature isn't found. */
80626a55
DE
11974
11975static struct dwo_unit *
976ca316 11976lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 11977 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11978 ULONGEST signature, int is_debug_types)
80626a55 11979{
57d63ce2
DE
11980 const struct dwp_hash_table *dwp_htab =
11981 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11982 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11983 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11984 uint32_t hash = signature & mask;
11985 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11986 unsigned int i;
11987 void **slot;
870f88f7 11988 struct dwo_unit find_dwo_cu;
80626a55
DE
11989
11990 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11991 find_dwo_cu.signature = signature;
19ac8c2e 11992 slot = htab_find_slot (is_debug_types
48b490f2
TT
11993 ? dwp_file->loaded_tus.get ()
11994 : dwp_file->loaded_cus.get (),
19ac8c2e 11995 &find_dwo_cu, INSERT);
80626a55
DE
11996
11997 if (*slot != NULL)
9a3c8263 11998 return (struct dwo_unit *) *slot;
80626a55
DE
11999
12000 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12001 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12002 {
12003 ULONGEST signature_in_table;
12004
12005 signature_in_table =
57d63ce2 12006 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12007 if (signature_in_table == signature)
12008 {
57d63ce2
DE
12009 uint32_t unit_index =
12010 read_4_bytes (dbfd,
12011 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12012
73869dc2
DE
12013 if (dwp_file->version == 1)
12014 {
976ca316
SM
12015 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12016 unit_index, comp_dir,
12017 signature, is_debug_types);
73869dc2 12018 }
d2854d8d 12019 else if (dwp_file->version == 2)
73869dc2 12020 {
976ca316
SM
12021 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12022 unit_index, comp_dir,
12023 signature, is_debug_types);
73869dc2 12024 }
d2854d8d
CT
12025 else /* version == 5 */
12026 {
12027 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12028 unit_index, comp_dir,
12029 signature, is_debug_types);
12030 }
9a3c8263 12031 return (struct dwo_unit *) *slot;
80626a55
DE
12032 }
12033 if (signature_in_table == 0)
12034 return NULL;
12035 hash = (hash + hash2) & mask;
12036 }
12037
12038 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12039 " [in module %s]"),
12040 dwp_file->name);
12041}
12042
ab5088bf 12043/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12044 Open the file specified by FILE_NAME and hand it off to BFD for
12045 preliminary analysis. Return a newly initialized bfd *, which
12046 includes a canonicalized copy of FILE_NAME.
80626a55 12047 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12048 SEARCH_CWD is true if the current directory is to be searched.
12049 It will be searched before debug-file-directory.
13aaf454
DE
12050 If successful, the file is added to the bfd include table of the
12051 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12052 If unable to find/open the file, return NULL.
3019eac3
DE
12053 NOTE: This function is derived from symfile_bfd_open. */
12054
192b62ce 12055static gdb_bfd_ref_ptr
976ca316 12056try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12057 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12058{
24b9144d 12059 int desc;
9c02c129
DE
12060 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12061 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12062 to debug_file_directory. */
e0cc99a6 12063 const char *search_path;
9c02c129
DE
12064 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12065
e0cc99a6 12066 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12067 if (search_cwd)
12068 {
12069 if (*debug_file_directory != '\0')
e0cc99a6
TT
12070 {
12071 search_path_holder.reset (concat (".", dirname_separator_string,
12072 debug_file_directory,
12073 (char *) NULL));
12074 search_path = search_path_holder.get ();
12075 }
6ac97d4c 12076 else
e0cc99a6 12077 search_path = ".";
6ac97d4c 12078 }
9c02c129 12079 else
e0cc99a6 12080 search_path = debug_file_directory;
3019eac3 12081
56d467f4
CT
12082 /* Add the path for the executable binary to the list of search paths. */
12083 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12084 search_path_holder.reset (concat (objfile_dir.c_str (),
12085 dirname_separator_string,
12086 search_path, nullptr));
12087 search_path = search_path_holder.get ();
12088
24b9144d 12089 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12090 if (is_dwp)
12091 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12092
12093 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12094 desc = openp (search_path, flags, file_name,
3019eac3
DE
12095 O_RDONLY | O_BINARY, &absolute_name);
12096 if (desc < 0)
12097 return NULL;
12098
e0cc99a6
TT
12099 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12100 gnutarget, desc));
9c02c129
DE
12101 if (sym_bfd == NULL)
12102 return NULL;
192b62ce 12103 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12104
192b62ce
TT
12105 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12106 return NULL;
3019eac3 12107
13aaf454
DE
12108 /* Success. Record the bfd as having been included by the objfile's bfd.
12109 This is important because things like demangled_names_hash lives in the
12110 objfile's per_bfd space and may have references to things like symbol
12111 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12112 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12113
3019eac3
DE
12114 return sym_bfd;
12115}
12116
ab5088bf 12117/* Try to open DWO file FILE_NAME.
3019eac3
DE
12118 COMP_DIR is the DW_AT_comp_dir attribute.
12119 The result is the bfd handle of the file.
12120 If there is a problem finding or opening the file, return NULL.
12121 Upon success, the canonicalized path of the file is stored in the bfd,
12122 same as symfile_bfd_open. */
12123
192b62ce 12124static gdb_bfd_ref_ptr
976ca316 12125open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12126 const char *file_name, const char *comp_dir)
3019eac3 12127{
80626a55 12128 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12129 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12130 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12131
12132 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12133
12134 if (comp_dir != NULL)
12135 {
43816ebc
TT
12136 gdb::unique_xmalloc_ptr<char> path_to_try
12137 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12138
12139 /* NOTE: If comp_dir is a relative path, this will also try the
12140 search path, which seems useful. */
976ca316 12141 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12142 0 /*is_dwp*/,
192b62ce 12143 1 /*search_cwd*/));
3019eac3
DE
12144 if (abfd != NULL)
12145 return abfd;
12146 }
12147
12148 /* That didn't work, try debug-file-directory, which, despite its name,
12149 is a list of paths. */
12150
12151 if (*debug_file_directory == '\0')
12152 return NULL;
12153
976ca316 12154 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12155 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12156}
12157
80626a55
DE
12158/* This function is mapped across the sections and remembers the offset and
12159 size of each of the DWO debugging sections we are interested in. */
12160
12161static void
5bb6e9dd
TT
12162dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12163 dwo_sections *dwo_sections)
80626a55 12164{
80626a55
DE
12165 const struct dwop_section_names *names = &dwop_section_names;
12166
fbedd546 12167 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12168 {
049412e3 12169 dwo_sections->abbrev.s.section = sectp;
fd361982 12170 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12171 }
fbedd546 12172 else if (names->info_dwo.matches (sectp->name))
80626a55 12173 {
049412e3 12174 dwo_sections->info.s.section = sectp;
fd361982 12175 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12176 }
fbedd546 12177 else if (names->line_dwo.matches (sectp->name))
80626a55 12178 {
049412e3 12179 dwo_sections->line.s.section = sectp;
fd361982 12180 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12181 }
fbedd546 12182 else if (names->loc_dwo.matches (sectp->name))
80626a55 12183 {
049412e3 12184 dwo_sections->loc.s.section = sectp;
fd361982 12185 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12186 }
fbedd546 12187 else if (names->loclists_dwo.matches (sectp->name))
41144253 12188 {
12189 dwo_sections->loclists.s.section = sectp;
12190 dwo_sections->loclists.size = bfd_section_size (sectp);
12191 }
fbedd546 12192 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12193 {
049412e3 12194 dwo_sections->macinfo.s.section = sectp;
fd361982 12195 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12196 }
fbedd546 12197 else if (names->macro_dwo.matches (sectp->name))
80626a55 12198 {
049412e3 12199 dwo_sections->macro.s.section = sectp;
fd361982 12200 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12201 }
fbedd546 12202 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12203 {
12204 dwo_sections->rnglists.s.section = sectp;
12205 dwo_sections->rnglists.size = bfd_section_size (sectp);
12206 }
fbedd546 12207 else if (names->str_dwo.matches (sectp->name))
80626a55 12208 {
049412e3 12209 dwo_sections->str.s.section = sectp;
fd361982 12210 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12211 }
fbedd546 12212 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12213 {
049412e3 12214 dwo_sections->str_offsets.s.section = sectp;
fd361982 12215 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12216 }
fbedd546 12217 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12218 {
12219 struct dwarf2_section_info type_section;
12220
12221 memset (&type_section, 0, sizeof (type_section));
049412e3 12222 type_section.s.section = sectp;
fd361982 12223 type_section.size = bfd_section_size (sectp);
fd5866f6 12224 dwo_sections->types.push_back (type_section);
80626a55
DE
12225 }
12226}
12227
ab5088bf 12228/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12229 by PER_CU. This is for the non-DWP case.
80626a55 12230 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12231
12232static struct dwo_file *
4ab09049
SM
12233open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12234 const char *comp_dir)
3019eac3 12235{
976ca316 12236 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12237
976ca316 12238 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12239 if (dbfd == NULL)
12240 {
6f738b01
SM
12241 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12242
80626a55
DE
12243 return NULL;
12244 }
263db9a1 12245
51ac9db5 12246 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12247 dwo_file->dwo_name = dwo_name;
12248 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12249 dwo_file->dbfd = std::move (dbfd);
3019eac3 12250
5bb6e9dd
TT
12251 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12252 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12253 &dwo_file->sections);
3019eac3 12254
976ca316
SM
12255 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12256 dwo_file->cus);
3019eac3 12257
d2854d8d
CT
12258 if (cu->per_cu->dwarf_version < 5)
12259 {
12260 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12261 dwo_file->sections.types, dwo_file->tus);
12262 }
12263 else
12264 {
12265 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12266 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12267 rcuh_kind::COMPILE);
d2854d8d 12268 }
3019eac3 12269
6f738b01 12270 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12271
263db9a1 12272 return dwo_file.release ();
3019eac3
DE
12273}
12274
80626a55 12275/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12276 size of each of the DWP debugging sections common to version 1 and 2 that
12277 we are interested in. */
3019eac3 12278
80626a55 12279static void
73869dc2 12280dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12281 dwp_file *dwp_file)
3019eac3 12282{
80626a55
DE
12283 const struct dwop_section_names *names = &dwop_section_names;
12284 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12285
80626a55 12286 /* Record the ELF section number for later lookup: this is what the
73869dc2 12287 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12288 gdb_assert (elf_section_nr < dwp_file->num_sections);
12289 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12290
80626a55 12291 /* Look for specific sections that we need. */
fbedd546 12292 if (names->str_dwo.matches (sectp->name))
80626a55 12293 {
049412e3 12294 dwp_file->sections.str.s.section = sectp;
fd361982 12295 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12296 }
fbedd546 12297 else if (names->cu_index.matches (sectp->name))
80626a55 12298 {
049412e3 12299 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12300 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12301 }
fbedd546 12302 else if (names->tu_index.matches (sectp->name))
80626a55 12303 {
049412e3 12304 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12305 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12306 }
12307}
3019eac3 12308
73869dc2
DE
12309/* This function is mapped across the sections and remembers the offset and
12310 size of each of the DWP version 2 debugging sections that we are interested
12311 in. This is split into a separate function because we don't know if we
d2854d8d 12312 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12313
12314static void
12315dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12316{
9a3c8263 12317 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12318 const struct dwop_section_names *names = &dwop_section_names;
12319 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12320
12321 /* Record the ELF section number for later lookup: this is what the
12322 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12323 gdb_assert (elf_section_nr < dwp_file->num_sections);
12324 dwp_file->elf_sections[elf_section_nr] = sectp;
12325
12326 /* Look for specific sections that we need. */
fbedd546 12327 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12328 {
049412e3 12329 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12330 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12331 }
fbedd546 12332 else if (names->info_dwo.matches (sectp->name))
73869dc2 12333 {
049412e3 12334 dwp_file->sections.info.s.section = sectp;
fd361982 12335 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12336 }
fbedd546 12337 else if (names->line_dwo.matches (sectp->name))
73869dc2 12338 {
049412e3 12339 dwp_file->sections.line.s.section = sectp;
fd361982 12340 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12341 }
fbedd546 12342 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12343 {
049412e3 12344 dwp_file->sections.loc.s.section = sectp;
fd361982 12345 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12346 }
fbedd546 12347 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12348 {
049412e3 12349 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12350 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12351 }
fbedd546 12352 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12353 {
049412e3 12354 dwp_file->sections.macro.s.section = sectp;
fd361982 12355 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12356 }
fbedd546 12357 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12358 {
049412e3 12359 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12360 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12361 }
fbedd546 12362 else if (names->types_dwo.matches (sectp->name))
73869dc2 12363 {
049412e3 12364 dwp_file->sections.types.s.section = sectp;
fd361982 12365 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12366 }
12367}
12368
d2854d8d
CT
12369/* This function is mapped across the sections and remembers the offset and
12370 size of each of the DWP version 5 debugging sections that we are interested
12371 in. This is split into a separate function because we don't know if we
12372 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12373
12374static void
12375dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12376{
12377 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12378 const struct dwop_section_names *names = &dwop_section_names;
12379 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12380
12381 /* Record the ELF section number for later lookup: this is what the
12382 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12383 gdb_assert (elf_section_nr < dwp_file->num_sections);
12384 dwp_file->elf_sections[elf_section_nr] = sectp;
12385
12386 /* Look for specific sections that we need. */
fbedd546 12387 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12388 {
12389 dwp_file->sections.abbrev.s.section = sectp;
12390 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12391 }
fbedd546 12392 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12393 {
12394 dwp_file->sections.info.s.section = sectp;
12395 dwp_file->sections.info.size = bfd_section_size (sectp);
12396 }
fbedd546 12397 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12398 {
12399 dwp_file->sections.line.s.section = sectp;
12400 dwp_file->sections.line.size = bfd_section_size (sectp);
12401 }
fbedd546 12402 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
12403 {
12404 dwp_file->sections.loclists.s.section = sectp;
12405 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12406 }
fbedd546 12407 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
12408 {
12409 dwp_file->sections.macro.s.section = sectp;
12410 dwp_file->sections.macro.size = bfd_section_size (sectp);
12411 }
fbedd546 12412 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
12413 {
12414 dwp_file->sections.rnglists.s.section = sectp;
12415 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12416 }
fbedd546 12417 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
12418 {
12419 dwp_file->sections.str_offsets.s.section = sectp;
12420 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12421 }
12422}
12423
80626a55 12424/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12425
80626a55
DE
12426static hashval_t
12427hash_dwp_loaded_cutus (const void *item)
12428{
9a3c8263 12429 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12430
80626a55
DE
12431 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12432 return dwo_unit->signature;
3019eac3
DE
12433}
12434
80626a55 12435/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12436
80626a55
DE
12437static int
12438eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12439{
9a3c8263
SM
12440 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12441 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12442
80626a55
DE
12443 return dua->signature == dub->signature;
12444}
3019eac3 12445
80626a55 12446/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12447
48b490f2 12448static htab_up
298e9637 12449allocate_dwp_loaded_cutus_table ()
80626a55 12450{
48b490f2
TT
12451 return htab_up (htab_create_alloc (3,
12452 hash_dwp_loaded_cutus,
12453 eq_dwp_loaded_cutus,
12454 NULL, xcalloc, xfree));
80626a55 12455}
3019eac3 12456
ab5088bf
DE
12457/* Try to open DWP file FILE_NAME.
12458 The result is the bfd handle of the file.
12459 If there is a problem finding or opening the file, return NULL.
12460 Upon success, the canonicalized path of the file is stored in the bfd,
12461 same as symfile_bfd_open. */
12462
192b62ce 12463static gdb_bfd_ref_ptr
976ca316 12464open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12465{
976ca316 12466 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12467 1 /*is_dwp*/,
192b62ce 12468 1 /*search_cwd*/));
6ac97d4c
DE
12469 if (abfd != NULL)
12470 return abfd;
12471
12472 /* Work around upstream bug 15652.
12473 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12474 [Whether that's a "bug" is debatable, but it is getting in our way.]
12475 We have no real idea where the dwp file is, because gdb's realpath-ing
12476 of the executable's path may have discarded the needed info.
12477 [IWBN if the dwp file name was recorded in the executable, akin to
12478 .gnu_debuglink, but that doesn't exist yet.]
12479 Strip the directory from FILE_NAME and search again. */
12480 if (*debug_file_directory != '\0')
12481 {
12482 /* Don't implicitly search the current directory here.
12483 If the user wants to search "." to handle this case,
12484 it must be added to debug-file-directory. */
976ca316
SM
12485 return try_open_dwop_file (per_objfile, lbasename (file_name),
12486 1 /*is_dwp*/,
6ac97d4c
DE
12487 0 /*search_cwd*/);
12488 }
12489
12490 return NULL;
ab5088bf
DE
12491}
12492
80626a55
DE
12493/* Initialize the use of the DWP file for the current objfile.
12494 By convention the name of the DWP file is ${objfile}.dwp.
12495 The result is NULL if it can't be found. */
a766d390 12496
400174b1 12497static std::unique_ptr<struct dwp_file>
976ca316 12498open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12499{
976ca316 12500 struct objfile *objfile = per_objfile->objfile;
80626a55 12501
82bf32bc
JK
12502 /* Try to find first .dwp for the binary file before any symbolic links
12503 resolving. */
6c447423
DE
12504
12505 /* If the objfile is a debug file, find the name of the real binary
12506 file and get the name of dwp file from there. */
d721ba37 12507 std::string dwp_name;
6c447423
DE
12508 if (objfile->separate_debug_objfile_backlink != NULL)
12509 {
12510 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12511 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12512
d721ba37 12513 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12514 }
12515 else
d721ba37
PA
12516 dwp_name = objfile->original_name;
12517
12518 dwp_name += ".dwp";
80626a55 12519
976ca316 12520 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12521 if (dbfd == NULL
12522 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12523 {
12524 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12525 dwp_name = objfile_name (objfile);
12526 dwp_name += ".dwp";
976ca316 12527 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12528 }
12529
80626a55
DE
12530 if (dbfd == NULL)
12531 {
6f738b01
SM
12532 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12533
400174b1 12534 return std::unique_ptr<dwp_file> ();
3019eac3 12535 }
400174b1
TT
12536
12537 const char *name = bfd_get_filename (dbfd.get ());
12538 std::unique_ptr<struct dwp_file> dwp_file
12539 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12540
0a0f4c01 12541 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12542 dwp_file->elf_sections =
976ca316 12543 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12544 dwp_file->num_sections, asection *);
12545
5bb6e9dd
TT
12546 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12547 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12548 dwp_file.get ());
80626a55 12549
976ca316 12550 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12551
976ca316 12552 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12553
73869dc2 12554 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12555 if (dwp_file->cus && dwp_file->tus
12556 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12557 {
12558 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12559 pretty bizarre. We use pulongest here because that's the established
4d65956b 12560 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12561 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12562 " TU version %s [in DWP file %s]"),
12563 pulongest (dwp_file->cus->version),
d721ba37 12564 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12565 }
08302ed2
DE
12566
12567 if (dwp_file->cus)
12568 dwp_file->version = dwp_file->cus->version;
12569 else if (dwp_file->tus)
12570 dwp_file->version = dwp_file->tus->version;
12571 else
12572 dwp_file->version = 2;
73869dc2 12573
5bb6e9dd
TT
12574 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12575 {
12576 if (dwp_file->version == 2)
12577 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12578 dwp_file.get ());
12579 else
12580 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12581 dwp_file.get ());
12582 }
73869dc2 12583
298e9637
SM
12584 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12585 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12586
6f738b01
SM
12587 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12588 dwarf_read_debug_printf (" %s CUs, %s TUs",
12589 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12590 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12591
12592 return dwp_file;
3019eac3 12593}
c906108c 12594
ab5088bf
DE
12595/* Wrapper around open_and_init_dwp_file, only open it once. */
12596
12597static struct dwp_file *
976ca316 12598get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12599{
976ca316 12600 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12601 {
976ca316
SM
12602 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12603 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12604 }
976ca316 12605 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12606}
12607
80626a55
DE
12608/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12609 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12610 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12611 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12612 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12613
12614 This is called, for example, when wanting to read a variable with a
12615 complex location. Therefore we don't want to do file i/o for every call.
12616 Therefore we don't want to look for a DWO file on every call.
12617 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12618 then we check if we've already seen DWO_NAME, and only THEN do we check
12619 for a DWO file.
12620
1c658ad5 12621 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12622 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12623
3019eac3 12624static struct dwo_unit *
4ab09049 12625lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12626 ULONGEST signature, int is_debug_types)
3019eac3 12627{
976ca316
SM
12628 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12629 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12630 const char *kind = is_debug_types ? "TU" : "CU";
12631 void **dwo_file_slot;
3019eac3 12632 struct dwo_file *dwo_file;
80626a55 12633 struct dwp_file *dwp_file;
cb1df416 12634
6a506a2d
DE
12635 /* First see if there's a DWP file.
12636 If we have a DWP file but didn't find the DWO inside it, don't
12637 look for the original DWO file. It makes gdb behave differently
12638 depending on whether one is debugging in the build tree. */
cf2c3c16 12639
976ca316 12640 dwp_file = get_dwp_file (per_objfile);
80626a55 12641 if (dwp_file != NULL)
cf2c3c16 12642 {
80626a55
DE
12643 const struct dwp_hash_table *dwp_htab =
12644 is_debug_types ? dwp_file->tus : dwp_file->cus;
12645
12646 if (dwp_htab != NULL)
12647 {
12648 struct dwo_unit *dwo_cutu =
976ca316
SM
12649 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12650 is_debug_types);
80626a55
DE
12651
12652 if (dwo_cutu != NULL)
12653 {
6f738b01
SM
12654 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12655 kind, hex_string (signature),
12656 host_address_to_string (dwo_cutu));
12657
80626a55
DE
12658 return dwo_cutu;
12659 }
12660 }
12661 }
6a506a2d 12662 else
80626a55 12663 {
6a506a2d 12664 /* No DWP file, look for the DWO file. */
80626a55 12665
976ca316 12666 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12667 if (*dwo_file_slot == NULL)
80626a55 12668 {
6a506a2d 12669 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12670 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12671 }
6a506a2d 12672 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12673 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12674
6a506a2d 12675 if (dwo_file != NULL)
19c3d4c9 12676 {
6a506a2d
DE
12677 struct dwo_unit *dwo_cutu = NULL;
12678
12679 if (is_debug_types && dwo_file->tus)
12680 {
12681 struct dwo_unit find_dwo_cutu;
12682
12683 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12684 find_dwo_cutu.signature = signature;
9a3c8263 12685 dwo_cutu
b0b6a987
TT
12686 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12687 &find_dwo_cutu);
6a506a2d 12688 }
33c5cd75 12689 else if (!is_debug_types && dwo_file->cus)
80626a55 12690 {
33c5cd75
DB
12691 struct dwo_unit find_dwo_cutu;
12692
12693 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12694 find_dwo_cutu.signature = signature;
b0b6a987 12695 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12696 &find_dwo_cutu);
6a506a2d
DE
12697 }
12698
12699 if (dwo_cutu != NULL)
12700 {
6f738b01
SM
12701 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12702 kind, dwo_name, hex_string (signature),
12703 host_address_to_string (dwo_cutu));
12704
6a506a2d 12705 return dwo_cutu;
80626a55
DE
12706 }
12707 }
2e276125 12708 }
9cdd5dbd 12709
80626a55
DE
12710 /* We didn't find it. This could mean a dwo_id mismatch, or
12711 someone deleted the DWO/DWP file, or the search path isn't set up
12712 correctly to find the file. */
12713
6f738b01
SM
12714 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12715 kind, dwo_name, hex_string (signature));
3019eac3 12716
6656a72d
DE
12717 /* This is a warning and not a complaint because it can be caused by
12718 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12719 {
12720 /* Print the name of the DWP file if we looked there, helps the user
12721 better diagnose the problem. */
791afaa2 12722 std::string dwp_text;
43942612
DE
12723
12724 if (dwp_file != NULL)
791afaa2
TT
12725 dwp_text = string_printf (" [in DWP file %s]",
12726 lbasename (dwp_file->name));
43942612 12727
9d8780f0 12728 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12729 " [in module %s]"),
4ab09049
SM
12730 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12731 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12732 }
3019eac3 12733 return NULL;
5fb290d7
DJ
12734}
12735
80626a55
DE
12736/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12737 See lookup_dwo_cutu_unit for details. */
12738
12739static struct dwo_unit *
4ab09049 12740lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12741 ULONGEST signature)
12742{
4ab09049
SM
12743 gdb_assert (!cu->per_cu->is_debug_types);
12744
12745 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12746}
12747
12748/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12749 See lookup_dwo_cutu_unit for details. */
12750
12751static struct dwo_unit *
4ab09049 12752lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12753{
4ab09049
SM
12754 gdb_assert (cu->per_cu->is_debug_types);
12755
12756 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12757
12758 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12759}
12760
89e63ee4
DE
12761/* Traversal function for queue_and_load_all_dwo_tus. */
12762
12763static int
12764queue_and_load_dwo_tu (void **slot, void *info)
12765{
12766 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12767 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12768 ULONGEST signature = dwo_unit->signature;
d460f660 12769 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12770
12771 if (sig_type != NULL)
12772 {
89e63ee4
DE
12773 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12774 a real dependency of PER_CU on SIG_TYPE. That is detected later
12775 while processing PER_CU. */
0d305d5c 12776 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
3e9f1ca1 12777 cu->per_cu->lang))
0d305d5c
TT
12778 load_full_type_unit (sig_type, cu->per_objfile);
12779 cu->per_cu->imported_symtabs_push (sig_type);
89e63ee4
DE
12780 }
12781
12782 return 1;
12783}
12784
1b555f17 12785/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12786 The DWO may have the only definition of the type, though it may not be
12787 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12788 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12789
12790static void
1b555f17 12791queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12792{
12793 struct dwo_unit *dwo_unit;
12794 struct dwo_file *dwo_file;
12795
1b555f17
SM
12796 gdb_assert (cu != nullptr);
12797 gdb_assert (!cu->per_cu->is_debug_types);
12798 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12799
1b555f17 12800 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12801 gdb_assert (dwo_unit != NULL);
12802
12803 dwo_file = dwo_unit->dwo_file;
12804 if (dwo_file->tus != NULL)
1b555f17 12805 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12806}
12807
3019eac3 12808/* Read in various DIEs. */
348e048f 12809
d389af10 12810/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12811 Inherit only the children of the DW_AT_abstract_origin DIE not being
12812 already referenced by DW_AT_abstract_origin from the children of the
12813 current DIE. */
d389af10
JK
12814
12815static void
12816inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12817{
12818 struct die_info *child_die;
791afaa2 12819 sect_offset *offsetp;
d389af10
JK
12820 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12821 struct die_info *origin_die;
12822 /* Iterator of the ORIGIN_DIE children. */
12823 struct die_info *origin_child_die;
d389af10 12824 struct attribute *attr;
cd02d79d
PA
12825 struct dwarf2_cu *origin_cu;
12826 struct pending **origin_previous_list_in_scope;
d389af10
JK
12827
12828 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12829 if (!attr)
12830 return;
12831
cd02d79d
PA
12832 /* Note that following die references may follow to a die in a
12833 different cu. */
12834
12835 origin_cu = cu;
12836 origin_die = follow_die_ref (die, attr, &origin_cu);
12837
12838 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12839 symbols in. */
12840 origin_previous_list_in_scope = origin_cu->list_in_scope;
12841 origin_cu->list_in_scope = cu->list_in_scope;
12842
edb3359d
DJ
12843 if (die->tag != origin_die->tag
12844 && !(die->tag == DW_TAG_inlined_subroutine
12845 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12846 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12847 sect_offset_str (die->sect_off),
12848 sect_offset_str (origin_die->sect_off));
d389af10 12849
34dc0f95
TBA
12850 /* Find if the concrete and abstract trees are structurally the
12851 same. This is a shallow traversal and it is not bullet-proof;
12852 the compiler can trick the debugger into believing that the trees
12853 are isomorphic, whereas they actually are not. However, the
12854 likelyhood of this happening is pretty low, and a full-fledged
12855 check would be an overkill. */
12856 bool are_isomorphic = true;
12857 die_info *concrete_child = die->child;
12858 die_info *abstract_child = origin_die->child;
12859 while (concrete_child != nullptr || abstract_child != nullptr)
12860 {
12861 if (concrete_child == nullptr
12862 || abstract_child == nullptr
12863 || concrete_child->tag != abstract_child->tag)
12864 {
12865 are_isomorphic = false;
12866 break;
12867 }
12868
12869 concrete_child = concrete_child->sibling;
12870 abstract_child = abstract_child->sibling;
12871 }
12872
12873 /* Walk the origin's children in parallel to the concrete children.
12874 This helps match an origin child in case the debug info misses
12875 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12876 origin tree may not have the same tree structure as the concrete
12877 DIE, though. */
12878 die_info *corresponding_abstract_child
12879 = are_isomorphic ? origin_die->child : nullptr;
12880
791afaa2 12881 std::vector<sect_offset> offsets;
d389af10 12882
3ea89b92
PMR
12883 for (child_die = die->child;
12884 child_die && child_die->tag;
436c571c 12885 child_die = child_die->sibling)
3ea89b92
PMR
12886 {
12887 struct die_info *child_origin_die;
12888 struct dwarf2_cu *child_origin_cu;
12889
12890 /* We are trying to process concrete instance entries:
216f72a1 12891 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12892 it's not relevant to our analysis here. i.e. detecting DIEs that are
12893 present in the abstract instance but not referenced in the concrete
12894 one. */
216f72a1 12895 if (child_die->tag == DW_TAG_call_site
dda83cd7 12896 || child_die->tag == DW_TAG_GNU_call_site)
34dc0f95
TBA
12897 {
12898 if (are_isomorphic)
12899 corresponding_abstract_child
12900 = corresponding_abstract_child->sibling;
12901 continue;
12902 }
3ea89b92 12903
c38f313d
DJ
12904 /* For each CHILD_DIE, find the corresponding child of
12905 ORIGIN_DIE. If there is more than one layer of
12906 DW_AT_abstract_origin, follow them all; there shouldn't be,
12907 but GCC versions at least through 4.4 generate this (GCC PR
12908 40573). */
3ea89b92
PMR
12909 child_origin_die = child_die;
12910 child_origin_cu = cu;
c38f313d
DJ
12911 while (1)
12912 {
cd02d79d
PA
12913 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12914 child_origin_cu);
c38f313d
DJ
12915 if (attr == NULL)
12916 break;
cd02d79d
PA
12917 child_origin_die = follow_die_ref (child_origin_die, attr,
12918 &child_origin_cu);
c38f313d
DJ
12919 }
12920
34dc0f95
TBA
12921 /* If missing DW_AT_abstract_origin, try the corresponding child
12922 of the origin. Clang emits such lexical scopes. */
12923 if (child_origin_die == child_die
12924 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12925 && are_isomorphic
12926 && child_die->tag == DW_TAG_lexical_block)
12927 child_origin_die = corresponding_abstract_child;
12928
d389af10
JK
12929 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12930 counterpart may exist. */
c38f313d 12931 if (child_origin_die != child_die)
d389af10 12932 {
edb3359d
DJ
12933 if (child_die->tag != child_origin_die->tag
12934 && !(child_die->tag == DW_TAG_inlined_subroutine
12935 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12936 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12937 "different tags"),
9d8780f0
SM
12938 sect_offset_str (child_die->sect_off),
12939 sect_offset_str (child_origin_die->sect_off));
c38f313d 12940 if (child_origin_die->parent != origin_die)
b98664d3 12941 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12942 "different parents"),
9d8780f0
SM
12943 sect_offset_str (child_die->sect_off),
12944 sect_offset_str (child_origin_die->sect_off));
c38f313d 12945 else
791afaa2 12946 offsets.push_back (child_origin_die->sect_off);
d389af10 12947 }
34dc0f95
TBA
12948
12949 if (are_isomorphic)
12950 corresponding_abstract_child = corresponding_abstract_child->sibling;
d389af10 12951 }
791afaa2
TT
12952 std::sort (offsets.begin (), offsets.end ());
12953 sect_offset *offsets_end = offsets.data () + offsets.size ();
12954 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12955 if (offsetp[-1] == *offsetp)
b98664d3 12956 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12957 "to DIE %s as their abstract origin"),
12958 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12959
791afaa2 12960 offsetp = offsets.data ();
d389af10
JK
12961 origin_child_die = origin_die->child;
12962 while (origin_child_die && origin_child_die->tag)
12963 {
12964 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12965 while (offsetp < offsets_end
9c541725 12966 && *offsetp < origin_child_die->sect_off)
d389af10 12967 offsetp++;
b64f50a1 12968 if (offsetp >= offsets_end
9c541725 12969 || *offsetp > origin_child_die->sect_off)
d389af10 12970 {
adde2bff
DE
12971 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12972 Check whether we're already processing ORIGIN_CHILD_DIE.
12973 This can happen with mutually referenced abstract_origins.
12974 PR 16581. */
12975 if (!origin_child_die->in_process)
12976 process_die (origin_child_die, origin_cu);
d389af10 12977 }
436c571c 12978 origin_child_die = origin_child_die->sibling;
d389af10 12979 }
cd02d79d 12980 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12981
12982 if (cu != origin_cu)
12983 compute_delayed_physnames (origin_cu);
d389af10
JK
12984}
12985
c906108c 12986static void
e7c27a73 12987read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12988{
5e22e966 12989 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 12990 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 12991 struct context_stack *newobj;
c906108c
SS
12992 CORE_ADDR lowpc;
12993 CORE_ADDR highpc;
12994 struct die_info *child_die;
edb3359d 12995 struct attribute *attr, *call_line, *call_file;
15d034d0 12996 const char *name;
e142c38c 12997 CORE_ADDR baseaddr;
801e3a5b 12998 struct block *block;
edb3359d 12999 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13000 std::vector<struct symbol *> template_args;
34eaf542 13001 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13002
13003 if (inlined_func)
13004 {
13005 /* If we do not have call site information, we can't show the
13006 caller of this inlined function. That's too confusing, so
13007 only use the scope for local variables. */
13008 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13009 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13010 if (call_line == NULL || call_file == NULL)
13011 {
13012 read_lexical_block_scope (die, cu);
13013 return;
13014 }
13015 }
c906108c 13016
b3b3bada 13017 baseaddr = objfile->text_section_offset ();
e142c38c 13018
94af9270 13019 name = dwarf2_name (die, cu);
c906108c 13020
e8d05480
JB
13021 /* Ignore functions with missing or empty names. These are actually
13022 illegal according to the DWARF standard. */
13023 if (name == NULL)
13024 {
b98664d3 13025 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13026 sect_offset_str (die->sect_off));
e8d05480
JB
13027 return;
13028 }
13029
13030 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13031 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13032 <= PC_BOUNDS_INVALID)
e8d05480 13033 {
ae4d0c03 13034 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13035 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13036 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13037 "for subprogram DIE at %s"),
13038 sect_offset_str (die->sect_off));
e8d05480
JB
13039 return;
13040 }
c906108c 13041
3e29f34a
MR
13042 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13043 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13044
34eaf542
TT
13045 /* If we have any template arguments, then we must allocate a
13046 different sort of symbol. */
436c571c 13047 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13048 {
13049 if (child_die->tag == DW_TAG_template_type_param
13050 || child_die->tag == DW_TAG_template_value_param)
13051 {
8c14c3a3 13052 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13053 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13054 break;
13055 }
13056 }
13057
702cf3f5 13058 gdb_assert (cu->get_builder () != nullptr);
c24bdb02 13059 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13060 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13061 (struct symbol *) templ_func);
4c2df51b 13062
81873cc8 13063 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13064 set_objfile_main_name (objfile, newobj->name->linkage_name (),
3e9f1ca1 13065 cu->per_cu->lang);
81873cc8 13066
4cecd739
DJ
13067 /* If there is a location expression for DW_AT_frame_base, record
13068 it. */
e142c38c 13069 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13070 if (attr != nullptr)
fe978cb0 13071 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13072
63e43d3a
PMR
13073 /* If there is a location for the static link, record it. */
13074 newobj->static_link = NULL;
13075 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13076 if (attr != nullptr)
63e43d3a 13077 {
224c3ddb
SM
13078 newobj->static_link
13079 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13080 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13081 cu->addr_type ());
63e43d3a
PMR
13082 }
13083
c24bdb02 13084 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13085
639d11d3 13086 if (die->child != NULL)
c906108c 13087 {
639d11d3 13088 child_die = die->child;
c906108c
SS
13089 while (child_die && child_die->tag)
13090 {
34eaf542
TT
13091 if (child_die->tag == DW_TAG_template_type_param
13092 || child_die->tag == DW_TAG_template_value_param)
13093 {
13094 struct symbol *arg = new_symbol (child_die, NULL, cu);
13095
f1078f66 13096 if (arg != NULL)
2f4732b0 13097 template_args.push_back (arg);
34eaf542
TT
13098 }
13099 else
13100 process_die (child_die, cu);
436c571c 13101 child_die = child_die->sibling;
c906108c
SS
13102 }
13103 }
13104
d389af10
JK
13105 inherit_abstract_dies (die, cu);
13106
4a811a97
UW
13107 /* If we have a DW_AT_specification, we might need to import using
13108 directives from the context of the specification DIE. See the
13109 comment in determine_prefix. */
3e9f1ca1 13110 if (cu->per_cu->lang == language_cplus
4a811a97
UW
13111 && dwarf2_attr (die, DW_AT_specification, cu))
13112 {
13113 struct dwarf2_cu *spec_cu = cu;
13114 struct die_info *spec_die = die_specification (die, &spec_cu);
13115
13116 while (spec_die)
13117 {
13118 child_die = spec_die->child;
13119 while (child_die && child_die->tag)
13120 {
13121 if (child_die->tag == DW_TAG_imported_module)
13122 process_die (child_die, spec_cu);
436c571c 13123 child_die = child_die->sibling;
4a811a97
UW
13124 }
13125
13126 /* In some cases, GCC generates specification DIEs that
13127 themselves contain DW_AT_specification attributes. */
13128 spec_die = die_specification (spec_die, &spec_cu);
13129 }
13130 }
13131
c24bdb02 13132 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13133 /* Make a block for the local symbols within. */
c24bdb02 13134 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13135 cstk.static_link, lowpc, highpc);
801e3a5b 13136
df8a16a1 13137 /* For C++, set the block's scope. */
3e9f1ca1
TT
13138 if ((cu->per_cu->lang == language_cplus
13139 || cu->per_cu->lang == language_fortran
13140 || cu->per_cu->lang == language_d
13141 || cu->per_cu->lang == language_rust)
4d4ec4e5 13142 && cu->processing_has_namespace_info)
195a3f6c
TT
13143 block_set_scope (block, determine_prefix (die, cu),
13144 &objfile->objfile_obstack);
df8a16a1 13145
801e3a5b
JB
13146 /* If we have address ranges, record them. */
13147 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13148
a60f3166 13149 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13150
34eaf542 13151 /* Attach template arguments to function. */
2f4732b0 13152 if (!template_args.empty ())
34eaf542
TT
13153 {
13154 gdb_assert (templ_func != NULL);
13155
2f4732b0 13156 templ_func->n_template_arguments = template_args.size ();
34eaf542 13157 templ_func->template_arguments
dda83cd7 13158 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13159 templ_func->n_template_arguments);
34eaf542 13160 memcpy (templ_func->template_arguments,
2f4732b0 13161 template_args.data (),
34eaf542 13162 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13163
13164 /* Make sure that the symtab is set on the new symbols. Even
13165 though they don't appear in this symtab directly, other parts
13166 of gdb assume that symbols do, and this is reasonably
13167 true. */
8634679f 13168 for (symbol *sym : template_args)
3e1d3d8c 13169 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13170 }
13171
208d8187
JB
13172 /* In C++, we can have functions nested inside functions (e.g., when
13173 a function declares a class that has methods). This means that
13174 when we finish processing a function scope, we may need to go
13175 back to building a containing block's symbol lists. */
c24bdb02
KS
13176 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13177 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13178
921e78cf
JB
13179 /* If we've finished processing a top-level function, subsequent
13180 symbols go in the file symbol list. */
c24bdb02
KS
13181 if (cu->get_builder ()->outermost_context_p ())
13182 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13183}
13184
13185/* Process all the DIES contained within a lexical block scope. Start
13186 a new scope, process the dies, and then close the scope. */
13187
13188static void
e7c27a73 13189read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13190{
5e22e966 13191 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13192 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13193 CORE_ADDR lowpc, highpc;
13194 struct die_info *child_die;
e142c38c
DJ
13195 CORE_ADDR baseaddr;
13196
b3b3bada 13197 baseaddr = objfile->text_section_offset ();
c906108c
SS
13198
13199 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13200 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13201 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13202 be nasty. Might be easier to properly extend generic blocks to
af34e669 13203 describe ranges. */
e385593e
JK
13204 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13205 {
13206 case PC_BOUNDS_NOT_PRESENT:
13207 /* DW_TAG_lexical_block has no attributes, process its children as if
13208 there was no wrapping by that DW_TAG_lexical_block.
13209 GCC does no longer produces such DWARF since GCC r224161. */
13210 for (child_die = die->child;
13211 child_die != NULL && child_die->tag;
436c571c 13212 child_die = child_die->sibling)
4f7bc5ed
TT
13213 {
13214 /* We might already be processing this DIE. This can happen
13215 in an unusual circumstance -- where a subroutine A
13216 appears lexically in another subroutine B, but A actually
13217 inlines B. The recursion is broken here, rather than in
13218 inherit_abstract_dies, because it seems better to simply
13219 drop concrete children here. */
13220 if (!child_die->in_process)
13221 process_die (child_die, cu);
13222 }
e385593e
JK
13223 return;
13224 case PC_BOUNDS_INVALID:
13225 return;
13226 }
3e29f34a
MR
13227 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13228 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13229
c24bdb02 13230 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13231 if (die->child != NULL)
c906108c 13232 {
639d11d3 13233 child_die = die->child;
c906108c
SS
13234 while (child_die && child_die->tag)
13235 {
e7c27a73 13236 process_die (child_die, cu);
436c571c 13237 child_die = child_die->sibling;
c906108c
SS
13238 }
13239 }
3ea89b92 13240 inherit_abstract_dies (die, cu);
c24bdb02 13241 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13242
c24bdb02
KS
13243 if (*cu->get_builder ()->get_local_symbols () != NULL
13244 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13245 {
801e3a5b 13246 struct block *block
dda83cd7 13247 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13248 cstk.start_addr, highpc);
801e3a5b
JB
13249
13250 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13251 do here, means that recording a parent's ranges entails
13252 walking across all its children's ranges as they appear in
13253 the address map, which is quadratic behavior.
13254
13255 It would be nicer to record the parent's ranges before
13256 traversing its children, simply overriding whatever you find
13257 there. But since we don't even decide whether to create a
13258 block until after we've traversed its children, that's hard
13259 to do. */
801e3a5b 13260 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13261 }
c24bdb02
KS
13262 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13263 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13264}
13265
216f72a1 13266/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13267
13268static void
13269read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13270{
5e22e966 13271 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13272 struct objfile *objfile = per_objfile->objfile;
08feed99 13273 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13274 CORE_ADDR pc, baseaddr;
13275 struct attribute *attr;
13276 struct call_site *call_site, call_site_local;
13277 void **slot;
13278 int nparams;
13279 struct die_info *child_die;
13280
b3b3bada 13281 baseaddr = objfile->text_section_offset ();
96408a79 13282
216f72a1
JK
13283 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13284 if (attr == NULL)
13285 {
13286 /* This was a pre-DWARF-5 GNU extension alias
13287 for DW_AT_call_return_pc. */
13288 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13289 }
96408a79
SA
13290 if (!attr)
13291 {
b98664d3 13292 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13293 "DIE %s [in module %s]"),
13294 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13295 return;
13296 }
95f982e5 13297 pc = attr->as_address () + baseaddr;
3e29f34a 13298 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13299
13300 if (cu->call_site_htab == NULL)
13301 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13302 NULL, &objfile->objfile_obstack,
13303 hashtab_obstack_allocate, NULL);
13304 call_site_local.pc = pc;
13305 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13306 if (*slot != NULL)
13307 {
b98664d3 13308 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13309 "DIE %s [in module %s]"),
13310 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13311 objfile_name (objfile));
96408a79
SA
13312 return;
13313 }
13314
13315 /* Count parameters at the caller. */
13316
13317 nparams = 0;
13318 for (child_die = die->child; child_die && child_die->tag;
436c571c 13319 child_die = child_die->sibling)
96408a79 13320 {
216f72a1 13321 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13322 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13323 {
b98664d3 13324 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13325 "DW_TAG_call_site child DIE %s [in module %s]"),
13326 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13327 objfile_name (objfile));
96408a79
SA
13328 continue;
13329 }
13330
13331 nparams++;
13332 }
13333
224c3ddb
SM
13334 call_site
13335 = ((struct call_site *)
13336 obstack_alloc (&objfile->objfile_obstack,
13337 sizeof (*call_site)
13338 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13339 *slot = call_site;
13340 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13341 call_site->pc = pc;
13342
216f72a1
JK
13343 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13344 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13345 {
13346 struct die_info *func_die;
13347
13348 /* Skip also over DW_TAG_inlined_subroutine. */
13349 for (func_die = die->parent;
13350 func_die && func_die->tag != DW_TAG_subprogram
13351 && func_die->tag != DW_TAG_subroutine_type;
13352 func_die = func_die->parent);
13353
216f72a1
JK
13354 /* DW_AT_call_all_calls is a superset
13355 of DW_AT_call_all_tail_calls. */
96408a79 13356 if (func_die
dda83cd7
SM
13357 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13358 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13359 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13360 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13361 {
13362 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13363 not complete. But keep CALL_SITE for look ups via call_site_htab,
13364 both the initial caller containing the real return address PC and
13365 the final callee containing the current PC of a chain of tail
13366 calls do not need to have the tail call list complete. But any
13367 function candidate for a virtual tail call frame searched via
13368 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13369 determined unambiguously. */
13370 }
13371 else
13372 {
13373 struct type *func_type = NULL;
13374
13375 if (func_die)
13376 func_type = get_die_type (func_die, cu);
13377 if (func_type != NULL)
13378 {
78134374 13379 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13380
13381 /* Enlist this call site to the function. */
13382 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13383 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13384 }
13385 else
b98664d3 13386 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13387 "DIE %s [in module %s]"),
13388 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13389 }
13390 }
13391
216f72a1
JK
13392 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13393 if (attr == NULL)
13394 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13395 if (attr == NULL)
13396 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13397 if (attr == NULL)
216f72a1
JK
13398 {
13399 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13400 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13401 }
96408a79 13402 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13403 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13404 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13405 else if (attr->form_is_block ())
96408a79
SA
13406 {
13407 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13408 struct dwarf_block *block = attr->as_block ();
96408a79 13409
8d749320 13410 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13411 dlbaton->data = block->data;
13412 dlbaton->size = block->size;
a50264ba 13413 dlbaton->per_objfile = per_objfile;
96408a79
SA
13414 dlbaton->per_cu = cu->per_cu;
13415
13416 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13417 }
cd6c91b4 13418 else if (attr->form_is_ref ())
96408a79 13419 {
96408a79
SA
13420 struct dwarf2_cu *target_cu = cu;
13421 struct die_info *target_die;
13422
ac9ec31b 13423 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13424 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13425 if (die_is_declaration (target_die, target_cu))
13426 {
7d45c7c3 13427 const char *target_physname;
9112db09
JK
13428
13429 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13430 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13431 if (target_physname == NULL)
9112db09 13432 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13433 if (target_physname == NULL)
b98664d3 13434 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13435 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13436 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13437 else
7d455152 13438 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13439 }
13440 else
13441 {
13442 CORE_ADDR lowpc;
13443
13444 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13445 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13446 <= PC_BOUNDS_INVALID)
b98664d3 13447 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13448 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13449 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13450 else
3e29f34a
MR
13451 {
13452 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13453 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13454 }
96408a79
SA
13455 }
13456 }
13457 else
b98664d3 13458 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13459 "block nor reference, for DIE %s [in module %s]"),
13460 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13461
13462 call_site->per_cu = cu->per_cu;
9f47c707 13463 call_site->per_objfile = per_objfile;
96408a79
SA
13464
13465 for (child_die = die->child;
13466 child_die && child_die->tag;
436c571c 13467 child_die = child_die->sibling)
96408a79 13468 {
96408a79 13469 struct call_site_parameter *parameter;
1788b2d3 13470 struct attribute *loc, *origin;
96408a79 13471
216f72a1 13472 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13473 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13474 {
13475 /* Already printed the complaint above. */
13476 continue;
13477 }
13478
13479 gdb_assert (call_site->parameter_count < nparams);
13480 parameter = &call_site->parameter[call_site->parameter_count];
13481
1788b2d3
JK
13482 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13483 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13484 register is contained in DW_AT_call_value. */
96408a79 13485
24c5c679 13486 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13487 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13488 if (origin == NULL)
13489 {
13490 /* This was a pre-DWARF-5 GNU extension alias
13491 for DW_AT_call_parameter. */
13492 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13493 }
cd6c91b4 13494 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13495 {
1788b2d3 13496 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13497
0826b30a 13498 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13499 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13500 {
13501 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13502 binding can be done only inside one CU. Such referenced DIE
13503 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13504 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13505 "DW_TAG_call_site child DIE %s [in module %s]"),
13506 sect_offset_str (child_die->sect_off),
9c541725 13507 objfile_name (objfile));
d76b7dbc
JK
13508 continue;
13509 }
9c541725
PA
13510 parameter->u.param_cu_off
13511 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13512 }
4fc6c0d5 13513 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13514 {
b98664d3 13515 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13516 "DW_TAG_call_site child DIE %s [in module %s]"),
13517 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13518 continue;
13519 }
24c5c679 13520 else
96408a79 13521 {
9d2246fc
TT
13522 struct dwarf_block *block = loc->as_block ();
13523
24c5c679 13524 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 13525 (block->data, &block->data[block->size]);
24c5c679
JK
13526 if (parameter->u.dwarf_reg != -1)
13527 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
13528 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13529 &block->data[block->size],
24c5c679
JK
13530 &parameter->u.fb_offset))
13531 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13532 else
13533 {
b98664d3 13534 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13535 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13536 "DW_TAG_call_site child DIE %s "
24c5c679 13537 "[in module %s]"),
9d8780f0 13538 sect_offset_str (child_die->sect_off),
9c541725 13539 objfile_name (objfile));
24c5c679
JK
13540 continue;
13541 }
96408a79
SA
13542 }
13543
216f72a1
JK
13544 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13545 if (attr == NULL)
13546 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13547 if (attr == NULL || !attr->form_is_block ())
96408a79 13548 {
b98664d3 13549 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13550 "DW_TAG_call_site child DIE %s [in module %s]"),
13551 sect_offset_str (child_die->sect_off),
9c541725 13552 objfile_name (objfile));
96408a79
SA
13553 continue;
13554 }
9d2246fc
TT
13555
13556 struct dwarf_block *block = attr->as_block ();
13557 parameter->value = block->data;
13558 parameter->value_size = block->size;
96408a79
SA
13559
13560 /* Parameters are not pre-cleared by memset above. */
13561 parameter->data_value = NULL;
13562 parameter->data_value_size = 0;
13563 call_site->parameter_count++;
13564
216f72a1
JK
13565 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13566 if (attr == NULL)
13567 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13568 if (attr != nullptr)
96408a79 13569 {
4fc6c0d5 13570 if (!attr->form_is_block ())
b98664d3 13571 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13572 "DW_TAG_call_site child DIE %s [in module %s]"),
13573 sect_offset_str (child_die->sect_off),
9c541725 13574 objfile_name (objfile));
96408a79
SA
13575 else
13576 {
9d2246fc
TT
13577 block = attr->as_block ();
13578 parameter->data_value = block->data;
13579 parameter->data_value_size = block->size;
96408a79
SA
13580 }
13581 }
13582 }
13583}
13584
71a3c369
TT
13585/* Helper function for read_variable. If DIE represents a virtual
13586 table, then return the type of the concrete object that is
13587 associated with the virtual table. Otherwise, return NULL. */
13588
13589static struct type *
13590rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13591{
13592 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13593 if (attr == NULL)
13594 return NULL;
13595
13596 /* Find the type DIE. */
13597 struct die_info *type_die = NULL;
13598 struct dwarf2_cu *type_cu = cu;
13599
cd6c91b4 13600 if (attr->form_is_ref ())
71a3c369
TT
13601 type_die = follow_die_ref (die, attr, &type_cu);
13602 if (type_die == NULL)
13603 return NULL;
13604
13605 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13606 return NULL;
13607 return die_containing_type (type_die, type_cu);
13608}
13609
13610/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13611
13612static void
13613read_variable (struct die_info *die, struct dwarf2_cu *cu)
13614{
13615 struct rust_vtable_symbol *storage = NULL;
13616
3e9f1ca1 13617 if (cu->per_cu->lang == language_rust)
71a3c369
TT
13618 {
13619 struct type *containing_type = rust_containing_type (die, cu);
13620
13621 if (containing_type != NULL)
13622 {
5e22e966 13623 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13624
8c14c3a3 13625 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13626 storage->concrete_type = containing_type;
cf724bc9 13627 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13628 }
13629 }
13630
e4a62c65
TV
13631 struct symbol *res = new_symbol (die, NULL, cu, storage);
13632 struct attribute *abstract_origin
13633 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13634 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13635 if (res == NULL && loc && abstract_origin)
13636 {
13637 /* We have a variable without a name, but with a location and an abstract
13638 origin. This may be a concrete instance of an abstract variable
13639 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13640 later. */
13641 struct dwarf2_cu *origin_cu = cu;
13642 struct die_info *origin_die
13643 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13644 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13645 per_objfile->per_bfd->abstract_to_concrete
13646 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13647 }
71a3c369
TT
13648}
13649
43988095
JK
13650/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13651 reading .debug_rnglists.
13652 Callback's type should be:
13653 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13654 Return true if the attributes are present and valid, otherwise,
13655 return false. */
13656
13657template <typename Callback>
13658static bool
13659dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 13660 dwarf_tag tag, Callback &&callback)
43988095 13661{
976ca316
SM
13662 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13663 struct objfile *objfile = per_objfile->objfile;
43988095 13664 bfd *obfd = objfile->obfd;
43988095 13665 /* Base address selection entry. */
2b24b6e4 13666 gdb::optional<CORE_ADDR> base;
43988095 13667 const gdb_byte *buffer;
43988095 13668 bool overflow = false;
d0ce17d8
CT
13669 ULONGEST addr_index;
13670 struct dwarf2_section_info *rnglists_section;
43988095 13671
43988095 13672 base = cu->base_address;
d0ce17d8
CT
13673 rnglists_section = cu_debug_rnglists_section (cu, tag);
13674 rnglists_section->read (objfile);
43988095 13675
d0ce17d8 13676 if (offset >= rnglists_section->size)
43988095 13677 {
b98664d3 13678 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13679 offset);
13680 return false;
13681 }
d0ce17d8 13682 buffer = rnglists_section->buffer + offset;
43988095 13683
43988095
JK
13684 while (1)
13685 {
7814882a
JK
13686 /* Initialize it due to a false compiler warning. */
13687 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
13688 const gdb_byte *buf_end = (rnglists_section->buffer
13689 + rnglists_section->size);
43988095
JK
13690 unsigned int bytes_read;
13691
13692 if (buffer == buf_end)
13693 {
13694 overflow = true;
13695 break;
13696 }
13697 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13698 switch (rlet)
13699 {
13700 case DW_RLE_end_of_list:
13701 break;
13702 case DW_RLE_base_address:
13703 if (buffer + cu->header.addr_size > buf_end)
13704 {
13705 overflow = true;
13706 break;
13707 }
c8a7a66f 13708 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13709 buffer += bytes_read;
13710 break;
dda83cd7
SM
13711 case DW_RLE_base_addressx:
13712 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13713 buffer += bytes_read;
13714 base = read_addr_index (cu, addr_index);
13715 break;
43988095
JK
13716 case DW_RLE_start_length:
13717 if (buffer + cu->header.addr_size > buf_end)
13718 {
13719 overflow = true;
13720 break;
13721 }
c8a7a66f
TT
13722 range_beginning = cu->header.read_address (obfd, buffer,
13723 &bytes_read);
43988095
JK
13724 buffer += bytes_read;
13725 range_end = (range_beginning
13726 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13727 buffer += bytes_read;
13728 if (buffer > buf_end)
13729 {
13730 overflow = true;
13731 break;
13732 }
13733 break;
d0ce17d8 13734 case DW_RLE_startx_length:
dda83cd7
SM
13735 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13736 buffer += bytes_read;
13737 range_beginning = read_addr_index (cu, addr_index);
13738 if (buffer > buf_end)
13739 {
13740 overflow = true;
13741 break;
13742 }
13743 range_end = (range_beginning
13744 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13745 buffer += bytes_read;
13746 break;
43988095
JK
13747 case DW_RLE_offset_pair:
13748 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13749 buffer += bytes_read;
13750 if (buffer > buf_end)
13751 {
13752 overflow = true;
13753 break;
13754 }
13755 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13756 buffer += bytes_read;
13757 if (buffer > buf_end)
13758 {
13759 overflow = true;
13760 break;
13761 }
13762 break;
13763 case DW_RLE_start_end:
13764 if (buffer + 2 * cu->header.addr_size > buf_end)
13765 {
13766 overflow = true;
13767 break;
13768 }
c8a7a66f
TT
13769 range_beginning = cu->header.read_address (obfd, buffer,
13770 &bytes_read);
43988095 13771 buffer += bytes_read;
c8a7a66f 13772 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13773 buffer += bytes_read;
13774 break;
d0ce17d8 13775 case DW_RLE_startx_endx:
dda83cd7
SM
13776 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13777 buffer += bytes_read;
13778 range_beginning = read_addr_index (cu, addr_index);
13779 if (buffer > buf_end)
13780 {
13781 overflow = true;
13782 break;
13783 }
13784 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13785 buffer += bytes_read;
13786 range_end = read_addr_index (cu, addr_index);
13787 break;
43988095 13788 default:
b98664d3 13789 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13790 return false;
13791 }
13792 if (rlet == DW_RLE_end_of_list || overflow)
13793 break;
13794 if (rlet == DW_RLE_base_address)
13795 continue;
13796
43988095
JK
13797 if (range_beginning > range_end)
13798 {
13799 /* Inverted range entries are invalid. */
b98664d3 13800 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13801 return false;
13802 }
13803
13804 /* Empty range entries have no effect. */
13805 if (range_beginning == range_end)
13806 continue;
13807
d0ce17d8
CT
13808 /* Only DW_RLE_offset_pair needs the base address added. */
13809 if (rlet == DW_RLE_offset_pair)
13810 {
13811 if (!base.has_value ())
13812 {
13813 /* We have no valid base address for the DW_RLE_offset_pair. */
13814 complaint (_("Invalid .debug_rnglists data (no base address for "
13815 "DW_RLE_offset_pair)"));
13816 return false;
13817 }
13818
13819 range_beginning += *base;
13820 range_end += *base;
13821 }
43988095
JK
13822
13823 /* A not-uncommon case of bad debug info.
13824 Don't pollute the addrmap with bad data. */
a656ee08 13825 if (range_beginning == 0
976ca316 13826 && !per_objfile->per_bfd->has_section_at_zero)
43988095 13827 {
b98664d3 13828 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13829 " [in module %s]"), objfile_name (objfile));
13830 continue;
13831 }
13832
13833 callback (range_beginning, range_end);
13834 }
13835
13836 if (overflow)
13837 {
b98664d3 13838 complaint (_("Offset %d is not terminated "
43988095
JK
13839 "for DW_AT_ranges attribute"),
13840 offset);
13841 return false;
13842 }
13843
13844 return true;
13845}
13846
13847/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13848 Callback's type should be:
13849 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13850 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13851
43988095 13852template <typename Callback>
43039443 13853static int
d0ce17d8 13854dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 13855 Callback &&callback)
43039443 13856{
5e22e966
SM
13857 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13858 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13859 struct comp_unit_head *cu_header = &cu->header;
13860 bfd *obfd = objfile->obfd;
13861 unsigned int addr_size = cu_header->addr_size;
13862 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13863 /* Base address selection entry. */
2b24b6e4 13864 gdb::optional<CORE_ADDR> base;
43039443 13865 unsigned int dummy;
d521ce57 13866 const gdb_byte *buffer;
43039443 13867
43988095 13868 if (cu_header->version >= 5)
d0ce17d8 13869 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 13870
d00adf39 13871 base = cu->base_address;
43039443 13872
5e22e966
SM
13873 per_objfile->per_bfd->ranges.read (objfile);
13874 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13875 {
b98664d3 13876 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13877 offset);
13878 return 0;
13879 }
5e22e966 13880 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13881
43039443
JK
13882 while (1)
13883 {
13884 CORE_ADDR range_beginning, range_end;
13885
c8a7a66f 13886 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13887 buffer += addr_size;
c8a7a66f 13888 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13889 buffer += addr_size;
13890 offset += 2 * addr_size;
13891
13892 /* An end of list marker is a pair of zero addresses. */
13893 if (range_beginning == 0 && range_end == 0)
13894 /* Found the end of list entry. */
13895 break;
13896
13897 /* Each base address selection entry is a pair of 2 values.
13898 The first is the largest possible address, the second is
13899 the base address. Check for a base address here. */
13900 if ((range_beginning & mask) == mask)
13901 {
28d2bfb9
AB
13902 /* If we found the largest possible address, then we already
13903 have the base address in range_end. */
13904 base = range_end;
43039443
JK
13905 continue;
13906 }
13907
2b24b6e4 13908 if (!base.has_value ())
43039443
JK
13909 {
13910 /* We have no valid base address for the ranges
13911 data. */
b98664d3 13912 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13913 return 0;
13914 }
13915
9277c30c
UW
13916 if (range_beginning > range_end)
13917 {
13918 /* Inverted range entries are invalid. */
b98664d3 13919 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13920 return 0;
13921 }
13922
13923 /* Empty range entries have no effect. */
13924 if (range_beginning == range_end)
13925 continue;
13926
2b24b6e4
TT
13927 range_beginning += *base;
13928 range_end += *base;
43039443 13929
01093045
DE
13930 /* A not-uncommon case of bad debug info.
13931 Don't pollute the addrmap with bad data. */
a656ee08 13932 if (range_beginning == 0
5e22e966 13933 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13934 {
b98664d3 13935 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13936 " [in module %s]"), objfile_name (objfile));
01093045
DE
13937 continue;
13938 }
13939
5f46c5a5
JK
13940 callback (range_beginning, range_end);
13941 }
13942
13943 return 1;
13944}
13945
13946/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13947 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 13948 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
13949
13950static int
13951dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13952 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 13953 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 13954{
5e22e966 13955 struct objfile *objfile = cu->per_objfile->objfile;
84685904 13956 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 13957 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13958 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13959 int low_set = 0;
13960 CORE_ADDR low = 0;
13961 CORE_ADDR high = 0;
13962 int retval;
13963
d0ce17d8 13964 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
13965 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13966 {
9277c30c 13967 if (ranges_pst != NULL)
3e29f34a
MR
13968 {
13969 CORE_ADDR lowpc;
13970 CORE_ADDR highpc;
13971
79748972
TT
13972 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13973 range_beginning + baseaddr)
13974 - baseaddr);
13975 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13976 range_end + baseaddr)
13977 - baseaddr);
84685904 13978 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 13979 lowpc, highpc - 1, ranges_pst);
3e29f34a 13980 }
ff013f42 13981
43039443
JK
13982 /* FIXME: This is recording everything as a low-high
13983 segment of consecutive addresses. We should have a
13984 data structure for discontiguous block ranges
13985 instead. */
13986 if (! low_set)
13987 {
13988 low = range_beginning;
13989 high = range_end;
13990 low_set = 1;
13991 }
13992 else
13993 {
13994 if (range_beginning < low)
13995 low = range_beginning;
13996 if (range_end > high)
13997 high = range_end;
13998 }
5f46c5a5
JK
13999 });
14000 if (!retval)
14001 return 0;
43039443
JK
14002
14003 if (! low_set)
14004 /* If the first entry is an end-of-list marker, the range
14005 describes an empty scope, i.e. no instructions. */
14006 return 0;
14007
14008 if (low_return)
14009 *low_return = low;
14010 if (high_return)
14011 *high_return = high;
14012 return 1;
14013}
14014
3a2b436a
JK
14015/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14016 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14017 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14018
3a2b436a 14019static enum pc_bounds_kind
af34e669 14020dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14021 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14022 dwarf2_psymtab *pst)
c906108c 14023{
976ca316 14024 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14025 struct attribute *attr;
91da1414 14026 struct attribute *attr_high;
af34e669
DJ
14027 CORE_ADDR low = 0;
14028 CORE_ADDR high = 0;
e385593e 14029 enum pc_bounds_kind ret;
c906108c 14030
91da1414
MW
14031 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14032 if (attr_high)
af34e669 14033 {
e142c38c 14034 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14035 if (attr != nullptr)
dda83cd7 14036 {
95f982e5
TT
14037 low = attr->as_address ();
14038 high = attr_high->as_address ();
cd6c91b4 14039 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14040 high += low;
91da1414 14041 }
af34e669
DJ
14042 else
14043 /* Found high w/o low attribute. */
e385593e 14044 return PC_BOUNDS_INVALID;
af34e669
DJ
14045
14046 /* Found consecutive range of addresses. */
3a2b436a 14047 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14048 }
c906108c 14049 else
af34e669 14050 {
e142c38c 14051 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14052 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14053 {
2b0c7f41
SM
14054 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14055 on DWARF version). */
14056 ULONGEST ranges_offset = attr->as_unsigned ();
14057
14058 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14059 this value. */
14060 if (die->tag != DW_TAG_compile_unit)
14061 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14062
af34e669 14063 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14064 .debug_ranges section. */
d0ce17d8
CT
14065 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14066 die->tag))
e385593e 14067 return PC_BOUNDS_INVALID;
43039443 14068 /* Found discontinuous range of addresses. */
3a2b436a 14069 ret = PC_BOUNDS_RANGES;
af34e669 14070 }
e385593e
JK
14071 else
14072 return PC_BOUNDS_NOT_PRESENT;
af34e669 14073 }
c906108c 14074
48fbe735 14075 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14076 if (high <= low)
e385593e 14077 return PC_BOUNDS_INVALID;
c906108c
SS
14078
14079 /* When using the GNU linker, .gnu.linkonce. sections are used to
14080 eliminate duplicate copies of functions and vtables and such.
14081 The linker will arbitrarily choose one and discard the others.
14082 The AT_*_pc values for such functions refer to local labels in
14083 these sections. If the section from that file was discarded, the
14084 labels are not in the output, so the relocs get a value of 0.
14085 If this is a discarded function, mark the pc bounds as invalid,
14086 so that GDB will ignore it. */
976ca316 14087 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14088 return PC_BOUNDS_INVALID;
c906108c
SS
14089
14090 *lowpc = low;
96408a79
SA
14091 if (highpc)
14092 *highpc = high;
af34e669 14093 return ret;
c906108c
SS
14094}
14095
b084d499
JB
14096/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14097 its low and high PC addresses. Do nothing if these addresses could not
14098 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14099 and HIGHPC to the high address if greater than HIGHPC. */
14100
14101static void
14102dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14103 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14104 struct dwarf2_cu *cu)
b084d499
JB
14105{
14106 CORE_ADDR low, high;
14107 struct die_info *child = die->child;
14108
e385593e 14109 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14110 {
325fac50
PA
14111 *lowpc = std::min (*lowpc, low);
14112 *highpc = std::max (*highpc, high);
b084d499
JB
14113 }
14114
14115 /* If the language does not allow nested subprograms (either inside
14116 subprograms or lexical blocks), we're done. */
3e9f1ca1 14117 if (cu->per_cu->lang != language_ada)
b084d499 14118 return;
6e70227d 14119
b084d499
JB
14120 /* Check all the children of the given DIE. If it contains nested
14121 subprograms, then check their pc bounds. Likewise, we need to
14122 check lexical blocks as well, as they may also contain subprogram
14123 definitions. */
14124 while (child && child->tag)
14125 {
14126 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14127 || child->tag == DW_TAG_lexical_block)
14128 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14129 child = child->sibling;
b084d499
JB
14130 }
14131}
14132
fae299cd
DC
14133/* Get the low and high pc's represented by the scope DIE, and store
14134 them in *LOWPC and *HIGHPC. If the correct values can't be
14135 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14136
14137static void
14138get_scope_pc_bounds (struct die_info *die,
14139 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14140 struct dwarf2_cu *cu)
14141{
14142 CORE_ADDR best_low = (CORE_ADDR) -1;
14143 CORE_ADDR best_high = (CORE_ADDR) 0;
14144 CORE_ADDR current_low, current_high;
14145
3a2b436a 14146 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14147 >= PC_BOUNDS_RANGES)
fae299cd
DC
14148 {
14149 best_low = current_low;
14150 best_high = current_high;
14151 }
14152 else
14153 {
14154 struct die_info *child = die->child;
14155
14156 while (child && child->tag)
14157 {
14158 switch (child->tag) {
14159 case DW_TAG_subprogram:
dda83cd7 14160 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14161 break;
14162 case DW_TAG_namespace:
f55ee35c 14163 case DW_TAG_module:
fae299cd
DC
14164 /* FIXME: carlton/2004-01-16: Should we do this for
14165 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14166 that current GCC's always emit the DIEs corresponding
14167 to definitions of methods of classes as children of a
14168 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14169 the DIEs giving the declarations, which could be
14170 anywhere). But I don't see any reason why the
14171 standards says that they have to be there. */
14172 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14173
14174 if (current_low != ((CORE_ADDR) -1))
14175 {
325fac50
PA
14176 best_low = std::min (best_low, current_low);
14177 best_high = std::max (best_high, current_high);
fae299cd
DC
14178 }
14179 break;
14180 default:
0963b4bd 14181 /* Ignore. */
fae299cd
DC
14182 break;
14183 }
14184
436c571c 14185 child = child->sibling;
fae299cd
DC
14186 }
14187 }
14188
14189 *lowpc = best_low;
14190 *highpc = best_high;
14191}
14192
801e3a5b
JB
14193/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14194 in DIE. */
380bca97 14195
801e3a5b
JB
14196static void
14197dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14198 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14199{
5e22e966 14200 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14201 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14202 struct attribute *attr;
91da1414 14203 struct attribute *attr_high;
801e3a5b 14204
91da1414
MW
14205 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14206 if (attr_high)
801e3a5b 14207 {
801e3a5b 14208 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14209 if (attr != nullptr)
dda83cd7 14210 {
95f982e5
TT
14211 CORE_ADDR low = attr->as_address ();
14212 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14213
cd6c91b4 14214 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14215 high += low;
9a619af0 14216
3e29f34a
MR
14217 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14218 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14219 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14220 }
801e3a5b
JB
14221 }
14222
14223 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14224 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14225 {
2b0c7f41
SM
14226 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14227 on DWARF version). */
14228 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14229
2b0c7f41
SM
14230 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14231 this value. */
14232 if (die->tag != DW_TAG_compile_unit)
14233 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14234
2d5f09ec 14235 std::vector<blockrange> blockvec;
2b0c7f41 14236 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14237 [&] (CORE_ADDR start, CORE_ADDR end)
14238 {
58fdfd2c
JK
14239 start += baseaddr;
14240 end += baseaddr;
5f46c5a5
JK
14241 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14242 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14243 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14244 blockvec.emplace_back (start, end);
5f46c5a5 14245 });
2d5f09ec
KB
14246
14247 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14248 }
14249}
14250
685b1105
JK
14251/* Check whether the producer field indicates either of GCC < 4.6, or the
14252 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14253
685b1105
JK
14254static void
14255check_producer (struct dwarf2_cu *cu)
60d5a603 14256{
38360086 14257 int major, minor;
60d5a603
JK
14258
14259 if (cu->producer == NULL)
14260 {
14261 /* For unknown compilers expect their behavior is DWARF version
14262 compliant.
14263
14264 GCC started to support .debug_types sections by -gdwarf-4 since
14265 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14266 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14267 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14268 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14269 }
b1ffba5a 14270 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14271 {
38360086
MW
14272 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14273 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14274 }
5230b05a 14275 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14276 {
14277 cu->producer_is_icc = true;
14278 cu->producer_is_icc_lt_14 = major < 14;
14279 }
c258c396
JD
14280 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14281 cu->producer_is_codewarrior = true;
685b1105
JK
14282 else
14283 {
14284 /* For other non-GCC compilers, expect their behavior is DWARF version
14285 compliant. */
60d5a603
JK
14286 }
14287
9068261f 14288 cu->checked_producer = true;
685b1105 14289}
ba919b58 14290
685b1105
JK
14291/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14292 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14293 during 4.6.0 experimental. */
14294
9068261f 14295static bool
685b1105
JK
14296producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14297{
14298 if (!cu->checked_producer)
14299 check_producer (cu);
14300
14301 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14302}
14303
c258c396
JD
14304
14305/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14306 with incorrect is_stmt attributes. */
14307
14308static bool
14309producer_is_codewarrior (struct dwarf2_cu *cu)
14310{
14311 if (!cu->checked_producer)
14312 check_producer (cu);
14313
14314 return cu->producer_is_codewarrior;
14315}
14316
bf23a268
TT
14317/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14318 If that attribute is not available, return the appropriate
14319 default. */
60d5a603
JK
14320
14321static enum dwarf_access_attribute
bf23a268 14322dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14323{
bf23a268
TT
14324 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14325 if (attr != nullptr)
14326 {
14327 LONGEST value = attr->constant_value (-1);
14328 if (value == DW_ACCESS_public
14329 || value == DW_ACCESS_protected
14330 || value == DW_ACCESS_private)
14331 return (dwarf_access_attribute) value;
14332 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14333 plongest (value));
14334 }
14335
60d5a603
JK
14336 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14337 {
14338 /* The default DWARF 2 accessibility for members is public, the default
14339 accessibility for inheritance is private. */
14340
14341 if (die->tag != DW_TAG_inheritance)
14342 return DW_ACCESS_public;
14343 else
14344 return DW_ACCESS_private;
14345 }
14346 else
14347 {
14348 /* DWARF 3+ defines the default accessibility a different way. The same
14349 rules apply now for DW_TAG_inheritance as for the members and it only
14350 depends on the container kind. */
14351
14352 if (die->parent->tag == DW_TAG_class_type)
14353 return DW_ACCESS_private;
14354 else
14355 return DW_ACCESS_public;
14356 }
14357}
14358
74ac6d43
TT
14359/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14360 offset. If the attribute was not found return 0, otherwise return
14361 1. If it was found but could not properly be handled, set *OFFSET
14362 to 0. */
14363
14364static int
14365handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14366 LONGEST *offset)
14367{
14368 struct attribute *attr;
14369
14370 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14371 if (attr != NULL)
14372 {
14373 *offset = 0;
14374
14375 /* Note that we do not check for a section offset first here.
14376 This is because DW_AT_data_member_location is new in DWARF 4,
14377 so if we see it, we can assume that a constant form is really
14378 a constant and not a section offset. */
cd6c91b4 14379 if (attr->form_is_constant ())
0826b30a 14380 *offset = attr->constant_value (0);
cd6c91b4 14381 else if (attr->form_is_section_offset ())
74ac6d43 14382 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14383 else if (attr->form_is_block ())
9d2246fc 14384 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14385 else
14386 dwarf2_complex_location_expr_complaint ();
14387
14388 return 1;
14389 }
14390
14391 return 0;
14392}
14393
7d79de9a
TT
14394/* Look for DW_AT_data_member_location and store the results in FIELD. */
14395
14396static void
14397handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14398 struct field *field)
14399{
14400 struct attribute *attr;
14401
14402 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14403 if (attr != NULL)
14404 {
14405 if (attr->form_is_constant ())
14406 {
14407 LONGEST offset = attr->constant_value (0);
14408 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14409 }
14410 else if (attr->form_is_section_offset ())
14411 dwarf2_complex_location_expr_complaint ();
14412 else if (attr->form_is_block ())
14413 {
14414 bool handled;
9d2246fc 14415 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14416 if (handled)
14417 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14418 else
14419 {
5e22e966
SM
14420 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14421 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14422 struct dwarf2_locexpr_baton *dlbaton
14423 = XOBNEW (&objfile->objfile_obstack,
14424 struct dwarf2_locexpr_baton);
9d2246fc
TT
14425 dlbaton->data = attr->as_block ()->data;
14426 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14427 /* When using this baton, we want to compute the address
14428 of the field, not the value. This is why
14429 is_reference is set to false here. */
14430 dlbaton->is_reference = false;
5e22e966 14431 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14432 dlbaton->per_cu = cu->per_cu;
14433
14434 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14435 }
14436 }
14437 else
14438 dwarf2_complex_location_expr_complaint ();
14439 }
14440}
14441
c906108c
SS
14442/* Add an aggregate field to the field list. */
14443
14444static void
107d2387 14445dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14446 struct dwarf2_cu *cu)
6e70227d 14447{
5e22e966 14448 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14449 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14450 struct nextfield *new_field;
14451 struct attribute *attr;
14452 struct field *fp;
15d034d0 14453 const char *fieldname = "";
c906108c 14454
7d0ccb61
DJ
14455 if (die->tag == DW_TAG_inheritance)
14456 {
be2daae6
TT
14457 fip->baseclasses.emplace_back ();
14458 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14459 }
14460 else
14461 {
be2daae6
TT
14462 fip->fields.emplace_back ();
14463 new_field = &fip->fields.back ();
7d0ccb61 14464 }
be2daae6 14465
9c6a1327
TT
14466 new_field->offset = die->sect_off;
14467
bf23a268 14468 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 14469 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 14470 fip->non_public_fields = true;
60d5a603 14471
e142c38c 14472 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14473 if (attr != nullptr)
23dca5c3 14474 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
14475 else
14476 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14477
14478 fp = &new_field->field;
a9a9bd0f 14479
e142c38c 14480 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14481 {
a9a9bd0f 14482 /* Data member other than a C++ static data member. */
6e70227d 14483
c906108c 14484 /* Get type of field. */
5d14b6e5 14485 fp->set_type (die_type (die, cu));
c906108c 14486
d6a843b5 14487 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14488
c906108c 14489 /* Get bit size of field (zero if none). */
e142c38c 14490 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14491 if (attr != nullptr)
c906108c 14492 {
529908cb 14493 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
14494 }
14495 else
14496 {
14497 FIELD_BITSIZE (*fp) = 0;
14498 }
14499
14500 /* Get bit offset of field. */
7d79de9a 14501 handle_data_member_location (die, cu, fp);
e142c38c 14502 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 14503 if (attr != nullptr && attr->form_is_constant ())
c906108c 14504 {
d5a22e77 14505 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14506 {
14507 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
14508 additional bit offset from the MSB of the containing
14509 anonymous object to the MSB of the field. We don't
14510 have to do anything special since we don't need to
14511 know the size of the anonymous object. */
529908cb 14512 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 14513 + attr->constant_value (0)));
c906108c
SS
14514 }
14515 else
14516 {
14517 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
14518 MSB of the anonymous object, subtract off the number of
14519 bits from the MSB of the field to the MSB of the
14520 object, and then subtract off the number of bits of
14521 the field itself. The result is the bit offset of
14522 the LSB of the field. */
c906108c 14523 int anonymous_size;
7c184d33 14524 int bit_offset = attr->constant_value (0);
c906108c 14525
e142c38c 14526 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 14527 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
14528 {
14529 /* The size of the anonymous object containing
14530 the bit field is explicit, so use the
14531 indicated size (in bytes). */
7c184d33 14532 anonymous_size = attr->constant_value (0);
c906108c
SS
14533 }
14534 else
14535 {
14536 /* The size of the anonymous object containing
14537 the bit field must be inferred from the type
14538 attribute of the data member containing the
14539 bit field. */
5d14b6e5 14540 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14541 }
f41f5e61
PA
14542 SET_FIELD_BITPOS (*fp,
14543 (FIELD_BITPOS (*fp)
14544 + anonymous_size * bits_per_byte
14545 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14546 }
14547 }
da5b30da
AA
14548 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14549 if (attr != NULL)
14550 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14551 + attr->constant_value (0)));
c906108c
SS
14552
14553 /* Get name of field. */
39cbfefa
DJ
14554 fieldname = dwarf2_name (die, cu);
14555 if (fieldname == NULL)
14556 fieldname = "";
d8151005
DJ
14557
14558 /* The name is already allocated along with this objfile, so we don't
14559 need to duplicate it for the type. */
14560 fp->name = fieldname;
c906108c
SS
14561
14562 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 14563 pointer or virtual base class pointer) to private. */
e142c38c 14564 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14565 {
d48cc9dd 14566 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 14567 new_field->accessibility = DW_ACCESS_private;
264fc0e2 14568 fip->non_public_fields = true;
c906108c
SS
14569 }
14570 }
a9a9bd0f 14571 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14572 {
a9a9bd0f
DC
14573 /* C++ static member. */
14574
14575 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14576 is a declaration, but all versions of G++ as of this writing
14577 (so through at least 3.2.1) incorrectly generate
14578 DW_TAG_variable tags. */
6e70227d 14579
ff355380 14580 const char *physname;
c906108c 14581
a9a9bd0f 14582 /* Get name of field. */
39cbfefa
DJ
14583 fieldname = dwarf2_name (die, cu);
14584 if (fieldname == NULL)
c906108c
SS
14585 return;
14586
254e6b9e 14587 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14588 if (attr
14589 /* Only create a symbol if this is an external value.
14590 new_symbol checks this and puts the value in the global symbol
14591 table, which we want. If it is not external, new_symbol
14592 will try to put the value in cu->list_in_scope which is wrong. */
14593 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14594 {
14595 /* A static const member, not much different than an enum as far as
14596 we're concerned, except that we can support more types. */
14597 new_symbol (die, NULL, cu);
14598 }
14599
2df3850c 14600 /* Get physical name. */
ff355380 14601 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14602
d8151005
DJ
14603 /* The name is already allocated along with this objfile, so we don't
14604 need to duplicate it for the type. */
14605 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14606 fp->set_type (die_type (die, cu));
d8151005 14607 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14608 }
14609 else if (die->tag == DW_TAG_inheritance)
14610 {
74ac6d43 14611 /* C++ base class field. */
7d79de9a 14612 handle_data_member_location (die, cu, fp);
c906108c 14613 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14614 fp->set_type (die_type (die, cu));
14615 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14616 }
2ddeaf8a
TT
14617 else
14618 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14619}
14620
883fd55a
KS
14621/* Can the type given by DIE define another type? */
14622
14623static bool
14624type_can_define_types (const struct die_info *die)
14625{
14626 switch (die->tag)
14627 {
14628 case DW_TAG_typedef:
14629 case DW_TAG_class_type:
14630 case DW_TAG_structure_type:
14631 case DW_TAG_union_type:
14632 case DW_TAG_enumeration_type:
14633 return true;
14634
14635 default:
14636 return false;
14637 }
14638}
14639
14640/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14641
14642static void
883fd55a
KS
14643dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14644 struct dwarf2_cu *cu)
6e70227d 14645{
be2daae6
TT
14646 struct decl_field fp;
14647 memset (&fp, 0, sizeof (fp));
98751a41 14648
883fd55a 14649 gdb_assert (type_can_define_types (die));
98751a41 14650
883fd55a 14651 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14652 fp.name = dwarf2_name (die, cu);
14653 fp.type = read_type_die (die, cu);
98751a41 14654
c191a687 14655 /* Save accessibility. */
bf23a268 14656 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
14657 switch (accessibility)
14658 {
14659 case DW_ACCESS_public:
14660 /* The assumed value if neither private nor protected. */
14661 break;
14662 case DW_ACCESS_private:
be2daae6 14663 fp.is_private = 1;
c191a687
KS
14664 break;
14665 case DW_ACCESS_protected:
be2daae6 14666 fp.is_protected = 1;
c191a687 14667 break;
c191a687
KS
14668 }
14669
883fd55a 14670 if (die->tag == DW_TAG_typedef)
be2daae6 14671 fip->typedef_field_list.push_back (fp);
883fd55a 14672 else
be2daae6 14673 fip->nested_types_list.push_back (fp);
98751a41
JK
14674}
14675
9c6a1327
TT
14676/* A convenience typedef that's used when finding the discriminant
14677 field for a variant part. */
1b95cdb7
SM
14678typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14679 offset_map_type;
9c6a1327
TT
14680
14681/* Compute the discriminant range for a given variant. OBSTACK is
14682 where the results will be stored. VARIANT is the variant to
14683 process. IS_UNSIGNED indicates whether the discriminant is signed
14684 or unsigned. */
14685
14686static const gdb::array_view<discriminant_range>
14687convert_variant_range (struct obstack *obstack, const variant_field &variant,
14688 bool is_unsigned)
14689{
14690 std::vector<discriminant_range> ranges;
14691
14692 if (variant.default_branch)
14693 return {};
14694
14695 if (variant.discr_list_data == nullptr)
14696 {
14697 discriminant_range r
14698 = {variant.discriminant_value, variant.discriminant_value};
14699 ranges.push_back (r);
14700 }
14701 else
14702 {
14703 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14704 variant.discr_list_data->size);
14705 while (!data.empty ())
14706 {
14707 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14708 {
14709 complaint (_("invalid discriminant marker: %d"), data[0]);
14710 break;
14711 }
14712 bool is_range = data[0] == DW_DSC_range;
14713 data = data.slice (1);
14714
14715 ULONGEST low, high;
14716 unsigned int bytes_read;
14717
14718 if (data.empty ())
14719 {
14720 complaint (_("DW_AT_discr_list missing low value"));
14721 break;
14722 }
14723 if (is_unsigned)
14724 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14725 else
14726 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14727 &bytes_read);
14728 data = data.slice (bytes_read);
14729
14730 if (is_range)
14731 {
14732 if (data.empty ())
14733 {
14734 complaint (_("DW_AT_discr_list missing high value"));
14735 break;
14736 }
14737 if (is_unsigned)
14738 high = read_unsigned_leb128 (nullptr, data.data (),
14739 &bytes_read);
14740 else
14741 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14742 &bytes_read);
14743 data = data.slice (bytes_read);
14744 }
14745 else
14746 high = low;
14747
14748 ranges.push_back ({ low, high });
14749 }
14750 }
14751
14752 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14753 ranges.size ());
14754 std::copy (ranges.begin (), ranges.end (), result);
14755 return gdb::array_view<discriminant_range> (result, ranges.size ());
14756}
14757
14758static const gdb::array_view<variant_part> create_variant_parts
14759 (struct obstack *obstack,
14760 const offset_map_type &offset_map,
14761 struct field_info *fi,
14762 const std::vector<variant_part_builder> &variant_parts);
14763
14764/* Fill in a "struct variant" for a given variant field. RESULT is
14765 the variant to fill in. OBSTACK is where any needed allocations
14766 will be done. OFFSET_MAP holds the mapping from section offsets to
14767 fields for the type. FI describes the fields of the type we're
14768 processing. FIELD is the variant field we're converting. */
14769
14770static void
14771create_one_variant (variant &result, struct obstack *obstack,
14772 const offset_map_type &offset_map,
14773 struct field_info *fi, const variant_field &field)
14774{
14775 result.discriminants = convert_variant_range (obstack, field, false);
14776 result.first_field = field.first_field + fi->baseclasses.size ();
14777 result.last_field = field.last_field + fi->baseclasses.size ();
14778 result.parts = create_variant_parts (obstack, offset_map, fi,
14779 field.variant_parts);
14780}
14781
14782/* Fill in a "struct variant_part" for a given variant part. RESULT
14783 is the variant part to fill in. OBSTACK is where any needed
14784 allocations will be done. OFFSET_MAP holds the mapping from
14785 section offsets to fields for the type. FI describes the fields of
14786 the type we're processing. BUILDER is the variant part to be
14787 converted. */
14788
14789static void
14790create_one_variant_part (variant_part &result,
14791 struct obstack *obstack,
14792 const offset_map_type &offset_map,
14793 struct field_info *fi,
14794 const variant_part_builder &builder)
14795{
14796 auto iter = offset_map.find (builder.discriminant_offset);
14797 if (iter == offset_map.end ())
14798 {
14799 result.discriminant_index = -1;
14800 /* Doesn't matter. */
14801 result.is_unsigned = false;
14802 }
14803 else
14804 {
14805 result.discriminant_index = iter->second;
14806 result.is_unsigned
c6d940a9 14807 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
14808 }
14809
14810 size_t n = builder.variants.size ();
14811 variant *output = new (obstack) variant[n];
14812 for (size_t i = 0; i < n; ++i)
14813 create_one_variant (output[i], obstack, offset_map, fi,
14814 builder.variants[i]);
14815
14816 result.variants = gdb::array_view<variant> (output, n);
14817}
14818
14819/* Create a vector of variant parts that can be attached to a type.
14820 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14821 holds the mapping from section offsets to fields for the type. FI
14822 describes the fields of the type we're processing. VARIANT_PARTS
14823 is the vector to convert. */
14824
14825static const gdb::array_view<variant_part>
14826create_variant_parts (struct obstack *obstack,
14827 const offset_map_type &offset_map,
14828 struct field_info *fi,
14829 const std::vector<variant_part_builder> &variant_parts)
14830{
14831 if (variant_parts.empty ())
14832 return {};
14833
14834 size_t n = variant_parts.size ();
14835 variant_part *result = new (obstack) variant_part[n];
14836 for (size_t i = 0; i < n; ++i)
14837 create_one_variant_part (result[i], obstack, offset_map, fi,
14838 variant_parts[i]);
14839
14840 return gdb::array_view<variant_part> (result, n);
14841}
14842
14843/* Compute the variant part vector for FIP, attaching it to TYPE when
14844 done. */
14845
14846static void
14847add_variant_property (struct field_info *fip, struct type *type,
14848 struct dwarf2_cu *cu)
14849{
14850 /* Map section offsets of fields to their field index. Note the
14851 field index here does not take the number of baseclasses into
14852 account. */
14853 offset_map_type offset_map;
14854 for (int i = 0; i < fip->fields.size (); ++i)
14855 offset_map[fip->fields[i].offset] = i;
14856
5e22e966 14857 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14858 gdb::array_view<variant_part> parts
14859 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14860 fip->variant_parts);
14861
14862 struct dynamic_prop prop;
8c2e4e06
SM
14863 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14864 obstack_copy (&objfile->objfile_obstack, &parts,
14865 sizeof (parts)));
9c6a1327 14866
5c54719c 14867 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14868}
14869
c906108c
SS
14870/* Create the vector of fields, and attach it to the type. */
14871
14872static void
fba45db2 14873dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14874 struct dwarf2_cu *cu)
c906108c 14875{
317f7127 14876 int nfields = fip->nfields ();
c906108c
SS
14877
14878 /* Record the field count, allocate space for the array of fields,
14879 and create blank accessibility bitfields if necessary. */
5e33d5f4 14880 type->set_num_fields (nfields);
3cabb6b0
SM
14881 type->set_fields
14882 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14883
3e9f1ca1 14884 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
c906108c
SS
14885 {
14886 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14887
14888 TYPE_FIELD_PRIVATE_BITS (type) =
14889 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14890 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14891
14892 TYPE_FIELD_PROTECTED_BITS (type) =
14893 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14894 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14895
774b6a14
TT
14896 TYPE_FIELD_IGNORE_BITS (type) =
14897 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14898 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14899 }
14900
14901 /* If the type has baseclasses, allocate and clear a bit vector for
14902 TYPE_FIELD_VIRTUAL_BITS. */
3e9f1ca1 14903 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
c906108c 14904 {
be2daae6 14905 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14906 unsigned char *pointer;
c906108c
SS
14907
14908 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14909 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14910 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14911 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14912 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14913 }
14914
9c6a1327
TT
14915 if (!fip->variant_parts.empty ())
14916 add_variant_property (fip, type, cu);
2ddeaf8a 14917
be2daae6
TT
14918 /* Copy the saved-up fields into the field vector. */
14919 for (int i = 0; i < nfields; ++i)
c906108c 14920 {
be2daae6
TT
14921 struct nextfield &field
14922 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14923 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14924
ceacbf6e 14925 type->field (i) = field.field;
be2daae6 14926 switch (field.accessibility)
c906108c 14927 {
c5aa993b 14928 case DW_ACCESS_private:
3e9f1ca1 14929 if (cu->per_cu->lang != language_ada)
be2daae6 14930 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14931 break;
c906108c 14932
c5aa993b 14933 case DW_ACCESS_protected:
3e9f1ca1 14934 if (cu->per_cu->lang != language_ada)
be2daae6 14935 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14936 break;
c906108c 14937
c5aa993b
JM
14938 case DW_ACCESS_public:
14939 break;
c906108c 14940
c5aa993b
JM
14941 default:
14942 /* Unknown accessibility. Complain and treat it as public. */
14943 {
b98664d3 14944 complaint (_("unsupported accessibility %d"),
be2daae6 14945 field.accessibility);
c5aa993b
JM
14946 }
14947 break;
c906108c 14948 }
be2daae6 14949 if (i < fip->baseclasses.size ())
c906108c 14950 {
be2daae6 14951 switch (field.virtuality)
c906108c 14952 {
c5aa993b
JM
14953 case DW_VIRTUALITY_virtual:
14954 case DW_VIRTUALITY_pure_virtual:
3e9f1ca1 14955 if (cu->per_cu->lang == language_ada)
a73c6dcd 14956 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14957 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14958 break;
c906108c
SS
14959 }
14960 }
c906108c
SS
14961 }
14962}
14963
7d27a96d
TT
14964/* Return true if this member function is a constructor, false
14965 otherwise. */
14966
14967static int
14968dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14969{
14970 const char *fieldname;
fe978cb0 14971 const char *type_name;
7d27a96d
TT
14972 int len;
14973
14974 if (die->parent == NULL)
14975 return 0;
14976
14977 if (die->parent->tag != DW_TAG_structure_type
14978 && die->parent->tag != DW_TAG_union_type
14979 && die->parent->tag != DW_TAG_class_type)
14980 return 0;
14981
14982 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14983 type_name = dwarf2_name (die->parent, cu);
14984 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14985 return 0;
14986
14987 len = strlen (fieldname);
fe978cb0
PA
14988 return (strncmp (fieldname, type_name, len) == 0
14989 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14990}
14991
c906108c
SS
14992/* Add a member function to the proper fieldlist. */
14993
14994static void
107d2387 14995dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14996 struct type *type, struct dwarf2_cu *cu)
c906108c 14997{
5e22e966 14998 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 14999 struct attribute *attr;
c906108c 15000 int i;
be2daae6 15001 struct fnfieldlist *flp = nullptr;
c906108c 15002 struct fn_field *fnp;
15d034d0 15003 const char *fieldname;
f792889a 15004 struct type *this_type;
c906108c 15005
3e9f1ca1 15006 if (cu->per_cu->lang == language_ada)
a73c6dcd 15007 error (_("unexpected member function in Ada type"));
b4ba55a1 15008
2df3850c 15009 /* Get name of member function. */
39cbfefa
DJ
15010 fieldname = dwarf2_name (die, cu);
15011 if (fieldname == NULL)
2df3850c 15012 return;
c906108c 15013
c906108c 15014 /* Look up member function name in fieldlist. */
be2daae6 15015 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15016 {
27bfe10e 15017 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15018 {
15019 flp = &fip->fnfieldlists[i];
15020 break;
15021 }
c906108c
SS
15022 }
15023
be2daae6
TT
15024 /* Create a new fnfieldlist if necessary. */
15025 if (flp == nullptr)
c906108c 15026 {
be2daae6
TT
15027 fip->fnfieldlists.emplace_back ();
15028 flp = &fip->fnfieldlists.back ();
c906108c 15029 flp->name = fieldname;
be2daae6 15030 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15031 }
15032
be2daae6
TT
15033 /* Create a new member function field and add it to the vector of
15034 fnfieldlists. */
15035 flp->fnfields.emplace_back ();
15036 fnp = &flp->fnfields.back ();
3da10d80
KS
15037
15038 /* Delay processing of the physname until later. */
3e9f1ca1 15039 if (cu->per_cu->lang == language_cplus)
be2daae6
TT
15040 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15041 die, cu);
3da10d80
KS
15042 else
15043 {
1d06ead6 15044 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15045 fnp->physname = physname ? physname : "";
15046 }
15047
c906108c 15048 fnp->type = alloc_type (objfile);
f792889a 15049 this_type = read_type_die (die, cu);
78134374 15050 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15051 {
1f704f76 15052 int nparams = this_type->num_fields ();
c906108c 15053
f792889a 15054 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15055 of the method itself (TYPE_CODE_METHOD). */
15056 smash_to_method_type (fnp->type, type,
f792889a 15057 TYPE_TARGET_TYPE (this_type),
80fc5e77 15058 this_type->fields (),
1f704f76 15059 this_type->num_fields (),
a409645d 15060 this_type->has_varargs ());
c906108c
SS
15061
15062 /* Handle static member functions.
dda83cd7
SM
15063 Dwarf2 has no clean way to discern C++ static and non-static
15064 member functions. G++ helps GDB by marking the first
15065 parameter for non-static member functions (which is the this
15066 pointer) as artificial. We obtain this information from
15067 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15068 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15069 fnp->voffset = VOFFSET_STATIC;
15070 }
15071 else
b98664d3 15072 complaint (_("member function type missing for '%s'"),
3da10d80 15073 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15074
15075 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15076 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15077 fnp->fcontext = die_containing_type (die, cu);
c906108c 15078
3e43a32a
MS
15079 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15080 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15081
15082 /* Get accessibility. */
bf23a268 15083 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15084 switch (accessibility)
c906108c 15085 {
60d5a603
JK
15086 case DW_ACCESS_private:
15087 fnp->is_private = 1;
15088 break;
15089 case DW_ACCESS_protected:
15090 fnp->is_protected = 1;
15091 break;
c906108c
SS
15092 }
15093
b02dede2 15094 /* Check for artificial methods. */
e142c38c 15095 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15096 if (attr && attr->as_boolean ())
b02dede2
DJ
15097 fnp->is_artificial = 1;
15098
e35000a7
TBA
15099 /* Check for defaulted methods. */
15100 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15101 if (attr != nullptr)
15102 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15103
15104 /* Check for deleted methods. */
15105 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15106 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15107 fnp->is_deleted = 1;
15108
7d27a96d
TT
15109 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15110
0d564a31 15111 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15112 function. For older versions of GCC, this is an offset in the
15113 appropriate virtual table, as specified by DW_AT_containing_type.
15114 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15115 to the object address. */
15116
e142c38c 15117 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15118 if (attr != nullptr)
8e19ed76 15119 {
9d2246fc 15120 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15121 {
9d2246fc
TT
15122 struct dwarf_block *block = attr->as_block ();
15123
15124 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15125 {
15126 /* Old-style GCC. */
9d2246fc 15127 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15128 }
9d2246fc
TT
15129 else if (block->data[0] == DW_OP_deref
15130 || (block->size > 1
15131 && block->data[0] == DW_OP_deref_size
15132 && block->data[1] == cu->header.addr_size))
aec5aa8b 15133 {
9d2246fc 15134 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15135 if ((fnp->voffset % cu->header.addr_size) != 0)
15136 dwarf2_complex_location_expr_complaint ();
15137 else
15138 fnp->voffset /= cu->header.addr_size;
15139 fnp->voffset += 2;
15140 }
15141 else
15142 dwarf2_complex_location_expr_complaint ();
15143
15144 if (!fnp->fcontext)
7e993ebf
KS
15145 {
15146 /* If there is no `this' field and no DW_AT_containing_type,
15147 we cannot actually find a base class context for the
15148 vtable! */
1f704f76 15149 if (this_type->num_fields () == 0
7e993ebf
KS
15150 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15151 {
b98664d3 15152 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15153 "function \"%s\" (offset %s)"),
15154 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15155 }
15156 else
15157 {
15158 fnp->fcontext
940da03e 15159 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15160 }
15161 }
aec5aa8b 15162 }
cd6c91b4 15163 else if (attr->form_is_section_offset ())
dda83cd7 15164 {
4d3c2250 15165 dwarf2_complex_location_expr_complaint ();
dda83cd7 15166 }
8e19ed76 15167 else
dda83cd7 15168 {
4d3c2250
KB
15169 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15170 fieldname);
dda83cd7 15171 }
0d564a31 15172 }
d48cc9dd
DJ
15173 else
15174 {
15175 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15176 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15177 {
15178 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15179 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15180 "but the vtable offset is not specified"),
9d8780f0 15181 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15182 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15183 TYPE_CPLUS_DYNAMIC (type) = 1;
15184 }
15185 }
c906108c
SS
15186}
15187
15188/* Create the vector of member function fields, and attach it to the type. */
15189
15190static void
fba45db2 15191dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15192 struct dwarf2_cu *cu)
c906108c 15193{
3e9f1ca1 15194 if (cu->per_cu->lang == language_ada)
a73c6dcd 15195 error (_("unexpected member functions in Ada type"));
b4ba55a1 15196
c906108c
SS
15197 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15198 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15199 TYPE_ALLOC (type,
15200 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15201
be2daae6 15202 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15203 {
be2daae6 15204 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15205 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15206
be2daae6
TT
15207 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15208 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15209 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15210 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15211
15212 for (int k = 0; k < nf.fnfields.size (); ++k)
15213 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15214 }
15215
be2daae6 15216 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15217}
15218
1168df01
JB
15219/* Returns non-zero if NAME is the name of a vtable member in CU's
15220 language, zero otherwise. */
15221static int
15222is_vtable_name (const char *name, struct dwarf2_cu *cu)
15223{
15224 static const char vptr[] = "_vptr";
15225
9c37b5ae
TT
15226 /* Look for the C++ form of the vtable. */
15227 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15228 return 1;
15229
15230 return 0;
15231}
15232
c0dd20ea 15233/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15234 functions, with the ABI-specified layout. If TYPE describes
15235 such a structure, smash it into a member function type.
61049d3b
DJ
15236
15237 GCC shouldn't do this; it should just output pointer to member DIEs.
15238 This is GCC PR debug/28767. */
c0dd20ea 15239
0b92b5bb
TT
15240static void
15241quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15242{
09e2d7c7 15243 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15244
15245 /* Check for a structure with no name and two children. */
1f704f76 15246 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15247 return;
c0dd20ea
DJ
15248
15249 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15250 if (TYPE_FIELD_NAME (type, 0) == NULL
15251 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15252 || TYPE_FIELD_NAME (type, 1) == NULL
15253 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15254 return;
c0dd20ea
DJ
15255
15256 /* Find the type of the method. */
940da03e 15257 pfn_type = type->field (0).type ();
c0dd20ea 15258 if (pfn_type == NULL
78134374
SM
15259 || pfn_type->code () != TYPE_CODE_PTR
15260 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15261 return;
c0dd20ea
DJ
15262
15263 /* Look for the "this" argument. */
15264 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15265 if (pfn_type->num_fields () == 0
940da03e
SM
15266 /* || pfn_type->field (0).type () == NULL */
15267 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15268 return;
c0dd20ea 15269
940da03e 15270 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15271 new_type = alloc_type (objfile);
09e2d7c7 15272 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15273 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15274 pfn_type->has_varargs ());
0b92b5bb 15275 smash_to_methodptr_type (type, new_type);
c0dd20ea 15276}
1168df01 15277
e26624c6
TT
15278/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15279 requires rewriting, then copy it and return the updated copy.
15280 Otherwise return nullptr. */
15281
15282static struct type *
15283rewrite_array_type (struct type *type)
15284{
15285 if (type->code () != TYPE_CODE_ARRAY)
15286 return nullptr;
15287
15288 struct type *index_type = type->index_type ();
15289 range_bounds *current_bounds = index_type->bounds ();
15290
15291 /* Handle multi-dimensional arrays. */
15292 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15293 if (new_target == nullptr)
15294 {
15295 /* Maybe we don't need to rewrite this array. */
15296 if (current_bounds->low.kind () == PROP_CONST
15297 && current_bounds->high.kind () == PROP_CONST)
15298 return nullptr;
15299 }
15300
15301 /* Either the target type was rewritten, or the bounds have to be
15302 updated. Either way we want to copy the type and update
15303 everything. */
15304 struct type *copy = copy_type (type);
15305 int nfields = copy->num_fields ();
15306 field *new_fields
15307 = ((struct field *) TYPE_ZALLOC (copy,
15308 nfields * sizeof (struct field)));
15309 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15310 copy->set_fields (new_fields);
15311 if (new_target != nullptr)
15312 TYPE_TARGET_TYPE (copy) = new_target;
15313
15314 struct type *index_copy = copy_type (index_type);
15315 range_bounds *bounds
15316 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15317 sizeof (range_bounds));
15318 *bounds = *current_bounds;
15319 bounds->low.set_const_val (1);
15320 bounds->high.set_const_val (0);
15321 index_copy->set_bounds (bounds);
15322 copy->set_index_type (index_copy);
15323
15324 return copy;
15325}
15326
57567375
TT
15327/* While some versions of GCC will generate complicated DWARF for an
15328 array (see quirk_ada_thick_pointer), more recent versions were
15329 modified to emit an explicit thick pointer structure. However, in
15330 this case, the array still has DWARF expressions for its ranges,
15331 and these must be ignored. */
15332
15333static void
15334quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15335 struct type *type)
15336{
3e9f1ca1 15337 gdb_assert (cu->per_cu->lang == language_ada);
57567375
TT
15338
15339 /* Check for a structure with two children. */
15340 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15341 return;
15342
15343 /* Check for P_ARRAY and P_BOUNDS members. */
15344 if (TYPE_FIELD_NAME (type, 0) == NULL
15345 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15346 || TYPE_FIELD_NAME (type, 1) == NULL
15347 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15348 return;
15349
15350 /* Make sure we're looking at a pointer to an array. */
15351 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15352 return;
57567375 15353
e26624c6
TT
15354 /* The Ada code already knows how to handle these types, so all that
15355 we need to do is turn the bounds into static bounds. However, we
15356 don't want to rewrite existing array or index types in-place,
15357 because those may be referenced in other contexts where this
15358 rewriting is undesirable. */
15359 struct type *new_ary_type
15360 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15361 if (new_ary_type != nullptr)
15362 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15363}
15364
2b4424c3
TT
15365/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15366 appropriate error checking and issuing complaints if there is a
15367 problem. */
15368
15369static ULONGEST
15370get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15371{
15372 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15373
15374 if (attr == nullptr)
15375 return 0;
15376
cd6c91b4 15377 if (!attr->form_is_constant ())
2b4424c3 15378 {
b98664d3 15379 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15380 " - DIE at %s [in module %s]"),
15381 sect_offset_str (die->sect_off),
5e22e966 15382 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15383 return 0;
15384 }
15385
529908cb
TT
15386 LONGEST val = attr->constant_value (0);
15387 if (val < 0)
2b4424c3 15388 {
529908cb
TT
15389 complaint (_("DW_AT_alignment value must not be negative"
15390 " - DIE at %s [in module %s]"),
15391 sect_offset_str (die->sect_off),
15392 objfile_name (cu->per_objfile->objfile));
15393 return 0;
2b4424c3 15394 }
529908cb 15395 ULONGEST align = val;
2b4424c3
TT
15396
15397 if (align == 0)
15398 {
b98664d3 15399 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15400 " - DIE at %s [in module %s]"),
15401 sect_offset_str (die->sect_off),
5e22e966 15402 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15403 return 0;
15404 }
15405 if ((align & (align - 1)) != 0)
15406 {
b98664d3 15407 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15408 " - DIE at %s [in module %s]"),
15409 sect_offset_str (die->sect_off),
5e22e966 15410 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15411 return 0;
15412 }
15413
15414 return align;
15415}
15416
15417/* If the DIE has a DW_AT_alignment attribute, use its value to set
15418 the alignment for TYPE. */
15419
15420static void
15421maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15422 struct type *type)
15423{
15424 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15425 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15426 " - DIE at %s [in module %s]"),
15427 sect_offset_str (die->sect_off),
5e22e966 15428 objfile_name (cu->per_objfile->objfile));
2b4424c3 15429}
685b1105 15430
e35000a7
TBA
15431/* Check if the given VALUE is a valid enum dwarf_calling_convention
15432 constant for a type, according to DWARF5 spec, Table 5.5. */
15433
15434static bool
15435is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15436{
15437 switch (value)
15438 {
15439 case DW_CC_normal:
15440 case DW_CC_pass_by_reference:
15441 case DW_CC_pass_by_value:
15442 return true;
15443
15444 default:
15445 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15446 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15447 return false;
15448 }
15449}
15450
d0922fcf
TBA
15451/* Check if the given VALUE is a valid enum dwarf_calling_convention
15452 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15453 also according to GNU-specific values (see include/dwarf2.h). */
15454
15455static bool
15456is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15457{
15458 switch (value)
15459 {
15460 case DW_CC_normal:
15461 case DW_CC_program:
15462 case DW_CC_nocall:
15463 return true;
15464
15465 case DW_CC_GNU_renesas_sh:
15466 case DW_CC_GNU_borland_fastcall_i386:
15467 case DW_CC_GDB_IBM_OpenCL:
15468 return true;
15469
15470 default:
15471 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15472 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15473 return false;
15474 }
15475}
15476
c906108c 15477/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15478 (definition) to create a type for the structure or union. Fill in
15479 the type's name and general properties; the members will not be
83655187
DE
15480 processed until process_structure_scope. A symbol table entry for
15481 the type will also not be done until process_structure_scope (assuming
15482 the type has a name).
c906108c 15483
c767944b
DJ
15484 NOTE: we need to call these functions regardless of whether or not the
15485 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15486 structure or union. This gets the type entered into our set of
83655187 15487 user defined types. */
c906108c 15488
f792889a 15489static struct type *
134d01f1 15490read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15491{
5e22e966 15492 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15493 struct type *type;
15494 struct attribute *attr;
15d034d0 15495 const char *name;
c906108c 15496
348e048f
DE
15497 /* If the definition of this type lives in .debug_types, read that type.
15498 Don't follow DW_AT_specification though, that will take us back up
15499 the chain and we want to go down. */
052c8bb8 15500 attr = die->attr (DW_AT_signature);
435d3d88 15501 if (attr != nullptr)
348e048f 15502 {
ac9ec31b 15503 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15504
ac9ec31b 15505 /* The type's CU may not be the same as CU.
02142a6c 15506 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15507 return set_die_type (die, type, cu);
15508 }
15509
c0dd20ea 15510 type = alloc_type (objfile);
c906108c 15511 INIT_CPLUS_SPECIFIC (type);
93311388 15512
39cbfefa
DJ
15513 name = dwarf2_name (die, cu);
15514 if (name != NULL)
c906108c 15515 {
3e9f1ca1
TT
15516 if (cu->per_cu->lang == language_cplus
15517 || cu->per_cu->lang == language_d
15518 || cu->per_cu->lang == language_rust)
63d06c5c 15519 {
15d034d0 15520 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15521
15522 /* dwarf2_full_name might have already finished building the DIE's
15523 type. If so, there is no need to continue. */
15524 if (get_die_type (die, cu) != NULL)
15525 return get_die_type (die, cu);
15526
d0e39ea2 15527 type->set_name (full_name);
63d06c5c
DC
15528 }
15529 else
15530 {
d8151005
DJ
15531 /* The name is already allocated along with this objfile, so
15532 we don't need to duplicate it for the type. */
d0e39ea2 15533 type->set_name (name);
63d06c5c 15534 }
c906108c
SS
15535 }
15536
15537 if (die->tag == DW_TAG_structure_type)
15538 {
67607e24 15539 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15540 }
15541 else if (die->tag == DW_TAG_union_type)
15542 {
67607e24 15543 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15544 }
15545 else
15546 {
67607e24 15547 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15548 }
15549
3e9f1ca1 15550 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
aa70e35c 15551 type->set_is_declared_class (true);
0cc2414c 15552
e35000a7
TBA
15553 /* Store the calling convention in the type if it's available in
15554 the die. Otherwise the calling convention remains set to
15555 the default value DW_CC_normal. */
15556 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15557 if (attr != nullptr
529908cb 15558 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
15559 {
15560 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15561 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 15562 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
15563 }
15564
e142c38c 15565 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15566 if (attr != nullptr)
c906108c 15567 {
cd6c91b4 15568 if (attr->form_is_constant ())
dda83cd7 15569 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
15570 else
15571 {
f8e89861 15572 struct dynamic_prop prop;
293e7e51 15573 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15574 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 15575 TYPE_LENGTH (type) = 0;
155bfbd3 15576 }
c906108c
SS
15577 }
15578 else
15579 {
15580 TYPE_LENGTH (type) = 0;
15581 }
15582
2b4424c3
TT
15583 maybe_set_alignment (cu, die, type);
15584
5230b05a 15585 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15586 {
5230b05a
WT
15587 /* ICC<14 does not output the required DW_AT_declaration on
15588 incomplete types, but gives them a size of zero. */
b4b73759 15589 type->set_is_stub (true);
685b1105
JK
15590 }
15591 else
9baccff6 15592 type->set_stub_is_supported (true);
685b1105 15593
dc718098 15594 if (die_is_declaration (die, cu))
b4b73759 15595 type->set_is_stub (true);
a6c727b2
DJ
15596 else if (attr == NULL && die->child == NULL
15597 && producer_is_realview (cu->producer))
15598 /* RealView does not output the required DW_AT_declaration
15599 on incomplete types. */
b4b73759 15600 type->set_is_stub (true);
dc718098 15601
c906108c
SS
15602 /* We need to add the type field to the die immediately so we don't
15603 infinitely recurse when dealing with pointers to the structure
0963b4bd 15604 type within the structure itself. */
1c379e20 15605 set_die_type (die, type, cu);
c906108c 15606
7e314c57
JK
15607 /* set_die_type should be already done. */
15608 set_descriptive_type (type, die, cu);
15609
c767944b
DJ
15610 return type;
15611}
15612
9c6a1327
TT
15613static void handle_struct_member_die
15614 (struct die_info *child_die,
15615 struct type *type,
15616 struct field_info *fi,
15617 std::vector<struct symbol *> *template_args,
15618 struct dwarf2_cu *cu);
15619
15620/* A helper for handle_struct_member_die that handles
15621 DW_TAG_variant_part. */
15622
15623static void
15624handle_variant_part (struct die_info *die, struct type *type,
15625 struct field_info *fi,
15626 std::vector<struct symbol *> *template_args,
15627 struct dwarf2_cu *cu)
15628{
15629 variant_part_builder *new_part;
15630 if (fi->current_variant_part == nullptr)
15631 {
15632 fi->variant_parts.emplace_back ();
15633 new_part = &fi->variant_parts.back ();
15634 }
15635 else if (!fi->current_variant_part->processing_variant)
15636 {
15637 complaint (_("nested DW_TAG_variant_part seen "
15638 "- DIE at %s [in module %s]"),
15639 sect_offset_str (die->sect_off),
5e22e966 15640 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15641 return;
15642 }
15643 else
15644 {
15645 variant_field &current = fi->current_variant_part->variants.back ();
15646 current.variant_parts.emplace_back ();
15647 new_part = &current.variant_parts.back ();
15648 }
15649
15650 /* When we recurse, we want callees to add to this new variant
15651 part. */
15652 scoped_restore save_current_variant_part
15653 = make_scoped_restore (&fi->current_variant_part, new_part);
15654
15655 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15656 if (discr == NULL)
15657 {
15658 /* It's a univariant form, an extension we support. */
15659 }
15660 else if (discr->form_is_ref ())
15661 {
15662 struct dwarf2_cu *target_cu = cu;
15663 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15664
15665 new_part->discriminant_offset = target_die->sect_off;
15666 }
15667 else
15668 {
15669 complaint (_("DW_AT_discr does not have DIE reference form"
15670 " - DIE at %s [in module %s]"),
15671 sect_offset_str (die->sect_off),
5e22e966 15672 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15673 }
15674
15675 for (die_info *child_die = die->child;
15676 child_die != NULL;
15677 child_die = child_die->sibling)
15678 handle_struct_member_die (child_die, type, fi, template_args, cu);
15679}
15680
15681/* A helper for handle_struct_member_die that handles
15682 DW_TAG_variant. */
15683
15684static void
15685handle_variant (struct die_info *die, struct type *type,
15686 struct field_info *fi,
15687 std::vector<struct symbol *> *template_args,
15688 struct dwarf2_cu *cu)
15689{
15690 if (fi->current_variant_part == nullptr)
15691 {
15692 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15693 "- DIE at %s [in module %s]"),
15694 sect_offset_str (die->sect_off),
5e22e966 15695 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15696 return;
15697 }
15698 if (fi->current_variant_part->processing_variant)
15699 {
15700 complaint (_("nested DW_TAG_variant seen "
15701 "- DIE at %s [in module %s]"),
15702 sect_offset_str (die->sect_off),
5e22e966 15703 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15704 return;
15705 }
15706
15707 scoped_restore save_processing_variant
15708 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15709 true);
15710
15711 fi->current_variant_part->variants.emplace_back ();
15712 variant_field &variant = fi->current_variant_part->variants.back ();
15713 variant.first_field = fi->fields.size ();
15714
15715 /* In a variant we want to get the discriminant and also add a
15716 field for our sole member child. */
15717 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 15718 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
15719 {
15720 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 15721 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
15722 variant.default_branch = true;
15723 else
9d2246fc 15724 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
15725 }
15726 else
cae21f8e 15727 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
15728
15729 for (die_info *variant_child = die->child;
15730 variant_child != NULL;
15731 variant_child = variant_child->sibling)
15732 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15733
15734 variant.last_field = fi->fields.size ();
15735}
15736
2ddeaf8a
TT
15737/* A helper for process_structure_scope that handles a single member
15738 DIE. */
15739
15740static void
15741handle_struct_member_die (struct die_info *child_die, struct type *type,
15742 struct field_info *fi,
15743 std::vector<struct symbol *> *template_args,
15744 struct dwarf2_cu *cu)
15745{
15746 if (child_die->tag == DW_TAG_member
9c6a1327 15747 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15748 {
15749 /* NOTE: carlton/2002-11-05: A C++ static data member
15750 should be a DW_TAG_member that is a declaration, but
15751 all versions of G++ as of this writing (so through at
15752 least 3.2.1) incorrectly generate DW_TAG_variable
15753 tags for them instead. */
15754 dwarf2_add_field (fi, child_die, cu);
15755 }
15756 else if (child_die->tag == DW_TAG_subprogram)
15757 {
15758 /* Rust doesn't have member functions in the C++ sense.
15759 However, it does emit ordinary functions as children
15760 of a struct DIE. */
3e9f1ca1 15761 if (cu->per_cu->lang == language_rust)
2ddeaf8a
TT
15762 read_func_scope (child_die, cu);
15763 else
15764 {
15765 /* C++ member function. */
15766 dwarf2_add_member_fn (fi, child_die, type, cu);
15767 }
15768 }
15769 else if (child_die->tag == DW_TAG_inheritance)
15770 {
15771 /* C++ base class field. */
15772 dwarf2_add_field (fi, child_die, cu);
15773 }
15774 else if (type_can_define_types (child_die))
15775 dwarf2_add_type_defn (fi, child_die, cu);
15776 else if (child_die->tag == DW_TAG_template_type_param
15777 || child_die->tag == DW_TAG_template_value_param)
15778 {
15779 struct symbol *arg = new_symbol (child_die, NULL, cu);
15780
15781 if (arg != NULL)
15782 template_args->push_back (arg);
15783 }
9c6a1327
TT
15784 else if (child_die->tag == DW_TAG_variant_part)
15785 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15786 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15787 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15788}
15789
c767944b
DJ
15790/* Finish creating a structure or union type, including filling in
15791 its members and creating a symbol for it. */
15792
15793static void
15794process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15795{
5e22e966 15796 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15797 struct die_info *child_die;
c767944b
DJ
15798 struct type *type;
15799
15800 type = get_die_type (die, cu);
15801 if (type == NULL)
15802 type = read_structure_type (die, cu);
15803
3e1d3d8c 15804 bool has_template_parameters = false;
e142c38c 15805 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15806 {
15807 struct field_info fi;
2f4732b0 15808 std::vector<struct symbol *> template_args;
c906108c 15809
639d11d3 15810 child_die = die->child;
c906108c
SS
15811
15812 while (child_die && child_die->tag)
15813 {
2ddeaf8a 15814 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15815 child_die = child_die->sibling;
c906108c
SS
15816 }
15817
34eaf542 15818 /* Attach template arguments to type. */
2f4732b0 15819 if (!template_args.empty ())
34eaf542 15820 {
3e1d3d8c 15821 has_template_parameters = true;
34eaf542 15822 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15823 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15824 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15825 = XOBNEWVEC (&objfile->objfile_obstack,
15826 struct symbol *,
15827 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15828 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15829 template_args.data (),
34eaf542
TT
15830 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15831 * sizeof (struct symbol *)));
34eaf542
TT
15832 }
15833
c906108c 15834 /* Attach fields and member functions to the type. */
317f7127 15835 if (fi.nfields () > 0)
e7c27a73 15836 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15837 if (!fi.fnfieldlists.empty ())
c906108c 15838 {
e7c27a73 15839 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15840
c5aa993b 15841 /* Get the type which refers to the base class (possibly this
c906108c 15842 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15843 class from the DW_AT_containing_type attribute. This use of
15844 DW_AT_containing_type is a GNU extension. */
c906108c 15845
e142c38c 15846 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15847 {
e7c27a73 15848 struct type *t = die_containing_type (die, cu);
c906108c 15849
ae6ae975 15850 set_type_vptr_basetype (type, t);
c906108c
SS
15851 if (type == t)
15852 {
c906108c
SS
15853 int i;
15854
15855 /* Our own class provides vtbl ptr. */
1f704f76 15856 for (i = t->num_fields () - 1;
c906108c
SS
15857 i >= TYPE_N_BASECLASSES (t);
15858 --i)
15859 {
0d5cff50 15860 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15861
dda83cd7 15862 if (is_vtable_name (fieldname, cu))
c906108c 15863 {
ae6ae975 15864 set_type_vptr_fieldno (type, i);
c906108c
SS
15865 break;
15866 }
15867 }
15868
15869 /* Complain if virtual function table field not found. */
15870 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15871 complaint (_("virtual function table pointer "
3e43a32a 15872 "not found when defining class '%s'"),
7d93a1e0 15873 type->name () ? type->name () : "");
c906108c
SS
15874 }
15875 else
15876 {
ae6ae975 15877 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15878 }
15879 }
f6235d4c 15880 else if (cu->producer
61012eef 15881 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15882 {
15883 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
15884 of the containing type, but the vtable pointer is
15885 always named __vfp. */
f6235d4c
EZ
15886
15887 int i;
15888
1f704f76 15889 for (i = type->num_fields () - 1;
f6235d4c
EZ
15890 i >= TYPE_N_BASECLASSES (type);
15891 --i)
15892 {
15893 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15894 {
ae6ae975
DE
15895 set_type_vptr_fieldno (type, i);
15896 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15897 break;
15898 }
15899 }
15900 }
c906108c 15901 }
98751a41
JK
15902
15903 /* Copy fi.typedef_field_list linked list elements content into the
15904 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15905 if (!fi.typedef_field_list.empty ())
98751a41 15906 {
be2daae6 15907 int count = fi.typedef_field_list.size ();
98751a41 15908
a0d7a4ff 15909 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15910 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15911 = ((struct decl_field *)
be2daae6
TT
15912 TYPE_ALLOC (type,
15913 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15914 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15915
be2daae6
TT
15916 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15917 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15918 }
c767944b 15919
883fd55a
KS
15920 /* Copy fi.nested_types_list linked list elements content into the
15921 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
3e9f1ca1
TT
15922 if (!fi.nested_types_list.empty ()
15923 && cu->per_cu->lang != language_ada)
883fd55a 15924 {
be2daae6 15925 int count = fi.nested_types_list.size ();
883fd55a
KS
15926
15927 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15928 TYPE_NESTED_TYPES_ARRAY (type)
15929 = ((struct decl_field *)
be2daae6
TT
15930 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15931 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15932
be2daae6
TT
15933 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15934 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15935 }
c906108c 15936 }
63d06c5c 15937
bb5ed363 15938 quirk_gcc_member_function_pointer (type, objfile);
3e9f1ca1 15939 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
c9317f21 15940 cu->rust_unions.push_back (type);
3e9f1ca1 15941 else if (cu->per_cu->lang == language_ada)
57567375 15942 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 15943
90aeadfc
DC
15944 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15945 snapshots) has been known to create a die giving a declaration
15946 for a class that has, as a child, a die giving a definition for a
15947 nested class. So we have to process our children even if the
15948 current die is a declaration. Normally, of course, a declaration
15949 won't have any children at all. */
134d01f1 15950
ca040673
DE
15951 child_die = die->child;
15952
90aeadfc
DC
15953 while (child_die != NULL && child_die->tag)
15954 {
15955 if (child_die->tag == DW_TAG_member
15956 || child_die->tag == DW_TAG_variable
34eaf542
TT
15957 || child_die->tag == DW_TAG_inheritance
15958 || child_die->tag == DW_TAG_template_value_param
15959 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15960 {
90aeadfc 15961 /* Do nothing. */
134d01f1 15962 }
90aeadfc
DC
15963 else
15964 process_die (child_die, cu);
134d01f1 15965
436c571c 15966 child_die = child_die->sibling;
134d01f1
DJ
15967 }
15968
fa4028e9
JB
15969 /* Do not consider external references. According to the DWARF standard,
15970 these DIEs are identified by the fact that they have no byte_size
15971 attribute, and a declaration attribute. */
15972 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15973 || !die_is_declaration (die, cu)
15974 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15975 {
15976 struct symbol *sym = new_symbol (die, type, cu);
15977
15978 if (has_template_parameters)
15979 {
a776957c
TT
15980 struct symtab *symtab;
15981 if (sym != nullptr)
15982 symtab = symbol_symtab (sym);
15983 else if (cu->line_header != nullptr)
15984 {
15985 /* Any related symtab will do. */
15986 symtab
7ba99d21 15987 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15988 }
15989 else
15990 {
15991 symtab = nullptr;
15992 complaint (_("could not find suitable "
15993 "symtab for template parameter"
15994 " - DIE at %s [in module %s]"),
15995 sect_offset_str (die->sect_off),
15996 objfile_name (objfile));
15997 }
15998
15999 if (symtab != nullptr)
16000 {
16001 /* Make sure that the symtab is set on the new symbols.
16002 Even though they don't appear in this symtab directly,
16003 other parts of gdb assume that symbols do, and this is
16004 reasonably true. */
16005 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16006 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16007 }
3e1d3d8c
TT
16008 }
16009 }
134d01f1
DJ
16010}
16011
ed6acedd
TT
16012/* Assuming DIE is an enumeration type, and TYPE is its associated
16013 type, update TYPE using some information only available in DIE's
16014 children. In particular, the fields are computed. */
55426c9d
JB
16015
16016static void
16017update_enumeration_type_from_children (struct die_info *die,
16018 struct type *type,
16019 struct dwarf2_cu *cu)
16020{
60f7655a 16021 struct die_info *child_die;
55426c9d
JB
16022 int unsigned_enum = 1;
16023 int flag_enum = 1;
55426c9d 16024
8268c778 16025 auto_obstack obstack;
ed6acedd 16026 std::vector<struct field> fields;
55426c9d 16027
60f7655a
DE
16028 for (child_die = die->child;
16029 child_die != NULL && child_die->tag;
436c571c 16030 child_die = child_die->sibling)
55426c9d
JB
16031 {
16032 struct attribute *attr;
16033 LONGEST value;
16034 const gdb_byte *bytes;
16035 struct dwarf2_locexpr_baton *baton;
16036 const char *name;
60f7655a 16037
55426c9d
JB
16038 if (child_die->tag != DW_TAG_enumerator)
16039 continue;
16040
16041 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16042 if (attr == NULL)
16043 continue;
16044
16045 name = dwarf2_name (child_die, cu);
16046 if (name == NULL)
16047 name = "<anonymous enumerator>";
16048
16049 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16050 &value, &bytes, &baton);
16051 if (value < 0)
16052 {
16053 unsigned_enum = 0;
16054 flag_enum = 0;
16055 }
55426c9d 16056 else
edd45eb0
SM
16057 {
16058 if (count_one_bits_ll (value) >= 2)
16059 flag_enum = 0;
edd45eb0 16060 }
55426c9d 16061
ed6acedd
TT
16062 fields.emplace_back ();
16063 struct field &field = fields.back ();
16064 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16065 SET_FIELD_ENUMVAL (field, value);
16066 }
16067
16068 if (!fields.empty ())
16069 {
5e33d5f4 16070 type->set_num_fields (fields.size ());
3cabb6b0
SM
16071 type->set_fields
16072 ((struct field *)
16073 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16074 memcpy (type->fields (), fields.data (),
ed6acedd 16075 sizeof (struct field) * fields.size ());
55426c9d
JB
16076 }
16077
16078 if (unsigned_enum)
653223d3
SM
16079 type->set_is_unsigned (true);
16080
55426c9d 16081 if (flag_enum)
9902b327 16082 type->set_is_flag_enum (true);
55426c9d
JB
16083}
16084
134d01f1
DJ
16085/* Given a DW_AT_enumeration_type die, set its type. We do not
16086 complete the type's fields yet, or create any symbols. */
c906108c 16087
f792889a 16088static struct type *
134d01f1 16089read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16090{
5e22e966 16091 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16092 struct type *type;
c906108c 16093 struct attribute *attr;
0114d602 16094 const char *name;
134d01f1 16095
348e048f
DE
16096 /* If the definition of this type lives in .debug_types, read that type.
16097 Don't follow DW_AT_specification though, that will take us back up
16098 the chain and we want to go down. */
052c8bb8 16099 attr = die->attr (DW_AT_signature);
435d3d88 16100 if (attr != nullptr)
348e048f 16101 {
ac9ec31b 16102 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16103
ac9ec31b 16104 /* The type's CU may not be the same as CU.
02142a6c 16105 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16106 return set_die_type (die, type, cu);
16107 }
16108
c906108c
SS
16109 type = alloc_type (objfile);
16110
67607e24 16111 type->set_code (TYPE_CODE_ENUM);
94af9270 16112 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16113 if (name != NULL)
d0e39ea2 16114 type->set_name (name);
c906108c 16115
0626fc76
TT
16116 attr = dwarf2_attr (die, DW_AT_type, cu);
16117 if (attr != NULL)
16118 {
16119 struct type *underlying_type = die_type (die, cu);
16120
16121 TYPE_TARGET_TYPE (type) = underlying_type;
16122 }
16123
e142c38c 16124 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16125 if (attr != nullptr)
c906108c 16126 {
529908cb 16127 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16128 }
16129 else
16130 {
16131 TYPE_LENGTH (type) = 0;
16132 }
16133
2b4424c3
TT
16134 maybe_set_alignment (cu, die, type);
16135
137033e9
JB
16136 /* The enumeration DIE can be incomplete. In Ada, any type can be
16137 declared as private in the package spec, and then defined only
16138 inside the package body. Such types are known as Taft Amendment
16139 Types. When another package uses such a type, an incomplete DIE
16140 may be generated by the compiler. */
02eb380e 16141 if (die_is_declaration (die, cu))
b4b73759 16142 type->set_is_stub (true);
02eb380e 16143
0626fc76
TT
16144 /* If this type has an underlying type that is not a stub, then we
16145 may use its attributes. We always use the "unsigned" attribute
16146 in this situation, because ordinarily we guess whether the type
16147 is unsigned -- but the guess can be wrong and the underlying type
16148 can tell us the reality. However, we defer to a local size
16149 attribute if one exists, because this lets the compiler override
16150 the underlying type if needed. */
e46d3488 16151 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16152 {
9e7c9a03
HD
16153 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16154 underlying_type = check_typedef (underlying_type);
653223d3
SM
16155
16156 type->set_is_unsigned (underlying_type->is_unsigned ());
16157
0626fc76 16158 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16159 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16160
2b4424c3 16161 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16162 && TYPE_RAW_ALIGN (underlying_type) != 0)
16163 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16164 }
16165
aa70e35c 16166 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
3d567982 16167
ed6acedd
TT
16168 set_die_type (die, type, cu);
16169
16170 /* Finish the creation of this type by using the enum's children.
16171 Note that, as usual, this must come after set_die_type to avoid
16172 infinite recursion when trying to compute the names of the
16173 enumerators. */
16174 update_enumeration_type_from_children (die, type, cu);
16175
16176 return type;
134d01f1
DJ
16177}
16178
16179/* Given a pointer to a die which begins an enumeration, process all
16180 the dies that define the members of the enumeration, and create the
16181 symbol for the enumeration type.
16182
16183 NOTE: We reverse the order of the element list. */
16184
16185static void
16186process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16187{
f792889a 16188 struct type *this_type;
134d01f1 16189
f792889a
DJ
16190 this_type = get_die_type (die, cu);
16191 if (this_type == NULL)
16192 this_type = read_enumeration_type (die, cu);
9dc481d3 16193
639d11d3 16194 if (die->child != NULL)
c906108c 16195 {
9dc481d3 16196 struct die_info *child_die;
15d034d0 16197 const char *name;
9dc481d3 16198
639d11d3 16199 child_die = die->child;
c906108c
SS
16200 while (child_die && child_die->tag)
16201 {
16202 if (child_die->tag != DW_TAG_enumerator)
16203 {
e7c27a73 16204 process_die (child_die, cu);
c906108c
SS
16205 }
16206 else
16207 {
39cbfefa
DJ
16208 name = dwarf2_name (child_die, cu);
16209 if (name)
ed6acedd 16210 new_symbol (child_die, this_type, cu);
c906108c
SS
16211 }
16212
436c571c 16213 child_die = child_die->sibling;
c906108c 16214 }
c906108c 16215 }
134d01f1 16216
6c83ed52
TT
16217 /* If we are reading an enum from a .debug_types unit, and the enum
16218 is a declaration, and the enum is not the signatured type in the
16219 unit, then we do not want to add a symbol for it. Adding a
16220 symbol would in some cases obscure the true definition of the
16221 enum, giving users an incomplete type when the definition is
16222 actually available. Note that we do not want to do this for all
16223 enums which are just declarations, because C++0x allows forward
16224 enum declarations. */
3019eac3 16225 if (cu->per_cu->is_debug_types
6c83ed52
TT
16226 && die_is_declaration (die, cu))
16227 {
52dc124a 16228 struct signatured_type *sig_type;
6c83ed52 16229
c0f78cd4 16230 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16231 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16232 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16233 return;
16234 }
16235
f792889a 16236 new_symbol (die, this_type, cu);
c906108c
SS
16237}
16238
57567375
TT
16239/* Helper function for quirk_ada_thick_pointer that examines a bounds
16240 expression for an index type and finds the corresponding field
16241 offset in the hidden "P_BOUNDS" structure. Returns true on success
16242 and updates *FIELD, false if it fails to recognize an
16243 expression. */
16244
16245static bool
16246recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16247 int *bounds_offset, struct field *field,
16248 struct dwarf2_cu *cu)
16249{
16250 struct attribute *attr = dwarf2_attr (die, name, cu);
16251 if (attr == nullptr || !attr->form_is_block ())
16252 return false;
16253
16254 const struct dwarf_block *block = attr->as_block ();
16255 const gdb_byte *start = block->data;
16256 const gdb_byte *end = block->data + block->size;
16257
16258 /* The expression to recognize generally looks like:
16259
16260 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16261 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16262
16263 However, the second "plus_uconst" may be missing:
16264
16265 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16266 DW_OP_deref_size: 4)
16267
16268 This happens when the field is at the start of the structure.
16269
16270 Also, the final deref may not be sized:
16271
16272 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16273 DW_OP_deref)
16274
16275 This happens when the size of the index type happens to be the
16276 same as the architecture's word size. This can occur with or
16277 without the second plus_uconst. */
16278
16279 if (end - start < 2)
16280 return false;
16281 if (*start++ != DW_OP_push_object_address)
16282 return false;
16283 if (*start++ != DW_OP_plus_uconst)
16284 return false;
16285
16286 uint64_t this_bound_off;
16287 start = gdb_read_uleb128 (start, end, &this_bound_off);
16288 if (start == nullptr || (int) this_bound_off != this_bound_off)
16289 return false;
16290 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16291 is consistent among all bounds. */
16292 if (*bounds_offset == -1)
16293 *bounds_offset = this_bound_off;
16294 else if (*bounds_offset != this_bound_off)
16295 return false;
16296
16297 if (start == end || *start++ != DW_OP_deref)
16298 return false;
16299
16300 int offset = 0;
16301 if (start ==end)
16302 return false;
16303 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16304 {
16305 /* This means an offset of 0. */
16306 }
16307 else if (*start++ != DW_OP_plus_uconst)
16308 return false;
16309 else
16310 {
16311 /* The size is the parameter to DW_OP_plus_uconst. */
16312 uint64_t val;
16313 start = gdb_read_uleb128 (start, end, &val);
16314 if (start == nullptr)
16315 return false;
16316 if ((int) val != val)
16317 return false;
16318 offset = val;
16319 }
16320
16321 if (start == end)
16322 return false;
16323
16324 uint64_t size;
16325 if (*start == DW_OP_deref_size)
16326 {
16327 start = gdb_read_uleb128 (start + 1, end, &size);
16328 if (start == nullptr)
16329 return false;
16330 }
16331 else if (*start == DW_OP_deref)
16332 {
16333 size = cu->header.addr_size;
16334 ++start;
16335 }
16336 else
16337 return false;
16338
16339 SET_FIELD_BITPOS (*field, 8 * offset);
16340 if (size != TYPE_LENGTH (field->type ()))
16341 FIELD_BITSIZE (*field) = 8 * size;
16342
16343 return true;
16344}
16345
16346/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16347 some kinds of Ada arrays:
16348
16349 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16350 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16351 <11e0> DW_AT_data_location: 2 byte block: 97 6
16352 (DW_OP_push_object_address; DW_OP_deref)
16353 <11e3> DW_AT_type : <0x1173>
16354 <11e7> DW_AT_sibling : <0x1201>
16355 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16356 <11ec> DW_AT_type : <0x1206>
16357 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16358 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16359 DW_OP_deref_size: 4)
16360 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16361 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16362 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16363
16364 This actually represents a "thick pointer", which is a structure
16365 with two elements: one that is a pointer to the array data, and one
16366 that is a pointer to another structure; this second structure holds
16367 the array bounds.
16368
16369 This returns a new type on success, or nullptr if this didn't
16370 recognize the type. */
16371
16372static struct type *
16373quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16374 struct type *type)
16375{
16376 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16377 /* So far we've only seen this with block form. */
16378 if (attr == nullptr || !attr->form_is_block ())
16379 return nullptr;
16380
16381 /* Note that this will fail if the structure layout is changed by
16382 the compiler. However, we have no good way to recognize some
16383 other layout, because we don't know what expression the compiler
16384 might choose to emit should this happen. */
16385 struct dwarf_block *blk = attr->as_block ();
16386 if (blk->size != 2
16387 || blk->data[0] != DW_OP_push_object_address
16388 || blk->data[1] != DW_OP_deref)
16389 return nullptr;
16390
16391 int bounds_offset = -1;
16392 int max_align = -1;
16393 std::vector<struct field> range_fields;
16394 for (struct die_info *child_die = die->child;
16395 child_die;
16396 child_die = child_die->sibling)
16397 {
16398 if (child_die->tag == DW_TAG_subrange_type)
16399 {
16400 struct type *underlying = read_subrange_index_type (child_die, cu);
16401
16402 int this_align = type_align (underlying);
16403 if (this_align > max_align)
16404 max_align = this_align;
16405
16406 range_fields.emplace_back ();
16407 range_fields.emplace_back ();
16408
16409 struct field &lower = range_fields[range_fields.size () - 2];
16410 struct field &upper = range_fields[range_fields.size () - 1];
16411
16412 lower.set_type (underlying);
16413 FIELD_ARTIFICIAL (lower) = 1;
16414
16415 upper.set_type (underlying);
16416 FIELD_ARTIFICIAL (upper) = 1;
16417
16418 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16419 &bounds_offset, &lower, cu)
16420 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16421 &bounds_offset, &upper, cu))
16422 return nullptr;
16423 }
16424 }
16425
16426 /* This shouldn't really happen, but double-check that we found
16427 where the bounds are stored. */
16428 if (bounds_offset == -1)
16429 return nullptr;
16430
16431 struct objfile *objfile = cu->per_objfile->objfile;
16432 for (int i = 0; i < range_fields.size (); i += 2)
16433 {
16434 char name[20];
16435
16436 /* Set the name of each field in the bounds. */
16437 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16438 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16439 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16440 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16441 }
16442
16443 struct type *bounds = alloc_type (objfile);
16444 bounds->set_code (TYPE_CODE_STRUCT);
16445
16446 bounds->set_num_fields (range_fields.size ());
16447 bounds->set_fields
16448 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16449 * sizeof (struct field))));
16450 memcpy (bounds->fields (), range_fields.data (),
16451 bounds->num_fields () * sizeof (struct field));
16452
16453 int last_fieldno = range_fields.size () - 1;
16454 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16455 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16456 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16457
16458 /* Rewrite the existing array type in place. Specifically, we
16459 remove any dynamic properties we might have read, and we replace
16460 the index types. */
16461 struct type *iter = type;
16462 for (int i = 0; i < range_fields.size (); i += 2)
16463 {
16464 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16465 iter->main_type->dyn_prop_list = nullptr;
16466 iter->set_index_type
16467 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16468 iter = TYPE_TARGET_TYPE (iter);
16469 }
16470
16471 struct type *result = alloc_type (objfile);
16472 result->set_code (TYPE_CODE_STRUCT);
16473
16474 result->set_num_fields (2);
16475 result->set_fields
16476 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16477 * sizeof (struct field))));
16478
16479 /* The names are chosen to coincide with what the compiler does with
16480 -fgnat-encodings=all, which the Ada code in gdb already
16481 understands. */
16482 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16483 result->field (0).set_type (lookup_pointer_type (type));
16484
16485 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16486 result->field (1).set_type (lookup_pointer_type (bounds));
16487 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16488
16489 result->set_name (type->name ());
16490 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16491 + TYPE_LENGTH (result->field (1).type ()));
16492
16493 return result;
16494}
16495
c906108c
SS
16496/* Extract all information from a DW_TAG_array_type DIE and put it in
16497 the DIE's type field. For now, this only handles one dimensional
16498 arrays. */
16499
f792889a 16500static struct type *
e7c27a73 16501read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16502{
5e22e966 16503 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16504 struct die_info *child_die;
7e314c57 16505 struct type *type;
c906108c 16506 struct type *element_type, *range_type, *index_type;
c906108c 16507 struct attribute *attr;
15d034d0 16508 const char *name;
a405673c 16509 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16510 unsigned int bit_stride = 0;
c906108c 16511
e7c27a73 16512 element_type = die_type (die, cu);
c906108c 16513
7e314c57
JK
16514 /* The die_type call above may have already set the type for this DIE. */
16515 type = get_die_type (die, cu);
16516 if (type)
16517 return type;
16518
dc53a7ad
JB
16519 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16520 if (attr != NULL)
a405673c
JB
16521 {
16522 int stride_ok;
293e7e51 16523 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16524
16525 byte_stride_prop
16526 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16527 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16528 prop_type);
a405673c
JB
16529 if (!stride_ok)
16530 {
b98664d3 16531 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16532 " - DIE at %s [in module %s]"),
16533 sect_offset_str (die->sect_off),
5e22e966 16534 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16535 /* Ignore this attribute. We will likely not be able to print
16536 arrays of this type correctly, but there is little we can do
16537 to help if we cannot read the attribute's value. */
16538 byte_stride_prop = NULL;
16539 }
16540 }
dc53a7ad
JB
16541
16542 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16543 if (attr != NULL)
529908cb 16544 bit_stride = attr->constant_value (0);
dc53a7ad 16545
c906108c
SS
16546 /* Irix 6.2 native cc creates array types without children for
16547 arrays with unspecified length. */
639d11d3 16548 if (die->child == NULL)
c906108c 16549 {
46bf5051 16550 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16551 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16552 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16553 byte_stride_prop, bit_stride);
f792889a 16554 return set_die_type (die, type, cu);
c906108c
SS
16555 }
16556
791afaa2 16557 std::vector<struct type *> range_types;
639d11d3 16558 child_die = die->child;
c906108c
SS
16559 while (child_die && child_die->tag)
16560 {
16561 if (child_die->tag == DW_TAG_subrange_type)
16562 {
f792889a 16563 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16564
dda83cd7
SM
16565 if (child_type != NULL)
16566 {
0963b4bd 16567 /* The range type was succesfully read. Save it for the
dda83cd7 16568 array type creation. */
791afaa2 16569 range_types.push_back (child_type);
dda83cd7 16570 }
c906108c 16571 }
436c571c 16572 child_die = child_die->sibling;
c906108c
SS
16573 }
16574
cf2b2075
TV
16575 if (range_types.empty ())
16576 {
16577 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16578 sect_offset_str (die->sect_off),
16579 objfile_name (cu->per_objfile->objfile));
16580 return NULL;
16581 }
16582
c906108c
SS
16583 /* Dwarf2 dimensions are output from left to right, create the
16584 necessary array types in backwards order. */
7ca2d3a3 16585
c906108c 16586 type = element_type;
7ca2d3a3
DL
16587
16588 if (read_array_order (die, cu) == DW_ORD_col_major)
16589 {
16590 int i = 0;
9a619af0 16591
791afaa2 16592 while (i < range_types.size ())
10f6a3ad
TT
16593 {
16594 type = create_array_type_with_stride (NULL, type, range_types[i++],
16595 byte_stride_prop, bit_stride);
16596 bit_stride = 0;
16597 byte_stride_prop = nullptr;
16598 }
7ca2d3a3
DL
16599 }
16600 else
16601 {
791afaa2 16602 size_t ndim = range_types.size ();
7ca2d3a3 16603 while (ndim-- > 0)
10f6a3ad
TT
16604 {
16605 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16606 byte_stride_prop, bit_stride);
16607 bit_stride = 0;
16608 byte_stride_prop = nullptr;
16609 }
7ca2d3a3 16610 }
c906108c 16611
cf2b2075
TV
16612 gdb_assert (type != element_type);
16613
f5f8a009
EZ
16614 /* Understand Dwarf2 support for vector types (like they occur on
16615 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16616 array type. This is not part of the Dwarf2/3 standard yet, but a
16617 custom vendor extension. The main difference between a regular
16618 array and the vector variant is that vectors are passed by value
16619 to functions. */
e142c38c 16620 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16621 if (attr != nullptr)
ea37ba09 16622 make_vector_type (type);
f5f8a009 16623
dbc98a8b
KW
16624 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16625 implementation may choose to implement triple vectors using this
16626 attribute. */
16627 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 16628 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 16629 {
529908cb
TT
16630 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16631 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 16632 else
b98664d3 16633 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16634 "than the total size of elements"));
dbc98a8b
KW
16635 }
16636
39cbfefa
DJ
16637 name = dwarf2_name (die, cu);
16638 if (name)
d0e39ea2 16639 type->set_name (name);
6e70227d 16640
2b4424c3
TT
16641 maybe_set_alignment (cu, die, type);
16642
57567375 16643 struct type *replacement_type = nullptr;
3e9f1ca1 16644 if (cu->per_cu->lang == language_ada)
57567375
TT
16645 {
16646 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16647 if (replacement_type != nullptr)
16648 type = replacement_type;
16649 }
16650
0963b4bd 16651 /* Install the type in the die. */
57567375 16652 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
16653
16654 /* set_die_type should be already done. */
b4ba55a1
JB
16655 set_descriptive_type (type, die, cu);
16656
7e314c57 16657 return type;
c906108c
SS
16658}
16659
7ca2d3a3 16660static enum dwarf_array_dim_ordering
6e70227d 16661read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16662{
16663 struct attribute *attr;
16664
16665 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16666
435d3d88 16667 if (attr != nullptr)
1bc397c5
TT
16668 {
16669 LONGEST val = attr->constant_value (-1);
16670 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16671 return (enum dwarf_array_dim_ordering) val;
16672 }
7ca2d3a3 16673
0963b4bd
MS
16674 /* GNU F77 is a special case, as at 08/2004 array type info is the
16675 opposite order to the dwarf2 specification, but data is still
16676 laid out as per normal fortran.
7ca2d3a3 16677
0963b4bd
MS
16678 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16679 version checking. */
7ca2d3a3 16680
3e9f1ca1 16681 if (cu->per_cu->lang == language_fortran
905e0470 16682 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16683 {
16684 return DW_ORD_row_major;
16685 }
16686
3a3440fb 16687 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
16688 {
16689 case array_column_major:
16690 return DW_ORD_col_major;
16691 case array_row_major:
16692 default:
16693 return DW_ORD_row_major;
16694 };
16695}
16696
72019c9c 16697/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16698 the DIE's type field. */
72019c9c 16699
f792889a 16700static struct type *
72019c9c
GM
16701read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16702{
7e314c57
JK
16703 struct type *domain_type, *set_type;
16704 struct attribute *attr;
f792889a 16705
7e314c57
JK
16706 domain_type = die_type (die, cu);
16707
16708 /* The die_type call above may have already set the type for this DIE. */
16709 set_type = get_die_type (die, cu);
16710 if (set_type)
16711 return set_type;
16712
16713 set_type = create_set_type (NULL, domain_type);
16714
16715 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
16716 if (attr != nullptr && attr->form_is_unsigned ())
16717 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 16718
2b4424c3
TT
16719 maybe_set_alignment (cu, die, set_type);
16720
f792889a 16721 return set_die_type (die, set_type, cu);
72019c9c 16722}
7ca2d3a3 16723
0971de02
TT
16724/* A helper for read_common_block that creates a locexpr baton.
16725 SYM is the symbol which we are marking as computed.
16726 COMMON_DIE is the DIE for the common block.
16727 COMMON_LOC is the location expression attribute for the common
16728 block itself.
16729 MEMBER_LOC is the location expression attribute for the particular
16730 member of the common block that we are processing.
16731 CU is the CU from which the above come. */
16732
16733static void
16734mark_common_block_symbol_computed (struct symbol *sym,
16735 struct die_info *common_die,
16736 struct attribute *common_loc,
16737 struct attribute *member_loc,
16738 struct dwarf2_cu *cu)
16739{
5e22e966 16740 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16741 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16742 struct dwarf2_locexpr_baton *baton;
16743 gdb_byte *ptr;
16744 unsigned int cu_off;
08feed99 16745 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16746 LONGEST offset = 0;
16747
16748 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16749 gdb_assert (common_loc->form_is_block ());
16750 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16751 || member_loc->form_is_constant ());
0971de02 16752
8d749320 16753 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16754 baton->per_objfile = per_objfile;
0971de02
TT
16755 baton->per_cu = cu->per_cu;
16756 gdb_assert (baton->per_cu);
16757
16758 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16759
cd6c91b4 16760 if (member_loc->form_is_constant ())
0971de02 16761 {
0826b30a 16762 offset = member_loc->constant_value (0);
0971de02
TT
16763 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16764 }
16765 else
9d2246fc 16766 baton->size += member_loc->as_block ()->size;
0971de02 16767
224c3ddb 16768 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16769 baton->data = ptr;
16770
16771 *ptr++ = DW_OP_call4;
9c541725 16772 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16773 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16774 ptr += 4;
16775
cd6c91b4 16776 if (member_loc->form_is_constant ())
0971de02
TT
16777 {
16778 *ptr++ = DW_OP_addr;
16779 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16780 ptr += cu->header.addr_size;
16781 }
16782 else
16783 {
16784 /* We have to copy the data here, because DW_OP_call4 will only
16785 use a DW_AT_location attribute. */
9d2246fc
TT
16786 struct dwarf_block *block = member_loc->as_block ();
16787 memcpy (ptr, block->data, block->size);
16788 ptr += block->size;
0971de02
TT
16789 }
16790
16791 *ptr++ = DW_OP_plus;
16792 gdb_assert (ptr - baton->data == baton->size);
16793
0971de02 16794 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16795 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16796}
16797
4357ac6c
TT
16798/* Create appropriate locally-scoped variables for all the
16799 DW_TAG_common_block entries. Also create a struct common_block
16800 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16801 is used to separate the common blocks name namespace from regular
4357ac6c 16802 variable names. */
c906108c
SS
16803
16804static void
e7c27a73 16805read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16806{
0971de02
TT
16807 struct attribute *attr;
16808
16809 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16810 if (attr != nullptr)
0971de02
TT
16811 {
16812 /* Support the .debug_loc offsets. */
4fc6c0d5 16813 if (attr->form_is_block ())
dda83cd7 16814 {
0971de02 16815 /* Ok. */
dda83cd7 16816 }
cd6c91b4 16817 else if (attr->form_is_section_offset ())
dda83cd7 16818 {
0971de02
TT
16819 dwarf2_complex_location_expr_complaint ();
16820 attr = NULL;
dda83cd7 16821 }
0971de02 16822 else
dda83cd7 16823 {
0971de02
TT
16824 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16825 "common block member");
16826 attr = NULL;
dda83cd7 16827 }
0971de02
TT
16828 }
16829
639d11d3 16830 if (die->child != NULL)
c906108c 16831 {
5e22e966 16832 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16833 struct die_info *child_die;
16834 size_t n_entries = 0, size;
16835 struct common_block *common_block;
16836 struct symbol *sym;
74ac6d43 16837
4357ac6c
TT
16838 for (child_die = die->child;
16839 child_die && child_die->tag;
436c571c 16840 child_die = child_die->sibling)
4357ac6c
TT
16841 ++n_entries;
16842
16843 size = (sizeof (struct common_block)
16844 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16845 common_block
16846 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16847 size);
4357ac6c
TT
16848 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16849 common_block->n_entries = 0;
16850
16851 for (child_die = die->child;
16852 child_die && child_die->tag;
436c571c 16853 child_die = child_die->sibling)
4357ac6c
TT
16854 {
16855 /* Create the symbol in the DW_TAG_common_block block in the current
16856 symbol scope. */
e7c27a73 16857 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16858 if (sym != NULL)
16859 {
16860 struct attribute *member_loc;
16861
16862 common_block->contents[common_block->n_entries++] = sym;
16863
16864 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16865 cu);
16866 if (member_loc)
16867 {
16868 /* GDB has handled this for a long time, but it is
16869 not specified by DWARF. It seems to have been
16870 emitted by gfortran at least as recently as:
16871 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16872 complaint (_("Variable in common block has "
0971de02 16873 "DW_AT_data_member_location "
9d8780f0
SM
16874 "- DIE at %s [in module %s]"),
16875 sect_offset_str (child_die->sect_off),
518817b3 16876 objfile_name (objfile));
0971de02 16877
cd6c91b4 16878 if (member_loc->form_is_section_offset ())
0971de02 16879 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16880 else if (member_loc->form_is_constant ()
4fc6c0d5 16881 || member_loc->form_is_block ())
0971de02 16882 {
435d3d88 16883 if (attr != nullptr)
0971de02
TT
16884 mark_common_block_symbol_computed (sym, die, attr,
16885 member_loc, cu);
16886 }
16887 else
16888 dwarf2_complex_location_expr_complaint ();
16889 }
16890 }
c906108c 16891 }
4357ac6c
TT
16892
16893 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16894 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16895 }
16896}
16897
0114d602 16898/* Create a type for a C++ namespace. */
d9fa45fe 16899
0114d602
DJ
16900static struct type *
16901read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16902{
5e22e966 16903 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16904 const char *previous_prefix, *name;
9219021c 16905 int is_anonymous;
0114d602
DJ
16906 struct type *type;
16907
16908 /* For extensions, reuse the type of the original namespace. */
16909 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16910 {
16911 struct die_info *ext_die;
16912 struct dwarf2_cu *ext_cu = cu;
9a619af0 16913
0114d602
DJ
16914 ext_die = dwarf2_extension (die, &ext_cu);
16915 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16916
16917 /* EXT_CU may not be the same as CU.
02142a6c 16918 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16919 return set_die_type (die, type, cu);
16920 }
9219021c 16921
e142c38c 16922 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16923
16924 /* Now build the name of the current namespace. */
16925
0114d602
DJ
16926 previous_prefix = determine_prefix (die, cu);
16927 if (previous_prefix[0] != '\0')
16928 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16929 previous_prefix, name, 0, cu);
0114d602
DJ
16930
16931 /* Create the type. */
19f392bc 16932 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16933
60531b24 16934 return set_die_type (die, type, cu);
0114d602
DJ
16935}
16936
22cee43f 16937/* Read a namespace scope. */
0114d602
DJ
16938
16939static void
16940read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16941{
5e22e966 16942 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16943 int is_anonymous;
9219021c 16944
5c4e30ca
DC
16945 /* Add a symbol associated to this if we haven't seen the namespace
16946 before. Also, add a using directive if it's an anonymous
16947 namespace. */
9219021c 16948
f2f0e013 16949 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16950 {
16951 struct type *type;
16952
0114d602 16953 type = read_type_die (die, cu);
e7c27a73 16954 new_symbol (die, type, cu);
5c4e30ca 16955
e8e80198 16956 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16957 if (is_anonymous)
0114d602
DJ
16958 {
16959 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16960
eb1e02fd 16961 std::vector<const char *> excludes;
804d2729 16962 add_using_directive (using_directives (cu),
7d93a1e0 16963 previous_prefix, type->name (), NULL,
eb1e02fd 16964 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16965 }
5c4e30ca 16966 }
9219021c 16967
639d11d3 16968 if (die->child != NULL)
d9fa45fe 16969 {
639d11d3 16970 struct die_info *child_die = die->child;
6e70227d 16971
d9fa45fe
DC
16972 while (child_die && child_die->tag)
16973 {
e7c27a73 16974 process_die (child_die, cu);
436c571c 16975 child_die = child_die->sibling;
d9fa45fe
DC
16976 }
16977 }
38d518c9
EZ
16978}
16979
f55ee35c
JK
16980/* Read a Fortran module as type. This DIE can be only a declaration used for
16981 imported module. Still we need that type as local Fortran "use ... only"
16982 declaration imports depend on the created type in determine_prefix. */
16983
16984static struct type *
16985read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16986{
5e22e966 16987 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16988 const char *module_name;
f55ee35c
JK
16989 struct type *type;
16990
16991 module_name = dwarf2_name (die, cu);
19f392bc 16992 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16993
f55ee35c
JK
16994 return set_die_type (die, type, cu);
16995}
16996
5d7cb8df
JK
16997/* Read a Fortran module. */
16998
16999static void
17000read_module (struct die_info *die, struct dwarf2_cu *cu)
17001{
17002 struct die_info *child_die = die->child;
530e8392
KB
17003 struct type *type;
17004
17005 type = read_type_die (die, cu);
17006 new_symbol (die, type, cu);
5d7cb8df 17007
5d7cb8df
JK
17008 while (child_die && child_die->tag)
17009 {
17010 process_die (child_die, cu);
436c571c 17011 child_die = child_die->sibling;
5d7cb8df
JK
17012 }
17013}
17014
38d518c9
EZ
17015/* Return the name of the namespace represented by DIE. Set
17016 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17017 namespace. */
17018
17019static const char *
e142c38c 17020namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17021{
17022 struct die_info *current_die;
17023 const char *name = NULL;
17024
17025 /* Loop through the extensions until we find a name. */
17026
17027 for (current_die = die;
17028 current_die != NULL;
f2f0e013 17029 current_die = dwarf2_extension (die, &cu))
38d518c9 17030 {
96553a0c
DE
17031 /* We don't use dwarf2_name here so that we can detect the absence
17032 of a name -> anonymous namespace. */
7d45c7c3 17033 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17034
38d518c9
EZ
17035 if (name != NULL)
17036 break;
17037 }
17038
17039 /* Is it an anonymous namespace? */
17040
17041 *is_anonymous = (name == NULL);
17042 if (*is_anonymous)
2b1dbab0 17043 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17044
17045 return name;
d9fa45fe
DC
17046}
17047
c906108c
SS
17048/* Extract all information from a DW_TAG_pointer_type DIE and add to
17049 the user defined type vector. */
17050
f792889a 17051static struct type *
e7c27a73 17052read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17053{
5e22e966 17054 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17055 struct comp_unit_head *cu_header = &cu->header;
c906108c 17056 struct type *type;
8b2dbe47
KB
17057 struct attribute *attr_byte_size;
17058 struct attribute *attr_address_class;
17059 int byte_size, addr_class;
7e314c57
JK
17060 struct type *target_type;
17061
17062 target_type = die_type (die, cu);
c906108c 17063
7e314c57
JK
17064 /* The die_type call above may have already set the type for this DIE. */
17065 type = get_die_type (die, cu);
17066 if (type)
17067 return type;
17068
17069 type = lookup_pointer_type (target_type);
8b2dbe47 17070
e142c38c 17071 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17072 if (attr_byte_size)
529908cb 17073 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17074 else
8b2dbe47
KB
17075 byte_size = cu_header->addr_size;
17076
e142c38c 17077 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17078 if (attr_address_class)
529908cb 17079 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17080 else
17081 addr_class = DW_ADDR_none;
17082
2b4424c3
TT
17083 ULONGEST alignment = get_alignment (cu, die);
17084
17085 /* If the pointer size, alignment, or address class is different
17086 than the default, create a type variant marked as such and set
17087 the length accordingly. */
17088 if (TYPE_LENGTH (type) != byte_size
17089 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17090 && alignment != TYPE_RAW_ALIGN (type))
17091 || addr_class != DW_ADDR_none)
c906108c 17092 {
5e2b427d 17093 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17094 {
314ad88d
PA
17095 type_instance_flags type_flags
17096 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17097 addr_class);
876cecd0
TT
17098 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17099 == 0);
8b2dbe47
KB
17100 type = make_type_with_address_space (type, type_flags);
17101 }
17102 else if (TYPE_LENGTH (type) != byte_size)
17103 {
b98664d3 17104 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17105 }
2b4424c3
TT
17106 else if (TYPE_RAW_ALIGN (type) != alignment)
17107 {
b98664d3 17108 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17109 " - DIE at %s [in module %s]"),
17110 sect_offset_str (die->sect_off),
5e22e966 17111 objfile_name (cu->per_objfile->objfile));
2b4424c3 17112 }
6e70227d 17113 else
9a619af0
MS
17114 {
17115 /* Should we also complain about unhandled address classes? */
17116 }
c906108c 17117 }
8b2dbe47
KB
17118
17119 TYPE_LENGTH (type) = byte_size;
2b4424c3 17120 set_type_align (type, alignment);
f792889a 17121 return set_die_type (die, type, cu);
c906108c
SS
17122}
17123
17124/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17125 the user defined type vector. */
17126
f792889a 17127static struct type *
e7c27a73 17128read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17129{
17130 struct type *type;
17131 struct type *to_type;
17132 struct type *domain;
17133
e7c27a73
DJ
17134 to_type = die_type (die, cu);
17135 domain = die_containing_type (die, cu);
0d5de010 17136
7e314c57
JK
17137 /* The calls above may have already set the type for this DIE. */
17138 type = get_die_type (die, cu);
17139 if (type)
17140 return type;
17141
78134374 17142 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17143 type = lookup_methodptr_type (to_type);
78134374 17144 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17145 {
5e22e966 17146 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17147
17148 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17149 to_type->fields (), to_type->num_fields (),
a409645d 17150 to_type->has_varargs ());
7078baeb
TT
17151 type = lookup_methodptr_type (new_type);
17152 }
0d5de010
DJ
17153 else
17154 type = lookup_memberptr_type (to_type, domain);
c906108c 17155
f792889a 17156 return set_die_type (die, type, cu);
c906108c
SS
17157}
17158
4297a3f0 17159/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17160 the user defined type vector. */
17161
f792889a 17162static struct type *
4297a3f0 17163read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17164 enum type_code refcode)
c906108c 17165{
e7c27a73 17166 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17167 struct type *type, *target_type;
c906108c
SS
17168 struct attribute *attr;
17169
4297a3f0
AV
17170 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17171
7e314c57
JK
17172 target_type = die_type (die, cu);
17173
17174 /* The die_type call above may have already set the type for this DIE. */
17175 type = get_die_type (die, cu);
17176 if (type)
17177 return type;
17178
4297a3f0 17179 type = lookup_reference_type (target_type, refcode);
e142c38c 17180 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17181 if (attr != nullptr)
c906108c 17182 {
529908cb 17183 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17184 }
17185 else
17186 {
107d2387 17187 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17188 }
2b4424c3 17189 maybe_set_alignment (cu, die, type);
f792889a 17190 return set_die_type (die, type, cu);
c906108c
SS
17191}
17192
cf363f18
MW
17193/* Add the given cv-qualifiers to the element type of the array. GCC
17194 outputs DWARF type qualifiers that apply to an array, not the
17195 element type. But GDB relies on the array element type to carry
17196 the cv-qualifiers. This mimics section 6.7.3 of the C99
17197 specification. */
17198
17199static struct type *
17200add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17201 struct type *base_type, int cnst, int voltl)
17202{
17203 struct type *el_type, *inner_array;
17204
17205 base_type = copy_type (base_type);
17206 inner_array = base_type;
17207
78134374 17208 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17209 {
17210 TYPE_TARGET_TYPE (inner_array) =
17211 copy_type (TYPE_TARGET_TYPE (inner_array));
17212 inner_array = TYPE_TARGET_TYPE (inner_array);
17213 }
17214
17215 el_type = TYPE_TARGET_TYPE (inner_array);
17216 cnst |= TYPE_CONST (el_type);
17217 voltl |= TYPE_VOLATILE (el_type);
17218 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17219
17220 return set_die_type (die, base_type, cu);
17221}
17222
f792889a 17223static struct type *
e7c27a73 17224read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17225{
f792889a 17226 struct type *base_type, *cv_type;
c906108c 17227
e7c27a73 17228 base_type = die_type (die, cu);
7e314c57
JK
17229
17230 /* The die_type call above may have already set the type for this DIE. */
17231 cv_type = get_die_type (die, cu);
17232 if (cv_type)
17233 return cv_type;
17234
2f608a3a
KW
17235 /* In case the const qualifier is applied to an array type, the element type
17236 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17237 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17238 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17239
f792889a
DJ
17240 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17241 return set_die_type (die, cv_type, cu);
c906108c
SS
17242}
17243
f792889a 17244static struct type *
e7c27a73 17245read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17246{
f792889a 17247 struct type *base_type, *cv_type;
c906108c 17248
e7c27a73 17249 base_type = die_type (die, cu);
7e314c57
JK
17250
17251 /* The die_type call above may have already set the type for this DIE. */
17252 cv_type = get_die_type (die, cu);
17253 if (cv_type)
17254 return cv_type;
17255
cf363f18
MW
17256 /* In case the volatile qualifier is applied to an array type, the
17257 element type is so qualified, not the array type (section 6.7.3
17258 of C99). */
78134374 17259 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17260 return add_array_cv_type (die, cu, base_type, 0, 1);
17261
f792889a
DJ
17262 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17263 return set_die_type (die, cv_type, cu);
c906108c
SS
17264}
17265
06d66ee9
TT
17266/* Handle DW_TAG_restrict_type. */
17267
17268static struct type *
17269read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17270{
17271 struct type *base_type, *cv_type;
17272
17273 base_type = die_type (die, cu);
17274
17275 /* The die_type call above may have already set the type for this DIE. */
17276 cv_type = get_die_type (die, cu);
17277 if (cv_type)
17278 return cv_type;
17279
17280 cv_type = make_restrict_type (base_type);
17281 return set_die_type (die, cv_type, cu);
17282}
17283
a2c2acaf
MW
17284/* Handle DW_TAG_atomic_type. */
17285
17286static struct type *
17287read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17288{
17289 struct type *base_type, *cv_type;
17290
17291 base_type = die_type (die, cu);
17292
17293 /* The die_type call above may have already set the type for this DIE. */
17294 cv_type = get_die_type (die, cu);
17295 if (cv_type)
17296 return cv_type;
17297
17298 cv_type = make_atomic_type (base_type);
17299 return set_die_type (die, cv_type, cu);
17300}
17301
c906108c
SS
17302/* Extract all information from a DW_TAG_string_type DIE and add to
17303 the user defined type vector. It isn't really a user defined type,
17304 but it behaves like one, with other DIE's using an AT_user_def_type
17305 attribute to reference it. */
17306
f792889a 17307static struct type *
e7c27a73 17308read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17309{
5e22e966 17310 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17311 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17312 struct type *type, *range_type, *index_type, *char_type;
17313 struct attribute *attr;
216a7e6b
AB
17314 struct dynamic_prop prop;
17315 bool length_is_constant = true;
17316 LONGEST length;
17317
17318 /* There are a couple of places where bit sizes might be made use of
17319 when parsing a DW_TAG_string_type, however, no producer that we know
17320 of make use of these. Handling bit sizes that are a multiple of the
17321 byte size is easy enough, but what about other bit sizes? Lets deal
17322 with that problem when we have to. Warn about these attributes being
17323 unsupported, then parse the type and ignore them like we always
17324 have. */
17325 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17326 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17327 {
17328 static bool warning_printed = false;
17329 if (!warning_printed)
17330 {
17331 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17332 "currently supported on DW_TAG_string_type."));
17333 warning_printed = true;
17334 }
17335 }
c906108c 17336
e142c38c 17337 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17338 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17339 {
17340 /* The string length describes the location at which the length of
17341 the string can be found. The size of the length field can be
17342 specified with one of the attributes below. */
17343 struct type *prop_type;
17344 struct attribute *len
17345 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17346 if (len == nullptr)
17347 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17348 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17349 {
17350 /* Pass 0 as the default as we know this attribute is constant
17351 and the default value will not be returned. */
0826b30a 17352 LONGEST sz = len->constant_value (0);
293e7e51 17353 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17354 }
17355 else
17356 {
17357 /* If the size is not specified then we assume it is the size of
17358 an address on this target. */
293e7e51 17359 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17360 }
17361
17362 /* Convert the attribute into a dynamic property. */
17363 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17364 length = 1;
17365 else
17366 length_is_constant = false;
17367 }
17368 else if (attr != nullptr)
17369 {
17370 /* This DW_AT_string_length just contains the length with no
17371 indirection. There's no need to create a dynamic property in this
17372 case. Pass 0 for the default value as we know it will not be
17373 returned in this case. */
0826b30a 17374 length = attr->constant_value (0);
216a7e6b
AB
17375 }
17376 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17377 {
216a7e6b 17378 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17379 length = attr->constant_value (1);
c906108c
SS
17380 }
17381 else
17382 {
216a7e6b
AB
17383 /* Use 1 as a fallback length if we have nothing else. */
17384 length = 1;
c906108c 17385 }
6ccb9162 17386
46bf5051 17387 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17388 if (length_is_constant)
17389 range_type = create_static_range_type (NULL, index_type, 1, length);
17390 else
17391 {
17392 struct dynamic_prop low_bound;
17393
8c2e4e06 17394 low_bound.set_const_val (1);
216a7e6b
AB
17395 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17396 }
3b7538c0
UW
17397 char_type = language_string_char_type (cu->language_defn, gdbarch);
17398 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17399
f792889a 17400 return set_die_type (die, type, cu);
c906108c
SS
17401}
17402
4d804846
JB
17403/* Assuming that DIE corresponds to a function, returns nonzero
17404 if the function is prototyped. */
17405
17406static int
17407prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17408{
17409 struct attribute *attr;
17410
17411 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17412 if (attr && attr->as_boolean ())
4d804846
JB
17413 return 1;
17414
17415 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17416 is only meaningful for C, but the concept also extends to other
4d804846
JB
17417 languages that allow unprototyped functions (Eg: Objective C).
17418 For all other languages, assume that functions are always
17419 prototyped. */
3e9f1ca1
TT
17420 if (cu->per_cu->lang != language_c
17421 && cu->per_cu->lang != language_objc
17422 && cu->per_cu->lang != language_opencl)
4d804846
JB
17423 return 1;
17424
17425 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17426 prototyped and unprototyped functions; default to prototyped,
17427 since that is more common in modern code (and RealView warns
17428 about unprototyped functions). */
17429 if (producer_is_realview (cu->producer))
17430 return 1;
17431
17432 return 0;
17433}
17434
c906108c
SS
17435/* Handle DIES due to C code like:
17436
17437 struct foo
c5aa993b
JM
17438 {
17439 int (*funcp)(int a, long l);
17440 int b;
17441 };
c906108c 17442
0963b4bd 17443 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17444
f792889a 17445static struct type *
e7c27a73 17446read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17447{
5e22e966 17448 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17449 struct type *type; /* Type that this function returns. */
17450 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17451 struct attribute *attr;
17452
e7c27a73 17453 type = die_type (die, cu);
7e314c57
JK
17454
17455 /* The die_type call above may have already set the type for this DIE. */
17456 ftype = get_die_type (die, cu);
17457 if (ftype)
17458 return ftype;
17459
0c8b41f1 17460 ftype = lookup_function_type (type);
c906108c 17461
4d804846 17462 if (prototyped_function_p (die, cu))
27e69b7a 17463 ftype->set_is_prototyped (true);
c906108c 17464
c055b101
CV
17465 /* Store the calling convention in the type if it's available in
17466 the subroutine die. Otherwise set the calling convention to
17467 the default value DW_CC_normal. */
17468 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17469 if (attr != nullptr
529908cb 17470 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17471 TYPE_CALLING_CONVENTION (ftype)
529908cb 17472 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17473 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17474 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17475 else
17476 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17477
743649fd
MW
17478 /* Record whether the function returns normally to its caller or not
17479 if the DWARF producer set that information. */
17480 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17481 if (attr && attr->as_boolean ())
743649fd
MW
17482 TYPE_NO_RETURN (ftype) = 1;
17483
76c10ea2
GM
17484 /* We need to add the subroutine type to the die immediately so
17485 we don't infinitely recurse when dealing with parameters
0963b4bd 17486 declared as the same subroutine type. */
76c10ea2 17487 set_die_type (die, ftype, cu);
6e70227d 17488
639d11d3 17489 if (die->child != NULL)
c906108c 17490 {
bb5ed363 17491 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17492 struct die_info *child_die;
8072405b 17493 int nparams, iparams;
c906108c
SS
17494
17495 /* Count the number of parameters.
dda83cd7
SM
17496 FIXME: GDB currently ignores vararg functions, but knows about
17497 vararg member functions. */
8072405b 17498 nparams = 0;
639d11d3 17499 child_die = die->child;
c906108c
SS
17500 while (child_die && child_die->tag)
17501 {
17502 if (child_die->tag == DW_TAG_formal_parameter)
17503 nparams++;
17504 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17505 ftype->set_has_varargs (true);
17506
436c571c 17507 child_die = child_die->sibling;
c906108c
SS
17508 }
17509
17510 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17511 ftype->set_num_fields (nparams);
3cabb6b0
SM
17512 ftype->set_fields
17513 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17514
8072405b
JK
17515 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17516 even if we error out during the parameters reading below. */
17517 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17518 ftype->field (iparams).set_type (void_type);
8072405b
JK
17519
17520 iparams = 0;
639d11d3 17521 child_die = die->child;
c906108c
SS
17522 while (child_die && child_die->tag)
17523 {
17524 if (child_die->tag == DW_TAG_formal_parameter)
17525 {
3ce3b1ba
PA
17526 struct type *arg_type;
17527
17528 /* DWARF version 2 has no clean way to discern C++
17529 static and non-static member functions. G++ helps
17530 GDB by marking the first parameter for non-static
17531 member functions (which is the this pointer) as
17532 artificial. We pass this information to
17533 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17534
17535 DWARF version 3 added DW_AT_object_pointer, which GCC
17536 4.5 does not yet generate. */
e142c38c 17537 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17538 if (attr != nullptr)
c45bc3f8 17539 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 17540 else
9c37b5ae 17541 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17542 arg_type = die_type (child_die, cu);
17543
17544 /* RealView does not mark THIS as const, which the testsuite
17545 expects. GCC marks THIS as const in method definitions,
17546 but not in the class specifications (GCC PR 43053). */
3e9f1ca1
TT
17547 if (cu->per_cu->lang == language_cplus
17548 && !TYPE_CONST (arg_type)
3ce3b1ba
PA
17549 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17550 {
17551 int is_this = 0;
17552 struct dwarf2_cu *arg_cu = cu;
17553 const char *name = dwarf2_name (child_die, cu);
17554
17555 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17556 if (attr != nullptr)
3ce3b1ba
PA
17557 {
17558 /* If the compiler emits this, use it. */
17559 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17560 is_this = 1;
17561 }
17562 else if (name && strcmp (name, "this") == 0)
17563 /* Function definitions will have the argument names. */
17564 is_this = 1;
17565 else if (name == NULL && iparams == 0)
17566 /* Declarations may not have the names, so like
17567 elsewhere in GDB, assume an artificial first
17568 argument is "this". */
17569 is_this = 1;
17570
17571 if (is_this)
17572 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17573 arg_type, 0);
17574 }
17575
5d14b6e5 17576 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17577 iparams++;
17578 }
436c571c 17579 child_die = child_die->sibling;
c906108c
SS
17580 }
17581 }
17582
76c10ea2 17583 return ftype;
c906108c
SS
17584}
17585
f792889a 17586static struct type *
e7c27a73 17587read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17588{
5e22e966 17589 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17590 const char *name = NULL;
3c8e0968 17591 struct type *this_type, *target_type;
c906108c 17592
94af9270 17593 name = dwarf2_full_name (NULL, die, cu);
19f392bc 17594 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 17595 this_type->set_target_is_stub (true);
f792889a 17596 set_die_type (die, this_type, cu);
3c8e0968
DE
17597 target_type = die_type (die, cu);
17598 if (target_type != this_type)
17599 TYPE_TARGET_TYPE (this_type) = target_type;
17600 else
17601 {
17602 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17603 spec and cause infinite loops in GDB. */
b98664d3 17604 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17605 "- DIE at %s [in module %s]"),
17606 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17607 TYPE_TARGET_TYPE (this_type) = NULL;
17608 }
e4003a34
TV
17609 if (name == NULL)
17610 {
17611 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17612 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17613 Handle these by just returning the target type, rather than
17614 constructing an anonymous typedef type and trying to handle this
17615 elsewhere. */
17616 set_die_type (die, target_type, cu);
17617 return target_type;
17618 }
f792889a 17619 return this_type;
c906108c
SS
17620}
17621
a625a8c9
TT
17622/* Helper for get_dwarf2_rational_constant that computes the value of
17623 a given gmp_mpz given an attribute. */
17624
17625static void
17626get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17627{
17628 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17629 location expression that pushes an implicit value. */
17630 if (attr->form == DW_FORM_exprloc)
17631 {
17632 dwarf_block *blk = attr->as_block ();
17633 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17634 {
17635 uint64_t len;
17636 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17637 blk->data + blk->size,
17638 &len);
17639 if (ptr - blk->data + len <= blk->size)
17640 {
17641 mpz_import (value->val, len,
17642 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17643 1, 0, 0, ptr);
17644 return;
17645 }
17646 }
17647
17648 /* On failure set it to 1. */
17649 *value = gdb_mpz (1);
17650 }
17651 else if (attr->form_is_block ())
17652 {
17653 dwarf_block *blk = attr->as_block ();
17654 mpz_import (value->val, blk->size,
17655 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17656 1, 0, 0, blk->data);
17657 }
17658 else
17659 *value = gdb_mpz (attr->constant_value (1));
17660}
17661
09584414
JB
17662/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17663 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17664
17665 If the numerator and/or numerator attribute is missing,
17666 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17667 untouched. */
17668
17669static void
17670get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 17671 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
17672{
17673 struct attribute *num_attr, *denom_attr;
17674
17675 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17676 if (num_attr == nullptr)
17677 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17678 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17679
17680 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17681 if (denom_attr == nullptr)
17682 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17683 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17684
17685 if (num_attr == nullptr || denom_attr == nullptr)
17686 return;
17687
a625a8c9
TT
17688 get_mpz (cu, numerator, num_attr);
17689 get_mpz (cu, denominator, denom_attr);
09584414
JB
17690}
17691
17692/* Same as get_dwarf2_rational_constant, but extracting an unsigned
17693 rational constant, rather than a signed one.
17694
17695 If the rational constant has a negative value, a complaint
17696 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17697
17698static void
17699get_dwarf2_unsigned_rational_constant (struct die_info *die,
17700 struct dwarf2_cu *cu,
5cde1d82
TT
17701 gdb_mpz *numerator,
17702 gdb_mpz *denominator)
09584414 17703{
5cde1d82
TT
17704 gdb_mpz num (1);
17705 gdb_mpz denom (1);
09584414
JB
17706
17707 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 17708 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 17709 {
5cde1d82
TT
17710 mpz_neg (num.val, num.val);
17711 mpz_neg (denom.val, denom.val);
09584414 17712 }
5cde1d82 17713 else if (mpz_sgn (num.val) == -1)
09584414
JB
17714 {
17715 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17716 " in DIE at %s"),
17717 sect_offset_str (die->sect_off));
17718 return;
17719 }
5cde1d82 17720 else if (mpz_sgn (denom.val) == -1)
09584414
JB
17721 {
17722 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17723 " in DIE at %s"),
17724 sect_offset_str (die->sect_off));
17725 return;
17726 }
17727
5cde1d82
TT
17728 *numerator = std::move (num);
17729 *denominator = std::move (denom);
09584414
JB
17730}
17731
bbcdf9ab
TT
17732/* Assuming that ENCODING is a string whose contents starting at the
17733 K'th character is "_nn" where "nn" is a decimal number, scan that
17734 number and set RESULT to the value. K is updated to point to the
17735 character immediately following the number.
17736
17737 If the string does not conform to the format described above, false
17738 is returned, and K may or may not be changed. */
17739
17740static bool
17741ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17742{
17743 /* The next character should be an underscore ('_') followed
17744 by a digit. */
17745 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17746 return false;
17747
17748 /* Skip the underscore. */
17749 k++;
17750 int start = k;
17751
17752 /* Determine the number of digits for our number. */
17753 while (isdigit (encoding[k]))
17754 k++;
17755 if (k == start)
17756 return false;
17757
17758 std::string copy (&encoding[start], k - start);
17759 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17760 return false;
17761
17762 return true;
17763}
17764
17765/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17766 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17767 DENOM, update OFFSET, and return true on success. Return false on
17768 failure. */
17769
17770static bool
17771ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17772 gdb_mpz *num, gdb_mpz *denom)
17773{
17774 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17775 return false;
17776 return ada_get_gnat_encoded_number (encoding, offset, denom);
17777}
17778
09584414 17779/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
17780 of the corresponding TYPE by setting its type-specific data. CU is
17781 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17782 encodings. It is nullptr if the GNAT encoding should be
17783 ignored. */
09584414
JB
17784
17785static void
bbcdf9ab
TT
17786finish_fixed_point_type (struct type *type, const char *suffix,
17787 struct die_info *die, struct dwarf2_cu *cu)
09584414 17788{
09584414
JB
17789 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17790 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17791
bbcdf9ab
TT
17792 /* If GNAT encodings are preferred, don't examine the
17793 attributes. */
17794 struct attribute *attr = nullptr;
17795 if (suffix == nullptr)
17796 {
17797 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17798 if (attr == nullptr)
17799 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17800 if (attr == nullptr)
17801 attr = dwarf2_attr (die, DW_AT_small, cu);
17802 }
09584414 17803
5cde1d82
TT
17804 /* Numerator and denominator of our fixed-point type's scaling factor.
17805 The default is a scaling factor of 1, which we use as a fallback
17806 when we are not able to decode it (problem with the debugging info,
17807 unsupported forms, bug in GDB, etc...). Using that as the default
17808 allows us to at least print the unscaled value, which might still
17809 be useful to a user. */
17810 gdb_mpz scale_num (1);
17811 gdb_mpz scale_denom (1);
17812
09584414
JB
17813 if (attr == nullptr)
17814 {
bbcdf9ab
TT
17815 int offset = 0;
17816 if (suffix != nullptr
17817 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17818 &scale_denom)
17819 /* The number might be encoded as _nn_dd_nn_dd, where the
17820 second ratio is the 'small value. In this situation, we
17821 want the second value. */
17822 && (suffix[offset] != '_'
17823 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17824 &scale_denom)))
17825 {
17826 /* Found it. */
17827 }
17828 else
17829 {
17830 /* Scaling factor not found. Assume a scaling factor of 1,
17831 and hope for the best. At least the user will be able to
17832 see the encoded value. */
17833 scale_num = 1;
17834 scale_denom = 1;
17835 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17836 sect_offset_str (die->sect_off));
17837 }
09584414
JB
17838 }
17839 else if (attr->name == DW_AT_binary_scale)
17840 {
17841 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 17842 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 17843
5cde1d82 17844 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
17845 }
17846 else if (attr->name == DW_AT_decimal_scale)
17847 {
17848 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 17849 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 17850
5cde1d82 17851 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
17852 }
17853 else if (attr->name == DW_AT_small)
17854 {
17855 struct die_info *scale_die;
17856 struct dwarf2_cu *scale_cu = cu;
17857
17858 scale_die = follow_die_ref (die, attr, &scale_cu);
17859 if (scale_die->tag == DW_TAG_constant)
17860 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17861 &scale_num, &scale_denom);
17862 else
17863 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17864 " (DIE at %s)"),
17865 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17866 }
17867 else
17868 {
17869 complaint (_("unsupported scale attribute %s for fixed-point type"
17870 " (DIE at %s)"),
17871 dwarf_attr_name (attr->name),
17872 sect_offset_str (die->sect_off));
17873 }
17874
2a12c336 17875 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
17876 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17877 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
17878 mpq_canonicalize (scaling_factor.val);
17879}
17880
bbcdf9ab
TT
17881/* The gnat-encoding suffix for fixed point. */
17882
17883#define GNAT_FIXED_POINT_SUFFIX "___XF_"
17884
17885/* If NAME encodes an Ada fixed-point type, return a pointer to the
17886 "XF" suffix of the name. The text after this is what encodes the
17887 'small and 'delta information. Otherwise, return nullptr. */
17888
17889static const char *
17890gnat_encoded_fixed_point_type_info (const char *name)
17891{
17892 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17893}
17894
9b790ce7
UW
17895/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17896 (which may be different from NAME) to the architecture back-end to allow
17897 it to guess the correct format if necessary. */
17898
17899static struct type *
17900dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17901 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17902{
08feed99 17903 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17904 const struct floatformat **format;
17905 struct type *type;
17906
17907 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17908 if (format)
103a685e 17909 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17910 else
77b7c781 17911 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17912
17913 return type;
17914}
17915
eb77c9df
AB
17916/* Allocate an integer type of size BITS and name NAME. */
17917
17918static struct type *
17919dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17920 int bits, int unsigned_p, const char *name)
17921{
17922 struct type *type;
17923
17924 /* Versions of Intel's C Compiler generate an integer type called "void"
17925 instead of using DW_TAG_unspecified_type. This has been seen on
17926 at least versions 14, 17, and 18. */
35ee2dc2
AB
17927 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17928 && strcmp (name, "void") == 0)
eb77c9df
AB
17929 type = objfile_type (objfile)->builtin_void;
17930 else
17931 type = init_integer_type (objfile, bits, unsigned_p, name);
17932
17933 return type;
17934}
17935
09584414
JB
17936/* Return true if DIE has a DW_AT_small attribute whose value is
17937 a constant rational, where both the numerator and denominator
17938 are equal to zero.
17939
17940 CU is the DIE's Compilation Unit. */
17941
17942static bool
17943has_zero_over_zero_small_attribute (struct die_info *die,
17944 struct dwarf2_cu *cu)
17945{
17946 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17947 if (attr == nullptr)
17948 return false;
17949
17950 struct dwarf2_cu *scale_cu = cu;
17951 struct die_info *scale_die
17952 = follow_die_ref (die, attr, &scale_cu);
17953
17954 if (scale_die->tag != DW_TAG_constant)
17955 return false;
17956
5cde1d82 17957 gdb_mpz num (1), denom (1);
09584414 17958 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 17959 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
17960}
17961
8bdc1658
AB
17962/* Initialise and return a floating point type of size BITS suitable for
17963 use as a component of a complex number. The NAME_HINT is passed through
17964 when initialising the floating point type and is the name of the complex
17965 type.
17966
17967 As DWARF doesn't currently provide an explicit name for the components
17968 of a complex number, but it can be helpful to have these components
17969 named, we try to select a suitable name based on the size of the
17970 component. */
17971static struct type *
17972dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17973 struct objfile *objfile,
103a685e
TT
17974 int bits, const char *name_hint,
17975 enum bfd_endian byte_order)
8bdc1658 17976{
08feed99 17977 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17978 struct type *tt = nullptr;
17979
35add35e
AB
17980 /* Try to find a suitable floating point builtin type of size BITS.
17981 We're going to use the name of this type as the name for the complex
17982 target type that we are about to create. */
3e9f1ca1 17983 switch (cu->per_cu->lang)
8bdc1658 17984 {
1db455a7
AB
17985 case language_fortran:
17986 switch (bits)
17987 {
17988 case 32:
17989 tt = builtin_f_type (gdbarch)->builtin_real;
17990 break;
17991 case 64:
17992 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17993 break;
17994 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17995 case 128:
17996 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17997 break;
17998 }
8bdc1658 17999 break;
1db455a7
AB
18000 default:
18001 switch (bits)
18002 {
18003 case 32:
18004 tt = builtin_type (gdbarch)->builtin_float;
18005 break;
18006 case 64:
18007 tt = builtin_type (gdbarch)->builtin_double;
18008 break;
18009 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18010 case 128:
18011 tt = builtin_type (gdbarch)->builtin_long_double;
18012 break;
18013 }
8bdc1658
AB
18014 break;
18015 }
18016
35add35e
AB
18017 /* If the type we found doesn't match the size we were looking for, then
18018 pretend we didn't find a type at all, the complex target type we
18019 create will then be nameless. */
a12e5744 18020 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18021 tt = nullptr;
18022
7d93a1e0 18023 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18024 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18025}
18026
c906108c
SS
18027/* Find a representation of a given base type and install
18028 it in the TYPE field of the die. */
18029
f792889a 18030static struct type *
e7c27a73 18031read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18032{
5e22e966 18033 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18034 struct type *type;
18035 struct attribute *attr;
19f392bc 18036 int encoding = 0, bits = 0;
15d034d0 18037 const char *name;
34877895 18038 gdbarch *arch;
c906108c 18039
e142c38c 18040 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18041 if (attr != nullptr && attr->form_is_constant ())
18042 encoding = attr->constant_value (0);
e142c38c 18043 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18044 if (attr != nullptr)
529908cb 18045 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18046 name = dwarf2_name (die, cu);
6ccb9162 18047 if (!name)
34877895 18048 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18049
08feed99 18050 arch = objfile->arch ();
103a685e
TT
18051 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18052
34877895 18053 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18054 if (attr != nullptr && attr->form_is_constant ())
103a685e 18055 {
529908cb 18056 int endianity = attr->constant_value (0);
103a685e
TT
18057
18058 switch (endianity)
18059 {
18060 case DW_END_big:
18061 byte_order = BFD_ENDIAN_BIG;
18062 break;
18063 case DW_END_little:
18064 byte_order = BFD_ENDIAN_LITTLE;
18065 break;
18066 default:
18067 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18068 break;
18069 }
18070 }
6ccb9162 18071
09584414 18072 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
3e9f1ca1 18073 && cu->per_cu->lang == language_ada
09584414
JB
18074 && has_zero_over_zero_small_attribute (die, cu))
18075 {
18076 /* brobecker/2018-02-24: This is a fixed point type for which
18077 the scaling factor is represented as fraction whose value
18078 does not make sense (zero divided by zero), so we should
18079 normally never see these. However, there is a small category
18080 of fixed point types for which GNAT is unable to provide
18081 the scaling factor via the standard DWARF mechanisms, and
18082 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18083 This is likely what this DIE is about. */
09584414
JB
18084 encoding = (encoding == DW_ATE_signed_fixed
18085 ? DW_ATE_signed
18086 : DW_ATE_unsigned);
18087 }
18088
bbcdf9ab
TT
18089 /* With GNAT encodings, fixed-point information will be encoded in
18090 the type name. Note that this can also occur with the above
18091 zero-over-zero case, which is why this is a separate "if" rather
18092 than an "else if". */
18093 const char *gnat_encoding_suffix = nullptr;
18094 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
3e9f1ca1 18095 && cu->per_cu->lang == language_ada
bbcdf9ab
TT
18096 && name != nullptr)
18097 {
18098 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18099 if (gnat_encoding_suffix != nullptr)
18100 {
18101 gdb_assert (startswith (gnat_encoding_suffix,
18102 GNAT_FIXED_POINT_SUFFIX));
18103 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18104 name, gnat_encoding_suffix - name);
18105 /* Use -1 here so that SUFFIX points at the "_" after the
18106 "XF". */
18107 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18108
18109 encoding = (encoding == DW_ATE_signed
18110 ? DW_ATE_signed_fixed
18111 : DW_ATE_unsigned_fixed);
18112 }
18113 }
18114
6ccb9162 18115 switch (encoding)
c906108c 18116 {
6ccb9162
UW
18117 case DW_ATE_address:
18118 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18119 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18120 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18121 break;
18122 case DW_ATE_boolean:
19f392bc 18123 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18124 break;
18125 case DW_ATE_complex_float:
103a685e
TT
18126 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18127 byte_order);
78134374 18128 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18129 {
18130 if (name == nullptr)
18131 {
18132 struct obstack *obstack
5e22e966 18133 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18134 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18135 nullptr);
18136 }
18137 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18138 }
18139 else
18140 type = init_complex_type (name, type);
6ccb9162
UW
18141 break;
18142 case DW_ATE_decimal_float:
19f392bc 18143 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18144 break;
18145 case DW_ATE_float:
103a685e 18146 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18147 break;
18148 case DW_ATE_signed:
eb77c9df 18149 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18150 break;
18151 case DW_ATE_unsigned:
3e9f1ca1 18152 if (cu->per_cu->lang == language_fortran
3b2b8fea 18153 && name
61012eef 18154 && startswith (name, "character("))
19f392bc
UW
18155 type = init_character_type (objfile, bits, 1, name);
18156 else
eb77c9df 18157 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18158 break;
18159 case DW_ATE_signed_char:
3e9f1ca1
TT
18160 if (cu->per_cu->lang == language_ada
18161 || cu->per_cu->lang == language_m2
18162 || cu->per_cu->lang == language_pascal
18163 || cu->per_cu->lang == language_fortran)
19f392bc
UW
18164 type = init_character_type (objfile, bits, 0, name);
18165 else
eb77c9df 18166 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18167 break;
18168 case DW_ATE_unsigned_char:
3e9f1ca1
TT
18169 if (cu->per_cu->lang == language_ada
18170 || cu->per_cu->lang == language_m2
18171 || cu->per_cu->lang == language_pascal
18172 || cu->per_cu->lang == language_fortran
18173 || cu->per_cu->lang == language_rust)
19f392bc
UW
18174 type = init_character_type (objfile, bits, 1, name);
18175 else
eb77c9df 18176 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18177 break;
75079b2b 18178 case DW_ATE_UTF:
53e710ac 18179 {
29b161c9 18180 type = init_character_type (objfile, bits, 1, name);
53e710ac
PA
18181 return set_die_type (die, type, cu);
18182 }
75079b2b 18183 break;
09584414
JB
18184 case DW_ATE_signed_fixed:
18185 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18186 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18187 break;
18188 case DW_ATE_unsigned_fixed:
18189 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18190 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18191 break;
75079b2b 18192
6ccb9162 18193 default:
b98664d3 18194 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18195 dwarf_type_encoding_name (encoding));
77b7c781 18196 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18197 break;
c906108c 18198 }
6ccb9162 18199
29b161c9
TT
18200 if (type->code () == TYPE_CODE_INT
18201 && name != nullptr
18202 && strcmp (name, "char") == 0)
15152a54 18203 type->set_has_no_signedness (true);
0114d602 18204
2b4424c3
TT
18205 maybe_set_alignment (cu, die, type);
18206
db558e34 18207 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18208
20a5fcbd
TT
18209 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18210 {
18211 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18212 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18213 {
529908cb 18214 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18215 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18216 /* Only use the attributes if they make sense together. */
18217 if (attr == nullptr
529908cb
TT
18218 || (attr->as_unsigned () + real_bit_size
18219 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18220 {
18221 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18222 = real_bit_size;
18223 if (attr != nullptr)
18224 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18225 = attr->as_unsigned ();
20a5fcbd
TT
18226 }
18227 }
18228 }
18229
f792889a 18230 return set_die_type (die, type, cu);
c906108c
SS
18231}
18232
386de171
TT
18233/* A helper function that returns the name of DIE, if it refers to a
18234 variable declaration. */
18235
18236static const char *
18237var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18238{
18239 if (die->tag != DW_TAG_variable)
18240 return nullptr;
18241
18242 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18243 if (attr == nullptr || !attr->as_boolean ())
18244 return nullptr;
18245
18246 attr = dwarf2_attr (die, DW_AT_name, cu);
18247 if (attr == nullptr)
18248 return nullptr;
18249 return attr->as_string ();
18250}
18251
80180f79
SA
18252/* Parse dwarf attribute if it's a block, reference or constant and put the
18253 resulting value of the attribute into struct bound_prop.
18254 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18255
18256static int
18257attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18258 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18259 struct type *default_type)
80180f79
SA
18260{
18261 struct dwarf2_property_baton *baton;
5e22e966 18262 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18263 struct objfile *objfile = per_objfile->objfile;
18264 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18265
9a49df9d
AB
18266 gdb_assert (default_type != NULL);
18267
80180f79
SA
18268 if (attr == NULL || prop == NULL)
18269 return 0;
18270
4fc6c0d5 18271 if (attr->form_is_block ())
80180f79 18272 {
8d749320 18273 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18274 baton->property_type = default_type;
80180f79 18275 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18276 baton->locexpr.per_objfile = per_objfile;
9d2246fc 18277
52a49e16
TV
18278 struct dwarf_block *block;
18279 if (attr->form == DW_FORM_data16)
18280 {
18281 size_t data_size = 16;
18282 block = XOBNEW (obstack, struct dwarf_block);
18283 block->size = (data_size
18284 + 2 /* Extra bytes for DW_OP and arg. */);
18285 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
18286 data[0] = DW_OP_implicit_value;
18287 data[1] = data_size;
18288 memcpy (&data[2], attr->as_block ()->data, data_size);
18289 block->data = data;
18290 }
18291 else
18292 block = attr->as_block ();
18293
9d2246fc
TT
18294 baton->locexpr.size = block->size;
18295 baton->locexpr.data = block->data;
216a7e6b
AB
18296 switch (attr->name)
18297 {
18298 case DW_AT_string_length:
18299 baton->locexpr.is_reference = true;
18300 break;
18301 default:
18302 baton->locexpr.is_reference = false;
18303 break;
18304 }
8c2e4e06
SM
18305
18306 prop->set_locexpr (baton);
18307 gdb_assert (prop->baton () != NULL);
80180f79 18308 }
cd6c91b4 18309 else if (attr->form_is_ref ())
80180f79
SA
18310 {
18311 struct dwarf2_cu *target_cu = cu;
18312 struct die_info *target_die;
18313 struct attribute *target_attr;
18314
18315 target_die = follow_die_ref (die, attr, &target_cu);
18316 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18317 if (target_attr == NULL)
18318 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18319 target_cu);
80180f79 18320 if (target_attr == NULL)
386de171
TT
18321 {
18322 const char *name = var_decl_name (target_die, target_cu);
18323 if (name != nullptr)
18324 {
18325 prop->set_variable_name (name);
18326 return 1;
18327 }
18328 return 0;
18329 }
80180f79 18330
df25ebbd 18331 switch (target_attr->name)
80180f79 18332 {
df25ebbd 18333 case DW_AT_location:
cd6c91b4 18334 if (target_attr->form_is_section_offset ())
df25ebbd 18335 {
8d749320 18336 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18337 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18338 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18339 prop->set_loclist (baton);
18340 gdb_assert (prop->baton () != NULL);
df25ebbd 18341 }
4fc6c0d5 18342 else if (target_attr->form_is_block ())
df25ebbd 18343 {
8d749320 18344 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18345 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18346 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18347 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18348 struct dwarf_block *block = target_attr->as_block ();
18349 baton->locexpr.size = block->size;
18350 baton->locexpr.data = block->data;
9a49df9d 18351 baton->locexpr.is_reference = true;
8c2e4e06
SM
18352 prop->set_locexpr (baton);
18353 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18354 }
18355 else
18356 {
18357 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18358 "dynamic property");
18359 return 0;
18360 }
18361 break;
18362 case DW_AT_data_member_location:
18363 {
18364 LONGEST offset;
18365
18366 if (!handle_data_member_location (target_die, target_cu,
18367 &offset))
18368 return 0;
18369
8d749320 18370 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18371 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18372 target_cu);
df25ebbd
JB
18373 baton->offset_info.offset = offset;
18374 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18375 prop->set_addr_offset (baton);
df25ebbd
JB
18376 break;
18377 }
80180f79
SA
18378 }
18379 }
cd6c91b4 18380 else if (attr->form_is_constant ())
8c2e4e06 18381 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18382 else
18383 {
18384 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18385 dwarf2_name (die, cu));
18386 return 0;
18387 }
18388
18389 return 1;
18390}
18391
09ba997f 18392/* See read.h. */
9a49df9d 18393
09ba997f 18394struct type *
293e7e51 18395dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18396{
9a49df9d
AB
18397 struct type *int_type;
18398
18399 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18400#define TRY_TYPE(F) \
18401 int_type = (unsigned_p \
18402 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18403 : objfile_type (objfile)->builtin_ ## F); \
18404 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18405 return int_type
18406
18407 TRY_TYPE (char);
18408 TRY_TYPE (short);
18409 TRY_TYPE (int);
18410 TRY_TYPE (long);
18411 TRY_TYPE (long_long);
18412
18413#undef TRY_TYPE
18414
18415 gdb_assert_not_reached ("unable to find suitable integer type");
18416}
18417
b86352cf
AB
18418/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18419 present (which is valid) then compute the default type based on the
18420 compilation units address size. */
18421
18422static struct type *
18423read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18424{
18425 struct type *index_type = die_type (die, cu);
18426
18427 /* Dwarf-2 specifications explicitly allows to create subrange types
18428 without specifying a base type.
18429 In that case, the base type must be set to the type of
18430 the lower bound, upper bound or count, in that order, if any of these
18431 three attributes references an object that has a type.
18432 If no base type is found, the Dwarf-2 specifications say that
18433 a signed integer type of size equal to the size of an address should
18434 be used.
18435 For the following C code: `extern char gdb_int [];'
18436 GCC produces an empty range DIE.
18437 FIXME: muller/2010-05-28: Possible references to object for low bound,
18438 high bound or count are not yet handled by this code. */
78134374 18439 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18440 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18441
18442 return index_type;
18443}
18444
a02abb62
JB
18445/* Read the given DW_AT_subrange DIE. */
18446
f792889a 18447static struct type *
a02abb62
JB
18448read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18449{
4c9ad8c2 18450 struct type *base_type, *orig_base_type;
a02abb62
JB
18451 struct type *range_type;
18452 struct attribute *attr;
729efb13 18453 struct dynamic_prop low, high;
4fae6e18 18454 int low_default_is_valid;
c451ebe5 18455 int high_bound_is_count = 0;
15d034d0 18456 const char *name;
d359392f 18457 ULONGEST negative_mask;
e77813c8 18458
b86352cf
AB
18459 orig_base_type = read_subrange_index_type (die, cu);
18460
4c9ad8c2
TT
18461 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18462 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18463 creating the range type, but we use the result of check_typedef
18464 when examining properties of the type. */
18465 base_type = check_typedef (orig_base_type);
a02abb62 18466
7e314c57
JK
18467 /* The die_type call above may have already set the type for this DIE. */
18468 range_type = get_die_type (die, cu);
18469 if (range_type)
18470 return range_type;
18471
8c2e4e06 18472 high.set_const_val (0);
729efb13 18473
4fae6e18
JK
18474 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18475 omitting DW_AT_lower_bound. */
3e9f1ca1 18476 switch (cu->per_cu->lang)
6e70227d 18477 {
4fae6e18
JK
18478 case language_c:
18479 case language_cplus:
8c2e4e06 18480 low.set_const_val (0);
4fae6e18
JK
18481 low_default_is_valid = 1;
18482 break;
18483 case language_fortran:
8c2e4e06 18484 low.set_const_val (1);
4fae6e18
JK
18485 low_default_is_valid = 1;
18486 break;
18487 case language_d:
4fae6e18 18488 case language_objc:
c44af4eb 18489 case language_rust:
8c2e4e06 18490 low.set_const_val (0);
4fae6e18
JK
18491 low_default_is_valid = (cu->header.version >= 4);
18492 break;
18493 case language_ada:
18494 case language_m2:
18495 case language_pascal:
8c2e4e06 18496 low.set_const_val (1);
4fae6e18
JK
18497 low_default_is_valid = (cu->header.version >= 4);
18498 break;
18499 default:
8c2e4e06 18500 low.set_const_val (0);
4fae6e18
JK
18501 low_default_is_valid = 0;
18502 break;
a02abb62
JB
18503 }
18504
e142c38c 18505 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18506 if (attr != nullptr)
9a49df9d 18507 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18508 else if (!low_default_is_valid)
b98664d3 18509 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18510 "- DIE at %s [in module %s]"),
18511 sect_offset_str (die->sect_off),
5e22e966 18512 objfile_name (cu->per_objfile->objfile));
a02abb62 18513
506f5c41
TV
18514 struct attribute *attr_ub, *attr_count;
18515 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18516 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18517 {
506f5c41 18518 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18519 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18520 {
c451ebe5 18521 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18522 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18523 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18524 else
18525 high_bound_is_count = 1;
c2ff108b 18526 }
506f5c41
TV
18527 else
18528 {
18529 if (attr_ub != NULL)
18530 complaint (_("Unresolved DW_AT_upper_bound "
18531 "- DIE at %s [in module %s]"),
18532 sect_offset_str (die->sect_off),
5e22e966 18533 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18534 if (attr_count != NULL)
18535 complaint (_("Unresolved DW_AT_count "
18536 "- DIE at %s [in module %s]"),
18537 sect_offset_str (die->sect_off),
5e22e966 18538 objfile_name (cu->per_objfile->objfile));
506f5c41 18539 }
e77813c8 18540 }
a02abb62 18541
4e962e74
TT
18542 LONGEST bias = 0;
18543 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18544 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18545 bias = bias_attr->constant_value (0);
4e962e74 18546
dbb9c2b1
JB
18547 /* Normally, the DWARF producers are expected to use a signed
18548 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18549 But this is unfortunately not always the case, as witnessed
18550 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18551 is used instead. To work around that ambiguity, we treat
18552 the bounds as signed, and thus sign-extend their values, when
18553 the base type is signed. */
6e70227d 18554 negative_mask =
d359392f 18555 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18556 if (low.kind () == PROP_CONST
c6d940a9 18557 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18558 low.set_const_val (low.const_val () | negative_mask);
18559 if (high.kind () == PROP_CONST
c6d940a9 18560 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18561 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18562
5bbd8269
AB
18563 /* Check for bit and byte strides. */
18564 struct dynamic_prop byte_stride_prop;
18565 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18566 if (attr_byte_stride != nullptr)
18567 {
293e7e51 18568 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18569 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18570 prop_type);
18571 }
18572
18573 struct dynamic_prop bit_stride_prop;
18574 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18575 if (attr_bit_stride != nullptr)
18576 {
18577 /* It only makes sense to have either a bit or byte stride. */
18578 if (attr_byte_stride != nullptr)
18579 {
18580 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18581 "- DIE at %s [in module %s]"),
18582 sect_offset_str (die->sect_off),
5e22e966 18583 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18584 attr_bit_stride = nullptr;
18585 }
18586 else
18587 {
293e7e51 18588 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18589 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18590 prop_type);
18591 }
18592 }
18593
18594 if (attr_byte_stride != nullptr
18595 || attr_bit_stride != nullptr)
18596 {
18597 bool byte_stride_p = (attr_byte_stride != nullptr);
18598 struct dynamic_prop *stride
18599 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18600
18601 range_type
18602 = create_range_type_with_stride (NULL, orig_base_type, &low,
18603 &high, bias, stride, byte_stride_p);
18604 }
18605 else
18606 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18607
c451ebe5 18608 if (high_bound_is_count)
599088e3 18609 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18610
c2ff108b 18611 /* Ada expects an empty array on no boundary attributes. */
3e9f1ca1 18612 if (attr == NULL && cu->per_cu->lang != language_ada)
8c2e4e06 18613 range_type->bounds ()->high.set_undefined ();
c2ff108b 18614
39cbfefa
DJ
18615 name = dwarf2_name (die, cu);
18616 if (name)
d0e39ea2 18617 range_type->set_name (name);
6e70227d 18618
e142c38c 18619 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18620 if (attr != nullptr)
529908cb 18621 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 18622
2b4424c3
TT
18623 maybe_set_alignment (cu, die, range_type);
18624
7e314c57
JK
18625 set_die_type (die, range_type, cu);
18626
18627 /* set_die_type should be already done. */
b4ba55a1
JB
18628 set_descriptive_type (range_type, die, cu);
18629
7e314c57 18630 return range_type;
a02abb62 18631}
6e70227d 18632
f792889a 18633static struct type *
81a17f79
JB
18634read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18635{
18636 struct type *type;
81a17f79 18637
5e22e966 18638 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18639 type->set_name (dwarf2_name (die, cu));
81a17f79 18640
74a2f8ff 18641 /* In Ada, an unspecified type is typically used when the description
85102364 18642 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18643 such a type, we treat it as a stub, and try to resolve it later on,
18644 when needed. */
3e9f1ca1 18645 if (cu->per_cu->lang == language_ada)
b4b73759 18646 type->set_is_stub (true);
74a2f8ff 18647
f792889a 18648 return set_die_type (die, type, cu);
81a17f79 18649}
a02abb62 18650
639d11d3
DC
18651/* Read a single die and all its descendents. Set the die's sibling
18652 field to NULL; set other fields in the die correctly, and set all
18653 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18654 location of the info_ptr after reading all of those dies. PARENT
18655 is the parent of the die in question. */
18656
18657static struct die_info *
dee91e82 18658read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18659 const gdb_byte *info_ptr,
18660 const gdb_byte **new_info_ptr,
dee91e82 18661 struct die_info *parent)
639d11d3
DC
18662{
18663 struct die_info *die;
d521ce57 18664 const gdb_byte *cur_ptr;
639d11d3 18665
3e225074 18666 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18667 if (die == NULL)
18668 {
18669 *new_info_ptr = cur_ptr;
18670 return NULL;
18671 }
93311388 18672 store_in_ref_table (die, reader->cu);
639d11d3 18673
3e225074 18674 if (die->has_children)
bf6af496 18675 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18676 else
18677 {
18678 die->child = NULL;
18679 *new_info_ptr = cur_ptr;
18680 }
18681
18682 die->sibling = NULL;
18683 die->parent = parent;
18684 return die;
18685}
18686
18687/* Read a die, all of its descendents, and all of its siblings; set
18688 all of the fields of all of the dies correctly. Arguments are as
18689 in read_die_and_children. */
18690
18691static struct die_info *
bf6af496 18692read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18693 const gdb_byte *info_ptr,
18694 const gdb_byte **new_info_ptr,
bf6af496 18695 struct die_info *parent)
639d11d3
DC
18696{
18697 struct die_info *first_die, *last_sibling;
d521ce57 18698 const gdb_byte *cur_ptr;
639d11d3 18699
c906108c 18700 cur_ptr = info_ptr;
639d11d3
DC
18701 first_die = last_sibling = NULL;
18702
18703 while (1)
c906108c 18704 {
639d11d3 18705 struct die_info *die
dee91e82 18706 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18707
1d325ec1 18708 if (die == NULL)
c906108c 18709 {
639d11d3
DC
18710 *new_info_ptr = cur_ptr;
18711 return first_die;
c906108c 18712 }
1d325ec1
DJ
18713
18714 if (!first_die)
18715 first_die = die;
c906108c 18716 else
1d325ec1
DJ
18717 last_sibling->sibling = die;
18718
18719 last_sibling = die;
c906108c 18720 }
c906108c
SS
18721}
18722
bf6af496
DE
18723/* Read a die, all of its descendents, and all of its siblings; set
18724 all of the fields of all of the dies correctly. Arguments are as
18725 in read_die_and_children.
18726 This the main entry point for reading a DIE and all its children. */
18727
18728static struct die_info *
18729read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18730 const gdb_byte *info_ptr,
18731 const gdb_byte **new_info_ptr,
bf6af496
DE
18732 struct die_info *parent)
18733{
18734 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18735 new_info_ptr, parent);
18736
b4f54984 18737 if (dwarf_die_debug)
bf6af496
DE
18738 {
18739 fprintf_unfiltered (gdb_stdlog,
18740 "Read die from %s@0x%x of %s:\n",
96b79293 18741 reader->die_section->get_name (),
bf6af496
DE
18742 (unsigned) (info_ptr - reader->die_section->buffer),
18743 bfd_get_filename (reader->abfd));
b4f54984 18744 dump_die (die, dwarf_die_debug);
bf6af496
DE
18745 }
18746
18747 return die;
18748}
18749
3019eac3
DE
18750/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18751 attributes.
18752 The caller is responsible for filling in the extra attributes
18753 and updating (*DIEP)->num_attrs.
18754 Set DIEP to point to a newly allocated die with its information,
3e225074 18755 except for its child, sibling, and parent fields. */
93311388 18756
d521ce57 18757static const gdb_byte *
3019eac3 18758read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18759 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18760 int num_extra_attrs)
93311388 18761{
b64f50a1 18762 unsigned int abbrev_number, bytes_read, i;
7c32eebb 18763 const struct abbrev_info *abbrev;
93311388
DE
18764 struct die_info *die;
18765 struct dwarf2_cu *cu = reader->cu;
18766 bfd *abfd = reader->abfd;
18767
9c541725 18768 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18769 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18770 info_ptr += bytes_read;
18771 if (!abbrev_number)
18772 {
18773 *diep = NULL;
93311388
DE
18774 return info_ptr;
18775 }
18776
685af9cd 18777 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18778 if (!abbrev)
348e048f
DE
18779 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18780 abbrev_number,
18781 bfd_get_filename (abfd));
18782
3019eac3 18783 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18784 die->sect_off = sect_off;
93311388
DE
18785 die->tag = abbrev->tag;
18786 die->abbrev = abbrev_number;
3e225074 18787 die->has_children = abbrev->has_children;
93311388 18788
3019eac3
DE
18789 /* Make the result usable.
18790 The caller needs to update num_attrs after adding the extra
18791 attributes. */
93311388
DE
18792 die->num_attrs = abbrev->num_attrs;
18793
7a5f294d 18794 bool any_need_reprocess = false;
93311388 18795 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 18796 {
7a5f294d
TT
18797 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18798 info_ptr);
18799 if (die->attrs[i].requires_reprocessing_p ())
18800 any_need_reprocess = true;
18a8505e
AT
18801 }
18802
052c8bb8 18803 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
18804 if (attr != nullptr && attr->form_is_unsigned ())
18805 cu->str_offsets_base = attr->as_unsigned ();
93311388 18806
41144253 18807 attr = die->attr (DW_AT_loclists_base);
18808 if (attr != nullptr)
529908cb 18809 cu->loclist_base = attr->as_unsigned ();
41144253 18810
a39fdb41 18811 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18812 if (maybe_addr_base.has_value ())
18813 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
18814
18815 attr = die->attr (DW_AT_rnglists_base);
18816 if (attr != nullptr)
2b0c7f41 18817 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 18818
7a5f294d
TT
18819 if (any_need_reprocess)
18820 {
18821 for (i = 0; i < abbrev->num_attrs; ++i)
18822 {
18823 if (die->attrs[i].requires_reprocessing_p ())
18824 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18825 }
18826 }
93311388 18827 *diep = die;
93311388
DE
18828 return info_ptr;
18829}
18830
3019eac3
DE
18831/* Read a die and all its attributes.
18832 Set DIEP to point to a newly allocated die with its information,
3e225074 18833 except for its child, sibling, and parent fields. */
3019eac3 18834
d521ce57 18835static const gdb_byte *
3019eac3 18836read_full_die (const struct die_reader_specs *reader,
3e225074 18837 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18838{
d521ce57 18839 const gdb_byte *result;
bf6af496 18840
3e225074 18841 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18842
b4f54984 18843 if (dwarf_die_debug)
bf6af496
DE
18844 {
18845 fprintf_unfiltered (gdb_stdlog,
18846 "Read die from %s@0x%x of %s:\n",
96b79293 18847 reader->die_section->get_name (),
bf6af496
DE
18848 (unsigned) (info_ptr - reader->die_section->buffer),
18849 bfd_get_filename (reader->abfd));
b4f54984 18850 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18851 }
18852
18853 return result;
3019eac3 18854}
433df2d4 18855\f
c906108c 18856
72bf9492
DJ
18857/* Returns nonzero if TAG represents a type that we might generate a partial
18858 symbol for. */
18859
18860static int
d8f62e84 18861is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
18862{
18863 switch (tag)
18864 {
18865#if 0
18866 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
18867 that we don't at present. Note that normally this does not
18868 matter, mainly because C compilers don't give names to these
18869 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
18870 case DW_TAG_file_type:
18871 case DW_TAG_ptr_to_member_type:
18872 case DW_TAG_set_type:
18873 case DW_TAG_string_type:
18874 case DW_TAG_subroutine_type:
18875#endif
d8f62e84
TT
18876
18877 /* GNAT may emit an array with a name, but no typedef, so we
18878 need to make a symbol in this case. */
18879 case DW_TAG_array_type:
18880 return lang == language_ada;
18881
72bf9492
DJ
18882 case DW_TAG_base_type:
18883 case DW_TAG_class_type:
680b30c7 18884 case DW_TAG_interface_type:
72bf9492
DJ
18885 case DW_TAG_enumeration_type:
18886 case DW_TAG_structure_type:
18887 case DW_TAG_subrange_type:
18888 case DW_TAG_typedef:
18889 case DW_TAG_union_type:
18890 return 1;
18891 default:
18892 return 0;
18893 }
18894}
18895
18896/* Load all DIEs that are interesting for partial symbols into memory. */
18897
18898static struct partial_die_info *
dee91e82 18899load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18900 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18901{
dee91e82 18902 struct dwarf2_cu *cu = reader->cu;
5e22e966 18903 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18904 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18905 unsigned int bytes_read;
5afb4e99 18906 unsigned int load_all = 0;
72bf9492
DJ
18907 int nesting_level = 1;
18908
18909 parent_die = NULL;
18910 last_die = NULL;
18911
7adf1e79 18912 gdb_assert (cu->per_cu != NULL);
6dcd1193 18913 if (cu->load_all_dies)
5afb4e99
DJ
18914 load_all = 1;
18915
72bf9492
DJ
18916 cu->partial_dies
18917 = htab_create_alloc_ex (cu->header.length / 12,
18918 partial_die_hash,
18919 partial_die_eq,
18920 NULL,
18921 &cu->comp_unit_obstack,
18922 hashtab_obstack_allocate,
18923 dummy_obstack_deallocate);
18924
72bf9492
DJ
18925 while (1)
18926 {
7c32eebb
TT
18927 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18928 &bytes_read);
72bf9492
DJ
18929
18930 /* A NULL abbrev means the end of a series of children. */
18931 if (abbrev == NULL)
18932 {
18933 if (--nesting_level == 0)
cd9983dd
YQ
18934 return first_die;
18935
72bf9492
DJ
18936 info_ptr += bytes_read;
18937 last_die = parent_die;
18938 parent_die = parent_die->die_parent;
18939 continue;
18940 }
18941
98bfdba5
PA
18942 /* Check for template arguments. We never save these; if
18943 they're seen, we just mark the parent, and go on our way. */
18944 if (parent_die != NULL
3e9f1ca1 18945 && cu->per_cu->lang == language_cplus
98bfdba5
PA
18946 && (abbrev->tag == DW_TAG_template_type_param
18947 || abbrev->tag == DW_TAG_template_value_param))
18948 {
18949 parent_die->has_template_arguments = 1;
18950
18951 if (!load_all)
18952 {
18953 /* We don't need a partial DIE for the template argument. */
dee91e82 18954 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18955 continue;
18956 }
18957 }
18958
0d99eb77 18959 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18960 Skip their other children. */
18961 if (!load_all
3e9f1ca1 18962 && cu->per_cu->lang == language_cplus
98bfdba5 18963 && parent_die != NULL
f9b5d5ea
TV
18964 && parent_die->tag == DW_TAG_subprogram
18965 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 18966 {
dee91e82 18967 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18968 continue;
18969 }
18970
5afb4e99
DJ
18971 /* Check whether this DIE is interesting enough to save. Normally
18972 we would not be interested in members here, but there may be
18973 later variables referencing them via DW_AT_specification (for
18974 static members). */
18975 if (!load_all
3e9f1ca1 18976 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
72929c62 18977 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18978 && abbrev->tag != DW_TAG_enumerator
18979 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18980 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18981 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18982 && abbrev->tag != DW_TAG_variable
5afb4e99 18983 && abbrev->tag != DW_TAG_namespace
f55ee35c 18984 && abbrev->tag != DW_TAG_module
95554aad 18985 && abbrev->tag != DW_TAG_member
74921315
KS
18986 && abbrev->tag != DW_TAG_imported_unit
18987 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18988 {
18989 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18990 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18991 continue;
18992 }
18993
6f06d47b
YQ
18994 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18995 abbrev);
cd9983dd 18996
48fbe735 18997 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18998
18999 /* This two-pass algorithm for processing partial symbols has a
19000 high cost in cache pressure. Thus, handle some simple cases
19001 here which cover the majority of C partial symbols. DIEs
19002 which neither have specification tags in them, nor could have
19003 specification tags elsewhere pointing at them, can simply be
19004 processed and discarded.
19005
19006 This segment is also optional; scan_partial_symbols and
19007 add_partial_symbol will handle these DIEs if we chain
19008 them in normally. When compilers which do not emit large
19009 quantities of duplicate debug information are more common,
19010 this code can probably be removed. */
19011
19012 /* Any complete simple types at the top level (pretty much all
19013 of them, for a language without namespaces), can be processed
19014 directly. */
19015 if (parent_die == NULL
cd9983dd
YQ
19016 && pdi.has_specification == 0
19017 && pdi.is_declaration == 0
19018 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19019 || pdi.tag == DW_TAG_base_type
d8f62e84 19020 || pdi.tag == DW_TAG_array_type
cd9983dd 19021 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19022 {
7d00ffec 19023 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19024 add_partial_symbol (&pdi, cu);
19025
cd9983dd 19026 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19027 continue;
19028 }
19029
d8228535
JK
19030 /* The exception for DW_TAG_typedef with has_children above is
19031 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19032 type_name_or_error will error on such types later.
d8228535
JK
19033
19034 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19035 it could not find the child DIEs referenced later, this is checked
19036 above. In correct DWARF DW_TAG_typedef should have no children. */
19037
cd9983dd 19038 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19039 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19040 "- DIE at %s [in module %s]"),
cd9983dd 19041 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19042
72bf9492
DJ
19043 /* If we're at the second level, and we're an enumerator, and
19044 our parent has no specification (meaning possibly lives in a
19045 namespace elsewhere), then we can add the partial symbol now
19046 instead of queueing it. */
cd9983dd 19047 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19048 && parent_die != NULL
19049 && parent_die->die_parent == NULL
19050 && parent_die->tag == DW_TAG_enumeration_type
19051 && parent_die->has_specification == 0)
19052 {
7d00ffec 19053 if (pdi.raw_name == NULL)
b98664d3 19054 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19055 else if (building_psymtab)
f0fbb768 19056 add_partial_symbol (&pdi, cu);
72bf9492 19057
cd9983dd 19058 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19059 continue;
19060 }
19061
cd9983dd 19062 struct partial_die_info *part_die
6f06d47b 19063 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19064
72bf9492
DJ
19065 /* We'll save this DIE so link it in. */
19066 part_die->die_parent = parent_die;
19067 part_die->die_sibling = NULL;
19068 part_die->die_child = NULL;
19069
19070 if (last_die && last_die == parent_die)
19071 last_die->die_child = part_die;
19072 else if (last_die)
19073 last_die->die_sibling = part_die;
19074
19075 last_die = part_die;
19076
19077 if (first_die == NULL)
19078 first_die = part_die;
19079
19080 /* Maybe add the DIE to the hash table. Not all DIEs that we
19081 find interesting need to be in the hash table, because we
19082 also have the parent/sibling/child chains; only those that we
19083 might refer to by offset later during partial symbol reading.
19084
19085 For now this means things that might have be the target of a
19086 DW_AT_specification, DW_AT_abstract_origin, or
19087 DW_AT_extension. DW_AT_extension will refer only to
19088 namespaces; DW_AT_abstract_origin refers to functions (and
19089 many things under the function DIE, but we do not recurse
19090 into function DIEs during partial symbol reading) and
19091 possibly variables as well; DW_AT_specification refers to
19092 declarations. Declarations ought to have the DW_AT_declaration
19093 flag. It happens that GCC forgets to put it in sometimes, but
19094 only for functions, not for types.
19095
19096 Adding more things than necessary to the hash table is harmless
19097 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19098 wasted time in find_partial_die, when we reread the compilation
19099 unit with load_all_dies set. */
72bf9492 19100
5afb4e99 19101 if (load_all
72929c62 19102 || abbrev->tag == DW_TAG_constant
5afb4e99 19103 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19104 || abbrev->tag == DW_TAG_variable
19105 || abbrev->tag == DW_TAG_namespace
19106 || part_die->is_declaration)
19107 {
19108 void **slot;
19109
19110 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19111 to_underlying (part_die->sect_off),
19112 INSERT);
72bf9492
DJ
19113 *slot = part_die;
19114 }
19115
72bf9492 19116 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19117 we have no reason to follow the children of structures; for other
98bfdba5
PA
19118 languages we have to, so that we can get at method physnames
19119 to infer fully qualified class names, for DW_AT_specification,
19120 and for C++ template arguments. For C++, we also look one level
19121 inside functions to find template arguments (if the name of the
19122 function does not already contain the template arguments).
bc30ff58 19123
0a4b0913
AB
19124 For Ada and Fortran, we need to scan the children of subprograms
19125 and lexical blocks as well because these languages allow the
19126 definition of nested entities that could be interesting for the
19127 debugger, such as nested subprograms for instance. */
72bf9492 19128 if (last_die->has_children
5afb4e99
DJ
19129 && (load_all
19130 || last_die->tag == DW_TAG_namespace
f55ee35c 19131 || last_die->tag == DW_TAG_module
72bf9492 19132 || last_die->tag == DW_TAG_enumeration_type
3e9f1ca1 19133 || (cu->per_cu->lang == language_cplus
98bfdba5 19134 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19135 && (last_die->raw_name == NULL
19136 || strchr (last_die->raw_name, '<') == NULL))
3e9f1ca1 19137 || (cu->per_cu->lang != language_c
72bf9492 19138 && (last_die->tag == DW_TAG_class_type
680b30c7 19139 || last_die->tag == DW_TAG_interface_type
72bf9492 19140 || last_die->tag == DW_TAG_structure_type
bc30ff58 19141 || last_die->tag == DW_TAG_union_type))
3e9f1ca1
TT
19142 || ((cu->per_cu->lang == language_ada
19143 || cu->per_cu->lang == language_fortran)
bc30ff58
JB
19144 && (last_die->tag == DW_TAG_subprogram
19145 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19146 {
19147 nesting_level++;
19148 parent_die = last_die;
19149 continue;
19150 }
19151
19152 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19153 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19154
19155 /* Back to the top, do it again. */
19156 }
19157}
19158
6f06d47b 19159partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19160 const struct abbrev_info *abbrev)
6f06d47b
YQ
19161 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19162{
19163}
19164
7d00ffec
TT
19165/* See class definition. */
19166
19167const char *
19168partial_die_info::name (dwarf2_cu *cu)
19169{
19170 if (!canonical_name && raw_name != nullptr)
19171 {
19172 struct objfile *objfile = cu->per_objfile->objfile;
19173 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19174 canonical_name = 1;
19175 }
19176
19177 return raw_name;
19178}
19179
35cc7ed7
YQ
19180/* Read a minimal amount of information into the minimal die structure.
19181 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19182
48fbe735
YQ
19183const gdb_byte *
19184partial_die_info::read (const struct die_reader_specs *reader,
19185 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19186{
dee91e82 19187 struct dwarf2_cu *cu = reader->cu;
976ca316 19188 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19189 unsigned int i;
c5aa993b 19190 int has_low_pc_attr = 0;
c906108c 19191 int has_high_pc_attr = 0;
91da1414 19192 int high_pc_relative = 0;
c906108c 19193
fd0a254f 19194 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19195 {
e7da7f8f 19196 attribute attr;
7a5f294d 19197 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19198 /* String and address offsets that need to do the reprocessing have
dda83cd7 19199 already been read at this point, so there is no need to wait until
18a8505e 19200 the loop terminates to do the reprocessing. */
7a5f294d 19201 if (attr.requires_reprocessing_p ())
d0ce17d8 19202 read_attribute_reprocess (reader, &attr, tag);
c906108c 19203 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19204 partial symbol table. */
c906108c
SS
19205 switch (attr.name)
19206 {
19207 case DW_AT_name:
48fbe735 19208 switch (tag)
71c25dea
TT
19209 {
19210 case DW_TAG_compile_unit:
95554aad 19211 case DW_TAG_partial_unit:
348e048f 19212 case DW_TAG_type_unit:
71c25dea
TT
19213 /* Compilation units have a DW_AT_name that is a filename, not
19214 a source language identifier. */
19215 case DW_TAG_enumeration_type:
19216 case DW_TAG_enumerator:
19217 /* These tags always have simple identifiers already; no need
19218 to canonicalize them. */
7d00ffec 19219 canonical_name = 1;
2c830f54 19220 raw_name = attr.as_string ();
71c25dea
TT
19221 break;
19222 default:
7d00ffec 19223 canonical_name = 0;
2c830f54 19224 raw_name = attr.as_string ();
71c25dea
TT
19225 break;
19226 }
c906108c 19227 break;
31ef98ae 19228 case DW_AT_linkage_name:
c906108c 19229 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19230 /* Note that both forms of linkage name might appear. We
19231 assume they will be the same, and we only store the last
19232 one we see. */
95f982e5 19233 linkage_name = attr.as_string ();
c906108c
SS
19234 break;
19235 case DW_AT_low_pc:
19236 has_low_pc_attr = 1;
95f982e5 19237 lowpc = attr.as_address ();
c906108c
SS
19238 break;
19239 case DW_AT_high_pc:
19240 has_high_pc_attr = 1;
95f982e5 19241 highpc = attr.as_address ();
cd6c91b4 19242 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19243 high_pc_relative = 1;
c906108c
SS
19244 break;
19245 case DW_AT_location:
dda83cd7
SM
19246 /* Support the .debug_loc offsets. */
19247 if (attr.form_is_block ())
19248 {
9d2246fc 19249 d.locdesc = attr.as_block ();
dda83cd7
SM
19250 }
19251 else if (attr.form_is_section_offset ())
19252 {
4d3c2250 19253 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19254 }
19255 else
19256 {
4d3c2250
KB
19257 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19258 "partial symbol information");
dda83cd7 19259 }
c906108c 19260 break;
c906108c 19261 case DW_AT_external:
c45bc3f8 19262 is_external = attr.as_boolean ();
c906108c
SS
19263 break;
19264 case DW_AT_declaration:
c45bc3f8 19265 is_declaration = attr.as_boolean ();
c906108c
SS
19266 break;
19267 case DW_AT_type:
48fbe735 19268 has_type = 1;
c906108c
SS
19269 break;
19270 case DW_AT_abstract_origin:
19271 case DW_AT_specification:
72bf9492 19272 case DW_AT_extension:
48fbe735 19273 has_specification = 1;
0826b30a 19274 spec_offset = attr.get_ref_die_offset ();
48fbe735 19275 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19276 || cu->per_cu->is_dwz);
c906108c
SS
19277 break;
19278 case DW_AT_sibling:
19279 /* Ignore absolute siblings, they might point outside of
19280 the current compile unit. */
19281 if (attr.form == DW_FORM_ref_addr)
b98664d3 19282 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19283 else
b9502d3f 19284 {
48fbe735 19285 const gdb_byte *buffer = reader->buffer;
0826b30a 19286 sect_offset off = attr.get_ref_die_offset ();
9c541725 19287 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19288
19289 if (sibling_ptr < info_ptr)
b98664d3 19290 complaint (_("DW_AT_sibling points backwards"));
22869d73 19291 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19292 reader->die_section->overflow_complaint ();
b9502d3f 19293 else
48fbe735 19294 sibling = sibling_ptr;
b9502d3f 19295 }
c906108c 19296 break;
dda83cd7
SM
19297 case DW_AT_byte_size:
19298 has_byte_size = 1;
19299 break;
19300 case DW_AT_const_value:
19301 has_const_value = 1;
19302 break;
68511cec
CES
19303 case DW_AT_calling_convention:
19304 /* DWARF doesn't provide a way to identify a program's source-level
19305 entry point. DW_AT_calling_convention attributes are only meant
19306 to describe functions' calling conventions.
19307
19308 However, because it's a necessary piece of information in
0c1b455e
TT
19309 Fortran, and before DWARF 4 DW_CC_program was the only
19310 piece of debugging information whose definition refers to
19311 a 'main program' at all, several compilers marked Fortran
19312 main programs with DW_CC_program --- even when those
19313 functions use the standard calling conventions.
19314
19315 Although DWARF now specifies a way to provide this
19316 information, we support this practice for backward
19317 compatibility. */
529908cb 19318 if (attr.constant_value (0) == DW_CC_program
3e9f1ca1 19319 && cu->per_cu->lang == language_fortran)
48fbe735 19320 main_subprogram = 1;
68511cec 19321 break;
481860b3 19322 case DW_AT_inline:
529908cb
TT
19323 {
19324 LONGEST value = attr.constant_value (-1);
19325 if (value == DW_INL_inlined
19326 || value == DW_INL_declared_inlined)
19327 may_be_inlined = 1;
19328 }
481860b3 19329 break;
95554aad
TT
19330
19331 case DW_AT_import:
48fbe735 19332 if (tag == DW_TAG_imported_unit)
36586728 19333 {
0826b30a 19334 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19335 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19336 || cu->per_cu->is_dwz);
19337 }
95554aad
TT
19338 break;
19339
0c1b455e 19340 case DW_AT_main_subprogram:
c45bc3f8 19341 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19342 break;
19343
05caa1d2
TT
19344 case DW_AT_ranges:
19345 {
2b0c7f41
SM
19346 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19347 on DWARF version). */
19348 ULONGEST ranges_offset = attr.as_unsigned ();
19349
19350 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19351 this value. */
19352 if (tag != DW_TAG_compile_unit)
19353 ranges_offset += cu->gnu_ranges_base;
19354
05caa1d2 19355 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19356 nullptr, tag))
05caa1d2
TT
19357 has_pc_info = 1;
19358 }
19359 break;
19360
c906108c
SS
19361 default:
19362 break;
19363 }
19364 }
19365
10d06d82
TT
19366 /* For Ada, if both the name and the linkage name appear, we prefer
19367 the latter. This lets "catch exception" work better, regardless
19368 of the order in which the name and linkage name were emitted.
19369 Really, though, this is just a workaround for the fact that gdb
19370 doesn't store both the name and the linkage name. */
3e9f1ca1 19371 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
7d00ffec 19372 raw_name = linkage_name;
10d06d82 19373
91da1414 19374 if (high_pc_relative)
48fbe735 19375 highpc += lowpc;
91da1414 19376
9373cf26
JK
19377 if (has_low_pc_attr && has_high_pc_attr)
19378 {
19379 /* When using the GNU linker, .gnu.linkonce. sections are used to
19380 eliminate duplicate copies of functions and vtables and such.
19381 The linker will arbitrarily choose one and discard the others.
19382 The AT_*_pc values for such functions refer to local labels in
19383 these sections. If the section from that file was discarded, the
19384 labels are not in the output, so the relocs get a value of 0.
19385 If this is a discarded function, mark the pc bounds as invalid,
19386 so that GDB will ignore it. */
976ca316 19387 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19388 {
976ca316 19389 struct objfile *objfile = per_objfile->objfile;
08feed99 19390 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19391
b98664d3 19392 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19393 "for DIE at %s [in module %s]"),
48fbe735
YQ
19394 paddress (gdbarch, lowpc),
19395 sect_offset_str (sect_off),
9d8780f0 19396 objfile_name (objfile));
9373cf26
JK
19397 }
19398 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19399 else if (lowpc >= highpc)
9373cf26 19400 {
976ca316 19401 struct objfile *objfile = per_objfile->objfile;
08feed99 19402 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19403
b98664d3 19404 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19405 "for DIE at %s [in module %s]"),
48fbe735
YQ
19406 paddress (gdbarch, lowpc),
19407 paddress (gdbarch, highpc),
19408 sect_offset_str (sect_off),
9c541725 19409 objfile_name (objfile));
9373cf26
JK
19410 }
19411 else
48fbe735 19412 has_pc_info = 1;
9373cf26 19413 }
85cbf3d3 19414
c906108c
SS
19415 return info_ptr;
19416}
19417
72bf9492
DJ
19418/* Find a cached partial DIE at OFFSET in CU. */
19419
d590ff25
YQ
19420struct partial_die_info *
19421dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19422{
19423 struct partial_die_info *lookup_die = NULL;
6f06d47b 19424 struct partial_die_info part_die (sect_off);
72bf9492 19425
9a3c8263 19426 lookup_die = ((struct partial_die_info *)
d590ff25 19427 htab_find_with_hash (partial_dies, &part_die,
9c541725 19428 to_underlying (sect_off)));
72bf9492 19429
72bf9492
DJ
19430 return lookup_die;
19431}
19432
348e048f
DE
19433/* Find a partial DIE at OFFSET, which may or may not be in CU,
19434 except in the case of .debug_types DIEs which do not reference
19435 outside their CU (they do however referencing other types via
55f1336d 19436 DW_FORM_ref_sig8). */
72bf9492 19437
122cf0f2 19438static const struct cu_partial_die_info
9c541725 19439find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19440{
976ca316
SM
19441 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19442 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19443 struct partial_die_info *pd = NULL;
72bf9492 19444
36586728 19445 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19446 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19447 {
d590ff25 19448 pd = cu->find_partial_die (sect_off);
5afb4e99 19449 if (pd != NULL)
fb816e8b 19450 return { cu, pd };
0d99eb77
DE
19451 /* We missed recording what we needed.
19452 Load all dies and try again. */
5afb4e99 19453 }
0d99eb77
DE
19454 else
19455 {
19456 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19457 if (cu->per_cu->is_debug_types)
0d99eb77 19458 {
9d8780f0
SM
19459 error (_("Dwarf Error: Type Unit at offset %s contains"
19460 " external reference to offset %s [in module %s].\n"),
19461 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19462 bfd_get_filename (objfile->obfd));
19463 }
7188ed02
SM
19464 dwarf2_per_cu_data *per_cu
19465 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19466 per_objfile);
72bf9492 19467
976ca316 19468 cu = per_objfile->get_cu (per_cu);
7188ed02 19469 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19470 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19471
976ca316 19472 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19473
19474 cu->last_used = 0;
19475 pd = cu->find_partial_die (sect_off);
0d99eb77 19476 }
5afb4e99 19477
dee91e82
DE
19478 /* If we didn't find it, and not all dies have been loaded,
19479 load them all and try again. */
19480
6dcd1193 19481 if (pd == NULL && cu->load_all_dies == 0)
5afb4e99 19482 {
6dcd1193 19483 cu->load_all_dies = 1;
fd820528
DE
19484
19485 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19486 THIS_CU->cu may already be in use. So we can't just free it and
19487 replace its DIEs with the ones we read in. Instead, we leave those
19488 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19489 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19490 set. */
976ca316 19491 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19492
7188ed02 19493 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19494 }
19495
19496 if (pd == NULL)
2152b4fd 19497 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
9d8780f0 19498 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19499 return { cu, pd };
72bf9492
DJ
19500}
19501
abc72ce4
DE
19502/* See if we can figure out if the class lives in a namespace. We do
19503 this by looking for a member function; its demangled name will
19504 contain namespace info, if there is any. */
19505
19506static void
19507guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19508 struct dwarf2_cu *cu)
19509{
19510 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19511 what template types look like, because the demangler
19512 frequently doesn't give the same name as the debug info. We
19513 could fix this by only using the demangled name to get the
19514 prefix (but see comment in read_structure_type). */
19515
19516 struct partial_die_info *real_pdi;
19517 struct partial_die_info *child_pdi;
19518
19519 /* If this DIE (this DIE's specification, if any) has a parent, then
19520 we should not do this. We'll prepend the parent's fully qualified
19521 name when we create the partial symbol. */
19522
19523 real_pdi = struct_pdi;
19524 while (real_pdi->has_specification)
fb816e8b 19525 {
122cf0f2
AB
19526 auto res = find_partial_die (real_pdi->spec_offset,
19527 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19528 real_pdi = res.pdi;
19529 cu = res.cu;
19530 }
abc72ce4
DE
19531
19532 if (real_pdi->die_parent != NULL)
19533 return;
19534
19535 for (child_pdi = struct_pdi->die_child;
19536 child_pdi != NULL;
19537 child_pdi = child_pdi->die_sibling)
19538 {
19539 if (child_pdi->tag == DW_TAG_subprogram
19540 && child_pdi->linkage_name != NULL)
19541 {
43816ebc 19542 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19543 (cu->language_defn->class_name_from_physname
19544 (child_pdi->linkage_name));
abc72ce4
DE
19545 if (actual_class_name != NULL)
19546 {
5e22e966 19547 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19548 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19549 struct_pdi->canonical_name = 1;
abc72ce4
DE
19550 }
19551 break;
19552 }
19553 }
19554}
19555
25c11aca
TV
19556/* Return true if a DIE with TAG may have the DW_AT_const_value
19557 attribute. */
19558
19559static bool
19560can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19561{
19562 switch (tag)
19563 {
19564 case DW_TAG_constant:
19565 case DW_TAG_enumerator:
19566 case DW_TAG_formal_parameter:
19567 case DW_TAG_template_value_param:
19568 case DW_TAG_variable:
19569 return true;
19570 }
19571
19572 return false;
19573}
19574
52356b79
YQ
19575void
19576partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19577{
abc72ce4
DE
19578 /* Once we've fixed up a die, there's no point in doing so again.
19579 This also avoids a memory leak if we were to call
19580 guess_partial_die_structure_name multiple times. */
52356b79 19581 if (fixup_called)
abc72ce4
DE
19582 return;
19583
72bf9492
DJ
19584 /* If we found a reference attribute and the DIE has no name, try
19585 to find a name in the referred to DIE. */
19586
7d00ffec 19587 if (raw_name == NULL && has_specification)
72bf9492
DJ
19588 {
19589 struct partial_die_info *spec_die;
72bf9492 19590
122cf0f2 19591 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19592 spec_die = res.pdi;
19593 cu = res.cu;
72bf9492 19594
52356b79 19595 spec_die->fixup (cu);
72bf9492 19596
7d00ffec 19597 if (spec_die->raw_name)
72bf9492 19598 {
7d00ffec
TT
19599 raw_name = spec_die->raw_name;
19600 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19601
19602 /* Copy DW_AT_external attribute if it is set. */
19603 if (spec_die->is_external)
52356b79 19604 is_external = spec_die->is_external;
72bf9492
DJ
19605 }
19606 }
19607
25c11aca
TV
19608 if (!has_const_value && has_specification
19609 && can_have_DW_AT_const_value_p (tag))
19610 {
19611 struct partial_die_info *spec_die;
19612
19613 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19614 spec_die = res.pdi;
19615 cu = res.cu;
19616
19617 spec_die->fixup (cu);
19618
19619 if (spec_die->has_const_value)
19620 {
19621 /* Copy DW_AT_const_value attribute if it is set. */
19622 has_const_value = spec_die->has_const_value;
19623 }
19624 }
19625
72bf9492 19626 /* Set default names for some unnamed DIEs. */
72bf9492 19627
7d00ffec
TT
19628 if (raw_name == NULL && tag == DW_TAG_namespace)
19629 {
19630 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19631 canonical_name = 1;
19632 }
72bf9492 19633
abc72ce4
DE
19634 /* If there is no parent die to provide a namespace, and there are
19635 children, see if we can determine the namespace from their linkage
122d1940 19636 name. */
3e9f1ca1 19637 if (cu->per_cu->lang == language_cplus
5e22e966 19638 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19639 && die_parent == NULL
19640 && has_children
19641 && (tag == DW_TAG_class_type
19642 || tag == DW_TAG_structure_type
19643 || tag == DW_TAG_union_type))
19644 guess_partial_die_structure_name (this, cu);
abc72ce4 19645
53832f31
TT
19646 /* GCC might emit a nameless struct or union that has a linkage
19647 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19648 if (raw_name == NULL
52356b79
YQ
19649 && (tag == DW_TAG_class_type
19650 || tag == DW_TAG_interface_type
19651 || tag == DW_TAG_structure_type
19652 || tag == DW_TAG_union_type)
19653 && linkage_name != NULL)
53832f31 19654 {
43816ebc
TT
19655 gdb::unique_xmalloc_ptr<char> demangled
19656 (gdb_demangle (linkage_name, DMGL_TYPES));
19657 if (demangled != nullptr)
53832f31 19658 {
96408a79
SA
19659 const char *base;
19660
19661 /* Strip any leading namespaces/classes, keep only the base name.
19662 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19663 base = strrchr (demangled.get (), ':');
19664 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19665 base++;
19666 else
43816ebc 19667 base = demangled.get ();
96408a79 19668
5e22e966 19669 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19670 raw_name = objfile->intern (base);
19671 canonical_name = 1;
53832f31
TT
19672 }
19673 }
19674
52356b79 19675 fixup_called = 1;
72bf9492
DJ
19676}
19677
d0ce17d8 19678/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
19679 contents from the given SECTION in the HEADER.
19680
19681 HEADER_OFFSET is the offset of the header in the section. */
41144253 19682static void
d0ce17d8 19683read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
19684 struct dwarf2_section_info *section,
19685 sect_offset header_offset)
41144253 19686{
19687 unsigned int bytes_read;
19688 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
19689 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19690
41144253 19691 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19692 info_ptr += bytes_read;
a1c40103 19693
41144253 19694 header->version = read_2_bytes (abfd, info_ptr);
19695 info_ptr += 2;
a1c40103 19696
41144253 19697 header->addr_size = read_1_byte (abfd, info_ptr);
19698 info_ptr += 1;
a1c40103 19699
41144253 19700 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19701 info_ptr += 1;
a1c40103 19702
41144253 19703 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19704}
19705
19706/* Return the DW_AT_loclists_base value for the CU. */
19707static ULONGEST
19708lookup_loclist_base (struct dwarf2_cu *cu)
19709{
19710 /* For the .dwo unit, the loclist_base points to the first offset following
19711 the header. The header consists of the following entities-
19712 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19713 bit format)
19714 2. version (2 bytes)
19715 3. address size (1 byte)
19716 4. segment selector size (1 byte)
19717 5. offset entry count (4 bytes)
19718 These sizes are derived as per the DWARFv5 standard. */
19719 if (cu->dwo_unit != nullptr)
19720 {
19721 if (cu->header.initial_length_size == 4)
19722 return LOCLIST_HEADER_SIZE32;
19723 return LOCLIST_HEADER_SIZE64;
19724 }
19725 return cu->loclist_base;
19726}
19727
19728/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19729 array of offsets in the .debug_loclists section. */
e57933dc
SM
19730
19731static sect_offset
41144253 19732read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19733{
976ca316
SM
19734 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19735 struct objfile *objfile = per_objfile->objfile;
41144253 19736 bfd *abfd = objfile->obfd;
a1c40103
SM
19737 ULONGEST loclist_header_size =
19738 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19739 : LOCLIST_HEADER_SIZE64);
41144253 19740 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
19741
19742 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19743 ULONGEST start_offset =
19744 loclist_base + loclist_index * cu->header.offset_size;
19745
a1c40103 19746 /* Get loclists section. */
41144253 19747 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19748
a1c40103 19749 /* Read the loclists section content. */
41144253 19750 section->read (objfile);
19751 if (section->buffer == NULL)
a0c1eeba
SM
19752 error (_("DW_FORM_loclistx used without .debug_loclists "
19753 "section [in module %s]"), objfile_name (objfile));
19754
a1c40103
SM
19755 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19756 so if loclist_base is smaller than the header size, we have a problem. */
19757 if (loclist_base < loclist_header_size)
19758 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19759 objfile_name (objfile));
19760
19761 /* Read the header of the loclists contribution. */
d0ce17d8 19762 struct loclists_rnglists_header header;
a1c40103
SM
19763 read_loclists_rnglists_header (&header, section,
19764 (sect_offset) (loclist_base - loclist_header_size));
19765
19766 /* Verify the loclist index is valid. */
41144253 19767 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
19768 error (_("DW_FORM_loclistx pointing outside of "
19769 ".debug_loclists offset array [in module %s]"),
19770 objfile_name (objfile));
19771
05787bad
SM
19772 /* Validate that reading won't go beyond the end of the section. */
19773 if (start_offset + cu->header.offset_size > section->size)
19774 error (_("Reading DW_FORM_loclistx index beyond end of"
19775 ".debug_loclists section [in module %s]"),
19776 objfile_name (objfile));
19777
19778 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 19779
19780 if (cu->header.offset_size == 4)
e57933dc 19781 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 19782 else
e57933dc 19783 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 19784}
19785
d0ce17d8
CT
19786/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19787 array of offsets in the .debug_rnglists section. */
e57933dc
SM
19788
19789static sect_offset
d0ce17d8
CT
19790read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19791 dwarf_tag tag)
19792{
19793 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19794 struct objfile *objfile = dwarf2_per_objfile->objfile;
19795 bfd *abfd = objfile->obfd;
19796 ULONGEST rnglist_header_size =
19797 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19798 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
19799
19800 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19801 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19802 doesn't apply. */
d0ce17d8 19803 ULONGEST rnglist_base =
2b0c7f41 19804 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
19805
19806 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
19807 ULONGEST start_offset =
19808 rnglist_base + rnglist_index * cu->header.offset_size;
19809
19810 /* Get rnglists section. */
19811 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19812
19813 /* Read the rnglists section content. */
19814 section->read (objfile);
19815 if (section->buffer == nullptr)
19816 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19817 "[in module %s]"),
19818 objfile_name (objfile));
19819
a1c40103
SM
19820 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19821 so if rnglist_base is smaller than the header size, we have a problem. */
19822 if (rnglist_base < rnglist_header_size)
19823 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19824 objfile_name (objfile));
19825
19826 /* Read the header of the rnglists contribution. */
d0ce17d8 19827 struct loclists_rnglists_header header;
a1c40103
SM
19828 read_loclists_rnglists_header (&header, section,
19829 (sect_offset) (rnglist_base - rnglist_header_size));
19830
19831 /* Verify the rnglist index is valid. */
d0ce17d8
CT
19832 if (rnglist_index >= header.offset_entry_count)
19833 error (_("DW_FORM_rnglistx index pointing outside of "
19834 ".debug_rnglists offset array [in module %s]"),
19835 objfile_name (objfile));
19836
d0ce17d8 19837 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 19838 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
19839 error (_("Reading DW_FORM_rnglistx index beyond end of"
19840 ".debug_rnglists section [in module %s]"),
19841 objfile_name (objfile));
19842
19843 const gdb_byte *info_ptr = section->buffer + start_offset;
19844
19845 if (cu->header.offset_size == 4)
e57933dc 19846 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 19847 else
e57933dc 19848 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
19849}
19850
18a8505e
AT
19851/* Process the attributes that had to be skipped in the first round. These
19852 attributes are the ones that need str_offsets_base or addr_base attributes.
19853 They could not have been processed in the first round, because at the time
19854 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19855static void
19856read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 19857 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
19858{
19859 struct dwarf2_cu *cu = reader->cu;
19860 switch (attr->form)
19861 {
19862 case DW_FORM_addrx:
19863 case DW_FORM_GNU_addr_index:
36d378cf
TT
19864 attr->set_address (read_addr_index (cu,
19865 attr->as_unsigned_reprocess ()));
dda83cd7 19866 break;
41144253 19867 case DW_FORM_loclistx:
b1829e1b 19868 {
e57933dc 19869 sect_offset loclists_sect_off
b1829e1b
SM
19870 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19871
e57933dc 19872 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
19873 }
19874 break;
d0ce17d8 19875 case DW_FORM_rnglistx:
b1829e1b 19876 {
e57933dc 19877 sect_offset rnglists_sect_off
b1829e1b
SM
19878 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19879
e57933dc 19880 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 19881 }
dda83cd7 19882 break;
18a8505e
AT
19883 case DW_FORM_strx:
19884 case DW_FORM_strx1:
19885 case DW_FORM_strx2:
19886 case DW_FORM_strx3:
19887 case DW_FORM_strx4:
19888 case DW_FORM_GNU_str_index:
19889 {
fe56917a 19890 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 19891 gdb_assert (!attr->canonical_string_p ());
18a8505e 19892 if (reader->dwo_file != NULL)
c6481205
TT
19893 attr->set_string_noncanonical (read_dwo_str_index (reader,
19894 str_index));
18a8505e 19895 else
c6481205
TT
19896 attr->set_string_noncanonical (read_stub_str_index (cu,
19897 str_index));
18a8505e
AT
19898 break;
19899 }
19900 default:
19901 gdb_assert_not_reached (_("Unexpected DWARF form."));
19902 }
19903}
19904
a8329558 19905/* Read an attribute value described by an attribute form. */
c906108c 19906
d521ce57 19907static const gdb_byte *
dee91e82
DE
19908read_attribute_value (const struct die_reader_specs *reader,
19909 struct attribute *attr, unsigned form,
7a5f294d 19910 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19911{
dee91e82 19912 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19913 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19914 struct objfile *objfile = per_objfile->objfile;
dee91e82 19915 bfd *abfd = reader->abfd;
e7c27a73 19916 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19917 unsigned int bytes_read;
19918 struct dwarf_block *blk;
19919
aead7601 19920 attr->form = (enum dwarf_form) form;
a8329558 19921 switch (form)
c906108c 19922 {
c906108c 19923 case DW_FORM_ref_addr:
7c290a04
TT
19924 if (cu_header->version == 2)
19925 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 19926 &bytes_read));
ae411497 19927 else
7c290a04 19928 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19929 &bytes_read));
ae411497
TT
19930 info_ptr += bytes_read;
19931 break;
36586728 19932 case DW_FORM_GNU_ref_alt:
7c290a04 19933 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19934 &bytes_read));
36586728
TT
19935 info_ptr += bytes_read;
19936 break;
ae411497 19937 case DW_FORM_addr:
08feed99
TT
19938 {
19939 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 19940 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
19941 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19942 attr->set_address (addr);
08feed99
TT
19943 info_ptr += bytes_read;
19944 }
c906108c
SS
19945 break;
19946 case DW_FORM_block2:
7b5a2f43 19947 blk = dwarf_alloc_block (cu);
c906108c
SS
19948 blk->size = read_2_bytes (abfd, info_ptr);
19949 info_ptr += 2;
19950 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19951 info_ptr += blk->size;
9d2246fc 19952 attr->set_block (blk);
c906108c
SS
19953 break;
19954 case DW_FORM_block4:
7b5a2f43 19955 blk = dwarf_alloc_block (cu);
c906108c
SS
19956 blk->size = read_4_bytes (abfd, info_ptr);
19957 info_ptr += 4;
19958 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19959 info_ptr += blk->size;
9d2246fc 19960 attr->set_block (blk);
c906108c
SS
19961 break;
19962 case DW_FORM_data2:
414ad644 19963 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
19964 info_ptr += 2;
19965 break;
19966 case DW_FORM_data4:
414ad644 19967 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
19968 info_ptr += 4;
19969 break;
19970 case DW_FORM_data8:
414ad644 19971 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
19972 info_ptr += 8;
19973 break;
0224619f
JK
19974 case DW_FORM_data16:
19975 blk = dwarf_alloc_block (cu);
19976 blk->size = 16;
19977 blk->data = read_n_bytes (abfd, info_ptr, 16);
19978 info_ptr += 16;
9d2246fc 19979 attr->set_block (blk);
0224619f 19980 break;
2dc7f7b3 19981 case DW_FORM_sec_offset:
7c290a04 19982 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19983 &bytes_read));
2dc7f7b3
TT
19984 info_ptr += bytes_read;
19985 break;
41144253 19986 case DW_FORM_loclistx:
19987 {
7a5f294d
TT
19988 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19989 &bytes_read));
19990 info_ptr += bytes_read;
41144253 19991 }
19992 break;
c906108c 19993 case DW_FORM_string:
c6481205
TT
19994 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19995 &bytes_read));
c906108c
SS
19996 info_ptr += bytes_read;
19997 break;
4bdf3d34 19998 case DW_FORM_strp:
36586728
TT
19999 if (!cu->per_cu->is_dwz)
20000 {
c6481205
TT
20001 attr->set_string_noncanonical
20002 (read_indirect_string (per_objfile,
20003 abfd, info_ptr, cu_header,
20004 &bytes_read));
36586728
TT
20005 info_ptr += bytes_read;
20006 break;
20007 }
20008 /* FALLTHROUGH */
43988095
JK
20009 case DW_FORM_line_strp:
20010 if (!cu->per_cu->is_dwz)
20011 {
c6481205
TT
20012 attr->set_string_noncanonical
20013 (per_objfile->read_line_string (info_ptr, cu_header,
20014 &bytes_read));
43988095
JK
20015 info_ptr += bytes_read;
20016 break;
20017 }
20018 /* FALLTHROUGH */
36586728
TT
20019 case DW_FORM_GNU_strp_alt:
20020 {
a7308ce0 20021 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
20022 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20023 &bytes_read);
36586728 20024
c6481205
TT
20025 attr->set_string_noncanonical
20026 (dwz->read_string (objfile, str_offset));
36586728
TT
20027 info_ptr += bytes_read;
20028 }
4bdf3d34 20029 break;
2dc7f7b3 20030 case DW_FORM_exprloc:
c906108c 20031 case DW_FORM_block:
7b5a2f43 20032 blk = dwarf_alloc_block (cu);
c906108c
SS
20033 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20034 info_ptr += bytes_read;
20035 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20036 info_ptr += blk->size;
9d2246fc 20037 attr->set_block (blk);
c906108c
SS
20038 break;
20039 case DW_FORM_block1:
7b5a2f43 20040 blk = dwarf_alloc_block (cu);
c906108c
SS
20041 blk->size = read_1_byte (abfd, info_ptr);
20042 info_ptr += 1;
20043 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20044 info_ptr += blk->size;
9d2246fc 20045 attr->set_block (blk);
c906108c
SS
20046 break;
20047 case DW_FORM_data1:
c906108c 20048 case DW_FORM_flag:
414ad644 20049 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20050 info_ptr += 1;
20051 break;
2dc7f7b3 20052 case DW_FORM_flag_present:
414ad644 20053 attr->set_unsigned (1);
2dc7f7b3 20054 break;
c906108c 20055 case DW_FORM_sdata:
1bc397c5 20056 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20057 info_ptr += bytes_read;
20058 break;
18a8505e 20059 case DW_FORM_rnglistx:
7a5f294d
TT
20060 {
20061 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20062 &bytes_read));
20063 info_ptr += bytes_read;
20064 }
20065 break;
d0ce17d8 20066 case DW_FORM_udata:
414ad644 20067 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20068 info_ptr += bytes_read;
20069 break;
20070 case DW_FORM_ref1:
7c290a04 20071 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20072 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20073 info_ptr += 1;
20074 break;
20075 case DW_FORM_ref2:
7c290a04 20076 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20077 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20078 info_ptr += 2;
20079 break;
20080 case DW_FORM_ref4:
7c290a04 20081 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20082 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20083 info_ptr += 4;
20084 break;
613e1657 20085 case DW_FORM_ref8:
7c290a04 20086 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20087 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20088 info_ptr += 8;
20089 break;
55f1336d 20090 case DW_FORM_ref_sig8:
630ed6b9 20091 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20092 info_ptr += 8;
20093 break;
c906108c 20094 case DW_FORM_ref_udata:
7c290a04 20095 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20096 + read_unsigned_leb128 (abfd, info_ptr,
20097 &bytes_read)));
c906108c
SS
20098 info_ptr += bytes_read;
20099 break;
c906108c 20100 case DW_FORM_indirect:
a8329558
KW
20101 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20102 info_ptr += bytes_read;
43988095
JK
20103 if (form == DW_FORM_implicit_const)
20104 {
20105 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20106 info_ptr += bytes_read;
20107 }
20108 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20109 info_ptr);
43988095
JK
20110 break;
20111 case DW_FORM_implicit_const:
1bc397c5 20112 attr->set_signed (implicit_const);
a8329558 20113 break;
336d760d 20114 case DW_FORM_addrx:
3019eac3 20115 case DW_FORM_GNU_addr_index:
fe56917a
TT
20116 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20117 &bytes_read));
3019eac3
DE
20118 info_ptr += bytes_read;
20119 break;
cf532bd1 20120 case DW_FORM_strx:
15f18d14
AT
20121 case DW_FORM_strx1:
20122 case DW_FORM_strx2:
20123 case DW_FORM_strx3:
20124 case DW_FORM_strx4:
3019eac3 20125 case DW_FORM_GNU_str_index:
3019eac3 20126 {
15f18d14
AT
20127 ULONGEST str_index;
20128 if (form == DW_FORM_strx1)
20129 {
20130 str_index = read_1_byte (abfd, info_ptr);
20131 info_ptr += 1;
20132 }
20133 else if (form == DW_FORM_strx2)
20134 {
20135 str_index = read_2_bytes (abfd, info_ptr);
20136 info_ptr += 2;
20137 }
20138 else if (form == DW_FORM_strx3)
20139 {
20140 str_index = read_3_bytes (abfd, info_ptr);
20141 info_ptr += 3;
20142 }
20143 else if (form == DW_FORM_strx4)
20144 {
20145 str_index = read_4_bytes (abfd, info_ptr);
20146 info_ptr += 4;
20147 }
20148 else
20149 {
20150 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20151 info_ptr += bytes_read;
20152 }
fe56917a 20153 attr->set_unsigned_reprocess (str_index);
7a5f294d 20154 }
3019eac3 20155 break;
c906108c 20156 default:
8a3fe4f8 20157 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20158 dwarf_form_name (form),
20159 bfd_get_filename (abfd));
c906108c 20160 }
28e94949 20161
36586728 20162 /* Super hack. */
cd6c91b4 20163 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20164 attr->form = DW_FORM_GNU_ref_alt;
20165
28e94949
JB
20166 /* We have seen instances where the compiler tried to emit a byte
20167 size attribute of -1 which ended up being encoded as an unsigned
20168 0xffffffff. Although 0xffffffff is technically a valid size value,
20169 an object of this size seems pretty unlikely so we can relatively
20170 safely treat these cases as if the size attribute was invalid and
20171 treat them as zero by default. */
20172 if (attr->name == DW_AT_byte_size
20173 && form == DW_FORM_data4
529908cb 20174 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20175 {
20176 complaint
dda83cd7
SM
20177 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20178 hex_string (attr->as_unsigned ()));
414ad644 20179 attr->set_unsigned (0);
01c66ae6 20180 }
28e94949 20181
c906108c
SS
20182 return info_ptr;
20183}
20184
a8329558
KW
20185/* Read an attribute described by an abbreviated attribute. */
20186
d521ce57 20187static const gdb_byte *
dee91e82 20188read_attribute (const struct die_reader_specs *reader,
4444f407 20189 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20190 const gdb_byte *info_ptr)
a8329558
KW
20191{
20192 attr->name = abbrev->name;
c6481205 20193 attr->string_is_canonical = 0;
fe56917a 20194 attr->requires_reprocessing = 0;
43988095 20195 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20196 abbrev->implicit_const, info_ptr);
a8329558
KW
20197}
20198
43988095
JK
20199/* Return pointer to string at .debug_str offset STR_OFFSET. */
20200
20201static const char *
976ca316 20202read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20203 LONGEST str_offset)
43988095 20204{
976ca316
SM
20205 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20206 str_offset, "DW_FORM_strp");
c906108c
SS
20207}
20208
43988095
JK
20209/* Return pointer to string at .debug_str offset as read from BUF.
20210 BUF is assumed to be in a compilation unit described by CU_HEADER.
20211 Return *BYTES_READ_PTR count of bytes read from BUF. */
20212
d521ce57 20213static const char *
976ca316 20214read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20215 const gdb_byte *buf,
cf2c3c16
TT
20216 const struct comp_unit_head *cu_header,
20217 unsigned int *bytes_read_ptr)
20218{
8266302d 20219 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20220
976ca316 20221 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20222}
20223
86c0bb4c 20224/* See read.h. */
43988095 20225
86c0bb4c
TT
20226const char *
20227dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20228 const struct comp_unit_head *cu_header,
20229 unsigned int *bytes_read_ptr)
43988095 20230{
86c0bb4c 20231 bfd *abfd = objfile->obfd;
8266302d 20232 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20233
5989a64e 20234 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20235}
20236
3019eac3 20237/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20238 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20239 ADDR_SIZE is the size of addresses from the CU header. */
20240
20241static CORE_ADDR
976ca316
SM
20242read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20243 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20244{
976ca316 20245 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20246 bfd *abfd = objfile->obfd;
20247 const gdb_byte *info_ptr;
18a8505e 20248 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20249
976ca316
SM
20250 per_objfile->per_bfd->addr.read (objfile);
20251 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20252 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20253 objfile_name (objfile));
18a8505e 20254 if (addr_base_or_zero + addr_index * addr_size
976ca316 20255 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20256 error (_("DW_FORM_addr_index pointing outside of "
20257 ".debug_addr section [in module %s]"),
4262abfb 20258 objfile_name (objfile));
976ca316
SM
20259 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20260 + addr_index * addr_size);
3019eac3
DE
20261 if (addr_size == 4)
20262 return bfd_get_32 (abfd, info_ptr);
20263 else
20264 return bfd_get_64 (abfd, info_ptr);
20265}
20266
20267/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20268
20269static CORE_ADDR
20270read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20271{
5e22e966 20272 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20273 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20274}
20275
20276/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20277
20278static CORE_ADDR
d521ce57 20279read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20280 unsigned int *bytes_read)
20281{
5e22e966 20282 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20283 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20284
20285 return read_addr_index (cu, addr_index);
20286}
20287
450a1bfc 20288/* See read.h. */
3019eac3
DE
20289
20290CORE_ADDR
82ca3f51 20291dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20292 dwarf2_per_objfile *per_objfile,
82ca3f51 20293 unsigned int addr_index)
3019eac3 20294{
976ca316 20295 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20296 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20297 int addr_size;
20298
3019eac3
DE
20299 /* We need addr_base and addr_size.
20300 If we don't have PER_CU->cu, we have to get it.
20301 Nasty, but the alternative is storing the needed info in PER_CU,
20302 which at this point doesn't seem justified: it's not clear how frequently
20303 it would get used and it would increase the size of every PER_CU.
20304 Entry points like dwarf2_per_cu_addr_size do a similar thing
20305 so we're not in uncharted territory here.
20306 Alas we need to be a bit more complicated as addr_base is contained
20307 in the DIE.
20308
20309 We don't need to read the entire CU(/TU).
20310 We just need the header and top level die.
a1b64ce1 20311
3019eac3 20312 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20313 For now we skip this optimization. */
3019eac3
DE
20314
20315 if (cu != NULL)
20316 {
20317 addr_base = cu->addr_base;
20318 addr_size = cu->header.addr_size;
20319 }
20320 else
20321 {
976ca316 20322 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20323 addr_base = reader.cu->addr_base;
20324 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20325 }
20326
976ca316 20327 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20328}
20329
18a8505e
AT
20330/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20331 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20332 DWO file. */
3019eac3 20333
d521ce57 20334static const char *
18a8505e
AT
20335read_str_index (struct dwarf2_cu *cu,
20336 struct dwarf2_section_info *str_section,
20337 struct dwarf2_section_info *str_offsets_section,
20338 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20339{
976ca316
SM
20340 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20341 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20342 const char *objf_name = objfile_name (objfile);
3019eac3 20343 bfd *abfd = objfile->obfd;
d521ce57 20344 const gdb_byte *info_ptr;
3019eac3 20345 ULONGEST str_offset;
cf532bd1 20346 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20347
96b79293
TT
20348 str_section->read (objfile);
20349 str_offsets_section->read (objfile);
73869dc2 20350 if (str_section->buffer == NULL)
18a8505e 20351 error (_("%s used without %s section"
9d8780f0 20352 " in CU at offset %s [in module %s]"),
96b79293 20353 form_name, str_section->get_name (),
dda83cd7 20354 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20355 if (str_offsets_section->buffer == NULL)
18a8505e 20356 error (_("%s used without %s section"
9d8780f0 20357 " in CU at offset %s [in module %s]"),
96b79293 20358 form_name, str_section->get_name (),
dda83cd7 20359 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20360 info_ptr = (str_offsets_section->buffer
18a8505e 20361 + str_offsets_base
3019eac3
DE
20362 + str_index * cu->header.offset_size);
20363 if (cu->header.offset_size == 4)
20364 str_offset = bfd_get_32 (abfd, info_ptr);
20365 else
20366 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20367 if (str_offset >= str_section->size)
57d63ce2 20368 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20369 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20370 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20371 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20372}
20373
18a8505e
AT
20374/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20375
20376static const char *
20377read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20378{
20379 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20380 ? reader->cu->header.addr_size : 0;
20381 return read_str_index (reader->cu,
20382 &reader->dwo_file->sections.str,
20383 &reader->dwo_file->sections.str_offsets,
20384 str_offsets_base, str_index);
20385}
20386
20387/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20388
20389static const char *
20390read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20391{
5e22e966 20392 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20393 const char *objf_name = objfile_name (objfile);
20394 static const char form_name[] = "DW_FORM_GNU_str_index";
20395 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20396
20397 if (!cu->str_offsets_base.has_value ())
20398 error (_("%s used in Fission stub without %s"
20399 " in CU at offset 0x%lx [in module %s]"),
20400 form_name, str_offsets_attr_name,
20401 (long) cu->header.offset_size, objf_name);
20402
20403 return read_str_index (cu,
5e22e966
SM
20404 &cu->per_objfile->per_bfd->str,
20405 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20406 *cu->str_offsets_base, str_index);
20407}
20408
3019eac3
DE
20409/* Return the length of an LEB128 number in BUF. */
20410
20411static int
20412leb128_size (const gdb_byte *buf)
20413{
20414 const gdb_byte *begin = buf;
20415 gdb_byte byte;
20416
20417 while (1)
20418 {
20419 byte = *buf++;
20420 if ((byte & 128) == 0)
20421 return buf - begin;
20422 }
20423}
20424
bf1dcdb3
TT
20425static enum language
20426dwarf_lang_to_enum_language (unsigned int lang)
c906108c 20427{
bf1dcdb3
TT
20428 enum language language;
20429
c906108c
SS
20430 switch (lang)
20431 {
20432 case DW_LANG_C89:
76bee0cc 20433 case DW_LANG_C99:
0cfd832f 20434 case DW_LANG_C11:
c906108c 20435 case DW_LANG_C:
d1be3247 20436 case DW_LANG_UPC:
bf1dcdb3 20437 language = language_c;
c906108c 20438 break;
9c37b5ae 20439 case DW_LANG_Java:
c906108c 20440 case DW_LANG_C_plus_plus:
0cfd832f
MW
20441 case DW_LANG_C_plus_plus_11:
20442 case DW_LANG_C_plus_plus_14:
bf1dcdb3 20443 language = language_cplus;
c906108c 20444 break;
6aecb9c2 20445 case DW_LANG_D:
bf1dcdb3 20446 language = language_d;
6aecb9c2 20447 break;
c906108c
SS
20448 case DW_LANG_Fortran77:
20449 case DW_LANG_Fortran90:
b21b22e0 20450 case DW_LANG_Fortran95:
f7de9aab
MW
20451 case DW_LANG_Fortran03:
20452 case DW_LANG_Fortran08:
bf1dcdb3 20453 language = language_fortran;
c906108c 20454 break;
a766d390 20455 case DW_LANG_Go:
bf1dcdb3 20456 language = language_go;
a766d390 20457 break;
c906108c 20458 case DW_LANG_Mips_Assembler:
bf1dcdb3 20459 language = language_asm;
c906108c
SS
20460 break;
20461 case DW_LANG_Ada83:
8aaf0b47 20462 case DW_LANG_Ada95:
bf1dcdb3 20463 language = language_ada;
bc5f45f8 20464 break;
72019c9c 20465 case DW_LANG_Modula2:
bf1dcdb3 20466 language = language_m2;
72019c9c 20467 break;
fe8e67fd 20468 case DW_LANG_Pascal83:
bf1dcdb3 20469 language = language_pascal;
fe8e67fd 20470 break;
22566fbd 20471 case DW_LANG_ObjC:
bf1dcdb3 20472 language = language_objc;
22566fbd 20473 break;
c44af4eb
TT
20474 case DW_LANG_Rust:
20475 case DW_LANG_Rust_old:
bf1dcdb3
TT
20476 language = language_rust;
20477 break;
20478 case DW_LANG_OpenCL:
20479 language = language_opencl;
c44af4eb 20480 break;
c906108c
SS
20481 case DW_LANG_Cobol74:
20482 case DW_LANG_Cobol85:
c906108c 20483 default:
bf1dcdb3 20484 language = language_minimal;
c906108c
SS
20485 break;
20486 }
bf1dcdb3
TT
20487
20488 return language;
c906108c
SS
20489}
20490
20491/* Return the named attribute or NULL if not there. */
20492
20493static struct attribute *
e142c38c 20494dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20495{
a48e046c 20496 for (;;)
c906108c 20497 {
a48e046c
TT
20498 unsigned int i;
20499 struct attribute *spec = NULL;
20500
20501 for (i = 0; i < die->num_attrs; ++i)
20502 {
20503 if (die->attrs[i].name == name)
20504 return &die->attrs[i];
20505 if (die->attrs[i].name == DW_AT_specification
20506 || die->attrs[i].name == DW_AT_abstract_origin)
20507 spec = &die->attrs[i];
20508 }
20509
20510 if (!spec)
20511 break;
c906108c 20512
f2f0e013 20513 die = follow_die_ref (die, spec, &cu);
f2f0e013 20514 }
c5aa993b 20515
c906108c
SS
20516 return NULL;
20517}
20518
7d45c7c3
KB
20519/* Return the string associated with a string-typed attribute, or NULL if it
20520 is either not found or is of an incorrect type. */
20521
20522static const char *
20523dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20524{
20525 struct attribute *attr;
20526 const char *str = NULL;
20527
20528 attr = dwarf2_attr (die, name, cu);
20529
20530 if (attr != NULL)
20531 {
95f982e5 20532 str = attr->as_string ();
e61108c9 20533 if (str == nullptr)
dda83cd7 20534 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20535 "DIE at %s in module %s"),
20536 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20537 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20538 }
20539
20540 return str;
20541}
20542
a084a2a6 20543/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20544 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20545static const char *
20546dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20547{
20548 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20549 if (dwo_name == nullptr)
20550 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20551 return dwo_name;
20552}
20553
05cf31d1
JB
20554/* Return non-zero iff the attribute NAME is defined for the given DIE,
20555 and holds a non-zero value. This function should only be used for
2dc7f7b3 20556 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20557
20558static int
20559dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20560{
20561 struct attribute *attr = dwarf2_attr (die, name, cu);
20562
c45bc3f8 20563 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20564}
20565
3ca72b44 20566static int
e142c38c 20567die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20568{
05cf31d1
JB
20569 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20570 which value is non-zero. However, we have to be careful with
20571 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20572 (via dwarf2_flag_true_p) follows this attribute. So we may
20573 end up accidently finding a declaration attribute that belongs
20574 to a different DIE referenced by the specification attribute,
20575 even though the given DIE does not have a declaration attribute. */
20576 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20577 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20578}
20579
63d06c5c 20580/* Return the die giving the specification for DIE, if there is
f2f0e013 20581 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20582 containing the return value on output. If there is no
20583 specification, but there is an abstract origin, that is
20584 returned. */
63d06c5c
DC
20585
20586static struct die_info *
f2f0e013 20587die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20588{
f2f0e013
DJ
20589 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20590 *spec_cu);
63d06c5c 20591
edb3359d
DJ
20592 if (spec_attr == NULL)
20593 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20594
63d06c5c
DC
20595 if (spec_attr == NULL)
20596 return NULL;
20597 else
f2f0e013 20598 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20599}
c906108c 20600
83769d0b 20601/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20602
20603static struct dwarf2_section_info *
20604get_debug_line_section (struct dwarf2_cu *cu)
20605{
20606 struct dwarf2_section_info *section;
976ca316 20607 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20608
20609 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20610 DWO file. */
20611 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20612 section = &cu->dwo_unit->dwo_file->sections.line;
20613 else if (cu->per_cu->is_dwz)
20614 {
a7308ce0 20615 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
20616
20617 section = &dwz->line;
20618 }
20619 else
976ca316 20620 section = &per_objfile->per_bfd->line;
36586728
TT
20621
20622 return section;
20623}
20624
debd256d 20625/* Read the statement program header starting at OFFSET in
3019eac3 20626 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20627 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20628 Returns NULL if there is a problem reading the header, e.g., if it
20629 has a version we don't understand.
debd256d
JB
20630
20631 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20632 the returned object point into the dwarf line section buffer,
20633 and must not be freed. */
ae2de4f8 20634
fff8551c 20635static line_header_up
9c541725 20636dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20637{
3019eac3 20638 struct dwarf2_section_info *section;
976ca316 20639 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20640
36586728 20641 section = get_debug_line_section (cu);
976ca316 20642 section->read (per_objfile->objfile);
3019eac3 20643 if (section->buffer == NULL)
debd256d 20644 {
3019eac3 20645 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20646 complaint (_("missing .debug_line.dwo section"));
3019eac3 20647 else
b98664d3 20648 complaint (_("missing .debug_line section"));
debd256d
JB
20649 return 0;
20650 }
20651
0df7ad3a 20652 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20653 per_objfile, section, &cu->header);
debd256d 20654}
c906108c 20655
c6da4cef 20656/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20657 Return the file name of the psymtab for the given file_entry.
c6da4cef 20658 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20659 If space for the result is malloc'd, *NAME_HOLDER will be set.
20660 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20661
d521ce57 20662static const char *
7ba99d21 20663psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20664 const dwarf2_psymtab *pst,
c89b44cd
TT
20665 const char *comp_dir,
20666 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20667{
d521ce57
TT
20668 const char *include_name = fe.name;
20669 const char *include_name_to_compare = include_name;
72b9f47f 20670 const char *pst_filename;
c6da4cef
DE
20671 int file_is_pst;
20672
8c43009f 20673 const char *dir_name = fe.include_dir (lh);
c6da4cef 20674
c89b44cd 20675 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20676 if (!IS_ABSOLUTE_PATH (include_name)
20677 && (dir_name != NULL || comp_dir != NULL))
20678 {
20679 /* Avoid creating a duplicate psymtab for PST.
20680 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20681 Before we do the comparison, however, we need to account
20682 for DIR_NAME and COMP_DIR.
20683 First prepend dir_name (if non-NULL). If we still don't
20684 have an absolute path prepend comp_dir (if non-NULL).
20685 However, the directory we record in the include-file's
20686 psymtab does not contain COMP_DIR (to match the
20687 corresponding symtab(s)).
20688
20689 Example:
20690
20691 bash$ cd /tmp
20692 bash$ gcc -g ./hello.c
20693 include_name = "hello.c"
20694 dir_name = "."
20695 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20696 DW_AT_name = "./hello.c"
20697
20698 */
c6da4cef
DE
20699
20700 if (dir_name != NULL)
20701 {
c89b44cd
TT
20702 name_holder->reset (concat (dir_name, SLASH_STRING,
20703 include_name, (char *) NULL));
20704 include_name = name_holder->get ();
c6da4cef 20705 include_name_to_compare = include_name;
c6da4cef
DE
20706 }
20707 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20708 {
c89b44cd
TT
20709 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20710 include_name, (char *) NULL));
20711 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20712 }
20713 }
20714
20715 pst_filename = pst->filename;
c89b44cd 20716 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20717 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20718 {
c89b44cd
TT
20719 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20720 pst_filename, (char *) NULL));
20721 pst_filename = copied_name.get ();
c6da4cef
DE
20722 }
20723
1e3fad37 20724 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20725
c6da4cef
DE
20726 if (file_is_pst)
20727 return NULL;
20728 return include_name;
20729}
20730
d9b3de22
DE
20731/* State machine to track the state of the line number program. */
20732
6f77053d 20733class lnp_state_machine
d9b3de22 20734{
6f77053d
PA
20735public:
20736 /* Initialize a machine state for the start of a line number
20737 program. */
804d2729
TT
20738 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20739 bool record_lines_p);
6f77053d 20740
8c43009f
PA
20741 file_entry *current_file ()
20742 {
20743 /* lh->file_names is 0-based, but the file name numbers in the
20744 statement program are 1-based. */
6f77053d
PA
20745 return m_line_header->file_name_at (m_file);
20746 }
20747
20748 /* Record the line in the state machine. END_SEQUENCE is true if
20749 we're processing the end of a sequence. */
20750 void record_line (bool end_sequence);
20751
a8caed5d 20752 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 20753 nop-out rest of the lines in this sequence. */
6f77053d
PA
20754 void check_line_address (struct dwarf2_cu *cu,
20755 const gdb_byte *line_ptr,
7ab6656f 20756 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20757
20758 void handle_set_discriminator (unsigned int discriminator)
20759 {
20760 m_discriminator = discriminator;
20761 m_line_has_non_zero_discriminator |= discriminator != 0;
20762 }
20763
20764 /* Handle DW_LNE_set_address. */
20765 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20766 {
20767 m_op_index = 0;
20768 address += baseaddr;
20769 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20770 }
20771
20772 /* Handle DW_LNS_advance_pc. */
20773 void handle_advance_pc (CORE_ADDR adjust);
20774
20775 /* Handle a special opcode. */
20776 void handle_special_opcode (unsigned char op_code);
20777
20778 /* Handle DW_LNS_advance_line. */
20779 void handle_advance_line (int line_delta)
20780 {
20781 advance_line (line_delta);
20782 }
20783
20784 /* Handle DW_LNS_set_file. */
20785 void handle_set_file (file_name_index file);
20786
20787 /* Handle DW_LNS_negate_stmt. */
20788 void handle_negate_stmt ()
20789 {
20790 m_is_stmt = !m_is_stmt;
20791 }
20792
20793 /* Handle DW_LNS_const_add_pc. */
20794 void handle_const_add_pc ();
20795
20796 /* Handle DW_LNS_fixed_advance_pc. */
20797 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20798 {
20799 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20800 m_op_index = 0;
20801 }
20802
20803 /* Handle DW_LNS_copy. */
20804 void handle_copy ()
20805 {
20806 record_line (false);
20807 m_discriminator = 0;
20808 }
20809
20810 /* Handle DW_LNE_end_sequence. */
20811 void handle_end_sequence ()
20812 {
804d2729 20813 m_currently_recording_lines = true;
6f77053d
PA
20814 }
20815
20816private:
20817 /* Advance the line by LINE_DELTA. */
20818 void advance_line (int line_delta)
20819 {
20820 m_line += line_delta;
20821
20822 if (line_delta != 0)
20823 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20824 }
20825
804d2729
TT
20826 struct dwarf2_cu *m_cu;
20827
6f77053d
PA
20828 gdbarch *m_gdbarch;
20829
20830 /* True if we're recording lines.
20831 Otherwise we're building partial symtabs and are just interested in
20832 finding include files mentioned by the line number program. */
20833 bool m_record_lines_p;
20834
8c43009f 20835 /* The line number header. */
6f77053d 20836 line_header *m_line_header;
8c43009f 20837
6f77053d
PA
20838 /* These are part of the standard DWARF line number state machine,
20839 and initialized according to the DWARF spec. */
d9b3de22 20840
6f77053d 20841 unsigned char m_op_index = 0;
7ba99d21
AT
20842 /* The line table index of the current file. */
20843 file_name_index m_file = 1;
6f77053d
PA
20844 unsigned int m_line = 1;
20845
20846 /* These are initialized in the constructor. */
20847
20848 CORE_ADDR m_address;
20849 bool m_is_stmt;
20850 unsigned int m_discriminator;
d9b3de22
DE
20851
20852 /* Additional bits of state we need to track. */
20853
20854 /* The last file that we called dwarf2_start_subfile for.
20855 This is only used for TLLs. */
6f77053d 20856 unsigned int m_last_file = 0;
d9b3de22 20857 /* The last file a line number was recorded for. */
6f77053d 20858 struct subfile *m_last_subfile = NULL;
d9b3de22 20859
1313c56e
AB
20860 /* The address of the last line entry. */
20861 CORE_ADDR m_last_address;
20862
20863 /* Set to true when a previous line at the same address (using
20864 m_last_address) had m_is_stmt true. This is reset to false when a
20865 line entry at a new address (m_address different to m_last_address) is
20866 processed. */
20867 bool m_stmt_at_address = false;
20868
804d2729
TT
20869 /* When true, record the lines we decode. */
20870 bool m_currently_recording_lines = false;
d9b3de22
DE
20871
20872 /* The last line number that was recorded, used to coalesce
20873 consecutive entries for the same line. This can happen, for
20874 example, when discriminators are present. PR 17276. */
6f77053d
PA
20875 unsigned int m_last_line = 0;
20876 bool m_line_has_non_zero_discriminator = false;
8c43009f 20877};
d9b3de22 20878
6f77053d
PA
20879void
20880lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20881{
20882 CORE_ADDR addr_adj = (((m_op_index + adjust)
20883 / m_line_header->maximum_ops_per_instruction)
20884 * m_line_header->minimum_instruction_length);
20885 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20886 m_op_index = ((m_op_index + adjust)
20887 % m_line_header->maximum_ops_per_instruction);
20888}
d9b3de22 20889
6f77053d
PA
20890void
20891lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20892{
6f77053d 20893 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20894 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20895 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20896 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20897 / m_line_header->maximum_ops_per_instruction)
20898 * m_line_header->minimum_instruction_length);
20899 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20900 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20901 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20902
258bf0ee 20903 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20904 advance_line (line_delta);
20905 record_line (false);
20906 m_discriminator = 0;
20907}
d9b3de22 20908
6f77053d
PA
20909void
20910lnp_state_machine::handle_set_file (file_name_index file)
20911{
20912 m_file = file;
20913
20914 const file_entry *fe = current_file ();
20915 if (fe == NULL)
20916 dwarf2_debug_line_missing_file_complaint ();
20917 else if (m_record_lines_p)
20918 {
20919 const char *dir = fe->include_dir (m_line_header);
20920
c24bdb02 20921 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20922 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20923 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20924 }
20925}
20926
20927void
20928lnp_state_machine::handle_const_add_pc ()
20929{
20930 CORE_ADDR adjust
20931 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20932
20933 CORE_ADDR addr_adj
20934 = (((m_op_index + adjust)
20935 / m_line_header->maximum_ops_per_instruction)
20936 * m_line_header->minimum_instruction_length);
20937
20938 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20939 m_op_index = ((m_op_index + adjust)
20940 % m_line_header->maximum_ops_per_instruction);
20941}
d9b3de22 20942
a05a36a5
DE
20943/* Return non-zero if we should add LINE to the line number table.
20944 LINE is the line to add, LAST_LINE is the last line that was added,
20945 LAST_SUBFILE is the subfile for LAST_LINE.
20946 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20947 had a non-zero discriminator.
20948
20949 We have to be careful in the presence of discriminators.
20950 E.g., for this line:
20951
20952 for (i = 0; i < 100000; i++);
20953
20954 clang can emit four line number entries for that one line,
20955 each with a different discriminator.
20956 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20957
20958 However, we want gdb to coalesce all four entries into one.
20959 Otherwise the user could stepi into the middle of the line and
20960 gdb would get confused about whether the pc really was in the
20961 middle of the line.
20962
20963 Things are further complicated by the fact that two consecutive
20964 line number entries for the same line is a heuristic used by gcc
20965 to denote the end of the prologue. So we can't just discard duplicate
20966 entries, we have to be selective about it. The heuristic we use is
20967 that we only collapse consecutive entries for the same line if at least
20968 one of those entries has a non-zero discriminator. PR 17276.
20969
20970 Note: Addresses in the line number state machine can never go backwards
20971 within one sequence, thus this coalescing is ok. */
20972
20973static int
804d2729
TT
20974dwarf_record_line_p (struct dwarf2_cu *cu,
20975 unsigned int line, unsigned int last_line,
a05a36a5
DE
20976 int line_has_non_zero_discriminator,
20977 struct subfile *last_subfile)
20978{
c24bdb02 20979 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20980 return 1;
20981 if (line != last_line)
20982 return 1;
20983 /* Same line for the same file that we've seen already.
20984 As a last check, for pr 17276, only record the line if the line
20985 has never had a non-zero discriminator. */
20986 if (!line_has_non_zero_discriminator)
20987 return 1;
20988 return 0;
20989}
20990
804d2729
TT
20991/* Use the CU's builder to record line number LINE beginning at
20992 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20993
20994static void
d9b3de22 20995dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20996 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20997 struct dwarf2_cu *cu)
252a6764
DE
20998{
20999 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21000
27e0867f
DE
21001 if (dwarf_line_debug)
21002 {
21003 fprintf_unfiltered (gdb_stdlog,
21004 "Recording line %u, file %s, address %s\n",
21005 line, lbasename (subfile->name),
21006 paddress (gdbarch, address));
21007 }
21008
804d2729 21009 if (cu != nullptr)
8c95582d 21010 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21011}
21012
21013/* Subroutine of dwarf_decode_lines_1 to simplify it.
21014 Mark the end of a set of line number records.
d9b3de22 21015 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21016 If SUBFILE is NULL the request is ignored. */
21017
21018static void
21019dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21020 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21021{
27e0867f
DE
21022 if (subfile == NULL)
21023 return;
21024
21025 if (dwarf_line_debug)
21026 {
21027 fprintf_unfiltered (gdb_stdlog,
21028 "Finishing current line, file %s, address %s\n",
21029 lbasename (subfile->name),
21030 paddress (gdbarch, address));
21031 }
21032
8c95582d 21033 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21034}
21035
6f77053d
PA
21036void
21037lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21038{
d9b3de22
DE
21039 if (dwarf_line_debug)
21040 {
21041 fprintf_unfiltered (gdb_stdlog,
21042 "Processing actual line %u: file %u,"
94a72be7 21043 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21044 m_line, m_file,
6f77053d 21045 paddress (m_gdbarch, m_address),
94a72be7
AB
21046 m_is_stmt, m_discriminator,
21047 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21048 }
21049
6f77053d 21050 file_entry *fe = current_file ();
8c43009f
PA
21051
21052 if (fe == NULL)
d9b3de22
DE
21053 dwarf2_debug_line_missing_file_complaint ();
21054 /* For now we ignore lines not starting on an instruction boundary.
21055 But not when processing end_sequence for compatibility with the
21056 previous version of the code. */
6f77053d 21057 else if (m_op_index == 0 || end_sequence)
d9b3de22 21058 {
8c43009f 21059 fe->included_p = 1;
8c95582d 21060 if (m_record_lines_p)
d9b3de22 21061 {
1313c56e
AB
21062 /* When we switch files we insert an end maker in the first file,
21063 switch to the second file and add a new line entry. The
21064 problem is that the end marker inserted in the first file will
21065 discard any previous line entries at the same address. If the
21066 line entries in the first file are marked as is-stmt, while
21067 the new line in the second file is non-stmt, then this means
21068 the end marker will discard is-stmt lines so we can have a
21069 non-stmt line. This means that there are less addresses at
21070 which the user can insert a breakpoint.
21071
21072 To improve this we track the last address in m_last_address,
21073 and whether we have seen an is-stmt at this address. Then
21074 when switching files, if we have seen a stmt at the current
21075 address, and we are switching to create a non-stmt line, then
21076 discard the new line. */
21077 bool file_changed
21078 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21079 bool ignore_this_line
dda83cd7
SM
21080 = ((file_changed && !end_sequence && m_last_address == m_address
21081 && !m_is_stmt && m_stmt_at_address)
21082 || (!end_sequence && m_line == 0));
1313c56e
AB
21083
21084 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21085 {
804d2729
TT
21086 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21087 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21088 }
21089
1313c56e 21090 if (!end_sequence && !ignore_this_line)
d9b3de22 21091 {
8c95582d
AB
21092 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21093
804d2729 21094 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21095 m_line_has_non_zero_discriminator,
21096 m_last_subfile))
d9b3de22 21097 {
c24bdb02 21098 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21099 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21100 builder->get_current_subfile (),
8c95582d 21101 m_line, m_address, is_stmt,
804d2729 21102 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21103 }
c24bdb02 21104 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21105 m_last_line = m_line;
d9b3de22
DE
21106 }
21107 }
21108 }
1313c56e
AB
21109
21110 /* Track whether we have seen any m_is_stmt true at m_address in case we
21111 have multiple line table entries all at m_address. */
21112 if (m_last_address != m_address)
21113 {
21114 m_stmt_at_address = false;
21115 m_last_address = m_address;
21116 }
21117 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21118}
21119
804d2729
TT
21120lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21121 line_header *lh, bool record_lines_p)
d9b3de22 21122{
804d2729 21123 m_cu = cu;
6f77053d
PA
21124 m_gdbarch = arch;
21125 m_record_lines_p = record_lines_p;
21126 m_line_header = lh;
d9b3de22 21127
804d2729 21128 m_currently_recording_lines = true;
d9b3de22 21129
d9b3de22
DE
21130 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21131 was a line entry for it so that the backend has a chance to adjust it
21132 and also record it in case it needs it. This is currently used by MIPS
21133 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21134 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21135 m_is_stmt = lh->default_is_stmt;
21136 m_discriminator = 0;
1313c56e
AB
21137
21138 m_last_address = m_address;
21139 m_stmt_at_address = false;
252a6764
DE
21140}
21141
6f77053d
PA
21142void
21143lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21144 const gdb_byte *line_ptr,
7ab6656f 21145 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21146{
a8caed5d
FS
21147 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21148 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21149 located at 0x0. In this case, additionally check that if
21150 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21151
a8caed5d
FS
21152 if ((address == 0 && address < unrelocated_lowpc)
21153 || address == (CORE_ADDR) -1)
924c2928
DE
21154 {
21155 /* This line table is for a function which has been
21156 GCd by the linker. Ignore it. PR gdb/12528 */
21157
5e22e966 21158 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21159 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21160
b98664d3 21161 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21162 line_offset, objfile_name (objfile));
804d2729
TT
21163 m_currently_recording_lines = false;
21164 /* Note: m_currently_recording_lines is left as false until we see
21165 DW_LNE_end_sequence. */
924c2928
DE
21166 }
21167}
21168
f3f5162e 21169/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21170 Process the line number information in LH.
21171 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21172 program in order to set included_p for every referenced header. */
debd256d 21173
c906108c 21174static void
43f3e411
DE
21175dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21176 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21177{
d521ce57
TT
21178 const gdb_byte *line_ptr, *extended_end;
21179 const gdb_byte *line_end;
a8c50c1f 21180 unsigned int bytes_read, extended_len;
699ca60a 21181 unsigned char op_code, extended_op;
e142c38c 21182 CORE_ADDR baseaddr;
5e22e966 21183 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21184 bfd *abfd = objfile->obfd;
08feed99 21185 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21186 /* True if we're recording line info (as opposed to building partial
21187 symtabs and just interested in finding include files mentioned by
21188 the line number program). */
21189 bool record_lines_p = !decode_for_pst_p;
e142c38c 21190
b3b3bada 21191 baseaddr = objfile->text_section_offset ();
c906108c 21192
debd256d
JB
21193 line_ptr = lh->statement_program_start;
21194 line_end = lh->statement_program_end;
c906108c
SS
21195
21196 /* Read the statement sequences until there's nothing left. */
21197 while (line_ptr < line_end)
21198 {
6f77053d
PA
21199 /* The DWARF line number program state machine. Reset the state
21200 machine at the start of each sequence. */
804d2729 21201 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21202 bool end_sequence = false;
d9b3de22 21203
8c43009f 21204 if (record_lines_p)
c906108c 21205 {
8c43009f
PA
21206 /* Start a subfile for the current file of the state
21207 machine. */
21208 const file_entry *fe = state_machine.current_file ();
21209
21210 if (fe != NULL)
804d2729 21211 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21212 }
21213
a738430d 21214 /* Decode the table. */
d9b3de22 21215 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21216 {
21217 op_code = read_1_byte (abfd, line_ptr);
21218 line_ptr += 1;
9aa1fe7e 21219
debd256d 21220 if (op_code >= lh->opcode_base)
6e70227d 21221 {
8e07a239 21222 /* Special opcode. */
6f77053d 21223 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21224 }
21225 else switch (op_code)
c906108c
SS
21226 {
21227 case DW_LNS_extended_op:
3e43a32a
MS
21228 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21229 &bytes_read);
473b7be6 21230 line_ptr += bytes_read;
a8c50c1f 21231 extended_end = line_ptr + extended_len;
c906108c
SS
21232 extended_op = read_1_byte (abfd, line_ptr);
21233 line_ptr += 1;
8f34b746
TV
21234 if (DW_LNE_lo_user <= extended_op
21235 && extended_op <= DW_LNE_hi_user)
21236 {
21237 /* Vendor extension, ignore. */
21238 line_ptr = extended_end;
21239 break;
21240 }
c906108c
SS
21241 switch (extended_op)
21242 {
21243 case DW_LNE_end_sequence:
6f77053d
PA
21244 state_machine.handle_end_sequence ();
21245 end_sequence = true;
c906108c
SS
21246 break;
21247 case DW_LNE_set_address:
d9b3de22
DE
21248 {
21249 CORE_ADDR address
c8a7a66f 21250 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21251 line_ptr += bytes_read;
6f77053d
PA
21252
21253 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21254 lowpc - baseaddr, address);
6f77053d 21255 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21256 }
c906108c
SS
21257 break;
21258 case DW_LNE_define_file:
dda83cd7
SM
21259 {
21260 const char *cur_file;
ecfb656c
PA
21261 unsigned int mod_time, length;
21262 dir_index dindex;
6e70227d 21263
dda83cd7 21264 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21265 &bytes_read);
dda83cd7
SM
21266 line_ptr += bytes_read;
21267 dindex = (dir_index)
21268 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21269 line_ptr += bytes_read;
21270 mod_time =
21271 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21272 line_ptr += bytes_read;
21273 length =
21274 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21275 line_ptr += bytes_read;
21276 lh->add_file_name (cur_file, dindex, mod_time, length);
21277 }
c906108c 21278 break;
d0c6ba3d 21279 case DW_LNE_set_discriminator:
6f77053d
PA
21280 {
21281 /* The discriminator is not interesting to the
21282 debugger; just ignore it. We still need to
21283 check its value though:
21284 if there are consecutive entries for the same
21285 (non-prologue) line we want to coalesce them.
21286 PR 17276. */
21287 unsigned int discr
21288 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21289 line_ptr += bytes_read;
21290
21291 state_machine.handle_set_discriminator (discr);
21292 }
d0c6ba3d 21293 break;
c906108c 21294 default:
b98664d3 21295 complaint (_("mangled .debug_line section"));
debd256d 21296 return;
c906108c 21297 }
a8c50c1f
DJ
21298 /* Make sure that we parsed the extended op correctly. If e.g.
21299 we expected a different address size than the producer used,
21300 we may have read the wrong number of bytes. */
21301 if (line_ptr != extended_end)
21302 {
b98664d3 21303 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21304 return;
21305 }
c906108c
SS
21306 break;
21307 case DW_LNS_copy:
6f77053d 21308 state_machine.handle_copy ();
c906108c
SS
21309 break;
21310 case DW_LNS_advance_pc:
2dc7f7b3
TT
21311 {
21312 CORE_ADDR adjust
21313 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21314 line_ptr += bytes_read;
6f77053d
PA
21315
21316 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21317 }
c906108c
SS
21318 break;
21319 case DW_LNS_advance_line:
a05a36a5
DE
21320 {
21321 int line_delta
21322 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21323 line_ptr += bytes_read;
6f77053d
PA
21324
21325 state_machine.handle_advance_line (line_delta);
a05a36a5 21326 }
c906108c
SS
21327 break;
21328 case DW_LNS_set_file:
d9b3de22 21329 {
6f77053d 21330 file_name_index file
ecfb656c
PA
21331 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21332 &bytes_read);
d9b3de22 21333 line_ptr += bytes_read;
8c43009f 21334
6f77053d 21335 state_machine.handle_set_file (file);
d9b3de22 21336 }
c906108c
SS
21337 break;
21338 case DW_LNS_set_column:
0ad93d4f 21339 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21340 line_ptr += bytes_read;
21341 break;
21342 case DW_LNS_negate_stmt:
6f77053d 21343 state_machine.handle_negate_stmt ();
c906108c
SS
21344 break;
21345 case DW_LNS_set_basic_block:
c906108c 21346 break;
c2c6d25f
JM
21347 /* Add to the address register of the state machine the
21348 address increment value corresponding to special opcode
a738430d
MK
21349 255. I.e., this value is scaled by the minimum
21350 instruction length since special opcode 255 would have
b021a221 21351 scaled the increment. */
c906108c 21352 case DW_LNS_const_add_pc:
6f77053d 21353 state_machine.handle_const_add_pc ();
c906108c
SS
21354 break;
21355 case DW_LNS_fixed_advance_pc:
3e29f34a 21356 {
6f77053d 21357 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21358 line_ptr += 2;
6f77053d
PA
21359
21360 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21361 }
c906108c 21362 break;
9aa1fe7e 21363 default:
a738430d
MK
21364 {
21365 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21366 int i;
a738430d 21367
debd256d 21368 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21369 {
21370 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21371 line_ptr += bytes_read;
21372 }
21373 }
c906108c
SS
21374 }
21375 }
d9b3de22
DE
21376
21377 if (!end_sequence)
21378 dwarf2_debug_line_missing_end_sequence_complaint ();
21379
21380 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21381 in which case we still finish recording the last line). */
6f77053d 21382 state_machine.record_line (true);
c906108c 21383 }
f3f5162e
DE
21384}
21385
21386/* Decode the Line Number Program (LNP) for the given line_header
21387 structure and CU. The actual information extracted and the type
21388 of structures created from the LNP depends on the value of PST.
21389
21390 1. If PST is NULL, then this procedure uses the data from the program
21391 to create all necessary symbol tables, and their linetables.
21392
21393 2. If PST is not NULL, this procedure reads the program to determine
21394 the list of files included by the unit represented by PST, and
21395 builds all the associated partial symbol tables.
21396
21397 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21398 It is used for relative paths in the line table.
21399 NOTE: When processing partial symtabs (pst != NULL),
21400 comp_dir == pst->dirname.
21401
21402 NOTE: It is important that psymtabs have the same file name (via strcmp)
21403 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21404 symtab we don't use it in the name of the psymtabs we create.
21405 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21406 A good testcase for this is mb-inline.exp.
21407
527f3840
JK
21408 LOWPC is the lowest address in CU (or 0 if not known).
21409
21410 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21411 for its PC<->lines mapping information. Otherwise only the filename
21412 table is read in. */
f3f5162e
DE
21413
21414static void
21415dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21416 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21417 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21418{
5e22e966 21419 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21420 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21421
527f3840
JK
21422 if (decode_mapping)
21423 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21424
21425 if (decode_for_pst_p)
21426 {
aaa75496 21427 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21428 create the psymtab of each included file. */
7ba99d21 21429 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21430 if (file_entry.included_p == 1)
21431 {
c89b44cd 21432 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21433 const char *include_name =
7ba99d21
AT
21434 psymtab_include_file_name (lh, file_entry, pst,
21435 comp_dir, &name_holder);
c6da4cef 21436 if (include_name != NULL)
7b249e47
TT
21437 dwarf2_create_include_psymtab
21438 (cu->per_objfile->per_bfd, include_name, pst,
21439 cu->per_objfile->per_bfd->partial_symtabs.get (),
0072c873 21440 objfile->per_bfd);
dda83cd7 21441 }
aaa75496 21442 }
cb1df416
DJ
21443 else
21444 {
21445 /* Make sure a symtab is created for every file, even files
21446 which contain only variables (i.e. no code with associated
21447 line numbers). */
c24bdb02
KS
21448 buildsym_compunit *builder = cu->get_builder ();
21449 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21450
7ba99d21 21451 for (auto &fe : lh->file_names ())
cb1df416 21452 {
804d2729 21453 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21454 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21455 {
c24bdb02 21456 builder->get_current_subfile ()->symtab
804d2729 21457 = allocate_symtab (cust,
c24bdb02 21458 builder->get_current_subfile ()->name);
43f3e411 21459 }
c24bdb02 21460 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21461 }
21462 }
c906108c
SS
21463}
21464
21465/* Start a subfile for DWARF. FILENAME is the name of the file and
21466 DIRNAME the name of the source directory which contains FILENAME
4d663531 21467 or NULL if not known.
c906108c
SS
21468 This routine tries to keep line numbers from identical absolute and
21469 relative file names in a common subfile.
21470
21471 Using the `list' example from the GDB testsuite, which resides in
21472 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21473 of /srcdir/list0.c yields the following debugging information for list0.c:
21474
c5aa993b 21475 DW_AT_name: /srcdir/list0.c
4d663531 21476 DW_AT_comp_dir: /compdir
357e46e7 21477 files.files[0].name: list0.h
c5aa993b 21478 files.files[0].dir: /srcdir
357e46e7 21479 files.files[1].name: list0.c
c5aa993b 21480 files.files[1].dir: /srcdir
c906108c
SS
21481
21482 The line number information for list0.c has to end up in a single
4f1520fb
FR
21483 subfile, so that `break /srcdir/list0.c:1' works as expected.
21484 start_subfile will ensure that this happens provided that we pass the
21485 concatenation of files.files[1].dir and files.files[1].name as the
21486 subfile's name. */
c906108c
SS
21487
21488static void
804d2729
TT
21489dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21490 const char *dirname)
c906108c 21491{
43816ebc 21492 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21493
4d663531 21494 /* In order not to lose the line information directory,
4f1520fb
FR
21495 we concatenate it to the filename when it makes sense.
21496 Note that the Dwarf3 standard says (speaking of filenames in line
21497 information): ``The directory index is ignored for file names
21498 that represent full path names''. Thus ignoring dirname in the
21499 `else' branch below isn't an issue. */
c906108c 21500
d5166ae1 21501 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21502 {
43816ebc
TT
21503 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21504 filename = copy.get ();
d521ce57 21505 }
c906108c 21506
c24bdb02 21507 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21508}
21509
4c2df51b
DJ
21510static void
21511var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21512 struct dwarf2_cu *cu)
4c2df51b 21513{
5e22e966 21514 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21515 struct comp_unit_head *cu_header = &cu->header;
21516
4c2df51b
DJ
21517 /* NOTE drow/2003-01-30: There used to be a comment and some special
21518 code here to turn a symbol with DW_AT_external and a
21519 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21520 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21521 with some versions of binutils) where shared libraries could have
21522 relocations against symbols in their debug information - the
21523 minimal symbol would have the right address, but the debug info
21524 would not. It's no longer necessary, because we will explicitly
21525 apply relocations when we read in the debug information now. */
21526
21527 /* A DW_AT_location attribute with no contents indicates that a
21528 variable has been optimized away. */
9d2246fc 21529 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21530 {
f1e6e072 21531 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21532 return;
21533 }
21534
21535 /* Handle one degenerate form of location expression specially, to
21536 preserve GDB's previous behavior when section offsets are
336d760d
AT
21537 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21538 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21539
9d2246fc
TT
21540 if (attr->form_is_block ())
21541 {
21542 struct dwarf_block *block = attr->as_block ();
21543
21544 if ((block->data[0] == DW_OP_addr
21545 && block->size == 1 + cu_header->addr_size)
21546 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21547 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21548 && (block->size
21549 == 1 + leb128_size (&block->data[1]))))
21550 {
21551 unsigned int dummy;
21552
21553 if (block->data[0] == DW_OP_addr)
21554 SET_SYMBOL_VALUE_ADDRESS
21555 (sym, cu->header.read_address (objfile->obfd,
21556 block->data + 1,
38583298 21557 &dummy));
9d2246fc
TT
21558 else
21559 SET_SYMBOL_VALUE_ADDRESS
21560 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21561 &dummy));
21562 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21563 fixup_symbol_section (sym, objfile);
21564 SET_SYMBOL_VALUE_ADDRESS
21565 (sym,
21566 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 21567 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
21568 return;
21569 }
4c2df51b
DJ
21570 }
21571
21572 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21573 expression evaluator, and use LOC_COMPUTED only when necessary
21574 (i.e. when the value of a register or memory location is
21575 referenced, or a thread-local block, etc.). Then again, it might
21576 not be worthwhile. I'm assuming that it isn't unless performance
21577 or memory numbers show me otherwise. */
21578
f1e6e072 21579 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21580
f1e6e072 21581 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21582 cu->has_loclist = true;
4c2df51b
DJ
21583}
21584
c906108c
SS
21585/* Given a pointer to a DWARF information entry, figure out if we need
21586 to make a symbol table entry for it, and if so, create a new entry
21587 and return a pointer to it.
21588 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21589 used the passed type.
21590 If SPACE is not NULL, use it to hold the new symbol. If it is
21591 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21592
21593static struct symbol *
5e2db402
TT
21594new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21595 struct symbol *space)
c906108c 21596{
976ca316
SM
21597 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21598 struct objfile *objfile = per_objfile->objfile;
08feed99 21599 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21600 struct symbol *sym = NULL;
15d034d0 21601 const char *name;
c906108c
SS
21602 struct attribute *attr = NULL;
21603 struct attribute *attr2 = NULL;
e142c38c 21604 CORE_ADDR baseaddr;
e37fd15a
SW
21605 struct pending **list_to_add = NULL;
21606
edb3359d 21607 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21608
b3b3bada 21609 baseaddr = objfile->text_section_offset ();
c906108c 21610
94af9270 21611 name = dwarf2_name (die, cu);
c906108c
SS
21612 if (name)
21613 {
34eaf542 21614 int suppress_add = 0;
94af9270 21615
34eaf542
TT
21616 if (space)
21617 sym = space;
21618 else
8c14c3a3 21619 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21620 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21621
21622 /* Cache this symbol's name and the name's demangled form (if any). */
3e9f1ca1 21623 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
f55ee35c
JK
21624 /* Fortran does not have mangling standard and the mangling does differ
21625 between gfortran, iFort etc. */
bcfe6157 21626 const char *physname
3e9f1ca1 21627 = (cu->per_cu->lang == language_fortran
bcfe6157
TT
21628 ? dwarf2_full_name (name, die, cu)
21629 : dwarf2_physname (name, die, cu));
21630 const char *linkagename = dw2_linkage_name (die, cu);
21631
3e9f1ca1 21632 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
bcfe6157
TT
21633 sym->set_linkage_name (physname);
21634 else
21635 {
21636 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21637 sym->set_linkage_name (linkagename);
21638 }
f55ee35c 21639
c906108c 21640 /* Default assumptions.
dda83cd7 21641 Use the passed type or decode it from the die. */
176620f1 21642 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21643 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21644 if (type != NULL)
21645 SYMBOL_TYPE (sym) = type;
21646 else
e7c27a73 21647 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21648 attr = dwarf2_attr (die,
21649 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21650 cu);
435d3d88 21651 if (attr != nullptr)
529908cb 21652 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 21653
edb3359d
DJ
21654 attr = dwarf2_attr (die,
21655 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21656 cu);
aa659cfa 21657 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 21658 {
529908cb 21659 file_name_index file_index
aa659cfa 21660 = (file_name_index) attr->as_nonnegative ();
8c43009f 21661 struct file_entry *fe;
9a619af0 21662
ecfb656c
PA
21663 if (cu->line_header != NULL)
21664 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21665 else
21666 fe = NULL;
21667
21668 if (fe == NULL)
b98664d3 21669 complaint (_("file index out of range"));
8c43009f
PA
21670 else
21671 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21672 }
21673
c906108c
SS
21674 switch (die->tag)
21675 {
21676 case DW_TAG_label:
e142c38c 21677 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21678 if (attr != nullptr)
3e29f34a
MR
21679 {
21680 CORE_ADDR addr;
21681
95f982e5 21682 addr = attr->as_address ();
3e29f34a 21683 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21684 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 21685 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 21686 }
8f5c6526
TV
21687 else
21688 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
21689 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21690 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 21691 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21692 break;
21693 case DW_TAG_subprogram:
21694 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21695 finish_block. */
f1e6e072 21696 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21697 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 21698 if ((attr2 != nullptr && attr2->as_boolean ())
3e9f1ca1
TT
21699 || cu->per_cu->lang == language_ada
21700 || cu->per_cu->lang == language_fortran)
c906108c 21701 {
dda83cd7
SM
21702 /* Subprograms marked external are stored as a global symbol.
21703 Ada and Fortran subprograms, whether marked external or
21704 not, are always stored as a global symbol, because we want
21705 to be able to access them globally. For instance, we want
21706 to be able to break on a nested subprogram without having
21707 to specify the context. */
c24bdb02 21708 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21709 }
21710 else
21711 {
e37fd15a 21712 list_to_add = cu->list_in_scope;
c906108c
SS
21713 }
21714 break;
edb3359d
DJ
21715 case DW_TAG_inlined_subroutine:
21716 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21717 finish_block. */
f1e6e072 21718 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21719 SYMBOL_INLINED (sym) = 1;
481860b3 21720 list_to_add = cu->list_in_scope;
edb3359d 21721 break;
34eaf542
TT
21722 case DW_TAG_template_value_param:
21723 suppress_add = 1;
21724 /* Fall through. */
72929c62 21725 case DW_TAG_constant:
c906108c 21726 case DW_TAG_variable:
254e6b9e 21727 case DW_TAG_member:
0963b4bd
MS
21728 /* Compilation with minimal debug info may result in
21729 variables with missing type entries. Change the
21730 misleading `void' type to something sensible. */
78134374 21731 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 21732 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21733
e142c38c 21734 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21735 /* In the case of DW_TAG_member, we should only be called for
21736 static const members. */
21737 if (die->tag == DW_TAG_member)
21738 {
3863f96c
DE
21739 /* dwarf2_add_field uses die_is_declaration,
21740 so we do the same. */
254e6b9e
DE
21741 gdb_assert (die_is_declaration (die, cu));
21742 gdb_assert (attr);
21743 }
435d3d88 21744 if (attr != nullptr)
c906108c 21745 {
e7c27a73 21746 dwarf2_const_value (attr, sym, cu);
e142c38c 21747 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21748 if (!suppress_add)
34eaf542 21749 {
c45bc3f8 21750 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 21751 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21752 else
e37fd15a 21753 list_to_add = cu->list_in_scope;
34eaf542 21754 }
c906108c
SS
21755 break;
21756 }
e142c38c 21757 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21758 if (attr != nullptr)
c906108c 21759 {
e7c27a73 21760 var_decode_location (attr, sym, cu);
e142c38c 21761 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21762
21763 /* Fortran explicitly imports any global symbols to the local
21764 scope by DW_TAG_common_block. */
3e9f1ca1 21765 if (cu->per_cu->lang == language_fortran && die->parent
4357ac6c
TT
21766 && die->parent->tag == DW_TAG_common_block)
21767 attr2 = NULL;
21768
caac4577
JG
21769 if (SYMBOL_CLASS (sym) == LOC_STATIC
21770 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 21771 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
21772 {
21773 /* When a static variable is eliminated by the linker,
21774 the corresponding debug information is not stripped
21775 out, but the variable address is set to null;
21776 do not add such variables into symbol table. */
21777 }
c45bc3f8 21778 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 21779 {
4b610737
TT
21780 if (SYMBOL_CLASS (sym) == LOC_STATIC
21781 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 21782 && per_objfile->per_bfd->can_copy)
4b610737
TT
21783 {
21784 /* A global static variable might be subject to
21785 copy relocation. We first check for a local
21786 minsym, though, because maybe the symbol was
21787 marked hidden, in which case this would not
21788 apply. */
21789 bound_minimal_symbol found
21790 = (lookup_minimal_symbol_linkage
987012b8 21791 (sym->linkage_name (), objfile));
4b610737
TT
21792 if (found.minsym != nullptr)
21793 sym->maybe_copied = 1;
21794 }
f55ee35c 21795
1c809c68
TT
21796 /* A variable with DW_AT_external is never static,
21797 but it may be block-scoped. */
804d2729 21798 list_to_add
c24bdb02
KS
21799 = ((cu->list_in_scope
21800 == cu->get_builder ()->get_file_symbols ())
21801 ? cu->get_builder ()->get_global_symbols ()
804d2729 21802 : cu->list_in_scope);
1c809c68 21803 }
c906108c 21804 else
e37fd15a 21805 list_to_add = cu->list_in_scope;
c906108c
SS
21806 }
21807 else
21808 {
21809 /* We do not know the address of this symbol.
dda83cd7
SM
21810 If it is an external symbol and we have type information
21811 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21812 The address of the variable will then be determined from
21813 the minimal symbol table whenever the variable is
21814 referenced. */
e142c38c 21815 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21816
21817 /* Fortran explicitly imports any global symbols to the local
21818 scope by DW_TAG_common_block. */
3e9f1ca1 21819 if (cu->per_cu->lang == language_fortran && die->parent
0971de02
TT
21820 && die->parent->tag == DW_TAG_common_block)
21821 {
21822 /* SYMBOL_CLASS doesn't matter here because
21823 read_common_block is going to reset it. */
21824 if (!suppress_add)
21825 list_to_add = cu->list_in_scope;
21826 }
c45bc3f8 21827 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 21828 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21829 {
0fe7935b
DJ
21830 /* A variable with DW_AT_external is never static, but it
21831 may be block-scoped. */
804d2729 21832 list_to_add
c24bdb02
KS
21833 = ((cu->list_in_scope
21834 == cu->get_builder ()->get_file_symbols ())
21835 ? cu->get_builder ()->get_global_symbols ()
804d2729 21836 : cu->list_in_scope);
0fe7935b 21837
f1e6e072 21838 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21839 }
442ddf59
JK
21840 else if (!die_is_declaration (die, cu))
21841 {
21842 /* Use the default LOC_OPTIMIZED_OUT class. */
21843 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21844 if (!suppress_add)
21845 list_to_add = cu->list_in_scope;
442ddf59 21846 }
c906108c
SS
21847 }
21848 break;
21849 case DW_TAG_formal_parameter:
a60f3166 21850 {
3db19b2d
TV
21851 /* If we are inside a function, mark this as an argument. If
21852 not, we might be looking at an argument to an inlined function
21853 when we do not have enough information to show inlined frames;
21854 pretend it's a local variable in that case so that the user can
21855 still see it. */
21856 struct context_stack *curr
21857 = cu->get_builder ()->get_current_context_stack ();
21858 if (curr != nullptr && curr->name != nullptr)
21859 SYMBOL_IS_ARGUMENT (sym) = 1;
a60f3166 21860 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21861 if (attr != nullptr)
a60f3166
TT
21862 {
21863 var_decode_location (attr, sym, cu);
21864 }
21865 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21866 if (attr != nullptr)
a60f3166
TT
21867 {
21868 dwarf2_const_value (attr, sym, cu);
21869 }
f346a30d 21870
a60f3166
TT
21871 list_to_add = cu->list_in_scope;
21872 }
c906108c
SS
21873 break;
21874 case DW_TAG_unspecified_parameters:
21875 /* From varargs functions; gdb doesn't seem to have any
21876 interest in this information, so just ignore it for now.
21877 (FIXME?) */
21878 break;
34eaf542
TT
21879 case DW_TAG_template_type_param:
21880 suppress_add = 1;
21881 /* Fall through. */
c906108c 21882 case DW_TAG_class_type:
680b30c7 21883 case DW_TAG_interface_type:
c906108c
SS
21884 case DW_TAG_structure_type:
21885 case DW_TAG_union_type:
72019c9c 21886 case DW_TAG_set_type:
c906108c 21887 case DW_TAG_enumeration_type:
f1e6e072 21888 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21889 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21890
63d06c5c 21891 {
9c37b5ae 21892 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21893 really ever be static objects: otherwise, if you try
21894 to, say, break of a class's method and you're in a file
21895 which doesn't mention that class, it won't work unless
21896 the check for all static symbols in lookup_symbol_aux
21897 saves you. See the OtherFileClass tests in
21898 gdb.c++/namespace.exp. */
21899
e37fd15a 21900 if (!suppress_add)
34eaf542 21901 {
c24bdb02 21902 buildsym_compunit *builder = cu->get_builder ();
804d2729 21903 list_to_add
c24bdb02 21904 = (cu->list_in_scope == builder->get_file_symbols ()
3e9f1ca1 21905 && cu->per_cu->lang == language_cplus
c24bdb02 21906 ? builder->get_global_symbols ()
804d2729 21907 : cu->list_in_scope);
63d06c5c 21908
64382290 21909 /* The semantics of C++ state that "struct foo {
9c37b5ae 21910 ... }" also defines a typedef for "foo". */
3e9f1ca1
TT
21911 if (cu->per_cu->lang == language_cplus
21912 || cu->per_cu->lang == language_ada
21913 || cu->per_cu->lang == language_d
21914 || cu->per_cu->lang == language_rust)
64382290
TT
21915 {
21916 /* The symbol's name is already allocated along
21917 with this objfile, so we don't need to
21918 duplicate it for the type. */
7d93a1e0 21919 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21920 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21921 }
63d06c5c
DC
21922 }
21923 }
c906108c
SS
21924 break;
21925 case DW_TAG_typedef:
f1e6e072 21926 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21927 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21928 list_to_add = cu->list_in_scope;
63d06c5c 21929 break;
d8f62e84 21930 case DW_TAG_array_type:
c906108c 21931 case DW_TAG_base_type:
dda83cd7 21932 case DW_TAG_subrange_type:
f1e6e072 21933 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21934 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21935 list_to_add = cu->list_in_scope;
c906108c
SS
21936 break;
21937 case DW_TAG_enumerator:
e142c38c 21938 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21939 if (attr != nullptr)
c906108c 21940 {
e7c27a73 21941 dwarf2_const_value (attr, sym, cu);
c906108c 21942 }
63d06c5c
DC
21943 {
21944 /* NOTE: carlton/2003-11-10: See comment above in the
21945 DW_TAG_class_type, etc. block. */
21946
804d2729 21947 list_to_add
c24bdb02 21948 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
3e9f1ca1 21949 && cu->per_cu->lang == language_cplus
c24bdb02 21950 ? cu->get_builder ()->get_global_symbols ()
804d2729 21951 : cu->list_in_scope);
63d06c5c 21952 }
c906108c 21953 break;
74921315 21954 case DW_TAG_imported_declaration:
5c4e30ca 21955 case DW_TAG_namespace:
f1e6e072 21956 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21957 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21958 break;
530e8392
KB
21959 case DW_TAG_module:
21960 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21961 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21962 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21963 break;
4357ac6c 21964 case DW_TAG_common_block:
f1e6e072 21965 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21966 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21967 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21968 break;
c906108c
SS
21969 default:
21970 /* Not a tag we recognize. Hopefully we aren't processing
21971 trash data, but since we must specifically ignore things
21972 we don't recognize, there is nothing else we should do at
0963b4bd 21973 this point. */
b98664d3 21974 complaint (_("unsupported tag: '%s'"),
4d3c2250 21975 dwarf_tag_name (die->tag));
c906108c
SS
21976 break;
21977 }
df8a16a1 21978
e37fd15a
SW
21979 if (suppress_add)
21980 {
21981 sym->hash_next = objfile->template_symbols;
21982 objfile->template_symbols = sym;
21983 list_to_add = NULL;
21984 }
21985
21986 if (list_to_add != NULL)
d3cb6808 21987 add_symbol_to_list (sym, list_to_add);
e37fd15a 21988
df8a16a1
DJ
21989 /* For the benefit of old versions of GCC, check for anonymous
21990 namespaces based on the demangled name. */
4d4ec4e5 21991 if (!cu->processing_has_namespace_info
3e9f1ca1 21992 && cu->per_cu->lang == language_cplus)
c24bdb02 21993 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21994 }
21995 return (sym);
21996}
21997
98bfdba5
PA
21998/* Given an attr with a DW_FORM_dataN value in host byte order,
21999 zero-extend it as appropriate for the symbol's type. The DWARF
22000 standard (v4) is not entirely clear about the meaning of using
22001 DW_FORM_dataN for a constant with a signed type, where the type is
22002 wider than the data. The conclusion of a discussion on the DWARF
22003 list was that this is unspecified. We choose to always zero-extend
22004 because that is the interpretation long in use by GCC. */
c906108c 22005
98bfdba5 22006static gdb_byte *
ff39bb5e 22007dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22008 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22009{
5e22e966 22010 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22011 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22012 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22013 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22014
22015 if (bits < sizeof (*value) * 8)
22016 {
22017 l &= ((LONGEST) 1 << bits) - 1;
22018 *value = l;
22019 }
22020 else if (bits == sizeof (*value) * 8)
22021 *value = l;
22022 else
22023 {
224c3ddb 22024 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22025 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22026 return bytes;
22027 }
22028
22029 return NULL;
22030}
22031
22032/* Read a constant value from an attribute. Either set *VALUE, or if
22033 the value does not fit in *VALUE, set *BYTES - either already
22034 allocated on the objfile obstack, or newly allocated on OBSTACK,
22035 or, set *BATON, if we translated the constant to a location
22036 expression. */
22037
22038static void
ff39bb5e 22039dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22040 const char *name, struct obstack *obstack,
22041 struct dwarf2_cu *cu,
d521ce57 22042 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22043 struct dwarf2_locexpr_baton **baton)
22044{
5e22e966 22045 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22046 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22047 struct comp_unit_head *cu_header = &cu->header;
c906108c 22048 struct dwarf_block *blk;
98bfdba5
PA
22049 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22050 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22051
22052 *value = 0;
22053 *bytes = NULL;
22054 *baton = NULL;
c906108c
SS
22055
22056 switch (attr->form)
22057 {
22058 case DW_FORM_addr:
336d760d 22059 case DW_FORM_addrx:
3019eac3 22060 case DW_FORM_GNU_addr_index:
ac56253d 22061 {
ac56253d
TT
22062 gdb_byte *data;
22063
98bfdba5
PA
22064 if (TYPE_LENGTH (type) != cu_header->addr_size)
22065 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22066 cu_header->addr_size,
98bfdba5 22067 TYPE_LENGTH (type));
ac56253d
TT
22068 /* Symbols of this form are reasonably rare, so we just
22069 piggyback on the existing location code rather than writing
22070 a new implementation of symbol_computed_ops. */
8d749320 22071 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22072 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22073 (*baton)->per_cu = cu->per_cu;
22074 gdb_assert ((*baton)->per_cu);
ac56253d 22075
98bfdba5 22076 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22077 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22078 (*baton)->data = data;
ac56253d
TT
22079
22080 data[0] = DW_OP_addr;
22081 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22082 byte_order, attr->as_address ());
ac56253d 22083 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22084 }
c906108c 22085 break;
4ac36638 22086 case DW_FORM_string:
93b5768b 22087 case DW_FORM_strp:
cf532bd1 22088 case DW_FORM_strx:
3019eac3 22089 case DW_FORM_GNU_str_index:
36586728 22090 case DW_FORM_GNU_strp_alt:
c6481205 22091 /* The string is already allocated on the objfile obstack, point
98bfdba5 22092 directly to it. */
2c830f54 22093 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22094 break;
c906108c
SS
22095 case DW_FORM_block1:
22096 case DW_FORM_block2:
22097 case DW_FORM_block4:
22098 case DW_FORM_block:
2dc7f7b3 22099 case DW_FORM_exprloc:
0224619f 22100 case DW_FORM_data16:
9d2246fc 22101 blk = attr->as_block ();
98bfdba5
PA
22102 if (TYPE_LENGTH (type) != blk->size)
22103 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22104 TYPE_LENGTH (type));
22105 *bytes = blk->data;
c906108c 22106 break;
2df3850c
JM
22107
22108 /* The DW_AT_const_value attributes are supposed to carry the
22109 symbol's value "represented as it would be on the target
22110 architecture." By the time we get here, it's already been
22111 converted to host endianness, so we just need to sign- or
22112 zero-extend it as appropriate. */
22113 case DW_FORM_data1:
3aef2284 22114 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22115 break;
c906108c 22116 case DW_FORM_data2:
3aef2284 22117 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22118 break;
c906108c 22119 case DW_FORM_data4:
3aef2284 22120 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22121 break;
c906108c 22122 case DW_FORM_data8:
3aef2284 22123 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22124 break;
22125
c906108c 22126 case DW_FORM_sdata:
663c44ac 22127 case DW_FORM_implicit_const:
1bc397c5 22128 *value = attr->as_signed ();
2df3850c
JM
22129 break;
22130
c906108c 22131 case DW_FORM_udata:
529908cb 22132 *value = attr->as_unsigned ();
c906108c 22133 break;
2df3850c 22134
c906108c 22135 default:
b98664d3 22136 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22137 dwarf_form_name (attr->form));
98bfdba5 22138 *value = 0;
c906108c
SS
22139 break;
22140 }
22141}
22142
2df3850c 22143
98bfdba5
PA
22144/* Copy constant value from an attribute to a symbol. */
22145
2df3850c 22146static void
ff39bb5e 22147dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22148 struct dwarf2_cu *cu)
2df3850c 22149{
5e22e966 22150 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22151 LONGEST value;
d521ce57 22152 const gdb_byte *bytes;
98bfdba5 22153 struct dwarf2_locexpr_baton *baton;
2df3850c 22154
98bfdba5 22155 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22156 sym->print_name (),
98bfdba5
PA
22157 &objfile->objfile_obstack, cu,
22158 &value, &bytes, &baton);
2df3850c 22159
98bfdba5
PA
22160 if (baton != NULL)
22161 {
98bfdba5 22162 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22163 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22164 }
22165 else if (bytes != NULL)
22166 {
22167 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22168 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22169 }
22170 else
22171 {
22172 SYMBOL_VALUE (sym) = value;
f1e6e072 22173 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22174 }
2df3850c
JM
22175}
22176
c906108c
SS
22177/* Return the type of the die in question using its DW_AT_type attribute. */
22178
22179static struct type *
e7c27a73 22180die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22181{
c906108c 22182 struct attribute *type_attr;
c906108c 22183
e142c38c 22184 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22185 if (!type_attr)
22186 {
5e22e966 22187 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22188 /* A missing DW_AT_type represents a void type. */
518817b3 22189 return objfile_type (objfile)->builtin_void;
c906108c 22190 }
348e048f 22191
673bfd45 22192 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22193}
22194
b4ba55a1
JB
22195/* True iff CU's producer generates GNAT Ada auxiliary information
22196 that allows to find parallel types through that information instead
22197 of having to do expensive parallel lookups by type name. */
22198
22199static int
22200need_gnat_info (struct dwarf2_cu *cu)
22201{
de4cb04a
JB
22202 /* Assume that the Ada compiler was GNAT, which always produces
22203 the auxiliary information. */
3e9f1ca1 22204 return (cu->per_cu->lang == language_ada);
b4ba55a1
JB
22205}
22206
b4ba55a1
JB
22207/* Return the auxiliary type of the die in question using its
22208 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22209 attribute is not present. */
22210
22211static struct type *
22212die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22213{
b4ba55a1 22214 struct attribute *type_attr;
b4ba55a1
JB
22215
22216 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22217 if (!type_attr)
22218 return NULL;
22219
673bfd45 22220 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22221}
22222
22223/* If DIE has a descriptive_type attribute, then set the TYPE's
22224 descriptive type accordingly. */
22225
22226static void
22227set_descriptive_type (struct type *type, struct die_info *die,
22228 struct dwarf2_cu *cu)
22229{
22230 struct type *descriptive_type = die_descriptive_type (die, cu);
22231
22232 if (descriptive_type)
22233 {
22234 ALLOCATE_GNAT_AUX_TYPE (type);
22235 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22236 }
22237}
22238
c906108c
SS
22239/* Return the containing type of the die in question using its
22240 DW_AT_containing_type attribute. */
22241
22242static struct type *
e7c27a73 22243die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22244{
c906108c 22245 struct attribute *type_attr;
5e22e966 22246 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22247
e142c38c 22248 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22249 if (!type_attr)
22250 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22251 "[in module %s]"), objfile_name (objfile));
33ac96f0 22252
673bfd45 22253 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22254}
22255
ac9ec31b
DE
22256/* Return an error marker type to use for the ill formed type in DIE/CU. */
22257
22258static struct type *
22259build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22260{
976ca316
SM
22261 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22262 struct objfile *objfile = per_objfile->objfile;
528e1572 22263 char *saved;
ac9ec31b 22264
528e1572
SM
22265 std::string message
22266 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22267 objfile_name (objfile),
22268 sect_offset_str (cu->header.sect_off),
22269 sect_offset_str (die->sect_off));
efba19b0 22270 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22271
19f392bc 22272 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22273}
22274
673bfd45 22275/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22276 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22277 DW_AT_containing_type.
673bfd45
DE
22278 If there is no type substitute an error marker. */
22279
c906108c 22280static struct type *
ff39bb5e 22281lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22282 struct dwarf2_cu *cu)
c906108c 22283{
976ca316
SM
22284 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22285 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22286 struct type *this_type;
22287
ac9ec31b
DE
22288 gdb_assert (attr->name == DW_AT_type
22289 || attr->name == DW_AT_GNAT_descriptive_type
22290 || attr->name == DW_AT_containing_type);
22291
673bfd45
DE
22292 /* First see if we have it cached. */
22293
36586728
TT
22294 if (attr->form == DW_FORM_GNU_ref_alt)
22295 {
22296 struct dwarf2_per_cu_data *per_cu;
0826b30a 22297 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22298
976ca316
SM
22299 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22300 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22301 }
cd6c91b4 22302 else if (attr->form_is_ref ())
673bfd45 22303 {
0826b30a 22304 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22305
976ca316 22306 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22307 }
55f1336d 22308 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22309 {
630ed6b9 22310 ULONGEST signature = attr->as_signature ();
673bfd45 22311
ac9ec31b 22312 return get_signatured_type (die, signature, cu);
673bfd45
DE
22313 }
22314 else
22315 {
b98664d3 22316 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22317 " at %s [in module %s]"),
22318 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22319 objfile_name (objfile));
ac9ec31b 22320 return build_error_marker_type (cu, die);
673bfd45
DE
22321 }
22322
22323 /* If not cached we need to read it in. */
22324
22325 if (this_type == NULL)
22326 {
ac9ec31b 22327 struct die_info *type_die = NULL;
673bfd45
DE
22328 struct dwarf2_cu *type_cu = cu;
22329
cd6c91b4 22330 if (attr->form_is_ref ())
ac9ec31b
DE
22331 type_die = follow_die_ref (die, attr, &type_cu);
22332 if (type_die == NULL)
22333 return build_error_marker_type (cu, die);
22334 /* If we find the type now, it's probably because the type came
3019eac3
DE
22335 from an inter-CU reference and the type's CU got expanded before
22336 ours. */
ac9ec31b 22337 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22338 }
22339
22340 /* If we still don't have a type use an error marker. */
22341
22342 if (this_type == NULL)
ac9ec31b 22343 return build_error_marker_type (cu, die);
673bfd45 22344
f792889a 22345 return this_type;
c906108c
SS
22346}
22347
673bfd45
DE
22348/* Return the type in DIE, CU.
22349 Returns NULL for invalid types.
22350
02142a6c 22351 This first does a lookup in die_type_hash,
673bfd45
DE
22352 and only reads the die in if necessary.
22353
22354 NOTE: This can be called when reading in partial or full symbols. */
22355
f792889a 22356static struct type *
e7c27a73 22357read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22358{
f792889a
DJ
22359 struct type *this_type;
22360
22361 this_type = get_die_type (die, cu);
22362 if (this_type)
22363 return this_type;
22364
673bfd45
DE
22365 return read_type_die_1 (die, cu);
22366}
22367
22368/* Read the type in DIE, CU.
22369 Returns NULL for invalid types. */
22370
22371static struct type *
22372read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22373{
22374 struct type *this_type = NULL;
22375
c906108c
SS
22376 switch (die->tag)
22377 {
22378 case DW_TAG_class_type:
680b30c7 22379 case DW_TAG_interface_type:
c906108c
SS
22380 case DW_TAG_structure_type:
22381 case DW_TAG_union_type:
f792889a 22382 this_type = read_structure_type (die, cu);
c906108c
SS
22383 break;
22384 case DW_TAG_enumeration_type:
f792889a 22385 this_type = read_enumeration_type (die, cu);
c906108c
SS
22386 break;
22387 case DW_TAG_subprogram:
22388 case DW_TAG_subroutine_type:
edb3359d 22389 case DW_TAG_inlined_subroutine:
f792889a 22390 this_type = read_subroutine_type (die, cu);
c906108c
SS
22391 break;
22392 case DW_TAG_array_type:
f792889a 22393 this_type = read_array_type (die, cu);
c906108c 22394 break;
72019c9c 22395 case DW_TAG_set_type:
f792889a 22396 this_type = read_set_type (die, cu);
72019c9c 22397 break;
c906108c 22398 case DW_TAG_pointer_type:
f792889a 22399 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22400 break;
22401 case DW_TAG_ptr_to_member_type:
f792889a 22402 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22403 break;
22404 case DW_TAG_reference_type:
4297a3f0
AV
22405 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22406 break;
22407 case DW_TAG_rvalue_reference_type:
22408 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22409 break;
22410 case DW_TAG_const_type:
f792889a 22411 this_type = read_tag_const_type (die, cu);
c906108c
SS
22412 break;
22413 case DW_TAG_volatile_type:
f792889a 22414 this_type = read_tag_volatile_type (die, cu);
c906108c 22415 break;
06d66ee9
TT
22416 case DW_TAG_restrict_type:
22417 this_type = read_tag_restrict_type (die, cu);
22418 break;
c906108c 22419 case DW_TAG_string_type:
f792889a 22420 this_type = read_tag_string_type (die, cu);
c906108c
SS
22421 break;
22422 case DW_TAG_typedef:
f792889a 22423 this_type = read_typedef (die, cu);
c906108c 22424 break;
a02abb62 22425 case DW_TAG_subrange_type:
f792889a 22426 this_type = read_subrange_type (die, cu);
a02abb62 22427 break;
c906108c 22428 case DW_TAG_base_type:
f792889a 22429 this_type = read_base_type (die, cu);
c906108c 22430 break;
81a17f79 22431 case DW_TAG_unspecified_type:
f792889a 22432 this_type = read_unspecified_type (die, cu);
81a17f79 22433 break;
0114d602
DJ
22434 case DW_TAG_namespace:
22435 this_type = read_namespace_type (die, cu);
22436 break;
f55ee35c
JK
22437 case DW_TAG_module:
22438 this_type = read_module_type (die, cu);
22439 break;
a2c2acaf
MW
22440 case DW_TAG_atomic_type:
22441 this_type = read_tag_atomic_type (die, cu);
22442 break;
c906108c 22443 default:
b98664d3 22444 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22445 dwarf_tag_name (die->tag));
c906108c
SS
22446 break;
22447 }
63d06c5c 22448
f792889a 22449 return this_type;
63d06c5c
DC
22450}
22451
abc72ce4
DE
22452/* See if we can figure out if the class lives in a namespace. We do
22453 this by looking for a member function; its demangled name will
22454 contain namespace info, if there is any.
22455 Return the computed name or NULL.
22456 Space for the result is allocated on the objfile's obstack.
22457 This is the full-die version of guess_partial_die_structure_name.
22458 In this case we know DIE has no useful parent. */
22459
43816ebc 22460static const char *
abc72ce4
DE
22461guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22462{
22463 struct die_info *spec_die;
22464 struct dwarf2_cu *spec_cu;
22465 struct die_info *child;
5e22e966 22466 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22467
22468 spec_cu = cu;
22469 spec_die = die_specification (die, &spec_cu);
22470 if (spec_die != NULL)
22471 {
22472 die = spec_die;
22473 cu = spec_cu;
22474 }
22475
22476 for (child = die->child;
22477 child != NULL;
22478 child = child->sibling)
22479 {
22480 if (child->tag == DW_TAG_subprogram)
22481 {
73b9be8b 22482 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22483
7d45c7c3 22484 if (linkage_name != NULL)
abc72ce4 22485 {
43816ebc 22486 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22487 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22488 const char *name = NULL;
abc72ce4
DE
22489
22490 if (actual_name != NULL)
22491 {
15d034d0 22492 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22493
22494 if (die_name != NULL
43816ebc 22495 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22496 {
22497 /* Strip off the class name from the full name.
22498 We want the prefix. */
22499 int die_name_len = strlen (die_name);
43816ebc
TT
22500 int actual_name_len = strlen (actual_name.get ());
22501 const char *ptr = actual_name.get ();
abc72ce4
DE
22502
22503 /* Test for '::' as a sanity check. */
22504 if (actual_name_len > die_name_len + 2
43816ebc 22505 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22506 name = obstack_strndup (
e3b94546 22507 &objfile->per_bfd->storage_obstack,
43816ebc 22508 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22509 }
22510 }
abc72ce4
DE
22511 return name;
22512 }
22513 }
22514 }
22515
22516 return NULL;
22517}
22518
96408a79
SA
22519/* GCC might emit a nameless typedef that has a linkage name. Determine the
22520 prefix part in such case. See
22521 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22522
a121b7c1 22523static const char *
96408a79
SA
22524anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22525{
22526 struct attribute *attr;
e6a959d6 22527 const char *base;
96408a79
SA
22528
22529 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22530 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22531 return NULL;
22532
7d45c7c3 22533 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22534 return NULL;
22535
73b9be8b 22536 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22537 const char *attr_name = attr->as_string ();
22538 if (attr == NULL || attr_name == NULL)
96408a79
SA
22539 return NULL;
22540
22541 /* dwarf2_name had to be already called. */
3b64bf15 22542 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22543
22544 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22545 base = strrchr (attr_name, ':');
22546 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22547 return "";
22548
5e22e966 22549 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22550 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22551 attr_name,
22552 &base[-1] - attr_name);
96408a79
SA
22553}
22554
fdde2d81 22555/* Return the name of the namespace/class that DIE is defined within,
0114d602 22556 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22557
0114d602
DJ
22558 For example, if we're within the method foo() in the following
22559 code:
22560
22561 namespace N {
22562 class C {
22563 void foo () {
22564 }
22565 };
22566 }
22567
22568 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22569
0d5cff50 22570static const char *
e142c38c 22571determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22572{
976ca316 22573 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22574 struct die_info *parent, *spec_die;
22575 struct dwarf2_cu *spec_cu;
22576 struct type *parent_type;
a121b7c1 22577 const char *retval;
63d06c5c 22578
3e9f1ca1
TT
22579 if (cu->per_cu->lang != language_cplus
22580 && cu->per_cu->lang != language_fortran
22581 && cu->per_cu->lang != language_d
22582 && cu->per_cu->lang != language_rust)
0114d602
DJ
22583 return "";
22584
96408a79
SA
22585 retval = anonymous_struct_prefix (die, cu);
22586 if (retval)
22587 return retval;
22588
0114d602
DJ
22589 /* We have to be careful in the presence of DW_AT_specification.
22590 For example, with GCC 3.4, given the code
22591
22592 namespace N {
22593 void foo() {
22594 // Definition of N::foo.
22595 }
22596 }
22597
22598 then we'll have a tree of DIEs like this:
22599
22600 1: DW_TAG_compile_unit
22601 2: DW_TAG_namespace // N
22602 3: DW_TAG_subprogram // declaration of N::foo
22603 4: DW_TAG_subprogram // definition of N::foo
22604 DW_AT_specification // refers to die #3
22605
22606 Thus, when processing die #4, we have to pretend that we're in
22607 the context of its DW_AT_specification, namely the contex of die
22608 #3. */
22609 spec_cu = cu;
22610 spec_die = die_specification (die, &spec_cu);
22611 if (spec_die == NULL)
22612 parent = die->parent;
22613 else
63d06c5c 22614 {
0114d602
DJ
22615 parent = spec_die->parent;
22616 cu = spec_cu;
63d06c5c 22617 }
0114d602
DJ
22618
22619 if (parent == NULL)
22620 return "";
98bfdba5
PA
22621 else if (parent->building_fullname)
22622 {
22623 const char *name;
22624 const char *parent_name;
22625
22626 /* It has been seen on RealView 2.2 built binaries,
22627 DW_TAG_template_type_param types actually _defined_ as
22628 children of the parent class:
22629
22630 enum E {};
22631 template class <class Enum> Class{};
22632 Class<enum E> class_e;
22633
dda83cd7
SM
22634 1: DW_TAG_class_type (Class)
22635 2: DW_TAG_enumeration_type (E)
22636 3: DW_TAG_enumerator (enum1:0)
22637 3: DW_TAG_enumerator (enum2:1)
22638 ...
22639 2: DW_TAG_template_type_param
22640 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
22641
22642 Besides being broken debug info, it can put GDB into an
22643 infinite loop. Consider:
22644
22645 When we're building the full name for Class<E>, we'll start
22646 at Class, and go look over its template type parameters,
22647 finding E. We'll then try to build the full name of E, and
22648 reach here. We're now trying to build the full name of E,
22649 and look over the parent DIE for containing scope. In the
22650 broken case, if we followed the parent DIE of E, we'd again
22651 find Class, and once again go look at its template type
22652 arguments, etc., etc. Simply don't consider such parent die
22653 as source-level parent of this die (it can't be, the language
22654 doesn't allow it), and break the loop here. */
22655 name = dwarf2_name (die, cu);
22656 parent_name = dwarf2_name (parent, cu);
b98664d3 22657 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22658 name ? name : "<unknown>",
22659 parent_name ? parent_name : "<unknown>");
22660 return "";
22661 }
63d06c5c 22662 else
0114d602
DJ
22663 switch (parent->tag)
22664 {
63d06c5c 22665 case DW_TAG_namespace:
0114d602 22666 parent_type = read_type_die (parent, cu);
acebe513
UW
22667 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22668 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22669 Work around this problem here. */
3e9f1ca1 22670 if (cu->per_cu->lang == language_cplus
7d93a1e0 22671 && strcmp (parent_type->name (), "::") == 0)
acebe513 22672 return "";
0114d602 22673 /* We give a name to even anonymous namespaces. */
7d93a1e0 22674 return parent_type->name ();
63d06c5c 22675 case DW_TAG_class_type:
680b30c7 22676 case DW_TAG_interface_type:
63d06c5c 22677 case DW_TAG_structure_type:
0114d602 22678 case DW_TAG_union_type:
f55ee35c 22679 case DW_TAG_module:
0114d602 22680 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
22681 if (parent_type->name () != NULL)
22682 return parent_type->name ();
0114d602
DJ
22683 else
22684 /* An anonymous structure is only allowed non-static data
22685 members; no typedefs, no member functions, et cetera.
22686 So it does not need a prefix. */
22687 return "";
abc72ce4 22688 case DW_TAG_compile_unit:
95554aad 22689 case DW_TAG_partial_unit:
abc72ce4 22690 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
3e9f1ca1 22691 if (cu->per_cu->lang == language_cplus
976ca316 22692 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
22693 && die->child != NULL
22694 && (die->tag == DW_TAG_class_type
22695 || die->tag == DW_TAG_structure_type
22696 || die->tag == DW_TAG_union_type))
22697 {
43816ebc 22698 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22699 if (name != NULL)
22700 return name;
22701 }
22702 return "";
0a4b0913
AB
22703 case DW_TAG_subprogram:
22704 /* Nested subroutines in Fortran get a prefix with the name
22705 of the parent's subroutine. */
3e9f1ca1 22706 if (cu->per_cu->lang == language_fortran)
0a4b0913
AB
22707 {
22708 if ((die->tag == DW_TAG_subprogram)
22709 && (dwarf2_name (parent, cu) != NULL))
22710 return dwarf2_name (parent, cu);
22711 }
22712 return determine_prefix (parent, cu);
3d567982
TT
22713 case DW_TAG_enumeration_type:
22714 parent_type = read_type_die (parent, cu);
3bc440a2 22715 if (parent_type->is_declared_class ())
3d567982 22716 {
7d93a1e0
SM
22717 if (parent_type->name () != NULL)
22718 return parent_type->name ();
3d567982
TT
22719 return "";
22720 }
22721 /* Fall through. */
63d06c5c 22722 default:
8176b9b8 22723 return determine_prefix (parent, cu);
63d06c5c 22724 }
63d06c5c
DC
22725}
22726
3e43a32a
MS
22727/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22728 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22729 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22730 an obconcat, otherwise allocate storage for the result. The CU argument is
22731 used to determine the language and hence, the appropriate separator. */
987504bb 22732
f55ee35c 22733#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22734
22735static char *
f55ee35c 22736typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 22737 int physname, struct dwarf2_cu *cu)
63d06c5c 22738{
f55ee35c 22739 const char *lead = "";
5c315b68 22740 const char *sep;
63d06c5c 22741
3e43a32a
MS
22742 if (suffix == NULL || suffix[0] == '\0'
22743 || prefix == NULL || prefix[0] == '\0')
987504bb 22744 sep = "";
3e9f1ca1 22745 else if (cu->per_cu->lang == language_d)
45280282
IB
22746 {
22747 /* For D, the 'main' function could be defined in any module, but it
22748 should never be prefixed. */
22749 if (strcmp (suffix, "D main") == 0)
22750 {
22751 prefix = "";
22752 sep = "";
22753 }
22754 else
22755 sep = ".";
22756 }
3e9f1ca1 22757 else if (cu->per_cu->lang == language_fortran && physname)
f55ee35c
JK
22758 {
22759 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22760 DW_AT_MIPS_linkage_name is preferred and used instead. */
22761
22762 lead = "__";
22763 sep = "_MOD_";
22764 }
987504bb
JJ
22765 else
22766 sep = "::";
63d06c5c 22767
6dd47d34
DE
22768 if (prefix == NULL)
22769 prefix = "";
22770 if (suffix == NULL)
22771 suffix = "";
22772
987504bb
JJ
22773 if (obs == NULL)
22774 {
3e43a32a 22775 char *retval
224c3ddb
SM
22776 = ((char *)
22777 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22778
f55ee35c
JK
22779 strcpy (retval, lead);
22780 strcat (retval, prefix);
6dd47d34
DE
22781 strcat (retval, sep);
22782 strcat (retval, suffix);
63d06c5c
DC
22783 return retval;
22784 }
987504bb
JJ
22785 else
22786 {
22787 /* We have an obstack. */
f55ee35c 22788 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22789 }
63d06c5c
DC
22790}
22791
71c25dea
TT
22792/* Get name of a die, return NULL if not found. */
22793
15d034d0
TT
22794static const char *
22795dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 22796 struct objfile *objfile)
71c25dea 22797{
3e9f1ca1 22798 if (name && cu->per_cu->lang == language_cplus)
71c25dea 22799 {
596dc4ad
TT
22800 gdb::unique_xmalloc_ptr<char> canon_name
22801 = cp_canonicalize_string (name);
71c25dea 22802
596dc4ad
TT
22803 if (canon_name != nullptr)
22804 name = objfile->intern (canon_name.get ());
71c25dea
TT
22805 }
22806
22807 return name;
c906108c
SS
22808}
22809
96553a0c
DE
22810/* Get name of a die, return NULL if not found.
22811 Anonymous namespaces are converted to their magic string. */
9219021c 22812
15d034d0 22813static const char *
e142c38c 22814dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22815{
22816 struct attribute *attr;
5e22e966 22817 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22818
e142c38c 22819 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
22820 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22821 if (attr_name == nullptr
96553a0c 22822 && die->tag != DW_TAG_namespace
53832f31
TT
22823 && die->tag != DW_TAG_class_type
22824 && die->tag != DW_TAG_interface_type
22825 && die->tag != DW_TAG_structure_type
22826 && die->tag != DW_TAG_union_type)
71c25dea
TT
22827 return NULL;
22828
22829 switch (die->tag)
22830 {
22831 case DW_TAG_compile_unit:
95554aad 22832 case DW_TAG_partial_unit:
71c25dea
TT
22833 /* Compilation units have a DW_AT_name that is a filename, not
22834 a source language identifier. */
22835 case DW_TAG_enumeration_type:
22836 case DW_TAG_enumerator:
22837 /* These tags always have simple identifiers already; no need
22838 to canonicalize them. */
2c830f54 22839 return attr_name;
907af001 22840
96553a0c 22841 case DW_TAG_namespace:
2c830f54
TT
22842 if (attr_name != nullptr)
22843 return attr_name;
96553a0c
DE
22844 return CP_ANONYMOUS_NAMESPACE_STR;
22845
907af001
UW
22846 case DW_TAG_class_type:
22847 case DW_TAG_interface_type:
22848 case DW_TAG_structure_type:
22849 case DW_TAG_union_type:
22850 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22851 structures or unions. These were of the form "._%d" in GCC 4.1,
22852 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22853 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
22854 if (attr_name != nullptr
22855 && (startswith (attr_name, "._")
22856 || startswith (attr_name, "<anonymous")))
907af001 22857 return NULL;
53832f31
TT
22858
22859 /* GCC might emit a nameless typedef that has a linkage name. See
22860 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 22861 if (!attr || attr_name == NULL)
53832f31 22862 {
73b9be8b 22863 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 22864 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 22865 if (attr == NULL || attr_name == NULL)
53832f31
TT
22866 return NULL;
22867
2c830f54 22868 /* Avoid demangling attr_name the second time on a second
df5c6c50 22869 call for the same DIE. */
3b64bf15 22870 if (!attr->canonical_string_p ())
53832f31 22871 {
43816ebc 22872 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 22873 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
22874 if (demangled == nullptr)
22875 return nullptr;
43816ebc 22876
c6481205 22877 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 22878 attr_name = attr->as_string ();
53832f31 22879 }
67430cd0 22880
2c830f54
TT
22881 /* Strip any leading namespaces/classes, keep only the
22882 base name. DW_AT_name for named DIEs does not
22883 contain the prefixes. */
22884 const char *base = strrchr (attr_name, ':');
22885 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
22886 return &base[1];
22887 else
2c830f54 22888 return attr_name;
53832f31 22889 }
907af001
UW
22890 break;
22891
71c25dea 22892 default:
907af001
UW
22893 break;
22894 }
22895
3b64bf15 22896 if (!attr->canonical_string_p ())
c6481205
TT
22897 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22898 objfile));
2c830f54 22899 return attr->as_string ();
9219021c
DC
22900}
22901
22902/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22903 is none. *EXT_CU is the CU containing DIE on input, and the CU
22904 containing the return value on output. */
9219021c
DC
22905
22906static struct die_info *
f2f0e013 22907dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22908{
22909 struct attribute *attr;
9219021c 22910
f2f0e013 22911 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22912 if (attr == NULL)
22913 return NULL;
22914
f2f0e013 22915 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22916}
22917
f9aca02d 22918static void
d97bc12b 22919dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22920{
22921 unsigned int i;
22922
d97bc12b 22923 print_spaces (indent, f);
9d8780f0 22924 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22925 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22926 sect_offset_str (die->sect_off));
d97bc12b
DE
22927
22928 if (die->parent != NULL)
22929 {
22930 print_spaces (indent, f);
9d8780f0
SM
22931 fprintf_unfiltered (f, " parent at offset: %s\n",
22932 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22933 }
22934
22935 print_spaces (indent, f);
22936 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22937 dwarf_bool_name (die->child != NULL));
c906108c 22938
d97bc12b
DE
22939 print_spaces (indent, f);
22940 fprintf_unfiltered (f, " attributes:\n");
22941
c906108c
SS
22942 for (i = 0; i < die->num_attrs; ++i)
22943 {
d97bc12b
DE
22944 print_spaces (indent, f);
22945 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22946 dwarf_attr_name (die->attrs[i].name),
22947 dwarf_form_name (die->attrs[i].form));
d97bc12b 22948
c906108c
SS
22949 switch (die->attrs[i].form)
22950 {
c906108c 22951 case DW_FORM_addr:
336d760d 22952 case DW_FORM_addrx:
3019eac3 22953 case DW_FORM_GNU_addr_index:
d97bc12b 22954 fprintf_unfiltered (f, "address: ");
36d378cf 22955 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
22956 break;
22957 case DW_FORM_block2:
22958 case DW_FORM_block4:
22959 case DW_FORM_block:
22960 case DW_FORM_block1:
56eb65bd 22961 fprintf_unfiltered (f, "block: size %s",
9d2246fc 22962 pulongest (die->attrs[i].as_block ()->size));
c906108c 22963 break;
2dc7f7b3 22964 case DW_FORM_exprloc:
56eb65bd 22965 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 22966 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 22967 break;
0224619f
JK
22968 case DW_FORM_data16:
22969 fprintf_unfiltered (f, "constant of 16 bytes");
22970 break;
4568ecf9
DE
22971 case DW_FORM_ref_addr:
22972 fprintf_unfiltered (f, "ref address: ");
529908cb 22973 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 22974 break;
36586728
TT
22975 case DW_FORM_GNU_ref_alt:
22976 fprintf_unfiltered (f, "alt ref address: ");
529908cb 22977 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 22978 break;
10b3939b
DJ
22979 case DW_FORM_ref1:
22980 case DW_FORM_ref2:
22981 case DW_FORM_ref4:
4568ecf9
DE
22982 case DW_FORM_ref8:
22983 case DW_FORM_ref_udata:
d97bc12b 22984 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 22985 (long) (die->attrs[i].as_unsigned ()));
10b3939b 22986 break;
c906108c
SS
22987 case DW_FORM_data1:
22988 case DW_FORM_data2:
22989 case DW_FORM_data4:
ce5d95e1 22990 case DW_FORM_data8:
c906108c 22991 case DW_FORM_udata:
43bbcdc2 22992 fprintf_unfiltered (f, "constant: %s",
529908cb 22993 pulongest (die->attrs[i].as_unsigned ()));
c906108c 22994 break;
2dc7f7b3
TT
22995 case DW_FORM_sec_offset:
22996 fprintf_unfiltered (f, "section offset: %s",
529908cb 22997 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 22998 break;
55f1336d 22999 case DW_FORM_ref_sig8:
ac9ec31b 23000 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23001 hex_string (die->attrs[i].as_signature ()));
348e048f 23002 break;
c906108c 23003 case DW_FORM_string:
4bdf3d34 23004 case DW_FORM_strp:
43988095 23005 case DW_FORM_line_strp:
cf532bd1 23006 case DW_FORM_strx:
3019eac3 23007 case DW_FORM_GNU_str_index:
36586728 23008 case DW_FORM_GNU_strp_alt:
8285870a 23009 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23010 die->attrs[i].as_string ()
23011 ? die->attrs[i].as_string () : "",
23012 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23013 break;
23014 case DW_FORM_flag:
c45bc3f8 23015 if (die->attrs[i].as_boolean ())
d97bc12b 23016 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23017 else
d97bc12b 23018 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23019 break;
2dc7f7b3
TT
23020 case DW_FORM_flag_present:
23021 fprintf_unfiltered (f, "flag: TRUE");
23022 break;
a8329558 23023 case DW_FORM_indirect:
0963b4bd
MS
23024 /* The reader will have reduced the indirect form to
23025 the "base form" so this form should not occur. */
5f48f8f3 23026 fprintf_unfiltered (f,
3e43a32a 23027 "unexpected attribute form: DW_FORM_indirect");
a8329558 23028 break;
1bc397c5 23029 case DW_FORM_sdata:
663c44ac
JK
23030 case DW_FORM_implicit_const:
23031 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23032 plongest (die->attrs[i].as_signed ()));
663c44ac 23033 break;
c906108c 23034 default:
d97bc12b 23035 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23036 die->attrs[i].form);
d97bc12b 23037 break;
c906108c 23038 }
d97bc12b 23039 fprintf_unfiltered (f, "\n");
c906108c
SS
23040 }
23041}
23042
f9aca02d 23043static void
d97bc12b 23044dump_die_for_error (struct die_info *die)
c906108c 23045{
d97bc12b
DE
23046 dump_die_shallow (gdb_stderr, 0, die);
23047}
23048
23049static void
23050dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23051{
23052 int indent = level * 4;
23053
23054 gdb_assert (die != NULL);
23055
23056 if (level >= max_level)
23057 return;
23058
23059 dump_die_shallow (f, indent, die);
23060
23061 if (die->child != NULL)
c906108c 23062 {
d97bc12b
DE
23063 print_spaces (indent, f);
23064 fprintf_unfiltered (f, " Children:");
23065 if (level + 1 < max_level)
23066 {
23067 fprintf_unfiltered (f, "\n");
23068 dump_die_1 (f, level + 1, max_level, die->child);
23069 }
23070 else
23071 {
3e43a32a
MS
23072 fprintf_unfiltered (f,
23073 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23074 }
23075 }
23076
23077 if (die->sibling != NULL && level > 0)
23078 {
23079 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23080 }
23081}
23082
d97bc12b
DE
23083/* This is called from the pdie macro in gdbinit.in.
23084 It's not static so gcc will keep a copy callable from gdb. */
23085
23086void
23087dump_die (struct die_info *die, int max_level)
23088{
23089 dump_die_1 (gdb_stdlog, 0, max_level, die);
23090}
23091
f9aca02d 23092static void
51545339 23093store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23094{
51545339 23095 void **slot;
c906108c 23096
9c541725
PA
23097 slot = htab_find_slot_with_hash (cu->die_hash, die,
23098 to_underlying (die->sect_off),
b64f50a1 23099 INSERT);
51545339
DJ
23100
23101 *slot = die;
c906108c
SS
23102}
23103
348e048f
DE
23104/* Follow reference or signature attribute ATTR of SRC_DIE.
23105 On entry *REF_CU is the CU of SRC_DIE.
23106 On exit *REF_CU is the CU of the result. */
23107
23108static struct die_info *
ff39bb5e 23109follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23110 struct dwarf2_cu **ref_cu)
23111{
23112 struct die_info *die;
23113
cd6c91b4 23114 if (attr->form_is_ref ())
348e048f 23115 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23116 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23117 die = follow_die_sig (src_die, attr, ref_cu);
23118 else
23119 {
23120 dump_die_for_error (src_die);
23121 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23122 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23123 }
23124
23125 return die;
03dd20cc
DJ
23126}
23127
5c631832 23128/* Follow reference OFFSET.
673bfd45
DE
23129 On entry *REF_CU is the CU of the source die referencing OFFSET.
23130 On exit *REF_CU is the CU of the result.
23131 Returns NULL if OFFSET is invalid. */
f504f079 23132
f9aca02d 23133static struct die_info *
9c541725 23134follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23135 struct dwarf2_cu **ref_cu)
c906108c 23136{
10b3939b 23137 struct die_info temp_die;
f2f0e013 23138 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23139 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23140
348e048f
DE
23141 gdb_assert (cu->per_cu != NULL);
23142
98bfdba5
PA
23143 target_cu = cu;
23144
17e593e9
SM
23145 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23146 "source CU contains target offset: %d",
23147 sect_offset_str (cu->per_cu->sect_off),
23148 sect_offset_str (sect_off),
23149 cu->header.offset_in_cu_p (sect_off));
23150
3019eac3 23151 if (cu->per_cu->is_debug_types)
348e048f
DE
23152 {
23153 /* .debug_types CUs cannot reference anything outside their CU.
23154 If they need to, they have to reference a signatured type via
55f1336d 23155 DW_FORM_ref_sig8. */
4057dfde 23156 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23157 return NULL;
348e048f 23158 }
36586728 23159 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23160 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23161 {
23162 struct dwarf2_per_cu_data *per_cu;
9a619af0 23163
9c541725 23164 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23165 per_objfile);
03dd20cc 23166
17e593e9
SM
23167 dwarf_read_debug_printf_v ("target CU offset: %s, "
23168 "target CU DIEs loaded: %d",
23169 sect_offset_str (per_cu->sect_off),
23170 per_objfile->get_cu (per_cu) != nullptr);
23171
616c069a
SM
23172 /* If necessary, add it to the queue and load its DIEs.
23173
23174 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23175 it doesn't mean they are currently loaded. Since we require them
23176 to be loaded, we must check for ourselves. */
3e9f1ca1 23177 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
616c069a 23178 || per_objfile->get_cu (per_cu) == nullptr)
4a636814 23179 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
3e9f1ca1 23180 false, cu->per_cu->lang);
03dd20cc 23181
976ca316 23182 target_cu = per_objfile->get_cu (per_cu);
616c069a 23183 gdb_assert (target_cu != nullptr);
10b3939b 23184 }
98bfdba5
PA
23185 else if (cu->dies == NULL)
23186 {
23187 /* We're loading full DIEs during partial symbol reading. */
976ca316 23188 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23189 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23190 language_minimal);
98bfdba5 23191 }
c906108c 23192
f2f0e013 23193 *ref_cu = target_cu;
9c541725 23194 temp_die.sect_off = sect_off;
c24bdb02 23195
9a3c8263 23196 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23197 &temp_die,
23198 to_underlying (sect_off));
5c631832 23199}
10b3939b 23200
5c631832
JK
23201/* Follow reference attribute ATTR of SRC_DIE.
23202 On entry *REF_CU is the CU of SRC_DIE.
23203 On exit *REF_CU is the CU of the result. */
23204
23205static struct die_info *
ff39bb5e 23206follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23207 struct dwarf2_cu **ref_cu)
23208{
0826b30a 23209 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23210 struct dwarf2_cu *cu = *ref_cu;
23211 struct die_info *die;
23212
9c541725 23213 die = follow_die_offset (sect_off,
36586728
TT
23214 (attr->form == DW_FORM_GNU_ref_alt
23215 || cu->per_cu->is_dwz),
23216 ref_cu);
5c631832 23217 if (!die)
9d8780f0
SM
23218 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23219 "at %s [in module %s]"),
23220 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23221 objfile_name (cu->per_objfile->objfile));
348e048f 23222
5c631832
JK
23223 return die;
23224}
23225
d4c9a4f8 23226/* See read.h. */
5c631832
JK
23227
23228struct dwarf2_locexpr_baton
9c541725 23229dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23230 dwarf2_per_cu_data *per_cu,
976ca316 23231 dwarf2_per_objfile *per_objfile,
041d9819
SM
23232 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23233 bool resolve_abstract_p)
5c631832 23234{
5c631832
JK
23235 struct die_info *die;
23236 struct attribute *attr;
23237 struct dwarf2_locexpr_baton retval;
976ca316 23238 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23239
976ca316 23240 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23241 if (cu == nullptr)
976ca316 23242 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23243
23244 if (cu == nullptr)
cc12ce38
DE
23245 {
23246 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23247 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23248 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23249 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23250 }
918dd910 23251
9c541725 23252 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23253 if (!die)
9d8780f0
SM
23254 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23255 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23256
23257 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23258 if (!attr && resolve_abstract_p
976ca316
SM
23259 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23260 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23261 {
041d9819 23262 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23263 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23264 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23265
3360b6e7 23266 for (const auto &cand_off
976ca316 23267 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23268 {
3360b6e7
TV
23269 struct dwarf2_cu *cand_cu = cu;
23270 struct die_info *cand
23271 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23272 if (!cand
23273 || !cand->parent
e4a62c65
TV
23274 || cand->parent->tag != DW_TAG_subprogram)
23275 continue;
23276
23277 CORE_ADDR pc_low, pc_high;
23278 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23279 if (pc_low == ((CORE_ADDR) -1))
23280 continue;
23281 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23282 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23283 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23284 continue;
23285
23286 die = cand;
23287 attr = dwarf2_attr (die, DW_AT_location, cu);
23288 break;
23289 }
23290 }
23291
5c631832
JK
23292 if (!attr)
23293 {
e103e986
JK
23294 /* DWARF: "If there is no such attribute, then there is no effect.".
23295 DATA is ignored if SIZE is 0. */
5c631832 23296
e103e986 23297 retval.data = NULL;
5c631832
JK
23298 retval.size = 0;
23299 }
cd6c91b4 23300 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23301 {
23302 struct dwarf2_loclist_baton loclist_baton;
041d9819 23303 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23304 size_t size;
23305
23306 fill_in_loclist_baton (cu, &loclist_baton, attr);
23307
23308 retval.data = dwarf2_find_location_expression (&loclist_baton,
23309 &size, pc);
23310 retval.size = size;
23311 }
5c631832
JK
23312 else
23313 {
4fc6c0d5 23314 if (!attr->form_is_block ())
9d8780f0 23315 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23316 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23317 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23318
9d2246fc
TT
23319 struct dwarf_block *block = attr->as_block ();
23320 retval.data = block->data;
23321 retval.size = block->size;
5c631832 23322 }
976ca316 23323 retval.per_objfile = per_objfile;
5c631832 23324 retval.per_cu = cu->per_cu;
918dd910 23325
976ca316 23326 per_objfile->age_comp_units ();
918dd910 23327
5c631832 23328 return retval;
348e048f
DE
23329}
23330
d4c9a4f8 23331/* See read.h. */
8b9737bf
TT
23332
23333struct dwarf2_locexpr_baton
23334dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23335 dwarf2_per_cu_data *per_cu,
14095eb3 23336 dwarf2_per_objfile *per_objfile,
041d9819 23337 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23338{
9c541725 23339 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23340
14095eb3 23341 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23342 get_frame_pc);
8b9737bf
TT
23343}
23344
b6807d98
TT
23345/* Write a constant of a given type as target-ordered bytes into
23346 OBSTACK. */
23347
23348static const gdb_byte *
23349write_constant_as_bytes (struct obstack *obstack,
23350 enum bfd_endian byte_order,
23351 struct type *type,
23352 ULONGEST value,
23353 LONGEST *len)
23354{
23355 gdb_byte *result;
23356
23357 *len = TYPE_LENGTH (type);
224c3ddb 23358 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23359 store_unsigned_integer (result, *len, byte_order, value);
23360
23361 return result;
23362}
23363
d4c9a4f8 23364/* See read.h. */
b6807d98
TT
23365
23366const gdb_byte *
9c541725 23367dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23368 dwarf2_per_cu_data *per_cu,
14095eb3 23369 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23370 obstack *obstack,
b6807d98
TT
23371 LONGEST *len)
23372{
b6807d98
TT
23373 struct die_info *die;
23374 struct attribute *attr;
23375 const gdb_byte *result = NULL;
23376 struct type *type;
23377 LONGEST value;
23378 enum bfd_endian byte_order;
14095eb3 23379 struct objfile *objfile = per_objfile->objfile;
b6807d98 23380
7188ed02 23381 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23382 if (cu == nullptr)
23383 cu = load_cu (per_cu, per_objfile, false);
23384
23385 if (cu == nullptr)
cc12ce38
DE
23386 {
23387 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23388 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23389 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23390 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23391 }
b6807d98 23392
9c541725 23393 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23394 if (!die)
9d8780f0
SM
23395 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23396 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23397
23398 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23399 if (attr == NULL)
23400 return NULL;
23401
e3b94546 23402 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23403 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23404
23405 switch (attr->form)
23406 {
23407 case DW_FORM_addr:
336d760d 23408 case DW_FORM_addrx:
b6807d98
TT
23409 case DW_FORM_GNU_addr_index:
23410 {
23411 gdb_byte *tem;
23412
23413 *len = cu->header.addr_size;
224c3ddb 23414 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23415 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23416 result = tem;
23417 }
23418 break;
23419 case DW_FORM_string:
23420 case DW_FORM_strp:
cf532bd1 23421 case DW_FORM_strx:
b6807d98
TT
23422 case DW_FORM_GNU_str_index:
23423 case DW_FORM_GNU_strp_alt:
c6481205 23424 /* The string is already allocated on the objfile obstack, point
b6807d98 23425 directly to it. */
2c830f54
TT
23426 {
23427 const char *attr_name = attr->as_string ();
23428 result = (const gdb_byte *) attr_name;
23429 *len = strlen (attr_name);
23430 }
b6807d98
TT
23431 break;
23432 case DW_FORM_block1:
23433 case DW_FORM_block2:
23434 case DW_FORM_block4:
23435 case DW_FORM_block:
23436 case DW_FORM_exprloc:
0224619f 23437 case DW_FORM_data16:
9d2246fc
TT
23438 {
23439 struct dwarf_block *block = attr->as_block ();
23440 result = block->data;
23441 *len = block->size;
23442 }
b6807d98
TT
23443 break;
23444
23445 /* The DW_AT_const_value attributes are supposed to carry the
23446 symbol's value "represented as it would be on the target
23447 architecture." By the time we get here, it's already been
23448 converted to host endianness, so we just need to sign- or
23449 zero-extend it as appropriate. */
23450 case DW_FORM_data1:
23451 type = die_type (die, cu);
23452 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23453 if (result == NULL)
23454 result = write_constant_as_bytes (obstack, byte_order,
23455 type, value, len);
23456 break;
23457 case DW_FORM_data2:
23458 type = die_type (die, cu);
23459 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23460 if (result == NULL)
23461 result = write_constant_as_bytes (obstack, byte_order,
23462 type, value, len);
23463 break;
23464 case DW_FORM_data4:
23465 type = die_type (die, cu);
23466 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23467 if (result == NULL)
23468 result = write_constant_as_bytes (obstack, byte_order,
23469 type, value, len);
23470 break;
23471 case DW_FORM_data8:
23472 type = die_type (die, cu);
23473 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23474 if (result == NULL)
23475 result = write_constant_as_bytes (obstack, byte_order,
23476 type, value, len);
23477 break;
23478
23479 case DW_FORM_sdata:
663c44ac 23480 case DW_FORM_implicit_const:
b6807d98
TT
23481 type = die_type (die, cu);
23482 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23483 type, attr->as_signed (), len);
b6807d98
TT
23484 break;
23485
23486 case DW_FORM_udata:
23487 type = die_type (die, cu);
23488 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23489 type, attr->as_unsigned (), len);
b6807d98
TT
23490 break;
23491
23492 default:
b98664d3 23493 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23494 dwarf_form_name (attr->form));
23495 break;
23496 }
23497
23498 return result;
23499}
23500
d4c9a4f8 23501/* See read.h. */
7942e96e
AA
23502
23503struct type *
9c541725 23504dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3 23505 dwarf2_per_cu_data *per_cu,
386de171
TT
23506 dwarf2_per_objfile *per_objfile,
23507 const char **var_name)
7942e96e 23508{
7942e96e
AA
23509 struct die_info *die;
23510
7188ed02 23511 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23512 if (cu == nullptr)
23513 cu = load_cu (per_cu, per_objfile, false);
23514
23515 if (cu == nullptr)
23516 return nullptr;
7942e96e 23517
9c541725 23518 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23519 if (!die)
23520 return NULL;
23521
386de171
TT
23522 if (var_name != nullptr)
23523 *var_name = var_decl_name (die, cu);
7942e96e
AA
23524 return die_type (die, cu);
23525}
23526
8cb5117c 23527/* See read.h. */
8a9b8146
TT
23528
23529struct type *
b64f50a1 23530dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23531 dwarf2_per_cu_data *per_cu,
23532 dwarf2_per_objfile *per_objfile)
8a9b8146 23533{
9c541725 23534 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23535 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23536}
23537
ac9ec31b 23538/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23539 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23540 On exit *REF_CU is the CU of the result.
23541 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23542
23543static struct die_info *
ac9ec31b
DE
23544follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23545 struct dwarf2_cu **ref_cu)
348e048f 23546{
348e048f 23547 struct die_info temp_die;
8457e5ec 23548 struct dwarf2_cu *sig_cu;
348e048f 23549 struct die_info *die;
976ca316 23550 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23551
348e048f 23552
ac9ec31b
DE
23553 /* While it might be nice to assert sig_type->type == NULL here,
23554 we can get here for DW_AT_imported_declaration where we need
23555 the DIE not the type. */
348e048f 23556
616c069a 23557 /* If necessary, add it to the queue and load its DIEs.
348e048f 23558
616c069a
SM
23559 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23560 it doesn't mean they are currently loaded. Since we require them
23561 to be loaded, we must check for ourselves. */
0d305d5c 23562 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
616c069a 23563 language_minimal)
0d305d5c 23564 || per_objfile->get_cu (sig_type) == nullptr)
976ca316 23565 read_signatured_type (sig_type, per_objfile);
348e048f 23566
0d305d5c 23567 sig_cu = per_objfile->get_cu (sig_type);
69d751e3 23568 gdb_assert (sig_cu != NULL);
9c541725
PA
23569 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23570 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23571 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23572 to_underlying (temp_die.sect_off));
348e048f
DE
23573 if (die)
23574 {
796a7ff8
DE
23575 /* For .gdb_index version 7 keep track of included TUs.
23576 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23577 if (per_objfile->per_bfd->index_table != NULL
23578 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23579 {
ae640021 23580 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23581 }
23582
348e048f
DE
23583 *ref_cu = sig_cu;
23584 return die;
23585 }
23586
ac9ec31b
DE
23587 return NULL;
23588}
23589
23590/* Follow signatured type referenced by ATTR in SRC_DIE.
23591 On entry *REF_CU is the CU of SRC_DIE.
23592 On exit *REF_CU is the CU of the result.
23593 The result is the DIE of the type.
23594 If the referenced type cannot be found an error is thrown. */
23595
23596static struct die_info *
ff39bb5e 23597follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23598 struct dwarf2_cu **ref_cu)
23599{
630ed6b9 23600 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23601 struct signatured_type *sig_type;
23602 struct die_info *die;
23603
23604 gdb_assert (attr->form == DW_FORM_ref_sig8);
23605
a2ce51a0 23606 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23607 /* sig_type will be NULL if the signatured type is missing from
23608 the debug info. */
23609 if (sig_type == NULL)
23610 {
23611 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
23612 " from DIE at %s [in module %s]"),
23613 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23614 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23615 }
23616
23617 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23618 if (die == NULL)
23619 {
23620 dump_die_for_error (src_die);
23621 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23622 " from DIE at %s [in module %s]"),
23623 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23624 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23625 }
23626
23627 return die;
23628}
23629
23630/* Get the type specified by SIGNATURE referenced in DIE/CU,
23631 reading in and processing the type unit if necessary. */
23632
23633static struct type *
23634get_signatured_type (struct die_info *die, ULONGEST signature,
23635 struct dwarf2_cu *cu)
23636{
976ca316 23637 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
23638 struct signatured_type *sig_type;
23639 struct dwarf2_cu *type_cu;
23640 struct die_info *type_die;
23641 struct type *type;
23642
a2ce51a0 23643 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23644 /* sig_type will be NULL if the signatured type is missing from
23645 the debug info. */
23646 if (sig_type == NULL)
23647 {
b98664d3 23648 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23649 " from DIE at %s [in module %s]"),
23650 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23651 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23652 return build_error_marker_type (cu, die);
23653 }
23654
23655 /* If we already know the type we're done. */
976ca316 23656 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
23657 if (type != nullptr)
23658 return type;
ac9ec31b
DE
23659
23660 type_cu = cu;
23661 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23662 if (type_die != NULL)
23663 {
23664 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23665 is created. This is important, for example, because for c++ classes
23666 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23667 type = read_type_die (type_die, type_cu);
23668 if (type == NULL)
23669 {
b98664d3 23670 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23671 " referenced from DIE at %s [in module %s]"),
23672 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23673 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23674 type = build_error_marker_type (cu, die);
23675 }
23676 }
23677 else
23678 {
b98664d3 23679 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23680 " from DIE at %s [in module %s]"),
23681 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23682 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23683 type = build_error_marker_type (cu, die);
23684 }
e286671b 23685
976ca316 23686 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
23687
23688 return type;
23689}
23690
23691/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23692 reading in and processing the type unit if necessary. */
23693
23694static struct type *
ff39bb5e 23695get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23696 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23697{
23698 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23699 if (attr->form_is_ref ())
ac9ec31b
DE
23700 {
23701 struct dwarf2_cu *type_cu = cu;
23702 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23703
23704 return read_type_die (type_die, type_cu);
23705 }
23706 else if (attr->form == DW_FORM_ref_sig8)
23707 {
630ed6b9 23708 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
23709 }
23710 else
23711 {
976ca316 23712 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23713
b98664d3 23714 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23715 " at %s [in module %s]"),
23716 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 23717 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23718 return build_error_marker_type (cu, die);
23719 }
348e048f
DE
23720}
23721
e5fe5e75 23722/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23723
23724static void
ab432490
SM
23725load_full_type_unit (dwarf2_per_cu_data *per_cu,
23726 dwarf2_per_objfile *per_objfile)
348e048f 23727{
52dc124a 23728 struct signatured_type *sig_type;
348e048f 23729
f4dc4d17 23730 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 23731 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 23732
6721b2ec
DE
23733 /* We have the per_cu, but we need the signatured_type.
23734 Fortunately this is an easy translation. */
23735 gdb_assert (per_cu->is_debug_types);
23736 sig_type = (struct signatured_type *) per_cu;
348e048f 23737
7188ed02 23738 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 23739
ab432490 23740 read_signatured_type (sig_type, per_objfile);
348e048f 23741
7188ed02 23742 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
23743}
23744
3019eac3
DE
23745/* Read in a signatured type and build its CU and DIEs.
23746 If the type is a stub for the real type in a DWO file,
23747 read in the real type from the DWO file as well. */
dee91e82
DE
23748
23749static void
ab432490
SM
23750read_signatured_type (signatured_type *sig_type,
23751 dwarf2_per_objfile *per_objfile)
dee91e82 23752{
0d305d5c
TT
23753 gdb_assert (sig_type->is_debug_types);
23754 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
348e048f 23755
0d305d5c 23756 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
23757
23758 if (!reader.dummy_p)
23759 {
23760 struct dwarf2_cu *cu = reader.cu;
23761 const gdb_byte *info_ptr = reader.info_ptr;
23762
23763 gdb_assert (cu->die_hash == NULL);
23764 cu->die_hash =
23765 htab_create_alloc_ex (cu->header.length / 12,
23766 die_hash,
23767 die_eq,
23768 NULL,
23769 &cu->comp_unit_obstack,
23770 hashtab_obstack_allocate,
23771 dummy_obstack_deallocate);
23772
3e225074 23773 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
23774 reader.comp_unit_die->child
23775 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23776 reader.comp_unit_die);
23777 cu->dies = reader.comp_unit_die;
23778 /* comp_unit_die is not stored in die_hash, no need. */
23779
23780 /* We try not to read any attributes in this function, because
23781 not all CUs needed for references have been loaded yet, and
23782 symbol table processing isn't initialized. But we have to
23783 set the CU language, or we won't be able to build types
23784 correctly. Similarly, if we do not read the producer, we can
23785 not apply producer-specific interpretation. */
23786 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
23787
23788 reader.keep ();
c0ab21c2
TT
23789 }
23790
0d305d5c 23791 sig_type->tu_read = 1;
c906108c
SS
23792}
23793
c906108c
SS
23794/* Decode simple location descriptions.
23795 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
23796 the location and return the value. If COMPUTED is non-null, it is
23797 set to true to indicate that decoding was successful, and false
23798 otherwise. If COMPUTED is null, then this function may emit a
23799 complaint. */
c906108c
SS
23800
23801static CORE_ADDR
7d79de9a 23802decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 23803{
5e22e966 23804 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
23805 size_t i;
23806 size_t size = blk->size;
d521ce57 23807 const gdb_byte *data = blk->data;
21ae7a4d
JK
23808 CORE_ADDR stack[64];
23809 int stacki;
23810 unsigned int bytes_read, unsnd;
23811 gdb_byte op;
c906108c 23812
7d79de9a
TT
23813 if (computed != nullptr)
23814 *computed = false;
23815
21ae7a4d
JK
23816 i = 0;
23817 stacki = 0;
23818 stack[stacki] = 0;
23819 stack[++stacki] = 0;
23820
23821 while (i < size)
23822 {
23823 op = data[i++];
23824 switch (op)
23825 {
23826 case DW_OP_lit0:
23827 case DW_OP_lit1:
23828 case DW_OP_lit2:
23829 case DW_OP_lit3:
23830 case DW_OP_lit4:
23831 case DW_OP_lit5:
23832 case DW_OP_lit6:
23833 case DW_OP_lit7:
23834 case DW_OP_lit8:
23835 case DW_OP_lit9:
23836 case DW_OP_lit10:
23837 case DW_OP_lit11:
23838 case DW_OP_lit12:
23839 case DW_OP_lit13:
23840 case DW_OP_lit14:
23841 case DW_OP_lit15:
23842 case DW_OP_lit16:
23843 case DW_OP_lit17:
23844 case DW_OP_lit18:
23845 case DW_OP_lit19:
23846 case DW_OP_lit20:
23847 case DW_OP_lit21:
23848 case DW_OP_lit22:
23849 case DW_OP_lit23:
23850 case DW_OP_lit24:
23851 case DW_OP_lit25:
23852 case DW_OP_lit26:
23853 case DW_OP_lit27:
23854 case DW_OP_lit28:
23855 case DW_OP_lit29:
23856 case DW_OP_lit30:
23857 case DW_OP_lit31:
23858 stack[++stacki] = op - DW_OP_lit0;
23859 break;
f1bea926 23860
21ae7a4d
JK
23861 case DW_OP_reg0:
23862 case DW_OP_reg1:
23863 case DW_OP_reg2:
23864 case DW_OP_reg3:
23865 case DW_OP_reg4:
23866 case DW_OP_reg5:
23867 case DW_OP_reg6:
23868 case DW_OP_reg7:
23869 case DW_OP_reg8:
23870 case DW_OP_reg9:
23871 case DW_OP_reg10:
23872 case DW_OP_reg11:
23873 case DW_OP_reg12:
23874 case DW_OP_reg13:
23875 case DW_OP_reg14:
23876 case DW_OP_reg15:
23877 case DW_OP_reg16:
23878 case DW_OP_reg17:
23879 case DW_OP_reg18:
23880 case DW_OP_reg19:
23881 case DW_OP_reg20:
23882 case DW_OP_reg21:
23883 case DW_OP_reg22:
23884 case DW_OP_reg23:
23885 case DW_OP_reg24:
23886 case DW_OP_reg25:
23887 case DW_OP_reg26:
23888 case DW_OP_reg27:
23889 case DW_OP_reg28:
23890 case DW_OP_reg29:
23891 case DW_OP_reg30:
23892 case DW_OP_reg31:
23893 stack[++stacki] = op - DW_OP_reg0;
23894 if (i < size)
7d79de9a
TT
23895 {
23896 if (computed == nullptr)
23897 dwarf2_complex_location_expr_complaint ();
23898 else
23899 return 0;
23900 }
21ae7a4d 23901 break;
c906108c 23902
21ae7a4d
JK
23903 case DW_OP_regx:
23904 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23905 i += bytes_read;
23906 stack[++stacki] = unsnd;
23907 if (i < size)
7d79de9a
TT
23908 {
23909 if (computed == nullptr)
23910 dwarf2_complex_location_expr_complaint ();
23911 else
23912 return 0;
23913 }
21ae7a4d 23914 break;
c906108c 23915
21ae7a4d 23916 case DW_OP_addr:
c8a7a66f
TT
23917 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23918 &bytes_read);
21ae7a4d
JK
23919 i += bytes_read;
23920 break;
d53d4ac5 23921
21ae7a4d
JK
23922 case DW_OP_const1u:
23923 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23924 i += 1;
23925 break;
23926
23927 case DW_OP_const1s:
23928 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23929 i += 1;
23930 break;
23931
23932 case DW_OP_const2u:
23933 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23934 i += 2;
23935 break;
23936
23937 case DW_OP_const2s:
23938 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23939 i += 2;
23940 break;
d53d4ac5 23941
21ae7a4d
JK
23942 case DW_OP_const4u:
23943 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23944 i += 4;
23945 break;
23946
23947 case DW_OP_const4s:
23948 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23949 i += 4;
23950 break;
23951
585861ea
JK
23952 case DW_OP_const8u:
23953 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23954 i += 8;
23955 break;
23956
21ae7a4d
JK
23957 case DW_OP_constu:
23958 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23959 &bytes_read);
23960 i += bytes_read;
23961 break;
23962
23963 case DW_OP_consts:
23964 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23965 i += bytes_read;
23966 break;
23967
23968 case DW_OP_dup:
23969 stack[stacki + 1] = stack[stacki];
23970 stacki++;
23971 break;
23972
23973 case DW_OP_plus:
23974 stack[stacki - 1] += stack[stacki];
23975 stacki--;
23976 break;
23977
23978 case DW_OP_plus_uconst:
23979 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23980 &bytes_read);
23981 i += bytes_read;
23982 break;
23983
23984 case DW_OP_minus:
23985 stack[stacki - 1] -= stack[stacki];
23986 stacki--;
23987 break;
23988
23989 case DW_OP_deref:
23990 /* If we're not the last op, then we definitely can't encode
23991 this using GDB's address_class enum. This is valid for partial
23992 global symbols, although the variable's address will be bogus
23993 in the psymtab. */
23994 if (i < size)
7d79de9a
TT
23995 {
23996 if (computed == nullptr)
23997 dwarf2_complex_location_expr_complaint ();
23998 else
23999 return 0;
24000 }
21ae7a4d
JK
24001 break;
24002
dda83cd7 24003 case DW_OP_GNU_push_tls_address:
4aa4e28b 24004 case DW_OP_form_tls_address:
21ae7a4d
JK
24005 /* The top of the stack has the offset from the beginning
24006 of the thread control block at which the variable is located. */
24007 /* Nothing should follow this operator, so the top of stack would
24008 be returned. */
24009 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24010 address will be bogus in the psymtab. Make it always at least
24011 non-zero to not look as a variable garbage collected by linker
24012 which have DW_OP_addr 0. */
21ae7a4d 24013 if (i < size)
7d79de9a
TT
24014 {
24015 if (computed == nullptr)
24016 dwarf2_complex_location_expr_complaint ();
24017 else
24018 return 0;
24019 }
585861ea 24020 stack[stacki]++;
dda83cd7 24021 break;
21ae7a4d
JK
24022
24023 case DW_OP_GNU_uninit:
7d79de9a
TT
24024 if (computed != nullptr)
24025 return 0;
21ae7a4d
JK
24026 break;
24027
336d760d 24028 case DW_OP_addrx:
3019eac3 24029 case DW_OP_GNU_addr_index:
49f6c839 24030 case DW_OP_GNU_const_index:
3019eac3
DE
24031 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24032 &bytes_read);
24033 i += bytes_read;
24034 break;
24035
21ae7a4d 24036 default:
7d79de9a
TT
24037 if (computed == nullptr)
24038 {
24039 const char *name = get_DW_OP_name (op);
21ae7a4d 24040
7d79de9a
TT
24041 if (name)
24042 complaint (_("unsupported stack op: '%s'"),
24043 name);
24044 else
24045 complaint (_("unsupported stack op: '%02x'"),
24046 op);
24047 }
21ae7a4d
JK
24048
24049 return (stack[stacki]);
d53d4ac5 24050 }
3c6e0cb3 24051
21ae7a4d 24052 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24053 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24054 if (stacki >= ARRAY_SIZE (stack) - 1)
24055 {
7d79de9a
TT
24056 if (computed == nullptr)
24057 complaint (_("location description stack overflow"));
21ae7a4d
JK
24058 return 0;
24059 }
24060
24061 if (stacki <= 0)
24062 {
7d79de9a
TT
24063 if (computed == nullptr)
24064 complaint (_("location description stack underflow"));
21ae7a4d
JK
24065 return 0;
24066 }
24067 }
7d79de9a
TT
24068
24069 if (computed != nullptr)
24070 *computed = true;
21ae7a4d 24071 return (stack[stacki]);
c906108c
SS
24072}
24073
24074/* memory allocation interface */
24075
c906108c 24076static struct dwarf_block *
7b5a2f43 24077dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24078{
8d749320 24079 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24080}
24081
c906108c 24082static struct die_info *
b60c80d6 24083dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24084{
24085 struct die_info *die;
b60c80d6
DJ
24086 size_t size = sizeof (struct die_info);
24087
24088 if (num_attrs > 1)
24089 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24090
b60c80d6 24091 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24092 memset (die, 0, sizeof (struct die_info));
24093 return (die);
24094}
2e276125
JB
24095
24096\f
a036ba48 24097
c90ec28a 24098/* Macro support. */
cf2c3c16 24099
9eac9650
TT
24100/* An overload of dwarf_decode_macros that finds the correct section
24101 and ensures it is read in before calling the other overload. */
24102
24103static void
24104dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24105 int section_is_gnu)
24106{
976ca316
SM
24107 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24108 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24109 const struct line_header *lh = cu->line_header;
9eac9650
TT
24110 unsigned int offset_size = cu->header.offset_size;
24111 struct dwarf2_section_info *section;
24112 const char *section_name;
24113
24114 if (cu->dwo_unit != nullptr)
24115 {
24116 if (section_is_gnu)
24117 {
24118 section = &cu->dwo_unit->dwo_file->sections.macro;
24119 section_name = ".debug_macro.dwo";
24120 }
24121 else
24122 {
24123 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24124 section_name = ".debug_macinfo.dwo";
24125 }
24126 }
24127 else
24128 {
24129 if (section_is_gnu)
24130 {
976ca316 24131 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24132 section_name = ".debug_macro";
24133 }
24134 else
24135 {
976ca316 24136 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24137 section_name = ".debug_macinfo";
24138 }
24139 }
24140
24141 section->read (objfile);
24142 if (section->buffer == nullptr)
24143 {
24144 complaint (_("missing %s section"), section_name);
24145 return;
24146 }
24147
24148 buildsym_compunit *builder = cu->get_builder ();
24149
048fde1e 24150 struct dwarf2_section_info *str_offsets_section;
24151 struct dwarf2_section_info *str_section;
24152 ULONGEST str_offsets_base;
24153
24154 if (cu->dwo_unit != nullptr)
24155 {
24156 str_offsets_section = &cu->dwo_unit->dwo_file
24157 ->sections.str_offsets;
24158 str_section = &cu->dwo_unit->dwo_file->sections.str;
24159 str_offsets_base = cu->header.addr_size;
24160 }
24161 else
24162 {
24163 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24164 str_section = &per_objfile->per_bfd->str;
24165 str_offsets_base = *cu->str_offsets_base;
24166 }
24167
976ca316 24168 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24169 offset_size, offset, str_section, str_offsets_section,
24170 str_offsets_base, section_is_gnu);
9eac9650
TT
24171}
24172
3019eac3
DE
24173/* Return the .debug_loc section to use for CU.
24174 For DWO files use .debug_loc.dwo. */
24175
24176static struct dwarf2_section_info *
24177cu_debug_loc_section (struct dwarf2_cu *cu)
24178{
976ca316 24179 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24180
3019eac3 24181 if (cu->dwo_unit)
43988095
JK
24182 {
24183 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24184
43988095
JK
24185 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24186 }
976ca316
SM
24187 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24188 : &per_objfile->per_bfd->loc);
3019eac3
DE
24189}
24190
d0ce17d8
CT
24191/* Return the .debug_rnglists section to use for CU. */
24192static struct dwarf2_section_info *
24193cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24194{
24195 if (cu->header.version < 5)
24196 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24197 cu->header.version);
24198 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24199
24200 /* Make sure we read the .debug_rnglists section from the file that
24201 contains the DW_AT_ranges attribute we are reading. Normally that
24202 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24203 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24204 program. */
24205 if (cu->dwo_unit != nullptr
24206 && tag != DW_TAG_compile_unit
24207 && tag != DW_TAG_skeleton_unit)
24208 {
24209 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24210
24211 if (sections->rnglists.size > 0)
24212 return &sections->rnglists;
24213 else
24214 error (_(".debug_rnglists section is missing from .dwo file."));
24215 }
24216 return &dwarf2_per_objfile->per_bfd->rnglists;
24217}
24218
8cf6f0b1
TT
24219/* A helper function that fills in a dwarf2_loclist_baton. */
24220
24221static void
24222fill_in_loclist_baton (struct dwarf2_cu *cu,
24223 struct dwarf2_loclist_baton *baton,
ff39bb5e 24224 const struct attribute *attr)
8cf6f0b1 24225{
976ca316 24226 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24227 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24228
976ca316 24229 section->read (per_objfile->objfile);
8cf6f0b1 24230
976ca316 24231 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24232 baton->per_cu = cu->per_cu;
24233 gdb_assert (baton->per_cu);
24234 /* We don't know how long the location list is, but make sure we
24235 don't run off the edge of the section. */
d4df075e
TT
24236 baton->size = section->size - attr->as_unsigned ();
24237 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24238 if (cu->base_address.has_value ())
24239 baton->base_address = *cu->base_address;
24240 else
24241 baton->base_address = 0;
f664829e 24242 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24243}
24244
4c2df51b 24245static void
ff39bb5e 24246dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24247 struct dwarf2_cu *cu, int is_block)
4c2df51b 24248{
976ca316
SM
24249 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24250 struct objfile *objfile = per_objfile->objfile;
3019eac3 24251 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24252
cd6c91b4 24253 if (attr->form_is_section_offset ()
3019eac3 24254 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24255 the section. If so, fall through to the complaint in the
24256 other branch. */
d4df075e 24257 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24258 {
0d53c4c4 24259 struct dwarf2_loclist_baton *baton;
4c2df51b 24260
8d749320 24261 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24262
8cf6f0b1 24263 fill_in_loclist_baton (cu, baton, attr);
be391dca 24264
2b24b6e4 24265 if (!cu->base_address.has_value ())
b98664d3 24266 complaint (_("Location list used without "
3e43a32a 24267 "specifying the CU base address."));
4c2df51b 24268
f1e6e072
TT
24269 SYMBOL_ACLASS_INDEX (sym) = (is_block
24270 ? dwarf2_loclist_block_index
24271 : dwarf2_loclist_index);
0d53c4c4
DJ
24272 SYMBOL_LOCATION_BATON (sym) = baton;
24273 }
24274 else
24275 {
24276 struct dwarf2_locexpr_baton *baton;
24277
8d749320 24278 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24279 baton->per_objfile = per_objfile;
ae0d2f24
UW
24280 baton->per_cu = cu->per_cu;
24281 gdb_assert (baton->per_cu);
0d53c4c4 24282
4fc6c0d5 24283 if (attr->form_is_block ())
0d53c4c4
DJ
24284 {
24285 /* Note that we're just copying the block's data pointer
24286 here, not the actual data. We're still pointing into the
6502dd73
DJ
24287 info_buffer for SYM's objfile; right now we never release
24288 that buffer, but when we do clean up properly this may
24289 need to change. */
9d2246fc
TT
24290 struct dwarf_block *block = attr->as_block ();
24291 baton->size = block->size;
24292 baton->data = block->data;
0d53c4c4
DJ
24293 }
24294 else
24295 {
24296 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24297 sym->natural_name ());
0d53c4c4 24298 baton->size = 0;
0d53c4c4 24299 }
6e70227d 24300
f1e6e072
TT
24301 SYMBOL_ACLASS_INDEX (sym) = (is_block
24302 ? dwarf2_locexpr_block_index
24303 : dwarf2_locexpr_index);
0d53c4c4
DJ
24304 SYMBOL_LOCATION_BATON (sym) = baton;
24305 }
4c2df51b 24306}
6502dd73 24307
2e6a9f79 24308/* See read.h. */
96408a79 24309
2e6a9f79
SM
24310const comp_unit_head *
24311dwarf2_per_cu_data::get_header () const
96408a79 24312{
2e6a9f79
SM
24313 if (!m_header_read_in)
24314 {
24315 const gdb_byte *info_ptr
24316 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24317
2e6a9f79 24318 memset (&m_header, 0, sizeof (m_header));
96408a79 24319
2e6a9f79
SM
24320 read_comp_unit_head (&m_header, info_ptr, this->section,
24321 rcuh_kind::COMPILE);
a9f172c6
TT
24322
24323 m_header_read_in = true;
2e6a9f79 24324 }
96408a79 24325
2e6a9f79 24326 return &m_header;
96408a79
SA
24327}
24328
09ba997f 24329/* See read.h. */
ae0d2f24 24330
98714339 24331int
09ba997f 24332dwarf2_per_cu_data::addr_size () const
ae0d2f24 24333{
2e6a9f79 24334 return this->get_header ()->addr_size;
ae0d2f24
UW
24335}
24336
09ba997f 24337/* See read.h. */
9eae7c52
TT
24338
24339int
09ba997f 24340dwarf2_per_cu_data::offset_size () const
9eae7c52 24341{
2e6a9f79 24342 return this->get_header ()->offset_size;
96408a79
SA
24343}
24344
09ba997f 24345/* See read.h. */
96408a79
SA
24346
24347int
09ba997f 24348dwarf2_per_cu_data::ref_addr_size () const
96408a79 24349{
2e6a9f79 24350 const comp_unit_head *header = this->get_header ();
96408a79 24351
2e6a9f79
SM
24352 if (header->version == 2)
24353 return header->addr_size;
96408a79 24354 else
2e6a9f79 24355 return header->offset_size;
181cebd4
JK
24356}
24357
22b6cd70
TT
24358/* A helper function for dwarf2_find_containing_comp_unit that returns
24359 the index of the result, and that searches a vector. It will
24360 return a result even if the offset in question does not actually
24361 occur in any CU. This is separate so that it can be unit
24362 tested. */
ae038cb0 24363
22b6cd70
TT
24364static int
24365dwarf2_find_containing_comp_unit
24366 (sect_offset sect_off,
24367 unsigned int offset_in_dwz,
473ab964 24368 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
ae038cb0 24369{
ae038cb0
DJ
24370 int low, high;
24371
ae038cb0 24372 low = 0;
22b6cd70 24373 high = all_comp_units.size () - 1;
ae038cb0
DJ
24374 while (high > low)
24375 {
36586728 24376 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24377 int mid = low + (high - low) / 2;
9a619af0 24378
0d305d5c 24379 mid_cu = all_comp_units[mid].get ();
36586728 24380 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24381 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24382 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24383 high = mid;
24384 else
24385 low = mid + 1;
24386 }
24387 gdb_assert (low == high);
22b6cd70
TT
24388 return low;
24389}
24390
24391/* Locate the .debug_info compilation unit from CU's objfile which contains
24392 the DIE at OFFSET. Raises an error on failure. */
24393
24394static struct dwarf2_per_cu_data *
24395dwarf2_find_containing_comp_unit (sect_offset sect_off,
24396 unsigned int offset_in_dwz,
976ca316 24397 dwarf2_per_objfile *per_objfile)
22b6cd70 24398{
976ca316
SM
24399 int low = dwarf2_find_containing_comp_unit
24400 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
0d305d5c
TT
24401 dwarf2_per_cu_data *this_cu
24402 = per_objfile->per_bfd->all_comp_units[low].get ();
22b6cd70 24403
45b8ae0c 24404 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24405 {
36586728 24406 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24407 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24408 "offset %s [in module %s]"),
24409 sect_offset_str (sect_off),
976ca316 24410 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24411
976ca316 24412 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24413 <= sect_off);
0d305d5c 24414 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
ae038cb0
DJ
24415 }
24416 else
24417 {
976ca316 24418 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24419 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24420 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24421 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24422 return this_cu;
24423 }
24424}
24425
22b6cd70
TT
24426#if GDB_SELF_TEST
24427
24428namespace selftests {
24429namespace find_containing_comp_unit {
24430
24431static void
24432run_test ()
24433{
473ab964 24434 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
0d305d5c 24435 dwarf2_per_cu_data *one_ptr = one.get ();
473ab964 24436 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
0d305d5c 24437 dwarf2_per_cu_data *two_ptr = two.get ();
473ab964 24438 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
0d305d5c 24439 dwarf2_per_cu_data *three_ptr = three.get ();
473ab964 24440 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
0d305d5c
TT
24441 dwarf2_per_cu_data *four_ptr = four.get ();
24442
24443 one->length = 5;
24444 two->sect_off = sect_offset (one->length);
24445 two->length = 7;
24446
24447 three->length = 5;
24448 three->is_dwz = 1;
24449 four->sect_off = sect_offset (three->length);
24450 four->length = 7;
24451 four->is_dwz = 1;
24452
473ab964 24453 std::vector<dwarf2_per_cu_data_up> units;
0d305d5c
TT
24454 units.push_back (std::move (one));
24455 units.push_back (std::move (two));
24456 units.push_back (std::move (three));
24457 units.push_back (std::move (four));
22b6cd70
TT
24458
24459 int result;
24460
24461 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
0d305d5c 24462 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24463 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
0d305d5c 24464 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24465 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
0d305d5c 24466 SELF_CHECK (units[result].get () == two_ptr);
22b6cd70
TT
24467
24468 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
0d305d5c 24469 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24470 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
0d305d5c 24471 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24472 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
0d305d5c 24473 SELF_CHECK (units[result].get () == four_ptr);
22b6cd70
TT
24474}
24475
24476}
24477}
24478
24479#endif /* GDB_SELF_TEST */
24480
9816fde3
JK
24481/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24482
24483static void
95554aad
TT
24484prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24485 enum language pretend_language)
9816fde3
JK
24486{
24487 struct attribute *attr;
24488
bf1dcdb3
TT
24489 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24490
9816fde3
JK
24491 /* Set the language we're debugging. */
24492 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
bf1dcdb3
TT
24493 if (cu->producer != nullptr
24494 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9cded63f 24495 {
bf1dcdb3
TT
24496 /* The XLCL doesn't generate DW_LANG_OpenCL because this
24497 attribute is not standardised yet. As a workaround for the
24498 language detection we fall back to the DW_AT_producer
24499 string. */
3e9f1ca1 24500 cu->per_cu->lang = language_opencl;
9cded63f 24501 }
bf1dcdb3
TT
24502 else if (cu->producer != nullptr
24503 && strstr (cu->producer, "GNU Go ") != NULL)
24504 {
24505 /* Similar hack for Go. */
3e9f1ca1 24506 cu->per_cu->lang = language_go;
bf1dcdb3
TT
24507 }
24508 else if (attr != nullptr)
3e9f1ca1 24509 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
bf1dcdb3 24510 else
3e9f1ca1
TT
24511 cu->per_cu->lang = pretend_language;
24512 cu->language_defn = language_def (cu->per_cu->lang);
93311388
DE
24513}
24514
7188ed02 24515/* See read.h. */
ae038cb0 24516
7188ed02
SM
24517dwarf2_cu *
24518dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24519{
7188ed02
SM
24520 auto it = m_dwarf2_cus.find (per_cu);
24521 if (it == m_dwarf2_cus.end ())
24522 return nullptr;
ae038cb0 24523
7188ed02
SM
24524 return it->second;
24525}
24526
24527/* See read.h. */
24528
24529void
24530dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24531{
24532 gdb_assert (this->get_cu (per_cu) == nullptr);
24533
24534 m_dwarf2_cus[per_cu] = cu;
24535}
24536
24537/* See read.h. */
24538
24539void
24540dwarf2_per_objfile::age_comp_units ()
24541{
17e593e9
SM
24542 dwarf_read_debug_printf_v ("running");
24543
08ac5771
SM
24544 /* This is not expected to be called in the middle of CU expansion. There is
24545 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24546 loaded in memory. Calling age_comp_units while the queue is in use could
24547 make us free the DIEs for a CU that is in the queue and therefore break
24548 that invariant. */
24549 gdb_assert (!this->per_bfd->queue.has_value ());
24550
7188ed02
SM
24551 /* Start by clearing all marks. */
24552 for (auto pair : m_dwarf2_cus)
347212b8 24553 pair.second->clear_mark ();
7188ed02
SM
24554
24555 /* Traverse all CUs, mark them and their dependencies if used recently
24556 enough. */
24557 for (auto pair : m_dwarf2_cus)
ae038cb0 24558 {
7188ed02
SM
24559 dwarf2_cu *cu = pair.second;
24560
24561 cu->last_used++;
24562 if (cu->last_used <= dwarf_max_cache_age)
347212b8 24563 cu->mark ();
ae038cb0
DJ
24564 }
24565
7188ed02
SM
24566 /* Delete all CUs still not marked. */
24567 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24568 {
7188ed02 24569 dwarf2_cu *cu = it->second;
ae038cb0 24570
347212b8 24571 if (!cu->is_marked ())
ae038cb0 24572 {
17e593e9
SM
24573 dwarf_read_debug_printf_v ("deleting old CU %s",
24574 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
24575 delete cu;
24576 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24577 }
24578 else
7188ed02 24579 it++;
ae038cb0
DJ
24580 }
24581}
24582
7188ed02 24583/* See read.h. */
ae038cb0 24584
7188ed02
SM
24585void
24586dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24587{
7188ed02
SM
24588 auto it = m_dwarf2_cus.find (per_cu);
24589 if (it == m_dwarf2_cus.end ())
24590 return;
ae038cb0 24591
7188ed02 24592 delete it->second;
ae038cb0 24593
7188ed02
SM
24594 m_dwarf2_cus.erase (it);
24595}
ae038cb0 24596
7188ed02
SM
24597dwarf2_per_objfile::~dwarf2_per_objfile ()
24598{
24599 remove_all_cus ();
ae038cb0
DJ
24600}
24601
dee91e82
DE
24602/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24603 We store these in a hash table separate from the DIEs, and preserve them
24604 when the DIEs are flushed out of cache.
24605
24606 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24607 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24608 or the type may come from a DWO file. Furthermore, while it's more logical
24609 to use per_cu->section+offset, with Fission the section with the data is in
24610 the DWO file but we don't know that section at the point we need it.
24611 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24612 because we can enter the lookup routine, get_die_type_at_offset, from
24613 outside this file, and thus won't necessarily have PER_CU->cu.
24614 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24615
dee91e82 24616struct dwarf2_per_cu_offset_and_type
1c379e20 24617{
dee91e82 24618 const struct dwarf2_per_cu_data *per_cu;
9c541725 24619 sect_offset sect_off;
1c379e20
DJ
24620 struct type *type;
24621};
24622
dee91e82 24623/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24624
24625static hashval_t
dee91e82 24626per_cu_offset_and_type_hash (const void *item)
1c379e20 24627{
9a3c8263
SM
24628 const struct dwarf2_per_cu_offset_and_type *ofs
24629 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24630
9c541725 24631 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24632}
24633
dee91e82 24634/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24635
24636static int
dee91e82 24637per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24638{
9a3c8263
SM
24639 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24640 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24641 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24642 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24643
dee91e82 24644 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24645 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24646}
24647
24648/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24649 table if necessary. For convenience, return TYPE.
24650
24651 The DIEs reading must have careful ordering to:
85102364 24652 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24653 reading current DIE.
24654 * Not trying to dereference contents of still incompletely read in types
24655 while reading in other DIEs.
24656 * Enable referencing still incompletely read in types just by a pointer to
24657 the type without accessing its fields.
24658
24659 Therefore caller should follow these rules:
24660 * Try to fetch any prerequisite types we may need to build this DIE type
24661 before building the type and calling set_die_type.
e71ec853 24662 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24663 possible before fetching more types to complete the current type.
24664 * Make the type as complete as possible before fetching more types. */
1c379e20 24665
f792889a 24666static struct type *
57567375
TT
24667set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24668 bool skip_data_location)
1c379e20 24669{
976ca316 24670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 24671 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 24672 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
24673 struct attribute *attr;
24674 struct dynamic_prop prop;
1c379e20 24675
b4ba55a1
JB
24676 /* For Ada types, make sure that the gnat-specific data is always
24677 initialized (if not already set). There are a few types where
24678 we should not be doing so, because the type-specific area is
24679 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24680 where the type-specific area is used to store the floatformat).
24681 But this is not a problem, because the gnat-specific information
24682 is actually not needed for these types. */
24683 if (need_gnat_info (cu)
78134374
SM
24684 && type->code () != TYPE_CODE_FUNC
24685 && type->code () != TYPE_CODE_FLT
24686 && type->code () != TYPE_CODE_METHODPTR
24687 && type->code () != TYPE_CODE_MEMBERPTR
24688 && type->code () != TYPE_CODE_METHOD
09584414 24689 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
24690 && !HAVE_GNAT_AUX_INFO (type))
24691 INIT_GNAT_SPECIFIC (type);
24692
3f2f83dd
KB
24693 /* Read DW_AT_allocated and set in type. */
24694 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 24695 if (attr != NULL)
3f2f83dd 24696 {
293e7e51 24697 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24698 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24699 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 24700 }
3f2f83dd
KB
24701
24702 /* Read DW_AT_associated and set in type. */
24703 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 24704 if (attr != NULL)
3f2f83dd 24705 {
293e7e51 24706 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24707 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24708 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 24709 }
3f2f83dd 24710
3cdcd0ce 24711 /* Read DW_AT_data_location and set in type. */
57567375
TT
24712 if (!skip_data_location)
24713 {
24714 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24715 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24716 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24717 }
3cdcd0ce 24718
976ca316
SM
24719 if (per_objfile->die_type_hash == NULL)
24720 per_objfile->die_type_hash
0335378b
TT
24721 = htab_up (htab_create_alloc (127,
24722 per_cu_offset_and_type_hash,
24723 per_cu_offset_and_type_eq,
24724 NULL, xcalloc, xfree));
1c379e20 24725
dee91e82 24726 ofs.per_cu = cu->per_cu;
9c541725 24727 ofs.sect_off = die->sect_off;
1c379e20 24728 ofs.type = type;
dee91e82 24729 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 24730 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24731 if (*slot)
b98664d3 24732 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24733 sect_offset_str (die->sect_off));
8d749320
SM
24734 *slot = XOBNEW (&objfile->objfile_obstack,
24735 struct dwarf2_per_cu_offset_and_type);
1c379e20 24736 **slot = ofs;
f792889a 24737 return type;
1c379e20
DJ
24738}
24739
9c541725 24740/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24741 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24742
24743static struct type *
9c541725 24744get_die_type_at_offset (sect_offset sect_off,
aa66c379 24745 dwarf2_per_cu_data *per_cu,
976ca316 24746 dwarf2_per_objfile *per_objfile)
1c379e20 24747{
dee91e82 24748 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 24749
976ca316 24750 if (per_objfile->die_type_hash == NULL)
f792889a 24751 return NULL;
1c379e20 24752
dee91e82 24753 ofs.per_cu = per_cu;
9c541725 24754 ofs.sect_off = sect_off;
9a3c8263 24755 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 24756 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24757 if (slot)
24758 return slot->type;
24759 else
24760 return NULL;
24761}
24762
02142a6c 24763/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24764 or return NULL if DIE does not have a saved type. */
24765
24766static struct type *
24767get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24768{
aa66c379 24769 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
24770}
24771
72bf9492
DJ
24772/* Trivial hash function for partial_die_info: the hash value of a DIE
24773 is its offset in .debug_info for this objfile. */
24774
24775static hashval_t
24776partial_die_hash (const void *item)
24777{
9a3c8263
SM
24778 const struct partial_die_info *part_die
24779 = (const struct partial_die_info *) item;
9a619af0 24780
9c541725 24781 return to_underlying (part_die->sect_off);
72bf9492
DJ
24782}
24783
24784/* Trivial comparison function for partial_die_info structures: two DIEs
24785 are equal if they have the same offset. */
24786
24787static int
24788partial_die_eq (const void *item_lhs, const void *item_rhs)
24789{
9a3c8263
SM
24790 const struct partial_die_info *part_die_lhs
24791 = (const struct partial_die_info *) item_lhs;
24792 const struct partial_die_info *part_die_rhs
24793 = (const struct partial_die_info *) item_rhs;
9a619af0 24794
9c541725 24795 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24796}
24797
3c3bb058
AB
24798struct cmd_list_element *set_dwarf_cmdlist;
24799struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 24800
9291a0cd 24801static void
cd4fb1b2
SM
24802show_check_physname (struct ui_file *file, int from_tty,
24803 struct cmd_list_element *c, const char *value)
9291a0cd 24804{
cd4fb1b2
SM
24805 fprintf_filtered (file,
24806 _("Whether to check \"physname\" is %s.\n"),
24807 value);
9291a0cd
TT
24808}
24809
6c265988 24810void _initialize_dwarf2_read ();
cd4fb1b2 24811void
6c265988 24812_initialize_dwarf2_read ()
9291a0cd 24813{
0743fc83 24814 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24815Set DWARF specific variables.\n\
590042fc 24816Configure DWARF variables such as the cache size."),
2f822da5 24817 &set_dwarf_cmdlist,
0743fc83 24818 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24819
0743fc83 24820 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24821Show DWARF specific variables.\n\
24822Show DWARF variables such as the cache size."),
2f822da5 24823 &show_dwarf_cmdlist,
0743fc83 24824 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24825
cd4fb1b2
SM
24826 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24827 &dwarf_max_cache_age, _("\
24828Set the upper bound on the age of cached DWARF compilation units."), _("\
24829Show the upper bound on the age of cached DWARF compilation units."), _("\
24830A higher limit means that cached compilation units will be stored\n\
24831in memory longer, and more total memory will be used. Zero disables\n\
24832caching, which can slow down startup."),
24833 NULL,
24834 show_dwarf_max_cache_age,
24835 &set_dwarf_cmdlist,
24836 &show_dwarf_cmdlist);
156942c7 24837
cd4fb1b2
SM
24838 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24839Set debugging of the DWARF reader."), _("\
24840Show debugging of the DWARF reader."), _("\
24841When enabled (non-zero), debugging messages are printed during DWARF\n\
24842reading and symtab expansion. A value of 1 (one) provides basic\n\
24843information. A value greater than 1 provides more verbose information."),
24844 NULL,
24845 NULL,
24846 &setdebuglist, &showdebuglist);
9291a0cd 24847
3a706c17 24848 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
cd4fb1b2
SM
24849Set debugging of the DWARF DIE reader."), _("\
24850Show debugging of the DWARF DIE reader."), _("\
3a706c17 24851When enabled (non-zero), DIEs are dumped after they are read in.\n\
cd4fb1b2 24852The value is the maximum depth to print."),
3a706c17
SM
24853 NULL,
24854 NULL,
24855 &setdebuglist, &showdebuglist);
9291a0cd 24856
cd4fb1b2
SM
24857 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24858Set debugging of the dwarf line reader."), _("\
24859Show debugging of the dwarf line reader."), _("\
24860When enabled (non-zero), line number entries are dumped as they are read in.\n\
24861A value of 1 (one) provides basic information.\n\
24862A value greater than 1 provides more verbose information."),
24863 NULL,
24864 NULL,
24865 &setdebuglist, &showdebuglist);
437afbb8 24866
cd4fb1b2
SM
24867 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24868Set cross-checking of \"physname\" code against demangler."), _("\
24869Show cross-checking of \"physname\" code against demangler."), _("\
24870When enabled, GDB's internal \"physname\" code is checked against\n\
24871the demangler."),
24872 NULL, show_check_physname,
24873 &setdebuglist, &showdebuglist);
900e11f9 24874
e615022a
DE
24875 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24876 no_class, &use_deprecated_index_sections, _("\
24877Set whether to use deprecated gdb_index sections."), _("\
24878Show whether to use deprecated gdb_index sections."), _("\
24879When enabled, deprecated .gdb_index sections are used anyway.\n\
24880Normally they are ignored either because of a missing feature or\n\
24881performance issue.\n\
24882Warning: This option must be enabled before gdb reads the file."),
24883 NULL,
24884 NULL,
24885 &setlist, &showlist);
24886
f1e6e072
TT
24887 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24888 &dwarf2_locexpr_funcs);
24889 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24890 &dwarf2_loclist_funcs);
24891
24892 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24893 &dwarf2_block_frame_base_locexpr_funcs);
24894 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24895 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24896
24897#if GDB_SELF_TEST
24898 selftests::register_test ("dw2_expand_symtabs_matching",
24899 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24900 selftests::register_test ("dwarf2_find_containing_comp_unit",
24901 selftests::find_containing_comp_unit::run_test);
c62446b1 24902#endif
6502dd73 24903}
This page took 5.918259 seconds and 4 git commands to generate.